1 /*
2  *  Copyright (C) 2005-2008  Maarten de Boer <maarten@resorama.com>
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License
6  *  as published by the Free Software Foundation; either version 2
7  *  of the License, or (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  *  02110-1301, USA.
18  *
19  */
20 
21 #ifndef __RGBA__
22 #define __RGBA__
23 
24 class Data2D
25 {
26 public:
Width() const27 	int Width() const { return width; }
Height() const28 	int Height() const { return height; }
Data() const29 	unsigned char* Data() const { return data; }
30 protected:
31 	int width,height,elemsize;
32 	unsigned char* data;
Data2D(int e)33 	Data2D(int e)
34 			:width(0),height(0),elemsize(e),data(0)
35 	{
36 	}
~Data2D()37 	~Data2D()
38 	{
39 		if (data) delete [] data;
40 	}
Data2D(int w,int h,int e)41 	Data2D(int w,int h,int e)
42 			:width(0),height(0),elemsize(e),data(0)
43 	{
44 
45 		Alloc(w,h);
46 	}
47 public:
Alloc(int w,int h)48 	void Alloc(int w,int h)
49 	{
50 		if (w==width && h==height) return;
51 		if (data) throw "Data2D already allocated with different size";
52 		width = w;
53 		height = h;
54 		data = new unsigned char[width*height*elemsize];
55 	}
56 };
57 
58 class RGB:public Data2D
59 {
60 public:
RGB()61 	RGB():Data2D(3) { };
RGB(int w,int h)62 	RGB(int w,int h):Data2D(w,h,3) { };
63 };
64 
65 class RGBA:public Data2D
66 {
67 public:
RGBA()68 	RGBA():Data2D(4) { };
RGBA(int w,int h)69 	RGBA(int w,int h):Data2D(w,h,4) { };
70 
Paste(const RGBA & src,int x,int y)71 	void Paste(const RGBA& src,int x,int y)
72 	{
73 		unsigned char* ptrB = src.data;
74 
75 		for (int j = 0; j < src.height; j++)
76 		{
77 			unsigned char* ptrA = data + (y*width+x)*4;
78 			for (int i = 0;i < src.width; i++)
79 			{
80 				*ptrA++ = *ptrB++;
81 				*ptrA++ = *ptrB++;
82 				*ptrA++ = *ptrB++;
83 				*ptrA++ = *ptrB++;
84 			}
85 			y++;
86 		}
87 	}
Paste(const RGB & src,int x,int y)88 	void Paste(const RGB& src,int x,int y)
89 	{
90 		unsigned char* ptrB = src.Data();
91 
92 		for (int j = 0; j < src.Height(); j++)
93 		{
94 			unsigned char* ptrA = data + (y*width+x)*4;
95 			for (int i = 0;i < src.Width(); i++)
96 			{
97 				*ptrA++ = *ptrB++;
98 				*ptrA++ = *ptrB++;
99 				*ptrA++ = *ptrB++;
100 				*ptrA++ = 255;
101 			}
102 			y++;
103 		}
104 	}
105 };
106 
107 #endif
108 
109 
110