Created
March 22, 2018 17:12
-
-
Save prateek-parashar/77c3d2e6b400cd2250b918b7a372be2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copies a BMP file | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <math.h> | |
| #include "bmp.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| // ensure proper usage | |
| if (argc != 4) | |
| { | |
| fprintf(stderr, "Usage: resize infile outfile\n"); | |
| return 1; | |
| } | |
| //getting the value of n by converting it to float | |
| float n = atof(argv[1]); | |
| // remember filenames | |
| char *infile = argv[2]; | |
| char *outfile = argv[3]; | |
| // 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); | |
| long width1 = round(bi.biWidth); | |
| long height1 = round(abs(bi.biHeight)); | |
| int row1 = n; | |
| int col1 = n; | |
| // 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 padding for scanlines | |
| int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
| int rowsiz = (bi.biWidth) * sizeof(RGBTRIPLE) + padding; | |
| //scaling the dimensions for the new file | |
| bi.biWidth *= n; | |
| bi.biHeight *= n; | |
| //storing for later use | |
| long width = round(bi.biWidth); | |
| long height = round(abs(bi.biHeight)); | |
| //padding for the new file | |
| int lpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
| //calculation of the new file | |
| bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + lpadding) * abs(bi.biHeight); | |
| 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); | |
| if (roundf(n) == n) | |
| { | |
| // iterate over infile's scanlines | |
| for (int i = 0; i < height1; i++) | |
| { | |
| while (col1 > 0) | |
| { | |
| // iterate over pixels in scanline | |
| for (int j = 0; j < width1; j++) | |
| { | |
| // temporary storage | |
| RGBTRIPLE triple; | |
| // read RGB triple from infile | |
| fread(&triple, sizeof(RGBTRIPLE), 1, inptr); | |
| while (row1 > 0) | |
| { | |
| // write RGB triple to outfile | |
| fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); | |
| row1--; | |
| } | |
| row1 = n; | |
| } | |
| // skip over padding, if any | |
| fseek(inptr, padding, SEEK_CUR); | |
| //adding the new padding for resized file | |
| for (int k = 0; k < lpadding; k++) | |
| { | |
| fputc(0x00, outptr); | |
| } | |
| int offset0 = - ((labs(width1) * sizeof(RGBTRIPLE)) + padding); | |
| //moving the file stream to rewrite the row | |
| if (col1 > 1) | |
| { | |
| fseek(inptr, offset0, SEEK_CUR); | |
| } | |
| col1--; | |
| } | |
| col1 = n; | |
| } | |
| } | |
| else | |
| { | |
| // iterate over infile's scanlines | |
| for (int i = 0; i < height; i++) | |
| { | |
| // iterate over pixels in scanline | |
| for (int j = 0; j < width; j++) | |
| { | |
| // temporary storage | |
| RGBTRIPLE triple; | |
| int offset1 = 54 + (i / n) * rowsiz + (j / n) * sizeof(RGBTRIPLE); | |
| fseek(inptr, offset1, SEEK_SET); | |
| // read RGB triple from infile | |
| fread(&triple, sizeof(RGBTRIPLE), 1, inptr); | |
| // write RGB triple to outfile | |
| fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); | |
| } | |
| //determining offset for skipping padding | |
| int paddingof = 54 + (i + 1) * (bi.biWidth) * sizeof(RGBTRIPLE) ; | |
| fseek(inptr, paddingof, SEEK_SET); | |
| // skip over padding, if any | |
| fseek(inptr, padding, SEEK_CUR); | |
| //adding the new padding for resized file | |
| for (int k = 0; k < lpadding; k++) | |
| { | |
| fputc(0x00, outptr); | |
| } | |
| } | |
| } | |
| // close infile | |
| fclose(inptr); | |
| // close outfile | |
| fclose(outptr); | |
| // success | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment