0

I don't exactly understand the principle of recopy method used in resize pset4. Can someone explain me without giving the actual solution. Here is my code so far. After running the code I recive a distorted image.I don't understand also the fseek function. From my understanding of fseek it must turn the cursor back to the beginning of the file so it must pass through pixels I wrote and the padding right ?

// iterate over infile's scanlines
for (int i = 0, old_biHeight = abs(old_bi.biHeight); i < old_biHeight; i++)
{
    for (int j = 0; j < n - 1; j++)
    {
        // iterate over pixels in scanline

        for (int k = 0; k < old_bi.biWidth; k++)
        {

            // temporary storage
            RGBTRIPLE triple;


            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);


            // write RGB triple to infile
            for (int z = 0; z < n; z++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
            //padding
            for (int z = 0; z < old_padding; z++)
            {
                fputc(0x00, outptr);
            }
            //Send infile cursor back
            fseek(inptr, bi.biWidth, SEEK_SET);
        }
    }

    for (int k = 0; k < old_bi.biWidth; k++)
    {
         RGBTRIPLE triple;


        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        for (int z = 0; z < n; z++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

        for (int y = 0; y < new_padding; y++)
        {
        fputc(0x00, outptr);
        }
    }
}

EDIT: please also tell me what's wrong in my code. The code now resizes images but the image is full of randomly colors. Is it a problem with padding ?

EDIT NEW CODE :

    // iterate over infile's scanlines
for (int i = 0, old_biHeight = abs(old_bi.biHeight); i < old_biHeight; i++)
{
    for (int j = 0; j < n - 1; j++)
    {
        // iterate over pixels in scanline

        for (int k = 0; k < old_bi.biWidth; k++)
        {

            // temporary storage
            RGBTRIPLE triple;


            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);


            // write RGB triple to infile
            for (int z = 0; z < n; z++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
            //padding
            for (int z = 0; z < new_padding; z++)
            {
                fputc(0x00, outptr);
            }
            //Send infile cursor back
            fseek(inptr, new_padding, SEEK_CUR);
        }
    }

    for (int k = 0; k < old_bi.biWidth; k++)
    {
         RGBTRIPLE triple;


        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        for (int z = 0; z < n; z++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

        for (int y = 0; y < new_padding; y++)
        {
        fputc(0x00, outptr);
        }
    }
}

// skip over padding, if any
fseek(inptr, old_padding, SEEK_CUR);

1 Answer 1

0

Principle of recopy method:
Say you are doubling the size of the image (n = 2). The default code in copy.c will write only 1 pixel - but now you need 2. So, you need to "recopy" the pixel another time. If n were 3, say, you'd need to "recopy" the pixel 2 more times.

That's across - now you also need to "recopy" the entire new row the equivalent amount of times.

fseek:
fseek is for moving around in the original file.

As you move through that file reading the original bytes, a "pointer" is keeping track of where you are in the file. Read a byte, and the pointer moves forward past that byte, ready to read the next byte.

Once you've read the original pixels for a row, you (and that pointer) will have reached the padding. However, the original padding is not likely the same as what's needed for the new file. Therefore, you need to move the pointer (using fseek) to skip over the padding in the original file in order to get to the next row's pixels for "recopy" to the new file.

5
  • Why do you think that is not working ? Is it a problem with padding and fseek ? So do i need to fseek the entire line of pixels ?
    – Criticalll
    Aug 27, 2019 at 14:51
  • One thing I see is that you're writing the old padding into the new file. You should only be writing the new padding in. As for your fseek, think about where the file pointer is at any given time. Ensure you're calculating the right number of bytes to move the pointer to get it where you want it to be.
    – leanne
    Aug 27, 2019 at 15:03
  • I modifiend the old padding to new paxxing. I think that i need to skip through new_padding only right ? Now i have the actual image on first row of the BMP but at the bottom i have lines of red and white. I couldn't find the problem. Is still the padding and fseek? Or should i look for something else ?
    – Criticalll
    Aug 27, 2019 at 15:51
  • fseek(inptr, old_bi.biWidth * sizeof(RGBTRIPLE) + old_padding, SEEK_CUR);Still not working.
    – Criticalll
    Aug 27, 2019 at 16:15
  • Per your original question, I explained the recopy principle and how fseek works. You stated "...explain me without giving the actual solution." If you think I've done that, please click the check mark to select this as the answer. For further troubleshooting of your code, though, please ask a new, more specific question, related to what you may think is causing the problem.
    – leanne
    Aug 27, 2019 at 16:48

You must log in to answer this question.

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