ahmedraza

resize.c

Nov 23rd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "bmp.h"
  4. int main(int argc, char* argv[])
  5. {
  6.     // ensure proper usage
  7.     if (argc != 4)
  8.     {
  9.         printf("Usage: ./resize n infile outfile\n");
  10.         return 1;
  11.     }
  12.  
  13.     // remember filenames
  14.     int n = atoi (argv[1]);
  15.     char* infile = argv[2];
  16.     char* outfile = argv[3];
  17.     if (n<=0||n>100)
  18.     {
  19.         printf("n must be between 1 and 100");
  20.         return 5;
  21.     }
  22.     // open input file
  23.     FILE* inptr = fopen(infile, "r");
  24.     if (inptr == NULL)
  25.     {
  26.         printf("Could not open %s.\n", infile);
  27.         return 2;
  28.     }
  29.  
  30.     // open output file
  31.     FILE* outptr = fopen(outfile, "w");
  32.     if (outptr == NULL)
  33.     {
  34.         fclose(inptr);
  35.         fprintf(stderr, "Could not create %s.\n", outfile);
  36.         return 3;
  37.     }
  38.  
  39.     // read infile's BITMAPFILEHEADER
  40.     BITMAPFILEHEADER bf,new_bf;
  41.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  42.  
  43.     // read infile's BITMAPINFOHEADER
  44.     BITMAPINFOHEADER bi, new_bi;
  45.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  46.    
  47.  
  48.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  49.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  50.         bi.biBitCount != 24 || bi.biCompression != 0)
  51.     {
  52.         fclose(outptr);
  53.         fclose(inptr);
  54.         fprintf(stderr, "Unsupported file format.\n");
  55.         return 4;
  56.     }
  57.     new_bf=bf;
  58.     new_bi=bi;
  59.     new_bi.biWidth=bi.biWidth*n;
  60.     new_bi.biHeight=bi.biHeight*n;
  61.     // determine padding for scanlines
  62.     int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  63.     int new_padding = (4-(new_bi.biWidth * sizeof(RGBTRIPLE))%4)%4;
  64.     //size of new image.
  65.     new_bi.biSizeImage = (bi.biWidth* sizeof (RGBTRIPLE) + new_padding) * (abs(new_bi.biHeight));
  66.     new_bf.bfSize = (bf.bfSize - bi.biSizeImage + new_bi.biSizeImage);
  67.  
  68.     // write outfile's BITMAPFILEHEADER
  69.     fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  70.    
  71.     // write outfile's BITMAPINFOHEADER
  72.     fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  73.  
  74.    
  75.     // iterate over infile's scanlines
  76.    
  77.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  78.     {
  79.          //iterate n times.
  80.          for (int m=0;m<n;m++)
  81.          {
  82.         // iterate over pixels in scanline
  83.         for (int j = 0; j < bi.biWidth; j++)
  84.             {
  85.             // temporary storage
  86.             RGBTRIPLE triple;
  87.  
  88.             // read RGB triple from infile
  89.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  90.         for (int f=0;f<n;f++)
  91.             {
  92.             // write RGB triple to outfile
  93.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  94.             }
  95.             // then add it back (to demonstrate how)
  96.             for (int k = 0; k < new_padding; k++)
  97.             {
  98.             fputc(0x00, outptr);
  99.             //start from scale line again
  100.             if (m<n-1)
  101.             {
  102.                 fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
  103.             }
  104.             }
  105.              
  106.  
  107.            
  108.             }
  109.             fseek(inptr, padding, SEEK_CUR);
  110.            
  111.          }
  112.          
  113.     }
  114.  
  115.     // close infile
  116.     fclose(inptr);
  117.  
  118.     // close outfile
  119.     fclose(outptr);
  120.  
  121.     // that's all folks
  122.     return 0;
  123. }
Add Comment
Please, Sign In to add comment