0

Check50 Message

I don't understand what I did wrong... Here's my code :

  // Copies a BMP file

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

#include "bmp.h"

int main(int argc, char *argv[])
{
    int n = atoi(argv[1]);
    // ensure proper usage
    if (argc != 4 && n < 101 && n > 0)
    {
        printf("Usage: resize n infile outfile\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 1;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 1;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // Header files resize to value
    int oldWidth = bi.biWidth;
    int oldHeight = bi.biHeight;
    int newWidth = oldWidth * n;
    int newHeight = oldHeight * n;

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

    // Padding
    int padding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4 ;
    int outpadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4 ;

    // New image size
    bi.biHeight = newHeight;
    bi.biWidth = newWidth;
    bi.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + outpadding) * abs(newHeight);
    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);

     // allocate a memory to store one scanline
    RGBTRIPLE scanline[newWidth * sizeof(RGBTRIPLE)];

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(oldHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < oldWidth; j++)
        {
            for (int k = 0; k < oldWidth; k++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

                // write RGB triple to outfile
                for (int l = 0; l < n; l++)
                {
                    scanline[(j * n) + k] = triple;
                }

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

            // write the current scanline
            for (int m = 0; m < n; m++)
            {
               fwrite(scanline,sizeof(RGBTRIPLE),newWidth, outptr);
            }


            // add padding
            if (outpadding != 0)
            {
                for (int k = 0; k < outpadding; k++)
                {
                    fputc(0x00, outptr);
                }
            }

        }



    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
6
  • There are a number of different issues here. Instead of going into detail, I'll ask one question. Instead of just looking at the image produced, have you looked at the raw data in the file? You should be able to sort out everything by looking at both the actual data generated and the volume of data written to the file. Several ideas about where to look should come to mind. Start with a multiplier of 1 and get that to work first.
    – Cliff B
    May 18 '19 at 7:35
  • It works now with n = 1 but still doesn't work for the others .... I don't know what to do next ... I changed my j loop so j <= n and in my 'L' loop it's now "scanline[(l * j) + k ] = triple; "
    – n1coii
    May 18 '19 at 17:04
  • The j loop seems useless to me. What about scanline[k*n+l]?
    – Blauelf
    May 18 '19 at 22:58
  • No, it doesn't work correctly for 1. While the image looks right, the image DATA contains twice as much data as it should. Since the header controls how much data is displayed, the excess data is not shown in the image. Like I said, look at the raw data, not the image.
    – Cliff B
    May 19 '19 at 0:00
  • Sorry for the stupid question... how do I look at the image data ?
    – n1coii
    May 19 '19 at 1:18

You must log in to answer this question.

Browse other questions tagged .