Advertisement
SKTLV

resize/more 21.12.19

Dec 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 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 HEADRS --------------
  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 lines
  95.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  96.     {
  97.  
  98.         for (int newLine = 0; newLine < f; newLine++) // resize every infile line f times
  99.  
  100.         {
  101.             // temporary storage
  102.                 RGBTRIPLE triple;
  103.                 int ptStLocation = ftell(inptr);
  104.                 printf("%i\n", ptStLocation);
  105.  
  106.             // iterate over  all the pixels in the line in the infile
  107.             for (int j = 0; j < bi.biWidth; j++)
  108.             {
  109.  
  110.                 // read a pixel at the position
  111.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  112.  
  113.  
  114.  
  115.                 for (int t = 0; t < f; t++)  // write every pixel of infile f times to outfile
  116.                 {
  117.                 // write RGB triple to outfile
  118.                 fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  119.                 }
  120.  
  121. // end resizing horizontaly
  122.  
  123.                 // adding padding to new outfile line (if needed)
  124.             }
  125.  
  126.             for (int k = 0; k < scaled_padding; k++)
  127.             {
  128.                 fputc(0x00, outptr);
  129.             }
  130.  
  131.                 // reseting the pointer to beginning of infile line with fsesk
  132.  
  133.             //fseek(inptr, -(bi.biWidth * 3 - 1), SEEK_CUR);
  134.  
  135.             ptEndLocation = ftell(inptr);
  136.             printf("%i\n", ptEndLocation);
  137.             printf("---\n");
  138.             fseek(inptr, - (ptEndLocation - ptStLocation), SEEK_CUR);
  139.  
  140.             // finished resizing verticall the line
  141.         }
  142.  
  143.         printf("---------NEW LINE\n");
  144.  
  145.         // before scanning new line, need to skeep padding of infile line
  146.  
  147.         // skip over padding of infile, if any
  148.            fseek(inptr, padding, SEEK_CUR);
  149.  
  150.     }  //  end of iterating infile line
  151.  
  152.     // close infile
  153.     fclose(inptr);
  154.  
  155.     // close outfile
  156.     fclose(outptr);
  157.  
  158.  
  159.  
  160.     // success
  161.     return 0;
  162.  
  163. }// main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement