I am on pset4 and working on resize, less comfortable. I chose to follow the "rewrite" method but it seems that I am going wrong somewhere and I have no idea what to do. Any advice or help is most needed and welcome.
This is the loop part of my code. I have not edited my comments, so please ignore those. -
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
LONG newWidth = bi.biWidth * number;
LONG newHeight = abs(bi.biHeight) * number;
int newPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bii.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + newPadding) * abs(newHeight);
bff.bfSize = bii.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
RGBTRIPLE triples[newWidth];
// 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);
for (int m = 0; m < number; m++)
{
triples[m + (j * number)] = triple;
}
}
for (int l = 0; l < number; l++)
{
// write RGB triple to outfile
fwrite(triples, sizeof(triples), 1, outptr);
for (int k = 0; k < newPadding; k++)
{
// then add padding back (to demonstrate how)
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
EDIT My outptr keeps giving me an "invalid or unsupported image format" result. Is there something wrong with my header edits? -
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bff;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bii;
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;
}
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
LONG newWidth = bi.biWidth * number;
LONG newHeight = abs(bi.biHeight) * number;
int newPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bii.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + newPadding) * abs(newHeight);
bff.bfSize = bii.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bff, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bii, sizeof(BITMAPINFOHEADER), 1, outptr);