0

I did the debugging using GDB. And it gets through pretty smoothily yet when I open the output file it says " invalid/ unsupported file format". NOTE : obi = bi ( where obi is the bmpinfoheader for the output file.) wasn't working in cs50 ide. So I had to do it manully for each structure element. Thank you for your time. /** this is the program to resize the * size of the bit map image */ #include #include

#include "bmp.h"

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

    // remember filenames
    int factor = atoi(argv[1]);
    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, obf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi, obi;
    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;
    }
    obi = bi;
    obf = bf;
    int biHeight = bi.biHeight > 0 ? bi.biHeight: - bi.biHeight;
    obi.biSizeImage = (bi.biWidth + biHeight) * factor * 3 + ((bi.biWidth * factor * 3) % 4) * biHeight;
    // write outfile's BITMAPFILEHEADER
    obi.biWidth = (bi.biWidth * factor); 
    obi.biHeight = bi.biHeight * factor;
    obf.bfSize = obi.biSizeImage + 54;
    fwrite(&obf, sizeof(BITMAPFILEHEADER), 1, outptr);

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

    // determine padding for scanlines
    int obipadding =  (4 - (obi.biWidth * 3) % 4);
    // iterate over infile's scanlines
    int bipadding = 4 - ((bi.biWidth * 3) % 4);
    for (int i = 0; i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int n = 0; n < factor; n++)
        {
            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 the temperory file
                for (int m = 0; m < factor; m++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            for (int d = 0; d < obipadding; d++)
            {
                fprintf(outptr, "%c", 0x00);
            }
        fseek(inptr, - bi.biWidth, SEEK_CUR);
        }
        fseek(inptr, bi.biWidth + bipadding, SEEK_CUR);
    }
    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);
    // that's all folks
    return 0;
}

1 Answer 1

1

First, obi=bi; should work fine, assuming it comes after reading in the header. It works fine for me and everyone else.

Next, the peek tool isn't working because of header issues that need to be resolved. When this happens, you can and should use the xxd tool to compare the data in the headers of the input file and the output file.

Finally, there are some serious issues here. Take note that a lot of the fields in the headers have very similar names. Changing the wrong ones can really corrupt the headers.

You should start with a resize factor of 1 and small.bmp as the input file. Get the headers right before you worry about the image data. Once the header is right, fix the code that handles the image data. Once you have the correct output for scale factor of 1, then repeat the process for a factor of 2. This systematic method usually fixes all the problems.

Happy coding!

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

3
  • I did as you suggested. Bmpheaderinfo is correct now. Thus a bmp image is formed but not the expected resized bmp. I ran the program with the resize factor 2 on small.bmp. On checking the output of xxd feature I noticed that the first 3 pixels ( first row) of the samll.bmp is horizontally resized but not vertically. Maybe fseek isn't working for the first line. How do i correct this? Commented Jan 1, 2017 at 18:02
  • What was the result of running with factor = 1?
    – Cliff B
    Commented Jan 1, 2017 at 20:52
  • It produces the copy of the small.bmp correctly. Picture comes to distort for the resize factor of one onwards. I have updated the source code please take a look and help me here. Thankyou. Commented Jan 4, 2017 at 5:55

You must log in to answer this question.

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