0

Please could somebody give me a hint on my code for the Pset4 resize less comfortable. It seems to resize the image horizontally but Im having trouble making the vertical resizing. Im getting garbled outputs with the code that I have below. (I didnt copy all the code in, just from the part that I have started my modifications). Any hints / help would be greatly appreciated.

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
    bi.biBitCount != 24 || bi.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

int paddingold = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biWidth = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;

RGBTRIPLE triplearray[bi.biWidth];

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

bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + paddingnew) * abs(bi.biHeight);

bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// 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++)
    {
        // temporary storage
        RGBTRIPLE triple;

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


        // write RGB triple to outfile
        for (int r = 0; r < n; r++)
        {
           fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
           triplearray[j] = triple;
        }
    }

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

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

     for (int r = 0; r < n - 1; r++)
        {
           for (int y = 0; y < bi.biWidth; y++)
            {
              fwrite(&triplearray[y], sizeof(RGBTRIPLE), 1, outptr);
            }

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

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1 Answer 1

1

The problems start here: the code modifies bi.biWidth and bi.biHeight for the output file and then uses them to control the processing of the input file.

Say that n=2. That means that its going to read roughly two full lines from the input as one line and then will try to read twice the size of the input image, which usually results in the bottom half of the output image looking like the last block of data read from the file.

The input file's parameters need to be used to process the input file.

There may be other issues, but these look like the big ones.

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

1
  • Thanks for your advice, it's greatly appreciated. Nov 10, 2017 at 9:26

You must log in to answer this question.

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