PageRenderTime 54ms CodeModel.GetById 46ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/os/qt/ftk_image_qt_decoder.cpp

http://ftk.googlecode.com/
C++ | 86 lines | 70 code | 15 blank | 1 comment | 20 complexity | f57860192a9966df08d32bfa72ce5a04 MD5 | raw file
 1
 2extern "C" {
 3#include "ftk_typedef.h"
 4}
 5#include "ftk_log.h"
 6#include "ftk_image_qt_decoder.h"
 7#include <QtGui/QImage>
 8
 9static Ret ftk_image_qt_decoder_match(FtkImageDecoder* thiz, const char* filename)
10{
11	return_val_if_fail(thiz != NULL && filename != NULL, RET_FAIL);
12
13	return (strstr(filename, "bmp") != NULL
14		|| strstr(filename, ".png") != NULL 
15		|| strstr(filename, ".jpg") != NULL
16		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
17}
18
19static FtkBitmap* ftk_image_qt_decoder_decode(FtkImageDecoder* thiz, const char* filename)
20{
21	int x = 0;
22	int y = 0;
23	int w = 0;
24	int h = 0;
25	FtkColor bg = {0};
26	FtkBitmap* bitmap = NULL;
27	FtkColor* src = NULL;
28	FtkColor* dst = NULL;
29	QImage* img = NULL;
30	return_val_if_fail(thiz != NULL && filename != NULL, NULL);
31
32	img = new QImage();
33	if(!img->load(QString::fromUtf8(filename)))
34	{
35		delete img;
36		return NULL;
37	}
38
39	w = img->width();
40	h = img->height();
41	bg.a = 0xff;
42	bitmap = ftk_bitmap_create(w, h, bg);
43
44	QImage::Format format = img->format();
45	const unsigned char* bits = img->bits();//img->constBits();
46
47	src = (FtkColor*)bits;
48	dst = ftk_bitmap_lock(bitmap);
49
50	// TODO: format
51
52	for(y = 0; y < h; y++)
53	{
54		for(x = 0; x < w; x++)
55		{
56			*dst = *src;
57			dst++;
58			src++;
59		}
60	}
61
62	delete img;
63
64	return bitmap;
65}
66
67static void ftk_image_qt_decoder_destroy(FtkImageDecoder* thiz)
68{
69	if(thiz != NULL)
70	{
71		FTK_ZFREE(thiz, sizeof(FtkImageDecoder));
72	}
73}
74
75FtkImageDecoder* ftk_image_qt_decoder_create(void)
76{
77	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
78	if(thiz != NULL)
79	{
80		thiz->match = ftk_image_qt_decoder_match;
81		thiz->decode = ftk_image_qt_decoder_decode;
82		thiz->destroy = ftk_image_qt_decoder_destroy;
83	}
84
85	return thiz;
86}