Skip to content

Instantly share code, notes, and snippets.

@prateek-parashar
Last active March 20, 2018 16:03
Show Gist options
  • Select an option

  • Save prateek-parashar/e9c39bdb781adc6f01971914d94bf93c to your computer and use it in GitHub Desktop.

Select an option

Save prateek-parashar/e9c39bdb781adc6f01971914d94bf93c to your computer and use it in GitHub Desktop.
C program to resize a .bmp file
// Copies 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: resize infile outfile\n");
return 1;
}
//getting the value of n
int n = atoi(argv[1]);
int row = n;
int col = n;
// 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 width = bi.biWidth;
long height = abs(bi.biHeight);
// 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;
bi.biWidth *= n;
bi.biHeight *= n;
int lpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
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);
int offset1 = -((labs(width) * sizeof(RGBTRIPLE)) + padding);
// iterate over infile's scanlines
for (int i = 0; i < height; i++)
{
while (col > 0)
{
// iterate over pixels in scanline
for (int j = 0; j < width; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
while(row > 0)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
row--;
}
row = 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);
}
if (col > 1)
{
fseek(inptr, offset1, SEEK_CUR);
}
col--;
}
col = n;
}
// 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