FLTK logo

Article #466: How to get pixel color out of Fl_Image

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 Home  |  Articles & FAQs  |  Bugs & Features  |  Documentation  |  Download  |  Screenshots  ]
 

Return to Articles | Show Comments | Submit Comment ]

Article #466: How to get pixel color out of Fl_Image

Created at 11:28 Jun 27, 2005 by matt

Last modified at 11:31 Jun 27, 2005

OK, the data() member is a somewhat private structure within Fl_Image. There are very specific cicumstances under which data() returns meaningful pointers (and I say pointers here, because data() returns *an array* of pointers, and not, as most assume, a single pointer; that is an entirely different thing!)

So, under which circumstances is data() meaningful?

There are two very different ways of storing images. The first one, which we will not discuss further, stored a number for each pixel. This number refers to a color inside a lookup table. .bmp's are often stored that way, so we can't really access them. Basically, data() returns an opaque pointer that holds different data for every lookup table format.

The other way of storing data is to spell out the color values for each pixel as a list of pixels. For example: pixel at 0, 0 has 30% red, 2% green, and 4% blue, pixel at 0, 1 has 50% red, etc. etc. (there are many many other ways of storing pixels, but this is how FLTK does it; it's called true color, because every pixell is represented in its true colors as opposed to some table value that is usually an approximation).

FLTK has four true color formats: Gray scale, gray scale with transparency (called "Alpha"), full color (red, green, blue: rgb), and RGB with transparency, or in short: Y, YA, RGB, and RGBA. This is where the d() function of Fl_Image comes into play. d() simply returns the number of color components, namely 1 fo Y, 2 for YA,... ah, got it, right ;-)

So now we have w(), h() and tthe array size. w() and h() are obvious, but how do we calculate the size. Well, we store each color component in a single unsigned byte, so a 16x16 image in RGB is 16x16x3 bytes large (do I hear you punching numbers into your pocket calculator? tsk tsk!).

Since we are an orderly bunch of guys, we ordered the pixels for you in a single array, left to right, top to bottom, giving you a nice linear chunk of memory that can easily be searched for pixel values. So what is the color at (x=12, y=15)? Simple math will do:
 

 Fl_Image *img;
 unsigned char *img_data = img->data()[0];
 int p = ( x + y * img->w() ) * img->d();
 unsigned char *pixel = img_data + p;
 if (d()==3) { // RGB
   unsigned char red_at_x_y   = pixel[0];
   unsigned char green_at_x_y = pixel[1];
   unsigned char blue_at_x_y  = pixel[2];
}

Remember that I said that data() returns an array of pointers? Yeah, well that's why you have the img->data()[0] code there. It returns the first pointer in the array. All other pointer are private and may or may not exist. They could contain the origional resolution, or the authors name or a copyright notice, who knows? All you need to remember is: data() returns and array of pointers, not a pointer, and data is pixel interleaved, not sorted by r, g, and b (because we would have to reshuffle it to get it on the screen, and that takes time).

I hope you can get you pixel data out of the Fl_Image class now?! ;-)

Matthias

Listing ]


Comments

Submit Comment ]

From duncan.gibson, 23:50 Jun 27, 2005 (score=3)

Example code can be found on Greg Ercolano's cheat sheet at:
http://seriss.com/people/erco/fltk/#Fl_Image


Reply ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.