Advertisement
cs-lazaro

short resize - r/cs50

Sep 8th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*HELPING ME FROM r/cs50? Thank you!
  2. Main appears at line 91. Before that, typedef and structs.
  3. Some important lines are: 138 and 173! (also in 145 the real action begins, all the other stuff from main until there are declarations, opening/reading files...that suff).
  4. THANKS AGAIN!!*/
  5.  
  6.  
  7. /**
  8.  * Resizes a BMP piece by piece, just because.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdint.h>
  14.  
  15. /**
  16.  * Common Data Types
  17.  *
  18.  * The data types in this section are essentially aliases for C/C++
  19.  * primitive data types.
  20.  *
  21.  * Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
  22.  * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
  23.  */
  24. typedef uint8_t  BYTE;
  25. typedef uint32_t DWORD;
  26. typedef int32_t  LONG;
  27. typedef uint16_t WORD;
  28.  
  29. /**
  30.  * BITMAPFILEHEADER
  31.  *
  32.  * The BITMAPFILEHEADER structure contains information about the type, size,
  33.  * and layout of a file that contains a DIB [device-independent bitmap].
  34.  *
  35.  * Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
  36.  */
  37. typedef struct
  38. {
  39.     WORD bfType;
  40.     DWORD bfSize;
  41.     WORD bfReserved1;
  42.     WORD bfReserved2;
  43.     DWORD bfOffBits;
  44. } __attribute__((__packed__))
  45. BITMAPFILEHEADER;
  46.  
  47. /**
  48.  * BITMAPINFOHEADER
  49.  *
  50.  * The BITMAPINFOHEADER structure contains information about the
  51.  * dimensions and color format of a DIB [device-independent bitmap].
  52.  *
  53.  * Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx.
  54.  */
  55. typedef struct
  56. {
  57.     DWORD biSize;
  58.     LONG biWidth;
  59.     LONG biHeight;
  60.     WORD biPlanes;
  61.     WORD biBitCount;
  62.     DWORD biCompression;
  63.     DWORD biSizeImage;
  64.     LONG biXPelsPerMeter;
  65.     LONG biYPelsPerMeter;
  66.     DWORD biClrUsed;
  67.     DWORD biClrImportant;
  68. } __attribute__((__packed__))
  69. BITMAPINFOHEADER;
  70.  
  71. /**
  72.  * RGBTRIPLE
  73.  *
  74.  * This structure describes a color consisting of relative intensities of
  75.  * red, green, and blue.
  76.  *
  77.  * Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
  78.  */
  79. typedef struct
  80. {
  81.     BYTE rgbtBlue;
  82.     BYTE rgbtGreen;
  83.     BYTE rgbtRed;
  84. } __attribute__((__packed__))
  85. RGBTRIPLE;
  86. //
  87. //
  88. //
  89. //
  90. //
  91. //
  92. //
  93. int main(int argc, char *argv[])
  94. {
  95.     // ensure proper usage
  96.     if (argc != 4)
  97.     {
  98.         fprintf(stderr, "Usage: ./resize k infile outfile\n");
  99.         return 1;
  100.     }
  101.  
  102.     // remember filenames
  103.     int factor = atoi(argv[1]);
  104.     char *infile = argv[2];
  105.     char *outfile = argv[3];
  106.  
  107.     // open input file
  108.     FILE *inptr = fopen(infile, "r");
  109.     // open output file
  110.     FILE *outptr = fopen(outfile, "w");
  111.     // read infile's BITMAPFILEHEADER
  112.     BITMAPFILEHEADER bf;
  113.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  114.     // read infile's BITMAPINFOHEADER
  115.     BITMAPINFOHEADER bi;
  116.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  117.  
  118.  
  119.     //increasing height and width by factor
  120.     bi.biWidth *= factor;
  121.     bi.biHeight *= factor;
  122.  
  123.  
  124.     //calculating padding from new width
  125.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  126.  
  127.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  128.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  129.     // write outfile's BITMAPFILEHEADER
  130.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  131.     // write outfile's BITMAPINFOHEADER
  132.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  133.  
  134.  
  135.  
  136.  
  137.     // iterate over infile's scanlines
  138.     for (int i = 0, biHeight = abs(bi.biHeight); i < (biHeight / factor); i++)
  139.     {
  140.         // to resize vertically
  141.         for (int l = 0; l < factor; l++)
  142.         {
  143.             //iterate over pixels in scanlines
  144.             for (int j = 0; j < (bi.biWidth / factor); j++)
  145.             {
  146.                 // temporary storage
  147.                 RGBTRIPLE triple;
  148.  
  149.                 // read RGB triple from infile
  150.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  151.  
  152.  
  153.                 // write RGB triple to outfile
  154.                 fwrite(&triple, sizeof(RGBTRIPLE), factor, outptr);
  155.             }
  156.  
  157.             // then add it back (to demonstrate how)
  158.             for (int k = 0; k < padding; k++)
  159.             {
  160.                 fputc(0x00, outptr);
  161.             }
  162.  
  163.             fseek(inptr, 0, SEEK_CUR);
  164.  
  165.  
  166.             //in the offset down here, choosing between -3 and the division (which equals -3) returns different .bmp outputs.
  167.             //set the cursor at the beginning of the line to rewrite it, for vertical resizing purposes.
  168.             if (l < factor - 1)
  169.             {
  170.                 fseek(inptr, /*-3*//*-(bi.biWidth / factor)*/, SEEK_CUR);
  171.  
  172.             }
  173.         }
  174.     }
  175.     // close infile
  176.     fclose(inptr);
  177.     // close outfile
  178.     fclose(outptr);
  179.     // success
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement