File: pixel_buffer.cpp

package info (click to toggle)
clanlib 1.0~svn3827-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 24,696 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,742; ansic: 463; perl: 424; php: 247; sh: 53
file content (350 lines) | stat: -rw-r--r-- 9,543 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "Display/display_precomp.h"
#include "API/Display/pixel_buffer.h"
#include "API/Core/IOData/datatypes.h"
#include "pixel_buffer_generic.h"
#include "pixel_buffer_memory.h"
#include "API/Core/System/error.h"

/////////////////////////////////////////////////////////////////////////////
// CL_PixelBuffer construction:

CL_PixelBuffer::CL_PixelBuffer(CL_PixelBuffer_Generic *impl)
: impl(impl)
{
	if (impl) impl->add_ref();
}

CL_PixelBuffer::CL_PixelBuffer(int width, int height, int pitch, const CL_PixelFormat &format, void *data)
: impl(new CL_PixelBuffer_Memory(width, height, pitch, format, data))
{
	impl->add_ref();
}

CL_PixelBuffer::CL_PixelBuffer(int width, int height, int pitch, const CL_PixelFormat &format, const CL_Palette &palette, void *data)
: impl(new CL_PixelBuffer_Memory(width, height, pitch, format, palette, data))
{
	impl->add_ref();
}

CL_PixelBuffer::CL_PixelBuffer()
: impl(0)
{
}

CL_PixelBuffer::CL_PixelBuffer(const CL_PixelBuffer &copy)
: impl(copy.impl)
{
	if (impl) impl->add_ref();
}

CL_PixelBuffer::~CL_PixelBuffer()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_PixelBuffer attributes:

const CL_PixelFormat &CL_PixelBuffer::get_format() const
{
	return impl->format;
}

const CL_Palette &CL_PixelBuffer::get_palette() const
{
	return impl->palette;
}

int CL_PixelBuffer::get_width() const
{
	return impl->width;
}

int CL_PixelBuffer::get_height() const
{
	return impl->height;
}

unsigned int CL_PixelBuffer::get_pitch() const
{
	return impl->pitch;
}

void *CL_PixelBuffer::get_data()
{
	return impl->get_data();
}

CL_Color CL_PixelBuffer::get_pixel(int x, int y)
{
	CL_Color color;

	lock();
	
	cl_uint8* buf = static_cast<cl_uint8*>(impl->get_data());
	CL_PixelFormat format = get_format();

	if (format.get_type() == pixelformat_index)
	{
		color = impl->palette[int(buf[y*impl->pitch + x])];
	}
	else if (format.get_type() == pixelformat_rgba)
	{
		int depth = format.get_depth ();

		cl_uint8 *pos = &buf[y * impl->pitch + x * ((depth + 7)/8)];

		if (8 == depth)
		{
			cl_uint8 value = *pos;

			color = CL_Color ((value & format.get_red_mask ()) >> format.get_mask_shift (format.get_red_mask ()),
							  (value & format.get_green_mask ()) >> format.get_mask_shift (format.get_green_mask ()),
							  (value & format.get_blue_mask ()) >> format.get_mask_shift (format.get_blue_mask ()),
							  (value & format.get_alpha_mask ()) >> format.get_mask_shift (format.get_alpha_mask ()));
		}
		else if (16 == depth)
		{
			cl_uint16 value = *((cl_uint16*)pos);

			color = CL_Color ((value & format.get_red_mask ()) >> format.get_mask_shift (format.get_red_mask ()),
							  (value & format.get_green_mask ()) >> format.get_mask_shift (format.get_green_mask ()),
							  (value & format.get_blue_mask ()) >> format.get_mask_shift (format.get_blue_mask ()),
							  (value & format.get_alpha_mask ()) >> format.get_mask_shift (format.get_alpha_mask ()));
		}
		else if (24 == depth)
		{
			cl_uint32 value = ((*pos) << 16) +
				              ((*(pos + 1)) << 8) +
							  *(pos + 2);

			color = CL_Color ((value & format.get_red_mask ()) >> format.get_mask_shift (format.get_red_mask ()),
							  (value & format.get_green_mask ()) >> format.get_mask_shift (format.get_green_mask ()),
							  (value & format.get_blue_mask ()) >> format.get_mask_shift (format.get_blue_mask ()),
							  (value & format.get_alpha_mask ()) >> format.get_mask_shift (format.get_alpha_mask ()));
		}
		else if (32 == depth)
		{
			cl_uint32 value = *((cl_uint32*)pos);

			color = CL_Color ((value & format.get_red_mask ()) >> format.get_mask_shift (format.get_red_mask ()),
							  (value & format.get_green_mask ()) >> format.get_mask_shift (format.get_green_mask ()),
							  (value & format.get_blue_mask ()) >> format.get_mask_shift (format.get_blue_mask ()),
							  (value & format.get_alpha_mask ()) >> format.get_mask_shift (format.get_alpha_mask ()));
		}
	}
	unlock();

	return color;
}

/////////////////////////////////////////////////////////////////////////////
// CL_PixelBuffer operations:

