Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Check to see if a bitmap is a solid color

Credits go to miran for helping out!
This thread is locked; no one can reply to it. rss feed Print
Check to see if a bitmap is a solid color
Ceagon Xylas
Member #5,495
February 2005
avatar

Like the topic title indicates, I'm asking what is the fastest way to check if a bitmap's a solid color?
I'm trying to check and see if a 32x32 bitmap is solid magic pink.

miran
Member #2,407
June 2002

EDIT: nm, I misunderstood the question.

The real answer: two for loops.

--
sig used to be here

Ceagon Xylas
Member #5,495
February 2005
avatar

And getpixel I assume? I guess it's not too slow for only loading bitmap routines.

miran
Member #2,407
June 2002

You can also use direct access. Or one of the colour depth specific access functions that don't do bounds checking.

--
sig used to be here

Ceagon Xylas
Member #5,495
February 2005
avatar

Okee doke. Thanks :)

GullRaDriel
Member #3,861
September 2003
avatar

Perhaps this from exfire.c, with some little change you can adjust it to fit your need:

1 /* It is even faster if we transfer the data in 32 bit chunks, rather
2 * than only one pixel at a time. This method may not work on really
3 * unusual machine architectures, but should be ok on just about
4 * anything that you are practically likely to come across.
5 */
6 while (!keypressed()) {
7 acquire_screen();
8 draw_bottom_line_of_fire();
9 
10 bmp_select(screen);
11 
12 for (y=64; y<SCREEN_H-1; y++) {
13 /* get an address for reading line y+1 */
14 address = bmp_read_line(screen, y+1);
15 
16 /* read line in 32 bit chunks */
17 for (x=0; x<SCREEN_W; x += sizeof(uint32_t))
18 *((uint32_t *)&temp[x]) = bmp_read32(address+x);
19 
20 /* adjust it */
21 for (x=0; x<SCREEN_W; x++)
22 if (temp[x] > 0)
23 temp[x]--;
24 
25 /* get an address for writing line y */
26 address = bmp_write_line(screen, y);
27 
28 /* write line in 32 bit chunks */
29 for (x=0; x<SCREEN_W; x += sizeof(uint32_t))
30 bmp_write32(address+x, *((uint32_t *)&temp[x]));
31 }
32 
33 bmp_unwrite_line(screen);
34 release_screen();
35 }
36 
37 free(temp);
38 
39 return 0;
40}

This article from Gamedev about digital processing can help too for implementing gaussian blur.
_

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Onewing
Member #6,152
August 2005
avatar

//Untested 
bool is_solid_color(BITMAP *src, int col)
{
  int x = 0, y = 0;
  int check_col = -1;
  while(y < src->h && (check_col = getpixel(src,x++,y)) == col)
    if(x == src->w) {  x = 0; y++;   }

  if(check_col != col)
    return false;
  return true;
}

Funny thing about this is the worst case scenario is when the bitmap IS the given solid color. :)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Rampage
Member #3,035
December 2002
avatar

Onewing Phipps:

bool is_solid_color(BITMAP *src, int col)
{
  int x = 0, y = 0;
  int check_col = -1;
  while(y < src->h && (check_col = getpixel(src,x++,y)) == col)
    if(x == src->w) {  x = 0; y++;   }

  // just a minor improvement
  return check_col == col;
}

-R

Ceagon Xylas
Member #5,495
February 2005
avatar

;D

Go to: