1

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:

Output from resizing small.bmp

Output from resizing smiley.bmp

What am I doing wrong?

Many thanks in advance for your help.

2
fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);

will write a chunk of memory containing n pixels, while you need to write the very same triple, n times. You'll have to use another loop for that.

| improve this answer | |
  • Aha! What my code actually requires is to look at the location given by &triple, write n [i.e. 2] elements of data, each sizeof(RGBTRIPLE) bytes [i.e. 3], to the output file. But of course, the location given by &triple only has 1 3-byte element of data. So when I ask it to look for another 3-bytes, it goes outside the memory that has been allocated, and I get junk back. Thanks Blauelf - the loop took 5 seconds to write and works! And I learned another trick - literally write out the function's formula and parse-in my variables - it does what it says, which may not be what I think it says! – Matt Friend Feb 10 '17 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .