Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copies a BMP file
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "bmp.h"
- int main(int argc, char *argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- fprintf(stderr, "Usage: ./resize n infile outfile\n");
- return 1;
- }
- // remember filenames
- char *N = argv[1];
- char *infile = argv[2];
- char *outfile = argv[3];
- // convert string to integer
- int n = atoi(N);
- if(n < 0 || n > 100)
- {
- fprintf(stderr, "Usage: ./resize n infile outfile\n");
- return 1;
- }
- // 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;
- }
- // determine new dimensions
- int oldWidth = bi.biWidth;
- int oldHeight = bi.biHeight;
- int newWidth = oldWidth * n;
- int newHeight = oldHeight * n;
- // determine padding for scanlines
- int inPadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int outPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- printf("oldwidth is %i\n", oldWidth);
- printf("newwidth is %i\n", newWidth);
- printf("oldheight is %i\n", oldHeight);
- printf("newheight is %i\n", newHeight);
- printf("oldpadding value is %i\n", inPadding);
- printf("newpadding value is %i\n ", outPadding);
- // reconfigure headers
- bi.biHeight = newHeight;
- bi.biWidth = newWidth;
- bi.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + outPadding) * abs(newHeight);
- bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // 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, biHeight = abs(oldHeight); i < biHeight; i++)
- {
- RGBTRIPLE buffer[newWidth];
- RGBTRIPLE* current = &buffer[0]; //current starts pointing to buffer[0]
- // iterate over pixels in scanline (Horizontally)
- for (int j = 0; j < oldWidth; j++)
- {
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write RGB triple to ARRAY
- for (int k = 0; k < n; k++)
- {
- *current = triple; // triple will store into *current address
- current++; // the pointer will point to the next address in buffer
- }
- }
- // Write scanline n times to output
- for (int l=0; l<n; l++){
- fwrite(&buffer, sizeof(buffer), 1, outptr); // Write array to outfile
- for (int k = 0; k < outPadding; k++)
- {
- fputc(0x00, outptr);
- }
- }
- // skip over padding, if any
- fseek(inptr, inPadding, SEEK_CUR);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement