| 348 | } |
| 349 | |
| 350 | void WICFileIONative::SaveTo(std::string const& filename, uint32_t inFormat, |
| 351 | size_t inWidth, size_t inHeight, uint8_t const* inTexels, float imageQuality) |
| 352 | { |
| 353 | if (inFormat >= NUM_FORMATS) |
| 354 | { |
| 355 | throw std::runtime_error("The texture format is incorrect."); |
| 356 | } |
| 357 | |
| 358 | if (inWidth == 0 || inHeight == 0 || inTexels == nullptr) |
| 359 | { |
| 360 | throw std::runtime_error("The texture and its data must exist."); |
| 361 | } |
| 362 | |
| 363 | // Start COM and create WIC. |
| 364 | ComInitializer comInitializer; |
| 365 | if (!comInitializer.IsInitialized()) |
| 366 | { |
| 367 | throw std::runtime_error("Unable to initialize COM for WIC."); |
| 368 | } |
| 369 | |
| 370 | // Select the WIC format that matches the input texture format. |
| 371 | WICPixelFormatGUID wicSourceGUID = GUID_WICPixelFormatUndefined; |
| 372 | for (size_t i = 0; i < NUM_SAVE_FORMATS; ++i) |
| 373 | { |
| 374 | if (msSaveFormatMap[i].format == inFormat) |
| 375 | { |
| 376 | wicSourceGUID = *msSaveFormatMap[i].wicOutputGUID; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | if (IsEqualGUID(wicSourceGUID, GUID_WICPixelFormatUndefined)) |
| 381 | { |
| 382 | throw std::runtime_error("Format " + std::to_string(inFormat) + |
| 383 | "is not supported for saving."); |
| 384 | } |
| 385 | |
| 386 | // Create a WIC imaging factory. |
| 387 | ComObject<IWICImagingFactory> wicFactory; |
| 388 | HRESULT hr = ::CoCreateInstance(CLSID_WICImagingFactory, nullptr, |
| 389 | CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, |
| 390 | reinterpret_cast<LPVOID*>(wicFactory.GetAddressOf())); |
| 391 | if (FAILED(hr)) |
| 392 | { |
| 393 | throw std::runtime_error("Unable to create WIC imaging factory."); |
| 394 | } |
| 395 | |
| 396 | // Create a WIC stream for output. |
| 397 | ComObject<IWICStream> wicStream; |
| 398 | hr = wicFactory->CreateStream(&wicStream); |
| 399 | if (FAILED(hr)) |
| 400 | { |
| 401 | throw std::runtime_error("wicFactory->CreateStream failed."); |
| 402 | } |
| 403 | |
| 404 | std::wstring wfilename(filename.begin(), filename.end()); |
| 405 | hr = wicStream->InitializeFromFilename(wfilename.c_str(), GENERIC_WRITE); |
| 406 | if (FAILED(hr)) |
| 407 | { |
nothing calls this directly
no test coverage detected