0

Why am I only getting unsupported image format when trying to resize clue.bmp. I have tried with small,large and smiley and these all seem to be working.

The image is being resized correctly but the image is black. I am checking the height and width using the debugger and indeed the values are changing in relation to n.

I imagine now I need to look into padding as this is what is causing the black image.

See code below:

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
    printf("Usage: ./copy infile outfile\n");
    return 1;
}

if (atoi(argv[1]) > 100){
 printf("Please enter number smaller than 100...");
  return 1;
}

int n = atoi(argv[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;
}
//need to modify height and width of bi.biWidth and bi.biHeight by increasing by n;
//also need to modify image size and filesize;

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

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

// copy these file headers so they can be written to output file
BITMAPFILEHEADER bfo = bf;
BITMAPINFOHEADER bio = bi;

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

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

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



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



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

// 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
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }

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

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

1

There are a number of errors in the code that need to be resolved and other issues that haven't even been coded for.

There are fields in the headers that haven't been updated. The padding for the input and output files need to be calculated independently. The pixels need to be output n times. Each line of pixels needs to be processed n times as well. There are probably more issues, but these need to be cured first.

You should start by testing the code with n=1 on a file like small.bmp. Compare the output file to the input file using the peek tool. If that's correct, then work on getting the image right. Compare the image data using the xxd tool, looking at the data - not visually looking at the image. Once all this works, move on to testing with n=2. Run the same input file through your code and through the staff example. Compare the headers of the two output files using the peek tool. Only after the header is processed correctly, move on to the image data and again, compare with the xxd tool.

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

1
  • Thanks for your response. When you say there are fields in the headers that haven't been updated - I think this is true but I thought the only fields that needed to be changed were width and height, and these are being changed correctly. I believe the actual image dimensions are correct but the output data is incorrect due to not having dealt with the padding correctly. I think the only problem I'm having here is with the padding and when this is dealt with, I will have solved the problem. – i'i'i'i'i'i'i'i'i'i Dec 21 '16 at 11:20

You must log in to answer this question.

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