HBITMAP ImageDecoder::loadImage(unsigned char *bytes, int size)
{
	IWICStream *stream = createStreamFromBytes(bytes, size);
	IWICBitmapDecoder *decoder = createDecoderFromStream(stream);

	HBITMAP bitmap = loadImage(decoder);

	decoder->Release();
	stream->Release();

	return bitmap;
}
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();
}
Example #3
0
// =========================================================
// Load the specified bitmap from file into D2D bitmap
// =========================================================
BOOL Loader::LoadBitmapFromResource(LPCWSTR resourceId, LPCWSTR resourceType, const Graphics* graphicsWrapper, ID2D1Bitmap** ppBitmap)
{
	if (!isInitialized)
		return FALSE;

	HRESULT hr;

	IWICStream *pStream = NULL;
	IWICBitmapDecoder *pDecoder = NULL;
	IWICBitmapFrameDecode *pSource = NULL;
	IWICFormatConverter *pConverter = NULL;

	HRSRC imageResHandle = NULL;
	HGLOBAL imageResDataHandle = NULL;
	void *pImageFile = NULL;
	DWORD imageFileSize = 0;

	HMODULE module = GetModuleHandle(0);

	// Locate the resource
	imageResHandle = FindResource(module, resourceId, resourceType);
	if (!imageResHandle)
	{
		OutputDebugStringA("Loader::LoadBitmapFromResource -> Failed to locate resource.\n");
		return FALSE;
	}

	imageResDataHandle = LoadResource(module, imageResHandle);
	if (!imageResDataHandle)
	{
		OutputDebugStringA("Loader::LoadBitmapFromResource -> Failed to load resource.\n");
		return FALSE;
	}

	// lock resource to get pointer to data
	pImageFile = LockResource(imageResDataHandle);
	if (!pImageFile)
	{
		OutputDebugStringA("Loader::LoadBitmapFromResource -> Failed to lock resource for processing.\n");
		return FALSE;
	}

	// get the size of the image
	imageFileSize = SizeofResource(module, imageResHandle);
	if (!imageFileSize)
	{
		OutputDebugStringA("Loader::LoadBitmapFromResource -> Failed to obtain size of image resource.\n");
		return FALSE;
	}

	// now that we have everything, we create the stream
	hr = pFactory->CreateStream(&pStream);
	CHECK(failedCreateStream);

	// initialise the stream
	hr = pStream->InitializeFromMemory((byte*)pImageFile, imageFileSize);
	CHECK(failedStreamInit);

	hr = pFactory->CreateDecoderFromStream(pStream, NULL, 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(pConverter, 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:
	pStream->Release();
	OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to create bitmap decoder.\n");
	return FALSE;
failedStreamInit:
	OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to initialize WIC stream.\n");
	return FALSE;
failedCreateStream:
	OutputDebugStringA("Loader::LoadBitmapFromFile -> Failed to create WIC stream.\n");
	return FALSE;
}