0

Ok guys, so i have been having some problem with pset4 resize. Everything is fine except my output image is upshifted by 1 pixel. Here's my code:

#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 n infile outfile\n");
    return 1;
}
// remember filenames
int scale=atoi(argv[1]);
if(scale>100 || scale<1)
{
    return 5;
}
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;
}
BITMAPFILEHEADER newbf=bf;
BITMAPINFOHEADER newbi=bi;

//modify the BITMAPINFOHEADER for scaling
newbi.biWidth=(bi.biWidth*scale);
newbi.biHeight=(bi.biHeight*scale);
newbi.biSizeImage=((sizeof(RGBTRIPLE) * newbi.biWidth + (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4)) * abs(newbi.biHeight);
newbf.bfSize=(newbi.biSizeImage + 54);


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

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


// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newpadding = (4-(newbi.biWidth * sizeof(RGBTRIPLE)) %4 ) %4;
int ver=(sizeof(RGBTRIPLE)*bi.biWidth) + newpadding;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int l=0;l<scale;l++)
    {

    // 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);
        for(int k=0;k<scale;k++)
        {
        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

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

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


    }

    fseek(inptr, -ver ,SEEK_CUR); 

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

HALP!

0

The input file position reset does not look right to me. Instead of

fseek(inptr, -ver ,SEEK_CUR);

in the outermost loop (executed once per input line), I'd expect something like

if (l < scale-1)
{
    fseek(inptr, -ver ,SEEK_CUR);
}

in the second-to-outermost loop (executed once per output line). That resets the position every but the last time that input line should be read.

You must log in to answer this question.

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