【问题标题】:gil boost : convert rgb8_image_t to rgba8_image_tgil boost:将 rgb8_image_t 转换为 rgba8_image_t
【发布时间】:2012-01-23 08:23:29
【问题描述】:

我对 GIL 语法有点困惑。我要转换

rgb8_image_t

到

rgba8_image_t

并将 Alpha 通道设置为 1。 有没有内置功能。如果不是如何手动执行此操作?

【问题讨论】:

    标签: c++ boost-gil


    【解决方案1】:

    您想在范围内使用 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 图像在转换时具有隐式最大alpha 这样没有争议的事情,似乎没有理由不定义它会在默认情况下被拾取。

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2015-09-03
      • 2011-04-22
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多