0

So what I tried to do here is to copy every pixel in each of infile's scanlines as many times as the factor provided, then go back to the start of the scanline and do it again but on the next row, so it also resizes vertically. But when I run this, I get a file size exceeded (512mb) so I guess it gets stuck on some kind of infinite loop, but I can't really see where. Any help would be appreciated, thanks.

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            int rowDone = 0;

            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile (factor)times
            for (int k = 0; k < factor; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

            // If on the end of scanline, go back to start of the scanline
            // and repeat factor times, on next row of outfile

            if (j == bi.biWidth - 1)
            {
                rowDone++;
                if (rowDone != factor)
                {
                    // Restarts for loop
                    j = 0;
                    // Seek to start of the scanline
                    fseek(inptr, i*(sizeof(RGBTRIPLE)*bi.biWidth)+1, SEEK_SET);
                }
            }

        }

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

        // then add it back (to demonstrate how)
        for (int l = 0; l < outpadding; l++)
        {
            fputc(0x00, outptr);
        }

    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

EDIT: So I figured out that the infinite loop was happening because I defined the rowDone variable inside of the pixels loop instead of the scanline loop. But now when I run it, I get a weird image with tons of colors in it, which I don't even know where they are coming from since the original picture is only red and white (I'm testing it with smiley.bmp). What's wrong with my logic here? :(

1 Answer 1

0

Assuming bi to refer to the input header, without any modifications to width/height.

                    // Restarts for loop
                    j = 0;

I think it should be set to -1, as it will be incremented before entering the loop another time. A somewhat hacky solution to avoid another layer of loop.

                    // Seek to start of the scanline
                    fseek(inptr, i*(sizeof(RGBTRIPLE)*bi.biWidth)+1, SEEK_SET);

That value looks odd. The absolute value (SEEK_SET) would be the file and bitmap header, plus i times the whole line, that's including the infile padding. Alternatively, use relative value (SEEK_CUR) with a negative value, moving the position back by the pixels you just read.

You must log in to answer this question.

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