sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description: Image implementation
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
/*!
|
sl@0
|
20 |
* \internal
|
sl@0
|
21 |
* \file
|
sl@0
|
22 |
* \brief Image implementation
|
sl@0
|
23 |
*
|
sl@0
|
24 |
*/
|
sl@0
|
25 |
|
sl@0
|
26 |
#ifndef M3G_CORE_INCLUDE
|
sl@0
|
27 |
# error included by m3g_core.c; do not compile separately.
|
sl@0
|
28 |
#endif
|
sl@0
|
29 |
|
sl@0
|
30 |
#include "m3g_image.h"
|
sl@0
|
31 |
#include "m3g_texture.h"
|
sl@0
|
32 |
|
sl@0
|
33 |
/* Declare prototypes for some of the helper functions called from the
|
sl@0
|
34 |
* platform-dependent code included below */
|
sl@0
|
35 |
|
sl@0
|
36 |
static M3Gint m3gBytesPerPixel(M3GPixelFormat format);
|
sl@0
|
37 |
static M3Gint m3gGetNumMipmapLevels(M3Gint w, M3Gint h);
|
sl@0
|
38 |
static void m3gDownsample(M3GPixelFormat format, const M3Gubyte *srcPixels, M3Gint *pw, M3Gint *ph, M3Gubyte *dstPixels);
|
sl@0
|
39 |
static void m3gFreeImageData(Image *img);
|
sl@0
|
40 |
|
sl@0
|
41 |
/* Include platform-dependent functionality */
|
sl@0
|
42 |
#include "m3g_image.inl"
|
sl@0
|
43 |
|
sl@0
|
44 |
/* Size of the buffer used in pixel format conversions; this should be
|
sl@0
|
45 |
* an even number */
|
sl@0
|
46 |
#define SPAN_BUFFER_SIZE 32
|
sl@0
|
47 |
|
sl@0
|
48 |
M3G_CT_ASSERT((SPAN_BUFFER_SIZE & 1) == 0);
|
sl@0
|
49 |
|
sl@0
|
50 |
/*----------------------------------------------------------------------
|
sl@0
|
51 |
* Private functions
|
sl@0
|
52 |
*--------------------------------------------------------------------*/
|
sl@0
|
53 |
|
sl@0
|
54 |
/*!
|
sl@0
|
55 |
* \internal
|
sl@0
|
56 |
* \brief Destroys this Image object.
|
sl@0
|
57 |
*
|
sl@0
|
58 |
* \param obj Image object
|
sl@0
|
59 |
*/
|
sl@0
|
60 |
static void m3gDestroyImage(Object *obj)
|
sl@0
|
61 |
{
|
sl@0
|
62 |
Image *image = (Image*)obj;
|
sl@0
|
63 |
Interface *m3g = M3G_INTERFACE(image);
|
sl@0
|
64 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
65 |
|
sl@0
|
66 |
if (!image->copyOf) {
|
sl@0
|
67 |
m3gFreeObject(m3g, image->data);
|
sl@0
|
68 |
m3gFreeObject(m3g, image->mipData);
|
sl@0
|
69 |
}
|
sl@0
|
70 |
M3G_ASSIGN_REF(image->copyOf, NULL);
|
sl@0
|
71 |
|
sl@0
|
72 |
if (image->powerOfTwo != image) {
|
sl@0
|
73 |
M3G_ASSIGN_REF(image->powerOfTwo, NULL);
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
# if !defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
77 |
if (image->texObject) {
|
sl@0
|
78 |
m3gDeleteGLTextures(m3g, 1, &image->texObject);
|
sl@0
|
79 |
}
|
sl@0
|
80 |
if (image->large != NULL) {
|
sl@0
|
81 |
m3gDestroyLargeImage(image);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
# endif
|
sl@0
|
84 |
|
sl@0
|
85 |
m3gDestroyObject(obj);
|
sl@0
|
86 |
}
|
sl@0
|
87 |
|
sl@0
|
88 |
/*--------------------------------------------------------------------*/
|
sl@0
|
89 |
|
sl@0
|
90 |
#define RED(argb) (((argb) >> 16) & 0xFFu)
|
sl@0
|
91 |
#define GREEN(argb) (((argb) >> 8) & 0xFFu)
|
sl@0
|
92 |
#define BLUE(argb) ( (argb) & 0xFFu)
|
sl@0
|
93 |
#define ALPHA(argb) (((argb) >> 24) & 0xFFu)
|
sl@0
|
94 |
|
sl@0
|
95 |
#define RGBSUM(argb) (0x4CB2u * RED(argb) + \
|
sl@0
|
96 |
0x9691u * GREEN(argb) + \
|
sl@0
|
97 |
0x1D3Eu * BLUE(argb))
|
sl@0
|
98 |
|
sl@0
|
99 |
/*!
|
sl@0
|
100 |
* \internal \brief ARGB -> A
|
sl@0
|
101 |
*/
|
sl@0
|
102 |
static void convertARGBToA8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
while (count--) {
|
sl@0
|
105 |
*dst++ = (M3Gubyte) ALPHA(*src++);
|
sl@0
|
106 |
}
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
/*!
|
sl@0
|
110 |
* \internal \brief ARGB -> L
|
sl@0
|
111 |
*/
|
sl@0
|
112 |
static void convertARGBToL8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
while (count--) {
|
sl@0
|
115 |
M3Guint argb = *src++;
|
sl@0
|
116 |
M3Guint sum = RGBSUM(argb);
|
sl@0
|
117 |
*dst++ = (M3Gubyte)(sum >> 16);
|
sl@0
|
118 |
}
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
/*!
|
sl@0
|
122 |
* \internal \brief ARGB -> LA */
|
sl@0
|
123 |
static void convertARGBToLA4(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
while (count--) {
|
sl@0
|
126 |
M3Guint argb = *src++;
|
sl@0
|
127 |
M3Guint sum = RGBSUM(argb);
|
sl@0
|
128 |
*dst++ = (M3Gubyte)(((sum >> 16) & 0xF0) | ((argb >> 28) & 0x0F));
|
sl@0
|
129 |
}
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
/*!
|
sl@0
|
133 |
* \internal \brief ARGB -> LA8
|
sl@0
|
134 |
*/
|
sl@0
|
135 |
static void convertARGBToLA8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
136 |
{
|
sl@0
|
137 |
while (count--) {
|
sl@0
|
138 |
M3Guint argb = *src++;
|
sl@0
|
139 |
M3Guint sum = RGBSUM(argb);
|
sl@0
|
140 |
*dst++ = (M3Gubyte)(sum >> 16); /* L */
|
sl@0
|
141 |
*dst++ = (M3Gubyte) ALPHA(argb);
|
sl@0
|
142 |
}
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
/*!
|
sl@0
|
146 |
* \internal \brief ARGB -> RGB
|
sl@0
|
147 |
*/
|
sl@0
|
148 |
static void convertARGBToRGB8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
149 |
{
|
sl@0
|
150 |
while (count--) {
|
sl@0
|
151 |
M3Guint argb = *src++;
|
sl@0
|
152 |
*dst++ = (M3Gubyte) RED(argb);
|
sl@0
|
153 |
*dst++ = (M3Gubyte) GREEN(argb);
|
sl@0
|
154 |
*dst++ = (M3Gubyte) BLUE(argb);
|
sl@0
|
155 |
}
|
sl@0
|
156 |
}
|
sl@0
|
157 |
|
sl@0
|
158 |
/*!
|
sl@0
|
159 |
* \internal \brief ARGB -> RGB565
|
sl@0
|
160 |
*/
|
sl@0
|
161 |
static void convertARGBToRGB565(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
162 |
{
|
sl@0
|
163 |
while (count--) {
|
sl@0
|
164 |
M3Guint argb = *src++;
|
sl@0
|
165 |
*(M3Gushort*)dst = (M3Gushort)(((argb >> 8) & 0xF800u)|
|
sl@0
|
166 |
((argb >> 5) & 0x07E0u)|
|
sl@0
|
167 |
((argb >> 3) & 0x001Fu));
|
sl@0
|
168 |
dst += 2;
|
sl@0
|
169 |
}
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
/*!
|
sl@0
|
173 |
* \internal \brief ARGB -> RGBA
|
sl@0
|
174 |
*/
|
sl@0
|
175 |
static void convertARGBToRGBA8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
while (count--) {
|
sl@0
|
178 |
M3Guint argb = *src++;
|
sl@0
|
179 |
*dst++ = (M3Gubyte) RED(argb);
|
sl@0
|
180 |
*dst++ = (M3Gubyte) GREEN(argb);
|
sl@0
|
181 |
*dst++ = (M3Gubyte) BLUE(argb);
|
sl@0
|
182 |
*dst++ = (M3Gubyte) ALPHA(argb);
|
sl@0
|
183 |
}
|
sl@0
|
184 |
}
|
sl@0
|
185 |
|
sl@0
|
186 |
/*!
|
sl@0
|
187 |
* \internal \brief ARGB -> BGRA
|
sl@0
|
188 |
*/
|
sl@0
|
189 |
static void convertARGBToBGRA8(const M3Guint *src, M3Gsizei count, M3Gubyte *dst)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
while (count--) {
|
sl@0
|
192 |
M3Guint argb = *src++;
|
sl@0
|
193 |
*dst++ = (M3Gubyte) BLUE(argb);
|
sl@0
|
194 |
*dst++ = (M3Gubyte) GREEN(argb);
|
sl@0
|
195 |
*dst++ = (M3Gubyte) RED(argb);
|
sl@0
|
196 |
*dst++ = (M3Gubyte) ALPHA(argb);
|
sl@0
|
197 |
}
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
#undef RED
|
sl@0
|
201 |
#undef GREEN
|
sl@0
|
202 |
#undef BLUE
|
sl@0
|
203 |
#undef ALPHA
|
sl@0
|
204 |
|
sl@0
|
205 |
/*!
|
sl@0
|
206 |
* \internal
|
sl@0
|
207 |
* \brief Converts a span of ARGB pixels to another format
|
sl@0
|
208 |
*
|
sl@0
|
209 |
* \param src source pixels
|
sl@0
|
210 |
* \param count pixel count
|
sl@0
|
211 |
* \param dstFormat destination format
|
sl@0
|
212 |
* \param dst destination pixels
|
sl@0
|
213 |
*/
|
sl@0
|
214 |
static void convertFromARGB(const M3Guint *src,
|
sl@0
|
215 |
M3Gsizei count,
|
sl@0
|
216 |
M3GPixelFormat dstFormat,
|
sl@0
|
217 |
M3Gubyte *dst)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
switch (dstFormat) {
|
sl@0
|
220 |
case M3G_L8:
|
sl@0
|
221 |
convertARGBToL8(src, count, dst);
|
sl@0
|
222 |
break;
|
sl@0
|
223 |
case M3G_A8:
|
sl@0
|
224 |
convertARGBToA8(src, count, dst);
|
sl@0
|
225 |
break;
|
sl@0
|
226 |
case M3G_LA4:
|
sl@0
|
227 |
convertARGBToLA4(src, count, dst);
|
sl@0
|
228 |
break;
|
sl@0
|
229 |
case M3G_LA8:
|
sl@0
|
230 |
convertARGBToLA8(src, count, dst);
|
sl@0
|
231 |
break;
|
sl@0
|
232 |
case M3G_RGB8:
|
sl@0
|
233 |
convertARGBToRGB8(src, count, dst);
|
sl@0
|
234 |
break;
|
sl@0
|
235 |
case M3G_RGB565:
|
sl@0
|
236 |
convertARGBToRGB565(src, count, dst);
|
sl@0
|
237 |
break;
|
sl@0
|
238 |
case M3G_RGBA8:
|
sl@0
|
239 |
case M3G_RGB8_32:
|
sl@0
|
240 |
convertARGBToRGBA8(src, count, dst);
|
sl@0
|
241 |
break;
|
sl@0
|
242 |
case M3G_BGRA8:
|
sl@0
|
243 |
case M3G_BGR8_32:
|
sl@0
|
244 |
convertARGBToBGRA8(src, count, dst);
|
sl@0
|
245 |
break;
|
sl@0
|
246 |
default:
|
sl@0
|
247 |
M3G_ASSERT(M3G_FALSE); /* conversion not supported */
|
sl@0
|
248 |
}
|
sl@0
|
249 |
}
|
sl@0
|
250 |
|
sl@0
|
251 |
/*--------------------------------------------------------------------*/
|
sl@0
|
252 |
|
sl@0
|
253 |
/*!
|
sl@0
|
254 |
* \internal \brief A8 -> ARGB
|
sl@0
|
255 |
*/
|
sl@0
|
256 |
static void convertA8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
257 |
{
|
sl@0
|
258 |
while (count--) {
|
sl@0
|
259 |
M3Guint argb = M3G_RGB_MASK;
|
sl@0
|
260 |
argb |= ((M3Guint) *src++) << 24;
|
sl@0
|
261 |
*dst++ = argb;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
}
|
sl@0
|
264 |
|
sl@0
|
265 |
/*!
|
sl@0
|
266 |
* \internal \brief L8 -> ARGB
|
sl@0
|
267 |
*/
|
sl@0
|
268 |
static void convertL8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
269 |
{
|
sl@0
|
270 |
while (count--) {
|
sl@0
|
271 |
M3Guint argb = *src++;
|
sl@0
|
272 |
argb |= (argb << 8) | (argb << 16);
|
sl@0
|
273 |
argb |= M3G_ALPHA_MASK;
|
sl@0
|
274 |
*dst++ = argb;
|
sl@0
|
275 |
}
|
sl@0
|
276 |
}
|
sl@0
|
277 |
|
sl@0
|
278 |
/*!
|
sl@0
|
279 |
* \internal \brief LA8 -> ARGB
|
sl@0
|
280 |
*/
|
sl@0
|
281 |
static void convertLA8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
282 |
{
|
sl@0
|
283 |
while (count--) {
|
sl@0
|
284 |
M3Guint argb = *src++;
|
sl@0
|
285 |
argb |= (argb << 8) | (argb << 16);
|
sl@0
|
286 |
argb |= ((M3Guint) *src++) << 24;
|
sl@0
|
287 |
*dst++ = argb;
|
sl@0
|
288 |
}
|
sl@0
|
289 |
}
|
sl@0
|
290 |
|
sl@0
|
291 |
/*!
|
sl@0
|
292 |
* \internal \brief RGB8 -> ARGB
|
sl@0
|
293 |
*/
|
sl@0
|
294 |
static void convertRGB8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
295 |
{
|
sl@0
|
296 |
while (count--) {
|
sl@0
|
297 |
M3Guint argb = M3G_ALPHA_MASK;
|
sl@0
|
298 |
argb |= ((M3Guint)(*src++)) << 16;
|
sl@0
|
299 |
argb |= ((M3Guint)(*src++)) << 8;
|
sl@0
|
300 |
argb |= (M3Guint)(*src++);
|
sl@0
|
301 |
*dst++ = argb;
|
sl@0
|
302 |
}
|
sl@0
|
303 |
}
|
sl@0
|
304 |
|
sl@0
|
305 |
/*!
|
sl@0
|
306 |
* \internal \brief RGB565 -> ARGB
|
sl@0
|
307 |
*/
|
sl@0
|
308 |
static void convertRGB565ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
while (count--) {
|
sl@0
|
311 |
M3Guint argb = M3G_ALPHA_MASK;
|
sl@0
|
312 |
const M3Guint rgb565 = *(const M3Gushort*)src;
|
sl@0
|
313 |
argb |= ((rgb565 & 0xF800u) << 8)|((rgb565 & 0xE000u) << 3);
|
sl@0
|
314 |
argb |= ((rgb565 & 0x07E0u) << 5)|((rgb565 & 0x0600u) >> 1);
|
sl@0
|
315 |
argb |= ((rgb565 & 0x001Fu) << 3)|((rgb565 & 0x001Cu) >> 2);
|
sl@0
|
316 |
*dst++ = argb;
|
sl@0
|
317 |
src += 2;
|
sl@0
|
318 |
}
|
sl@0
|
319 |
}
|
sl@0
|
320 |
|
sl@0
|
321 |
/*!
|
sl@0
|
322 |
* \internal \brief RGBA8 -> ARGB
|
sl@0
|
323 |
*/
|
sl@0
|
324 |
static void convertRGBA8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
325 |
{
|
sl@0
|
326 |
while (count--) {
|
sl@0
|
327 |
M3Guint argb;
|
sl@0
|
328 |
argb = ((M3Guint)(*src++)) << 16;
|
sl@0
|
329 |
argb |= ((M3Guint)(*src++)) << 8;
|
sl@0
|
330 |
argb |= (M3Guint)(*src++);
|
sl@0
|
331 |
argb |= ((M3Guint)(*src++)) << 24;
|
sl@0
|
332 |
*dst++ = argb;
|
sl@0
|
333 |
}
|
sl@0
|
334 |
}
|
sl@0
|
335 |
|
sl@0
|
336 |
/*!
|
sl@0
|
337 |
* \internal \brief BGRA8 -> ARGB
|
sl@0
|
338 |
*/
|
sl@0
|
339 |
static void convertBGRA8ToARGB(const M3Gubyte *src, M3Gsizei count, M3Guint *dst)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
while (count--) {
|
sl@0
|
342 |
M3Guint argb;
|
sl@0
|
343 |
argb = (M3Guint)(*src++);
|
sl@0
|
344 |
argb |= ((M3Guint)(*src++)) << 8;
|
sl@0
|
345 |
argb |= ((M3Guint)(*src++)) << 16;
|
sl@0
|
346 |
argb |= ((M3Guint)(*src++)) << 24;
|
sl@0
|
347 |
*dst++ = argb;
|
sl@0
|
348 |
}
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
/*!
|
sl@0
|
352 |
* \internal
|
sl@0
|
353 |
* \brief Converts a span of pixels to ARGB
|
sl@0
|
354 |
*
|
sl@0
|
355 |
* \param srcFormat source format
|
sl@0
|
356 |
* \param src source pixels
|
sl@0
|
357 |
* \param count pixel count
|
sl@0
|
358 |
* \param dst destination pixels
|
sl@0
|
359 |
*/
|
sl@0
|
360 |
static void convertToARGB(M3GPixelFormat srcFormat,
|
sl@0
|
361 |
const M3Gubyte *src,
|
sl@0
|
362 |
M3Gsizei count,
|
sl@0
|
363 |
M3Guint *dst)
|
sl@0
|
364 |
{
|
sl@0
|
365 |
switch (srcFormat) {
|
sl@0
|
366 |
case M3G_A8:
|
sl@0
|
367 |
convertA8ToARGB(src, count, dst);
|
sl@0
|
368 |
break;
|
sl@0
|
369 |
case M3G_L8:
|
sl@0
|
370 |
convertL8ToARGB(src, count, dst);
|
sl@0
|
371 |
break;
|
sl@0
|
372 |
case M3G_LA8:
|
sl@0
|
373 |
convertLA8ToARGB(src, count, dst);
|
sl@0
|
374 |
break;
|
sl@0
|
375 |
case M3G_RGB8:
|
sl@0
|
376 |
convertRGB8ToARGB(src, count, dst);
|
sl@0
|
377 |
break;
|
sl@0
|
378 |
case M3G_RGB565:
|
sl@0
|
379 |
convertRGB565ToARGB(src, count, dst);
|
sl@0
|
380 |
break;
|
sl@0
|
381 |
case M3G_RGBA8:
|
sl@0
|
382 |
case M3G_RGB8_32:
|
sl@0
|
383 |
convertRGBA8ToARGB(src, count, dst);
|
sl@0
|
384 |
break;
|
sl@0
|
385 |
case M3G_BGRA8:
|
sl@0
|
386 |
case M3G_BGR8_32:
|
sl@0
|
387 |
convertBGRA8ToARGB(src, count, dst);
|
sl@0
|
388 |
break;
|
sl@0
|
389 |
default:
|
sl@0
|
390 |
M3G_ASSERT(M3G_FALSE); /* conversion not supported */
|
sl@0
|
391 |
}
|
sl@0
|
392 |
}
|
sl@0
|
393 |
|
sl@0
|
394 |
/*!
|
sl@0
|
395 |
* \internal
|
sl@0
|
396 |
* \brief Fast path for BGRA-to-RGBA conversion
|
sl@0
|
397 |
*/
|
sl@0
|
398 |
#if defined (M3G_HW_ARMV6)
|
sl@0
|
399 |
__asm void fastConvertBGRAToRGBA(const M3Gubyte *src, M3Gsizei srcStride,
|
sl@0
|
400 |
M3Gsizei width, M3Gsizei height,
|
sl@0
|
401 |
M3Gubyte *dst)
|
sl@0
|
402 |
{
|
sl@0
|
403 |
// r0 = *src
|
sl@0
|
404 |
// r1 = srcStride
|
sl@0
|
405 |
// r2 = width
|
sl@0
|
406 |
// r3 = height
|
sl@0
|
407 |
// sp[0] = *dst
|
sl@0
|
408 |
|
sl@0
|
409 |
CODE32
|
sl@0
|
410 |
|
sl@0
|
411 |
CMP r3, #0 // if height = 0, do nothing
|
sl@0
|
412 |
BXEQ lr
|
sl@0
|
413 |
|
sl@0
|
414 |
STMFD sp!, {r4-r12, lr}
|
sl@0
|
415 |
|
sl@0
|
416 |
LDR r12, [sp, #(10*4)]
|
sl@0
|
417 |
SUB r1, r1, r2, LSL #2
|
sl@0
|
418 |
MOV r14, r2
|
sl@0
|
419 |
|
sl@0
|
420 |
_fastConvertBGRAToRGBA_outerLoop
|
sl@0
|
421 |
MOVS r2, r14, ASR #2 // amount of 4x32 bit writes
|
sl@0
|
422 |
BEQ _fastConvertBGRAToRGBA_tail
|
sl@0
|
423 |
|
sl@0
|
424 |
_fastConvertBGRAToRGBA_innerLoop
|
sl@0
|
425 |
|
sl@0
|
426 |
LDMIA r0!, {r4-r7} // AARRGGBB
|
sl@0
|
427 |
SUBS r2, #1
|
sl@0
|
428 |
REV r4, r4 // BBGGRRAA
|
sl@0
|
429 |
REV r5, r5
|
sl@0
|
430 |
REV r6, r6
|
sl@0
|
431 |
REV r7, r7
|
sl@0
|
432 |
MOV r8, r4, ROR #8 // AABBGGRR
|
sl@0
|
433 |
MOV r9, r5, ROR #8
|
sl@0
|
434 |
MOV r10, r6, ROR #8
|
sl@0
|
435 |
MOV r11, r7, ROR #8
|
sl@0
|
436 |
STMIA r12!, {r8-r11}
|
sl@0
|
437 |
BNE _fastConvertBGRAToRGBA_innerLoop
|
sl@0
|
438 |
|
sl@0
|
439 |
_fastConvertBGRAToRGBA_tail
|
sl@0
|
440 |
MOV r2, r14, ASR #2
|
sl@0
|
441 |
SUBS r2, r14, r2, LSL #2 // number of remaining writes in the tail
|
sl@0
|
442 |
|
sl@0
|
443 |
_fastConvertBGRAToRGBA_tail_loop
|
sl@0
|
444 |
|
sl@0
|
445 |
LDRNE r4, [r0], #4
|
sl@0
|
446 |
REVNE r4, r4
|
sl@0
|
447 |
MOVNE r8, r4, ROR #8
|
sl@0
|
448 |
STRNE r8, [r12], #4
|
sl@0
|
449 |
SUBNES r2, #1
|
sl@0
|
450 |
BNE _fastConvertBGRAToRGBA_tail_loop
|
sl@0
|
451 |
|
sl@0
|
452 |
SUBS r3, #1
|
sl@0
|
453 |
ADD r0, r0, r1
|
sl@0
|
454 |
BNE _fastConvertBGRAToRGBA_outerLoop
|
sl@0
|
455 |
|
sl@0
|
456 |
LDMFD sp!, {r4-r12, lr}
|
sl@0
|
457 |
BX lr
|
sl@0
|
458 |
|
sl@0
|
459 |
}
|
sl@0
|
460 |
#else /* #if defined (M3G_HW_ARMV6) */
|
sl@0
|
461 |
static void fastConvertBGRAToRGBA(const M3Gubyte *src, M3Gsizei srcStride,
|
sl@0
|
462 |
M3Gsizei width, M3Gsizei height,
|
sl@0
|
463 |
M3Gubyte *dst)
|
sl@0
|
464 |
{
|
sl@0
|
465 |
unsigned int pixel, pixel2;
|
sl@0
|
466 |
unsigned int temp;
|
sl@0
|
467 |
unsigned int mask = 0x00ff00ff;
|
sl@0
|
468 |
int spanwidth = (width >> 1) - 1;
|
sl@0
|
469 |
int x, y;
|
sl@0
|
470 |
unsigned int *d = (unsigned int *)dst;
|
sl@0
|
471 |
|
sl@0
|
472 |
M3G_ASSERT(width > 2);
|
sl@0
|
473 |
|
sl@0
|
474 |
for (y = 0; y < height; ++y) {
|
sl@0
|
475 |
unsigned int *s = (unsigned int *)(src + y*srcStride);
|
sl@0
|
476 |
|
sl@0
|
477 |
pixel = *s++;
|
sl@0
|
478 |
|
sl@0
|
479 |
for (x = 0; x < spanwidth; ++x) {
|
sl@0
|
480 |
pixel2 = *s++;
|
sl@0
|
481 |
|
sl@0
|
482 |
temp = pixel & mask; /* 00RR00BB */
|
sl@0
|
483 |
pixel = pixel - temp; /* AA00GG00 */
|
sl@0
|
484 |
pixel = pixel | (temp << 16); /* AABBGG00 */
|
sl@0
|
485 |
*d++ = pixel | (temp >> 16); /* AABBGGRR */
|
sl@0
|
486 |
|
sl@0
|
487 |
pixel = *s++;
|
sl@0
|
488 |
|
sl@0
|
489 |
temp = pixel2 & mask; /* 00RR00BB */
|
sl@0
|
490 |
pixel2 = pixel2 - temp; /* AA00GG00 */
|
sl@0
|
491 |
pixel2 = pixel2 | (temp << 16); /* AABBGG00 */
|
sl@0
|
492 |
*d++ = pixel2 | (temp >> 16); /* AABBGGRR */
|
sl@0
|
493 |
}
|
sl@0
|
494 |
|
sl@0
|
495 |
pixel2 = *s++;
|
sl@0
|
496 |
temp = pixel & mask; /* 00RR00BB */
|
sl@0
|
497 |
pixel = pixel - temp; /* AA00GG00 */
|
sl@0
|
498 |
pixel = pixel | (temp << 16); /* AABBGG00 */
|
sl@0
|
499 |
*d++ = pixel | (temp >> 16); /* AABBGGRR */
|
sl@0
|
500 |
|
sl@0
|
501 |
temp = pixel2 & mask; /* 00RR00BB */
|
sl@0
|
502 |
pixel2 = pixel2 - temp; /* AA00GG00 */
|
sl@0
|
503 |
pixel2 = pixel2 | (temp << 16); /* AABBGG00 */
|
sl@0
|
504 |
*d++ = pixel2 | (temp >> 16); /* AABBGGRR */
|
sl@0
|
505 |
}
|
sl@0
|
506 |
}
|
sl@0
|
507 |
#endif /* #if defined (M3G_HW_ARMV6) */
|
sl@0
|
508 |
|
sl@0
|
509 |
/*--------------------------------------------------------------------*/
|
sl@0
|
510 |
|
sl@0
|
511 |
/*!
|
sl@0
|
512 |
* \internal
|
sl@0
|
513 |
* \brief Maps a logical image format to an internal pixel format
|
sl@0
|
514 |
*
|
sl@0
|
515 |
* \param imgFormat logical image format
|
sl@0
|
516 |
* \param paletted paletted flag
|
sl@0
|
517 |
* \return the internal image pixel format
|
sl@0
|
518 |
*/
|
sl@0
|
519 |
static M3GPixelFormat getInternalFormat(M3GImageFormat imgFormat,
|
sl@0
|
520 |
M3Gbool paletted)
|
sl@0
|
521 |
{
|
sl@0
|
522 |
if (paletted) {
|
sl@0
|
523 |
switch (imgFormat) {
|
sl@0
|
524 |
case M3G_RGB:
|
sl@0
|
525 |
# if defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
526 |
return M3G_PALETTE8_RGB8_32;
|
sl@0
|
527 |
# else
|
sl@0
|
528 |
return M3G_PALETTE8_RGB8;
|
sl@0
|
529 |
# endif
|
sl@0
|
530 |
case M3G_RGBA:
|
sl@0
|
531 |
return M3G_PALETTE8_RGBA8;
|
sl@0
|
532 |
default:
|
sl@0
|
533 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
534 |
return (M3GPixelFormat)0;
|
sl@0
|
535 |
}
|
sl@0
|
536 |
}
|
sl@0
|
537 |
else {
|
sl@0
|
538 |
M3GPixelFormat format = m3gPixelFormat(imgFormat);
|
sl@0
|
539 |
|
sl@0
|
540 |
# if defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
541 |
if (format == M3G_RGB8) {
|
sl@0
|
542 |
return (M3G_USE_16BIT_TEXTURES) ? M3G_RGB565 : M3G_RGB8_32;
|
sl@0
|
543 |
}
|
sl@0
|
544 |
if (format == M3G_LA8) {
|
sl@0
|
545 |
return M3G_LA4;
|
sl@0
|
546 |
}
|
sl@0
|
547 |
# endif
|
sl@0
|
548 |
|
sl@0
|
549 |
return format;
|
sl@0
|
550 |
}
|
sl@0
|
551 |
}
|
sl@0
|
552 |
|
sl@0
|
553 |
/*!
|
sl@0
|
554 |
* \internal
|
sl@0
|
555 |
* \brief Gets the correct pixel format for setting data to an image
|
sl@0
|
556 |
*/
|
sl@0
|
557 |
static M3GPixelFormat m3gInputDataFormat(const Image *img)
|
sl@0
|
558 |
{
|
sl@0
|
559 |
/* Any of the paletted formats will do for a paletted image, as
|
sl@0
|
560 |
* they all have 8-bit indices; we pick PALETTE8_RGBA8 here */
|
sl@0
|
561 |
|
sl@0
|
562 |
if (img->flags & M3G_PALETTED) {
|
sl@0
|
563 |
return M3G_PALETTE8_RGBA8;
|
sl@0
|
564 |
}
|
sl@0
|
565 |
|
sl@0
|
566 |
return m3gPixelFormat(img->format);
|
sl@0
|
567 |
}
|
sl@0
|
568 |
|
sl@0
|
569 |
|
sl@0
|
570 |
/*!
|
sl@0
|
571 |
* \internal
|
sl@0
|
572 |
* \brief Returns log2(resolution)+1. Assumes that resolution is power of two.
|
sl@0
|
573 |
*
|
sl@0
|
574 |
* \param w width in pixels
|
sl@0
|
575 |
* \param h height in pixels
|
sl@0
|
576 |
* \return number of needed mipmap levels
|
sl@0
|
577 |
*/
|
sl@0
|
578 |
static M3Gint m3gGetNumMipmapLevels(M3Gint w, M3Gint h)
|
sl@0
|
579 |
{
|
sl@0
|
580 |
M3Gint res = (w > h) ? w : h;
|
sl@0
|
581 |
M3Gint levels = 0;
|
sl@0
|
582 |
while (res > 0) {
|
sl@0
|
583 |
++levels;
|
sl@0
|
584 |
res >>= 1;
|
sl@0
|
585 |
};
|
sl@0
|
586 |
return levels;
|
sl@0
|
587 |
}
|
sl@0
|
588 |
|
sl@0
|
589 |
/*!
|
sl@0
|
590 |
* \internal
|
sl@0
|
591 |
* \brief Downsamples an image to half the original size
|
sl@0
|
592 |
*
|
sl@0
|
593 |
*
|
sl@0
|
594 |
* \param format pixel format
|
sl@0
|
595 |
* \param srcPixels source pixels
|
sl@0
|
596 |
* \param pw pointer to width
|
sl@0
|
597 |
* \param ph pointer to height
|
sl@0
|
598 |
* \param dstPixels destination pixels
|
sl@0
|
599 |
*/
|
sl@0
|
600 |
static void m3gDownsample(M3GPixelFormat format,
|
sl@0
|
601 |
const M3Gubyte *srcPixels,
|
sl@0
|
602 |
M3Gint *pw, M3Gint *ph,
|
sl@0
|
603 |
M3Gubyte *dstPixels)
|
sl@0
|
604 |
{
|
sl@0
|
605 |
M3Gint i, j, bpp, pixStride, lineStride;
|
sl@0
|
606 |
M3Gint w = *pw, h = *ph;
|
sl@0
|
607 |
M3Gubyte *dst;
|
sl@0
|
608 |
M3Guint temp[2][SPAN_BUFFER_SIZE/2];
|
sl@0
|
609 |
|
sl@0
|
610 |
M3G_ASSERT_PTR(srcPixels);
|
sl@0
|
611 |
M3G_ASSERT(w >= 1 && h >= 1);
|
sl@0
|
612 |
|
sl@0
|
613 |
bpp = m3gBytesPerPixel(format);
|
sl@0
|
614 |
lineStride = (h > 1) ? w * bpp : 0;
|
sl@0
|
615 |
pixStride = (w > 1) ? bpp : 0;
|
sl@0
|
616 |
|
sl@0
|
617 |
dst = dstPixels;
|
sl@0
|
618 |
|
sl@0
|
619 |
/* Iterate over buffer-sized blocks in the image */
|
sl@0
|
620 |
|
sl@0
|
621 |
for (j = 0; j < h; j += 2) {
|
sl@0
|
622 |
for (i = 0; i < w; i += SPAN_BUFFER_SIZE/2) {
|
sl@0
|
623 |
|
sl@0
|
624 |
/* Fill the buffer from the source image */
|
sl@0
|
625 |
|
sl@0
|
626 |
const M3Gubyte *src = srcPixels + (j*lineStride + i*pixStride);
|
sl@0
|
627 |
M3Gint c = SPAN_BUFFER_SIZE/2;
|
sl@0
|
628 |
if (w - i < c) {
|
sl@0
|
629 |
c = w - i;
|
sl@0
|
630 |
}
|
sl@0
|
631 |
convertToARGB(format, src, c, &temp[0][0]);
|
sl@0
|
632 |
convertToARGB(format, src + lineStride, c, &temp[1][0]);
|
sl@0
|
633 |
if (w == 1) {
|
sl@0
|
634 |
temp[0][1] = temp[0][0];
|
sl@0
|
635 |
temp[1][1] = temp[1][0];
|
sl@0
|
636 |
}
|
sl@0
|
637 |
|
sl@0
|
638 |
/* Average the pixels in the buffer */
|
sl@0
|
639 |
{
|
sl@0
|
640 |
# define AG_MASK 0xFF00FF00u
|
sl@0
|
641 |
# define RB_MASK 0x00FF00FFu
|
sl@0
|
642 |
|
sl@0
|
643 |
M3Gint k;
|
sl@0
|
644 |
for (k = 0; k < c; k += 2) {
|
sl@0
|
645 |
M3Guint ag, rb;
|
sl@0
|
646 |
|
sl@0
|
647 |
/* Add two components in parallel */
|
sl@0
|
648 |
|
sl@0
|
649 |
ag = ((temp[0][k] & AG_MASK) >> 8)
|
sl@0
|
650 |
+ ((temp[1][k] & AG_MASK) >> 8)
|
sl@0
|
651 |
+ ((temp[0][k+1] & AG_MASK) >> 8)
|
sl@0
|
652 |
+ ((temp[1][k+1] & AG_MASK) >> 8);
|
sl@0
|
653 |
|
sl@0
|
654 |
rb = (temp[0][k] & RB_MASK)
|
sl@0
|
655 |
+ (temp[1][k] & RB_MASK)
|
sl@0
|
656 |
+ (temp[0][k+1] & RB_MASK)
|
sl@0
|
657 |
+ (temp[1][k+1] & RB_MASK);
|
sl@0
|
658 |
|
sl@0
|
659 |
/* Shift to divide by 4, adding ½ for rounding */
|
sl@0
|
660 |
|
sl@0
|
661 |
temp[0][k>>1] = ((((ag + 0x00020002u) << 6) & AG_MASK) |
|
sl@0
|
662 |
(((rb + 0x00020002u) >> 2) & RB_MASK));
|
sl@0
|
663 |
}
|
sl@0
|
664 |
|
sl@0
|
665 |
# undef AG_MASK
|
sl@0
|
666 |
# undef RB_MASK
|
sl@0
|
667 |
}
|
sl@0
|
668 |
|
sl@0
|
669 |
/* Write result to the output buffer */
|
sl@0
|
670 |
|
sl@0
|
671 |
convertFromARGB(&temp[0][0], c>>1, format, dst);
|
sl@0
|
672 |
dst += (c>>1) * bpp;
|
sl@0
|
673 |
}
|
sl@0
|
674 |
}
|
sl@0
|
675 |
|
sl@0
|
676 |
/* Return output width and height */
|
sl@0
|
677 |
|
sl@0
|
678 |
if (w > 1) {
|
sl@0
|
679 |
*pw = (w >> 1);
|
sl@0
|
680 |
}
|
sl@0
|
681 |
if (h > 1) {
|
sl@0
|
682 |
*ph = (h >> 1);
|
sl@0
|
683 |
}
|
sl@0
|
684 |
}
|
sl@0
|
685 |
|
sl@0
|
686 |
/*!
|
sl@0
|
687 |
* \internal
|
sl@0
|
688 |
* \brief Returns the OpenGL minification filter corresponding to M3G
|
sl@0
|
689 |
* filtering flags
|
sl@0
|
690 |
*/
|
sl@0
|
691 |
static GLenum m3gGetGLMinFilter(M3Genum levelFilter, M3Genum imageFilter)
|
sl@0
|
692 |
{
|
sl@0
|
693 |
static const GLenum minFilter[3][2] = {
|
sl@0
|
694 |
GL_LINEAR, GL_NEAREST,
|
sl@0
|
695 |
GL_LINEAR_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_LINEAR,
|
sl@0
|
696 |
GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_NEAREST
|
sl@0
|
697 |
};
|
sl@0
|
698 |
|
sl@0
|
699 |
return minFilter[levelFilter - M3G_FILTER_BASE_LEVEL][imageFilter - M3G_FILTER_LINEAR];
|
sl@0
|
700 |
}
|
sl@0
|
701 |
|
sl@0
|
702 |
/*----------------------------------------------------------------------
|
sl@0
|
703 |
* Internal functions
|
sl@0
|
704 |
*--------------------------------------------------------------------*/
|
sl@0
|
705 |
|
sl@0
|
706 |
/*!
|
sl@0
|
707 |
* \internal
|
sl@0
|
708 |
* \brief Converts an internal ARGB color to four GLfixed components
|
sl@0
|
709 |
*/
|
sl@0
|
710 |
static void m3gGLColor(M3Guint argb, GLfixed *dst)
|
sl@0
|
711 |
{
|
sl@0
|
712 |
GLfixed r, g, b, a;
|
sl@0
|
713 |
|
sl@0
|
714 |
r = (GLfixed)((argb & 0x00FF0000u) >> 16);
|
sl@0
|
715 |
g = (GLfixed)((argb & 0x0000FF00u) >> 8);
|
sl@0
|
716 |
b = (GLfixed)( argb & 0x000000FFu );
|
sl@0
|
717 |
a = (GLfixed)((argb & 0xFF000000u) >> 24);
|
sl@0
|
718 |
|
sl@0
|
719 |
dst[0] = ((r << 8) | r) + (r >> 7);
|
sl@0
|
720 |
dst[1] = ((g << 8) | g) + (g >> 7);
|
sl@0
|
721 |
dst[2] = ((b << 8) | b) + (b >> 7);
|
sl@0
|
722 |
dst[3] = ((a << 8) | a) + (a >> 7);
|
sl@0
|
723 |
}
|
sl@0
|
724 |
|
sl@0
|
725 |
/*!
|
sl@0
|
726 |
* \internal
|
sl@0
|
727 |
* \brief Binds an image into the current texture unit and sets up
|
sl@0
|
728 |
* texture filtering
|
sl@0
|
729 |
*/
|
sl@0
|
730 |
static void m3gBindTextureImage(Image *img, M3Genum levelFilter, M3Genum imageFilter)
|
sl@0
|
731 |
{
|
sl@0
|
732 |
M3G_ASSERT_GL;
|
sl@0
|
733 |
|
sl@0
|
734 |
/* We have no mipmap generation for paletted images, so disable
|
sl@0
|
735 |
* mipmapping in that case */
|
sl@0
|
736 |
|
sl@0
|
737 |
if (m3gIsInternallyPaletted(img)) {
|
sl@0
|
738 |
levelFilter = M3G_FILTER_BASE_LEVEL;
|
sl@0
|
739 |
}
|
sl@0
|
740 |
|
sl@0
|
741 |
/* Bind the OpenGL texture object, generating mipmaps if
|
sl@0
|
742 |
* required */
|
sl@0
|
743 |
|
sl@0
|
744 |
m3gBindTextureObject(img, levelFilter != M3G_FILTER_BASE_LEVEL);
|
sl@0
|
745 |
|
sl@0
|
746 |
/* Set up OpenGL texture filtering according to our flags */
|
sl@0
|
747 |
|
sl@0
|
748 |
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
sl@0
|
749 |
(imageFilter == M3G_FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
|
sl@0
|
750 |
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
sl@0
|
751 |
m3gGetGLMinFilter(levelFilter, imageFilter));
|
sl@0
|
752 |
|
sl@0
|
753 |
M3G_ASSERT_GL;
|
sl@0
|
754 |
}
|
sl@0
|
755 |
|
sl@0
|
756 |
/*!
|
sl@0
|
757 |
* \internal
|
sl@0
|
758 |
* \brief Maps a logical image format to the matching default pixel
|
sl@0
|
759 |
* format
|
sl@0
|
760 |
*
|
sl@0
|
761 |
* \param imgFormat logical image format
|
sl@0
|
762 |
* \return a one-byte-per-pixel pixel format
|
sl@0
|
763 |
*/
|
sl@0
|
764 |
static M3GPixelFormat m3gPixelFormat(M3GImageFormat imgFormat)
|
sl@0
|
765 |
{
|
sl@0
|
766 |
switch (imgFormat) {
|
sl@0
|
767 |
case M3G_ALPHA:
|
sl@0
|
768 |
return M3G_A8;
|
sl@0
|
769 |
case M3G_LUMINANCE:
|
sl@0
|
770 |
return M3G_L8;
|
sl@0
|
771 |
case M3G_LUMINANCE_ALPHA:
|
sl@0
|
772 |
return M3G_LA8;
|
sl@0
|
773 |
case M3G_RGB:
|
sl@0
|
774 |
return M3G_RGB8;
|
sl@0
|
775 |
case M3G_RGBA:
|
sl@0
|
776 |
return M3G_RGBA8;
|
sl@0
|
777 |
default:
|
sl@0
|
778 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
779 |
return M3G_NO_FORMAT;
|
sl@0
|
780 |
}
|
sl@0
|
781 |
}
|
sl@0
|
782 |
|
sl@0
|
783 |
/*!
|
sl@0
|
784 |
* \internal
|
sl@0
|
785 |
* \brief Returns the number of bytes per pixel in a given pixel format
|
sl@0
|
786 |
*
|
sl@0
|
787 |
* \param format pixel format
|
sl@0
|
788 |
* \return bytes per pixel
|
sl@0
|
789 |
*/
|
sl@0
|
790 |
static M3Gint m3gBytesPerPixel(M3GPixelFormat format)
|
sl@0
|
791 |
{
|
sl@0
|
792 |
switch (format) {
|
sl@0
|
793 |
case M3G_L8:
|
sl@0
|
794 |
case M3G_A8:
|
sl@0
|
795 |
case M3G_LA4:
|
sl@0
|
796 |
case M3G_PALETTE8_RGB8:
|
sl@0
|
797 |
case M3G_PALETTE8_RGB8_32:
|
sl@0
|
798 |
case M3G_PALETTE8_RGBA8:
|
sl@0
|
799 |
return 1;
|
sl@0
|
800 |
case M3G_RGB4:
|
sl@0
|
801 |
case M3G_RGB565:
|
sl@0
|
802 |
case M3G_RGBA4:
|
sl@0
|
803 |
case M3G_RGB5A1:
|
sl@0
|
804 |
case M3G_LA8:
|
sl@0
|
805 |
return 2;
|
sl@0
|
806 |
case M3G_RGB8:
|
sl@0
|
807 |
return 3;
|
sl@0
|
808 |
case M3G_RGBA8:
|
sl@0
|
809 |
case M3G_BGRA8:
|
sl@0
|
810 |
case M3G_ARGB8:
|
sl@0
|
811 |
case M3G_BGR8_32:
|
sl@0
|
812 |
case M3G_RGB8_32:
|
sl@0
|
813 |
return 4;
|
sl@0
|
814 |
default:
|
sl@0
|
815 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
816 |
return 0;
|
sl@0
|
817 |
}
|
sl@0
|
818 |
}
|
sl@0
|
819 |
|
sl@0
|
820 |
/*!
|
sl@0
|
821 |
* \internal
|
sl@0
|
822 |
* \brief Converts pixels between formats
|
sl@0
|
823 |
*
|
sl@0
|
824 |
* \note Only a limited subset of source and destination formats may
|
sl@0
|
825 |
* be supported; see the \c convert functions in m3g_image.c
|
sl@0
|
826 |
*
|
sl@0
|
827 |
* \param srcFormat source format
|
sl@0
|
828 |
* \param src source pixels
|
sl@0
|
829 |
* \param dstFormat destination format
|
sl@0
|
830 |
* \param dst destination pixels
|
sl@0
|
831 |
* \param count pixel count
|
sl@0
|
832 |
*/
|
sl@0
|
833 |
static void m3gConvertPixels(M3GPixelFormat srcFormat, const M3Gubyte *src,
|
sl@0
|
834 |
M3GPixelFormat dstFormat, M3Gubyte *dst,
|
sl@0
|
835 |
M3Gsizei count)
|
sl@0
|
836 |
{
|
sl@0
|
837 |
M3Guint temp[SPAN_BUFFER_SIZE];
|
sl@0
|
838 |
const char endianTest[4] = { 1, 0, 0, 0 };
|
sl@0
|
839 |
|
sl@0
|
840 |
M3Guint srcBpp = m3gBytesPerPixel(srcFormat);
|
sl@0
|
841 |
M3Guint dstBpp = m3gBytesPerPixel(dstFormat);
|
sl@0
|
842 |
M3G_ASSERT(srcBpp > 0 && dstBpp > 0);
|
sl@0
|
843 |
|
sl@0
|
844 |
while (count > 0) {
|
sl@0
|
845 |
M3Gsizei n = count;
|
sl@0
|
846 |
|
sl@0
|
847 |
/* Check the source and destination formats to avoid
|
sl@0
|
848 |
the intermediate ARGB format conversion. */
|
sl@0
|
849 |
if (((srcFormat == M3G_RGBA8 && (dstFormat == M3G_BGRA8 || dstFormat == M3G_BGR8_32))
|
sl@0
|
850 |
|| (dstFormat == M3G_RGBA8 && (srcFormat == M3G_BGRA8 || srcFormat == M3G_BGR8_32)))
|
sl@0
|
851 |
&& (n > 2) && ((*(const int *)endianTest) == 1)) {
|
sl@0
|
852 |
/* use fast path for RGBA<->BGRA conversion */
|
sl@0
|
853 |
fastConvertBGRAToRGBA(src, n * srcBpp, n, 1, dst);
|
sl@0
|
854 |
} else if (srcFormat == M3G_ARGB8 && dstFormat != M3G_ARGB8) {
|
sl@0
|
855 |
convertFromARGB((M3Guint*)src, n, dstFormat, dst);
|
sl@0
|
856 |
} else if (srcFormat != M3G_ARGB8 && dstFormat == M3G_ARGB8) {
|
sl@0
|
857 |
convertToARGB(srcFormat, src, n, (M3Guint*)dst);
|
sl@0
|
858 |
} else {
|
sl@0
|
859 |
/* no luck, do the conversion via ARGB (source format -> ARGB -> destination format) */
|
sl@0
|
860 |
n = (count < SPAN_BUFFER_SIZE) ? count : SPAN_BUFFER_SIZE;
|
sl@0
|
861 |
convertToARGB(srcFormat, src, n, temp);
|
sl@0
|
862 |
convertFromARGB(temp, n, dstFormat, dst);
|
sl@0
|
863 |
}
|
sl@0
|
864 |
count -= n;
|
sl@0
|
865 |
src += n * srcBpp;
|
sl@0
|
866 |
dst += n * dstBpp;
|
sl@0
|
867 |
}
|
sl@0
|
868 |
}
|
sl@0
|
869 |
|
sl@0
|
870 |
/*!
|
sl@0
|
871 |
* \internal
|
sl@0
|
872 |
* \brief Copies image data. The source image is copied to
|
sl@0
|
873 |
* the destination image.
|
sl@0
|
874 |
*
|
sl@0
|
875 |
* \param dst destination image
|
sl@0
|
876 |
* \param src source image
|
sl@0
|
877 |
*/
|
sl@0
|
878 |
static void m3gCopyImagePixels(Image *dst,
|
sl@0
|
879 |
const Image *src)
|
sl@0
|
880 |
{
|
sl@0
|
881 |
const M3Gubyte *pSrc;
|
sl@0
|
882 |
M3Gubyte *pDst;
|
sl@0
|
883 |
M3Gint bpp;
|
sl@0
|
884 |
|
sl@0
|
885 |
/* Check inputs (debug only!) */
|
sl@0
|
886 |
M3G_VALIDATE_OBJECT(dst);
|
sl@0
|
887 |
M3G_VALIDATE_OBJECT(src);
|
sl@0
|
888 |
|
sl@0
|
889 |
M3G_ASSERT(src->internalFormat == dst->internalFormat);
|
sl@0
|
890 |
M3G_ASSERT(src->format == dst->format);
|
sl@0
|
891 |
|
sl@0
|
892 |
M3G_ASSERT(src->paletteBytes == dst->paletteBytes);
|
sl@0
|
893 |
|
sl@0
|
894 |
/* Compute source and destination pixel data pointers */
|
sl@0
|
895 |
pSrc = (M3Gubyte *)m3gMapObject(M3G_INTERFACE(src), src->data);
|
sl@0
|
896 |
pDst = (M3Gubyte *)m3gMapObject(M3G_INTERFACE(dst), dst->data);
|
sl@0
|
897 |
|
sl@0
|
898 |
bpp = m3gBytesPerPixel(src->internalFormat);
|
sl@0
|
899 |
|
sl@0
|
900 |
if (src->paletteBytes > 0) {
|
sl@0
|
901 |
m3gCopy(pDst, pSrc, src->paletteBytes);
|
sl@0
|
902 |
pDst += dst->paletteBytes;
|
sl@0
|
903 |
pSrc += src->paletteBytes;
|
sl@0
|
904 |
}
|
sl@0
|
905 |
|
sl@0
|
906 |
/* Do a straight copy if the sizes match, or resample if not */
|
sl@0
|
907 |
if (src->width == dst->width && src->height == dst->height ) {
|
sl@0
|
908 |
m3gCopy(pDst, pSrc, src->width * src->height * bpp);
|
sl@0
|
909 |
}
|
sl@0
|
910 |
else {
|
sl@0
|
911 |
/* Adder values as 8.8 fixed point */
|
sl@0
|
912 |
M3Gint xAdd, yAdd;
|
sl@0
|
913 |
M3Gint x, y;
|
sl@0
|
914 |
|
sl@0
|
915 |
xAdd = (256 * src->width) / dst->width;
|
sl@0
|
916 |
yAdd = (256 * src->height) / dst->height;
|
sl@0
|
917 |
|
sl@0
|
918 |
for (y = 0; y < dst->height; y++) {
|
sl@0
|
919 |
for (x = 0; x < dst->width; x++) {
|
sl@0
|
920 |
m3gCopy(pDst, pSrc + bpp * (((xAdd * x) >> 8) + ((yAdd * y) >> 8) * src->width), bpp);
|
sl@0
|
921 |
pDst += bpp;
|
sl@0
|
922 |
}
|
sl@0
|
923 |
}
|
sl@0
|
924 |
}
|
sl@0
|
925 |
|
sl@0
|
926 |
m3gUnmapObject(M3G_INTERFACE(dst), dst->data);
|
sl@0
|
927 |
m3gUnmapObject(M3G_INTERFACE(src), src->data);
|
sl@0
|
928 |
|
sl@0
|
929 |
m3gInvalidateImage(dst);
|
sl@0
|
930 |
}
|
sl@0
|
931 |
|
sl@0
|
932 |
/*!
|
sl@0
|
933 |
* \internal
|
sl@0
|
934 |
* \brief Invalidates any cached data for this image
|
sl@0
|
935 |
*
|
sl@0
|
936 |
* Used when rendering to the image.
|
sl@0
|
937 |
*
|
sl@0
|
938 |
* \param img Image object
|
sl@0
|
939 |
*/
|
sl@0
|
940 |
static void m3gInvalidateImage(Image *img)
|
sl@0
|
941 |
{
|
sl@0
|
942 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
943 |
img->dirty = M3G_TRUE;
|
sl@0
|
944 |
|
sl@0
|
945 |
# if !defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
946 |
if (img->large) {
|
sl@0
|
947 |
img->large->dirty = M3G_TRUE;
|
sl@0
|
948 |
}
|
sl@0
|
949 |
# endif /*M3G_NGL_TEXTURE_API*/
|
sl@0
|
950 |
|
sl@0
|
951 |
if (img->powerOfTwo != img) {
|
sl@0
|
952 |
img->powerOfTwoDirty = M3G_TRUE;
|
sl@0
|
953 |
}
|
sl@0
|
954 |
}
|
sl@0
|
955 |
|
sl@0
|
956 |
/*!
|
sl@0
|
957 |
* \internal
|
sl@0
|
958 |
* \brief Overloaded Object3D method.
|
sl@0
|
959 |
*
|
sl@0
|
960 |
* \param originalObj original Image object
|
sl@0
|
961 |
* \param cloneObj pointer to cloned Image object
|
sl@0
|
962 |
* \param pairs array for all object-duplicate pairs
|
sl@0
|
963 |
* \param numPairs number of pairs
|
sl@0
|
964 |
*/
|
sl@0
|
965 |
static M3Gbool m3gImageDuplicate(const Object *originalObj,
|
sl@0
|
966 |
Object **cloneObj,
|
sl@0
|
967 |
Object **pairs,
|
sl@0
|
968 |
M3Gint *numPairs)
|
sl@0
|
969 |
{
|
sl@0
|
970 |
Image *original = (Image *)originalObj;
|
sl@0
|
971 |
Image *clone;
|
sl@0
|
972 |
|
sl@0
|
973 |
/* If the original image still has its pixel data, make a full
|
sl@0
|
974 |
* copy -- this is wasteful for immutable images, but the shame's
|
sl@0
|
975 |
* on the user in that case */
|
sl@0
|
976 |
|
sl@0
|
977 |
if (original->data) {
|
sl@0
|
978 |
clone = (Image*) m3gCreateImage(originalObj->interface,
|
sl@0
|
979 |
original->format,
|
sl@0
|
980 |
original->width,
|
sl@0
|
981 |
original->height,
|
sl@0
|
982 |
original->flags);
|
sl@0
|
983 |
}
|
sl@0
|
984 |
else {
|
sl@0
|
985 |
|
sl@0
|
986 |
/* Otherwise, just point to the original and use its data
|
sl@0
|
987 |
* buffers */
|
sl@0
|
988 |
|
sl@0
|
989 |
clone = (Image*) m3gAlloc(M3G_INTERFACE(original), sizeof(*clone));
|
sl@0
|
990 |
*clone = *original;
|
sl@0
|
991 |
M3G_ASSIGN_REF(clone->copyOf, original);
|
sl@0
|
992 |
}
|
sl@0
|
993 |
|
sl@0
|
994 |
*cloneObj = (Object *)clone;
|
sl@0
|
995 |
if (*cloneObj == NULL) {
|
sl@0
|
996 |
return M3G_FALSE;
|
sl@0
|
997 |
}
|
sl@0
|
998 |
|
sl@0
|
999 |
if (m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
|
sl@0
|
1000 |
/* Copy image contents */
|
sl@0
|
1001 |
if (original->data) {
|
sl@0
|
1002 |
m3gCopyImagePixels(clone, original);
|
sl@0
|
1003 |
}
|
sl@0
|
1004 |
return M3G_TRUE;
|
sl@0
|
1005 |
}
|
sl@0
|
1006 |
else {
|
sl@0
|
1007 |
return M3G_FALSE;
|
sl@0
|
1008 |
}
|
sl@0
|
1009 |
}
|
sl@0
|
1010 |
|
sl@0
|
1011 |
/*!
|
sl@0
|
1012 |
* \internal
|
sl@0
|
1013 |
*
|
sl@0
|
1014 |
* \brief Frees the pixel data associated with this image; used for
|
sl@0
|
1015 |
* optimizing memory usage after copying the data to a secondary
|
sl@0
|
1016 |
* location
|
sl@0
|
1017 |
*/
|
sl@0
|
1018 |
static void m3gFreeImageData(Image *img)
|
sl@0
|
1019 |
{
|
sl@0
|
1020 |
M3G_ASSERT(img->format == M3G_RGB || img->format == M3G_LUMINANCE);
|
sl@0
|
1021 |
# if !defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
1022 |
M3G_ASSERT(!img->mipmapsDirty);
|
sl@0
|
1023 |
# endif
|
sl@0
|
1024 |
M3G_ASSERT(!img->powerOfTwoDirty);
|
sl@0
|
1025 |
M3G_ASSERT(img->powerOfTwo != NULL);
|
sl@0
|
1026 |
M3G_ASSERT(!img->pinned);
|
sl@0
|
1027 |
|
sl@0
|
1028 |
M3G_LOG1(M3G_LOG_IMAGES, "Freeing copy of image 0x%08X\n",
|
sl@0
|
1029 |
(unsigned) img);
|
sl@0
|
1030 |
|
sl@0
|
1031 |
if (!img->copyOf) {
|
sl@0
|
1032 |
m3gFreeObject(M3G_INTERFACE(img), img->data);
|
sl@0
|
1033 |
img->data = 0;
|
sl@0
|
1034 |
m3gFreeObject(M3G_INTERFACE(img), img->mipData);
|
sl@0
|
1035 |
img->mipData = 0;
|
sl@0
|
1036 |
}
|
sl@0
|
1037 |
M3G_ASSIGN_REF(img->copyOf, NULL);
|
sl@0
|
1038 |
}
|
sl@0
|
1039 |
|
sl@0
|
1040 |
/*!
|
sl@0
|
1041 |
* \internal
|
sl@0
|
1042 |
* \brief Returns a power-of-two variant of an image
|
sl@0
|
1043 |
*
|
sl@0
|
1044 |
* This is used for sprites and background images.
|
sl@0
|
1045 |
*/
|
sl@0
|
1046 |
static Image *m3gGetPowerOfTwoImage(Image *img)
|
sl@0
|
1047 |
{
|
sl@0
|
1048 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1049 |
|
sl@0
|
1050 |
/* Create a power-of-two variant of the image if one doesn't exist
|
sl@0
|
1051 |
* already */
|
sl@0
|
1052 |
|
sl@0
|
1053 |
if (img->powerOfTwo == NULL) {
|
sl@0
|
1054 |
|
sl@0
|
1055 |
M3Gint width, height;
|
sl@0
|
1056 |
M3Gbitmask flags;
|
sl@0
|
1057 |
Image *potImage;
|
sl@0
|
1058 |
|
sl@0
|
1059 |
M3G_ASSERT(!m3gIsPowerOfTwo(img->width) ||
|
sl@0
|
1060 |
!m3gIsPowerOfTwo(img->height));
|
sl@0
|
1061 |
|
sl@0
|
1062 |
/* Choose new image size to allow a maximum shrinkage of 25%;
|
sl@0
|
1063 |
* this is to weed out pathological cases of quadruple memory
|
sl@0
|
1064 |
* usage because an image is one pixel too wide */
|
sl@0
|
1065 |
|
sl@0
|
1066 |
width = m3gNextPowerOfTwo((img->width * 3) >> 2);
|
sl@0
|
1067 |
height = m3gNextPowerOfTwo((img->height * 3) >> 2);
|
sl@0
|
1068 |
|
sl@0
|
1069 |
width = M3G_MIN(width, M3G_MAX_TEXTURE_DIMENSION);
|
sl@0
|
1070 |
height = M3G_MIN(height, M3G_MAX_TEXTURE_DIMENSION);
|
sl@0
|
1071 |
|
sl@0
|
1072 |
flags = img->flags & (~M3G_RENDERING_TARGET);
|
sl@0
|
1073 |
|
sl@0
|
1074 |
potImage = m3gCreateImage(M3G_INTERFACE(img),
|
sl@0
|
1075 |
img->format,
|
sl@0
|
1076 |
width, height,
|
sl@0
|
1077 |
flags);
|
sl@0
|
1078 |
if (!potImage) {
|
sl@0
|
1079 |
return NULL; /* automatic out-of-memory */
|
sl@0
|
1080 |
}
|
sl@0
|
1081 |
|
sl@0
|
1082 |
M3G_ASSIGN_REF(img->powerOfTwo, potImage);
|
sl@0
|
1083 |
img->powerOfTwoDirty = M3G_TRUE;
|
sl@0
|
1084 |
}
|
sl@0
|
1085 |
|
sl@0
|
1086 |
/* Update POT image data if necessary */
|
sl@0
|
1087 |
|
sl@0
|
1088 |
if (img->powerOfTwoDirty) {
|
sl@0
|
1089 |
m3gCopyImagePixels(img->powerOfTwo, img);
|
sl@0
|
1090 |
img->powerOfTwoDirty = M3G_FALSE;
|
sl@0
|
1091 |
|
sl@0
|
1092 |
/* Get rid of the original at this point if we can */
|
sl@0
|
1093 |
|
sl@0
|
1094 |
if (!img->pinned) {
|
sl@0
|
1095 |
m3gFreeImageData(img);
|
sl@0
|
1096 |
}
|
sl@0
|
1097 |
}
|
sl@0
|
1098 |
|
sl@0
|
1099 |
return img->powerOfTwo;
|
sl@0
|
1100 |
}
|
sl@0
|
1101 |
|
sl@0
|
1102 |
/*!
|
sl@0
|
1103 |
* \internal
|
sl@0
|
1104 |
* \brief Gets image alpha at x, y.
|
sl@0
|
1105 |
*
|
sl@0
|
1106 |
* \param image Image object
|
sl@0
|
1107 |
* \param x x-coordinate
|
sl@0
|
1108 |
* \param y y-coordinate
|
sl@0
|
1109 |
* \return alpha value
|
sl@0
|
1110 |
*
|
sl@0
|
1111 |
*/
|
sl@0
|
1112 |
static M3Gint m3gGetAlpha(Image *image, M3Gint x, M3Gint y)
|
sl@0
|
1113 |
{
|
sl@0
|
1114 |
M3Gint alpha = 255;
|
sl@0
|
1115 |
M3Gint bpp = m3gBytesPerPixel(image->internalFormat);
|
sl@0
|
1116 |
M3Guint data = 0;
|
sl@0
|
1117 |
M3Gubyte *pixels;
|
sl@0
|
1118 |
|
sl@0
|
1119 |
/* Quick exit for non-alpha formats */
|
sl@0
|
1120 |
|
sl@0
|
1121 |
if (image->format == M3G_RGB || image->format == M3G_LUMINANCE) {
|
sl@0
|
1122 |
return alpha;
|
sl@0
|
1123 |
}
|
sl@0
|
1124 |
|
sl@0
|
1125 |
/* For other formats, we have to sample the image data */
|
sl@0
|
1126 |
|
sl@0
|
1127 |
if (!image->data) {
|
sl@0
|
1128 |
Image *potImage = image->powerOfTwo;
|
sl@0
|
1129 |
M3G_ASSERT(potImage != image);
|
sl@0
|
1130 |
return m3gGetAlpha(potImage,
|
sl@0
|
1131 |
(x * image->width) / potImage->width,
|
sl@0
|
1132 |
(y * image->height) / potImage->height);
|
sl@0
|
1133 |
}
|
sl@0
|
1134 |
|
sl@0
|
1135 |
pixels = ((M3Gubyte *)m3gMapObject(M3G_INTERFACE(image), image->data));
|
sl@0
|
1136 |
|
sl@0
|
1137 |
if (image->paletteBytes == 0) {
|
sl@0
|
1138 |
if (bpp == 1) {
|
sl@0
|
1139 |
data = pixels[x + y * image->width];
|
sl@0
|
1140 |
}
|
sl@0
|
1141 |
else if (bpp == 2) {
|
sl@0
|
1142 |
data = ((M3Gushort *)pixels)[x + y * image->width];
|
sl@0
|
1143 |
}
|
sl@0
|
1144 |
else {
|
sl@0
|
1145 |
data = ((M3Guint *)pixels)[x + y * image->width];
|
sl@0
|
1146 |
}
|
sl@0
|
1147 |
}
|
sl@0
|
1148 |
else {
|
sl@0
|
1149 |
M3Guint *palette;
|
sl@0
|
1150 |
palette = (M3Guint *)pixels;
|
sl@0
|
1151 |
pixels += image->paletteBytes;
|
sl@0
|
1152 |
|
sl@0
|
1153 |
data = palette[pixels[x + y * image->width]];
|
sl@0
|
1154 |
}
|
sl@0
|
1155 |
|
sl@0
|
1156 |
m3gUnmapObject(M3G_INTERFACE(image), image->data);
|
sl@0
|
1157 |
|
sl@0
|
1158 |
switch (image->internalFormat) {
|
sl@0
|
1159 |
|
sl@0
|
1160 |
case M3G_A8:
|
sl@0
|
1161 |
alpha = data;
|
sl@0
|
1162 |
break;
|
sl@0
|
1163 |
case M3G_LA8:
|
sl@0
|
1164 |
alpha = data >> 8;
|
sl@0
|
1165 |
break;
|
sl@0
|
1166 |
case M3G_RGBA8:
|
sl@0
|
1167 |
alpha = data >> 24;
|
sl@0
|
1168 |
break;
|
sl@0
|
1169 |
default:
|
sl@0
|
1170 |
/* Should never be here!! */
|
sl@0
|
1171 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
1172 |
}
|
sl@0
|
1173 |
|
sl@0
|
1174 |
return alpha;
|
sl@0
|
1175 |
}
|
sl@0
|
1176 |
|
sl@0
|
1177 |
/*!
|
sl@0
|
1178 |
* \internal
|
sl@0
|
1179 |
* \brief Computes the scanline stride of an image
|
sl@0
|
1180 |
*/
|
sl@0
|
1181 |
static M3Gsizei m3gGetImageStride(const Image *img)
|
sl@0
|
1182 |
{
|
sl@0
|
1183 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1184 |
return img->width * m3gBytesPerPixel(img->internalFormat);
|
sl@0
|
1185 |
}
|
sl@0
|
1186 |
|
sl@0
|
1187 |
/*----------------------------------------------------------------------
|
sl@0
|
1188 |
* Virtual function table
|
sl@0
|
1189 |
*--------------------------------------------------------------------*/
|
sl@0
|
1190 |
|
sl@0
|
1191 |
static const ObjectVFTable m3gvf_Image = {
|
sl@0
|
1192 |
m3gObjectApplyAnimation,
|
sl@0
|
1193 |
m3gObjectIsCompatible,
|
sl@0
|
1194 |
m3gObjectUpdateProperty,
|
sl@0
|
1195 |
m3gObjectDoGetReferences,
|
sl@0
|
1196 |
m3gObjectFindID,
|
sl@0
|
1197 |
m3gImageDuplicate,
|
sl@0
|
1198 |
m3gDestroyImage
|
sl@0
|
1199 |
};
|
sl@0
|
1200 |
|
sl@0
|
1201 |
|
sl@0
|
1202 |
/*----------------------------------------------------------------------
|
sl@0
|
1203 |
* Public API functions
|
sl@0
|
1204 |
*--------------------------------------------------------------------*/
|
sl@0
|
1205 |
|
sl@0
|
1206 |
/*!
|
sl@0
|
1207 |
* \brief Creates a new Image
|
sl@0
|
1208 |
*
|
sl@0
|
1209 |
* \param interface M3G interface
|
sl@0
|
1210 |
* \param srcFormat source format
|
sl@0
|
1211 |
* \param width width in pixels
|
sl@0
|
1212 |
* \param height height in pixels
|
sl@0
|
1213 |
* \param flags creation flags; a combination of
|
sl@0
|
1214 |
* M3G_DYNAMIC, M3G_STATIC,
|
sl@0
|
1215 |
* M3G_RENDERING_TARGET, and M3G_PALETTED
|
sl@0
|
1216 |
* \retval Image new Image object
|
sl@0
|
1217 |
* \retval NULL Image creating failed
|
sl@0
|
1218 |
*/
|
sl@0
|
1219 |
M3G_API M3GImage m3gCreateImage(/*@dependent@*/ M3GInterface interface,
|
sl@0
|
1220 |
M3GImageFormat srcFormat,
|
sl@0
|
1221 |
M3Gint width, M3Gint height,
|
sl@0
|
1222 |
M3Gbitmask flags)
|
sl@0
|
1223 |
{
|
sl@0
|
1224 |
Interface *m3g = (Interface *) interface;
|
sl@0
|
1225 |
M3G_VALIDATE_INTERFACE(m3g);
|
sl@0
|
1226 |
|
sl@0
|
1227 |
/* Check errors */
|
sl@0
|
1228 |
|
sl@0
|
1229 |
if (width <= 0 || height <= 0) {
|
sl@0
|
1230 |
m3gRaiseError(m3g, M3G_INVALID_VALUE);
|
sl@0
|
1231 |
return NULL;
|
sl@0
|
1232 |
}
|
sl@0
|
1233 |
|
sl@0
|
1234 |
if (!m3gInRange(srcFormat, M3G_ALPHA, M3G_RGBA)) {
|
sl@0
|
1235 |
m3gRaiseError(m3g, M3G_INVALID_ENUM);
|
sl@0
|
1236 |
return NULL;
|
sl@0
|
1237 |
}
|
sl@0
|
1238 |
|
sl@0
|
1239 |
/* Parameters OK; allocate and initialize the object */
|
sl@0
|
1240 |
|
sl@0
|
1241 |
{
|
sl@0
|
1242 |
Image *img = m3gAllocZ(m3g, sizeof(Image));
|
sl@0
|
1243 |
if (img == NULL) {
|
sl@0
|
1244 |
return NULL;
|
sl@0
|
1245 |
}
|
sl@0
|
1246 |
|
sl@0
|
1247 |
/* Clean up and set flags */
|
sl@0
|
1248 |
|
sl@0
|
1249 |
M3G_LOG3(M3G_LOG_IMAGES, "Image 0x%08X is %d x %d",
|
sl@0
|
1250 |
(unsigned) img, width, height);
|
sl@0
|
1251 |
|
sl@0
|
1252 |
flags |= M3G_DYNAMIC; /* the default */
|
sl@0
|
1253 |
|
sl@0
|
1254 |
if (flags & M3G_STATIC) {
|
sl@0
|
1255 |
M3G_LOG(M3G_LOG_IMAGES, ", immutable");
|
sl@0
|
1256 |
flags &= ~M3G_DYNAMIC;
|
sl@0
|
1257 |
}
|
sl@0
|
1258 |
if (flags & M3G_RENDERING_TARGET) {
|
sl@0
|
1259 |
M3G_LOG(M3G_LOG_IMAGES, ", rendertarget");
|
sl@0
|
1260 |
flags |= M3G_DYNAMIC;
|
sl@0
|
1261 |
}
|
sl@0
|
1262 |
if (flags & M3G_PALETTED) {
|
sl@0
|
1263 |
M3G_LOG(M3G_LOG_IMAGES, ", paletted");
|
sl@0
|
1264 |
}
|
sl@0
|
1265 |
img->flags = flags;
|
sl@0
|
1266 |
|
sl@0
|
1267 |
M3G_LOG(M3G_LOG_IMAGES, "\n");
|
sl@0
|
1268 |
|
sl@0
|
1269 |
{
|
sl@0
|
1270 |
/* Allocate pixel & palette data; the palette is stored at
|
sl@0
|
1271 |
* the beginning of the pixel data chunk */
|
sl@0
|
1272 |
|
sl@0
|
1273 |
M3Gbool paletted = ((img->flags & M3G_PALETTED) != 0)
|
sl@0
|
1274 |
&& m3gSupportedPaletteFormat(srcFormat);
|
sl@0
|
1275 |
M3GPixelFormat internalFormat = getInternalFormat(srcFormat,
|
sl@0
|
1276 |
paletted);
|
sl@0
|
1277 |
M3Guint bpp = m3gBytesPerPixel(internalFormat);
|
sl@0
|
1278 |
M3Guint pixelBytes = width * height * bpp;
|
sl@0
|
1279 |
|
sl@0
|
1280 |
if ((img->flags & M3G_PALETTED) != 0 && !paletted) {
|
sl@0
|
1281 |
M3G_LOG(M3G_LOG_WARNINGS|M3G_LOG_IMAGES,
|
sl@0
|
1282 |
"Warning: Unsupported paletted format\n");
|
sl@0
|
1283 |
}
|
sl@0
|
1284 |
|
sl@0
|
1285 |
/* The palette will always have 256 elements and one byte
|
sl@0
|
1286 |
* per color component (except padded 32-bit for NGL) */
|
sl@0
|
1287 |
|
sl@0
|
1288 |
if (paletted) {
|
sl@0
|
1289 |
img->paletteBytes =
|
sl@0
|
1290 |
# if defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
1291 |
256 * 4;
|
sl@0
|
1292 |
# else
|
sl@0
|
1293 |
256 * m3gBytesPerPixel(m3gPixelFormat(srcFormat));
|
sl@0
|
1294 |
# endif
|
sl@0
|
1295 |
}
|
sl@0
|
1296 |
|
sl@0
|
1297 |
/* Set up the rest of the image parameters */
|
sl@0
|
1298 |
|
sl@0
|
1299 |
img->width = width;
|
sl@0
|
1300 |
img->height = height;
|
sl@0
|
1301 |
img->format = srcFormat;
|
sl@0
|
1302 |
img->internalFormat = internalFormat;
|
sl@0
|
1303 |
img->glFormat = m3gGetGLFormat(internalFormat);
|
sl@0
|
1304 |
|
sl@0
|
1305 |
M3G_LOG1(M3G_LOG_IMAGES, "Image data %d bytes\n",
|
sl@0
|
1306 |
pixelBytes + img->paletteBytes);
|
sl@0
|
1307 |
|
sl@0
|
1308 |
/* Allocate the image memory */
|
sl@0
|
1309 |
|
sl@0
|
1310 |
img->data = m3gAllocObject(m3g, pixelBytes + img->paletteBytes);
|
sl@0
|
1311 |
if (img->data == 0) {
|
sl@0
|
1312 |
m3gFree(m3g, img);
|
sl@0
|
1313 |
return NULL;
|
sl@0
|
1314 |
}
|
sl@0
|
1315 |
|
sl@0
|
1316 |
#ifdef M3G_ENABLE_GLES_RESOURCE_HANDLING
|
sl@0
|
1317 |
/* If GLES resource freeing (see function m3gFreeGLESResources)
|
sl@0
|
1318 |
is enabled, the GL texture might get deleted at any point, so
|
sl@0
|
1319 |
a copy of the texture data has to be always kept in memory. */
|
sl@0
|
1320 |
img->pinned = M3G_TRUE;
|
sl@0
|
1321 |
#else
|
sl@0
|
1322 |
/* Lock the image data in memory if the image is dynamic,
|
sl@0
|
1323 |
* or the format has alpha information; otherwise, we'll
|
sl@0
|
1324 |
* be able to get rid of an extra copy when generating a
|
sl@0
|
1325 |
* power-of-two version or uploading to OpenGL */
|
sl@0
|
1326 |
|
sl@0
|
1327 |
if ((img->flags & M3G_DYNAMIC) != 0
|
sl@0
|
1328 |
|| (img->format != M3G_RGB &&
|
sl@0
|
1329 |
img->format != M3G_LUMINANCE)) {
|
sl@0
|
1330 |
img->pinned = M3G_TRUE;
|
sl@0
|
1331 |
}
|
sl@0
|
1332 |
#endif
|
sl@0
|
1333 |
/* If the image can be used as a rendering target, clear
|
sl@0
|
1334 |
* to opaque white by default */
|
sl@0
|
1335 |
|
sl@0
|
1336 |
if ((img->flags & M3G_RENDERING_TARGET) != 0) {
|
sl@0
|
1337 |
M3Gubyte *pixels = ((M3Gubyte *)m3gMapObject(m3g, img->data))
|
sl@0
|
1338 |
+ img->paletteBytes;
|
sl@0
|
1339 |
m3gFill(pixels, (size_t) pixelBytes, -1);
|
sl@0
|
1340 |
m3gUnmapObject(m3g, img->data);
|
sl@0
|
1341 |
}
|
sl@0
|
1342 |
|
sl@0
|
1343 |
/* Check for "special" images that can't be used as
|
sl@0
|
1344 |
* textures without some extra trickery */
|
sl@0
|
1345 |
|
sl@0
|
1346 |
if (!m3gIsPowerOfTwo((M3Guint) width) ||
|
sl@0
|
1347 |
!m3gIsPowerOfTwo((M3Guint) height)) {
|
sl@0
|
1348 |
img->special |= IMG_NPOT;
|
sl@0
|
1349 |
}
|
sl@0
|
1350 |
else {
|
sl@0
|
1351 |
img->powerOfTwo = img;
|
sl@0
|
1352 |
}
|
sl@0
|
1353 |
|
sl@0
|
1354 |
if (width > M3G_MAX_TEXTURE_DIMENSION ||
|
sl@0
|
1355 |
height > M3G_MAX_TEXTURE_DIMENSION) {
|
sl@0
|
1356 |
img->special |= IMG_LARGE;
|
sl@0
|
1357 |
}
|
sl@0
|
1358 |
}
|
sl@0
|
1359 |
|
sl@0
|
1360 |
/* Call base class constructor (can not fail) and return */
|
sl@0
|
1361 |
m3gInitObject(&img->object, m3g, M3G_CLASS_IMAGE);
|
sl@0
|
1362 |
|
sl@0
|
1363 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1364 |
return (M3GImage) img;
|
sl@0
|
1365 |
}
|
sl@0
|
1366 |
}
|
sl@0
|
1367 |
|
sl@0
|
1368 |
/*!
|
sl@0
|
1369 |
* \brief Prevents further modifications to an image
|
sl@0
|
1370 |
*
|
sl@0
|
1371 |
* Essentially, this changes the default M3G_DYNAMIC flag to
|
sl@0
|
1372 |
* M3G_STATIC; this allows the implementation to make memory and
|
sl@0
|
1373 |
* performance optimizations not possible for dynamically modified
|
sl@0
|
1374 |
* images.
|
sl@0
|
1375 |
*/
|
sl@0
|
1376 |
M3G_API void m3gCommitImage(M3GImage hImage)
|
sl@0
|
1377 |
{
|
sl@0
|
1378 |
Image *image = (Image *) hImage;
|
sl@0
|
1379 |
M3Gbitmask flags;
|
sl@0
|
1380 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
1381 |
|
sl@0
|
1382 |
flags = image->flags;
|
sl@0
|
1383 |
flags &= ~(M3G_DYNAMIC|M3G_RENDERING_TARGET);
|
sl@0
|
1384 |
flags |= M3G_STATIC;
|
sl@0
|
1385 |
|
sl@0
|
1386 |
image->flags = flags;
|
sl@0
|
1387 |
|
sl@0
|
1388 |
#ifndef M3G_ENABLE_GLES_RESOURCE_HANDLING
|
sl@0
|
1389 |
/* If the image format has no alpha information, we can discard
|
sl@0
|
1390 |
* the image data under suitable conditions */
|
sl@0
|
1391 |
|
sl@0
|
1392 |
if (image->format == M3G_RGB || image->format == M3G_LUMINANCE) {
|
sl@0
|
1393 |
image->pinned = M3G_FALSE;
|
sl@0
|
1394 |
}
|
sl@0
|
1395 |
#endif
|
sl@0
|
1396 |
M3G_LOG1(M3G_LOG_IMAGES, "Image 0x%08X made immutable\n",
|
sl@0
|
1397 |
(unsigned) image);
|
sl@0
|
1398 |
}
|
sl@0
|
1399 |
|
sl@0
|
1400 |
/*!
|
sl@0
|
1401 |
* \brief Check if image is mutable.
|
sl@0
|
1402 |
*
|
sl@0
|
1403 |
* \param hImage Image object
|
sl@0
|
1404 |
* \retval M3G_TRUE image is mutable
|
sl@0
|
1405 |
* \retval M3G_FALSE image is immutable
|
sl@0
|
1406 |
*/
|
sl@0
|
1407 |
M3G_API M3Gbool m3gIsMutable(M3GImage hImage)
|
sl@0
|
1408 |
{
|
sl@0
|
1409 |
Image *image = (Image *) hImage;
|
sl@0
|
1410 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
1411 |
return ((image->flags & M3G_DYNAMIC) != 0);
|
sl@0
|
1412 |
}
|
sl@0
|
1413 |
|
sl@0
|
1414 |
/*!
|
sl@0
|
1415 |
* \brief Gets image format as JSR-184 constant
|
sl@0
|
1416 |
*
|
sl@0
|
1417 |
* \param hImage Image object
|
sl@0
|
1418 |
* \return JSR-184 format
|
sl@0
|
1419 |
*/
|
sl@0
|
1420 |
M3G_API M3GImageFormat m3gGetFormat(M3GImage hImage)
|
sl@0
|
1421 |
{
|
sl@0
|
1422 |
Image *image = (Image *) hImage;
|
sl@0
|
1423 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
1424 |
return image->format;
|
sl@0
|
1425 |
}
|
sl@0
|
1426 |
|
sl@0
|
1427 |
/*!
|
sl@0
|
1428 |
* \brief Gets image width
|
sl@0
|
1429 |
*
|
sl@0
|
1430 |
* \param hImage Image object
|
sl@0
|
1431 |
* \return width in pixels
|
sl@0
|
1432 |
*/
|
sl@0
|
1433 |
M3G_API M3Gint m3gGetWidth(M3GImage hImage)
|
sl@0
|
1434 |
{
|
sl@0
|
1435 |
Image *image = (Image *) hImage;
|
sl@0
|
1436 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
1437 |
return image->width;
|
sl@0
|
1438 |
}
|
sl@0
|
1439 |
|
sl@0
|
1440 |
/*!
|
sl@0
|
1441 |
* \brief Gets image height
|
sl@0
|
1442 |
*
|
sl@0
|
1443 |
* \param hImage Image object
|
sl@0
|
1444 |
* \return height in pixels
|
sl@0
|
1445 |
*/
|
sl@0
|
1446 |
M3G_API M3Gint m3gGetHeight(M3GImage hImage)
|
sl@0
|
1447 |
{
|
sl@0
|
1448 |
Image *image = (Image *) hImage;
|
sl@0
|
1449 |
M3G_VALIDATE_OBJECT(image);
|
sl@0
|
1450 |
return image->height;
|
sl@0
|
1451 |
}
|
sl@0
|
1452 |
|
sl@0
|
1453 |
/*!
|
sl@0
|
1454 |
* \brief Converts a rectangle of pixels of src to dst as srcFormat to
|
sl@0
|
1455 |
* dstFormat conversion requires.
|
sl@0
|
1456 |
*
|
sl@0
|
1457 |
* \param srcFormat source format
|
sl@0
|
1458 |
* \param src source pixels
|
sl@0
|
1459 |
* \param srcStride source stride
|
sl@0
|
1460 |
* \param width width in pixels
|
sl@0
|
1461 |
* \param height height in pixels
|
sl@0
|
1462 |
* \param dstFormat destination format
|
sl@0
|
1463 |
* \param dst destination pixels
|
sl@0
|
1464 |
* \param dstStride destination stride
|
sl@0
|
1465 |
*/
|
sl@0
|
1466 |
static void m3gConvertPixelRect(
|
sl@0
|
1467 |
M3GPixelFormat srcFormat, const M3Gubyte *src, M3Gsizei srcStride,
|
sl@0
|
1468 |
M3Gsizei width, M3Gsizei height,
|
sl@0
|
1469 |
M3GPixelFormat dstFormat, M3Gubyte *dst, M3Gsizei dstStride)
|
sl@0
|
1470 |
{
|
sl@0
|
1471 |
/* Detect any fast path cases */
|
sl@0
|
1472 |
|
sl@0
|
1473 |
if ((srcFormat == M3G_BGRA8 || srcFormat == M3G_BGR8_32)
|
sl@0
|
1474 |
&& dstFormat == M3G_RGBA8) {
|
sl@0
|
1475 |
if (width > 2 && dstStride == width*4) {
|
sl@0
|
1476 |
|
sl@0
|
1477 |
const char endianTest[4] = { 1, 0, 0, 0 };
|
sl@0
|
1478 |
if ((*(const int *)endianTest) == 1) {
|
sl@0
|
1479 |
fastConvertBGRAToRGBA(src, srcStride, width, height, dst);
|
sl@0
|
1480 |
}
|
sl@0
|
1481 |
return;
|
sl@0
|
1482 |
}
|
sl@0
|
1483 |
}
|
sl@0
|
1484 |
|
sl@0
|
1485 |
/* No luck, do the generic conversion */
|
sl@0
|
1486 |
|
sl@0
|
1487 |
while (height-- > 0) {
|
sl@0
|
1488 |
m3gConvertPixels(srcFormat, src, dstFormat, dst, width);
|
sl@0
|
1489 |
src += srcStride;
|
sl@0
|
1490 |
dst += dstStride;
|
sl@0
|
1491 |
}
|
sl@0
|
1492 |
}
|
sl@0
|
1493 |
|
sl@0
|
1494 |
/*!
|
sl@0
|
1495 |
* \brief Sets the pixel data for an image
|
sl@0
|
1496 |
*
|
sl@0
|
1497 |
* \param hImage Image object
|
sl@0
|
1498 |
* \param srcPixels source pixels
|
sl@0
|
1499 |
*/
|
sl@0
|
1500 |
M3G_API void m3gSetImage(M3GImage hImage, const void *srcPixels)
|
sl@0
|
1501 |
{
|
sl@0
|
1502 |
Image *img = (Image *) hImage;
|
sl@0
|
1503 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1504 |
|
sl@0
|
1505 |
{
|
sl@0
|
1506 |
M3Gsizei bpp = m3gBytesPerPixel(m3gInputDataFormat(img));
|
sl@0
|
1507 |
m3gSetSubImage(hImage,
|
sl@0
|
1508 |
0, 0, img->width, img->height,
|
sl@0
|
1509 |
img->width * img->height * bpp, srcPixels);
|
sl@0
|
1510 |
}
|
sl@0
|
1511 |
}
|
sl@0
|
1512 |
|
sl@0
|
1513 |
/*!
|
sl@0
|
1514 |
* \brief Reads pixel data from an image
|
sl@0
|
1515 |
*
|
sl@0
|
1516 |
* \param hImage Image object
|
sl@0
|
1517 |
* \param pixels output buffer for pixels
|
sl@0
|
1518 |
*/
|
sl@0
|
1519 |
M3G_API void m3gGetImageARGB(M3GImage hImage, M3Guint *pixels)
|
sl@0
|
1520 |
{
|
sl@0
|
1521 |
Interface *m3g;
|
sl@0
|
1522 |
const Image *img = (const Image *) hImage;
|
sl@0
|
1523 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1524 |
m3g = M3G_INTERFACE(img);
|
sl@0
|
1525 |
|
sl@0
|
1526 |
if (!pixels) {
|
sl@0
|
1527 |
m3gRaiseError(m3g, M3G_NULL_POINTER);
|
sl@0
|
1528 |
return;
|
sl@0
|
1529 |
}
|
sl@0
|
1530 |
|
sl@0
|
1531 |
if (img->data) {
|
sl@0
|
1532 |
const M3Gubyte *src = (const M3Gubyte*) m3gMapObject(m3g, img->data);
|
sl@0
|
1533 |
convertToARGB(img->internalFormat, src,
|
sl@0
|
1534 |
img->width * img->height,
|
sl@0
|
1535 |
pixels);
|
sl@0
|
1536 |
m3gUnmapObject(m3g, img->data);
|
sl@0
|
1537 |
}
|
sl@0
|
1538 |
}
|
sl@0
|
1539 |
|
sl@0
|
1540 |
/*!
|
sl@0
|
1541 |
* \brief Sets the palette for an image
|
sl@0
|
1542 |
*
|
sl@0
|
1543 |
* \param hImage Image object
|
sl@0
|
1544 |
* \param paletteLength length of the palette
|
sl@0
|
1545 |
* \param srcPalette palette data
|
sl@0
|
1546 |
*/
|
sl@0
|
1547 |
M3G_API void m3gSetImagePalette(M3GImage hImage,
|
sl@0
|
1548 |
M3Gint paletteLength,
|
sl@0
|
1549 |
const void *srcPalette)
|
sl@0
|
1550 |
{
|
sl@0
|
1551 |
Interface *m3g;
|
sl@0
|
1552 |
Image *img = (Image *) hImage;
|
sl@0
|
1553 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1554 |
m3g = M3G_INTERFACE(img);
|
sl@0
|
1555 |
|
sl@0
|
1556 |
/* Check for errors */
|
sl@0
|
1557 |
|
sl@0
|
1558 |
if (img->data == 0 || (img->flags & M3G_STATIC) != 0
|
sl@0
|
1559 |
|| (img->flags & M3G_PALETTED) == 0) {
|
sl@0
|
1560 |
M3G_ASSERT(!(img->flags & M3G_DYNAMIC));
|
sl@0
|
1561 |
m3gRaiseError(m3g, M3G_INVALID_OPERATION);
|
sl@0
|
1562 |
return;
|
sl@0
|
1563 |
}
|
sl@0
|
1564 |
if (srcPalette == NULL) {
|
sl@0
|
1565 |
m3gRaiseError(m3g, M3G_NULL_POINTER);
|
sl@0
|
1566 |
return;
|
sl@0
|
1567 |
}
|
sl@0
|
1568 |
if (!m3gInRange(paletteLength, 0, 256)) {
|
sl@0
|
1569 |
m3gRaiseError(m3g, M3G_INVALID_VALUE);
|
sl@0
|
1570 |
return;
|
sl@0
|
1571 |
}
|
sl@0
|
1572 |
|
sl@0
|
1573 |
/*
|
sl@0
|
1574 |
* Copy the palette data into the allocated palette (for natively
|
sl@0
|
1575 |
* supported paletted formats), or remap the existing image data
|
sl@0
|
1576 |
* using the supplied palette entries (for non-native formats)
|
sl@0
|
1577 |
*
|
sl@0
|
1578 |
* NOTE the latter is a one-time operation!
|
sl@0
|
1579 |
*/
|
sl@0
|
1580 |
if (img->paletteBytes > 0) {
|
sl@0
|
1581 |
M3Gubyte *palette = (M3Gubyte *)m3gMapObject(m3g, img->data);
|
sl@0
|
1582 |
# if defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
1583 |
m3gConvertPixels(m3gPixelFormat(img->format), srcPalette,
|
sl@0
|
1584 |
M3G_RGBA8, palette,
|
sl@0
|
1585 |
paletteLength);
|
sl@0
|
1586 |
# else
|
sl@0
|
1587 |
M3Gsizei bpp = m3gBytesPerPixel(m3gPixelFormat(img->format));
|
sl@0
|
1588 |
m3gCopy(palette, srcPalette, (size_t) paletteLength * bpp);
|
sl@0
|
1589 |
# endif
|
sl@0
|
1590 |
}
|
sl@0
|
1591 |
else {
|
sl@0
|
1592 |
M3Gint count = img->width * img->height;
|
sl@0
|
1593 |
M3Gubyte *pixel = (M3Gubyte*)m3gMapObject(m3g, img->data);
|
sl@0
|
1594 |
const M3Gubyte *bytePalette = (const M3Gubyte *) srcPalette;
|
sl@0
|
1595 |
|
sl@0
|
1596 |
/* We need to treat the input and internal formats as
|
sl@0
|
1597 |
* separate, as the internal storage may be padded to more
|
sl@0
|
1598 |
* bytes than there are color components */
|
sl@0
|
1599 |
|
sl@0
|
1600 |
M3GPixelFormat paletteFormat = m3gPixelFormat(img->format);
|
sl@0
|
1601 |
const int numComponents = m3gBytesPerPixel(paletteFormat);
|
sl@0
|
1602 |
M3GPixelFormat imgFormat = img->internalFormat;
|
sl@0
|
1603 |
const int imgBpp = m3gBytesPerPixel(imgFormat);
|
sl@0
|
1604 |
|
sl@0
|
1605 |
/* In most cases we can just copy the corresponding palette
|
sl@0
|
1606 |
* entry on top of each pixel based on the pixel intensity (R
|
sl@0
|
1607 |
* or L component), but special formats require a more
|
sl@0
|
1608 |
* complicated conversion. We just use the (slow) general
|
sl@0
|
1609 |
* conversion routine, as it already incorporates support for
|
sl@0
|
1610 |
* all formats. */
|
sl@0
|
1611 |
|
sl@0
|
1612 |
if (imgBpp >= numComponents) {
|
sl@0
|
1613 |
while (count--) {
|
sl@0
|
1614 |
int offset = (*pixel) * numComponents;
|
sl@0
|
1615 |
int c;
|
sl@0
|
1616 |
for (c = 0; c < numComponents; ++c) {
|
sl@0
|
1617 |
*pixel++ = bytePalette[offset + c];
|
sl@0
|
1618 |
}
|
sl@0
|
1619 |
while (c++ < imgBpp) { /* padding for e.g. 24-bit RGB */
|
sl@0
|
1620 |
*pixel++ = 0xFF;
|
sl@0
|
1621 |
}
|
sl@0
|
1622 |
}
|
sl@0
|
1623 |
}
|
sl@0
|
1624 |
else {
|
sl@0
|
1625 |
while (count--) {
|
sl@0
|
1626 |
int offset = (*pixel) * numComponents;
|
sl@0
|
1627 |
m3gConvertPixels(paletteFormat, &bytePalette[offset],
|
sl@0
|
1628 |
imgFormat, pixel,
|
sl@0
|
1629 |
1);
|
sl@0
|
1630 |
pixel += imgBpp;
|
sl@0
|
1631 |
}
|
sl@0
|
1632 |
}
|
sl@0
|
1633 |
}
|
sl@0
|
1634 |
m3gUnmapObject(m3g, img->data);
|
sl@0
|
1635 |
m3gInvalidateImage(img);
|
sl@0
|
1636 |
}
|
sl@0
|
1637 |
|
sl@0
|
1638 |
/*!
|
sl@0
|
1639 |
* \brief Sets a scanline of an image
|
sl@0
|
1640 |
*
|
sl@0
|
1641 |
* \param hImage Image object
|
sl@0
|
1642 |
* \param line scanline
|
sl@0
|
1643 |
* \param trueAlpha M3G_TRUE if the source image has an alpha channel,
|
sl@0
|
1644 |
* M3G_FALSE if it should come from the RGB values;
|
sl@0
|
1645 |
* this only matters for alpha-only destination images
|
sl@0
|
1646 |
* \param pixels souce pixels
|
sl@0
|
1647 |
*/
|
sl@0
|
1648 |
M3G_API void m3gSetImageScanline(M3GImage hImage,
|
sl@0
|
1649 |
M3Gint line,
|
sl@0
|
1650 |
M3Gbool trueAlpha,
|
sl@0
|
1651 |
const M3Guint *pixels)
|
sl@0
|
1652 |
{
|
sl@0
|
1653 |
Image *img = (Image *) hImage;
|
sl@0
|
1654 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1655 |
|
sl@0
|
1656 |
if (img->data == 0 || (img->flags & M3G_STATIC) != 0
|
sl@0
|
1657 |
|| img->paletteBytes != 0) {
|
sl@0
|
1658 |
m3gRaiseError(M3G_INTERFACE(img), M3G_INVALID_OPERATION);
|
sl@0
|
1659 |
return;
|
sl@0
|
1660 |
}
|
sl@0
|
1661 |
|
sl@0
|
1662 |
{
|
sl@0
|
1663 |
Interface *m3g = M3G_INTERFACE(img);
|
sl@0
|
1664 |
M3Gint stride = img->width * m3gBytesPerPixel(img->internalFormat);
|
sl@0
|
1665 |
M3Gubyte *dst = ((M3Gubyte *) m3gMapObject(m3g, img->data))
|
sl@0
|
1666 |
+ img->paletteBytes;
|
sl@0
|
1667 |
|
sl@0
|
1668 |
#ifdef M3G_NGL_TEXTURE_API
|
sl@0
|
1669 |
/* For RGB images without alpha channel, source alpha is
|
sl@0
|
1670 |
* forced to 0xff. */
|
sl@0
|
1671 |
|
sl@0
|
1672 |
if (img->format == M3G_RGB) {
|
sl@0
|
1673 |
M3Gint i;
|
sl@0
|
1674 |
M3Guint argb, *dst;
|
sl@0
|
1675 |
|
sl@0
|
1676 |
dst = (M3Guint *) pixels;
|
sl@0
|
1677 |
|
sl@0
|
1678 |
for (i = 0; i < img->width; i++) {
|
sl@0
|
1679 |
argb = *dst | 0xff000000;
|
sl@0
|
1680 |
*dst++ = argb;
|
sl@0
|
1681 |
}
|
sl@0
|
1682 |
}
|
sl@0
|
1683 |
#endif
|
sl@0
|
1684 |
|
sl@0
|
1685 |
/* Note that an alpha-only destination format is faked for
|
sl@0
|
1686 |
* luminance if the source contained no true alpha data; alpha
|
sl@0
|
1687 |
* is then inferred from the RGB values instead */
|
sl@0
|
1688 |
|
sl@0
|
1689 |
convertFromARGB(pixels,
|
sl@0
|
1690 |
img->width,
|
sl@0
|
1691 |
(img->internalFormat == M3G_A8 && !trueAlpha) ? M3G_L8 : img->internalFormat,
|
sl@0
|
1692 |
dst + line * stride);
|
sl@0
|
1693 |
|
sl@0
|
1694 |
m3gUnmapObject(m3g, img->data);
|
sl@0
|
1695 |
m3gInvalidateImage(img);
|
sl@0
|
1696 |
}
|
sl@0
|
1697 |
}
|
sl@0
|
1698 |
|
sl@0
|
1699 |
/*!
|
sl@0
|
1700 |
* \brief Sets a rectangular subregion of an image
|
sl@0
|
1701 |
*
|
sl@0
|
1702 |
* \param hImage Image object
|
sl@0
|
1703 |
* \param x x-coordinate in destination image
|
sl@0
|
1704 |
* \param y y-coordinate in destination image
|
sl@0
|
1705 |
* \param width width of source pixels
|
sl@0
|
1706 |
* \param height height of source pixels
|
sl@0
|
1707 |
* \param length length of source data, in bytes
|
sl@0
|
1708 |
* \param pixels source pixels
|
sl@0
|
1709 |
*/
|
sl@0
|
1710 |
M3G_API void m3gSetSubImage(M3GImage hImage,
|
sl@0
|
1711 |
M3Gint x, M3Gint y,
|
sl@0
|
1712 |
M3Gint width, M3Gint height,
|
sl@0
|
1713 |
M3Gint length, const void *pixels)
|
sl@0
|
1714 |
{
|
sl@0
|
1715 |
Interface *m3g;
|
sl@0
|
1716 |
Image *img = (Image *) hImage;
|
sl@0
|
1717 |
|
sl@0
|
1718 |
M3GPixelFormat srcFormat;
|
sl@0
|
1719 |
M3Gsizei srcBpp;
|
sl@0
|
1720 |
|
sl@0
|
1721 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1722 |
m3g = M3G_INTERFACE(img);
|
sl@0
|
1723 |
|
sl@0
|
1724 |
/* Check for errors */
|
sl@0
|
1725 |
|
sl@0
|
1726 |
if (img->data == 0 || (img->flags & M3G_STATIC) != 0) {
|
sl@0
|
1727 |
M3G_ASSERT(!(img->flags & M3G_DYNAMIC));
|
sl@0
|
1728 |
m3gRaiseError(m3g, M3G_INVALID_OPERATION);
|
sl@0
|
1729 |
return;
|
sl@0
|
1730 |
}
|
sl@0
|
1731 |
if (pixels == NULL) {
|
sl@0
|
1732 |
m3gRaiseError(m3g, M3G_INVALID_VALUE);
|
sl@0
|
1733 |
return;
|
sl@0
|
1734 |
}
|
sl@0
|
1735 |
if (x < 0 || y < 0 || width <= 0 || height <= 0
|
sl@0
|
1736 |
|| x+width > img->width || y+height > img->height) {
|
sl@0
|
1737 |
m3gRaiseError(m3g, M3G_INVALID_VALUE);
|
sl@0
|
1738 |
return;
|
sl@0
|
1739 |
}
|
sl@0
|
1740 |
|
sl@0
|
1741 |
srcFormat = m3gInputDataFormat(img);
|
sl@0
|
1742 |
srcBpp = m3gBytesPerPixel(srcFormat);
|
sl@0
|
1743 |
|
sl@0
|
1744 |
if (length < width * height * srcBpp) {
|
sl@0
|
1745 |
m3gRaiseError(m3g, M3G_INVALID_VALUE);
|
sl@0
|
1746 |
return;
|
sl@0
|
1747 |
}
|
sl@0
|
1748 |
|
sl@0
|
1749 |
/* Copy the image data, doing a conversion if the input format
|
sl@0
|
1750 |
* does not match the internal storage format */
|
sl@0
|
1751 |
{
|
sl@0
|
1752 |
const M3Gubyte *srcPixels = (const M3Gubyte*) pixels;
|
sl@0
|
1753 |
M3Gsizei srcStride = width * srcBpp;
|
sl@0
|
1754 |
|
sl@0
|
1755 |
M3GPixelFormat dstFormat = img->internalFormat;
|
sl@0
|
1756 |
M3Gsizei dstBpp = m3gBytesPerPixel(dstFormat);
|
sl@0
|
1757 |
M3Gsizei dstStride = img->width * dstBpp;
|
sl@0
|
1758 |
M3Gubyte *dstPixels =
|
sl@0
|
1759 |
((M3Gubyte *)m3gMapObject(m3g, img->data))
|
sl@0
|
1760 |
+ img->paletteBytes
|
sl@0
|
1761 |
+ y * dstStride + x * dstBpp;
|
sl@0
|
1762 |
|
sl@0
|
1763 |
M3Gint numLines = height, numPixels = width;
|
sl@0
|
1764 |
M3Gbool paletted = (img->flags & M3G_PALETTED) != 0;
|
sl@0
|
1765 |
|
sl@0
|
1766 |
/* Optimize the copy for full image width */
|
sl@0
|
1767 |
|
sl@0
|
1768 |
if (width == img->width) {
|
sl@0
|
1769 |
numLines = 1;
|
sl@0
|
1770 |
numPixels = width * height;
|
sl@0
|
1771 |
}
|
sl@0
|
1772 |
|
sl@0
|
1773 |
/* Copy a scanline at a time, converting as necessary */
|
sl@0
|
1774 |
|
sl@0
|
1775 |
while (numLines-- > 0) {
|
sl@0
|
1776 |
|
sl@0
|
1777 |
/* Matching pixel formats are just copied without
|
sl@0
|
1778 |
* conversion, and all internally supported paletted
|
sl@0
|
1779 |
* formats match each other physically, so they can be
|
sl@0
|
1780 |
* copied as well */
|
sl@0
|
1781 |
|
sl@0
|
1782 |
if (dstFormat == srcFormat || img->paletteBytes > 0) {
|
sl@0
|
1783 |
m3gCopy(dstPixels, srcPixels, numPixels * dstBpp);
|
sl@0
|
1784 |
}
|
sl@0
|
1785 |
else {
|
sl@0
|
1786 |
if (!paletted) {
|
sl@0
|
1787 |
|
sl@0
|
1788 |
/* Ordinary conversion into an internal format
|
sl@0
|
1789 |
* that is encoded differently from the external
|
sl@0
|
1790 |
* format; can not be a paletted image */
|
sl@0
|
1791 |
|
sl@0
|
1792 |
M3G_ASSERT((img->flags & M3G_PALETTED) == 0);
|
sl@0
|
1793 |
m3gConvertPixels(srcFormat, srcPixels,
|
sl@0
|
1794 |
dstFormat, dstPixels,
|
sl@0
|
1795 |
numPixels);
|
sl@0
|
1796 |
}
|
sl@0
|
1797 |
else {
|
sl@0
|
1798 |
M3G_ASSERT(!m3gSupportedPaletteFormat(img->format));
|
sl@0
|
1799 |
|
sl@0
|
1800 |
/* Palette indices for one-byte-per-pixel formats
|
sl@0
|
1801 |
* are just copied in and mapped to actual values
|
sl@0
|
1802 |
* later; multibyte paletted formats require a
|
sl@0
|
1803 |
* conversion into LA, RGB, or RGBA format
|
sl@0
|
1804 |
* intensity levels temporarily before remapping
|
sl@0
|
1805 |
* to actual colors in m3gSetImagePalette */
|
sl@0
|
1806 |
|
sl@0
|
1807 |
if (dstBpp == 1) {
|
sl@0
|
1808 |
m3gCopy(dstPixels, srcPixels, numPixels);
|
sl@0
|
1809 |
}
|
sl@0
|
1810 |
else {
|
sl@0
|
1811 |
m3gConvertPixels(M3G_L8, srcPixels,
|
sl@0
|
1812 |
dstFormat, dstPixels,
|
sl@0
|
1813 |
numPixels);
|
sl@0
|
1814 |
}
|
sl@0
|
1815 |
}
|
sl@0
|
1816 |
}
|
sl@0
|
1817 |
|
sl@0
|
1818 |
srcPixels += srcStride;
|
sl@0
|
1819 |
dstPixels += dstStride;
|
sl@0
|
1820 |
}
|
sl@0
|
1821 |
|
sl@0
|
1822 |
/* Release the image data and invalidate mipmap levels */
|
sl@0
|
1823 |
|
sl@0
|
1824 |
m3gUnmapObject(m3g, img->data);
|
sl@0
|
1825 |
m3gInvalidateImage(img);
|
sl@0
|
1826 |
}
|
sl@0
|
1827 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1828 |
}
|
sl@0
|
1829 |
|
sl@0
|
1830 |
#undef SPAN_BUFFER_SIZE
|
sl@0
|
1831 |
|