Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help with understanding bitmap locking

This thread is locked; no one can reply to it. rss feed Print
Help with understanding bitmap locking
jmasterx
Member #11,410
October 2009

Essentially I want to lock the bitmap and get the alpha component of each pixel.

I have the following code:

  //lock the bitmap
    ALLEGRO_LOCKED_REGION* region;
    region = al_lock_bitmap
      (m_bitmap,
      ALLEGRO_PIXEL_FORMAT_ABGR_8888,
      ALLEGRO_LOCK_READONLY);

    for(int y = 0; y < m_bitmapDimensions.y; ++y)
    {
      for(int x = 0; x < m_bitmapDimensions.x; ++x)
      {

      }
    }

But after that I'm stuck. I'm not sure how to get the 32 bit integer representing the color components, and then I'm unsure how the logical shifts work to extract the alpha in an endian independent way.

Thanks

Elias
Member #358
May 2000

ALLEGRO_LOCKED_REGION* region;
region = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_READONLY);
for(int y = 0; y < al_get_bitmap_height(bitmap); ++y) {
    unsigned char *row = region->data;
    row += y * region->pitch;
    uint32_t *pixel = row;
    for(int x = 0; x < al_get_bitmap_width(bitmap); ++x) {
        int alpha = *pixel >> 24;
        /* do something with alpha */
        pixel++;
    }
}
al_unlock_bitmap(bitmap);

[edit:]
A simpler solution:

ALLEGRO_LOCKED_REGION* region;
region = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_READONLY);
for(int y = 0; y < al_get_bitmap_height(bitmap); ++y) {
    unsigned char *pixel = region->data;
    pixel += y * region->pitch;
    for(int x = 0; x < al_get_bitmap_width(bitmap); ++x) {
        pixel += 3;
        int alpha = *pixel;
        /* do something with alpha */
        pixel++;
    }
}
al_unlock_bitmap(bitmap);

Both solution are endian independent (not that you will still find a lot of big endian hardware).

--
"Either help out or stop whining" - Evert

Matthew Leverton
Supreme Loser
January 1999
avatar

uint32_t *p = lock->data;
int a;

for each pixel: a = p[x] >> 24

at end of row: p += lock->pitch;

jmasterx
Member #11,410
October 2009

Thank you to both of you :)

Go to: