0

Having some growing pains with this pset. I think I have the horizontal resizing figured out with my for loop. My problem is iterating over the same line x number of times (where x is the scale entered). Each time I try something I run into issues with the padding or it doesn't iterate over that same line.

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

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

int originalPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i", originalPadding);

// 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 oldHeight = bi.biHeight;
int oldWidth = bi.biWidth;
//printf("%i%i", oldHeight, oldWidth);

bi.biWidth = (bi.biWidth * resizeBy);
bi.biHeight = bi.biHeight * resizeBy;
// determine padding for scanlines
//this is padding for adjusted width of outfile
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i", padding);
//vvvvvv We need to add the padding vvvvvv
bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + 54;
//printf("%i%i", oldHeight, oldWidth);

// 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, oldHeight1 = abs(oldHeight); i < oldHeight1; i++)
{
    for ( int r = 0; r < resizeBy; r++)
    {
    // iterate over pixels in scanline
    for (int j = 0; j < oldWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        for (int  k= 0; k < resizeBy; k++)

        {
        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }

    // skip over padding, if any
    fseek(inptr, originalPadding, SEEK_CUR);
    // then add it back (to demonstrate how)
    for (int p = 0; p < padding; p++)
    {
        fputc(0x00, outptr);
    }
    if (r % resizeBy != resizeBy - 1)
    {
    fseek (inptr, -1 * (oldWidth * (int)sizeof(RGBTRIPLE)) + originalPadding , SEEK_CUR);
    }
    }


    }
    //move back to same line??


// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

}

1
  • Also, this is not all of my code....I cut out a large if statement at the beginning, just in case someone sent a factor of 1.
    – iHusk
    Feb 9 '18 at 2:04
1

So, I see that it calculates the padding for the input file. Where is the padding calculation for the output file? They are not necessarily the same, especially if the scaling factor != 1.

Also, biSizeImage and bfSize are going to have some issues because of that and because they can't simply be multiplied by the scaling factor that was entered.

Finally, when scaling up more than 1, the code needs to go back to the beginning of the current line in the input file the correct number of times. Be sure not to process each line one more time than needed. I'll let you think about exactly how to do that. ;-)

There may be other issues, but these just jumps off the page.

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

[EDIT] Take a very, very close look at this line, particularly the parentheses:

fseek (inptr, -1 * (oldWidth * (int)sizeof(RGBTRIPLE)) + originalPadding , SEEK_CUR);

See anything wrong?

2
  • OK. I sat down last night with the debugger and really went through each line. I now get Smiley to work, Large to work, Headers are sized and set up correctly. small.bmp does not output anything close to what I want it too. I am somehow getting black in the image. I have stepped through and cannot find an error. I'm going to update my code above....
    – iHusk
    Feb 9 '18 at 16:41
  • Check edited answer.
    – Cliff B
    Feb 9 '18 at 23:23

You must log in to answer this question.

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