Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Resizes a bmp file
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- fprintf(stderr, "Usage: number copy infile outfile\n");
- return 1;
- }
- // remember filenames
- int n = atoi(argv[1]);
- char *infile = argv[2];
- char *outfile = argv[3];
- if (n < 0 || n > 100) //making sure n is in specification
- {
- printf("positive integer please");
- 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);
- int input_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
- // 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
- bi.biWidth *= n;
- bi.biHeight *= n;
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
- int paddingOutput = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //setting the padding
- bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + paddingOutput) * abs(bi.biHeight);
- bf.bfSize = (bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));// setting the file size
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // determine padding for scanlines
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(bi.biHeight); i < bi.biHeight; i++)
- {
- // iterate over pixels in scanline
- for (int j = 0; j < bi.biWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi.biHeight);
- bf.bfSize = (bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
- // read RGB triple from infile
- fread(&triple, sizeof(BITMAPFILEHEADER), 1, inptr);
- // write RGB triple to outfile
- fwrite(&triple, sizeof(BITMAPFILEHEADER), 1, outptr);
- }
- // resizing the image horizontally
- for (long offset = 0; offset < bi.biSizeImage; offset++)
- {
- if (offset == 0) //making the file pointer 0
- {
- fseek(outptr, -1 * (offset), SEEK_SET);
- }
- if (SEEK_CUR == 0x00)
- {
- fseek(outptr, 1, SEEK_CUR);
- }
- fwrite(&outptr, 1, n, outptr);//writing each byte 1 by 1 using the for loop
- //checking if file pointer is at end of the width
- for (int c = 0; c < bi.biWidth; c++)
- {
- if (bi.biSizeImage % bi.biWidth == 0) //checking each row
- {
- for(int p = 0; p < padding; p++) //actually printing the padding
- {
- fputc(0x00,outptr);
- }
- }
- }
- }
- //resizing the image vertically
- for(int b = 0; b < n; b++)
- {
- fseek(outptr, bi.biWidth, SEEK_END);
- fwrite(&outptr, 1, n, outptr);//writing each byte 1 by 1 using the for loop
- }
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement