CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10);
How can I get rgb
from ptr
?
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10);
How can I get rgb
from ptr
?
The easiest way to access data is with the
()
operator:You are probably hitting confusion because CImg stores the raw data non-interleaved. i.e. your raw data is stored
R1, R2, ..., G1, G2, ..., B1, B2, ...
instead ofR1, G1, B1, R2, G2, B2, ...
see: http://cimg.eu/reference/group__cimg__storage.html.data()
just returns a pointer, so to access the data directly as above you would do:From the CImg documentation -- section 6.13 on page 34, and section 8.1.4.16 on page 120 -- it looks like the
data
method can take four arguments: x, y, z, and c:...where
c
refers to the color channel. I'm guessing that if your image is indeed an RGB image, then using values of 0, 1, or 2 forc
will give you the red, green, and blue components at a givenx, y
location.For example:
(But this is just a guess!)
Edit:
It looks like there's also an operator() for CImg that works in a similar manner:
Tested on Ubuntu 10.04 with a handmade 3x3 RGB image saved as
test.png
:Source file
cimg_test.cpp
:Compile and run:
It works.
@wamp: I don't know about CImg but grayscale images in RGB have:
R = G = B
and in CMYK:
C = M = Y = 0
K = luminance
So you don't even need a function for that...