1

I am trying to solve resize but I am facing problems and I don't know what to do. I don't even know is the problem with my info update or with fseek. I feel that there is something I miss or don't understand.

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

if (n < 0 || n > 100)
{
    printf("invalid n");
    return 1;
}

// 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 = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;
int pad_new = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((bi.biWidth + pad_new) * bi.biHeight) * 3;
bf.bfSize = bi.biSizeImage + sizeof(bf);

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

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight / n); i < biHeight; i++)
{
    // iterate over pixels in scanline
    for (int w = 0; w < n; w++){
        for (int j = 0; j < bi.biWidth / n; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

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

        }

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

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

        fseek(inptr, -(bi.biWidth / n + padding_original), SEEK_CUR);

    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

2

Well, you seem to be almost there, so I won't review all of your code, just give you some pointers:

The first and most crucial tip is that you need to pass fseek() the number of BYTES as the second argument, but you're passing it the number of PIXELS + padding here: fseek(inptr, -(bi.biWidth / n + padding_original), SEEK_CUR);. Understand what I mean? Remember that bi.biWidth is the number of PIXELS (not bytes) of a scanline... and each pixel is 3 bytes long. Padding is already okay, because each unit of padding is just one byte long.


Second crucial thing:

If you fseek() back n times like you're doing, in the last iteration of your loop you'll rewrite part of the file. To prevent that, you should fseek() n - 1 times.

I hope those two pointers get you going.


PS: Other than that, your calculation of bi.biSizeImage and bf.bfSize are also wrong (and you're writing bf to the outfile before bf is updated), but leave them aside for a while until you get a code that can at least produce the correct resized image.

You must log in to answer this question.

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