Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * resize a bmp
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./copy n(scale) infile outfile\n");
- return 1;
- }
- if (atoi(argv[1])<0 || atoi(argv[1])>100)
- {
- printf("please provide a positive integer smaller or equal to 100\n");
- return 5;
- }
- // define scale
- int scale = atoi(argv[1]);
- // 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_inptr, bf_outptr;
- fread(&bf_inptr, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi_inptr, bi_outptr;
- fread(&bi_inptr, sizeof(BITMAPINFOHEADER), 1, inptr);
- // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
- if (bf_inptr.bfType != 0x4d42 || bf_inptr.bfOffBits != 54 || bi_inptr.biSize != 40 ||
- bi_inptr.biBitCount != 24 || bi_inptr.biCompression != 0)
- {
- fclose(outptr);
- fclose(inptr);
- fprintf(stderr, "Unsupported file format.\n");
- return 4;
- }
- int *inptr_padding = malloc(sizeof(int));
- *inptr_padding = (4 - (bi_inptr.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // updating the outfile's BITMAPINFOHEADER
- bi_outptr.biWidth = bi_inptr.biWidth * scale;
- bi_outptr.biHeight = bi_inptr.biHeight * scale;
- int padding = (4 - (bi_outptr.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- bi_outptr.biSizeImage = ((bi_outptr.biWidth * sizeof(RGBTRIPLE)) + padding) * bi_outptr.biHeight;
- // updating the outfile's BITMAPFILEHEADER
- bf_outptr.bfSize = bi_outptr.biSizeImage + 54;
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf_outptr, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi_outptr, sizeof(BITMAPINFOHEADER), 1, outptr);
- // determine padding for scanlines
- // int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(bi_inptr.biHeight); i < biHeight; i++)
- {
- for (int m = 0; m<scale; m++)
- {
- // iterate over pixels in scanline
- for (int j = 0; j < bi_inptr.biWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- for (int k = 0; k<scale;k++)
- {
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- // padding in outfile
- for (int l = 0; l < padding; l++)
- {
- fputc(0x00, outptr);
- }
- if (m<(scale-1))
- {
- fseek(inptr, -(sizeof(RGBTRIPLE)*bi_inptr.biWidth), SEEK_CUR);
- }
- }
- // skip over padding, if any
- fseek(inptr, *inptr_padding, SEEK_CUR);
- // then add it back (to demonstrate how)
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // free inptr_padding
- free(inptr_padding);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement