Skip to content

Instantly share code, notes, and snippets.

@Ari-Roda
Created July 1, 2019 14:23
Show Gist options
  • Save Ari-Roda/ec5a75c7dc40417e2daadaca3a85f9aa to your computer and use it in GitHub Desktop.
Save Ari-Roda/ec5a75c7dc40417e2daadaca3a85f9aa to your computer and use it in GitHub Desktop.
// determine padding for scanlines
int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
float newbiWidth = newbi.biWidth; //make new width a float
newbiWidth *= nn; //get downsize number
newbiWidth = round(newbiWidth); //round downsize number
int inewbiWidth = newbiWidth;
int horizontal_pixels = round(bi.biWidth/newbiWidth);
float newbiHeight = newbi.biHeight; //make new height a float
newbiHeight *= nn; // get downsize number
newbiHeight = round(newbiHeight); // round downsize number
int inewbiHeight = newbiHeight; // make downsize number int
int vertical_pixels = round(bi.biHeight/inewbiHeight);
// determine padding for scanlines
int new_padding = (4 - (inewbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
newbi.biWidth = inewbiWidth;
newbi.biHeight = inewbiHeight;
newbi.biSizeImage = ((sizeof(RGBTRIPLE) * newbi.biWidth) + new_padding) * abs(newbi.biHeight);
newbf.bfSize = newbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&newbf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&newbi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = (abs(bi.biHeight)); i < biHeight; i++)
{
int realer_pixel = i % vertical_pixels; // row mod (infile height / new height) to choose row to write
if (realer_pixel == 0)
{
// iterate over pixels in scanline
for (int j = 0; j < ((bi.biWidth)); j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
int real_pixel = j % horizontal_pixels;
if (real_pixel == 0)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// put padding
for (int k = 0; k < new_padding; k++)
{
fputc(0x00, outptr);
}
// skip over padding, if any
fseek(inptr, old_padding, SEEK_CUR);
} else {
// skip over padding, if any
fseek(inptr, ((bi.biWidth) * 3) + old_padding, SEEK_CUR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment