Advertisement
cs-lazaro

MCVE - resize

Sep 7th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.98 KB | None | 0 0
  1. /*HELPING ME FROM SO? 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.  
  120.  
  121.     //increasing height and width by factor
  122.     bi.biWidth *= factor;
  123.     bi.biHeight *= factor;
  124.  
  125.  
  126.  
  127.  
  128.     //calculating padding from new width
  129.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  130.  
  131.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  132.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  133.     // write outfile's BITMAPFILEHEADER
  134.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  135.     // write outfile's BITMAPINFOHEADER
  136.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  137.  
  138.  
  139.  
  140.  
  141.     //DECLARATION OF A VARIABLE TO STORE WHAT LATER WILL BE THE ORIGIN OF THE PROBLEM WE ARE DEALING HEREIN.
  142.     int temporary = -bi.biWidth/factor;
  143.  
  144.  
  145.  
  146.  
  147.     // iterate over infile's scanlines
  148.     for (int i = 0, biHeight = abs(bi.biHeight); i < (biHeight / factor); i++)
  149.     {
  150.         // to resize vertically
  151.         for (int l = 0; l < factor; l++)
  152.         {
  153.             //iterate over pixels in scanlines
  154.             for (int j = 0; j < (bi.biWidth / factor); j++)
  155.             {
  156.                 // temporary storage
  157.                 RGBTRIPLE triple;
  158.  
  159.                 // read RGB triple from infile
  160.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  161.  
  162.  
  163.                 // write RGB triple to outfile
  164.                 fwrite(&triple, sizeof(RGBTRIPLE), factor, outptr);
  165.             }
  166.  
  167.             // then add it back (to demonstrate how)
  168.             for (int k = 0; k < padding; k++)
  169.             {
  170.                 fputc(0x00, outptr);
  171.             }
  172.  
  173.             fseek(inptr, 0, SEEK_CUR);
  174.  
  175.  
  176.             //IF YOU ARE HELPING ME FROM SO (thank you), WITHIN THE FSEEK() YOU HAVE ALL THREE POSSIBLE OPTIONS:
  177.             //(1)DOING THE MATH OPERATION, (2)WRITING DIRECTLY THE NUMBER -3, OR (3)WRITING THE VARIABLE (temporary)WHERE THE RESULT OF THE OPERATION HAS BEEN STORED.
  178.             // ALL THREE RETURN DIFFERENT OUTPUTS
  179.             if (l < factor - 1)
  180.             {
  181.                 fseek(inptr, temporary/*-3*//*-(bi.biWidth / factor)*/, SEEK_CUR);
  182.  
  183.             }
  184.         }
  185.     }
  186.     // close infile
  187.     fclose(inptr);
  188.     // close outfile
  189.     fclose(outptr);
  190.     // success
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement