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[])
- {
- int ptEndLocation;
- 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 = 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 HEADERS --------------
- // 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 Height
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- for (int newLine = 0; newLine < f; newLine++) // resize every infile line f times
- {
- // set inpts pointer to start of current line
- fseek(inptr, 54 + (newLine + i) * (bi.biWidth * 3 + padding), SEEK_SET);
- // iterate over all the pixels in the line in the infile
- for (int j = 0; j < bi.biWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // 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 resizing horizontaly
- for (int k = 0; k < scaled_padding; k++) // adding padding to new outfile line (if needed)
- {
- fputc(0x00, outptr);
- }
- } // finished resizing vertically f times one line of the infile
- } // end of iterating infile line
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }// main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement