0

I'm stuck on resize. I feel like i'm doing everything right but have no idea why my code isn't working right. I think i'm pretty close but the vertical scaling isn't working as it should it seems. Here's my code

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

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

    // 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;
    }

    //Create header files for output file
    BITMAPFILEHEADER bf_out = bf;
    BITMAPINFOHEADER bi_out = bi;

    //change BITMAPINFOHEADER and BITMAPFILEHEADER
    bi_out.biWidth *= n;
    bi_out.biHeight *= n;
    int newpadding = (4 - (bi_out.biWidth * 3) % 4) % 4;
    bi_out.biSizeImage = ((3 * bi.biWidth) + newpadding) * abs(bi.biHeight);
    bf_out.bfSize = bi.biSizeImage + 14 + 40;


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

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

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

    int x = 1;
    int y = 0;

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

        for (int g = 0; g < n; g++)
        {

            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

                // write RGB triple to outfile
                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)
            if (bi.biWidth % 4 != 0) {
                for (int l = 0; l < newpadding; l++)
                {
                    fputc(0x00, outptr);
                }
            }

            //fseek back to beginning of the line to repeat
            if (x % (n+1) == 0) {
                y++;
            }

            fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (y * (bi.biWidth + oldpadding)), SEEK_SET);
            x++;
        }
    }

Any help would be greatly appreciated :)

0

In

bi_out.biSizeImage = ((3 * bi.biWidth) + newpadding) * abs(bi.biHeight);

it should all be bi_out, not bi

The if (bi.biWidth % 4 != 0) { doesn't add anything as in case of bi.biWidth % 4 == 0, newpadding would be 0 and for (int l = 0; l < newpadding; l++) would not enter the loop even once. It won't hurt either.

The fseek(inptr, oldpadding, SEEK_CUR); also doesn't add anything as you later use SEEK_SET, so old value of file cursor does not matter.

I think I get your fseek logic, but in my opinion, if (x % (n+1) == 0) { should be if ((x+1) % n == 0) { with the +1 at another variable.

You must log in to answer this question.

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