| 153 | } |
| 154 | |
| 155 | ImageTargetFileWic::ImageTargetFileWic( DataTargetRef dataTarget, ImageSourceRef imageSource, ImageTarget::Options options, const string &extensionData ) |
| 156 | : ImageTarget(), mDataTarget( dataTarget ) |
| 157 | { |
| 158 | mCodecGUID = getExtensionMap()[extensionData]; |
| 159 | |
| 160 | setSize( imageSource->getWidth(), imageSource->getHeight() ); |
| 161 | |
| 162 | // determine the pixel format we'll request |
| 163 | WICPixelFormatGUID formatGUID; |
| 164 | if( imageSource->hasAlpha() ) { |
| 165 | bool premultAlpha = imageSource->isPremultiplied(); |
| 166 | // WIC doesn't support gray+alpha, so we need to do RGBA regardless |
| 167 | if( imageSource->getDataType() == ImageIo::UINT8 ) |
| 168 | formatGUID = premultAlpha ? GUID_WICPixelFormat32bppPBGRA : GUID_WICPixelFormat32bppBGRA; |
| 169 | else if( imageSource->getDataType() == ImageIo::UINT16 ) |
| 170 | formatGUID = premultAlpha ? GUID_WICPixelFormat64bppPRGBA : GUID_WICPixelFormat64bppRGBA; |
| 171 | else |
| 172 | formatGUID = premultAlpha ? GUID_WICPixelFormat128bppPRGBAFloat : GUID_WICPixelFormat128bppRGBAFloat; |
| 173 | } |
| 174 | else { |
| 175 | ImageIo::ColorModel cm = options.isColorModelDefault() ? imageSource->getColorModel() : options.getColorModel(); |
| 176 | if( cm == ImageIo::CM_GRAY ) { |
| 177 | if( imageSource->getDataType() == ImageIo::UINT8 ) |
| 178 | formatGUID = GUID_WICPixelFormat8bppGray; |
| 179 | else if( imageSource->getDataType() == ImageIo::UINT16 ) |
| 180 | formatGUID = GUID_WICPixelFormat16bppGray; |
| 181 | else |
| 182 | formatGUID = GUID_WICPixelFormat32bppGrayFloat; |
| 183 | } |
| 184 | else { // RGB |
| 185 | if( imageSource->getDataType() == ImageIo::UINT8 ) |
| 186 | formatGUID = GUID_WICPixelFormat24bppBGR; |
| 187 | else if( imageSource->getDataType() == ImageIo::UINT16 ) |
| 188 | formatGUID = GUID_WICPixelFormat48bppRGB; |
| 189 | else |
| 190 | formatGUID = GUID_WICPixelFormat128bppRGBFloat; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | ::HRESULT hr = S_OK; |
| 195 | |
| 196 | msw::initializeCom(); |
| 197 | |
| 198 | // Create WIC factory |
| 199 | msw::ComPtr<IWICImagingFactory> IWICFactory; |
| 200 | hr = ::CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void**)IWICFactory.releaseAndGetAddressOf() ); |
| 201 | if( ! SUCCEEDED( hr ) ) |
| 202 | throw ImageIoExceptionFailedWrite( "Could not create WIC Factory." ); |
| 203 | |
| 204 | hr = IWICFactory->CreateEncoder( *mCodecGUID, 0, mEncoder.releaseAndGetAddressOf() ); |
| 205 | if( ! SUCCEEDED( hr ) ) |
| 206 | throw ImageIoExceptionFailedWrite( "Could not create WIC Encoder." ); |
| 207 | |
| 208 | // create the stream |
| 209 | msw::ComPtr<IWICStream> stream; |
| 210 | hr = IWICFactory->CreateStream( stream.releaseAndGetAddressOf() ); |
| 211 | if( ! SUCCEEDED(hr) ) |
| 212 | throw ImageIoExceptionFailedWrite( "Could not create WIC stream." ); |
nothing calls this directly
no test coverage detected