0

Having a lot of difficulty with pset4 resize. I have been struggling through it, and have finally reached the code I have shown here. However, when I attempt to resize small.bmp, I get a square that is green in the top half, but squares of other colors in the bottom half. When I try to resize large.bmp, I get a green square with a white square that is almost in the middle, but slightly lower. On the CS50 Facebook page, someone alluded to my resetting of the pointer with fseek being the issue, but did not expand further, and I have no idea why my use of fseek is not working correctly. Here is the code I have written:

#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 infile outfile\n");
    return 1;
}

// remember filenames
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;
}
int n = atoi(argv[1]);
// 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;
}
long oldbiWidth= bi.biWidth;
long oldbiHeight= bi.biHeight;    
bi.biWidth = n * oldbiWidth;
bi.biHeight = n * oldbiHeight;
int oldpadding = (4 - (oldbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * (bi.biHeight);
bf.bfSize = bi.biSizeImage + 54;




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

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



// iterate over infile's scanlines
for (int i = 0, x = abs(oldbiHeight); i < x; i++)
{
    for (int a = 0; a < n; a++)
    {
        fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + (i * (oldbiWidth * sizeof(RGBTRIPLE) + padding)), SEEK_SET);
        for (int j = 0; j < oldbiWidth; j++)
        {
        // temporary storage
            RGBTRIPLE triple;

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

            for (int k = 0; k < n; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }   

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

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


}
}
// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
0

You seem to be using the wrong padding in your fseek calculation for resetting the pointer

1
  • Your'e right. Thank you so much. I went over and over this and couldn't see the problem. And it was such a stupid mistake. Very frustrating, but thank you so much for helping!
    – user10066
    Nov 1 '15 at 14:47

You must log in to answer this question.

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