Advertisement
rupek1995

resize.c new draft

Feb 16th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. /**
  2.  * Resizes BMP images n times (basically enlarges images)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string.h>
  9. #include <sys/wait.h>
  10.  
  11. #include "bmp.h"
  12.  
  13. int copy(char *infile, char *outfile);
  14.  
  15. int main (int argc, char *argv[])
  16. {
  17.   // check if correct number of arguments used
  18.   if (argc != 4)
  19.   {
  20.     fprintf(stderr, "Usage: ./resize value infile outfile\n");
  21.     return 1;
  22.   }
  23.  
  24.   // keep track of argument inputs
  25.   double value = atof(argv[1]);
  26.   char *infile = argv[2];
  27.   char *outfile = argv[3];
  28.  
  29.   // check if provided value is in range
  30.   if (value > 100.0 || value < 0.0)
  31.   {
  32.     fprintf(stderr, "Value out of range. \n");
  33.     return 2;
  34.   }
  35.  
  36.   // open file input
  37.   FILE *inptr = fopen(infile, "r");
  38.   if (inptr == NULL)
  39.   {
  40.     fprintf(stderr, "Could not open the file %s. \n", infile);
  41.     return 3;
  42.   }
  43.  
  44.   // open output file
  45.   FILE *outptr = fopen(outfile, "w");
  46.   if (outptr == NULL)
  47.   {
  48.     fprintf(stderr, "Can not create file %s. \n", outfile);
  49.     return 4;
  50.   }
  51.  
  52.   // if rounded resize value is 1 - just copy the image
  53.   if (ceil(value) == 1)
  54.   {
  55.     return (copy(infile, outfile));
  56.   }
  57.  
  58.   // read infile's BITMAPFILEHEADER
  59.   BITMAPFILEHEADER bf;
  60.   fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  61.  
  62.   // read infile's BITMAPINFOHEADER
  63.   BITMAPINFOHEADER bi;
  64.   fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  65.  
  66.   // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  67.   if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  68.       bi.biBitCount != 24 || bi.biCompression != 0)
  69.   {
  70.       fclose(outptr);
  71.       fclose(inptr);
  72.       fprintf(stderr, "Unsupported file format.\n");
  73.       return 4;
  74.   }
  75.  
  76.   // determine original's padding for scanlines
  77.   int og_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  78.  
  79.   // resize bitmap by given value
  80.   bi.biWidth *= value;
  81.   bi.biHeight *= value;
  82.  
  83.   // determine resized padding
  84.   int resized_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  85.  
  86.   // update the rest of parameters
  87.   bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + resized_padding) * abs(bi.biHeight);
  88.   bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  89.  
  90.   // write BITMAPFILEHEADER to output file
  91.   fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  92.  
  93.   // write BITMAPINFOHEADER to output file
  94.   fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  95.  
  96.   // iterate over infile's scanlines
  97.   for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  98.   {
  99.     // resize scanlines vertically (basically copy them n times)
  100.     for (int v_row = 0; v_row < value; v_row++)
  101.     {
  102.       // iterate over pixels in scanline
  103.       for (int j = 0; j < bi.biWidth; j++)
  104.       {
  105.           // temporary storage
  106.           RGBTRIPLE triple;
  107.  
  108.           // read RGB triple from infile
  109.           fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  110.  
  111.           // resize horizontally (write each pixel n times)
  112.           for (int h_row = 0; h_row < value; h_row++)
  113.           {
  114.             // write RGB triple to outfile (write original pixel counter times)
  115.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  116.           }
  117.       }
  118.  
  119.       // skip over padding, if any
  120.       fseek(inptr, og_padding, SEEK_CUR);
  121.  
  122.       // then add it back (to demonstrate how)
  123.       for (int k = 0; k < resized_padding; k++)
  124.       {
  125.           fputc(0x00, outptr);
  126.       }
  127.  
  128.     }
  129.   }
  130.  
  131.   // close infile
  132.   fclose(inptr);
  133.  
  134.   // close outfile
  135.   fclose(outptr);
  136.  
  137.   // success
  138.   return 0;
  139. }
  140.  
  141. /**
  142. * Runs external copy program to copy the image
  143. **/
  144. int copy(char *infile, char *outfile)
  145. {
  146.   // calculate length of the command, including \0 terminator
  147.   int command_length = strlen("./copy ") + strlen(infile) + 1 + strlen(outfile) + 1;
  148.   char command[command_length];
  149.  
  150.   // assign whole command to variable
  151.   snprintf(command, sizeof(command), "./copy %s %s", infile, outfile);
  152.   // launch command in terminal, WEXITSTATUS returns exit code from child comm.
  153.  
  154.   int status = WEXITSTATUS(system(command));
  155.   // return status from the copy program
  156.   return status;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement