Advertisement
SKTLV

resize/more 23.12.19

Dec 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 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.     if (f >= 1)
  95.  
  96.     {
  97.         // iterate over infile's Height
  98.         for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  99.         {
  100.             for (int newLine = 0; newLine < f; newLine++) // resize every infile line f times
  101.             {
  102.                 // iterate over  all the pixels in the line in the infile
  103.                 for (int j = 0; j < bi.biWidth; j++)
  104.                 {
  105.  
  106.                     // temporary storage
  107.                     RGBTRIPLE triple;
  108.  
  109.                     // read a pixel at the position
  110.                     fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  111.  
  112.                     for (int t = 0; t < f; t++)  // write every pixel of infile f times to outfile
  113.                     {
  114.                         // write RGB triple to outfile
  115.                         fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  116.                     }
  117.  
  118.                 }  // end resizing horizontaly
  119.  
  120.                     if (newLine < (f-1))  //  set inptr pointer back to start of current line (if needed)
  121.                     {
  122.                         fseek(inptr,  - (bi.biWidth * 3), SEEK_CUR);
  123.                     }
  124.  
  125.             for (int k = 0; k < scaled_padding; k++)  // add padding to new outfile line (if needed)
  126.             {
  127.                 fputc(0x00, outptr);
  128.             }
  129.  
  130.         }   // finished resizing vertically f times one line of the infile
  131.  
  132.             // skip over padding of infile, if any
  133.             fseek(inptr, padding, SEEK_CUR);
  134.  
  135.  
  136.         }  //  end of iterating infile line
  137.     }   // end of f>=1
  138.  
  139.     else // if f<1   ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()-----f<1 f<1
  140.  
  141.     {
  142.         int newf = f*10;
  143.  
  144.         printf("scaled Height:");
  145.         printf("%i\n", abs(scaled_bi.biHeight));
  146.         printf("scaled widtht:");
  147.         printf("%i\n", abs(scaled_bi.biWidth));
  148.         printf("scaled biSizeImage:");
  149.         printf("%i\n", scaled_bi.biSizeImage);
  150.         printf("scaled bfSize:");
  151.         printf("%i\n", scaled_bf.bfSize);
  152.         printf("scaled padding:");
  153.         printf("%i\n", scaled_padding);
  154.         printf("scaled factor:");
  155.         printf("%i\n", newf);
  156.  
  157.  
  158.         for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  159.         {
  160.  
  161.             // iterate over  all the pixels in the line in the infile
  162.             for (int j = 0; j < bi.biWidth; j++)
  163.             {
  164.                 // temporary storage
  165.                 RGBTRIPLE triple;
  166.  
  167.                 // read a pixel of infile
  168.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  169.  
  170.  
  171.                 if (((j + 10) % (10 / newf)) == 0 && ((i + 10) % (10 / newf == 0)))  // write pixel only if its in 1/f position
  172.                 {
  173.                     // write RGB triple to outfile
  174.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  175.                 }
  176.  
  177.     }// end resizing horizontaly
  178.  
  179.  
  180.         fseek(inptr,  padding, SEEK_CUR); // set pointer of infile to skeep padding
  181.  
  182.  
  183.         for (int k = 0; k < scaled_padding; k++)  // add padding to new outfile line (if needed)
  184.         {
  185.             fputc(0x00, outptr);
  186.         }
  187.  
  188.     }  //  end of iterating infile line
  189.  
  190.  
  191.  
  192.  
  193.     }  //  end of iterating infile line
  194.  
  195.     // close infile
  196.     fclose(inptr);
  197.  
  198.     // close outfile
  199.     fclose(outptr);
  200.  
  201.  
  202.  
  203. }// main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement