Advertisement
SKTLV

Untitled

Dec 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "bmp.h"
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     if (argc != 4)  // make sure input has 3 arguments
  9.     {
  10.         printf("Usage: ./resize f infile outfile\n");
  11.         return (1);
  12.     }
  13.  
  14.     float f = atof(argv[1]);  //   The factor by which new BMP will be resized
  15.     if (f <= 0 || f > 100)
  16.     {
  17.         printf("Scaling must be > 0 and <= 100\n");
  18.         return (1);
  19.     }
  20.  
  21.     // remember filenames
  22.     char *infile = argv[2];
  23.     char *outfile = argv[3];
  24.  
  25.     // open input file
  26.     FILE *inptr = fopen(infile, "r");
  27.     if (inptr == NULL)
  28.     {
  29.         fprintf(stderr, "Could not open %s.\n", infile);
  30.         return 2;
  31.     }
  32.  
  33.     // open output file
  34.     FILE *outptr = fopen(outfile, "w");
  35.     if (outptr == NULL)
  36.     {
  37.         fclose(inptr);
  38.         fprintf(stderr, "Could not create %s.\n", outfile);
  39.         return 3;
  40.     }
  41.  
  42.     // read infile's BITMAPFILEHEADER
  43.     BITMAPFILEHEADER bf;
  44.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  45.  
  46.     // read infile's BITMAPINFOHEADER
  47.     BITMAPINFOHEADER bi;
  48.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  49.  
  50.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  51.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  52.         bi.biBitCount != 24 || bi.biCompression != 0)
  53.     {
  54.         fclose(outptr);
  55.         fclose(inptr);
  56.         fprintf(stderr, "Unsupported file format.\n");
  57.         return 4;
  58.     }
  59.  
  60.     //  Declare new BITMAPFILEHEADER and BITMAPINFOHEADER
  61.  
  62.     BITMAPFILEHEADER scaled_bf = bf;
  63.     BITMAPINFOHEADER scaled_bi = bi;
  64.  
  65.     // Scale the outptr file with the new Headers
  66.  
  67.     scaled_bi.biHeight = bi.biHeight * f;
  68.     scaled_bi.biWidth = bi.biWidth * f;
  69.  
  70.     // determine padding for infile and outfile
  71.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  72.     int scaled_padding = (4 - (scaled_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  73.  
  74.  
  75.     //// printing for test only
  76.     printf("%i\n", padding);
  77.     printf("%i\n", scaled_padding);
  78.     printf("%i\n", bi.biWidth);
  79.     printf("%i\n", scaled_bi.biWidth);
  80.  
  81.  
  82.  
  83.     // Scale the biSizeImage of outfile
  84.     scaled_bi.biSizeImage = ((sizeof(RGBTRIPLE) * scaled_bi.biWidth) + scaled_padding) * (bi.biHeight);
  85.  
  86.     // Scale the bfSize of outfile
  87.     scaled_bf.bfSize = scaled_bi.biSizeImage + 54;
  88.  
  89.     // -------- FINISHED SCALING THE HEADRS --------------
  90.  
  91.     // Write the outfile new HEADERS/ FILE & INFO
  92.  
  93.     // write outfile's BITMAPFILEHEADER
  94.     fwrite(&scaled_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  95.  
  96.     // write outfile's BITMAPINFOHEADER
  97.     fwrite(&scaled_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  98.  
  99.    //  ----------   Start iterating the infile and write to outfile   ------------------
  100.  
  101.     // iterate over infile's lines
  102.     for (int i = 0; i < bi.biHeight; i++)
  103.     {
  104.         // execute every line f times
  105.  
  106.         for (int newLines = 0; newLines < f; newLines++)
  107.  
  108.         {
  109.             // temporary storage
  110.             RGBTRIPLE triple;
  111.  
  112.              // iterate over  all the pixels in the line in the infile
  113.             for (int j = 0; j < bi.biWidth; j++)
  114.             {
  115.  
  116.             // read a pixel at the position
  117.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  118.  
  119.                 for (int t = 0; t < f; t++)  // write every pixel of infile f times to outfile
  120.                 {
  121.                 // write RGB triple to outfile
  122.                 fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  123.                 }
  124.  
  125.             } // end of reading and writing single line from infile to outfile
  126.  
  127.             //reset the pointer of the infile to the beginning of the line
  128.             fseek(inptr, (-bi.biWidth*3), SEEK_CUR);  // movinv back from end is in bytes hence (minus) -bi.width*3
  129.  
  130.             // skip over padding, if any
  131.             fseek(inptr, scaled_padding, SEEK_CUR);
  132.  
  133.             // then add it back (to demonstrate how)
  134.             for (int k = 0; k < scaled_padding; k++)
  135.             {
  136.                 fputc(0x00, outptr);
  137.             }
  138.  
  139.         } // end of writing new line f times to outfile
  140.  
  141.  
  142.     }  //  end of iterating infile line
  143.  
  144.     // close infile
  145.     fclose(inptr);
  146.  
  147.     // close outfile
  148.     fclose(outptr);
  149.  
  150.     // success
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement