I'm working on Pset 4 re-size.
I've got the header files working correctly (as tested by using the "peek" function, and by using a factor of 1 - my programme creates perfect copies of any of the .bmp images supplied)
As suggested in Zamyla's walkthrough, I'm now trying to get the horizontal re-size working before I worry about the vertical re-sizing.
I'm pasting the relevant parts of my code below:
// include, declare main, etc.
int n = atoi(argv[1]);
// open input file and output file
// read input file's headers
// update headers for output file
output_bi.biWidth = n * bi.biWidth;
// Calculate padding
int input_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int output_padding = (4 - (output_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// calculate output image size
// write output file's headers
Now we get to the part that I think is causing my problem:
// Now iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) // Iterates the number of times equal to the field "bi.biHeight"
// (i.e. the number of lines in the original image)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++) // Iterates the number of times the original image is wide
{
// temporary storage
RGBTRIPLE triple; // Creates a struct of format RGBTRIPLE in a temporary buffer called "triple"
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr); // Reads something the size of an RGBTRIPLE (3 bytes) from the infile *once*
// and stores it in the temporary RGBTRIPLE called "triple"
// write RGB triple to outfile, resizing as we go
fwrite(&triple, sizeof(RGBTRIPLE), n, outptr); // Writes the 3 bytes stored in "triple" *n times* to the outfile
// (where n is the factor entered by the user - see line 30)
}
// Add padding to outfile as needed
for (int k = 0; k < output_padding; k++) // Iterates over the number of units of padding required in the output...
{
fputc(0x00, outptr); // ...and writes "0x00" to the outfile at the end of each scanline
}
// skip over padding in infile, if any
fseek(inptr, input_padding, SEEK_CUR); // In the file pointed to by inptr (i.e the infile), starting from the current "cursor position",
// move forward by "input padding" (which is 3 bytes for "small.bmp")
}
// TO DO - then need to re-size VERTICALLY
OK, so my intent with this code is that it will:
Look at each vertical line of the input file (image) in turn
For each line, read each RGBTRIPLE in turn and copy this to the buffer
Write that RGBTRIPLE n times from the buffer to the output file
Write output_padding's number of bytes of padding the output file
Go back to the input file, find the cursor at its current position, and skip forward the number of bytes of padding in the input file (i.e. to the beginning of the next line)
Start again at the next vertical line of the input file...
This doesn't work, as you can see from the outputs that I'm getting with a factor of 2:
What am I doing wrong?
Many thanks in advance for your help.