0

i am lost on Pset 4 Resize. It appears that the output file is not really larger and on top of that the pixels seem jumbled. If anyone could point me in the right direction of where I am wrong, would be much appreciated.

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

#include "bmp.h"



int main(int argc, char *argv[])
{   
// convert factor to atoi
int n = atoi(argv[1]);

// ensure proper usage
if (argc != 4 || n < 1 || n > 100)
{
    fprintf(stderr, "Usage: ./copy factor (between 1 and 100) infile outfile\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;
}

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

// Do the changes for the BITMAPINFOHEADER FIRST, since the BITMAPFILEHEADER is hierachily on a level above that
BITMAPINFOHEADER bioutput = bi;
bioutput.biWidth =  bi.biWidth * n;
bioutput.biHeight = bi.biHeight * n;
int paddingoutput = (4 - (bioutput.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bioutput.biSizeImage = ((sizeof(RGBTRIPLE) * bioutput.biWidth) + paddingoutput) * abs(bioutput.biHeight);

// Do the changes for the BITMAPFILEHEADER now;
BITMAPFILEHEADER bfoutput = bf;
bfoutput.bfSize = bioutput.biSizeImage + 54;

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{   
    // writes the line n number of times
    for (int j = 0; j < n; j++)
    {   

        // iterate over pixels in scanline
        for (int k = 0; k < bi.biWidth; k++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile n times

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

        }
        // add paddingoutput here
        for (int m = 0; m < paddingoutput; m++)
        {
            fputc(0x00, outptr);
        }

        if (j < n-1)
        {
            fseek(inptr, (54+ bi.biWidth * sizeof(RGBTRIPLE) + paddingoriginal)*i, SEEK_SET);
        }
        else
        {
            // skip over padding in original file if any is there, if any
            fseek(inptr, paddingoriginal, SEEK_CUR);
        }
    }


}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1

Problem 1: fseek(inptr, (54+ bi.biWidth * sizeof(RGBTRIPLE) + paddingoriginal)*i, SEEK_SET);
54 is for all intents and purposes, a constant.

Problem 2: It is writing out the headers of the original image to the resized image.

You must log in to answer this question.

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