Page 1 of 1

Padding calculation in I10 Sprite Drawing / Bitmap Loading

Posted: November 3rd, 2017, 5:50 pm
by HavocVulture
I just completed the homework for I10, but while working on it I was confused by the padding formula. In the video the calculation used is:

Code: Select all

const int padding = (4 - (width * 3) % 4) % 4;
Seemed a bit overwrought to me so I replaced it with:

Code: Select all

const int padding = (width * 3) % 4;
This worked with all the supplied 24-bit images. Is there something I'm missing? Is there a case where the simpler formula fails?

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Posted: November 3rd, 2017, 7:22 pm
by albinopapa
Let's look at the possible values of width in regards to multiples of 4.

width = 12
(12 * 3) % 4 = 36 % 4 = 0 // Good

width = 13
(13 * 3) % 4 = 39 % 4 = 3 // Not good, we only need a padding of 1 here to make it 40

width = 14
(14 * 3) % 4 = 42 % 4 = 2 // Good

width = 15
(15 * 3) % 4 = 45 % 4 = 1 // Not good, we need a padding of 3 here to make it 48

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Posted: November 3rd, 2017, 10:08 pm
by HavocVulture
Thanks for clearing that up!

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Posted: November 4th, 2017, 3:42 am
by albinopapa
That only explains part of it, the 4- part, the reason for the last %4 is in cases where padding = 0, 4 - 0 = 4, but you don't need 4 padding bytes so %4 would make it 0.