PageRenderTime 76ms CodeModel.GetById 57ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/os/ucos-ii/ftk_image_win32_decoder.cpp

http://ftk.googlecode.com/
C++ | 131 lines | 78 code | 24 blank | 29 comment | 18 complexity | d473e2f8264f9104fc878f8de7bb3c53 MD5 | raw file
  1/*
  2 * File: ftk_image_win32_decoder.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   png/jpeg format image decoder on win32.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-01-17 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include <windows.h>
 32#include <gdiplus.h>
 33
 34using namespace Gdiplus;
 35static ULONG_PTR gdiplusToken;
 36
 37#include "ftk_image_win32_decoder.h"
 38static Ret ftk_image_win32_decoder_match(FtkImageDecoder* thiz, const char* filename)
 39{
 40	return_val_if_fail(filename != NULL, RET_FAIL);
 41
 42	return (strstr(filename, ".png") != NULL 
 43		|| strstr(filename, ".jpg") != NULL
 44		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
 45}
 46
 47static FtkBitmap* load_win32 (const char *filename)
 48{
 49	int x = 0;
 50	int y = 0;
 51	int w = 0;
 52	int h = 0;
 53	FtkColor bg = {0};
 54	FtkBitmap* bitmap = NULL;
 55	WCHAR wfilename[MAX_PATH] = {0};
 56	mbstowcs(wfilename, filename, MAX_PATH);
 57	Bitmap* img = Bitmap::FromFile(wfilename);
 58
 59	return_val_if_fail(img != NULL, NULL);
 60	if(img->GetWidth() == 0 || img->GetHeight() == 0)
 61	{
 62		delete img;
 63
 64		return NULL;
 65	}
 66
 67	w = img->GetWidth();
 68	h = img->GetHeight();
 69
 70	bg.a = 0xff;
 71	bitmap = ftk_bitmap_create(w, h, bg);
 72	Rect r(0, 0, w, h);
 73	BitmapData bitmapData;
 74	img->LockBits(&r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
 75
 76	FtkColor* src = (FtkColor*)bitmapData.Scan0;
 77	FtkColor* dst = ftk_bitmap_lock(bitmap);
 78
 79	for(y = 0; y < h; y++)
 80	{
 81		for(x = 0; x < w; x++)
 82		{
 83			*dst = *src;
 84			dst++;
 85			src++;
 86		}
 87	}
 88
 89	img->UnlockBits(&bitmapData);
 90	
 91	delete img;
 92
 93	return bitmap;
 94}
 95
 96static FtkBitmap* ftk_image_win32_decoder_decode(FtkImageDecoder* thiz, const char* filename)
 97{
 98	return_val_if_fail(ftk_image_win32_decoder_match(thiz, filename) == RET_OK, NULL);
 99
100	return load_win32(filename);
101}
102
103static void ftk_image_win32_decoder_destroy(FtkImageDecoder* thiz)
104{
105	if(thiz != NULL)
106	{
107		FTK_ZFREE(thiz, sizeof(thiz));
108	}
109
110	 GdiplusShutdown(gdiplusToken);
111
112	return;
113}
114
115FtkImageDecoder* ftk_image_win32_decoder_create(void)
116{
117	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
118
119	if(thiz != NULL)
120	{
121		GdiplusStartupInput gdiplusStartupInput;
122
123		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
124		thiz->match   = ftk_image_win32_decoder_match;
125		thiz->decode  = ftk_image_win32_decoder_decode;
126		thiz->destroy = ftk_image_win32_decoder_destroy;
127	}
128
129	return thiz;
130}
131