Performing Graphics Operations with BREW Bitmaps, Page 2
Of course, at times you might want to do more to a bitmap than simply set a handful of pixels or bit blit in another bitmap. You actually can use any of the BREW graphics primitives, including IImage, IDisplay, and IGraphics with a bitmap, although the process is a little awkward. To do this, you want to replace the bitmap used by the display with a target bitmap, perform whatever drawing you like, and then replace the default display bitmap. The easiest way to do this is to create a compatible bitmap of the desired size, and then use IDISPLAY_SetBitmap. Once you're done, invoke IDISPLAY_SetBitmap again, this time passing NULL to restore the original bitmap:
IDisplay *piDisplay = p->a.m_pIDisplay; IBitmap *piBitmap, *piScreenBitmap; GETRAND( abyRand, sizeof( abyRand ) ); IDISPLAY_ClearScreen( piDisplay ); IDISPLAY_GetDeviceBitmap( piDisplay, &piScreenBitmap ); IBITMAP_CreateCompatibleBitmap( piScreenBitmap, &piBitmap, WIDTH, HEIGHT ); IBITMAP_Release( piScreenBitmap ); IDISPLAY_SetBitmap( piDisplay, piBitmap ); // Do all your drawing here as usual. IDISPLAY_SetBitmap( piDisplay, NULL );
Accessing All the Pixels in a Bitmap
Sometimes, you might want to access all the pixels in a bitmap. This is the function of IDIB, an interface with public data you can access to obtain the pixels within a bitmap. IDIB has the following fields that are of interest:
- The pBmp field points to the first pixel in the top row of the pixel data.
- The pRGB field points to the color palette for indexed bitmaps, whereas the cntRGB field indicates the number of colors in the color palette. Beware—the colors here are in memory order, not necessarily RGB! On little-endian processors, be sure to use NHTOL to convert the pixel value.
- The ncTransparent field is the color representing transparency in the bitmap.
- The cx and cy fields indicate the width and height of the bitmap.
- The nPitch field is the length of a row of pixels in the pixmap in bytes.
- The nDepth field indicates the size of each pixel in bits; the nColorScheme indicates the encoding used to represent a color, such as 5 bits each for the red, green, and blue pixels.
Using an IDIB takes a bit of fussing because there's no guarantee what sort of bit depth (denoted by the nDepth field) and color encoding (encoded by the nColorScheme field) you'll get when you get an IDIB. The algorithm for reading pixels is essentially the same regardless, however:
- Beginning at pBmp, read a row of nPitch pixels.
- For each nDepth bits, if cntRGB is non-zero, look up the appropriate color in the pRGB array. If cntRGB is zero, decode the bits as a color in the format specified by nColorScheme.
- Repeat Steps 1 and 2 for cy iterations.
The IDIB interface has many limitations, making it essentially a read-only medium. Specifically, you can't draw to it using IDisplay, IGraphics, or any of the other drawing mechanisms, and in present versions of BREW, IBITMAP_BltOut is unsupported as well, making it impossible to blit out of an IDIB into another bitmap. If you want to do a lot of manipulation on an IDIB and then present the results on the handset, be prepared to do a bit of hacking—one approach is to create a Windows BMP bitmap using the IDIB data, and then invoke BREW's CONVERTBMP to obtain a bitmap again. Although this should definitely work in principle, be prepared for performance issues because you're going to impose a great deal of memory shuffling: first, the shuffling you do on the IDIB itself preparing the image, and then the conversion to a Windows BMP (typically setting up the header and then using MEMCPY to copy the pixmap), and finally a conversion back to a native bitmap again.
Access to raw bitmaps can be a crucial part of application development when there's something you need to do that isn't supported by BREW's IDisplay or IGraphics APIs. With the information you now have in hand, you should be well prepared to harness the power of BREW's bitmap interfaces.
About the Author
Ray Rischpater is the chief architect at Rocket Mobile, Inc., specializing in the design and development of messaging and information access applications for today's wireless devices. He is the author of several books on software development, including eBay Application Development and Software Development for the QUALCOMM BREW Platform, both available from Apress, and is an active Amateur Radio operator. Contact Ray at email@example.com.