ImageSource::RowFunc ImageSource::setupRowFuncForTypesAndTargetColorModel( ImageTargetRef target ) { switch( mColorModel ) { case CM_RGB: { setupRowFuncRgbSource( target ); bool alpha = ( mRowFuncSourceAlpha != -1 ) && ( mRowFuncTargetAlpha != -1 ); if( alpha ) return &ImageSource::rowFuncSourceRgb<SD,TD,TCM,true>; else return &ImageSource::rowFuncSourceRgb<SD,TD,TCM,false>; } break; case CM_GRAY: { setupRowFuncGraySource( target ); bool alpha = ( mRowFuncSourceAlpha != -1 ) && ( mRowFuncTargetAlpha != -1 ); if( alpha ) return &ImageSource::rowFuncSourceGray<SD,TD,TCM,true>; else return &ImageSource::rowFuncSourceGray<SD,TD,TCM,false>; } break; default: throw ImageIoExceptionIllegalColorModel(); } }
vec3 ColorAT<T>::get( ColorModel cm ) const { switch( cm ) { case CM_HSV: return rgbToHsv( Colorf( r, g, b ) ); break; case CM_RGB: return vec3( CHANTRAIT<float>::convert( r ), CHANTRAIT<float>::convert( g ), CHANTRAIT<float>::convert( b ) ); break; default: throw ImageIoExceptionIllegalColorModel(); } }
ImageSource::RowFunc ImageSource::setupRowFuncForTypes( ImageTargetRef target ) { switch( target->getColorModel() ) { case CM_RGB: return setupRowFuncForTypesAndTargetColorModel<SD,TD,CM_RGB>( target ); break; case CM_GRAY: return setupRowFuncForTypesAndTargetColorModel<SD,TD,CM_GRAY>( target ); break; case CM_UNKNOWN: default: throw ImageIoExceptionIllegalColorModel(); } }
void ColorAT<T>::set( ColorModel cm, const vec4 &v ) { a = v.w; switch( cm ) { case CM_HSV: { Colorf rgb = hsvToRgb( vec3( v ) ); r = CHANTRAIT<T>::convert( rgb.r ); g = CHANTRAIT<T>::convert( rgb.g ); b = CHANTRAIT<T>::convert( rgb.b ); } break; case CM_RGB: r = CHANTRAIT<T>::convert( v.r ); g = CHANTRAIT<T>::convert( v.g ); b = CHANTRAIT<T>::convert( v.b ); break; default: throw ImageIoExceptionIllegalColorModel(); } }
ImageTargetFileTinyExr::ImageTargetFileTinyExr( DataTargetRef dataTarget, ImageSourceRef imageSource, ImageTarget::Options options, const std::string & /*extensionData*/ ) { if( ! dataTarget->providesFilePath() ) { throw ImageIoExceptionFailedWrite( "ImageTargetFileTinyExr only supports writing to files." ); } mFilePath = dataTarget->getFilePath(); setSize( imageSource->getWidth(), imageSource->getHeight() ); ImageIo::ColorModel cm = options.isColorModelDefault() ? imageSource->getColorModel() : options.getColorModel(); switch( cm ) { case ImageIo::ColorModel::CM_RGB: mNumComponents = ( imageSource->hasAlpha() ) ? 4 : 3; setColorModel( ImageIo::ColorModel::CM_RGB ); setChannelOrder( ( mNumComponents == 3 ) ? ImageIo::ChannelOrder::BGR : ImageIo::ChannelOrder::ABGR ); if( mNumComponents == 3 ) mChannelNames = { "G", "B", "R" }; else mChannelNames = { "A", "G", "B", "R" }; break; case ImageIo::ColorModel::CM_GRAY: mNumComponents = ( imageSource->hasAlpha() ) ? 2 : 1; setColorModel( ImageIo::ColorModel::CM_GRAY ); setChannelOrder( ( mNumComponents == 2 ) ? ImageIo::ChannelOrder::YA : ImageIo::ChannelOrder::Y ); if( mNumComponents == 2 ) mChannelNames = { "Y", "A" }; else mChannelNames = { "Y" }; break; default: throw ImageIoExceptionIllegalColorModel(); } // TODO: consider supporting half float and uint types as well setDataType( ImageIo::DataType::FLOAT32 ); mData.resize( mHeight * imageSource->getWidth() * mNumComponents ); }
ImageTargetFileStbImage::ImageTargetFileStbImage( DataTargetRef dataTarget, ImageSourceRef imageSource, ImageTarget::Options options, const std::string &extensionData ) { if( ! dataTarget->providesFilePath() ) { throw ImageIoExceptionFailedWrite( "ImageTargetFileStbImage only supports writing to files." ); } mFilePath = dataTarget->getFilePath(); setSize( imageSource->getWidth(), imageSource->getHeight() ); ImageIo::ColorModel cm = options.isColorModelDefault() ? imageSource->getColorModel() : options.getColorModel(); switch( cm ) { case ImageIo::ColorModel::CM_RGB: mNumComponents = ( imageSource->hasAlpha() ) ? 4 : 3; setColorModel( ImageIo::ColorModel::CM_RGB ); setChannelOrder( ( mNumComponents == 4 ) ? ImageIo::ChannelOrder::RGBA : ImageIo::ChannelOrder::RGB ); break; case ImageIo::ColorModel::CM_GRAY: mNumComponents = ( imageSource->hasAlpha() ) ? 2 : 1; setColorModel( ImageIo::ColorModel::CM_GRAY ); setChannelOrder( ( mNumComponents == 2 ) ? ImageIo::ChannelOrder::YA : ImageIo::ChannelOrder::Y ); break; default: throw ImageIoExceptionIllegalColorModel(); } mExtension = extensionData; if( mExtension == "hdr" ) { // Radiance files are always float* setDataType( ImageIo::DataType::FLOAT32 ); mRowBytes = mNumComponents * imageSource->getWidth() * sizeof(float); } else { setDataType( ImageIo::DataType::UINT8 ); mRowBytes = mNumComponents * imageSource->getWidth() * sizeof(float); } mData = std::unique_ptr<uint8_t[]>( new uint8_t[mWidth * mRowBytes] ); }