| 203 | } |
| 204 | |
| 205 | void LuminanceOp::modify( Object *object, const CompoundObject *operands ) |
| 206 | { |
| 207 | ImagePrimitive *image = runTimeCast<ImagePrimitive>( object ); |
| 208 | ImagePrimitive::ChannelMap &channels = image->channels; |
| 209 | |
| 210 | DataPtr luminanceData = nullptr; |
| 211 | int steps[3] = { 1, 1, 1 }; |
| 212 | |
| 213 | const auto colorIt = channels.find( m_colorChannelParameter->getTypedValue() ); |
| 214 | if( colorIt != channels.end() && colorIt->second ) |
| 215 | { |
| 216 | // RGB in a single channel |
| 217 | switch( colorIt->second->typeId() ) |
| 218 | { |
| 219 | case Color3fDataTypeId : |
| 220 | { |
| 221 | FloatDataPtr l = new FloatData; |
| 222 | const float *d = boost::static_pointer_cast<Color3fData>( colorIt->second )->baseReadable(); |
| 223 | calculate( d, d + 1, d + 2, steps, 1, l->baseWritable() ); |
| 224 | luminanceData = l; |
| 225 | } |
| 226 | break; |
| 227 | case Color3fVectorDataTypeId : |
| 228 | { |
| 229 | FloatVectorDataPtr l = new FloatVectorData; |
| 230 | Color3fVectorDataPtr d = boost::static_pointer_cast<Color3fVectorData>( colorIt->second ); |
| 231 | l->writable().resize( d->readable().size() ); |
| 232 | const float *dd = d->baseReadable(); |
| 233 | steps[0] = steps[1] = steps[2] = 3; |
| 234 | calculate( dd, dd + 1, dd + 2, steps, d->readable().size(), l->baseWritable() ); |
| 235 | luminanceData = l; |
| 236 | } |
| 237 | break; |
| 238 | default : |
| 239 | throw Exception( "Channel has unsupported type." ); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | // separate RGB channels? |
| 246 | const auto rIt = channels.find( m_redChannelParameter->getTypedValue() ); |
| 247 | const auto gIt = channels.find( m_greenChannelParameter->getTypedValue() ); |
| 248 | const auto bIt = channels.find( m_blueChannelParameter->getTypedValue() ); |
| 249 | |
| 250 | std::string reason; |
| 251 | if( !image->channelValid( rIt->first, &reason ) ) |
| 252 | { |
| 253 | throw Exception( str( format( "Channel \"%s\" is invalid: " ) % rIt->first ) + reason ); |
| 254 | } |
| 255 | if( !image->channelValid( gIt->first, &reason ) ) |
| 256 | { |
| 257 | throw Exception( str( format( "Channel \"%s\" is invalid: " ) % gIt->first ) + reason ); |
| 258 | } |
| 259 | if( !image->channelValid( bIt->first, &reason ) ) |
| 260 | { |
| 261 | throw Exception( str( format( "Channel \"%s\" is invalid: " ) % bIt->first ) + reason ); |
| 262 | } |