0

I was actually able to solve resize (more), but I'm not able to figure out what's wrong with one of my previous codes (the one I was stuck for longest on)

This is my old code:

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: copy f infile outfile\n");
        return 1;
    }

    // remember filenames

    char *zoomasdf = argv[1];
    char *infile = argv[2];
    char *outfile = argv[3];
    float f = atof(zoomasdf);

    if(f <= 0 || f > 100.0)
    {
        printf("f must lie in (0.0,100.0]");
        return 5;
    }

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "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;
    }


    BITMAPFILEHEADER outbf = bf;
    BITMAPINFOHEADER outbi = bi;

    long WidthIn = bi.biWidth;
    long HeightIn = abs(bi.biHeight);


    int paddingIn = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //padding of input file
    outbi.biWidth = (int) (f * bi.biWidth);
    outbi.biHeight = (int) (f * bi.biHeight);
    int paddingOut = (4 - (outbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //padding of output file
    outbi.biSizeImage = ((sizeof(RGBTRIPLE) * outbi.biWidth) + paddingOut) * abs(outbi.biHeight);
    outbf.bfSize = outbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    if(outbi.biHeight == 0 || outbi.biWidth == 0)
    {
        printf("How much do you want to zoom out, you crazy?");
        return 6;
    }

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

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



    for (int i = 0; i < f * outbi.biHeight; i++) //i represents the row number
    {
        // iterate over pixels in scanline
        for (int j = 0; j < outbi.biWidth; j++) //j represents the column number
        {
            // temporary storage
            RGBTRIPLE triple;

            fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (WidthIn * sizeof(RGBTRIPLE) + paddingIn) * (int) (i / f) + sizeof(RGBTRIPLE) * (int) (j / f), SEEK_SET);

            // 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, paddingIn, SEEK_CUR);

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



    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

and the correct code I used finally is this:

[EDIT: Code removed for Honor Code compliance. Posting of working code is not allowed.]

Literally all I changed was the limits of i, and both seem to be exactly the same to me. (If I use the wrong code, then all the pixels are empty and it doesn't write anything, the height and width are correct, but just nothing is written)

I would greatly appreciate it if anyone could point out the mistake.

Thanks!

3

That line seems to be the culprit:

for (int i = 0; i < f * outbi.biHeight; i++)

Would have to use abs(outbi.biHeight) or equivalently (int) (f * HeightIn), not f * outbi.biHeight.

The line fseek(inptr, paddingIn, SEEK_CUR); is pretty useless here (but shouldn't hurt).

3
  • What's wrong with that inner loop? – Blauelf Jul 15 '19 at 16:39
  • I must be slipping. You're right and I've deleted my erroneous comment. – Cliff B Jul 15 '19 at 16:42
  • Yes, it's working now! I believe I removed the `fseek(inptr, paddingIn, SEEK_CUR) in the final code I submitted, but thanks anyways! – Amit Rajaraman Jul 16 '19 at 5:33

You must log in to answer this question.

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