2

First of all thanks for your time.

Taking cs50 through edX - struggling as all alone here and videos / lectures are burnt out for information - need real people! Online cs50 really does assume a lot of the learner.

I'm struggling big time with recover, just took a 5 day break. currently trying to create the formulas for the new files headers, as follows:

// new width
bi.biWidth = bi.Width * n;
// new height
bi.biHeight = bi.Height * n;
// The new size, in bytes, of the image
bi.biSizeImage = abs(bi.biSizeImage) * n;
// The new size, in bytes, of the bitmap file (includes headers)
bf.bfSize = ((abs(bi.SizeImage) * n) + (bf + bi));

are these correct? if not please help, don't be cryptic, i seriously am at wits end :)

thanks

1 Answer 1

21

Not exactly. If you read the BITMAPINFOHEADER like so

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

to get the width, the correct is bi.biWidth. Instead you have bi.Width which doesn't exist.

If you then create a new BITMAPINFOHEADER like so

BITMAPINFOHEADER out_bi;

your first two lines should be:

out_bi.biWidth = bi.biWidth * n;

and

out_bi.biHeight = bi.biHeight * n;

If you don't create a separate header, you should. You will make many more mistakes if you use the one from the in-file directly for your out-file.

Also your

bi.biSizeImage = abs(bi.biSizeImage) * n;

and

bf.bfSize = ((abs(bi.SizeImage) * n) + (bf + bi));

are totally wrong.

biSizeImage

The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps.

source: MSDN

If you have a 3 x 3 bmp file, and multiply it by 2, even if its size was 9 (it isn't), the new size wouldn't be 18 as you have 9 * 2. It would be 6 x 6, so 6 * 6 == 36. But you also have to consider the padding (which may be different from the in-file's), and the fact that the size is counted in bytes and each pixel is 3 bytes. Each padding pixel is 1 byte. I'll leave the math to you. It's not that hard.

bfSize

The size, in bytes, of the bitmap file.

source: MSDN

So basically, bfSize is biSizeImage plus the bfOffBits. I'll leave this math to you too.

Edit for example

infile (3 × 3) + 1 padding per line

###p
###p
###p

outfile (6 × 6) + 2 padding per line

######pp
######pp
######pp
######pp
######pp
######pp
#: pixel (3 bytes)
p: padding (1 byte)

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

7
  • Good answer; it's good you made the distinction that biWidth and biHeight are measured in pixels and that each pixel is 3 bytes, while padding itself is measured in bytes already.
    – Sam Gerber
    Commented Sep 21, 2015 at 18:39
  • 1
    Thanks, this clears quite a lot up. very helpful :) Commented Sep 22, 2015 at 14:44
  • Is this formula correct for resized image bi_resize.biSizeImage=(bi_resize.biWidth*sizeof(RGBTRIPLE)+padding_new)*(abs(bi_resize.biHeight)*sizeof(RGBTRIPLE)) Commented Jul 1, 2016 at 8:39
  • Why are we using absolute function for bi.biSize? Can it be negative like bi.biHeight? Commented Dec 22, 2016 at 12:47
  • Indeed you don't have to use abs(), as biSize and biSizeImage are both non-negative integers representing number of Bytes. If you read my answer more carefully you will see that the code you are referring to, is from the OP's question, and I specify that it's wrong.
    – ChrisG
    Commented Dec 22, 2016 at 18:58

You must log in to answer this question.

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