Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created September 22, 2017 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedTaha98/405c1403e674171e9e6f0320f3dfeaad to your computer and use it in GitHub Desktop.
Save MohamedTaha98/405c1403e674171e9e6f0320f3dfeaad to your computer and use it in GitHub Desktop.
/**
* Copies a BMP piece by piece, just because.
*/
#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 n infile outfile\n");
return 1;
}
// remember filenames
int n = atoi(argv[1]);
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);
// 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;
}
// remember infile's width & length
BITMAPINFOHEADER infile_bi;
infile_bi.biWidth = bi.biWidth;
infile_bi.biHeight = abs(bi.biHeight);
// determine infile padding for scanlines
int infile_padding = (4 - (infile_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine outfile width
bi.biWidth *= n;
// determine outfile padding for scalines
int outfile_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//update outfile headers
bi.biHeight *= n;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + outfile_padding) * 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);
// iterate over infile's scanlines
for (int i = 0; i < infile_bi.biHeight; i++)
{
// array of RGB tripples
RGBTRIPLE RGB_array[bi.biWidth];
// index of RGB_array
int index = 0;
// iterate over pixels in scanline
for (int j = 0; j < infile_bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple into RGB_array n times
for (int k = 0; k < n; k++)
{
RGB_array[index] = triple;
index++;
// another sanity check xD
if (index >= bi.biWidth)
break;
}
}
for (int l = 0; l < n; l++)
{
// write RGB_array to outfile n times
fwrite(RGB_array, sizeof(RGBTRIPLE), bi.biWidth, outptr);
// then add the outfile padding back (to demonstrate how)
for (int m = 0; m < outfile_padding; m++)
{
fputc(0x00, outptr);
}
}
// skip over infile padding, if any
fseek(inptr, infile_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