1

So I am really having a hard time with pset4 resize. After 2 days I finally managed to have it resize the smiley correctly, but the small image is still incorrectly displayed (black bg, with glitchy RGB lines tilting to the side). I figure that something is wrong with my padding or fseek(). I tried reasoning through both, but I couldn't grasp my mistake. It's probably staring me right in the face, but I just can't seem to find it. Help is much appreciated!

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

#include "bmp.h"

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

// (...) ensure proper usage

// remember filenames
char* infile = argv[2];
char* outfile = argv[3];

// (...) open input file 

// (...) open output file

//declare and initialize headers for old file
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;

// (...) read infile's headers

//alter header for resized image
BITMAPFILEHEADER bf_rs;
bf_rs = bf;
BITMAPINFOHEADER bi_rs;
bi_rs = bi;

bi_rs.biWidth = bi.biWidth * n;
bi_rs.biHeight = bi.biHeight * n;

    //determine these for computation later
    int sizeTriple = sizeof(RGBTRIPLE);
    int padding_rs =  (4 - (bi_rs.biWidth * sizeTriple) % 4) % 4;
    int padding =  (4 - (bi.biWidth * sizeTriple) % 4) % 4;
    int sizeScanline = (bi.biWidth * sizeTriple) + padding;

bi_rs.biSizeImage = (bi_rs.biWidth * padding_rs * sizeTriple) + bi_rs.biHeight;
bf_rs.bfSize = bi_rs.biSizeImage + sizeof(bf_rs) + sizeof(bi_rs);

// (...) ensure infile is (likely) a 24-bit uncompressed BMP 4.0

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++){
        for (int hMultiplier = 0; hMultiplier < n; hMultiplier++){ //repeat to multiply the height
            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++){
                for (int wMultiplier = 0; wMultiplier < n; wMultiplier++){
                    // temporary storage
                    RGBTRIPLE triple;

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

                    // write RGB triple to outfile
                    fwrite(&triple, sizeTriple, 1, outptr);

                    //reset cursur to previous position
                    fseek(inptr, -sizeTriple, SEEK_CUR);
                }

                // reset cursor position
                fseek(inptr, sizeTriple, SEEK_CUR);

                // Step over padding at the end of the line in input file
                fseek(inptr,padding, SEEK_CUR);

                //now put padding in resize file. First check if padding is needed
                if(padding_rs != 0){       
                    // then add padding
                    for (int k = 0; k < padding_rs; k++){
                        fputc(0x00, outptr);
                    }
                }
            }
        //reset cursur to previous position
        fseek(inptr, -sizeScanline, SEEK_CUR);
    }
    // reset cursor position
    fseek(inptr, sizeScanline, SEEK_CUR);
}

// (...) close files and return
}

1 Answer 1

0

I didn't go further, but the calculation you use for determining the value of bi_rs.biSizeImage is wrong.

Try it on paper or in your mind with a specific value for the height, width and padding, it'll help you find what's wrong.

Tell us if you still have issues after.

9
  • Thanks! I have resolved this to: bi_rs.biSizeImage = (bi_rs.biWidth * sizeTriple + padding) * abs(bi_rs.biHeight); But the error persists.
    – Eknaram
    Aug 23, 2016 at 12:12
  • @Eknaram (Please see this, mostly for the curly braces' position.) Look closely at where you are handling the padding. It might not be the right place ;)
    – Aizen
    Aug 23, 2016 at 12:28
  • Oh my! That did it, and I finally understand my error in reasoning. Thank you so much. :)
    – Eknaram
    Aug 23, 2016 at 13:20
  • @Eknaram You're welcome :)
    – Aizen
    Aug 23, 2016 at 13:23
  • So I was a little hasty. While it does resize the small.bmp correctly, check50 is still not happy. :) resize.c and bmp.h exist :) resize.c compiles :) doesn't resize 1x1-pixel BMP when n is 1 :( resizes 1x1-pixel BMP to 2x2 correctly when n is 2 :( resizes 1x1-pixel BMP to 3x3 correctly when n is 3 :( resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :) resizes 1x1-pixel BMP to 5x5 correctly when n is 5 :( resizes 2x2-pixel BMP to 4x4 correctly when n is 2 padding is now added just above the -sizeScanline
    – Eknaram
    Aug 23, 2016 at 13:26

You must log in to answer this question.

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