0

Here's the code I finally have. but the issue is the cs50 check says :

:) 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

and when I try to just go head resize the image from small.bmp to large.bmp, the system says "invalid unsupported image or format". SO FRUSTRATED......I have been working on this resize.c for days!!! I read all over the internet and people say it was because of the issue of header, but I just cannot figure out!! please help me and tell me what happened.

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Resize a BMP piece by piece, just because.
 */

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

    // remember filenames
    int factor = atoi(argv[1]);
    char* infile = argv[2];
    char* outfile = argv[3];

    if ((factor < 1) || (factor > 100))
    {
        printf( "factor can only be the number in range 1~100\n");
        return 4;
    }

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

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



     bi.biWidth *=  factor;
     bi.biHeight *= factor;

     int old_width = bi.biWidth;
     int old_height = bi.biHeight;

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


    int new_padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

      // determine the padding the new bmp

    int padding =  (4 - (old_width * sizeof(RGBTRIPLE)) % 4) % 4;



    bi.biSizeImage = (sizeof(RGBTRIPLE) * bi.biWidth + new_padding) * bi.biHeight; 
    bi.biSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);



    // iterate over infile's scanlines
    for (int i = 0; i < old_height; i++)
    {
        // iterate over pixels in scanline
      for (int k = 0; k < factor; k++)
       {
        for (int j = 0; j < old_width; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            for (int q = 0; q < factor; q++)
            {
                   // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        fseek(inptr, padding, SEEK_CUR);

       for(int m = 0; m < new_padding; m++)
       {
           fputc(0x00, outptr);
       }

        if( k + 1 < factor)
        {
        fseek(inptr, 54 + (old_width * sizeof(RGBTRIPLE) + padding * i), SEEK_SET);
        }
       }        
    }        
    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

1 Answer 1

0

There are a number of issues. Most of them can be attributed to not paying careful attention, not understanding certain parameters in full, and not paying attention to sequencing.

First, there are header problems. Many of the fields in the headers have very similar names. You should go back and check that you are updating the correct fields. You should also understand how certain fields are used.

Next, ordering is important. The headers should only be written to the output file after all the fields have been updated.

Once the headers are right, then the image processing has to be dealt with. While the code may work for factor=1, it fails for anything larger. Again, there's a problem with order, or sequence of code. The limit for the main for loop has a couple problems. What happens if it is a negative number? Then, the absolute value is too large because the limiting value is incorrect. I'll leave it to you to track down why. Another clue would be that the output file produced can be 54 bytes - the size of the headers - depending on the factor size.

Even if this were right, the repositioning of the input file pointer while reprocessing lines from the input file is also flawed.

There may be more issues, or more may appear as you work on the code, but this should get you going again. My advice is to start with factor = 1 and first get the header right. THen get the image right. Once you can duplicate the input file correctly (factor=1), change to factor = 2 and repeat the same process.

There are just a lot of issues with this code that have to be dealt with. When you have new issues, please feel free to open a new question with the updated code.

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

1
  • Thank you so much!!! I do see the problem you point out and am still in the process of fixing others. Now i can finally opens the large.bmp after run it, but it apparently did not resize them correctly. The large bmp does not even have the right color!! it looks green, blue, red, or white randomly. Seems like i did not copy the pixel correctly in my loop. Will you point out for me that what's the issue in my loop? THANKS!!!
    – qingming
    Commented Oct 2, 2016 at 15:07

You must log in to answer this question.

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