4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2012 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 /* This file is included multiple times with different definitions for
25 the component_type type (either guint8 or guint16). The code ends
26 up exactly the same for both but we only want to end up hitting the
27 16-bit path when one of the types in the conversion is > 8 bits per
30 /* Unpacking to RGBA */
32 #define UNPACK_1(b) ((b) * ((1 << (sizeof (component_type) * 8)) - 1))
33 #define UNPACK_2(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
35 #define UNPACK_4(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
37 #define UNPACK_5(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
39 #define UNPACK_6(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
41 #define UNPACK_10(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
45 G_PASTE (_cogl_unpack_a_8_, component_type) (const guint8 *src,
54 dst[3] = UNPACK_BYTE (*src);
61 G_PASTE (_cogl_unpack_g_8_, component_type) (const guint8 *src,
65 /* FIXME: I'm not sure if this is right. It looks like Nvidia and
66 Mesa handle luminance textures differently. Maybe we should
67 consider just removing luminance textures for Cogl 2.0 because
68 they have been removed in GL 3.0 */
71 component_type v = UNPACK_BYTE (src[0]);
75 dst[3] = UNPACK_BYTE (255);
82 G_PASTE (_cogl_unpack_rgb_888_, component_type) (const guint8 *src,
88 dst[0] = UNPACK_BYTE (src[0]);
89 dst[1] = UNPACK_BYTE (src[1]);
90 dst[2] = UNPACK_BYTE (src[2]);
91 dst[3] = UNPACK_BYTE (255);
98 G_PASTE (_cogl_unpack_bgr_888_, component_type) (const guint8 *src,
104 dst[0] = UNPACK_BYTE (src[2]);
105 dst[1] = UNPACK_BYTE (src[1]);
106 dst[2] = UNPACK_BYTE (src[0]);
107 dst[3] = UNPACK_BYTE (255);
114 G_PASTE (_cogl_unpack_bgra_8888_, component_type) (const guint8 *src,
120 dst[0] = UNPACK_BYTE (src[2]);
121 dst[1] = UNPACK_BYTE (src[1]);
122 dst[2] = UNPACK_BYTE (src[0]);
123 dst[3] = UNPACK_BYTE (src[3]);
130 G_PASTE (_cogl_unpack_argb_8888_, component_type) (const guint8 *src,
136 dst[0] = UNPACK_BYTE (src[1]);
137 dst[1] = UNPACK_BYTE (src[2]);
138 dst[2] = UNPACK_BYTE (src[3]);
139 dst[3] = UNPACK_BYTE (src[0]);
146 G_PASTE (_cogl_unpack_abgr_8888_, component_type) (const guint8 *src,
152 dst[0] = UNPACK_BYTE (src[3]);
153 dst[1] = UNPACK_BYTE (src[2]);
154 dst[2] = UNPACK_BYTE (src[1]);
155 dst[3] = UNPACK_BYTE (src[0]);
162 G_PASTE (_cogl_unpack_rgba_8888_, component_type) (const guint8 *src,
168 dst[0] = UNPACK_BYTE (src[0]);
169 dst[1] = UNPACK_BYTE (src[1]);
170 dst[2] = UNPACK_BYTE (src[2]);
171 dst[3] = UNPACK_BYTE (src[3]);
178 G_PASTE (_cogl_unpack_rgb_565_, component_type) (const guint8 *src,
184 guint16 v = *(const guint16 *) src;
186 dst[0] = UNPACK_5 (v >> 11);
187 dst[1] = UNPACK_6 ((v >> 5) & 63);
188 dst[2] = UNPACK_5 (v & 31);
189 dst[3] = UNPACK_BYTE (255);
196 G_PASTE (_cogl_unpack_rgba_4444_, component_type) (const guint8 *src,
202 guint16 v = *(const guint16 *) src;
204 dst[0] = UNPACK_4 (v >> 12);
205 dst[1] = UNPACK_4 ((v >> 8) & 15);
206 dst[2] = UNPACK_4 ((v >> 4) & 15);
207 dst[3] = UNPACK_4 (v & 15);
214 G_PASTE (_cogl_unpack_rgba_5551_, component_type) (const guint8 *src,
220 guint16 v = *(const guint16 *) src;
222 dst[0] = UNPACK_5 (v >> 11);
223 dst[1] = UNPACK_5 ((v >> 6) & 31);
224 dst[2] = UNPACK_5 ((v >> 1) & 31);
225 dst[3] = UNPACK_1 (v & 1);
232 G_PASTE (_cogl_unpack_rgba_1010102_, component_type) (const guint8 *src,
238 guint32 v = *(const guint32 *) src;
240 dst[0] = UNPACK_10 (v >> 22);
241 dst[1] = UNPACK_10 ((v >> 12) & 1023);
242 dst[2] = UNPACK_10 ((v >> 2) & 1023);
243 dst[3] = UNPACK_2 (v & 3);
250 G_PASTE (_cogl_unpack_bgra_1010102_, component_type) (const guint8 *src,
256 guint32 v = *(const guint32 *) src;
258 dst[2] = UNPACK_10 (v >> 22);
259 dst[1] = UNPACK_10 ((v >> 12) & 1023);
260 dst[0] = UNPACK_10 ((v >> 2) & 1023);
261 dst[3] = UNPACK_2 (v & 3);
268 G_PASTE (_cogl_unpack_argb_2101010_, component_type) (const guint8 *src,
274 guint32 v = *(const guint32 *) src;
276 dst[3] = UNPACK_2 (v >> 30);
277 dst[0] = UNPACK_10 ((v >> 20) & 1023);
278 dst[1] = UNPACK_10 ((v >> 10) & 1023);
279 dst[2] = UNPACK_10 (v & 1023);
286 G_PASTE (_cogl_unpack_abgr_2101010_, component_type) (const guint8 *src,
292 guint32 v = *(const guint32 *) src;
294 dst[3] = UNPACK_2 (v >> 30);
295 dst[2] = UNPACK_10 ((v >> 20) & 1023);
296 dst[1] = UNPACK_10 ((v >> 10) & 1023);
297 dst[0] = UNPACK_10 (v & 1023);
311 G_PASTE (_cogl_unpack_, component_type) (CoglPixelFormat format,
318 case COGL_PIXEL_FORMAT_A_8:
319 G_PASTE (_cogl_unpack_a_8_, component_type) (src, dst, width);
321 case COGL_PIXEL_FORMAT_G_8:
322 G_PASTE (_cogl_unpack_g_8_, component_type) (src, dst, width);
324 case COGL_PIXEL_FORMAT_RGB_888:
325 G_PASTE (_cogl_unpack_rgb_888_, component_type) (src, dst, width);
327 case COGL_PIXEL_FORMAT_BGR_888:
328 G_PASTE (_cogl_unpack_bgr_888_, component_type) (src, dst, width);
330 case COGL_PIXEL_FORMAT_RGBA_8888:
331 case COGL_PIXEL_FORMAT_RGBA_8888_PRE:
332 G_PASTE (_cogl_unpack_rgba_8888_, component_type) (src, dst, width);
334 case COGL_PIXEL_FORMAT_BGRA_8888:
335 case COGL_PIXEL_FORMAT_BGRA_8888_PRE:
336 G_PASTE (_cogl_unpack_bgra_8888_, component_type) (src, dst, width);
338 case COGL_PIXEL_FORMAT_ARGB_8888:
339 case COGL_PIXEL_FORMAT_ARGB_8888_PRE:
340 G_PASTE (_cogl_unpack_argb_8888_, component_type) (src, dst, width);
342 case COGL_PIXEL_FORMAT_ABGR_8888:
343 case COGL_PIXEL_FORMAT_ABGR_8888_PRE:
344 G_PASTE (_cogl_unpack_abgr_8888_, component_type) (src, dst, width);
346 case COGL_PIXEL_FORMAT_RGB_565:
347 G_PASTE (_cogl_unpack_rgb_565_, component_type) (src, dst, width);
349 case COGL_PIXEL_FORMAT_RGBA_4444:
350 case COGL_PIXEL_FORMAT_RGBA_4444_PRE:
351 G_PASTE (_cogl_unpack_rgba_4444_, component_type) (src, dst, width);
353 case COGL_PIXEL_FORMAT_RGBA_5551:
354 case COGL_PIXEL_FORMAT_RGBA_5551_PRE:
355 G_PASTE (_cogl_unpack_rgba_5551_, component_type) (src, dst, width);
357 case COGL_PIXEL_FORMAT_RGBA_1010102:
358 case COGL_PIXEL_FORMAT_RGBA_1010102_PRE:
359 G_PASTE (_cogl_unpack_rgba_1010102_, component_type) (src, dst, width);
361 case COGL_PIXEL_FORMAT_BGRA_1010102:
362 case COGL_PIXEL_FORMAT_BGRA_1010102_PRE:
363 G_PASTE (_cogl_unpack_bgra_1010102_, component_type) (src, dst, width);
365 case COGL_PIXEL_FORMAT_ARGB_2101010:
366 case COGL_PIXEL_FORMAT_ARGB_2101010_PRE:
367 G_PASTE (_cogl_unpack_argb_2101010_, component_type) (src, dst, width);
369 case COGL_PIXEL_FORMAT_ABGR_2101010:
370 case COGL_PIXEL_FORMAT_ABGR_2101010_PRE:
371 G_PASTE (_cogl_unpack_abgr_2101010_, component_type) (src, dst, width);
373 case COGL_PIXEL_FORMAT_ANY:
374 case COGL_PIXEL_FORMAT_YUV:
375 g_assert_not_reached ();
379 /* Packing from RGBA */
381 /* Pack and round to nearest */
382 #define PACK_SIZE(b, max) \
383 (((b) * (max) + (1 << (sizeof (component_type) * 8 - 1)) - 1) / \
384 ((1 << (sizeof (component_type) * 8)) - 1))
386 #define PACK_1(b) PACK_SIZE (b, 1)
387 #define PACK_2(b) PACK_SIZE (b, 3)
388 #define PACK_4(b) PACK_SIZE (b, 15)
389 #define PACK_5(b) PACK_SIZE (b, 31)
390 #define PACK_6(b) PACK_SIZE (b, 63)
391 #define PACK_10(b) PACK_SIZE (b, 1023)
394 G_PASTE (_cogl_pack_a_8_, component_type) (const component_type *src,
400 *dst = PACK_BYTE (src[3]);
407 G_PASTE (_cogl_pack_g_8_, component_type) (const component_type *src,
411 /* FIXME: I'm not sure if this is right. It looks like Nvidia and
412 Mesa handle luminance textures differently. Maybe we should
413 consider just removing luminance textures for Cogl 2.0 because
414 they have been removed in GL 3.0 */
417 component_type v = (src[0] + src[1] + src[2]) / 3;
418 *dst = PACK_BYTE (v);
425 G_PASTE (_cogl_pack_rgb_888_, component_type) (const component_type *src,
431 dst[0] = PACK_BYTE (src[0]);
432 dst[1] = PACK_BYTE (src[1]);
433 dst[2] = PACK_BYTE (src[2]);
440 G_PASTE (_cogl_pack_bgr_888_, component_type) (const component_type *src,
446 dst[2] = PACK_BYTE (src[0]);
447 dst[1] = PACK_BYTE (src[1]);
448 dst[0] = PACK_BYTE (src[2]);
455 G_PASTE (_cogl_pack_bgra_8888_, component_type) (const component_type *src,
461 dst[2] = PACK_BYTE (src[0]);
462 dst[1] = PACK_BYTE (src[1]);
463 dst[0] = PACK_BYTE (src[2]);
464 dst[3] = PACK_BYTE (src[3]);
471 G_PASTE (_cogl_pack_argb_8888_, component_type) (const component_type *src,
477 dst[1] = PACK_BYTE (src[0]);
478 dst[2] = PACK_BYTE (src[1]);
479 dst[3] = PACK_BYTE (src[2]);
480 dst[0] = PACK_BYTE (src[3]);
487 G_PASTE (_cogl_pack_abgr_8888_, component_type) (const component_type *src,
493 dst[3] = PACK_BYTE (src[0]);
494 dst[2] = PACK_BYTE (src[1]);
495 dst[1] = PACK_BYTE (src[2]);
496 dst[0] = PACK_BYTE (src[3]);
503 G_PASTE (_cogl_pack_rgba_8888_, component_type) (const component_type *src,
509 dst[0] = PACK_BYTE (src[0]);
510 dst[1] = PACK_BYTE (src[1]);
511 dst[2] = PACK_BYTE (src[2]);
512 dst[3] = PACK_BYTE (src[3]);
519 G_PASTE (_cogl_pack_rgb_565_, component_type) (const component_type *src,
525 guint16 *v = (guint16 *) dst;
527 *v = ((PACK_5 (src[0]) << 11) |
528 (PACK_6 (src[1]) << 5) |
536 G_PASTE (_cogl_pack_rgba_4444_, component_type) (const component_type *src,
542 guint16 *v = (guint16 *) dst;
544 *v = ((PACK_4 (src[0]) << 12) |
545 (PACK_4 (src[1]) << 8) |
546 (PACK_4 (src[2]) << 4) |
554 G_PASTE (_cogl_pack_rgba_5551_, component_type) (const component_type *src,
560 guint16 *v = (guint16 *) dst;
562 *v = ((PACK_5 (src[0]) << 11) |
563 (PACK_5 (src[1]) << 6) |
564 (PACK_5 (src[2]) << 1) |
572 G_PASTE (_cogl_pack_rgba_1010102_, component_type) (const component_type *src,
578 guint32 *v = (guint32 *) dst;
580 *v = ((PACK_10 (src[0]) << 22) |
581 (PACK_10 (src[1]) << 12) |
582 (PACK_10 (src[2]) << 2) |
590 G_PASTE (_cogl_pack_bgra_1010102_, component_type) (const component_type *src,
596 guint32 *v = (guint32 *) dst;
598 *v = ((PACK_10 (src[2]) << 22) |
599 (PACK_10 (src[1]) << 12) |
600 (PACK_10 (src[0]) << 2) |
608 G_PASTE (_cogl_pack_argb_2101010_, component_type) (const component_type *src,
614 guint32 *v = (guint32 *) dst;
616 *v = ((PACK_2 (src[3]) << 30) |
617 (PACK_10 (src[0]) << 20) |
618 (PACK_10 (src[1]) << 10) |
626 G_PASTE (_cogl_pack_abgr_2101010_, component_type) (const component_type *src,
632 guint32 *v = (guint32 *) dst;
634 *v = ((PACK_2 (src[3]) << 30) |
635 (PACK_10 (src[2]) << 20) |
636 (PACK_10 (src[1]) << 10) |
652 G_PASTE (_cogl_pack_, component_type) (CoglPixelFormat format,
653 const component_type *src,
659 case COGL_PIXEL_FORMAT_A_8:
660 G_PASTE (_cogl_pack_a_8_, component_type) (src, dst, width);
662 case COGL_PIXEL_FORMAT_G_8:
663 G_PASTE (_cogl_pack_g_8_, component_type) (src, dst, width);
665 case COGL_PIXEL_FORMAT_RGB_888:
666 G_PASTE (_cogl_pack_rgb_888_, component_type) (src, dst, width);
668 case COGL_PIXEL_FORMAT_BGR_888:
669 G_PASTE (_cogl_pack_bgr_888_, component_type) (src, dst, width);
671 case COGL_PIXEL_FORMAT_RGBA_8888:
672 case COGL_PIXEL_FORMAT_RGBA_8888_PRE:
673 G_PASTE (_cogl_pack_rgba_8888_, component_type) (src, dst, width);
675 case COGL_PIXEL_FORMAT_BGRA_8888:
676 case COGL_PIXEL_FORMAT_BGRA_8888_PRE:
677 G_PASTE (_cogl_pack_bgra_8888_, component_type) (src, dst, width);
679 case COGL_PIXEL_FORMAT_ARGB_8888:
680 case COGL_PIXEL_FORMAT_ARGB_8888_PRE:
681 G_PASTE (_cogl_pack_argb_8888_, component_type) (src, dst, width);
683 case COGL_PIXEL_FORMAT_ABGR_8888:
684 case COGL_PIXEL_FORMAT_ABGR_8888_PRE:
685 G_PASTE (_cogl_pack_abgr_8888_, component_type) (src, dst, width);
687 case COGL_PIXEL_FORMAT_RGB_565:
688 G_PASTE (_cogl_pack_rgb_565_, component_type) (src, dst, width);
690 case COGL_PIXEL_FORMAT_RGBA_4444:
691 case COGL_PIXEL_FORMAT_RGBA_4444_PRE:
692 G_PASTE (_cogl_pack_rgba_4444_, component_type) (src, dst, width);
694 case COGL_PIXEL_FORMAT_RGBA_5551:
695 case COGL_PIXEL_FORMAT_RGBA_5551_PRE:
696 G_PASTE (_cogl_pack_rgba_5551_, component_type) (src, dst, width);
698 case COGL_PIXEL_FORMAT_RGBA_1010102:
699 case COGL_PIXEL_FORMAT_RGBA_1010102_PRE:
700 G_PASTE (_cogl_pack_rgba_1010102_, component_type) (src, dst, width);
702 case COGL_PIXEL_FORMAT_BGRA_1010102:
703 case COGL_PIXEL_FORMAT_BGRA_1010102_PRE:
704 G_PASTE (_cogl_pack_bgra_1010102_, component_type) (src, dst, width);
706 case COGL_PIXEL_FORMAT_ARGB_2101010:
707 case COGL_PIXEL_FORMAT_ARGB_2101010_PRE:
708 G_PASTE (_cogl_pack_argb_2101010_, component_type) (src, dst, width);
710 case COGL_PIXEL_FORMAT_ABGR_2101010:
711 case COGL_PIXEL_FORMAT_ABGR_2101010_PRE:
712 G_PASTE (_cogl_pack_abgr_2101010_, component_type) (src, dst, width);
714 case COGL_PIXEL_FORMAT_ANY:
715 case COGL_PIXEL_FORMAT_YUV:
716 g_assert_not_reached ();