Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Jody W Moore
- *
- * Computer Science 50
- * Problem Set 5
- *
- * Copies a BMP piece by piece, just because.
- */
- #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 infile outfile\n");
- return 1;
- }
- if (!(argv[1] > 0))
- {
- printf("please enter a positive number\n");
- }
- int n = 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;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- // local variables for calculations
- int inputHeight = abs(bi.biHeight);
- int outputHeight = bi.biHeight * n;
- int inputWidth = bi.biWidth;
- int outputWidth = inputWidth * n;
- // determine padding for scanlines
- int inputPadding = (4 - (inputWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int outputPadding = (4 - (outputWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // output variables
- int outputImageSize = (outputWidth * sizeof(RGBTRIPLE) + outputPadding) * abs(outputHeight);
- int outputBFSize = outputImageSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // 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;
- }
- // write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER
- if ( n < 100)
- {
- bf.bfSize = outputBFSize;
- bi.biWidth = outputWidth;
- bi.biHeight = outputHeight;
- bi.biSizeImage = outputImageSize;
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- }
- // for each row in the input
- for (int i = 0; i < inputHeight; i++)
- {
- // iterate over infile's scanlines repeat n times
- for (int j = 0; j < n; j++)
- {
- // iterate over pixels in the row
- for (int rPix = 0; rPix < inputWidth; rPix++)
- {
- // temporary storage
- RGBTRIPLE triple ;
- // read a pixel from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write n copies of pixel
- for (int wPix = 0; wPix < n ; wPix ++)
- {
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE),1, outptr);
- }
- }
- for (int m = 0; m < outputPadding; m++)
- {
- fputc(0x00, outptr);
- }
- int seeker = -1 * (inputWidth * sizeof(RGBTRIPLE));
- if ( j == n-1)
- {
- fseek(inptr, inputPadding, SEEK_CUR);
- }
- else
- {
- fseek(inptr,seeker,SEEK_CUR);
- }
- }
- // skip over input padding, if any
- // fseek(inptr, inputPadding, SEEK_CUR);
- // then adds output padding
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement