This code outputs a seemingly random assortment of colors after the initial scanline, which is correct. If I comment out the for loop that fwrites the scanline "n"(the multiplier) times for every row, the output's width is correct, but the height is obviously still the same as the small image. What am I doing wrong?

// iterate over infile's scanlines
for (int i = 0 , h = abs(oldHeight); i < h; i++)
    //scanline with new width
    RGBTRIPLE scan[newWidth+1];
    int counter = 0;

    // iterate over pixels in scanline
    for (int j = 0; j < oldWidth; j++)
        // temporary storage
        RGBTRIPLE triple;
        RGBTRIPLE nTriple[n+1];

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

        for (int k = 0; k < n; k++)
            printf("%i", counter);
            nTriple[k] = triple;
            scan[counter] = triple;
            counter = counter+1;


        // write RGB triple to outfile
        fwrite(&nTriple, sizeof(RGBTRIPLE)*n, 1, outptr);

    for (int m = 0; m < n; m++)
        fwrite(&scan, sizeof(RGBTRIPLE)*newWidth, 1, outptr);

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

    // then add it back (to demonstrate how)
    for (int o = 0; o < newPadding; o++)
        fputc(0x00, outptr);


You would have to write output padding each time you write an output line, not once per input line, that's the real issue here. You write like <line1><line1><line1><padding>, while it should be <line1><padding><line1><padding>.

The whole concept of nTriple is not clear to me, looks like you don't need that at all if you fwrite the scan array in your loop.

I also don't understand your +1 (I use +1 in arrays mostly for strings). But that does no harm, as you ignore that extra element.

You must log in to answer this question.

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