Skip to content

Instantly share code, notes, and snippets.

@jonenzl
Last active August 20, 2022 13:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonenzl/4b0ae68a503a84ab555cbeba6ceb2a45 to your computer and use it in GitHub Desktop.
Save jonenzl/4b0ae68a503a84ab555cbeba6ceb2a45 to your computer and use it in GitHub Desktop.
My implementation of CS50x Pset4 - Resize
/****************************************************************************
* resize.c
*
* HarvardX - CS50
* Problem Set 4
*
* Resize a BMP image by a factor of n
***************************************************************************/
#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 factor infile outfile\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
int factor = atoi(argv[1]);
// check to make sure the factor is an integer between 1 and 100
if (factor < 1 || factor > 100)
{
fprintf(stderr, "The factor must be a number between 1 and 100.\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, bfResize;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfResize = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, biResize;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biResize = bi;
// 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 the dimensions for the resized image
biResize.biWidth = bi.biWidth * factor;
biResize.biHeight = bi.biHeight * factor;
// determine the padding for the original image and the resized image
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int resizePadding = (4 - (biResize.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine the size of the resized image
biResize.biSizeImage = ((sizeof(RGBTRIPLE) * biResize.biWidth) + resizePadding) * abs(biResize.biHeight);
bfResize.bfSize = biResize.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bfResize, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&biResize, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// write each line factor times
for (int j = 0; j < factor; j++)
{
// iterate over pixels in scanline
for (int k = 0; k < bi.biWidth; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile factor times
for (int l = 0; l < factor; l++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding in infile
fseek(inptr, padding, SEEK_CUR);
// then add it to outfile
for (int m = 0; m < resizePadding; m++)
{
fputc(0x00, outptr);
}
// return to start of scanline
fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR);
}
fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE) + padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
/****************************************************************************
* bmp.h
*
* BMP-related data types based on Microsoft's own.
***************************************************************************/
#include <stdint.h>
/**
* Common Data Types
*
* The data types in this section are essentially aliases for C/C++
* primitive data types.
*
* Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
*/
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
*/
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;
/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx.
*/
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} __attribute__((__packed__))
BITMAPINFOHEADER;
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment