Skip to content

Instantly share code, notes, and snippets.

@chicklet-goldsmith
Created December 4, 2018 10:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chicklet-goldsmith/1eaad870c7ce39d5ee39e68767f2519b to your computer and use it in GitHub Desktop.
// 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: resize size(n) infile outfile\n");
return 1;
}
//convert char to an int
unsigned int n = atoi(argv[1]);
//ensure int is less than or equal to 100
if (n <= 0 || n > 100)
{
fprintf(stderr, "Usage: size(n) must be positive integer less than or equal to 100\n");
return 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, bfo;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bio;
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;
}
// change outfile's headers
bio = bi;
bfo = bf;
bio.biWidth = bi.biWidth *= n;
bio.biHeight = bi.biHeight *= n;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding_out = (4 - (bio.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bio.biSizeImage = ((sizeof(RGBTRIPLE) * bio.biWidth) + padding) * abs(bio.biHeight);
bfo.bfSize = bio.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's headers
fwrite(&bfo, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bio, sizeof(BITMAPINFOHEADER), 1, outptr);
//iterate over scanline
for (int scanline = 0, biHeight = abs(bi.biHeight); scanline < biHeight; scanline++)
{
//iterate over pixels
for (int pixels = 0; pixels < bi.biWidth; pixels++)
{
//repeat n times
for (int repeat = 0; repeat < n; repeat++)
{
//temporary storage
RGBTRIPLE triple;
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//write to outfield n times
for (int print = 0; print < n; print++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
//add padding
for(int print_padding = 0; print_padding < padding_out; print_padding++)
{
fputc(0x00, outptr);
}
}
//skip padding
fseek(inptr, padding, SEEK_CUR);
}
//repeat for next row
fseek(inptr, -((bi.biWidth * sizeof(RGBTRIPLE)) + padding), SEEK_CUR);
}
// 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