Advertisement
SKTLV

resize/more 21.12.19 - last version

Dec 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 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.     int ptEndLocation;
  9.  
  10.     if (argc != 4)  // make sure input has 3 arguments
  11.     {
  12.         printf("Usage: ./resize f infile outfile\n");
  13.         return (1);
  14.     }
  15.  
  16.     float f = atof(argv[1]);  //   The factor by which new BMP will be resized
  17.     if (f <= 0 || f > 100)
  18.     {
  19.         printf("Scaling must be > 0 and <= 100\n");
  20.         return (1);
  21.     }
  22.  
  23.     // remember filenames
  24.     char *infile = argv[2];
  25.     char *outfile = argv[3];
  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.  
  52.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  53.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  54.         bi.biBitCount != 24 || bi.biCompression != 0)
  55.     {
  56.         fclose(outptr);
  57.         fclose(inptr);
  58.         fprintf(stderr, "Unsupported file format.\n");
  59.         return 4;
  60.     }
  61.  
  62.     //  Declare new BITMAPFILEHEADER and BITMAPINFOHEADER
  63.  
  64.     BITMAPFILEHEADER scaled_bf = bf;
  65.     BITMAPINFOHEADER scaled_bi = bi;
  66.  
  67.     // Scale the outptr file with the new Headers
  68.  
  69.     scaled_bi.biHeight = bi.biHeight * f;
  70.     scaled_bi.biWidth = bi.biWidth * f;
  71.  
  72.     // determine padding for infile and outfile
  73.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  74.     int scaled_padding = (4 - (scaled_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  75.  
  76.     // Scale the biSizeImage of outfile
  77.     scaled_bi.biSizeImage = ((sizeof(RGBTRIPLE) * scaled_bi.biWidth) + scaled_padding) * abs(scaled_bi.biHeight);
  78.  
  79.     // Scale the bfSize of outfile
  80.     scaled_bf.bfSize = scaled_bi.biSizeImage + 54;
  81.  
  82.     // -------- FINISHED SCALING THE HEADERS --------------
  83.  
  84.     // Write the outfile new HEADERS/ FILE & INFO
  85.  
  86.     // write outfile's BITMAPFILEHEADER
  87.     fwrite(&scaled_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  88.  
  89.     // write outfile's BITMAPINFOHEADER
  90.     fwrite(&scaled_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  91.  
  92.    //  ----------   Start iterating the infile and write to outfile   ------------------
  93.  
  94.     // iterate over infile's Height
  95.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  96.     {
  97.         for (int newLine = 0; newLine < f; newLine++) // resize every infile line f times
  98.         {
  99.             // iterate over  all the pixels in the line in the infile
  100.             for (int j = 0; j < bi.biWidth; j++)
  101.             {
  102.  
  103.                 // temporary storage
  104.                 RGBTRIPLE triple;
  105.  
  106.                 // read a pixel at the position
  107.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  108.  
  109.                 for (int t = 0; t < f; t++)  // write every pixel of infile f times to outfile
  110.                 {
  111.                 // write RGB triple to outfile
  112.                 fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  113.                 }
  114.  
  115.             }  // end resizing horizontaly
  116.  
  117.              if (newLine < (f-1))  //  set inptr pointer back to start of current line (if needed)
  118.             {
  119.                fseek(inptr,  - (bi.biWidth * 3), SEEK_CUR);
  120.             }
  121.  
  122.             for (int k = 0; k < scaled_padding; k++)  // add padding to new outfile line (if needed)
  123.             {
  124.                 fputc(0x00, outptr);
  125.             }
  126.  
  127.         }  // finished resizing vertically f times one line of the infile
  128.  
  129.  
  130.     }  //  end of iterating infile line
  131.  
  132.     // close infile
  133.     fclose(inptr);
  134.  
  135.     // close outfile
  136.     fclose(outptr);
  137.  
  138.  
  139.  
  140.     // success
  141.     return 0;
  142.  
  143. }// main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement