Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * copy.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * Copies a BMP piece by piece, just because.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./copy infile outfile\n");
- return 1;
- }
- // remember filenames
- int factor = atoi(argv[1]);
- char* infile = argv[2];
- char* outfile = argv[3];
- if (factor <= 1 || factor >= 100)
- {
- printf("Please enter a factor between 1 and 100.\n");
- }
- // 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 BITMxAPINFOHEADER
- 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;
- }
- // write outfile's BITMAPFILEHEADER
- //fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- //fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // determine new height
- int oldWidth = bi.biWidth;
- int newWidth = oldWidth * factor;
- // determine new width
- int oldHeight = bi.biHeight;
- int newHeight = oldHeight * factor;
- // determine new padding
- //int oldPadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int newPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // image size
- bi.biSizeImage = (newWidth * sizeof(RGBTRIPLE) + abs(newPadding)) * abs(newHeight);
- bf.bfSize = (bi.biSizeImage) + 54;
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // iterate over infile's scanlines
- for (int i = 0, oldHeight = abs(newHeight); i < oldHeight; i++)
- {
- // iterate over scanline factor times
- for (int j = 0; j < factor; j++)
- {
- // iterate over pixels of scanline
- for (int k = 0; k < newWidth; k++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write RGB triple to outfile
- for (int l = 0; l < factor; l++)
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- // update padding
- for (int l = 0; l < newPadding; l++)
- fputc(0x00, outptr);
- // go back to the beginning of the line
- if (j < factor - 1)
- fseek(inptr, (-oldWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
- }
- // skip over padding, if any
- fseek(inptr, newPadding, SEEK_CUR);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement