PageRenderTime 327ms CodeModel.GetById 80ms app.highlight 13ms RepoModel.GetById 130ms app.codeStats 1ms

/src/canvas/skia/ftk_bitmap_skia.cpp

http://ftk.googlecode.com/
C++ | 157 lines | 99 code | 30 blank | 28 comment | 15 complexity | 7daff10f25422bf8f3abd66dbe104adf MD5 | raw file
  1/*
  2 * File: ftk_bitmap_skia.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   skia bitmap implemntation.
  5 *
  6 * Copyright (c) 2009 - 2011  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 * 2011-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 */
 30
 31#include "ftk_bitmap.h"
 32#include "SkColor.h"
 33#include "SkCanvas.h"
 34#include "ftk_globals.h"
 35#include "SkImageEncoder.h"
 36#include "effects/SkPorterDuff.h"
 37
 38using namespace android;
 39
 40FtkBitmap* ftk_bitmap_create(int w, int h, FtkColor color)
 41{
 42	FtkBitmap* thiz = (FtkBitmap*)FTK_ALLOC(sizeof(FtkBitmap));
 43
 44	if(thiz != NULL)
 45	{
 46		SkBitmap* bitmap = new SkBitmap();
 47
 48		if(bitmap != NULL)
 49		{
 50			thiz->ref = 1;
 51			thiz->data = bitmap;
 52			bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, 0);
 53			bitmap->allocPixels();
 54			bitmap->eraseARGB(color.a, color.r, color.g, color.b);
 55		}
 56		else
 57		{
 58			FTK_FREE(thiz);	
 59		}
 60	}
 61
 62	return thiz;
 63}
 64
 65FtkBitmap* ftk_bitmap_create_with_native(void* bitmap)
 66{
 67	FtkBitmap* thiz = (FtkBitmap*)FTK_ALLOC(sizeof(FtkBitmap));
 68
 69	if(thiz != NULL)
 70	{
 71		thiz->ref = 1;
 72		thiz->data = bitmap;
 73	}
 74
 75	return thiz;
 76}
 77
 78void* ftk_bitmap_get_native(FtkBitmap* thiz)
 79{
 80	return_val_if_fail(thiz != NULL, NULL);
 81
 82	return thiz->data;
 83}
 84
 85int ftk_bitmap_width(FtkBitmap* thiz)
 86{
 87	SkBitmap* bitmap = NULL;
 88	return_val_if_fail(thiz != NULL, 0);
 89
 90	bitmap = (SkBitmap*)thiz->data;
 91
 92	return bitmap->width();
 93}
 94
 95int ftk_bitmap_height(FtkBitmap* thiz)
 96{
 97	SkBitmap* bitmap = NULL;
 98	return_val_if_fail(thiz != NULL, 0);
 99
100	bitmap = (SkBitmap*)thiz->data;
101
102	return bitmap->height();
103}
104
105FtkColor* ftk_bitmap_lock(FtkBitmap* thiz)
106{
107	SkBitmap* bitmap = NULL;
108	return_val_if_fail(thiz != NULL, NULL);
109
110	bitmap = (SkBitmap*)thiz->data;
111
112	return (FtkColor*)bitmap->getPixels();
113}
114
115void    ftk_bitmap_unlock(FtkBitmap* thiz)
116{
117	return;
118}
119
120FtkColor ftk_bitmap_get_pixel(FtkBitmap* thiz, int x, int y)
121{
122	SkColor sc;
123	FtkColor c = {0, 0, 0, 0};	
124	return_val_if_fail(thiz != NULL && x < ftk_bitmap_width(thiz) && y < ftk_bitmap_height(thiz), c);
125	
126	SkBitmap* bitmap = (SkBitmap*)thiz->data;
127#ifdef ANDROID
128	sc = *bitmap->getAddr32(x, y);
129	c.a = SkColorGetA(sc);
130	c.r = SkColorGetR(sc);
131	c.g = SkColorGetG(sc);
132	c.b = SkColorGetB(sc);
133#else
134	sc = bitmap->getColor(x, y);
135	c.a = SkColorGetA(sc);
136	c.r = SkColorGetR(sc);
137	c.g = SkColorGetG(sc);
138	c.b = SkColorGetB(sc);
139
140#endif
141
142	return c;
143}
144
145void ftk_bitmap_destroy(FtkBitmap* thiz)
146{
147	if(thiz != NULL)
148	{
149		SkBitmap* bitmap = (SkBitmap*)thiz->data;
150
151		delete bitmap;
152		FTK_ZFREE(thiz, sizeof(FtkBitmap));
153	}
154
155	return;
156}
157