0

hey i don't know why but my code works perfect resizing the images but for some reason i don't understand the check50 command tells me it is not correct because it does not resize anything correctly could someone please help

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * rezises 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: ./resize numberoftimestoberesized infile outfile\n");
    return 1;
}

// remember filenames and key to resize

int key = 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;
BITMAPFILEHEADER bfc;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
BITMAPINFOHEADER bic;
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;
}
//makes a copy of the header files 
bfc = bf;
bic = bi;

bic.biWidth = bi.biWidth * key; 
bic.biHeight = bi.biHeight * key;

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

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

// Padddings
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) %4) % 4;
int paddingc = (4 - (bic.biWidth * sizeof(RGBTRIPLE)) %4) % 4;

// New seize
bic.biSizeImage = (bic.biWidth * sizeof(RGBTRIPLE) + paddingc) * abs(bic.biHeight);
bfc.bfSize = bf.bfSize - bi.biSizeImage + bic.biSizeImage;

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    //to ultiply times the hight
    for (int j = 0; j < key; j++)
    {
        // iterate over pixels in scanline
       for(int b = 0; b < bi.biWidth; b++ )
        {
            RGBTRIPLE triple;

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

            // multiplies times the width
            for(int y = 0; y < key; y++)
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        } 

        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);
        // Add a new padding
        for (int f = 0; f < paddingc; f++)
            fputc(0x00, outptr);

        // Back to the begining
        //f (j < key - 1)
        fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);      
    }

    // Back to the begining
    //if (j < key - 1)
    fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

return 0;
}

1 Answer 1

1

Actually, it isn't resizing correctly. The images are bad, with black pixels, due to padding being translated as pixels. (I compiled your posted code and ran it.)

You have problems in two areas. First, you're not generating correct headers. Second, your bitmaps are not being correctly scaled up because you have logic problems with padding.

First, the headers. You've calculated the necessary header info correctly, but not before writing the headers to the output file.

Next, for key value > 1, you are processing each line of the input file 'key' times. It works fine for key = 1, but when you try to go back to reprocess any line a second or more times, you're not moving your input file pointer the correct distance. Look carefully at your fseek() commands. In each case, you're not accounting for the padding in the input file line. In one case, you're dealing with the padding when you shouldn't, in another you're overlooking it completely.

I'll leave it to you to figure out how to recode it to make it work. ;-)

Run both your code and the staff solution against the same input file and compare the output files from both. Look at the headers and at the generated bitmaps to track down what isn't working.

BTW, when you run CHECK50, make sure that you get all green smiling faces. If you have a bad header but a good bitmap image, it will tell you the image is right, but won't say anything about the header, and will just give you the red text and a frown. See the following:

check50 gives confusing results for pset4 resize

You must log in to answer this question.

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