PageRenderTime 421ms CodeModel.GetById 221ms app.highlight 9ms RepoModel.GetById 190ms app.codeStats 0ms

/src/os/gtk/ftk_image_gtk_decoder.c

http://ftk.googlecode.com/
C | 100 lines | 82 code | 17 blank | 1 comment | 26 complexity | 19f79855cd8bbe16d32a3495331fd42e MD5 | raw file
  1
  2#include "ftk_typedef.h"
  3#include "ftk_log.h"
  4#include "ftk_image_gtk_decoder.h"
  5#include <gdk-pixbuf/gdk-pixbuf.h>
  6
  7static Ret ftk_image_gtk_decoder_match(FtkImageDecoder* thiz, const char* filename)
  8{
  9	return_val_if_fail(thiz != NULL && filename != NULL, RET_FAIL);
 10
 11	return (strstr(filename, "bmp") != NULL
 12		|| strstr(filename, ".png") != NULL 
 13		|| strstr(filename, ".jpg") != NULL
 14		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
 15}
 16
 17static FtkBitmap* ftk_image_gtk_decoder_decode(FtkImageDecoder* thiz, const char* filename)
 18{
 19	int x = 0;
 20	int y = 0;
 21	int w = 0;
 22	int h = 0;
 23	FtkColor bg = {0};
 24	FtkBitmap* bitmap = NULL;
 25	FtkColor* src = NULL;
 26	FtkColor* dst = NULL;
 27	int bpp = 0;
 28	GError *error = NULL;
 29	GdkPixbuf* img = NULL;
 30	return_val_if_fail(thiz != NULL && filename != NULL, NULL);
 31
 32	img = gdk_pixbuf_new_from_file(filename, &error);
 33	if(img == NULL)
 34	{
 35		ftk_loge("gdk_pixbuf_new_from_file() \"%s\" failed\n", filename);
 36		return NULL;
 37	}
 38
 39	w = gdk_pixbuf_get_width(img);
 40	h = gdk_pixbuf_get_height(img);
 41	bpp = gdk_pixbuf_get_rowstride(img) / w * 8;
 42
 43	bg.a = 0xff;
 44	bitmap = ftk_bitmap_create(w, h, bg);
 45
 46	src = (FtkColor*)gdk_pixbuf_get_pixels(img);
 47	dst = ftk_bitmap_lock(bitmap);
 48
 49	ftk_logd("ftk_image_gtk_decoder_decode() bpp:%d\n", bpp);
 50
 51	// TODO: bpp
 52
 53	for(y = 0; y < h; y++)
 54	{
 55		for(x = 0; x < w; x++)
 56		{
 57			if(bpp == 32)
 58			{
 59				*dst = *src;
 60				dst++;
 61				src++;
 62			}
 63			else if(bpp == 24)
 64			{
 65				dst->b = src->b;
 66				dst->g = src->g;
 67				dst->r = src->r;
 68				dst->a = 0xff;
 69
 70				dst++;
 71				src = (FtkColor*)((unsigned char*)src + 3);
 72			}
 73		}
 74	}
 75
 76	g_object_unref(img);
 77
 78	return bitmap;
 79}
 80
 81static void ftk_image_gtk_decoder_destroy(FtkImageDecoder* thiz)
 82{
 83	if(thiz != NULL)
 84	{
 85		FTK_ZFREE(thiz, sizeof(FtkImageDecoder));
 86	}
 87}
 88
 89FtkImageDecoder* ftk_image_gtk_decoder_create(void)
 90{
 91	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
 92	if(thiz != NULL)
 93	{
 94		thiz->match = ftk_image_gtk_decoder_match;
 95		thiz->decode = ftk_image_gtk_decoder_decode;
 96		thiz->destroy = ftk_image_gtk_decoder_destroy;
 97	}
 98
 99	return thiz;
100}