0

Resize.c is by far the most difficult code for me this far... I have asked the question before and was told to work on my padding which I did and I think it´s good now. But check50 still says this :) resize.c and bmp.h exist :) resize.c compiles :) doesn't resize 1x1-pixel BMP when n is 1 :( resizes 1x1-pixel BMP to 2x2 correctly when n is 2 :( resizes 1x1-pixel BMP to 3x3 correctly when n is 3 :) resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :( resizes 1x1-pixel BMP to 5x5 correctly when n is 5 :) resizes 2x2-pixel BMP to 4x4 correctly when n is 2 It resizes properly though... I just don´t know what to try anymore... THank you for any ideas! here my code:int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage: ./copy infile outfile\n"); return 1; } //check the resize factor int factor = atoi(argv[1]); if (factor < 1 || factor > 100) { printf("invalid integer. Try again!\n"); return 2; } // remember filenames

char* infile = argv[2];
char* outfile = argv[3];

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", infile);
    return 3;
}

// open output file
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile); 
    return 4;
}

// 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;
}
// set new dimension for bitmap & calculate
int originalWidth = bi.biWidth;
int originalHeight = bi.biHeight;
int originalHeight2 = abs(originalHeight);
bi.biHeight = originalHeight * factor;
bi.biWidth = originalWidth * factor;


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


//create new image size
bi.biSizeImage = (bi.biWidth * sizeof (RGBTRIPLE) + out_padding) * abs(bi.biHeight);
bf.bfSize = (bi.biSizeImage) + 54;

// 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; i < originalHeight2; i++)
{
    //write factor each line
    for ( int n = 0; n < factor; n++)
    {
    // iterate over remaining padding
        fseek (inptr, (BHEADER + ((originalWidth * 3 + in_padding) * i)), SEEK_SET);
        for( int j = 0; j < originalWidth; 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 < factor; k++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
    }
    // store outfile padding
    for (int l = 0; l < out_padding; l++)
    {
      fputc(0x00, outptr);
    }

1 Answer 1

2

You are very close to the solution. The headers are all correct, but there's an issue with the image area. You need to take a close look at the image data itself and compare it to the staff solution. Run your program with ./resize 2 small.bmp s2.bmp and then do the same thing with the staff solution. Then use xxd -c 12 -g 3 -s 54 s2.bmp to look at yours and the staff output file (use a different file name for theirs) and compare them.

Look at the size of the output of both. Is your output file shorter? How much shorter? Does it have a relationship to some characteristic of the image? Now, look at the first line of output. How does yours differ. Where does your second line of data actually start? What's missing? Why is it missing? Does this problem repeat?

Once you realize what's happening, the fix is a very simple edit.

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 .