Back to Arangodb

Channel

3rdParty/boost/1.78.0/libs/gil/doc/html/design/channel.html

3.12.9.17.4 KB
Original Source

Channel

Overview

A channel indicates the intensity of a color component (for example, the red channel in an RGB pixel). Typical channel operations are getting, comparing and setting the channel values. Channels have associated minimum and maximum value. GIL channels model the following concept:

conceptChannelConcept\<typenameT\>:EqualityComparable\<T\>{typenamevalue\_type=T;// use channel\_traits\<T\>::value\_type to access itwhereChannelValueConcept\<value\_type\>;typenamereference=T&;// use channel\_traits\<T\>::reference to access ittypenamepointer=T\*;// use channel\_traits\<T\>::pointer to access ittypenameconst\_reference=constT&;// use channel\_traits\<T\>::const\_reference to access ittypenameconst\_pointer=constT\*;// use channel\_traits\<T\>::const\_pointer to access itstaticconstboolis\_mutable;// use channel\_traits\<T\>::is\_mutable to access itstaticTmin\_value();// use channel\_traits\<T\>::min\_value to access itstaticTmax\_value();// use channel\_traits\<T\>::min\_value to access it};conceptMutableChannelConcept\<ChannelConceptT\>:Swappable\<T\>,Assignable\<T\>{};conceptChannelValueConcept\<ChannelConceptT\>:Regular\<T\>{};

GIL allows built-in integral and floating point types to be channels. Therefore the associated types and range information are defined in channel_traits with the following default implementation:

template\<typenameT\>structchannel\_traits{typedefTvalue\_type;typedefT&reference;typedefT\*pointer;typedefT&constconst\_reference;typedefT\*constconst\_pointer;staticvalue\_typemin\_value(){returnstd::numeric\_limits\<T\>::min();}staticvalue\_typemax\_value(){returnstd::numeric\_limits\<T\>::max();}};

Two channel types are compatible if they have the same value type:

conceptChannelsCompatibleConcept\<ChannelConceptT1,ChannelConceptT2\>{whereSameType\<T1::value\_type,T2::value\_type\>;};

A channel may be convertible to another channel:

template\<ChannelConceptSrc,ChannelValueConceptDst\>conceptChannelConvertibleConcept{Dstchannel\_convert(Src);};

Note that ChannelConcept and MutableChannelConcept do not require a default constructor. Channels that also support default construction (and thus are regular types) model ChannelValueConcept. To understand the motivation for this distinction, consider a 16-bit RGB pixel in a “565” bit pattern. Its channels correspond to bit ranges. To support such channels, we need to create a custom proxy class corresponding to a reference to a sub-byte channel. Such a proxy reference class models only ChannelConcept, because, similar to native C++ references, it may not have a default constructor.

Note also that algorithms may impose additional requirements on channels, such as support for arithmetic operations.

See also

Models

All C++11 fundamental integer and float point types are valid channels.

The minimum and maximum values of a channel modeled by a built-in type correspond to the minimum and maximum physical range of the built-in type, as specified by its std::numeric_limits. Sometimes the physical range is not appropriate. GIL provides scoped_channel_value, a model for a channel adapter that allows for specifying a custom range. We use it to define a [0..1] floating point channel type as follows:

structfloat\_zero{staticfloatapply(){return0.0f;}};structfloat\_one{staticfloatapply(){return1.0f;}};typedefscoped\_channel\_value\<float,float\_zero,float\_one\>bits32f;

GIL also provides models for channels corresponding to ranges of bits:

// Value of a channel defined over NumBits bits. Models ChannelValueConcepttemplate\<intNumBits\>classpacked\_channel\_value;// Reference to a channel defined over NumBits bits. Models ChannelConcepttemplate\<intFirstBit,intNumBits,// Defines the sequence of bits in the data value that contain the channelboolMutable\>// true if the reference is mutableclasspacked\_channel\_reference;// Reference to a channel defined over NumBits bits. Its FirstBit is a run-time parameter. Models ChannelConcepttemplate\<intNumBits,// Defines the sequence of bits in the data value that contain the channelboolMutable\>// true if the reference is mutableclasspacked\_dynamic\_channel\_reference;

Note that there are two models of a reference proxy which differ based on whether the offset of the channel range is specified as a template or a run-time parameter. The first model is faster and more compact while the second model is more flexible. For example, the second model allows us to construct an iterator over bit range channels.

Algorithms

Here is how to construct the three channels of a 16-bit “565” pixel and set them to their maximum value:

usingchannel16\_0\_5\_reference\_t=packed\_channel\_reference\<0,5,true\>;usingchannel16\_5\_6\_reference\_t=packed\_channel\_reference\<5,6,true\>;usingchannel16\_11\_5\_reference\_t=packed\_channel\_reference\<11,5,true\>;std::uint16\_tdata=0;channel16\_0\_5\_reference\_tchannel1(&data);channel16\_5\_6\_reference\_tchannel2(&data);channel16\_11\_5\_reference\_tchannel3(&data);channel1=channel\_traits\<channel16\_0\_5\_reference\_t\>::max\_value();channel2=channel\_traits\<channel16\_5\_6\_reference\_t\>::max\_value();channel3=channel\_traits\<channel16\_11\_5\_reference\_t\>::max\_value();assert(data==65535);

Assignment, equality comparison and copy construction are defined only between compatible channels:

packed\_channel\_value\<5\>channel\_6bit=channel1;channel\_6bit=channel3;// compile error: Assignment between incompatible channels//channel\_6bit = channel2;

All channel models provided by GIL are pairwise convertible:

channel1=channel\_traits\<channel16\_0\_5\_reference\_t\>::max\_value();assert(channel1==31);bits16chan16=channel\_convert\<bits16\>(channel1);assert(chan16==65535);

Channel conversion is a lossy operation. GIL’s channel conversion is a linear transformation between the ranges of the source and destination channel. It maps precisely the minimum to the minimum and the maximum to the maximum. (For example, to convert from uint8_t to uint16_t GIL does not do a bit shift because it will not properly match the maximum values. Instead GIL multiplies the source by 257).

All channel models that GIL provides are convertible from/to an integral or floating point type. Thus they support arithmetic operations. Here are the channel-level algorithms that GIL provides:

// Converts a source channel value into a destination channel.// Linearly maps the value of the source into the range of the destination.template\<typenameDstChannel,typenameSrcChannel\>typenamechannel\_traits\<DstChannel\>::value\_typechannel\_convert(SrcChannelsrc);// returns max\_value - x + min\_valuetemplate\<typenameChannel\>typenamechannel\_traits\<Channel\>::value\_typechannel\_invert(Channelx);// returns a \* b / max\_valuetemplate\<typenameChannel\>typenamechannel\_traits\<Channel\>::value\_typechannel\_multiply(Channela,Channelb);