I'm having trouble with the vertical scaling. I've reviewed my code and it seems like it should scale correctly, but instead I get the following picture.

My pseudocode for vertically scaling goes as follows:

for each row
    for each pixel
        copy each pixel (RGBTRIPLE value) "n" times to an array
    for n times
        write array to outfile
        write outfile padding
    skip infile padding

Below is my actual code. Any hints on what I'm doing wrong?


    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        // create array
        RGBTRIPLE array[n];
        // create pointer to array
        RGBTRIPLE *a;
        a = array;

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile
            for(int k = 0; k<n; k++)
                // write each pixel "n" times to the array
                array[k].rgbtBlue = triple.rgbtBlue;
                array[k].rgbtGreen = triple.rgbtGreen;
                array[k].rgbtRed = triple.rgbtRed;

        for(int b=0;b<n;b++)
        // write array to outfile
        fwrite(&array, sizeof(RGBTRIPLE), 1, outptr);
            // write outfile padding
            for (int k = 0; k < outPadding; k++)
                fputc(0x00, outptr);
        // skip infile padding
        fseek(inptr, inPadding, SEEK_CUR);

1 Answer 1


First, I'm assuming that n is the scaling factor.

Look carefully at what the code is doing. It starts by creating array[n], an array of RGBTRIPLEs that can hold n pixels. Then, it stores n copies of a pixel in the array. Next, it writes out the n copies of the pixels to the output file. Finally, it writes out padding and skips the input file padding.

Think carefully about that. It writes padding out after each PIXEL is processed. It should only write padding out after each LINE OF PIXELS.

The code needs to be modified to either read an entire line of pixels into an array (scaled or not), or it needs to go back to the beginning of each line to reprocess it until it has been processed n times.

Here's a side note. Processing the RGBTRIPLE elements red, green and blue is good practice, but those three elements constitute a struct. An entire struct can be read, copied, or written as a single var. You don't have to process each element individually.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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