Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "bmp.h"
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.     if (argc != 4)
  10.     {
  11.         printf("Usage: ./resize f infile outfile \n");
  12.         return 1;
  13.     }
  14.      // remember filenames
  15.     char *infile = argv[2];
  16.     char *outfile = argv[3];
  17.     float key = atof(argv[1]);
  18.  
  19.     // open input file
  20.     FILE *inptr = fopen(infile, "r");
  21.     if (inptr == NULL)
  22.     {
  23.         fprintf(stderr, "Could not open %s.\n", infile);
  24.         return 1;
  25.     }
  26.  
  27.     // open output file
  28.     FILE *outptr = fopen(outfile, "w");
  29.     if (outptr == NULL)
  30.     {
  31.         fclose(inptr);
  32.         fprintf(stderr, "Could not create %s.\n", outfile);
  33.         return 1;
  34.     }
  35.  
  36.     // read infile's BITMAPFILEHEADER
  37.     BITMAPFILEHEADER bf;
  38.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  39.  
  40.     // read infile's BITMAPINFOHEADER
  41.     BITMAPINFOHEADER bi;
  42.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  43.  
  44.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  45.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  46.         bi.biBitCount != 24 || bi.biCompression != 0)
  47.     {
  48.         fclose(outptr);
  49.         fclose(inptr);
  50.         fprintf(stderr, "Unsupported file format.\n");
  51.         return 4;
  52.     }
  53.     int hH = abs(bi.biHeight), hW = bi.biWidth;
  54.     bi.biHeight *= round(key);
  55.     bi.biWidth *= round(key);
  56.     int padding = (bi.biWidth * sizeof(RGBTRIPLE)) % 4;
  57.     bf.bfSize = 54 +   abs(bi.biHeight)* sizeof(RGBTRIPLE) *(bi.biWidth +  padding);
  58.     //printf("size: %i | width: %i height: %i | padding: %i\n", bf.bfSize, bi.biWidth, bi.biHeight, padding);
  59.     // write outfile's BITMAPFILEHEADER
  60.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  61.  
  62.     // write outfile's BITMAPINFOHEADER
  63.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  64.  
  65.     // iterate over infile's scanlines
  66.  
  67.     for (int i = 0, ptr_pos = 54, size_pad = (hW * sizeof(RGBTRIPLE)) % 4, rkey = round(key), biH = abs(bi.biHeight), igl = 0  ; i < hH; i++)
  68.     {
  69.  
  70.         for (int j = 0; j < hW; j++)
  71.         {
  72.  
  73.             RGBTRIPLE triple;
  74.  
  75.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  76.             for (int z = 0; z < rkey; z++)
  77.             {
  78.                 for (int y = 0; y < rkey; y++)
  79.                 {
  80.                      //printf("out: %ld  in: %ld pixel: %i%i%i ||  ", ftell(outptr), ftell(inptr), (int)triple.rgbtRed,(int)triple.rgbtGreen,(int)triple.rgbtBlue);
  81.                      fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  82.                 }
  83.                 //printf("\n");
  84.                 fseek(outptr,(sizeof(RGBTRIPLE)* bi.biWidth) - (key) * sizeof(RGBTRIPLE), SEEK_CUR);
  85.             }
  86.             fseek(outptr, sizeof(RGBTRIPLE)* (key - bi.biWidth* key)  , SEEK_CUR);
  87.        // printf("nline : %f pos: %ld\n", sizeof(RGBTRIPLE)* (key - bi.biWidth* key), ftell(outptr));
  88.         }
  89.         fseek(outptr,sizeof(RGBTRIPLE), SEEK_CUR);
  90.         // then add it back (to demonstrate how)
  91.         for (int k = 0; k < padding; k++)
  92.         {
  93.              RGBTRIPLE triple;
  94.              
  95.  
  96.              printf("out: %ld || in: %ld \n", ftell(outptr), ftell(inptr));
  97.              fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  98.              fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  99.         }
  100.         fseek(outptr, 54 + sizeof(RGBTRIPLE)* ( bi.biWidth* key) * (i + 1) , SEEK_SET);
  101.     }
  102.  
  103.     // close infile
  104.     fclose(inptr);
  105.  
  106.     // close outfile
  107.     fclose(outptr);
  108.  
  109.     // success
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement