Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*HELPING ME FROM SO? Thank you!
- Main appears at line 91. Before that, typedef and structs.
- 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).
- THANKS AGAIN!!*/
- /**
- * Resizes a BMP piece by piece, just because.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- /**
- * Common Data Types
- *
- * The data types in this section are essentially aliases for C/C++
- * primitive data types.
- *
- * Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
- * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
- */
- typedef uint8_t BYTE;
- typedef uint32_t DWORD;
- typedef int32_t LONG;
- typedef uint16_t WORD;
- /**
- * BITMAPFILEHEADER
- *
- * The BITMAPFILEHEADER structure contains information about the type, size,
- * and layout of a file that contains a DIB [device-independent bitmap].
- *
- * Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
- */
- typedef struct
- {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffBits;
- } __attribute__((__packed__))
- BITMAPFILEHEADER;
- /**
- * BITMAPINFOHEADER
- *
- * The BITMAPINFOHEADER structure contains information about the
- * dimensions and color format of a DIB [device-independent bitmap].
- *
- * Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx.
- */
- typedef struct
- {
- DWORD biSize;
- LONG biWidth;
- LONG biHeight;
- WORD biPlanes;
- WORD biBitCount;
- DWORD biCompression;
- DWORD biSizeImage;
- LONG biXPelsPerMeter;
- LONG biYPelsPerMeter;
- DWORD biClrUsed;
- DWORD biClrImportant;
- } __attribute__((__packed__))
- BITMAPINFOHEADER;
- /**
- * RGBTRIPLE
- *
- * This structure describes a color consisting of relative intensities of
- * red, green, and blue.
- *
- * Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
- */
- typedef struct
- {
- BYTE rgbtBlue;
- BYTE rgbtGreen;
- BYTE rgbtRed;
- } __attribute__((__packed__))
- RGBTRIPLE;
- //
- //
- //
- //
- //
- //
- //
- int main(int argc, char *argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- fprintf(stderr, "Usage: ./resize k infile outfile\n");
- return 1;
- }
- // remember filenames
- int factor = atoi(argv[1]);
- char *infile = argv[2];
- char *outfile = argv[3];
- // open input file
- FILE *inptr = fopen(infile, "r");
- // open output file
- FILE *outptr = fopen(outfile, "w");
- // read infile's BITMAPFILEHEADER
- BITMAPFILEHEADER bf;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- //increasing height and width by factor
- bi.biWidth *= factor;
- bi.biHeight *= factor;
- //calculating padding from new width
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
- bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- //DECLARATION OF A VARIABLE TO STORE WHAT LATER WILL BE THE ORIGIN OF THE PROBLEM WE ARE DEALING HEREIN.
- int temporary = -bi.biWidth/factor;
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(bi.biHeight); i < (biHeight / factor); i++)
- {
- // to resize vertically
- for (int l = 0; l < factor; l++)
- {
- //iterate over pixels in scanlines
- for (int j = 0; j < (bi.biWidth / factor); j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), factor, outptr);
- }
- // then add it back (to demonstrate how)
- for (int k = 0; k < padding; k++)
- {
- fputc(0x00, outptr);
- }
- fseek(inptr, 0, SEEK_CUR);
- //IF YOU ARE HELPING ME FROM SO (thank you), WITHIN THE FSEEK() YOU HAVE ALL THREE POSSIBLE OPTIONS:
- //(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.
- // ALL THREE RETURN DIFFERENT OUTPUTS
- if (l < factor - 1)
- {
- fseek(inptr, temporary/*-3*//*-(bi.biWidth / factor)*/, SEEK_CUR);
- }
- }
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement