bool WICImageLoader::decodeImageData(ImageBlob blob, size_t size) { bool bRet = false; HRESULT hr = E_FAIL; IWICStream* pWicStream = NULL; IWICImagingFactory* pWicFactory = getWICFactory(); if(NULL != pWicFactory) { hr = pWicFactory->CreateStream(&pWicStream); } if(SUCCEEDED(hr)) { hr = pWicStream->InitializeFromMemory((BYTE*)blob, static_cast<DWORD>(size)); } IWICBitmapDecoder* pDecoder = NULL; if(SUCCEEDED(hr)) { hr = pWicFactory->CreateDecoderFromStream(pWicStream, NULL, WICDecodeMetadataCacheOnLoad, &pDecoder); } bRet = processImage(pDecoder); SafeRelease(&pWicStream); SafeRelease(&pDecoder); return bRet; }
int SImgX_WIC::LoadFromMemory(void *pBuf,size_t bufLen ) { SASSERT(m_pImgArray == NULL); IWICImagingFactory* factory = SImgDecoderFactory_WIC::s_wicImgFactory; CAutoRefPtr<IWICBitmapDecoder> decoder; CAutoRefPtr<IWICStream> stream ; if(FAILED(factory->CreateStream(&stream))) return 0; if(FAILED(stream->InitializeFromMemory((BYTE*)pBuf,(DWORD)bufLen))) return 0; if(FAILED(factory->CreateDecoderFromStream(stream,NULL,WICDecodeMetadataCacheOnDemand,&decoder))) return 0; return _DoDecode(decoder); }
void GL::Image::load(const unsigned char *buf, size_t bufSize) { if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK) { // bad! return; } IStream *stream = SHCreateMemStream((const BYTE*)buf, (UINT)bufSize); if (stream != NULL) { IWICImagingFactory *pFactory; if (CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pFactory) == S_OK) { IWICBitmapDecoder *pDecoder; if (pFactory->CreateDecoderFromStream(stream, &CLSID_WICPngDecoder, WICDecodeMetadataCacheOnDemand, &pDecoder) == S_OK) { IWICBitmapFrameDecode *frame; if (pDecoder->GetFrame(0, &frame) == S_OK) { UINT w, h; if (frame->GetSize(&w, &h) == S_OK) { width_ = w; height_ = h; } IWICFormatConverter *formatConverter; if (pFactory->CreateFormatConverter(&formatConverter) == S_OK) { if (formatConverter->Initialize(frame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.0, WICBitmapPaletteTypeCustom) == S_OK) { unsigned char *pixels = new unsigned char[w * h * 4]; if (formatConverter->CopyPixels(0, w * 4, w * h * 4, pixels) == S_OK) { loadTextureData_(pixels); } delete[] pixels; } formatConverter->Release(); } } pDecoder->Release(); } pFactory->Release(); } stream->Release(); } CoUninitialize(); }
_Use_decl_annotations_ HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dContext, const uint8_t* wicData, size_t wicDataSize, size_t maxsize, D3D11_USAGE usage, unsigned int bindFlags, unsigned int cpuAccessFlags, unsigned int miscFlags, bool forceSRGB, ID3D11Resource** texture, ID3D11ShaderResourceView** textureView ) { if ( texture ) { *texture = nullptr; } if ( textureView ) { *textureView = nullptr; } if (!d3dDevice || !wicData || (!texture && !textureView)) return E_INVALIDARG; if ( !wicDataSize ) return E_FAIL; #ifdef _M_AMD64 if ( wicDataSize > 0xFFFFFFFF ) return HRESULT_FROM_WIN32( ERROR_FILE_TOO_LARGE ); #endif IWICImagingFactory* pWIC = _GetWIC(); if ( !pWIC ) return E_NOINTERFACE; // Create input stream for memory ComPtr<IWICStream> stream; HRESULT hr = pWIC->CreateStream( stream.GetAddressOf() ); if ( FAILED(hr) ) return hr; hr = stream->InitializeFromMemory( const_cast<uint8_t*>( wicData ), static_cast<DWORD>( wicDataSize ) ); if ( FAILED(hr) ) return hr; // Initialize WIC ComPtr<IWICBitmapDecoder> decoder; hr = pWIC->CreateDecoderFromStream( stream.Get(), 0, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() ); if ( FAILED(hr) ) return hr; ComPtr<IWICBitmapFrameDecode> frame; hr = decoder->GetFrame( 0, frame.GetAddressOf() ); if ( FAILED(hr) ) return hr; hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, texture, textureView ); if ( FAILED(hr)) return hr; if (texture != 0 && *texture != 0) { SetDebugObjectName(*texture, "WICTextureLoader"); } if (textureView != 0 && *textureView != 0) { SetDebugObjectName(*textureView, "WICTextureLoader"); } return hr; }
/// <summary> /// Tries to set the cover based on the Tags of the specified file. /// </summary> /// <param name="filePath">Path to the file to get the cover from.</param> bool CoverArt::SetCoverFromTag(LPCWSTR filePath) { LPCWSTR extension = wcsrchr(filePath, L'.'); if (extension == nullptr) { return false; } auto ParseImage = [this] (const BYTE * data, UINT size) { IWICImagingFactory *factory = nullptr; IWICBitmapDecoder *decoder = nullptr; IWICBitmapFrameDecode *source = nullptr; HRESULT hr = E_FAIL; IStream *stream = SHCreateMemStream(data, size); if (stream) { hr = Factories::GetWICFactory(reinterpret_cast<LPVOID*>(&factory)); if (SUCCEEDED(hr)) { hr = factory->CreateDecoderFromStream(stream, nullptr, WICDecodeMetadataCacheOnDemand, &decoder); } if (SUCCEEDED(hr)) { hr = decoder->GetFrame(0, &source); } if (SUCCEEDED(hr)) { SendMessage(gLSModule.GetMessageWindow(), WM_COVERARTUPDATE, (WPARAM)this, (LPARAM)source); } SAFERELEASE(decoder); SAFERELEASE(stream); } return hr == S_OK; }; ++extension; if (_wcsicmp(extension, L"mp3") == 0) { TagLib::ID3v2::AttachedPictureFrame *pictureFrame = nullptr; BYTE picturePriority = 0xFF; TagLib::MPEG::File mp3File(filePath); auto tag = mp3File.ID3v2Tag(); if (tag && tag->frameListMap().contains("APIC")) { for (auto frame : mp3File.ID3v2Tag()->frameListMap()["APIC"]) { auto picFrame = (TagLib::ID3v2::AttachedPictureFrame *)frame; BYTE priority = mID3CoverTypePriority[picFrame->type()]; if (priority < picturePriority) { pictureFrame = picFrame; picturePriority = priority; } } if (pictureFrame != nullptr) { return ParseImage((const BYTE *)pictureFrame->picture().data(), pictureFrame->picture().size()); } } } else if (_wcsicmp(extension, L"flac") == 0) { TagLib::FLAC::File flacFile(filePath); for (auto &picture : flacFile.pictureList()) { if (picture->type() == TagLib::FLAC::Picture::FrontCover) { return ParseImage((const BYTE *)picture->data().data(), picture->data().size()); } } } else if (_wcsicmp(extension, L"mp4") == 0 || _wcsicmp(extension, L"m4a") == 0) { TagLib::MP4::File mp4File(filePath); if (mp4File.tag()->itemListMap().contains("covr")) { auto map = mp4File.tag()->itemListMap()["covr"]; auto list = map.toCoverArtList(); auto cover = list.front(); return ParseImage((const BYTE *)cover.data().data(), cover.data().size()); } } return false; }
/// <summary> /// Load an image from a resource into a buffer /// </summary> /// <param name="resourceName">name of image resource to load</param> /// <param name="resourceType">type of resource to load</param> /// <param name="nOutputWidth">width (in pixels) of scaled output bitmap</param> /// <param name="nOutputHeight">height (in pixels) of scaled output bitmap</param> /// <param name="pOutputBuffer">buffer that will hold the loaded image</param> /// <returns>S_OK on success, otherwise failure code</returns> HRESULT CCoordinateMappingBasics::LoadResourceImage(PCWSTR resourceName, PCWSTR resourceType, UINT nOutputWidth, UINT nOutputHeight, RGBQUAD* pOutputBuffer) { IWICImagingFactory* pIWICFactory = NULL; IWICBitmapDecoder* pDecoder = NULL; IWICBitmapFrameDecode* pSource = NULL; IWICStream* pStream = NULL; IWICFormatConverter* pConverter = NULL; IWICBitmapScaler* pScaler = NULL; HRSRC imageResHandle = NULL; HGLOBAL imageResDataHandle = NULL; void *pImageFile = NULL; DWORD imageFileSize = 0; HRESULT hrCoInit = CoInitialize(NULL); HRESULT hr = hrCoInit; if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pIWICFactory); } if (SUCCEEDED(hr)) { // Locate the resource imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType); hr = imageResHandle ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Load the resource imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle); hr = imageResDataHandle ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Lock it to get a system memory pointer. pImageFile = LockResource(imageResDataHandle); hr = pImageFile ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Calculate the size. imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle); hr = imageFileSize ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Create a WIC stream to map onto the memory. hr = pIWICFactory->CreateStream(&pStream); } if (SUCCEEDED(hr)) { // Initialize the stream with the memory pointer and size. hr = pStream->InitializeFromMemory( reinterpret_cast<BYTE*>(pImageFile), imageFileSize); } if (SUCCEEDED(hr)) { // Create a decoder for the stream. hr = pIWICFactory->CreateDecoderFromStream( pStream, NULL, WICDecodeMetadataCacheOnLoad, &pDecoder); } if (SUCCEEDED(hr)) { // Create the initial frame. hr = pDecoder->GetFrame(0, &pSource); } if (SUCCEEDED(hr)) { // Convert the image format to 32bppPBGRA // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). hr = pIWICFactory->CreateFormatConverter(&pConverter); } if (SUCCEEDED(hr)) { hr = pIWICFactory->CreateBitmapScaler(&pScaler); } if (SUCCEEDED(hr)) { hr = pScaler->Initialize( pSource, nOutputWidth, nOutputHeight, WICBitmapInterpolationModeCubic ); } if (SUCCEEDED(hr)) { hr = pConverter->Initialize( pScaler, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut); } UINT width = 0; UINT height = 0; if (SUCCEEDED(hr)) { hr = pConverter->GetSize(&width, &height); } // make sure the image scaled correctly so the output buffer is big enough if (SUCCEEDED(hr)) { if ((width != nOutputWidth) || (height != nOutputHeight)) { hr = E_FAIL; } } if (SUCCEEDED(hr)) { hr = pConverter->CopyPixels(NULL, width * sizeof(RGBQUAD), nOutputWidth * nOutputHeight * sizeof(RGBQUAD), reinterpret_cast<BYTE*>(pOutputBuffer)); } SafeRelease(pScaler); SafeRelease(pConverter); SafeRelease(pSource); SafeRelease(pDecoder); SafeRelease(pStream); SafeRelease(pIWICFactory); if (SUCCEEDED(hrCoInit)) { CoUninitialize(); } return hr; }
bool Image::Initialize(wchar_t *filename, Graphics *graphics) { std::ifstream file; IWICImagingFactory *pWIC; IWICStream *stream; IWICBitmapDecoder *decoder; IWICBitmapFrameDecode *frame; unsigned int width, height, twidth, theight, maxsize, bpp, i, support, rowPitch, imageSize; float ratio; WICPixelFormatGUID pixelFormat; DXGI_FORMAT format; IWICComponentInfo *cinfo; IWICPixelFormatInfo *pfinfo; IWICBitmapScaler *scaler; bool autogen = false; D3D11_TEXTURE2D_DESC desc; D3D11_SUBRESOURCE_DATA initData; D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; file.open(filename, std::ios::in | std::ios::binary | std::ios::ate); if(!file.is_open()) return false; m_size = file.tellg(); file.seekg(0, std::ios::beg); m_bytes = new unsigned char [m_size]; file.read((char *)m_bytes, m_size); file.close(); RETURN_FAIL( CoCreateInstance( CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), (LPVOID*)&pWIC ) ); RETURN_FAIL( pWIC->CreateStream( &stream ) ); RETURN_FAIL( stream->InitializeFromMemory( m_bytes, m_size ) ); RETURN_FAIL( pWIC->CreateDecoderFromStream( stream, 0, WICDecodeMetadataCacheOnDemand, &decoder ) ); RETURN_FAIL( decoder->GetFrame( 0, &frame ) ); RETURN_FAIL( frame->GetSize( &width, &height ) ); SAFE_RELEASE( stream ); SAFE_RELEASE( decoder ); if( width <= 0 || height <= 0 ) return false; maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; if( width > maxsize || height > maxsize ) { ratio = static_cast<float>(height) / static_cast<float>(width); if(width > height) { twidth = maxsize; theight = static_cast<unsigned int>( static_cast<float>(maxsize) * ratio ); } else { theight = maxsize; twidth = static_cast<unsigned int>( static_cast<float>(maxsize) / ratio ); } } else { twidth = width; theight = height; } RETURN_FAIL( frame->GetPixelFormat( &pixelFormat ) ); format = DXGI_FORMAT_UNKNOWN; for( i = 0; i < _countof(g_WICFormats); ++i ) { if( memcmp( &g_WICFormats[i].wic, &pixelFormat, sizeof(GUID) ) == 0 ) { format = g_WICFormats[i].dxgi; break; } } RETURN_FAIL( pWIC->CreateComponentInfo( pixelFormat, &cinfo ) ); RETURN_FAIL( cinfo->QueryInterface( __uuidof(IWICPixelFormatInfo), (LPVOID*)&pfinfo ) ); RETURN_FAIL( pfinfo->GetBitsPerPixel( &bpp ) ); SAFE_RELEASE( cinfo ); SAFE_RELEASE( pfinfo ); RETURN_FAIL( graphics->GetDevice()->CheckFormatSupport( format, &support ) ); if( !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D) ) return false; rowPitch = ( twidth * bpp + 7 ) / 8; imageSize = rowPitch * theight; std::unique_ptr<uint8_t[]> temp( new (std::nothrow) uint8_t[ imageSize ] ); if( !temp ) return false; if( twidth == width && theight == height ) { RETURN_FAIL( frame->CopyPixels( 0, rowPitch, imageSize, temp.get() ) ); } else { RETURN_FAIL( pWIC->CreateBitmapScaler( &scaler ) ); RETURN_FAIL( scaler->Initialize( frame, twidth, theight, WICBitmapInterpolationModeFant ) ); RETURN_FAIL( scaler->CopyPixels( 0, rowPitch, imageSize, temp.get() ) ); SAFE_RELEASE( scaler ); } SAFE_RELEASE( frame ); if( support & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) { autogen = true; } desc.Width = twidth; desc.Height = theight; desc.MipLevels = (autogen) ? 0 : 1; desc.ArraySize = 1; desc.Format = format; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_DEFAULT; desc.CPUAccessFlags = 0; desc.BindFlags = (autogen) ? D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET : D3D11_BIND_SHADER_RESOURCE; desc.MiscFlags = (autogen) ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0; initData.pSysMem = temp.get(); initData.SysMemPitch = rowPitch; initData.SysMemSlicePitch = imageSize; RETURN_FAIL( graphics->GetDevice()->CreateTexture2D( &desc, (autogen) ? nullptr : &initData, &m_texture2D ) ); //ZeroMemory( &SRVDesc, sizeof( SRVDesc ) ); memset( &SRVDesc, 0, sizeof( SRVDesc ) ); SRVDesc.Format = desc.Format; SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1; RETURN_FAIL( graphics->GetDevice()->CreateShaderResourceView( m_texture2D, &SRVDesc, &m_shaderResourceView ) ); if( autogen ) { graphics->GetContext()->UpdateSubresource( m_texture2D, 0, nullptr, temp.get(), rowPitch, imageSize ); graphics->GetContext()->GenerateMips( m_shaderResourceView ); } SAFE_RELEASE(pWIC); }
/// <summary> /// Load an image from a resource into a buffer /// </summary> /// <param name="resourceName">name of image resource to load</param> /// <param name="resourceType">type of resource to load</param> /// <param name="cOutputBuffer">size of output buffer, in bytes</param> /// <param name="outputBuffer">buffer that will hold the loaded image</param> /// <returns>S_OK on success, otherwise failure code</returns> HRESULT KinectEasyGrabber::LoadResourceImage( PCWSTR resourceName, PCWSTR resourceType, DWORD cOutputBuffer, BYTE* outputBuffer ) { HRESULT hr = S_OK; IWICImagingFactory* pIWICFactory = NULL; IWICBitmapDecoder* pDecoder = NULL; IWICBitmapFrameDecode* pSource = NULL; IWICStream* pStream = NULL; IWICFormatConverter* pConverter = NULL; IWICBitmapScaler* pScaler = NULL; HRSRC imageResHandle = NULL; HGLOBAL imageResDataHandle = NULL; void *pImageFile = NULL; DWORD imageFileSize = 0; hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pIWICFactory); if ( FAILED(hr) ) return hr; // Locate the resource. imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType); hr = imageResHandle ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { // Load the resource. imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle); hr = imageResDataHandle ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Lock it to get a system memory pointer. pImageFile = LockResource(imageResDataHandle); hr = pImageFile ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Calculate the size. imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle); hr = imageFileSize ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { // Create a WIC stream to map onto the memory. hr = pIWICFactory->CreateStream(&pStream); } if (SUCCEEDED(hr)) { // Initialize the stream with the memory pointer and size. hr = pStream->InitializeFromMemory( reinterpret_cast<BYTE*>(pImageFile), imageFileSize ); } if (SUCCEEDED(hr)) { // Create a decoder for the stream. hr = pIWICFactory->CreateDecoderFromStream( pStream, NULL, WICDecodeMetadataCacheOnLoad, &pDecoder ); } if (SUCCEEDED(hr)) { // Create the initial frame. hr = pDecoder->GetFrame(0, &pSource); } if (SUCCEEDED(hr)) { // Convert the image format to 32bppPBGRA // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). hr = pIWICFactory->CreateFormatConverter(&pConverter); } if (SUCCEEDED(hr)) { hr = pIWICFactory->CreateBitmapScaler(&pScaler); } if (SUCCEEDED(hr)) { hr = pScaler->Initialize( pSource, m_colorWidth, m_colorHeight, WICBitmapInterpolationModeCubic ); } if (SUCCEEDED(hr)) { hr = pConverter->Initialize( pScaler, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut ); } UINT width = 0; UINT height = 0; if (SUCCEEDED(hr)) { hr = pConverter->GetSize(&width, &height); } // make sure the output buffer is large enough if (SUCCEEDED(hr)) { if ( width*height*cBytesPerPixel > cOutputBuffer ) { hr = E_FAIL; } } if (SUCCEEDED(hr)) { hr = pConverter->CopyPixels(NULL, width*cBytesPerPixel, cOutputBuffer, outputBuffer); } SafeRelease(pScaler); SafeRelease(pConverter); SafeRelease(pSource); SafeRelease(pDecoder); SafeRelease(pStream); SafeRelease(pIWICFactory); return hr; }