0

I wrote my code for pset4 resize.c (the goal is to resize an image by n)

Currently, I have no compilation errors or memory leaks.

The problem is the output is wrong: I get resized BMPs that are blank.

I also get the following error:

* Error in `./resize': double free or corruption (top): 0x000000000125c250 * Aborted

My code:

int main(int argc, char* argv[]) { int n;

// ensure proper usage
if (argc != 4)
{
    printf("Usage: ./resize n infile outfile\n");
    return 1;
}

else 
{
    n = atoi(argv[1]);


     if ( ( n < 0 ) || ( n > 100 ) )
     {

        printf(" n must be a positive floating integer from 0.0 up till 100.0\n");
        return 1;

     }

}

// remember filenames
char* infile = argv[2];
char* outfile = argv[3];


// open input file 
FILE* input = fopen(infile, "r");

if (input == NULL)
{
    printf("Could not open %s.\n", infile);
    return 2;
}

// open output file
FILE* output = fopen(outfile, "w");

if (output == NULL)
{
    fclose(input);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 3;
}



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

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

// 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(output);
    fclose(input);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

int originalwidth = bi.biWidth;
int originalheight = bi.biHeight;
int originalpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

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

int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 bi.biSizeImage = bi.biHeight * ( 3 * bi.biWidth + newpadding );
bf.bfSize = bf.bfOffBits + bi.biSizeImage;    

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

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

// iterate over infile's scanlines
for (int i = 0; i < abs(originalheight); i++)
{
    int numberoflines = 0;
    // iterate over pixels in scanline
    for (int j = 0; j < abs(originalwidth); j++)
    {
         // temporary storage
        RGBTRIPLE triple;

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


        // write RGB triple to outfile n times for horizontal resize
        for (int a = 0; a < n; a++)
        {

            fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
            fclose(output);


            //temporary = malloc(sizeof(RGBTRIPLE));
            //temporary = fopen("tempmem.txt", "w");
            //fwrite(&triple, sizeof(RGBTRIPLE), 1, temporary);

        }

        //for (int b = 0; b < n-1; b++)
        //{
            //fwrite(temporary, sizeof(RGBTRIPLE),1, output);
        //}

    }



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

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

    if ( numberoflines < n - 1)
    {
        fseek( input, -1 * ( 3 * originalwidth + originalpadding ), SEEK_CUR);
    }

    numberoflines++;

}

// close infile

//fclose( temporary);
fclose(input);

// close outfile
fclose(output);

// that's all folks
    return 0;
}

Thanks a lot,

-1

You fclose output in a loop. Close the file only before exiting, not in between, when you still want to write to it.

1
  • I removed the fclose(output); statement inside the loop and I still get the blank BMPs. Thanks, – KhalidMukadam Jan 3 '17 at 13:18

You must log in to answer this question.

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