0

I don't know what went went wrong! I'd understand if it was giving me coloured blocks and it's messed up in some order but it's just white and grey?

#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 *infile = argv[1];
    char *outfile = argv[2];
    int factor = atoi(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);

    int oldheight = bi.biHeight;
    int oldwidth = bi.biWidth;
    bi.biWidth = bi.biWidth * factor;
    bi.biHeight = bi.biHeight * factor;

    int padding = (4 - ( bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;


    bi.biSizeImage = (sizeof(RGBTRIPLE) * bi.biWidth * bi.biHeight) + (padding * bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    // 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;
    }

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

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

//for each row

for (int i = 0; i < oldheight; i++)
{
    int z = 0;
    RGBTRIPLE *t = malloc((sizeof(RGBTRIPLE) * factor * oldwidth));

//for each pixel
    for (int j = 0; j < oldwidth; j++)
    {
        RGBTRIPLE triple;

        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        //put what's in triple in malloc n times
        for (int f = 0; f < factor; f++)

        {
            *(t + (z * sizeof(RGBTRIPLE))) = triple;
            z++;
        }
    }

    for (int q = 0; q < factor; q++)

    {
        fwrite(&t, sizeof(RGBTRIPLE) * factor * oldwidth, 1, outptr);

        for (int y = 0; y < padding; y++)

        {
            fputc(0x00, outptr);
        }

        fseek(inptr, padding, SEEK_CUR);
    }

    free(t);

}

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

This code has a number of issues. I'll point some of them out, just to get you going.

For starters, the code has the parameters in the wrong order. The sizing factor should be the first parameter, not last.

Second, some of the header fields are not being correctly calculated. They have a big impact on how the file is displayed. Another good reason why it's good to look at both the image and the raw data of the image. BTW, did you verify that the output file header data was correct?

Third, padding is only calculated for the output file, not for the input file. The padding for the two files may or may not be the same, so they need to be calculated separately.

The "image" that you're seeing is nothing more than garbage data - padding on the disk at the end of a file to fill out a sector. The code, as posted, will never write data to the image section of an output file! If you look at the size of the files, you'll see they're no larger than the headers. You need to run the program in debug50 to discover exactly where and why this is happening!!!!

Once that last issue is fixed, there are more problems waiting to be resolved, but you need to get to that point and work through them, or at least give it a good try. Debugging programs is one of the most important skills a programmer needs. It includes locating and identifying problems, as much as solving them!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance! ;-)

You must log in to answer this question.

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