0

The code I wrote (using the pseudo-code of Zamyla's video on resize) make the image proper for some cases while not for others. I really need help in finding the flaw in the code.

pseudo-code
1. for each row
2.     for n-1 times
3.         write pixels, padding to the file
4.         send infile cursor back
5.     write pixels, padding to outfile
6.     skip over infile padding

for (int i = 0, biHeight = abs(iHeight); i < biHeight; i++)                
{
    int j;
    // temporary storage
    RGBTRIPLE triple;
    for (int k = 0; k < n - 1; k++)                                        
    {
        /*--------write pixels, padding to the file------------*/
        for (j = 0; j < iWidth; j++)                                       
        {
            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            for (int l = 0; l < n; l++)                                     
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // write padding to the outfile
        for (int l = 0; l < paddingNew; l++)                                
        {
            fputc(0x00, outptr);
        }
        /*------------send infile cursor back-------------*/
        fseek(inptr, -1 * sizeof(RGBTRIPLE) * j, SEEK_CUR);                
    }
    /*-----------------------write pixels, padding to outfile--------------*/
    for (j = 0; j < iWidth; j++)                                           
    {

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


        /*--------write pixels, padding to the file------------*/
        // write RGB triple to outfile
        for (int l = 0; l < n; l++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }
    // write padding to the outfile
    for (int l = 0; l < paddingNew; l++)
    {
        fputc(0x00, outptr);
    }
    /*-----------------------skip over infile padding------------------*/
    fseek(inptr, padding, SEEK_CUR);                                        
}
  • I don't see a mistake, does the file size match? Have you adjusted the header? I think you need to change four fields. You could compare the output of your tool and the reference implementation byte-wise to see if and where they differ. – Blauelf Nov 2 '16 at 14:17
  • This is the code I wrote to change the headers. // determine padding for scanlines int padding = (4 - (iWidth * sizeof(RGBTRIPLE)) % 4) % 4; int paddingNew = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // edit header values bi.biHeight = bi.biHeight * n; bi.biWidth = bi.biWidth * n; bi.biSizeImage = (sizeof(RGBTRIPLE) * bi.biWidth + paddingNew) * abs(bi.biHeight); bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); – RISHABH KALAKOTI Nov 2 '16 at 15:52
  • I know this code is not formatted when I copied it out, but you will not need to format it I guess, it's a small piece. – RISHABH KALAKOTI Nov 2 '16 at 15:56
  • Finally, it's done. This piece of code was perfect, the problem was with my 'paddingNew' definition and I never looked towards it. It was only after comparing the files with 'peek' I got to see the header differences. Thank You. – RISHABH KALAKOTI Nov 2 '16 at 20:20
  • Glad you solved it. Self-answers are highly suggested. Could you please take your comments and create an answer, and then accept it so that the question will be removed from the unanswered question pool? – Cliff B Nov 5 '16 at 7:55
0

The code is perfect. I had bug in some other part of the program.

| improve this answer | |

You must log in to answer this question.

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