CL_PixelBuffer &CL_PixelBuffer::operator =(const CL_PixelBuffer &copy)
{
	if (impl) impl->release_ref();
	impl = copy.impl;
	if (impl) impl->add_ref();
	return *this;
}

CL_PixelBuffer::operator bool() const
{
  return (impl != 0);
}

void CL_PixelBuffer::lock()
{
	impl->lock_count++;
	if (impl->lock_count == 1) impl->perform_lock();
}

void CL_PixelBuffer::unlock()
{
	impl->lock_count--;
	if (impl->lock_count == 0) impl->perform_unlock();
}

void CL_PixelBuffer::convert(CL_PixelBuffer target)
{
	target.lock();

	convert(
		target.get_data(),
		target.get_format(),
		target.get_pitch(),
		CL_Rect(
			0, 0,
			target.get_width(), target.get_height()));

	target.unlock();
}

void CL_PixelBuffer::convert(
	void *buffer,
	const CL_PixelFormat &format,
	int dest_pitch,
	const CL_Rect &dest_rect,
	const CL_Rect &src_rect)
{
	lock();

	bool null_src = false;
	if (src_rect.left == 0 && src_rect.top == 0 && src_rect.right == 0 && src_rect.bottom == 0)
	{
		null_src = true;
	}

	if (format.get_type() == pixelformat_index)
	{
		throw CL_Error("Converting to indexed pixelformats not supported.");
	}

	if (null_src == false && dest_rect.get_size() != src_rect.get_size())
	{
		throw CL_Error("Source and destination rects must have same size. Scaled converting not supported.");
	}

	char *src_data = (char *) get_data();
	char *dest_data = (char *) buffer;
	if (null_src == false) src_data += src_rect.top*get_pitch() + src_rect.left*((get_format().get_depth()+7)/8);
	dest_data += dest_rect.top*dest_pitch + dest_rect.left*((format.get_depth()+7)/8);

	if (get_format().get_type() == pixelformat_index)
	{
		CL_PixelBuffer_Generic::convert_pal(
			src_data,
			get_format(),
			get_pitch(),
			get_palette(),
			dest_data,
			format,
			dest_pitch,
			dest_rect.get_size());
	}
	else
	{
		CL_PixelBuffer_Generic::convert(
			src_data,
			get_format(),
			get_pitch(),
			dest_data,
			format,
			dest_pitch,
			dest_rect.get_size());
	}

	unlock();
}

void CL_PixelBuffer::convert_line(void *buffer, const CL_PixelFormat &format, int y)
{
	convert(
		buffer,
		format,
		(format.get_depth()+7)/8*get_width(),
		CL_Rect(0, 0, get_width(), 1),
		CL_Rect(0, y, get_width(), 1));
}

CL_PixelBuffer CL_PixelBuffer::to_format(const CL_PixelFormat &format)
{
	lock();
	CL_PixelBuffer result(get_width(), get_height(), get_width()*((format.get_depth()+7)/8), format);
	convert(result);
	unlock();
	return result;
}

void CL_PixelBuffer::set_colorkey(bool enabled, unsigned int colorkey)
{
	impl->format.enable_colorkey(enabled);
	impl->format.set_colorkey(colorkey);
}

void CL_PixelBuffer::draw_pixel(int x, int y, const CL_Color &color)
{
	lock();

	cl_uint8* buf = static_cast<cl_uint8*>(impl->get_data());
	CL_PixelFormat format = get_format();

	if (format.get_type() == pixelformat_index)
	{
		throw CL_Error("Direct settings of CL_Colors pixels in paletted mode is not supported.");
	}
	else if (format.get_type() == pixelformat_rgba)
	{
		int depth = format.get_depth ();
		int bytes_per_pixel = (depth + 7)/8;
		cl_uint8 *pos = &buf[y * impl->pitch + x * bytes_per_pixel];

		int red_shift = format.get_mask_shift(format.get_red_mask()),
			green_shift = format.get_mask_shift(format.get_green_mask()),
			blue_shift = format.get_mask_shift(format.get_blue_mask()),
			alpha_shift = format.get_mask_shift(format.get_alpha_mask());

		switch (bytes_per_pixel)
		{
		case 4:
		case 3:
			{
				cl_uint32 c = (color.get_red() << red_shift) | (color.get_green() << green_shift) | (color.get_blue() << blue_shift) | (color.get_alpha() << alpha_shift);
				memcpy (pos, &c, bytes_per_pixel);
			}
			break;

		case 2:
			{
				cl_uint16 c = (color.get_red() << red_shift) | (color.get_green() << green_shift) | (color.get_blue() << blue_shift) | (color.get_alpha() << alpha_shift);
				memcpy (pos, &c, bytes_per_pixel);
			}
			break;

		case 1:
			{
				cl_uint8 c = (color.get_red() << red_shift) | (color.get_green() << green_shift) | (color.get_blue() << blue_shift) | (color.get_alpha() << alpha_shift);
				*pos = c;
			}
			break;

		default:
			throw CL_Error("Unsuported pixel format depth for draw_pixel.");
		}

	}
	unlock();
}

/////////////////////////////////////////////////////////////////////////////
// CL_PixelBuffer implementation: