PageRenderTime 80ms CodeModel.GetById 67ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/src/os/windows/ftk_image_win32_decoder.cpp

http://ftk.googlecode.com/
C++ | 140 lines | 86 code | 25 blank | 29 comment | 18 complexity | 6c9b10518503fc523c3964cbd07a204a 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
 32#ifdef VC6

 33#define ULONG_PTR unsigned long*

 34#endif 

 35
 36#include <windows.h>
 37#include <gdiplus.h>
 38#include "ftk_win32.h"
 39
 40using namespace Gdiplus;
 41static ULONG_PTR gdiplusToken;
 42
 43#include "ftk_image_win32_decoder.h"
 44static Ret ftk_image_win32_decoder_match(FtkImageDecoder* thiz, const char* filename)
 45{
 46	return_val_if_fail(filename != NULL, RET_FAIL);
 47
 48	return (strstr(filename, ".png") != NULL 
 49		|| strstr(filename, ".jpg") != NULL
 50		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
 51}
 52
 53static FtkBitmap* load_win32 (const char *filename)
 54{
 55	int x = 0;
 56	int y = 0;
 57	int w = 0;
 58	int h = 0;
 59	FtkColor bg = {0};
 60	FtkBitmap* bitmap = NULL;
 61	WCHAR wfilename[MAX_PATH] = {0};
 62	mbstowcs(wfilename, filename, MAX_PATH);
 63	Bitmap* img = Bitmap::FromFile(wfilename);
 64
 65	return_val_if_fail(img != NULL, NULL);
 66	if(img->GetWidth() == 0 || img->GetHeight() == 0)
 67	{
 68		delete img;
 69
 70		return NULL;
 71	}
 72
 73	w = img->GetWidth();
 74	h = img->GetHeight();
 75
 76	bg.a = 0xff;
 77	bitmap = ftk_bitmap_create(w, h, bg);
 78	Rect r(0, 0, w, h);
 79	BitmapData bitmapData;
 80#ifdef VC6
 81	img->LockBits(r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
 82#else

 83	img->LockBits(&r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
 84#endif
 85	FtkColor* src = (FtkColor*)bitmapData.Scan0;
 86	FtkColor* dst = ftk_bitmap_lock(bitmap);
 87
 88	for(y = 0; y < h; y++)
 89	{
 90		for(x = 0; x < w; x++)
 91		{
 92			*dst = *src;
 93			dst++;
 94			src++;
 95		}
 96	}
 97
 98	img->UnlockBits(&bitmapData);
 99	
100	delete img;
101
102	return bitmap;
103}
104
105static FtkBitmap* ftk_image_win32_decoder_decode(FtkImageDecoder* thiz, const char* filename)
106{
107	return_val_if_fail(ftk_image_win32_decoder_match(thiz, filename) == RET_OK, NULL);
108
109	return load_win32(filename);
110}
111
112static void ftk_image_win32_decoder_destroy(FtkImageDecoder* thiz)
113{
114	if(thiz != NULL)
115	{
116		FTK_ZFREE(thiz, sizeof(thiz));
117	}
118
119	 GdiplusShutdown(gdiplusToken);
120
121	return;
122}
123
124FtkImageDecoder* ftk_image_win32_decoder_create(void)
125{
126	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
127
128	if(thiz != NULL)
129	{
130		GdiplusStartupInput gdiplusStartupInput;
131
132		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
133		thiz->match   = ftk_image_win32_decoder_match;
134		thiz->decode  = ftk_image_win32_decoder_decode;
135		thiz->destroy = ftk_image_win32_decoder_destroy;
136	}
137
138	return thiz;
139}
140