Advertisement
rupek1995

resize.c almost working

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