Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Computer Science 50
- * Problem Set 5
- *
- * Copies a BMP piece by piece, and creates a another bmp with requested dimensions.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./resize scale infile outfile\n");
- return 1;
- }
- // receive scale value and check for validity
- int n = atoi(argv[1]);
- if ( n < 1 || n > 100)
- {
- printf("Enter a value for scale between 1 and 100");
- }
- // remember filenames
- char* infile = argv[2];
- char* outfile = argv[3];
- // open input file
- FILE* inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- printf("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;
- }
- // determine padding for scanlines for infile
- int paddingInfile = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // updating BITMAPINFOHEADER height and width varibles inorder to resize
- //int initialHeight = abs(bi.biHeight); // to be used for modifying offset
- int initialWidth = bi.biWidth;
- bi.biHeight *= n;
- bi.biWidth *= n;
- // determine padding for scanlines for outfile
- int paddingOutfile = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // update BITMAPINFOHEADER biSizeImage and BITMAPFILEHEADER bfSize
- bi.biSizeImage = ((bi.biWidth)* abs(bi.biHeight) * sizeof(RGBTRIPLE)) + paddingOutfile;
- bf.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // offset variable used to shift fseek
- int offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- // iterate over pixels in scanline
- for (int j = 0; j < initialWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write RGB triple to outfile. Iterate n times to copy the pixels (n-1) times horizontally.
- for(int k = 0; k < n; k++)
- {
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- // then add it back
- for (int k = 0; k < paddingOutfile; k++)
- {
- fputc(0x00, outptr);
- }
- ////////////////////////TEST///////////////////////////
- //printf("i : %i\n",i);
- //printf("offset : %i\n",offset);
- //Modifying offset inorder to change fseek SEEK_SET psition
- if(i%n == (n-1))
- {
- offset += ((initialWidth) * sizeof(RGBTRIPLE)) + paddingInfile;
- }
- // skip over padding, if any
- fseek(inptr, offset, SEEK_SET);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement