I'm having problems with my vertical re-size in Pset4. I've got the horizontal re-size working - go me! But I'm having problems with the vertical re-size. I'm trying to implement a version of what Zamyla calls the "re-copy" method. I've pasted my code below, from the point after where I've written the header files to the output file successfully):
// 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 no. of
// pixels the original image is "high" = the number of lines in the image)
{
for (int ctr = 0; ctr < (n-1); ctr++) // does the following n-1 times
{
// **************************************************************************************************************
// execute horizontal re-sizing, including adding padding required to each line in the outfile - "horiz (n);"
{
// 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
for (int k = 0; k < n; k++) // Iterates n times (where n is the factor entered by the user - see line 30)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); // Writes the 3 bytes stored in "triple" to the outfile
}
}
// Add padding to outfile as needed
for (int l = 0; l < output_padding; l++) // 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 back to beginning of input line
fseek(inptr, -((bi.biWidth * sizeof(RGBTRIPLE))-1), SEEK_CUR); // In the file pointed to by inptr (i.e the infile), starting from the current "cursor position",
// move *backward* by bi.biWidth (i.e. the number of pixels in each row in the infile, before padding) * the size of each RGBTRIPLE
}
// repeat the horizontal write code ["horiz()" function]
{
// iterate over pixels in scanline
for (int m = 0; m < bi.biWidth; m++) // 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
for (int o = 0; o < n; o++) // Iterates n times (where n is the factor entered by the user - see line 30)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); // Writes the 3 bytes stored in "triple" to the outfile
}
}
// Add padding to outfile as needed
for (int p = 0; p < output_padding; p++) // 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")
}
The code compiles, but when I run it with a factor of 3, I get the following error: " resize.c:147:26: runtime error: negation of 8 cannot be represented in type 'unsigned long' " Line 147 is the line where I skip back to the beginning of the infile line. I understand why an unsigned long can't take a value of (-8). What I don't understand is - why does fseek require an unsigned long? Can I change this, somehow? And if not, how do I go backwards?
NB - the code inside the two //***** lines is my horizontal re-size code. I wrote it like this because I thought I might be able to extract it as a function and just call that here (where it's repeated (n-1) times), and then below - which would be more elegant and functionally disaggregated. As an aside, that's tricky as it refers to other variables that have been defined in main() (such as the biwidth etc.) - so these are local to main and not available to a separate function. Any thoughts on that would be appreciated, too!
Many thanks in advance for your help. Matt