Re: [Algorithms] OT: How to read a bitmap of arbitrary size?
Brought to you by:
vexxed72
From: Klaus H. <k_h...@os...> - 2000-08-13 20:16:58
|
Well, this is where it gets very much off-topic :) So I'll try to answer your question with a single mail. If you still don't get it, then please mail me off-line. First off, let's define: bpr = bytes_per_pixel * pixels_per_row Now assume that you want to round bpr *down* to the nearest 4, if and only if bpr is not a multiple of 4. This can be done, by setting the two least-significant bits of bpr to 0. Assuming that you use a postive 32-bit integer, you can achieve this as follows: bpr_rounded_down = bpr & (~3); <=> bpr_rounded_down = bpr & 0xFFFFFFFC; However, in order to compute the pitch, you need to round *up* to the nearest 4, if and only if bpr is not a multiple of 4. In order to do this, we first add 3 to bpr, and call the result bpr'. Examples: bpr = 16 => bpr' = bpr + 3 = 19 bpr = 17 => bpr' = bpr + 3 = 20 bpr = 18 => bpr' = bpr + 3 = 21 bpr = 19 => bpr' = bpr + 3 = 22 bpr = 20 => bpr' = bpr + 3 = 23 Now you use the above approach, and round bpr' *down* to the nearest 4 (which results in the pitch you are looking for): bpr = 16 => pitch = bpr' & (~3) = 16 bpr = 17 => pitch = bpr' & (~3) = 20 bpr = 18 => pitch = bpr' & (~3) = 20 bpr = 19 => pitch = bpr' & (~3) = 20 bpr = 20 => pitch = bpr' & (~3) = 20 So the formula for the pitch is: pitch = (bpr + 3) & (~3); Another way to implement this would be: pitch = (bpr + 3) - ((bpr + 3) % 4); <=> pitch = (bpr + 3) - ((bpr + 3) & 3); HTH, Niki PS: I'm not going to answer the question, why you need to add 3, instead of 4 :) PPS: Binary operations, like AND, OR, XOR, NOT, et cetera, are very useful, and it makes a lot of sense to learn more about them. ----- Original Message ----- From: Pai-Hung Chen <pa...@ac...> To: <gda...@li...> Sent: Sunday, August 13, 2000 8:31 PM Subject: Re: [Algorithms] OT: How to read a bitmap of arbitrary size? > Hi, > > That explains everything! :-) Thanks a lot! But could you explain what's > the meaning of ((bytes_per_pixel * pixels_per_row) + 3) & (~3), specifically > the ...+ 3) & (~3) part? > > Thank you, > > Pai-Hung Chen |