Advertisement
rupek1995

resize.c old draft

Feb 16th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.41 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.   bi.biWidth *= value;
  80.   bi.biHeight *= value;
  81.  
  82.   // determine resized padding
  83.   int resized_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  84.  
  85.   // update the rest of parameters
  86.   bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + resized_padding) * abs(bi.biHeight);
  87.   bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  88.  
  89.   // write BITMAPFILEHEADER to output file
  90.   fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  91.  
  92.   // write BITMAPINFOHEADER to output file
  93.   fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  94.  
  95.   // allocate memory for RGBTRIPLE array
  96.   RGBTRIPLE *RGB_array = malloc(sizeof(RGBTRIPLE));
  97.  
  98.   // iterate over infile's scanlines
  99.   for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  100.   {
  101.     int counter = 0;
  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.           // store RGB values in an array
  112.           for (int k = 0, RGB_size = sizeof(RGBTRIPLE); k < RGB_size; k++)
  113.           {
  114.             RGB_array[k] = triple;
  115.           }
  116.  
  117.           // write RGB triple to outfile (write original pixel counter times)
  118.           fwrite(&RGB_array[counter], sizeof(RGBTRIPLE), 1, outptr);
  119.  
  120.           // if current pixel has ben written n times - go to next pixel
  121.           if (j % sizeof(RGB_array))
  122.           {
  123.             counter++;
  124.             if (counter < sizeof(RGB_array))
  125.             {
  126.               counter = 0;
  127.             }
  128.           }
  129.       }
  130.  
  131.       // skip over padding, if any
  132.       fseek(inptr, og_padding, SEEK_CUR);
  133.  
  134.       // then add it back (to demonstrate how)
  135.       for (int k = 0; k < resized_padding; k++)
  136.       {
  137.           fseek(outptr, resized_padding, bi.biWidth);
  138.           fputc(0x00, outptr);
  139.       }
  140.   }
  141.  
  142.   // close infile
  143.   fclose(inptr);
  144.  
  145.   // close outfile
  146.   fclose(outptr);
  147.  
  148.   // success
  149.   return 0;
  150. }
  151.  
  152. /**
  153. * Runs external copy program to copy the image
  154. **/
  155. int copy(char *infile, char *outfile)
  156. {
  157.   // calculate length of the command, including \0 terminator
  158.   int command_length = strlen("./copy ") + strlen(infile) + 1 + strlen(outfile) + 1;
  159.   char command[command_length];
  160.  
  161.   // assign whole command to variable
  162.   snprintf(command, sizeof(command), "./copy %s %s", infile, outfile);
  163.   // launch command in terminal, WEXITSTATUS returns exit code from child comm.
  164.  
  165.   int status = WEXITSTATUS(system(command));
  166.   // return status from the copy program
  167.   return status;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement