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 ------------------
- if (f >= 1)
- {
- // 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
- {
- // 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
- if (newLine < (f-1)) // set inptr pointer back to start of current line (if needed)
- {
- fseek(inptr, - (bi.biWidth * 3), SEEK_CUR);
- }
- for (int k = 0; k < scaled_padding; k++) // add padding to new outfile line (if needed)
- {
- fputc(0x00, outptr);
- }
- } // finished resizing vertically f times one line of the infile
- // skip over padding of infile, if any
- fseek(inptr, padding, SEEK_CUR);
- } // end of iterating infile line
- } // end of f>=1
- else // if f<1 ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()-----f<1 f<1
- {
- int newf = f*10;
- printf("scaled Height:");
- printf("%i\n", abs(scaled_bi.biHeight));
- printf("scaled widtht:");
- printf("%i\n", abs(scaled_bi.biWidth));
- printf("scaled biSizeImage:");
- printf("%i\n", scaled_bi.biSizeImage);
- printf("scaled bfSize:");
- printf("%i\n", scaled_bf.bfSize);
- printf("scaled padding:");
- printf("%i\n", scaled_padding);
- printf("scaled factor:");
- printf("%i\n", newf);
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- // 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 of infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- if (((j + 10) % (10 / newf)) == 0 && ((i + 10) % (10 / newf == 0))) // write pixel only if its in 1/f position
- {
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }// end resizing horizontaly
- fseek(inptr, padding, SEEK_CUR); // set pointer of infile to skeep padding
- for (int k = 0; k < scaled_padding; k++) // add padding to new outfile line (if needed)
- {
- fputc(0x00, outptr);
- }
- } // end of iterating infile line
- } // end of iterating infile line
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- }// main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement