0

I have looked at a variety of other questions about getting the wrong colors, so I know that I have some sort of padding issue, but I have no idea what exactly it is/how to fix it. I get the correct size, and the first line of the larger image is correct, but after that the colors are wrong. Here is my code:

// Resizes a BMP file

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

#include "bmp.h"

int main(int argc, char *argv[])
{
// verify correct number of arguments
if (argc != 4)
{
    fprintf(stderr, "Usage: ./resize n infile outfile\n");
    return 1;
}

// remember size variable n
int n = atoi(argv[1]);

// verify n is within bounds
if (n > 100 || n < 0)
{
    fprintf(stderr, "%i is not within bounds\n", n);
    return 2;
}

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

// open infile
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s. \n", infile);
    return 3;
}

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

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

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

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

// define old dimensions
int oldWidth = bi.biWidth;
int oldHeight = bi.biHeight;

// determine new dimensions
int newWidth = oldWidth * n;
int newHeight = oldHeight * n;

// update dimensions
bi_new.biWidth = newWidth;
bi_new.biHeight = newHeight;

// determine padding
int oldpadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newpadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// determine new image size
bi_new.biSizeImage = (((sizeof(RGBTRIPLE) * newWidth) + newpadding) * abs(newHeight));

// determine new file size
bf_new.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

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

            // resize pixels
            for (int k = 0; k < n; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

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

        // add it back in
        for (int l = 0; l < newpadding; l++)
        {
            fputc(0x00, outptr);
        }
    }
}

//close infile
fclose(inptr);

//close outfile
fclose(outptr);

// success!
return 0;
}

I also have some screenshots of how the images turn out when I try to use n = 2. They turn out fine when I use n = 1, but when I try to use anything past that, although the size comes out fine, the colors do not.

small.bmp scaled by a factor of 2

smiley.bmp scaled by a factor of 2

1 Answer 1

0

There are a few problems here. First, there's a header calculation that is in error, but is easily fixed. I'll let you sort that out.

Second, there is indeed a padding issue.

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

Which padding should be used to move around in the input file?

Finally, the code needs to generate n copies of each line. There is no code to handle this, yet. Happy coding! ;-)

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 .