[gil] Using gil::color_converted_view with gil::for_each_pixel
Hey guys, I realized that writing to gil::color_converted_view doesn't affect the underlying view's data. I wonder if that's correct? For example, let's say that I want to write a program that will take the value of the red channel and set the blue channel's value to half of that. Here's my failed attempt: template <typename SrcView> void half_red_to_blue(SrcView & view) { // Since SrcView might be RGB or BGR or some other types, // I decided to use a color_converted_view to ensure that I'm // accessing the correct channels type gil::color_converted_view_type<SrcView, gil::rgb8_pixel_t>::type MyView; MyView my_view = gil::color_converted_view<gil::rgb8_pixel_t>(view): struct my_lambda { void operator()(gil::rgb8_pixel_t & p) { p[2] = p[0] / 2; } }; gil::for_each_pixel(my_view, my_lambda()); } However, it only works when SrcView is not actually gil::rgb8_view_t. If I call, e.g. half_red_to_blue<gil::bgr8_view_t>(view), the view is not changed at all! I inspected a little in the debugger and it seems the write operation is writing to some kind of proxy location instead of the original pixels. Any ideas? Thanks in advance! Best regards, Chris Yuen
Hi Chris, please see below: On Sat, Dec 4, 2010 at 1:00 PM, Chris Yuen <kizzx2+boost@gmail.com> wrote:
Hey guys,
I realized that writing to gil::color_converted_view doesn't affect the underlying view's data. I wonder if that's correct?
Yes, you're correct about your findings. The color of a pixel is only converted when you access that pixel. That's the mantra of all mutating views. Which can save a lot operations if you don't need the whole image to be converted.
For example, let's say that I want to write a program that will take the value of the red channel and set the blue channel's value to half of that. Here's my failed attempt:
template <typename SrcView> void half_red_to_blue(SrcView & view) { // Since SrcView might be RGB or BGR or some other types, // I decided to use a color_converted_view to ensure that I'm // accessing the correct channels
Are you aware of "get_color()"? Here is small example: rgb8_image_t img( 10, 10 ); rgb8_pixel_t p = *view( img ).xy_at( 0, 0 ); unsigned char c = get_color( p, red_t() ); This works for rgb, bgr, etc.
type gil::color_converted_view_type<SrcView, gil::rgb8_pixel_t>::type MyView; MyView my_view = gil::color_converted_view<gil::rgb8_pixel_t>(view):
struct my_lambda { void operator()(gil::rgb8_pixel_t & p) { p[2] = p[0] / 2; } };
gil::for_each_pixel(my_view, my_lambda()); }
Does that answer you question? Please mail back if you have more questions. A complete source code example would be very helpful to address your needs. Regards, Christian PS: There is a review of my io extension for gil going on right now. If you have some time please write a review. Thanks!
participants (2)
-
Chris Yuen
-
Christian Henning