0

I've been struggling with Resize on Pset4 for awhile now and can't quite seem to figure out what I am doing wrong. I am trying to implement the Recopy method with the following psuedocode:

for each row for n-1 times write pixels, padding to outfile send infile cursor back write pixels, padding to outfile skip over infile padding

I've attached my code below, does anyone have any guidance?

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

#include "bmp.h"

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

// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(argv[1]);
printf("%i\n", n);
// 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;
}

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

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

// create header for output file
BITMAPFILEHEADER bfo;
bfo = bf;
BITMAPINFOHEADER bio;
bio = bi;
bio.biWidth = bi.biWidth * n;
bio.biHeight = bi.biHeight * n;

int resized_padding = (4 - (bio.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bio.biSizeImage = (bio.biWidth * abs(bio.biHeight) * 3) + resized_padding*abs(bio.biHeight); // in bytes
bfo.bfSize = bio.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // the size of the picture in bytes

// 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++)
{
    // repeat n-1 times to resize image
    for (int x = 0; x < n-1; x++)
    {
        for (int j = 0; j < bio.biWidth; j++)
        {
            printf("%i\n", bio.biWidth);
            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile for the amount of times needed
            for (int y = 0; y < n-1; y++)
            {
              fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
    // write padding to outfile
    for (int k = 0; k < resized_padding; k++)
    {
        fputc(0x00, outptr);
    }
    //send infile cursor back
    fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
    }

    // write pixels and padding to outfile
    for (int j = 0; j < bio.biWidth; j++)
     {
          // temporary storage
        RGBTRIPLE triple;

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

        // write RGB triple to outfile for the amount of times needed
        for (int z = 0; z < n-1; z ++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }
     for (int k = 0; k < resized_padding; k++)
    {
        fputc(0x00, outptr);
    }
    // skip over padding
    fseek(inptr, padding, SEEK_CUR);

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1 Answer 1

0

There are several issues, but I'm going to give you a push in the right direction. First, try simply running ./resize 1 small.bmp s2.bmp A scaling factor of 1 will simplify things a lot for now.

First, take a careful look at the output file's image data using xxd -c 12 -g 3 -s 54 s2.bmp Compare it to the input file using xxd again. You should immediately notice that it only has padding.

Here's the big hint. Take a really close look at the limits on your for loops, x < n-1 or similar. If n is 1, does the loop ever run?

From there, you'll need to look closely at the image data. Once you start outputting data, it will change dramatically. I'll leave it to you to sort it out at that point.

Once you get this working, then change to n=2. Start with the header and see if it's correct. 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 .