本文整理汇总了C++中IWICBitmapFrameDecode类的典型用法代码示例。如果您正苦于以下问题:C++ IWICBitmapFrameDecode类的具体用法?C++ IWICBitmapFrameDecode怎么用?C++ IWICBitmapFrameDecode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IWICBitmapFrameDecode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: processImage
bool WICImageLoader::processImage(IWICBitmapDecoder* pDecoder)
{
HRESULT hr = E_FAIL;
IWICBitmapFrameDecode* pFrame = NULL;
if(NULL != pDecoder)
{
hr = pDecoder->GetFrame(0, &pFrame);
}
if(SUCCEEDED(hr))
{
hr = pFrame->GetPixelFormat(&_format);
}
IWICFormatConverter* pConv = NULL;
if(SUCCEEDED(hr))
{
hr = convertFormatIfRequired(pFrame, &pConv);
}
if(SUCCEEDED(hr))
{
_bpp = getBitsPerPixel(_format);
if(NULL != pConv)
{
hr = pConv->GetSize((UINT*)&_width, (UINT*)&_height);
}
else
{
hr = pFrame->GetSize((UINT*)&_width, (UINT*)&_height);
}
}
assert(_bpp > 0);
assert(_width > 0 && _height > 0);
if(SUCCEEDED(hr))
{
size_t rowPitch = (_width * _bpp + 7) / 8;
_dataLen = rowPitch * _height;
_data = new (std::nothrow) BYTE[_dataLen];
if(NULL != pConv)
{
hr = pConv->CopyPixels(NULL, static_cast<UINT>(rowPitch), static_cast<UINT>(_dataLen), _data);
}
else
{
hr = pFrame->CopyPixels(NULL, static_cast<UINT>(rowPitch), static_cast<UINT>(_dataLen), _data);
}
}
SafeRelease(&pFrame);
SafeRelease(&pConv);
return SUCCEEDED(hr);
}
示例2: Buffer
Image::Buffer * WIC::Api::CreateImage(char * buffer, unsigned bufferLength)
{
if(!factory) return 0;
// Create input stream for memory
IWICStream * stream = 0;
factory->CreateStream(&stream);
if(!stream) return 0;
stream->InitializeFromMemory((unsigned char*)(buffer), static_cast<DWORD>(bufferLength));
IWICBitmapDecoder * decoder = 0;
factory->CreateDecoderFromStream(stream, 0, WICDecodeMetadataCacheOnDemand, &decoder);
if(!decoder) return 0;
IWICBitmapFrameDecode * bitmapSource = 0;
decoder->GetFrame(0, &bitmapSource);
if(!bitmapSource)
{
decoder->Release();
return 0;
}
IWICBitmapSource * convertedSource = 0;
WICConvertBitmapSource(GUID_WICPixelFormat32bppRGBA, bitmapSource, &convertedSource);
bitmapSource->Release();
if(!convertedSource)
{
decoder->Release();
return 0;
}
// Create the bitmap from the image frame.
IWICBitmap * bitmap = 0;
factory->CreateBitmapFromSource(
convertedSource, // Create a bitmap from the image frame
WICBitmapCacheOnDemand, // Cache metadata when needed
&bitmap); // Pointer to the bitmap
convertedSource->Release();
decoder->Release();
if(!bitmap) return 0;
unsigned width = 0, height = 0;
bitmap->GetSize(&width, &height);
WICRect lockRect = { 0, 0, width, height };
IWICBitmapLock * lock = 0;
bitmap->Lock(&lockRect, WICBitmapLockWrite, &lock);
if(!lock)
{
bitmap->Release();
return 0;
}
return new WIC::Buffer(bitmap, lock, width, height);
}
示例3: CoCreateInstance
SpriteSheet::SpriteSheet(wchar_t* filename, Graphics* gfx)
{
this->gfx = gfx;
bmp = NULL;
HRESULT hr;
//create a wic factory
IWICImagingFactory *wicFactory = NULL;
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&wicFactory);
//create a decoder
IWICBitmapDecoder *wicDecoder = NULL;
hr = wicFactory->CreateDecoderFromFilename(
filename, // THE FILE NAME
NULL, // the preferred vendor
GENERIC_READ, // we're reading the file, not writing
WICDecodeMetadataCacheOnLoad,
&wicDecoder);
// read a frame from the image
IWICBitmapFrameDecode* wicFrame = NULL;
hr = wicDecoder->GetFrame(0, &wicFrame);
// create a converter
IWICFormatConverter *wicConverter = NULL;
hr = wicFactory->CreateFormatConverter(&wicConverter);
// setup the converter
hr = wicConverter->Initialize(
wicFrame, // frame
GUID_WICPixelFormat32bppPBGRA, // pixel format
WICBitmapDitherTypeNone, // irrelevant
NULL, // no palette needed, irrlevant
0.0, // alpha transparency % irrelevant
WICBitmapPaletteTypeCustom // irrelevant
);
// use the converter to create an D2D1Bitmap
/// ID2D1Bitmap* bmp; // this will be a member variable
hr = gfx->GetRenderTarget()->CreateBitmapFromWicBitmap(
wicConverter, // converter
NULL, // D2D1_BITMAP_PROPERIES
&bmp // destiatnion D2D1 bitmap
);
if (wicFactory) wicFactory->Release();
if (wicDecoder) wicDecoder->Release();
if (wicConverter) wicConverter->Release();
if (wicFrame) wicFrame->Release();
}
示例4: LoadLocalPNG
HBITMAP LoadLocalPNG( const std::string& sPath, const RASize& sz )
{
SetCurrentDirectory( Widen( g_sHomeDir ).c_str() );
ASSERT( _FileExists( sPath ) );
if( !_FileExists( sPath ) )
{
RA_LOG( "File could not be found: %s\n", sPath.c_str() );
return nullptr;
}
HBITMAP hRetVal = nullptr;
// Step 2: Decode the source image to IWICBitmapSource
IWICBitmapDecoder* pDecoder = nullptr;
HRESULT hr = g_UserImageFactoryInst.m_pIWICFactory->CreateDecoderFromFilename( Widen( sPath ).c_str(), // Image to be decoded
nullptr, // Do not prefer a particular vendor
GENERIC_READ, // Desired read access to the file
WICDecodeMetadataCacheOnDemand, // Cache metadata when needed
&pDecoder ); // Pointer to the decoder
// Retrieve the first frame of the image from the decoder
IWICBitmapFrameDecode* pFrame = nullptr;
if( SUCCEEDED( hr ) )
hr = pDecoder->GetFrame( 0, &pFrame );
// Retrieve IWICBitmapSource from the frame
if( SUCCEEDED( hr ) )
{
SAFE_RELEASE( g_UserImageFactoryInst.m_pOriginalBitmapSource ); //##SD ???
pFrame->QueryInterface( IID_IWICBitmapSource, reinterpret_cast<void**>( &g_UserImageFactoryInst.m_pOriginalBitmapSource ) );
}
// Step 3: Scale the original IWICBitmapSource to the client rect size
// and convert the pixel format
IWICBitmapSource* pToRenderBitmapSource = nullptr;
if( SUCCEEDED( hr ) )
hr = ConvertBitmapSource( { 0, 0, sz.Width(), sz.Height() }, pToRenderBitmapSource );
// Step 4: Create a DIB from the converted IWICBitmapSource
if( SUCCEEDED( hr ) )
hr = UserImageFactory_CreateDIBSectionFromBitmapSource( pToRenderBitmapSource, hRetVal );
SAFE_RELEASE( pToRenderBitmapSource );
SAFE_RELEASE( pDecoder );
SAFE_RELEASE( pFrame );
SAFE_RELEASE( g_UserImageFactoryInst.m_pOriginalBitmapSource );
return hRetVal;
}
示例5: LoadBitmapFromStream
// Loads a PNG image from the specified stream (using Windows Imaging Component).
IWICBitmapSource * LoadBitmapFromStream(IStream * ipImageStream)
{
// initialize return value
IWICBitmapSource * ipBitmap = NULL;
// load WIC's PNG decoder
IWICBitmapDecoder * ipDecoder = NULL;
IID i = IID_IWICBitmapDecoder;
if (FAILED(CoCreateInstance(CLSID_WICPngDecoder, NULL, CLSCTX_INPROC_SERVER,
i,//__uuidof(ipDecoder)
(void **)&ipDecoder)))
goto Return;
// load the PNG
if (FAILED(ipDecoder->Initialize(ipImageStream, WICDecodeMetadataCacheOnLoad)))
goto ReleaseDecoder;
// check for the presence of the first frame in the bitmap
UINT nFrameCount = 0;
if (FAILED(ipDecoder->GetFrameCount(&nFrameCount)) || nFrameCount != 1)
goto ReleaseDecoder;
// load the first frame (i.e., the image)
IWICBitmapFrameDecode * ipFrame = NULL;
if (FAILED(ipDecoder->GetFrame(0, &ipFrame)))
goto ReleaseDecoder;
// convert the image to 32bpp BGRA format with pre-multiplied alpha
// (it may not be stored in that format natively in the PNG resource,
// but we need this format to create the DIB to use on-screen)
WICConvertBitmapSource(GUID_WICPixelFormat32bppPBGRA, ipFrame, &ipBitmap);
ipFrame->Release();
ReleaseDecoder:
ipDecoder->Release();
Return:
return ipBitmap;
}
示例6: LoadBitmapFromFile
// =========================================================
// Load the specified bitmap from file into D2D bitmap
// =========================================================
BOOL Loader::LoadBitmapFromFile(LPCWSTR filename, const Graphics* graphicsWrapper, ID2D1Bitmap** ppBitmap)
{
if (!isInitialized)
return FALSE;
HRESULT hr;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;
hr = pFactory->CreateDecoderFromFilename(filename, NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &pDecoder);
CHECK(failedDecoder);
hr = pDecoder->GetFrame(0, &pSource);
CHECK(failedGetFrame);
hr = pFactory->CreateFormatConverter(&pConverter);
CHECK(failedConverter);
hr = pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0, WICBitmapPaletteTypeMedianCut);
CHECK(failedConversion);
hr = graphicsWrapper->pRenderTarget->CreateBitmapFromWicBitmap(pSource, ppBitmap);
CHECK(failedBitmap);
return TRUE;
failedBitmap:
OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to create bitmap.\n");
failedConversion:
pConverter->Release();
OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to initialise format converter.\n");
failedConverter:
pSource->Release();
OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to create format converter.\n");
failedGetFrame:
pDecoder->Release();
OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to get frame 0 from source bitmap.\n");
failedDecoder:
OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to create bitmap decoder.\n");
return FALSE;
}
示例7: loadImage
HBITMAP ImageDecoder::loadImage(IWICBitmapDecoder *decoder)
{
IWICBitmapFrameDecode *frame = NULL;
if (FAILED(decoder->GetFrame(0, &frame)))
{
throw Exception("could not get frame");
}
IWICBitmapSource *bitmapSource = NULL;
if (FAILED(WICConvertBitmapSource(GUID_WICPixelFormat32bppPBGRA, frame, &bitmapSource)))
{
throw Exception("could not convert bitmap");
}
HBITMAP bitmap = convertFrameToBitmap(frame);
frame->Release();
return bitmap;
}
示例8: SHCreateMemStream
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();
}
示例9: loadResource
void ImageDecoder::loadGifFromResource(const WCHAR *name, const WCHAR *type, std::vector<AnimatedGifFrame *> &frames)
{
unsigned char *bytes = NULL;
DWORD size = 0;
loadResource(name, type, bytes, size);
IWICStream *stream = createStreamFromBytes(bytes, size);
IWICBitmapDecoder *decoder = createDecoderFromStream(stream);
unsigned int frameCount = 0;
if (FAILED(decoder->GetFrameCount(&frameCount)))
{
throw Exception("could not get gif frame count");
}
if (frameCount <= 1)
{
throw Exception("not a gif animation");
}
for (unsigned int frameIndex = 0; frameIndex < frameCount; frameIndex++)
{
IWICBitmapFrameDecode *bitmapFrame = NULL;
if (FAILED(decoder->GetFrame(frameIndex, &bitmapFrame)))
{
throw Exception("could not get frame");
}
HBITMAP bitmap = convertFrameToBitmap(bitmapFrame);
bitmapFrame->Release();
AnimatedGifFrame *frame = new AnimatedGifFrame(bitmap);
frames.push_back(frame);
}
decoder->Release();
stream->Release();
}
示例10: INETRException
HBITMAP ImageUtil::LoadImage(string path) {
size_t pathFileExtDot = path.find_last_of('.');
if (pathFileExtDot == string::npos || pathFileExtDot + 1 >=
path.length())
throw INETRException("[imgLoadFailed]");
string ext = path.substr(pathFileExtDot + 1);
const GUID *decoderCLSID = nullptr;
if (ext == "png")
decoderCLSID = &CLSID_WICPngDecoder;
else if (ext == "bmp")
decoderCLSID = &CLSID_WICBmpDecoder;
else if (ext == "gif")
decoderCLSID = &CLSID_WICGifDecoder;
else if (ext == "jpg" || ext == "jpeg")
decoderCLSID = &CLSID_WICJpegDecoder;
if (decoderCLSID == nullptr)
throw INETRException("[unsupportedImg]: " + ext);
IStream *pngFileStream;
if (FAILED(SHCreateStreamOnFile(path.c_str(), STGM_READ,
&pngFileStream)))
throw INETRException("[imgLoadFailed]:\n" + path);
IWICBitmapDecoder *bmpDecoder = nullptr;
if (FAILED(CoCreateInstance(*decoderCLSID, nullptr,
CLSCTX_INPROC_SERVER, __uuidof(bmpDecoder),
reinterpret_cast<void**>(&bmpDecoder))))
throw INETRException("[imgDecFailed]");
if (FAILED(bmpDecoder->Initialize(pngFileStream,
WICDecodeMetadataCacheOnLoad))) {
bmpDecoder->Release();
throw INETRException("[imgDecFailed]");
}
UINT bmpFrameCount = 0;
if (FAILED(bmpDecoder->GetFrameCount(&bmpFrameCount)) && bmpFrameCount
!= 1) {
bmpDecoder->Release();
throw INETRException("[imgDecFailed]");
}
IWICBitmapFrameDecode *bmpFrame = nullptr;
if (FAILED(bmpDecoder->GetFrame(0, &bmpFrame))) {
bmpDecoder->Release();
throw INETRException("[imgDecFailed]");
}
IWICBitmapSource *bmpSource = nullptr;
WICConvertBitmapSource(GUID_WICPixelFormat32bppPBGRA, bmpFrame,
&bmpSource);
bmpFrame->Release();
bmpDecoder->Release();
UINT width = 0, height = 0;
if (FAILED(bmpSource->GetSize(&width, &height)) || width == 0 || height
== 0)
throw INETRException("[imgDecFailed]");
BITMAPINFO bmInfo;
ZeroMemory(&bmInfo, sizeof(bmInfo));
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = width;
bmInfo.bmiHeader.biHeight = -((LONG)height);
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 32;
bmInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP hbmp = nullptr;
void *imageBits = nullptr;
HDC screenDC = GetDC(nullptr);
hbmp = CreateDIBSection(screenDC, &bmInfo, DIB_RGB_COLORS, &imageBits,
nullptr, 0);
ReleaseDC(nullptr, screenDC);
if (hbmp == nullptr)
throw INETRException("[imgDecFailed]");
const UINT bmpStride = width * 4;
const UINT bmpSize = bmpStride * height;
if (FAILED(bmpSource->CopyPixels(nullptr, bmpStride, bmpSize,
static_cast<BYTE*>(imageBits)))) {
DeleteObject(hbmp);
hbmp = nullptr;
throw INETRException("[imgDecFailed]");
}
bmpSource->Release();
return hbmp;
}
示例11: DecodeFrame
// 解码照片的基础位图文件的.
HRESULT VideoEncoder::DecodeFrame(Photo* pPhoto, BYTE** ppOutputBitmap, int* pBitmapSize)
{
HRESULT hr = S_OK;
IWICBitmapDecoder *pDecoder = nullptr;
IWICBitmapFrameDecode *pFrame = nullptr;
IWICBitmap* pSourceBitmap = nullptr;
IWICBitmapLock* pLock = nullptr;
BYTE* pSourceBuffer = nullptr;
BYTE* pDestinationBuffer = nullptr;
UINT pixelWidth;
UINT pixelHeight;
WICRect lockRect;
*ppOutputBitmap = nullptr;
hr = m_pIWICFactory->CreateDecoderFromFilename(
pPhoto->GetFile().c_str(),
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnDemand,
&pDecoder
);
CheckHR(hr);
this->m_logFileStream << "WIC解码器创建成功." << endl;
GUID containerFormat;
CheckHR(pDecoder->GetContainerFormat(&containerFormat));
// 我们仅支持jpg 文件.
if (containerFormat != GUID_ContainerFormatJpeg)
{
this->m_logFileStream << "仅支持jpeg文件." << endl;
return E_NOTSUPPORTEDFORMAT;
}
// 我们仅支持jpg 文件. 因此只有一桢.
CheckHR(pDecoder->GetFrame(0, &pFrame));
// TODO: 目前我们需要所有照片有相同的大小.
// 如果需求在将来发生变化,修改代码.
pFrame->GetSize(&pixelWidth, &pixelHeight);
if (pixelWidth != this->m_frameWidth || pixelHeight != this->m_frameHeight)
{
this->m_logFileStream << "所有的照片必须使用固定的大小." << endl;
return E_IMAGESIZEINCORRECT;
}
// 创建源位图对象.
CheckHR(this->m_pIWICFactory->CreateBitmapFromSource(pFrame, WICBitmapCacheOnLoad, &pSourceBitmap));
this->m_logFileStream << "位图资源创建成功." << endl;
lockRect.X = 0;
lockRect.Y = 0;
lockRect.Width = pixelWidth;
lockRect.Height = pixelHeight;
CheckHR(pSourceBitmap->Lock(&lockRect, WICBitmapLockWrite, &pLock));
UINT sourceBufferSize;
CheckHR(pLock->GetDataPointer(&sourceBufferSize, &pSourceBuffer));
// Jpg BGR 位图转换 RGBA 格式.
UINT destinationBufferSize = sourceBufferSize / 3 * 4;
pDestinationBuffer = new BYTE[destinationBufferSize];
for (UINT i = 0, j = 0; i < sourceBufferSize; i+=3, j+=4)
{
pDestinationBuffer[j] = pSourceBuffer[i]; // R
pDestinationBuffer[j + 1] = pSourceBuffer[i + 1]; // G
pDestinationBuffer[j + 2] = pSourceBuffer[i + 2]; // B
pDestinationBuffer[j + 3] = 255; // A
}
*ppOutputBitmap = pDestinationBuffer;
*pBitmapSize = destinationBufferSize;
cleanup:
if (!SUCCEEDED(hr))
{
DWORD error = GetLastError();
this->m_logFileStream << "意外错误: " << error << endl;
this->m_logFileStream << "HResult是: " << hr << endl;
}
if (pSourceBuffer != nullptr)
{
// 删除pSourceBuffer;
}
SafeRelease(&pDecoder);
SafeRelease(&pFrame);
SafeRelease(&pSourceBitmap);
SafeRelease(&pLock);
return hr;
}
示例12: LoadResourceBitmap
HRESULT LoadResourceBitmap(
ID2D1RenderTarget* pRendertarget,
IWICImagingFactory* pIWICFactory,
PCWSTR resourceName,
PCWSTR resourceType,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap** ppBitmap
)
{
HRESULT hr = S_OK;
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;
// Find the resource then load it
imageResHandle = FindResource(HINST_THISCOMPONENT, resourceName, resourceType);
hr = imageResHandle ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageResDataHandle ? S_OK : E_FAIL;
}
// Lock the resource and calculate the image's size
if (SUCCEEDED(hr))
{
// Lock it to get the 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;
}
// Create an IWICStream object
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
);
}
// Create IWICBitmapDecoder
if (SUCCEEDED(hr))
{
// Create a decoder for the stream
hr = pIWICFactory->CreateDecoderFromStream(
pStream,
NULL,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
}
// Retrieve a frame from the image and store it in an IWICBitmapFrameDecode object
if (SUCCEEDED(hr))
{
// Create the initial frame
hr = pDecoder->GetFrame(0, &pSource);
}
// Before Direct2D can use the image, it must be converted to the 32bppPBGRA pixel format.
// To convert the image format, use the IWICImagingFactory::CreateFormatConverter method to create an IWICFormatConverter object, then use the IWICFormatConverter object's Initialize method to perform the conversion.
if (SUCCEEDED(hr))
{
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
// If a new width or height was specified, create and
// IWICBitmapScaler and use it to resize the image.
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth;
//.........这里部分代码省略.........
示例13: LoadResourceBitmap
HRESULT App::LoadResourceBitmap(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR resourceName,
PCWSTR resourceType,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
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;
// Locate the resource.
imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
HRESULT 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))
{
// If a new width or height was specified, create an
// IWICBitmapScaler and use it to resize the image.
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth, originalHeight;
hr = pSource->GetSize(&originalWidth, &originalHeight);
if (SUCCEEDED(hr))
{
if (destinationWidth == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destinationHeight == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
}
//.........这里部分代码省略.........
示例14: DXASS
unsigned char *BBWin8Game::LoadImageData( String path,int *pwidth,int *pheight,int *pformat ){
if( !_wicFactory ){
DXASS( CoCreateInstance( CLSID_WICImagingFactory,0,CLSCTX_INPROC_SERVER,__uuidof(IWICImagingFactory),(LPVOID*)&_wicFactory ) );
}
path=PathToFilePath( path );
IWICBitmapDecoder *decoder;
if( !SUCCEEDED( _wicFactory->CreateDecoderFromFilename( path.ToCString<wchar_t>(),NULL,GENERIC_READ,WICDecodeMetadataCacheOnDemand,&decoder ) ) ){
return 0;
}
unsigned char *data=0;
IWICBitmapFrameDecode *bitmapFrame;
DXASS( decoder->GetFrame( 0,&bitmapFrame ) );
UINT width,height;
WICPixelFormatGUID pixelFormat;
DXASS( bitmapFrame->GetSize( &width,&height ) );
DXASS( bitmapFrame->GetPixelFormat( &pixelFormat ) );
if( pixelFormat==GUID_WICPixelFormat24bppBGR ){
unsigned char *t=(unsigned char*)malloc( width*3*height );
DXASS( bitmapFrame->CopyPixels( 0,width*3,width*3*height,t ) );
data=(unsigned char*)malloc( width*4*height );
unsigned char *s=t,*d=data;
int n=width*height;
while( n-- ){
*d++=s[2];
*d++=s[1];
*d++=s[0];
*d++=0xff;
s+=3;
}
free( t );
}else if( pixelFormat==GUID_WICPixelFormat32bppBGRA ){
unsigned char *t=(unsigned char*)malloc( width*4*height );
DXASS( bitmapFrame->CopyPixels( 0,width*4,width*4*height,t ) );
data=t;
int n=width*height;
while( n-- ){ //premultiply alpha
unsigned char r=t[0];
t[0]=t[2]*t[3]/255;
t[1]=t[1]*t[3]/255;
t[2]=r*t[3]/255;
t+=4;
}
}
if( data ){
*pwidth=width;
*pheight=height;
*pformat=4;
}
bitmapFrame->Release();
decoder->Release();
gc_force_sweep=true;
return data;
}
示例15: LoadFile
bool ResourceManager::LoadFile(ID2D1HwndRenderTarget* renderTarget, wchar_t * filename)
{
HRESULT result;
IWICImagingFactory2* wicFactory;
IWICBitmapDecoder* wicDecoder;
IWICBitmapFrameDecode* wicFrame;
IWICBitmapFlipRotator* wicFlip;
IWICFormatConverter *wicConverter;
Sprite* newSprite;
// WIC의 각종 인터페이스를 사용하기 위한 factory 생성
result = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicFactory));
if (FAILED(result))
return false;
// 파일을 읽고 디코딩 하기 위한 decoder 생성
result = wicFactory->CreateDecoderFromFilename(filename, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &wicDecoder);
if (FAILED(result))
return false;
// decoder에서 프레임을 얻어옴, // 일반적인 이미지 파일은 single frame만을 지원하므로 0으로 고정
result = wicDecoder->GetFrame(0, &wicFrame);
if (FAILED(result))
return false;
// 수평으로 뒤집힌 이미지를 얻기 위해 BitmapFlipRotator 생성
result = wicFactory->CreateBitmapFlipRotator(&wicFlip);
if (FAILED(result))
return false;
// wicFrame를 수평으로 뒤집음
wicFlip->Initialize(wicFrame, WICBitmapTransformFlipHorizontal);
// WICBitmap을 D2DBitmap으로 변환시키기 위해 format converter 생성
result = wicFactory->CreateFormatConverter(&wicConverter);
if (FAILED(result))
return false;
// Converter[0]의 Format을 일반 이미지(wicFrame)에 맞춤
result = wicConverter->Initialize(wicFrame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom);
if (FAILED(result))
return false;
// 리소스 정보를 저장 할 Sprite 생성
newSprite = new Sprite(renderTarget);
if (!newSprite)
return false;
// WICBitmap을 D2DBitmap으로 변환
//result = renderTarget->CreateBitmapFromWicBitmap(wicConverter, nullptr, &m_Bitmap);
result = renderTarget->CreateBitmapFromWicBitmap(wicConverter, nullptr, newSprite->GetBitmap());
if (FAILED(result))
return false;
ID2D1Bitmap* bitmap = *(newSprite->GetBitmap());
int numberOfFrame = bitmap->GetSize().width / IMAGE_SIZE;
int numberOfAction = bitmap->GetSize().height / IMAGE_SIZE;
newSprite->Initialize(numberOfFrame, numberOfAction);
wchar_t* buffer = new wchar_t[128];
wcscpy_s(buffer, wcslen(filename) + 1, filename);
// 스프라이트 등록
m_Sprites.push_back(newSprite);
m_Filenames.push_back(buffer);
wicConverter->Release();
wicConverter = nullptr;
wicFrame->Release();
wicFrame = nullptr;
wicFlip->Release();
wicFlip = nullptr;
wicDecoder->Release();
wicDecoder = nullptr;
wicFactory->Release();
wicFactory = nullptr;
return true;
}