0

include

include

include

include "bmp.h"

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

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

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("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,bf_copy;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bf_copy = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,bi_copy;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bi_copy = bi;
bi_copy.biHeight = n * bi.biHeight;
bi_copy.biWidth = n * bi.biWidth;
int inpadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpadding =  (4 - (bi_copy.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi_copy.biSizeImage = (bi_copy.biWidth + outpadding) * abs(bi_copy.biHeight) * 3;
bf_copy.bfSize = bi_copy.biSizeImage + 54;
// 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_copy, sizeof(BITMAPFILEHEADER), 1, outptr);

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

// determine padding for scanlines


// iterate over infile's scanlines
RGBTRIPLE array[bi.biWidth];
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // 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);
        array[j] = triple;
    }
    for(int a = 0;a < bi.biWidth;a++)
    {
        printf("%d\n",array[a].rgbtBlue);
        printf("%d\n",array[a].rgbtGreen);
        printf("%d\n",array[a].rgbtRed);
        printf("\n");
    }

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

    for(int m = 0;m < n;m++)
    {
        for(int l = 0; l < bi.biWidth;l++)
        {
            fwrite(&array[l], sizeof(RGBTRIPLE), n, outptr);
        }
        for (int k = 0; k < outpadding; k++)
        {
            fputc(0x00, outptr);
        }
    }

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

The program is compiling correctly but when I execute it ./resize 4 small.bmp large.bmp the large.bmp file is not coming correctly. There are black lines coming in the midddle. Whereas acc to code padding = 0. And also my code is running correctly till before for(int m = 0 ,m

1 Answer 1

0

I see two problems in your code. First, certain header values are not being correctly calculated. You should be able to resolve this on your own. Try running ./resize 1 small.bmp s2.bmp and compare the headers. The problem should be obvious to you.

Second, look at the following line:

fwrite(&array[l], sizeof(RGBTRIPLE), n, outptr);

You've misunderstood how fwrite() works. The third parameter, n, is not how many times to write the source. It says how many consecutive elements of the second parameter, sizeof(RGBTRIPLE), to write. For demonstration purposes, let's say that it was working with characters instead. Say that n = 3 and l=2. and the array contained abcdef. It looks like you think it would print ccc, but in fact, it would print cde.

There may be other issues, but this will get you going.

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 .