The 'L' in '31L' and '32L' is a C/C++ convention as a way to specify a 
constant as a 'long' (32-bit signed value).  Since you're probably dealing 
with images aren't that large, you can remove the 'L' from the numbers to do 
'integer' math.

Yep, you can use this calculation for both 1bpp and 8bpp (provided what you 
need is 32-bit alignment).

1bpp example:
ROWBYTES(75, 1)
WIDTHBYTES(75)
(75 + 31) / 32 * 4
106 / 32 * 4
3 * 4
12

8bpp example:
ROWBYTES(75, 8)
WIDTHBYTES(600)
(600 + 31) / 32 * 4
631 / 32 * 4
19 * 4
76

Here's a breakdown of what the algorithm is doing:
#define ROWBYTES(cx, bpp)   WIDTHBYTES(cx * bpp)

This macro takes the number of pixels in a row (cx) and multiplies it by the 
number of bits per pixel (bpp).  The result is the number of 'bits per row', 
which is then passed to the WIDTHBYTES macro.

And here's the breakdown of the WIDTHBYTES macro:
#define WIDTHBYTES(bits)        (((bits) + 31L) / 32L * 4)

'(bits + 31) / 32L' calculates the number of 32-bit values that are required. 
 Note that you can't just use the calculation 'bits / 32' because you could 
have bits that fill only a portion of a 32-bit value, and they would not be 
accounted for if you only did a division.  To get around this, you add 31 and 
let the division by 32 truncate the value to a whole number.

Then multiply the value by 4 to convert from 'number of 32-bit values' to 
'number of bytes'.


For 16-bit alignment, you're going to want a slightly different macro for 
WIDTHBYTES.  I think that this is the one you want since it gives you a 
result of '10' instead of '12' for the 1bpp example.

//Calculate DWORD-aligned bytecount based on a bitcount.
#define WIDTHBYTES(bits)        (((bits) + 15L) / 16L * 2)

-Pete


>  Hi Pete,
>  Thanks for your reply, but I don't quite understand the calculation.
>  I realise that the 8bpp played some part in the calculation, but in your
>  WIDTHBYES macro, what does the "L" refer to? Would I be able to use that
>  calculation for both 1bpp and 8bpp?
>  How does it work out that for a 75 pixel wide bitmap, rowBytes is 10 for
>  1bpp, and 76 for 8bpp?
>  Thanks,
>  Trung
>  
>  
>  
>  From: GreatOwlS
>  Date: Tue, 13 Jun 2000 10:35:15 
>  
>  Each bitmap row is probably aligned to a 32-bit boundary (don't know for
>  sure 
>  as I've never looked into it, but its not unreasonable).  Your results are 
>  consistent with bitmap depths at various bits-per-pixel (in your case, 
1bpp 
>  when < OS3.5 and 8bpp when OS3.5).
>  
>  The calculation for rowsize that you should be using is something like
>  
>  //Calculate DWORD-aligned bytecount based on a bitcount.
>  #define WIDTHBYTES(bits)        (((bits) + 31L) / 32L * 4)
>  
>  //Calculate DWORD-aligned bytecount based on pixel count and bitcount per 
>  pixel.
>  #define ROWBYTES(cx, bpp)   WIDTHBYTES(cx * bpp)

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to