0

I've looked through a couple of threads and forum questions but I am not able to figure out what are the problems with my code. It resizes correctly but puts out random colors.

// 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: resize positive integer infile outfile\n");
    return 1;
}
int n = atoi(argv[1]);
if (n < 0 || n > 100)
{
    fprintf(stderr, "Usage: positive integer must be between 0 and 100\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 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;
}
// save input width and height
int oldWidth = bi.biWidth;
int oldHeight = bi.biHeight;

// resizing the width and height of outfile
bi.biWidth *= n;
bi.biHeight *= n;

// determine the old and new padding for scanlines
int oldpadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//TODO saving the old header files PROBLEM seems to be that I am working with new data for the input file

// upgrade SizeImage and Size
bi.biSizeImage = ((sizeof (RGBTRIPLE) *  bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

//declare an array for later use
RGBTRIPLE *row = malloc(sizeof(RGBTRIPLE) * bi.biWidth);

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

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        // put pixels into an array
        for (int l = 0; l < n; l++)
        {
            row [j] = triple;
            j++;
        }
        // for n times write the array plus padding
        for (int o = 0; o < n; o++)
        {
            // write array to outfile
            fwrite(&row, sizeof(RGBTRIPLE), 1, outptr);
            // add padding
            for (int p = 0; p < padding; p++)
                {
                    fputc(0x00, outptr);
                }
        }
    }

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

// free memory
free (row);

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

I am thankful for any suggestions.

1

For starters, it doesn't resize correctly. Next, the header isn't correct.

Now, there are two more complex issues. The code only writes out one pixel to the output file when it should write out the entire row.

Finally, it's writing out padding after each pixel instead of the end of each line of pixels. A restructuring of the for loops is in order.

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

1
  • What's wrong about the header and resizing? Do I need to manually update BITMAPFILEHEADER and BITMAPINFOHEADER? Thank you for your help already so far.
    – Dennis
    Nov 21 '18 at 11:02

You must log in to answer this question.

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