2012-01-23 3 views

答えて

7

boost::gil::copy_and_convert_pixelsを適切なマッチングcolor_convertの有効範囲で使用したいと考えています。ここで

は完全な例です:

#include <boost/gil/gil_all.hpp> 
#include <cassert> 

namespace boost { namespace gil { 

    // Define a color conversion rule NB in the boost::gil namespace 
    template <> void color_convert<rgb8_pixel_t,rgba8_pixel_t>(
     const rgb8_pixel_t& src, 
     rgba8_pixel_t& dst 
    ) { 

     // Well we _could_ just write... 

     // dst[0]=src[0]; 
     // dst[1]=src[1]; 
     // dst[2]=src[2]; 
     // dst[3]=255; 

     // ...but that'd be too easy/not as generic as it could be 
     // so let's go crazy... 
     get_color(dst,red_t())=get_color(src,red_t()); 
     get_color(dst,green_t())=get_color(src,green_t()); 
     get_color(dst,blue_t())=get_color(src,blue_t()); 

     typedef color_element_type<rgba8_pixel_t,alpha_t>::type alpha_channel_t; 
     get_color(dst,alpha_t())=channel_traits<alpha_channel_t>::max_value(); 
    } 
    } 
} 

int main(int,char**) { 

    // Create a 1x1 RGB image and set its pixel to a color 
    boost::gil::rgb8_image_t a(1,1); 
    boost::gil::view(a)(0,0)=boost::gil::rgb8_pixel_t(1,2,3); 

    // Create a 1x1 RGBA 
    boost::gil::rgba8_image_t b(1,1); 

    // Copy AND CONVERT 
    boost::gil::copy_and_convert_pixels(
    boost::gil::const_view(a), 
    boost::gil::view(b) 
); 

    // Check the alpha has been set as expected 
    const boost::gil::rgba8_pixel_t p=boost::gil::const_view(b)(0,0); 
    assert(p==boost::gil::rgba8_pixel_t(1,2,3,255)); 

    return 0; 
} 

はまたそこに明確な色変換ファンクタを受け入れるcopy_and_convert_pixelsオーバーロード(docsを参照)があるが、RGB画像を作るほどの異論何かを変換上の暗黙の最大アルファを持っていますそれがデフォルトで選択される場所を定義しない理由はほとんどないようです。

関連する問題