Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. // Resizes a bmp file
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "bmp.h"
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.     // ensure proper usage
  10.     if (argc != 4)
  11.     {
  12.         fprintf(stderr, "Usage: number copy infile outfile\n");
  13.         return 1;
  14.     }
  15.      // remember filenames
  16.  
  17.     int n = atoi(argv[1]);
  18.     char *infile = argv[2];
  19.     char *outfile = argv[3];
  20.  
  21.     if (n < 0 || n > 100) //making sure n is in specification
  22.     {
  23.         printf("positive integer please");
  24.         return 1;
  25.     }
  26.  
  27.     // open input file
  28.     FILE *inptr = fopen(infile, "r");
  29.     if (inptr == NULL)
  30.     {
  31.         fprintf(stderr, "Could not open %s.\n", infile);
  32.         return 2;
  33.     }
  34.  
  35.     // open output file
  36.     FILE *outptr = fopen(outfile, "w");
  37.     if (outptr == NULL)
  38.     {
  39.         fclose(inptr);
  40.         fprintf(stderr, "Could not create %s.\n", outfile);
  41.         return 3;
  42.     }
  43.  
  44.     // read infile's BITMAPFILEHEADER
  45.     BITMAPFILEHEADER bf;
  46.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  47.  
  48.     // read infile's BITMAPINFOHEADER
  49.     BITMAPINFOHEADER bi;  
  50.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  51.     int input_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
  52.  
  53.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  54.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  55.         bi.biBitCount != 24 || bi.biCompression != 0)
  56.     {
  57.         fclose(outptr);
  58.         fclose(inptr);
  59.         fprintf(stderr, "Unsupported file format.\n");
  60.         return 4;
  61.     }
  62.  
  63.     // write outfile's BITMAPFILEHEADER
  64.     bi.biWidth *= n;
  65.     bi.biHeight *= n;
  66.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
  67.     int paddingOutput = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
  68.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + paddingOutput) * abs(bi.biHeight);
  69.     bf.bfSize = (bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));// setting the file size
  70.  
  71.  
  72.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  73.  
  74.     // write outfile's BITMAPINFOHEADER
  75.  
  76.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  77.  
  78.     // determine padding for scanlines
  79.  
  80.  
  81.     // iterate over infile's scanlines
  82.     for (int i = 0, biHeight = abs(bi.biHeight); i < bi.biHeight; i++)
  83.     {
  84.         // iterate over pixels in scanline
  85.         for (int j = 0; j < bi.biWidth; j++)
  86.         {
  87.             // temporary storage
  88.             RGBTRIPLE triple;
  89.             bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi.biHeight);
  90.             bf.bfSize = (bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
  91.  
  92.             // read RGB triple from infile
  93.             fread(&triple, sizeof(BITMAPFILEHEADER), 1, inptr);
  94.  
  95.             // write RGB triple to outfile
  96.             fwrite(&triple, sizeof(BITMAPFILEHEADER), 1, outptr);
  97.         }
  98.         // resizing the image horizontally
  99.  
  100.         for (long offset = 0; offset < bi.biSizeImage; offset++)
  101.         {
  102.             if (offset == 0) //making the file pointer 0
  103.             {
  104.                 fseek(outptr, -1 * (offset), SEEK_SET);
  105.             }  
  106.             if (SEEK_CUR == 0x00)
  107.             {
  108.                 fseek(outptr, 1, SEEK_CUR);
  109.             }
  110.             fwrite(&outptr, 1, n, outptr);//writing each byte 1 by 1 using the for loop
  111.         //checking if file pointer is at end of the width
  112.         for (int c = 0; c < bi.biWidth; c++)
  113.         {  
  114.             if (bi.biSizeImage % bi.biWidth == 0) //checking each row
  115.             {
  116.                 for(int p = 0; p < padding; p++) //actually printing the padding
  117.                 {
  118.                     fputc(0x00,outptr);
  119.                 }
  120.             }
  121.         }
  122.         }
  123.  
  124.         //resizing the image vertically
  125.  
  126.         for(int b = 0; b < n; b++)
  127.         {
  128.             fseek(outptr, bi.biWidth, SEEK_END);
  129.             fwrite(&outptr, 1, n, outptr);//writing each byte 1 by 1 using the for loop
  130.         }
  131.  
  132.     }
  133.  
  134.     // close infile
  135.     fclose(inptr);
  136.  
  137.     // close outfile
  138.     fclose(outptr);
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement