Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char *argv[])
- {
- if (argc != 4) // make sure input has 3 arguments
- {
- printf("Usage: ./resize f infile outfile\n");
- return (1);
- }
- float f = atof(argv[1]); // The factor by which new BMP will be resized
- if (f <= 0 || f > 100)
- {
- printf("Scaling must be > 0 and <= 100\n");
- return (1);
- }
- // remember filenames
- char *infile = argv[2];
- char *outfile = argv[3];
- // open input file
- FILE *inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- 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;
- }
- // Declare new BITMAPFILEHEADER and BITMAPINFOHEADER
- BITMAPFILEHEADER scaled_bf = bf;
- BITMAPINFOHEADER scaled_bi = bi;
- // Scale the outptr file with the new Headers
- scaled_bi.biHeight = abs(bi.biHeight) * f;
- scaled_bi.biWidth = bi.biWidth * f;
- // determine padding for infile and outfile
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int scaled_padding = (4 - (scaled_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // Scale the biSizeImage of outfile
- scaled_bi.biSizeImage = ((sizeof(RGBTRIPLE) * scaled_bi.biWidth) + scaled_padding) * abs(scaled_bi.biHeight);
- // Scale the bfSize of outfile
- scaled_bf.bfSize = scaled_bi.biSizeImage + 54;
- // -------- FINISHED SCALING THE HEADRS --------------
- // Write the outfile new HEADERS/ FILE & INFO
- // write outfile's BITMAPFILEHEADER
- fwrite(&scaled_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&scaled_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // ---------- Start iterating the infile and write to outfile ------------------
- // iterate over infile's lines
- for (int i = 0; i < abs(bi.biHeight); i++)
- {
- // execute every line f times
- for (int newLines = 0; newLines < f; newLines++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // iterate over all the pixels in the line in the infile
- for (int j = 0; j < bi.biWidth; j++)
- {
- // read a pixel at the position
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- for (int t = 0; t < f; t++) // write every pixel of infile f times to outfile
- {
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- } // end of reading and writing single line from infile to outfile
- //reset the pointer of the infile to the beginning of the line
- fseek(inptr, (-bi.biWidth*3), SEEK_CUR); // movinv back from end is in bytes hence (minus) -bi.width*3
- // then add it back (to demonstrate how)
- for (int k = 0; k < scaled_padding; k++)
- {
- fputc(0x00, outptr);
- }
- } // end of writing new line f times to outfile
- // skip over padding, if any
- fseek(inptr, padding, SEEK_CUR);
- } // end of iterating infile line
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- //printing for control
- printf("1/ infile biWidth:");
- printf("%i\n", bi.biWidth);
- printf(" outfile biWidth:");
- printf("%i\n", scaled_bi.biWidth);
- printf("2/ infile biHeight:");
- printf("%i\n", bi.biHeight);
- printf(" outfile biHeight:");
- printf("%i\n", scaled_bi.biHeight);
- printf("3/ infile biSizeImage:");
- printf("%i\n", bi.biSizeImage);
- printf(" outfile biSizeImage:");
- printf("%i\n", scaled_bi.biSizeImage);
- printf("4/ infile bfsize:");
- printf("%i\n", bf.bfSize);
- printf(" outfile bfsize:");
- printf("%i\n", scaled_bf.bfSize);
- printf("5/ infile Padding:");
- printf("%i\n", padding);
- printf(" outfile Padding:");
- printf("%i\n", scaled_padding);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement