0

Can I get some help please with the pset4's resize? My code SEEMS to work just fine, as in it compiles and runs with no problems (even with different n factors), and when I use (xxd ...) to show the pixels in the terminal, everything is in its right place. However, when I run check50 or compare with the staff's implementation, a lot of things are in red! Here's my code and a screenshot of the errors!

 /**
 * resize.c
  *
  * Computer Science 50
  * Problem Set 4
  *
  * Copies 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 n original copied\n");
    return 1;
}

// remember filenames
char* k = malloc(sizeof(char) + 1);
                                                     //free
k = argv[1];
int n;
sscanf(k,"%i", &n);    
if (n < 1 || n > 100)
{
    printf("the resizing factor must be between 1 and 100!\n");
    return 2;   
}
char* infile = argv[2];
char* outfile = argv[3];

//allocating the space for the new file
//char* fact = malloc();
//ignore the memory leak for now!

//each pixel is to be copied n times in the new file

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", infile);
    return 3;
}

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

// 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 5;
}
    long biWidth_org = bi.biWidth;
    long biHeight_org = bi.biHeight;
    bi.biWidth = biWidth_org * n;
    bi.biHeight = biHeight_org * n;

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

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

// determine padding for scanlines (IN BYTES)
int padding =  (4 - (biWidth_org * sizeof(RGBTRIPLE)) % 4) % 4;

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

bi.biSizeImage = abs(bi.biHeight) * ((bi.biWidth - (padding * n))* sizeof(RGBTRIPLE) + padding_enlarged);
bf.bfSize = bi.biSizeImage + 54;

if (n == 1)
{
    for (int i = 0, x = abs(biHeight_org); i < x; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < biWidth_org; 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);
        }
    }
}
else
{

    // iterate over infile's scanlines
    for (int i = 0, x = abs(biHeight_org); i < x; i++)
    {

            // iterate over pixels in scanline
            for (int j = 0; j < biWidth_org; j++)
            {
                    // temporary storage                           why do we have to redefine it?
                    RGBTRIPLE triple;
                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

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

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

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

                // then add it back (to demonstrate how)
                for (int k = 0; k < 3 * padding_enlarged; k++)
                {
                    fputc(0x00, outptr);
                }            
            // horizontal scalling 
            for (int h = 0; h < n - 1; h++)
            {

                fseek(inptr, -12, SEEK_CUR);
                for (int j = 0; j < biWidth_org; j++)
                {
                        // temporary storage
                        RGBTRIPLE triple;
                        // read RGB triple from infile
                        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

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

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

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

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}

errors for n factor = 4, all look normal w.r.t pixels, then I get the errors

0

It seems that your header calculations are wrong. Wouldn't it work better if you wrote the headers out after you calculated bfsize and bisizeimage instead of before? ;-)

There may be other issues, so no guarantees that this fixes everything.

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

You must log in to answer this question.

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