0

Please someone help me with my code. I think that problem is the fseek function. I don't know how to calculate the number of bytes needed to be skipped. I obtained wierd images with distorted collors.

 // 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, old_bi.biWidth * sizeof(RGBTRIPLE) + old_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);
        }
    }
}
2
  • Does your code work with an n value of 1?
    – leanne
    Aug 27 '19 at 18:29
  • yes it gives me the correct image.
    – Criticalll
    Aug 27 '19 at 18:33
0

strong textI manage to finish this. Here's the code (maybe will help other students) :

  // 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, -(old_bi.biWidth * sizeof(RGBTRIPLE)), 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);
}

P.S. as you can see is not that hard you need to turn the cursor back the number of pixels you have read on which is the size of one pixel (3 bytes) times the size of the row you have written on (width). The padding should not be included because is it skipped at the end of the row as you can see in the code.

1
  • I'm glad you figured it out, Criticalll!
    – leanne
    Aug 28 '19 at 13:15

You must log in to answer this question.

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