PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/canvas/default/ftk_bitmap_factory.c

http://ftk.googlecode.com/
C | 146 lines | 89 code | 28 blank | 29 comment | 22 complexity | f08c516d476a583fb8ae0d24cccd15c3 MD5 | raw file
  1/*
  2 * File: ftk_bitmap_factory.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   bitmap factory.
  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 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_allocator.h"
 34#include "ftk_bitmap_factory.h"
 35
 36#ifdef HAS_BMP
 37#include "ftk_image_bmp_decoder.h"
 38#endif
 39
 40#ifdef HAS_JPEG
 41#include "ftk_image_jpeg_decoder.h"
 42#endif
 43
 44#ifdef HAS_PNG
 45#include "ftk_image_png_decoder.h"
 46#endif
 47
 48#if defined(WIN32) && !defined(WINCE)
 49#include "ftk_image_win32_decoder.h"
 50#endif
 51
 52#ifdef IPHONE
 53#include "ftk_image_iphone_decoder.h"
 54#endif
 55
 56#if defined(ANDROID) && defined(ANDROID_NDK)
 57#include "ftk_image_android_decoder.h"
 58#endif
 59
 60struct _FtkBitmapFactory
 61{
 62	int nr;
 63	FtkImageDecoder* decoders[FTK_MAX_IMAGE_DECODERS];
 64};
 65
 66FtkBitmapFactory* ftk_bitmap_factory_create(void)
 67{
 68	FtkBitmapFactory* thiz = (FtkBitmapFactory*)FTK_ZALLOC(sizeof(FtkBitmapFactory));
 69
 70	if(thiz != NULL)
 71	{
 72#ifdef HAS_BMP
 73		ftk_bitmap_factory_add_decoder(thiz, ftk_image_bmp_decoder_create());	
 74#endif
 75
 76#ifdef HAS_JPEG
 77		ftk_bitmap_factory_add_decoder(thiz, ftk_image_jpeg_decoder_create());	
 78#endif
 79
 80#ifdef HAS_PNG
 81		ftk_bitmap_factory_add_decoder(thiz, ftk_image_png_decoder_create());
 82#endif
 83
 84#if defined(WIN32) && !defined(WINCE)
 85		ftk_bitmap_factory_add_decoder(thiz, ftk_image_win32_decoder_create());
 86#endif
 87
 88#ifdef IPHONE
 89		ftk_bitmap_factory_add_decoder(thiz, ftk_image_iphone_decoder_create());
 90#endif
 91
 92#if defined(ANDROID) && defined(ANDROID_NDK)
 93		ftk_bitmap_factory_add_decoder(thiz, ftk_image_android_decoder_create());
 94#endif
 95	}
 96
 97	return thiz;
 98}
 99
100FtkBitmap* ftk_bitmap_factory_load(FtkBitmapFactory* thiz, const char* filename)
101{
102	int i = 0;
103	return_val_if_fail(thiz != NULL && filename != NULL, NULL);
104
105	for(i = 0; i < thiz->nr; i++)
106	{
107		if(thiz->decoders[i] != NULL && ftk_image_decoder_match(thiz->decoders[i], filename) == RET_OK)
108		{
109			return ftk_image_decoder_decode(thiz->decoders[i], filename);
110		}
111	}
112
113	ftk_loge("%s:%d %s is not supported format.\n", __func__, __LINE__, filename);
114
115	return NULL;
116}
117
118Ret        ftk_bitmap_factory_add_decoder(FtkBitmapFactory* thiz, FtkImageDecoder* decoder)
119{
120	return_val_if_fail(thiz != NULL && decoder != NULL, RET_FAIL);
121	return_val_if_fail(thiz->nr < FTK_MAX_IMAGE_DECODERS, RET_FAIL);
122
123	thiz->decoders[thiz->nr++] = decoder;
124
125	return RET_OK;
126}
127
128void       ftk_bitmap_factory_destroy(FtkBitmapFactory* thiz)
129{
130	int i = 0;
131	if(thiz != NULL)
132	{
133		for(i = 0; i < thiz->nr; i++)
134		{
135			if(thiz->decoders[i] != NULL)
136			{
137				ftk_image_decoder_destroy(thiz->decoders[i]);
138			}
139		}
140
141		FTK_ZFREE(thiz, sizeof(FtkBitmapFactory));
142	}
143
144	return;
145}
146