Page 1 of 1 [ 4 posts ]

 Print view Previous topic | Next topic

Author Message
 Posted: November 3rd, 2017, 5:50 pm

Joined: October 30th, 2017, 11:50 pm
Posts: 4
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:
const int padding = (4 - (width * 3) % 4) % 4;

Seemed a bit overwrought to me so I replaced it with:

Code:
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?

 Posted: November 3rd, 2017, 7:22 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2850
Location: Oklahoma, United States
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

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

 Posted: November 3rd, 2017, 10:08 pm

Joined: October 30th, 2017, 11:50 pm
Posts: 4
Thanks for clearing that up!

 Posted: November 4th, 2017, 3:42 am

Joined: February 28th, 2013, 3:23 am
Posts: 2850
Location: Oklahoma, United States
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.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Display posts from previous:  Sort by

 Page 1 of 1 [ 4 posts ]

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: