PageRenderTime 551ms CodeModel.GetById 291ms app.highlight 14ms RepoModel.GetById 243ms app.codeStats 0ms

/src/os/wince/ftk_image_wince_decoder.cpp

http://ftk.googlecode.com/
C++ | 127 lines | 117 code | 10 blank | 0 comment | 25 complexity | 8cb046b846282f709784b9baa5ea489e MD5 | raw file
  1
  2#include "ftk_wince.h"
  3#include "ftk_image_wince_decoder.h"
  4#include "ftk_log.h"
  5#include <initguid.h>
  6#include <Imaging.h>
  7
  8static Ret ftk_image_wince_decoder_match(FtkImageDecoder* thiz, const char* filename)
  9{
 10	return_val_if_fail(filename != NULL, RET_FAIL);
 11
 12	return (strstr(filename, "bmp") != NULL
 13		|| strstr(filename, ".png") != NULL 
 14		|| strstr(filename, ".jpg") != NULL
 15		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
 16}
 17
 18static FtkBitmap* load_image(const char *filename)
 19{
 20	int x = 0;
 21	int y = 0;
 22	int w = 0;
 23	int h = 0;
 24	FtkColor bg = {0};
 25	FtkBitmap* bitmap = NULL;
 26	HRESULT hr;
 27	IImagingFactory *img_factory = NULL;
 28	IImage *img = NULL;
 29	IBitmapImage *bmp = NULL;
 30	SIZE size;
 31	BitmapData bmp_data;
 32	RECT rect;
 33	wchar_t buf[MAX_PATH];
 34	if (MultiByteToWideChar(CP_ACP, 0, filename, -1, buf, MAX_PATH) == 0)
 35	{
 36		return NULL;
 37	}
 38	hr = CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **) &img_factory);
 39	if (FAILED(hr))
 40	{
 41		ftk_loge("CoCreateInstance(CLSID_ImagingFactory) failed\n");
 42		return NULL;
 43	}
 44	hr = img_factory->CreateImageFromFile(buf, &img);
 45	if (FAILED(hr))
 46	{
 47		ftk_loge("CreateImageFromFile() failed (%s)\n", filename);
 48		img_factory->Release();
 49		return NULL;
 50	}
 51	hr = img_factory->CreateBitmapFromImage(img, 0, 0, PixelFormatDontCare, InterpolationHintDefault, &bmp);
 52	if (FAILED(hr))
 53	{
 54		ftk_loge("CreateBitmapFromImage() failed\n");
 55		img->Release();
 56		img_factory->Release();
 57		return NULL;
 58	}
 59	size.cx = 0;
 60	size.cy = 0;
 61	hr = bmp->GetSize(&size);
 62	if (FAILED(hr))
 63	{
 64		ftk_loge("GetSize() failed\n");
 65	}
 66	if (size.cx == 0 || size.cy == 0)
 67	{
 68		bmp->Release();
 69		img->Release();
 70		img_factory->Release();
 71		return NULL;
 72	}
 73	w = size.cx;
 74	h = size.cy;
 75	bg.a = 0xff;
 76	bitmap = ftk_bitmap_create(w, h, bg);
 77	rect.left = 0;
 78	rect.top = 0;
 79	rect.right = w;
 80	rect.bottom = h;
 81	hr = bmp->LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, &bmp_data);
 82	FtkColor* src = (FtkColor*)bmp_data.Scan0;
 83	FtkColor* dst = ftk_bitmap_lock(bitmap);
 84	for(y = 0; y < h; y++)
 85	{
 86		for(x = 0; x < w; x++)
 87		{
 88			*dst = *src;
 89			dst++;
 90			src++;
 91		}
 92	}
 93	hr = bmp->UnlockBits(&bmp_data);
 94	bmp->Release();
 95	img->Release();
 96	img_factory->Release();
 97	return bitmap;
 98}
 99
100static FtkBitmap* ftk_image_wince_decoder_decode(FtkImageDecoder* thiz, const char* filename)
101{
102	return_val_if_fail(ftk_image_wince_decoder_match(thiz, filename) == RET_OK, NULL);
103
104	return load_image(filename);
105}
106
107static void ftk_image_wince_decoder_destroy(FtkImageDecoder* thiz)
108{
109	if(thiz != NULL)
110	{
111		FTK_ZFREE(thiz, sizeof(thiz));
112	}
113}
114
115FtkImageDecoder* ftk_image_wince_decoder_create(void)
116{
117	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
118
119	if(thiz != NULL)
120	{
121		thiz->match   = ftk_image_wince_decoder_match;
122		thiz->decode  = ftk_image_wince_decoder_decode;
123		thiz->destroy = ftk_image_wince_decoder_destroy;
124	}
125
126	return thiz;
127}