2

I have been looking for answers here but wasn't able to make the code work, so I am hoping someone will be able to help with this question. I have been working on pset4 resize for several days now and I must be doing something fundamental wrong, because small won't change size (just colour). I have put some of the code below. I haven't changed much outside this section. Thanks in advance!

//formula for padding int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
bf.bfSize = (bf.bfSize * n) + padding;

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
bi.biWidth = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;
bi.biSize = bi.biHeight * (bi.biWidth + padding);

// iterate over infile's scanlines, adjusting height
for(int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
    for(int r = 0; r < n; r++)
        {
        // iterate over infile's scanlines, adjusting width    
        for(int j = 0; j < bi.biWidth; j++)
            {
            //temporary storage
            RGBTRIPLE triple;

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

            //iterate size of outfile by n
            for(int s = 0; s < n; s++)
                {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);        
                }
            }
            //skip over padding
            fseek(inptr, padding, SEEK_CUR);

            // determine padding for scanlines
            for(int k = 0; k < padding; k++)
                {
                fputc(0x00, outptr);
                }
            //return pointer to just after headers
            fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), SEEK_SET);

        }    
    }
2

I see two significant problems. First, it appears that you have only calculated the padding for the input file and are using it for both the input and output files. The two paddings are not related and must be calculated separately. The wrong output file padding will cause problems in the headers and the image.

Second, after processing a line of pixels from the input file, you are repositioning to the beginning of the image every time. The effect is that you are processing the first line of the image over and over - never processing any of the rest of the image. You need to reposition to the beginning of either the current line (if you have to process it multiple times to scale up), or to the next line (after you've finished processing the current line n times.)

These are the two big problems. You probably have some header issues too, and there may be more bugs to fix, but this will get you going.

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

1
  • Thanks very much for your help with this. Made some changes, still fixing a few things but this has been very helpful. Oct 25 '15 at 16:55

You must log in to answer this question.

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