Advertisement
SKTLV

resize/more

Dec 14th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 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.     double 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.     // write outfile's BITMAPFILEHEADER
  71.     fwrite(&scaled_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  72.  
  73.     // write outfile's BITMAPINFOHEADER
  74.     fwrite(&scaled_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  75.  
  76.     // determine padding for infile
  77.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  78.  
  79.     // determine padding for outfile
  80.     int scaled_padding = (4 - (scaled_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  81.  
  82.     // iterate over infile's scanlines
  83.     for (int i = 0, biHeight = abs(scaled_bi.biHeight); i < biHeight; i++)
  84.     {
  85.         // iterate over pixels in scanline
  86.         for (int j = 0; j < bi.biWidth; j++)
  87.         {
  88.             // temporary storage
  89.             RGBTRIPLE triple;
  90.  
  91.             // read RGB triple from infile
  92.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  93.  
  94.             for (int t = 0; t < f; t++)
  95.             {
  96.             // write RGB triple to outfile
  97.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  98.             }
  99.         }
  100.  
  101.         // skip over padding, if any
  102.         fseek(inptr, padding, SEEK_CUR);
  103.  
  104.         // then add it back (to demonstrate how)
  105.         for (int k = 0; k < scaled_padding; k++)
  106.         {
  107.             fputc(0x00, outptr);
  108.         }
  109.     }
  110.  
  111.     // close infile
  112.     fclose(inptr);
  113.  
  114.     // close outfile
  115.     fclose(outptr);
  116.  
  117.     // success
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement