0

I've been tackling the pset4 resize problem and I have run into a few issues. I have attached my code. I also don't really know how to stretch horizontally, but one thing at a time. It looks like I just made the bmp file bigger and moved columns of pixels.

#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 number 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)
{
    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);
//making resized bitmapfileheader
BITMAPFILEHEADER big_bf;
big_bf = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
//making resized bitmapinfoheader
BITMAPINFOHEADER big_bi;
big_bi = bi;

//changing the sizes with the scaling factor accordingly
big_bi.biWidth *= n;
big_bi.biHeight *= n;
int big_padding = (4 - (big_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
big_bi.biSize = ((sizeof(RGBTRIPLE) * big_bi.biWidth) + big_padding) * abs(big_bi.biHeight);
big_bf.bfSize = big_bi.biSize + sizeof(big_bf) + sizeof(big_bi);

// 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(&big_bf, sizeof(BITMAPFILEHEADER), 1, outptr);

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

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// iterate over infile's scanlines
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);

        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
    }

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

    // then add it back (to demonstrate how)
    for (int k = 0; k < big_padding; k++)
    {
        fputc(0x00, outptr);
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

For some reason my output image file is not recognized, but it is recognized when I comment out these two lines:

    big_bi.biSize = ((sizeof(RGBTRIPLE) * big_bi.biWidth) + big_padding) * abs(big_bi.biHeight);
    big_bf.bfSize = big_bi.biSize + sizeof(big_bf) + sizeof(big_bi);

I believe that I did the math correctly, but the output file is still not being recognized. If anyone can explain this to me, that'd be awesome. I am reading other stackexchange questions/answers as well as googling things, but everything is going over my head a bit.

1 Answer 1

1

Without giving too much away, take a look at what the size header variables in a bitmap file represent. There are 3 of them: bfSize, biSize and biSizeImage. Double check what each one means and then decide whether your calculations are correct or not.

1
  • Ugh. I feel so stupid. This is what I get for rushing. Thanks Ste. I'm fixing my code to use biSizeImage properly.
    – Peter K.
    Feb 15, 2017 at 3:29

You must log in to answer this question.

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