Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » finding out bitmap pixel info

This thread is locked; no one can reply to it. rss feed Print
finding out bitmap pixel info
qwertq
Member #7,632
August 2006

I need to make a program that can tell me what color a specific pixel (with a specific coordinate) from a BMP has. Even smth as simple as this:

Pixel(0,0): Color Green; Pixel(1,0): Color Blue...
..................................................
Pixel(0,344): Color Magenta etc...

till the end of file.
Actual code would be very helpful.

SonShadowCat
Member #1,548
September 2001
avatar

Go look at the manual and find the getpixel functions.

Maikol
Member #4,997
September 2004
avatar

 /*
  * Returns an array containing colors
  */
 int *get_pixels_color_from(BITMAP *image)
 {
    static int colors[image->w*image->h];
    int f, c;

 for (f = 0;f < image->w;f++)
    for (c = 0;c < image->h;c++)
        colors[f*image->w+c] = getpixel(f,c);

 return colors;
 }

El sabio no dice todo lo que piensa, pero siempre piensa todo lo que dice
Aristóteles

miran
Member #2,407
June 2002

Maikol: 1. Your code does the same thing as blit() and 2. Your code doesn't even compile.

qwertq: If you really need to find the names of the colours in the bitmap, do the following for each pixel:

1. Read colour with getpixel().
2. Decompose to RGB components with getr(), getg() and getb().
3. Convert to HSV colourspace with rgb_to_hsv().
4. Check the H, S and V components. H's value ranges from 0 to 360, where 0 is red, 120 is green, 240 is blue and 360 is again red. 60 is yellow, 180 is cyan, 300 is purple and so on. So if H is between 15 and 45, you can say the colour is orange. The value of S ranges from 0 to 1. If S is 0 (or very close to 0) you can say the colour is a shade of gray. Which shade depends on the value of V, which ranges from 0 to 1. 0 is black, 1 is white and everything between is gray.

If you don't actually need to put names to the colours, but just want to read integer values for pixels, then follow SonShadowCat's advice to RTFM.

--
sig used to be here

Go to: