0

I've been able to resize images correctly (I checked several sizes and images and apparently everything works), but check50 complains saying that it doesn't resize correctly. I think the problem is in writing the headers, but I'm not sure what's wrong with the code.

Here's my code:

int main(int argc, char* argv[])

{ // ensure proper usage if (argc != 4) { printf("Usage: ./copy new_size infile outfile\n"); return 1; }

// store new size in int
int new_size = atoi(argv[1]);

// check if the value is between 1 and 100
if (new_size < 1 || new_size > 100)
{
    printf("Resize value should be between 1 and 100\n");
    return 1;
}

// 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 2;
}

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

// 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;
}

// Store previous width and height
int original_width = bi.biWidth;
int original_height = abs(bi.biHeight);

// Determine the new width and height
bi.biWidth = bi.biWidth * new_size;
bi.biHeight = bi.biHeight * new_size;

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

// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + 54;

// Determine the new size of the new image
bi.biSizeImage = bf.bfSize - 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 < original_height; i++)
{
    // To know where to come back
    int current_position = ftell(inptr);

    for (int j = 0; j < new_size; j++)
    {
        // Detect current position so we can go back to resize the height
        fseek(inptr, current_position, SEEK_SET);

        // iterate over pixels in scanline
        for (int k = 0; k < original_width; k++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            for (int l = 0; l < new_size; l++)
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

        // Add new padding if necessary
        for (int k = 0; k < new_padding; k++)
        {
            fputc(0x00, outptr);
        }
    }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

I have also tried with:

// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + new_padding + 54;

which should be more correct actually because that why I'm including the new padding, but it doesn't work either.

Any ideas? Thanks a lot!

Pablo

1
  • did you use peek, xxd, and diff as suggested by the specs?
    – kzidane
    Jun 29, 2015 at 14:38

1 Answer 1

3

I think that the problem comes from the headers as you said, so probably the problem is here:

// Determine the new width and height
bi.biWidth = bi.biWidth * new_size;
bi.biHeight = bi.biHeight * new_size;

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

// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + 54;

// Determine the new size of the new image
bi.biSizeImage = bf.bfSize - 54;

Firstly, in determining the bfSize, you used:

bi.biWidth*new_size

But as you can see in the first lines of the snippet above, you already did it:

// Determine the new width and height
bi.biWidth = bi.biWidth*new_size;

So, in fact, what you are assigning to your bfSize is:

bi.biWidth * new_size * new_size;

Secondly, bfSizeImage is the size of your BMP file, and the BMP has a width and height. Every line has width of RGBTRIPLEs and some paddings...

So, maybe you should multiply the width by 3 and add padding before multiplying by the height.

// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + 54;

And remember, RGBTRIPLE, BITMAPFILEHEADER and BITMAPFILEINFO are all structures.. Maybe you can use sizeof() instead of hard-codding the values :)

Hope this will help

2
  • Yes, you were right! Thanks Ahmed! I tried with the following code: // Determine new size of the resized bitmap file bf.bfSize = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + new_padding * abs(bi.biHeight) + 54; // Determine the new size of the new image bi.biSizeImage = bf.bfSize - 54; And now I pass all the checks :) (I store the original bi.biWidth and bi.biHeight in variables) Jun 29, 2015 at 19:20
  • Congratulations @PabloMuñozSánchez ;)
    – Ahmed
    Jun 29, 2015 at 20:22

You must log in to answer this question.

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