Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int sizeOf();
- int main(int argc, char *argv[])
- {
- if (argc != 4) // make sure input has 3 arguments
- {
- printf("Usage: ./resize f infile outfile\n");
- return (1);
- }
- double f = atof(argv[1]);
- if (f <= 0 || f > 100)
- {
- printf("First argument must be 0-100\n");
- return (1);
- }
- // remember filenames
- char *infile = argv[2];
- char *outfile = argv[3];
- // end of user input
- //----------------------------------------------------------
- // open input file
- FILE *inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- fclose(inptr);
- fprintf(stderr, "Could not open %s.\n", infile);
- return 2;
- }
- // open output file
- FILE *outptr = fopen(outfile, "w");
- if (outptr == NULL)
- {
- fclose(inptr);
- fprintf(stderr, "Could not create %s.\n", outfile);
- return 3;
- }
- // read infile's BITMAPFILEHEADER
- BITMAPFILEHEADER bf;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
- if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
- bi.biBitCount != 24 || bi.biCompression != 0)
- {
- fclose(outptr);
- fclose(inptr);
- fprintf(stderr, "Unsupported file format.\n");
- return 4;
- }
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ here we start manipulating the image ++++++++++++++
- // determine padding for scanlines - general formula
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // Declare new HEADER FILE & INFO for the scaled file and initialize it with old picture value
- // so we can manipulate it
- BITMAPFILEHEADER scaled_bf = bf;
- BITMAPINFOHEADER scaled_bi = bi;
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scaling the BitMapInfoHeader
- scaled_bi.biHeight *= f;
- scaled_bi.biWidth *= f;
- // after resizing bi.biwidth, we update the padding formula with the new bi.biwidth
- int scaled_Padding = (4 - (scaled_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // now that we have new Padding, we can change the sizeimage in the BITMAPINFOHEADER
- scaled_bi.biSizeImage = ((sizeof(RGBTRIPLE) * scaled_bi.biWidth) + scaled_Padding) * abs(scaled_bi.biHeight);
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Scaling the BITMAPFILEHEADER
- scaled_bf.bfSize = scaled_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Writing the new Headers of the Scaled File
- RGBTRIPLE scan[scaled_bi.biWidth];
- // write outfile's BITMAPFILEHEADER
- // determine padding for scanlines - general formula
- fwrite(&scaled_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&scaled_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ resizing the pixels
- // iterate over ROW (the original picture)
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- // iterate over pixels in Lines of original Picture
- for (int j = 0; j < bi.biWidth; 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), 1, outptr); // Here it duplicate every pixel f times
- }
- // skip over padding, if any
- fseek(inptr, padding, SEEK_CUR);
- // then add it back (to demonstrate how)
- for (int k = 0; k < scaled_Padding; k++) // here it adds the new padding if needed
- {
- fputc(0x00, outptr);
- }
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement