0

After a lot of time, my resize finally works for the original conditions, that is, scale of 4. However, when I try a scale of 3 or 5, my program suddenly contains a lot of black slides, instead of pixels. I think this has to do with padding, and my padding is probably completely wrong, but I don't know in what way,

The relevant code is:

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

BITMAPFILEHEADER bf_new = bf;
BITMAPINFOHEADER bi_new = bi;

// determine padding for scanlines
int padding =  (4 - ((bi.biWidth * sizeof(RGBTRIPLE)) % 4)) % 4;
int newpadding = (4 - (scale*(bi.biWidth * sizeof(RGBTRIPLE)) % 4)) % 4;

//Change variables in BITMAPFILEHEADER and BITMAPINFOHEADER
bi_new.biWidth = bi.biWidth*scale; 
bi_new.biHeight = 0 - (abs(bi.biHeight)*scale);
bf_new.bfSize = 54+((bi.biWidth+padding)*abs(bi.biHeight)*sizeof(RGBTRIPLE));
bi_new.biSizeImage = ((bi.biWidth+padding)*(bi.biHeight)*sizeof(RGBTRIPLE));

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{   
    //sets position after every line
    for(int l = 0; l < scale;l++){
        if(l > 0){
            fseek(inptr,-bi.biWidth*scale,SEEK_CUR);  
        }
        // 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);

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

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


        // then add it back (to demonstrate how)
        for (int k = 0; k < newpadding; k++)
        {
            fputc(0x00, outptr);
        }
    }
}
1

Let's assume the original image is a 3x3 BMP which you are scaling by a factor of 3.

In your program, after reading in the bytes of a row, you are 9 bytes in. You then skip over the padding, reaching 12 bytes in. When you rewind to write the row again, you are rewinding width*scale, or 9 bytes in this case. This results in a skipped pixel every time you rewrite a row, and is additive over your program.

Why does it work with a scale factor of 4?

You read each byte in the row (9 bytes), then skip beyond the padding (3 bytes) in the original image to end up 12 bytes in. When you go to write a new row, you rewind your file pointer width*scale, which happens to be 12 bytes.

| improve this answer | |
  • This clears the confusion, thank you :) – Matus Mikus Nov 21 '14 at 17:03

You must log in to answer this question.

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