Allegro.cc Forums » Programming Questions » Determine if an image is solid

 Edgar Reynaldo Major Reynaldo May 2007 Nazerith said: 1) Test if a bitmap is a solid color.2) Test if two bitmaps are equal. You'll have to do both yourself. The first is easy, and the second is only slightly harder.1) ```int is_bitmap_solid_color(BITMAP* bmp , int color) { if (!bmp) {return 0;} for (int y = 0 ; y < bmp->h ; ++y) { for (int x = 0 ; x < bmp->w ; ++x) { if (color != getpixel(bmp , x , y)) {return 0;} } } return 1; } ``` 2) ```int are_bitmaps_equal(BITMAP* b1 , BITMAP* b2) { if (!b1 || !b2) {return 0;} if (b1 == b2) {return 1;} if (bitmap_color_depth(b1) != bitmap_color_depth(b2)) {return 0;} if ((b1->w != b2->w) || (b1->h != b2->h)) {return 0;} for (int y = 0 ; y < b1->h ; ++y) { for (int x = 0 ; x < b1->w ; ++x) { if (getpixel(b1,x,y) != getpixel(b2,x,y)) {return 0;} } } return 1; } ``` I just wrote them myself, since they only took a couple minutes.
 Edgar Reynaldo Major Reynaldo May 2007 You didn't specify A4 or A5, so I just used A4.Using A5 will be somewhat different :1) ```int is_bitmap_solid_color(ALLEGRO_BITMAP* bmp , ALLEGRO_COLOR color) { if (!bmp) {return 0;} al_lock_bitmap(bmp , al_get_bitmap_format(bmp) , ALLEGRO_LOCK_READONLY); for (int y = 0 ; y < al_get_bitmap_height(bmp) ; ++y) { for (int x = 0 ; x < al_get_bitmap_width(bmp) ; ++x) { if (color != al_get_pixel(bmp , x , y)) { al_unlock_bitmap(bmp); return 0; } } } al_unlock_bitmap(bmp); return 1; } ``` 2) #SelectExpand 1int are_bitmaps_equal(ALLEGRO_BITMAP* b1 , ALLEGRO_BITMAP* b2) { 2 if (!b1 || !b2) {return 0;} 3 if (b1 == b2) {return 1;} 4 if (al_get_bitmap_format(b1) != al_get_bitmap_format(b2)) {return 0;} 5 if ((al_get_bitmap_width(b1) != al_get_bitmap_width(b2)) || 6 (al_get_bitmap_height(b1) != al_get_bitmap_height(b2))) {return 0;} 7 8 al_lock_bitmap(b1 , al_get_bitmap_format(b1) , ALLEGRO_LOCK_READONLY); 9 al_lock_bitmap(b2 , al_get_bitmap_format(b2) , ALLEGRO_LOCK_READONLY); 10 11 for (int y = 0 ; y < al_get_bitmap_height(b1) ; ++y) { 12 for (int x = 0 ; x < al_get_bitmap_width(b1) ; ++x) { 13 if (al_get_pixel(b1,x,y) != al_get_pixel(b2,x,y)) { 14 al_unlock_bitmap(b1); 15 al_unlock_bitmap(b2); 16 return 0; 17 } 18 } 19 } 20 al_unlock_bitmap(b1); 21 al_unlock_bitmap(b2); 22 return 1; 23} A5 takes more code.
 Audric Member #907 January 2001 Looks like ML already posted some code for fast image comparison:http://www.allegro.cc/forums/thread/606084/898790#targetIn the linked post he compared only edges, ie. lines on top, bottom, left and right.The critical code is: ``` // Do once b1_lock = al_lock_bitmap(b1, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY); b2_lock = al_lock_bitmap(b2, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY); b1_data = b1_lock->data; b2_data = b2_lock->data; // Do for each y line of bitmaps: if (memcmp( (uint8_t*)&b1_data[x1] + b1_lock->pitch * (y1+y), (uint8_t*)&b2_data[x2] + b2_lock->pitch * (y2+y), width)) {...} // is different ``` (with x1,y1 the coordinates of the block in bitmap b1, and x2,y2 the coordinates in bitmap b2)