0

Hi I've been working on this resize.c from pset4 for a long time now but the images are still being copied over upside-down! Thanks for your help!!

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

#include "bmp.h"

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

// remember filenames and n
int multiple = atoi (argv[1]);
char* infile = argv[2];
char* outfile = argv[3];

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("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;
}


// remember original height/width
int orgheight = abs(bi.biHeight);
int orgwidth = bi.biWidth;


// set new height/width
int newheight = abs(bi.biHeight) * multiple;
int newwidth = bi.biWidth * multiple;

bi.biHeight = newheight;
bi.biWidth = newwidth;

// remember original padding and determine new
int orgpadding =  (4 - (orgwidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newpadding =  (4 - (newwidth * sizeof(RGBTRIPLE)) % 4) % 4;

// updating rest of BITMAPFILEHEADER and BITMAPINFOHEADER
bi.biSizeImage = ((newwidth + newpadding) * newheight * sizeof(RGBTRIPLE));
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// 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 -- moves down the rows
for (int i = 0; i < orgheight; i++)
{
    // copies row n times
    for (int a = 0; a < multiple; a++)
    {
        // iterating over each pixel in the row
        for (int j = 0; j < orgwidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile n times
            for (int b = 0; b < multiple; b++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

        // add padding
        for (int k = 0; k < newpadding; k++)
        {
            fputc(0x00, outptr);
        }

        // go to begining of line
        fseek(inptr, -orgwidth * 3, SEEK_CUR);
    }

    // go to next line
    fseek(inptr, (orgwidth * sizeof(RGBTRIPLE)) + orgpadding, SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

0

The fact that the new image is upside-down is a really big clue. Have you taken a close look at the results? Did you, perhaps, try creating an image of scale = 1 and then comparing the headers of the input and output files using the peek tool?

If you do, I believe you should zero in on the problem very quickly. If not, leave a message and I'll give you a better pointer. ;-)

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

1
  • Thanks Cliff got it all working great!!! – R. Baker Dec 13 '16 at 23:28

You must log in to answer this question.

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