PageRenderTime 140ms CodeModel.GetById 129ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/canvas/win32/ftk_bitmap_win32.cpp

http://ftk.googlecode.com/
C++ | 160 lines | 126 code | 34 blank | 0 comment | 32 complexity | 0d1a07e152543a2fd18043c8125cdd5a MD5 | raw file
  1#include "ftk_bitmap.h"
  2#include "ftk_globals.h"
  3
  4#ifdef VC6
  5#define ULONG_PTR unsigned long*
  6#endif 
  7
  8#include <windows.h>
  9#include <gdiplus.h>
 10#include "ftk_win32.h"
 11
 12FtkBitmap* ftk_bitmap_create(int w, int h, FtkColor color)
 13{
 14	Bitmap* bitmap = NULL;
 15	FtkColor* bits = NULL;
 16	FtkBitmap* thiz = (FtkBitmap*)FTK_ZALLOC(sizeof(FtkBitmap));
 17
 18	if(thiz != NULL)
 19	{
 20		thiz->ref = 1;
 21
 22		bits = FTK_ZALLOC(w * h * 4);
 23		if(bits != NULL)
 24		{
 25			bitmap = new Bitmap(w, h, w * 4, PixelFormat32bppARGB, (char*)bits);
 26			if(bitmap != NULL)
 27			{
 28				thiz->data = bitmap;
 29				thiz->data1 = bits;
 30			}
 31			else
 32			{
 33				FTK_FREE(bits);
 34				FTK_FREE(thiz);
 35			}
 36		}
 37		else
 38		{
 39			FTK_FREE(thiz);
 40		}
 41	}
 42
 43	return thiz;
 44}
 45
 46FtkBitmap* ftk_bitmap_create_with_native(void* bitmap)
 47{
 48	FtkBitmap* thiz = (FtkBitmap*)FTK_ZALLOC(sizeof(FtkBitmap));
 49
 50	if(thiz != NULL)
 51	{
 52		thiz->ref = 1;
 53		thiz->data = bitmap;
 54	}
 55
 56	return thiz;
 57}
 58
 59void* ftk_bitmap_get_native(FtkBitmap* thiz)
 60{
 61	return_val_if_fail(thiz != NULL, NULL);
 62
 63	return thiz->data;
 64}
 65
 66int ftk_bitmap_width(FtkBitmap* thiz)
 67{
 68	Bitmap* bitmap = NULL;
 69	return_val_if_fail(thiz != NULL && thiz->data != NULL, 0);
 70
 71	bitmap = (Bitmap*)thiz->data;
 72
 73	return bitmap->GetWidth();
 74}
 75
 76int ftk_bitmap_height(FtkBitmap* thiz)
 77{
 78	Bitmap* bitmap = NULL;
 79	return_val_if_fail(thiz != NULL && thiz->data != NULL, 0);
 80
 81	bitmap = (Bitmap*)thiz->data;
 82
 83	return bitmap->GetHeight();
 84}
 85
 86FtkColor* ftk_bitmap_lock(FtkBitmap* thiz)
 87{
 88	Rect r(0, 0, w, h);
 89	Bitmap* bitmap = NULL;
 90	BitmapData *bitmapData = new BitmapData();
 91	return_val_if_fail(thiz != NULL && thiz->data != NULL, 0);
 92
 93	bitmap = (Bitmap*)thiz->data;
 94
 95#ifdef VC6
 96	bitmap->LockBits(r, ImageLockModeRead, PixelFormat32bppARGB, bitmapData);
 97#else
 98	bitmap->LockBits(&r, ImageLockModeRead, PixelFormat32bppARGB, bitmapData);
 99#endif
100
101	thiz->data2 = bitmapData;
102
103	return (FtkColor*)bitmapData->Scan0;
104}
105
106void    ftk_bitmap_unlock(FtkBitmap* thiz)
107{
108	Bitmap* bitmap = NULL;
109	BitmapData *bitmapData = NULL;
110	return_val_if_fail(thiz != NULL && thiz->data != NULL, 0);
111
112	bitmap = (Bitmap*)thiz->data;
113	bitmapData = (BitmapData)thiz->data2;
114	bitmap->UnlockBits(bitmapData);
115	delete bitmapData;
116	thiz->data2 = NULL;
117
118	return;
119}
120
121FtkColor ftk_bitmap_get_pixel(FtkBitmap* thiz, int x, int y)
122{
123	FtkColor c;
124	Color cc = {0};
125	Bitmap* bitmap = NULL;
126	return_val_if_fail(thiz != NULL && thiz->data != NULL
127		&& x < ftk_bitmap_width(thiz) && y < ftk_bitmap_height(thiz), c);
128	
129	bitmap = (Bitmap*)thiz->data;
130	bitmap->GetPixel(x, y, &cc);
131
132	c.a = cc.GetA();
133	c.r = cc.GetR();
134	c.g = cc.GetG();
135	c.b = cc.GetB();
136
137	return c;
138}
139
140void ftk_bitmap_destroy(FtkBitmap* thiz)
141{
142	if(thiz != NULL)
143	{
144		Bitmap* bitmap = (Bitmap*)thiz->data;
145		if(bitmap != NULL)
146		{
147			delete bitmap;
148		}
149
150		if(thiz->data1 != NULL)
151		{
152			FTK_FREE(thiz->data1);
153		}
154
155		FTK_ZFREE(thiz, sizeof(FtkBitmap));
156	}
157
158	return;
159}
160