0

im having some problme with resize and cant find them.. would love some help, ty!

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

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

    int nu = atoi(n);


    if (nu < 1 || nu > 100)
     {
         fprintf(stderr, "Usage: n must be a full number between 1 and a 100\n");
         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;
    }

    //long biWidthold = bi.biWidth;
    long newbiWidth = bi.biWidth * nu;
    long newbiHeight = bi.biHeight * nu;

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

    // calculates new bisizeimage
    bi.biSizeImage = ((sizeof(RGBTRIPLE) * newbiWidth) + padding) * newbiHeight;
    // calculates new bfSize
    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
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
     for (int o = 0; o < nu; o++)
       {
        // 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);

            // copy the pixel nu times
            for (int l = 0; l < nu; l++)
            {
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }


        }
         for (int k = 0; k < padding; k++)
         {
             fputc(0x00, outptr);
         }
         if (o < nu -1)
         {
            fseek(inptr,-bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
         }

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




       }
    }


    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

1 Answer 1

1

Would be nice if you could describe the problem next time. Like does the programme crash? Is there a header mismatch (use cs50's peek as described in the "Testing" chapter)? Is the file size wrong? Does xxd show you wrong bytes? Does the file look weird in some way (with screenshot)?

If you had run peek on your output, you could have seen that you write an unaltered bi.biWidth and bi.biHeight, while the written fields should correspond to output size. You could keep input size in separate variables, calculate them from output size and factor, or create a completely different BITMAPINFOHEADER-type variable that you initialize to bi (so all the values are copied over), adjust that, and write it to disk instead of bi.

Also, your second fseek should be either in an else, or one } later, after that for loop.

You must log in to answer this question.

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