0

I have been struggling with cs50 resize for quite some time. Can't figure out why my code is wrong. The output seems to have a right size but it is in a bunch of random colour.

// Copies a BMP file

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



    int n = atoi(argv[1]);

    if (n <= 0 || n > 100)
    {
        fprintf(stderr, "Invalid n.\n");
        return 1;
    }

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

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

    int oldWidth = bi.biWidth;
    int oldHeight = bi.biHeight;
    bi.biWidth  *= n;
    bi.biHeight *= n;
    // determine padding for scanlines
    int opadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int npadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biSizeImage = abs(bi.biHeight) * ((bi.biWidth * sizeof (RGBTRIPLE)) + npadding);
    bf.bfSize = bi.biSizeImage + sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER);


    // 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
    RGBTRIPLE triple;

    for (int i = 0, biHeight = abs(oldHeight); i < biHeight; i++)
    {

            // write RGB triple to outfile
                for(int g = 0; g < n; g++)
                {

                for (int j = 0; j < oldWidth; j++)
                {

                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                for (int l = 0; l < n; l++)
                    {

                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }

                }
                fseek(inptr, opadding, SEEK_CUR);
                for (int k = 0; k < npadding; k++)
                        {
                                    fputc(0x00, outptr);
                        }
                        fseek(inptr, -oldWidth, SEEK_CUR);
                }



        }










            // skip over padding, if any







        // then add it back (to demonstrate h

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

'''
0

For each output line, you fseek forward by opadding bytes, and backward by oldWidth bytes.

When you want to re-read the last input line, you would instead fseek backward by oldWidth pixels (of 3 bytes each, do not forget that factor), and when ready to move on in the input file, fseek forward by opadding bytes instead. Any of these could be done in multiple steps if that fits your code (e.g. always rewind, but jump forward by a full line plus padding after your g loop, whatever makes sense to you).

You must log in to answer this question.

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