1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  *
29  */
30 
31 /* This file is included multiple times with different definitions for
32    the component_type type (either uint8_t or uint16_t). The code ends
33    up exactly the same for both but we only want to end up hitting the
34    16-bit path when one of the types in the conversion is > 8 bits per
35    component. */
36 
37 /* Unpacking to RGBA */
38 
39 #define UNPACK_1(b) ((b) * ((1 << (sizeof (component_type) * 8)) - 1))
40 #define UNPACK_2(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
41                       1) / 3)
42 #define UNPACK_4(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
43                       7) / 15)
44 #define UNPACK_5(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
45                       15) / 31)
46 #define UNPACK_6(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
47                       31) / 63)
48 #define UNPACK_10(b) (((b) * ((1 << (sizeof (component_type) * 8)) - 1) + \
49                        511) / 1023)
50 
51 inline static void
G_PASTE(_cogl_unpack_a_8_,component_size)52 G_PASTE (_cogl_unpack_a_8_, component_size) (const uint8_t *src,
53                                              component_type *dst,
54                                              int width)
55 {
56   while (width-- > 0)
57     {
58       dst[0] = 0;
59       dst[1] = 0;
60       dst[2] = 0;
61       dst[3] = UNPACK_BYTE (*src);
62       dst += 4;
63       src++;
64     }
65 }
66 
67 inline static void
G_PASTE(_cogl_unpack_g_8_,component_size)68 G_PASTE (_cogl_unpack_g_8_, component_size) (const uint8_t *src,
69                                              component_type *dst,
70                                              int width)
71 {
72   /* FIXME: I'm not sure if this is right. It looks like Nvidia and
73      Mesa handle luminance textures differently. Maybe we should
74      consider just removing luminance textures for Cogl 2.0 because
75      they have been removed in GL 3.0 */
76   while (width-- > 0)
77     {
78       component_type v = UNPACK_BYTE (src[0]);
79       dst[0] = v;
80       dst[1] = v;
81       dst[2] = v;
82       dst[3] = UNPACK_BYTE (255);
83       dst += 4;
84       src++;
85     }
86 }
87 
88 inline static void
G_PASTE(_cogl_unpack_rg_88_,component_size)89 G_PASTE (_cogl_unpack_rg_88_, component_size) (const uint8_t *src,
90                                                component_type *dst,
91                                                int width)
92 {
93   while (width-- > 0)
94     {
95       dst[0] = UNPACK_BYTE (src[0]);
96       dst[1] = UNPACK_BYTE (src[1]);
97       dst[2] = 0;
98       dst[3] = UNPACK_BYTE (255);
99       dst += 4;
100       src += 2;
101     }
102 }
103 
104 inline static void
G_PASTE(_cogl_unpack_rgb_888_,component_size)105 G_PASTE (_cogl_unpack_rgb_888_, component_size) (const uint8_t *src,
106                                                  component_type *dst,
107                                                  int width)
108 {
109   while (width-- > 0)
110     {
111       dst[0] = UNPACK_BYTE (src[0]);
112       dst[1] = UNPACK_BYTE (src[1]);
113       dst[2] = UNPACK_BYTE (src[2]);
114       dst[3] = UNPACK_BYTE (255);
115       dst += 4;
116       src += 3;
117     }
118 }
119 
120 inline static void
G_PASTE(_cogl_unpack_bgr_888_,component_size)121 G_PASTE (_cogl_unpack_bgr_888_, component_size) (const uint8_t *src,
122                                                  component_type *dst,
123                                                  int width)
124 {
125   while (width-- > 0)
126     {
127       dst[0] = UNPACK_BYTE (src[2]);
128       dst[1] = UNPACK_BYTE (src[1]);
129       dst[2] = UNPACK_BYTE (src[0]);
130       dst[3] = UNPACK_BYTE (255);
131       dst += 4;
132       src += 3;
133     }
134 }
135 
136 inline static void
G_PASTE(_cogl_unpack_bgra_8888_,component_size)137 G_PASTE (_cogl_unpack_bgra_8888_, component_size) (const uint8_t *src,
138                                                    component_type *dst,
139                                                    int width)
140 {
141   while (width-- > 0)
142     {
143       dst[0] = UNPACK_BYTE (src[2]);
144       dst[1] = UNPACK_BYTE (src[1]);
145       dst[2] = UNPACK_BYTE (src[0]);
146       dst[3] = UNPACK_BYTE (src[3]);
147       dst += 4;
148       src += 4;
149     }
150 }
151 
152 inline static void
G_PASTE(_cogl_unpack_argb_8888_,component_size)153 G_PASTE (_cogl_unpack_argb_8888_, component_size) (const uint8_t *src,
154                                                    component_type *dst,
155                                                    int width)
156 {
157   while (width-- > 0)
158     {
159       dst[0] = UNPACK_BYTE (src[1]);
160       dst[1] = UNPACK_BYTE (src[2]);
161       dst[2] = UNPACK_BYTE (src[3]);
162       dst[3] = UNPACK_BYTE (src[0]);
163       dst += 4;
164       src += 4;
165     }
166 }
167 
168 inline static void
G_PASTE(_cogl_unpack_abgr_8888_,component_size)169 G_PASTE (_cogl_unpack_abgr_8888_, component_size) (const uint8_t *src,
170                                                    component_type *dst,
171                                                    int width)
172 {
173   while (width-- > 0)
174     {
175       dst[0] = UNPACK_BYTE (src[3]);
176       dst[1] = UNPACK_BYTE (src[2]);
177       dst[2] = UNPACK_BYTE (src[1]);
178       dst[3] = UNPACK_BYTE (src[0]);
179       dst += 4;
180       src += 4;
181     }
182 }
183 
184 inline static void
G_PASTE(_cogl_unpack_rgba_8888_,component_size)185 G_PASTE (_cogl_unpack_rgba_8888_, component_size) (const uint8_t *src,
186                                                    component_type *dst,
187                                                    int width)
188 {
189   while (width-- > 0)
190     {
191       dst[0] = UNPACK_BYTE (src[0]);
192       dst[1] = UNPACK_BYTE (src[1]);
193       dst[2] = UNPACK_BYTE (src[2]);
194       dst[3] = UNPACK_BYTE (src[3]);
195       dst += 4;
196       src += 4;
197     }
198 }
199 
200 inline static void
G_PASTE(_cogl_unpack_rgb_565_,component_size)201 G_PASTE (_cogl_unpack_rgb_565_, component_size) (const uint8_t *src,
202                                                  component_type *dst,
203                                                  int width)
204 {
205   while (width-- > 0)
206     {
207       uint16_t v = *(const uint16_t *) src;
208 
209       dst[0] = UNPACK_5 (v >> 11);
210       dst[1] = UNPACK_6 ((v >> 5) & 63);
211       dst[2] = UNPACK_5 (v & 31);
212       dst[3] = UNPACK_BYTE (255);
213       dst += 4;
214       src += 2;
215     }
216 }
217 
218 inline static void
G_PASTE(_cogl_unpack_rgba_4444_,component_size)219 G_PASTE (_cogl_unpack_rgba_4444_, component_size) (const uint8_t *src,
220                                                    component_type *dst,
221                                                    int width)
222 {
223   while (width-- > 0)
224     {
225       uint16_t v = *(const uint16_t *) src;
226 
227       dst[0] = UNPACK_4 (v >> 12);
228       dst[1] = UNPACK_4 ((v >> 8) & 15);
229       dst[2] = UNPACK_4 ((v >> 4) & 15);
230       dst[3] = UNPACK_4 (v & 15);
231       dst += 4;
232       src += 2;
233     }
234 }
235 
236 inline static void
G_PASTE(_cogl_unpack_rgba_5551_,component_size)237 G_PASTE (_cogl_unpack_rgba_5551_, component_size) (const uint8_t *src,
238                                                    component_type *dst,
239                                                    int width)
240 {
241   while (width-- > 0)
242     {
243       uint16_t v = *(const uint16_t *) src;
244 
245       dst[0] = UNPACK_5 (v >> 11);
246       dst[1] = UNPACK_5 ((v >> 6) & 31);
247       dst[2] = UNPACK_5 ((v >> 1) & 31);
248       dst[3] = UNPACK_1 (v & 1);
249       dst += 4;
250       src += 2;
251     }
252 }
253 
254 inline static void
G_PASTE(_cogl_unpack_rgba_1010102_,component_size)255 G_PASTE (_cogl_unpack_rgba_1010102_, component_size) (const uint8_t *src,
256                                                       component_type *dst,
257                                                       int width)
258 {
259   while (width-- > 0)
260     {
261       uint32_t v = *(const uint32_t *) src;
262 
263       dst[0] = UNPACK_10 (v >> 22);
264       dst[1] = UNPACK_10 ((v >> 12) & 1023);
265       dst[2] = UNPACK_10 ((v >> 2) & 1023);
266       dst[3] = UNPACK_2 (v & 3);
267       dst += 4;
268       src += 2;
269     }
270 }
271 
272 inline static void
G_PASTE(_cogl_unpack_bgra_1010102_,component_size)273 G_PASTE (_cogl_unpack_bgra_1010102_, component_size) (const uint8_t *src,
274                                                       component_type *dst,
275                                                       int width)
276 {
277   while (width-- > 0)
278     {
279       uint32_t v = *(const uint32_t *) src;
280 
281       dst[2] = UNPACK_10 (v >> 22);
282       dst[1] = UNPACK_10 ((v >> 12) & 1023);
283       dst[0] = UNPACK_10 ((v >> 2) & 1023);
284       dst[3] = UNPACK_2 (v & 3);
285       dst += 4;
286       src += 2;
287     }
288 }
289 
290 inline static void
G_PASTE(_cogl_unpack_argb_2101010_,component_size)291 G_PASTE (_cogl_unpack_argb_2101010_, component_size) (const uint8_t *src,
292                                                       component_type *dst,
293                                                       int width)
294 {
295   while (width-- > 0)
296     {
297       uint32_t v = *(const uint32_t *) src;
298 
299       dst[3] = UNPACK_2 (v >> 30);
300       dst[0] = UNPACK_10 ((v >> 20) & 1023);
301       dst[1] = UNPACK_10 ((v >> 10) & 1023);
302       dst[2] = UNPACK_10 (v & 1023);
303       dst += 4;
304       src += 2;
305     }
306 }
307 
308 inline static void
G_PASTE(_cogl_unpack_abgr_2101010_,component_size)309 G_PASTE (_cogl_unpack_abgr_2101010_, component_size) (const uint8_t *src,
310                                                       component_type *dst,
311                                                       int width)
312 {
313   while (width-- > 0)
314     {
315       uint32_t v = *(const uint32_t *) src;
316 
317       dst[3] = UNPACK_2 (v >> 30);
318       dst[2] = UNPACK_10 ((v >> 20) & 1023);
319       dst[1] = UNPACK_10 ((v >> 10) & 1023);
320       dst[0] = UNPACK_10 (v & 1023);
321       dst += 4;
322       src += 2;
323     }
324 }
325 
326 #undef UNPACK_1
327 #undef UNPACK_2
328 #undef UNPACK_4
329 #undef UNPACK_5
330 #undef UNPACK_6
331 #undef UNPACK_10
332 
333 inline static void
G_PASTE(_cogl_unpack_,component_size)334 G_PASTE (_cogl_unpack_, component_size) (CoglPixelFormat format,
335                                          const uint8_t *src,
336                                          component_type *dst,
337                                          int width)
338 {
339   switch (format)
340     {
341     case COGL_PIXEL_FORMAT_A_8:
342       G_PASTE (_cogl_unpack_a_8_, component_size) (src, dst, width);
343       break;
344     case COGL_PIXEL_FORMAT_G_8:
345       G_PASTE (_cogl_unpack_g_8_, component_size) (src, dst, width);
346       break;
347     case COGL_PIXEL_FORMAT_RG_88:
348       G_PASTE (_cogl_unpack_rg_88_, component_size) (src, dst, width);
349       break;
350     case COGL_PIXEL_FORMAT_RGB_888:
351       G_PASTE (_cogl_unpack_rgb_888_, component_size) (src, dst, width);
352       break;
353     case COGL_PIXEL_FORMAT_BGR_888:
354       G_PASTE (_cogl_unpack_bgr_888_, component_size) (src, dst, width);
355       break;
356     case COGL_PIXEL_FORMAT_RGBA_8888:
357     case COGL_PIXEL_FORMAT_RGBA_8888_PRE:
358       G_PASTE (_cogl_unpack_rgba_8888_, component_size) (src, dst, width);
359       break;
360     case COGL_PIXEL_FORMAT_BGRA_8888:
361     case COGL_PIXEL_FORMAT_BGRA_8888_PRE:
362       G_PASTE (_cogl_unpack_bgra_8888_, component_size) (src, dst, width);
363       break;
364     case COGL_PIXEL_FORMAT_ARGB_8888:
365     case COGL_PIXEL_FORMAT_ARGB_8888_PRE:
366       G_PASTE (_cogl_unpack_argb_8888_, component_size) (src, dst, width);
367       break;
368     case COGL_PIXEL_FORMAT_ABGR_8888:
369     case COGL_PIXEL_FORMAT_ABGR_8888_PRE:
370       G_PASTE (_cogl_unpack_abgr_8888_, component_size) (src, dst, width);
371       break;
372     case COGL_PIXEL_FORMAT_RGB_565:
373       G_PASTE (_cogl_unpack_rgb_565_, component_size) (src, dst, width);
374       break;
375     case COGL_PIXEL_FORMAT_RGBA_4444:
376     case COGL_PIXEL_FORMAT_RGBA_4444_PRE:
377       G_PASTE (_cogl_unpack_rgba_4444_, component_size) (src, dst, width);
378       break;
379     case COGL_PIXEL_FORMAT_RGBA_5551:
380     case COGL_PIXEL_FORMAT_RGBA_5551_PRE:
381       G_PASTE (_cogl_unpack_rgba_5551_, component_size) (src, dst, width);
382       break;
383     case COGL_PIXEL_FORMAT_RGBA_1010102:
384     case COGL_PIXEL_FORMAT_RGBA_1010102_PRE:
385       G_PASTE (_cogl_unpack_rgba_1010102_, component_size) (src, dst, width);
386       break;
387     case COGL_PIXEL_FORMAT_BGRA_1010102:
388     case COGL_PIXEL_FORMAT_BGRA_1010102_PRE:
389       G_PASTE (_cogl_unpack_bgra_1010102_, component_size) (src, dst, width);
390       break;
391     case COGL_PIXEL_FORMAT_ARGB_2101010:
392     case COGL_PIXEL_FORMAT_ARGB_2101010_PRE:
393       G_PASTE (_cogl_unpack_argb_2101010_, component_size) (src, dst, width);
394       break;
395     case COGL_PIXEL_FORMAT_ABGR_2101010:
396     case COGL_PIXEL_FORMAT_ABGR_2101010_PRE:
397       G_PASTE (_cogl_unpack_abgr_2101010_, component_size) (src, dst, width);
398       break;
399     case COGL_PIXEL_FORMAT_DEPTH_16:
400     case COGL_PIXEL_FORMAT_DEPTH_32:
401     case COGL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
402     case COGL_PIXEL_FORMAT_ANY:
403     case COGL_PIXEL_FORMAT_YUV:
404       g_assert_not_reached ();
405     }
406 }
407 
408 /* Packing from RGBA */
409 
410 /* Pack and round to nearest */
411 #define PACK_SIZE(b, max) \
412   (((b) * (max) + (1 << (sizeof (component_type) * 8 - 1)) - 1) / \
413    ((1 << (sizeof (component_type) * 8)) - 1))
414 
415 #define PACK_1(b) PACK_SIZE (b, 1)
416 #define PACK_2(b) PACK_SIZE (b, 3)
417 #define PACK_4(b) PACK_SIZE (b, 15)
418 #define PACK_5(b) PACK_SIZE (b, 31)
419 #define PACK_6(b) PACK_SIZE (b, 63)
420 #define PACK_10(b) PACK_SIZE (b, 1023)
421 
422 inline static void
G_PASTE(_cogl_pack_a_8_,component_size)423 G_PASTE (_cogl_pack_a_8_, component_size) (const component_type *src,
424                                            uint8_t *dst,
425                                            int width)
426 {
427   while (width-- > 0)
428     {
429       *dst = PACK_BYTE (src[3]);
430       src += 4;
431       dst++;
432     }
433 }
434 
435 inline static void
G_PASTE(_cogl_pack_g_8_,component_size)436 G_PASTE (_cogl_pack_g_8_, component_size) (const component_type *src,
437                                            uint8_t *dst,
438                                            int width)
439 {
440   /* FIXME: I'm not sure if this is right. It looks like Nvidia and
441      Mesa handle luminance textures differently. Maybe we should
442      consider just removing luminance textures for Cogl 2.0 because
443      they have been removed in GL 3.0 */
444   while (width-- > 0)
445     {
446       component_type v = (src[0] + src[1] + src[2]) / 3;
447       *dst = PACK_BYTE (v);
448       src += 4;
449       dst++;
450     }
451 }
452 
453 inline static void
G_PASTE(_cogl_pack_rg_88_,component_size)454 G_PASTE (_cogl_pack_rg_88_, component_size) (const component_type *src,
455                                              uint8_t *dst,
456                                              int width)
457 {
458   while (width-- > 0)
459     {
460       dst[0] = PACK_BYTE (src[0]);
461       dst[1] = PACK_BYTE (src[1]);
462       src += 4;
463       dst += 2;
464     }
465 }
466 
467 inline static void
G_PASTE(_cogl_pack_rgb_888_,component_size)468 G_PASTE (_cogl_pack_rgb_888_, component_size) (const component_type *src,
469                                                uint8_t *dst,
470                                                int width)
471 {
472   while (width-- > 0)
473     {
474       dst[0] = PACK_BYTE (src[0]);
475       dst[1] = PACK_BYTE (src[1]);
476       dst[2] = PACK_BYTE (src[2]);
477       src += 4;
478       dst += 3;
479     }
480 }
481 
482 inline static void
G_PASTE(_cogl_pack_bgr_888_,component_size)483 G_PASTE (_cogl_pack_bgr_888_, component_size) (const component_type *src,
484                                                uint8_t *dst,
485                                                int width)
486 {
487   while (width-- > 0)
488     {
489       dst[2] = PACK_BYTE (src[0]);
490       dst[1] = PACK_BYTE (src[1]);
491       dst[0] = PACK_BYTE (src[2]);
492       src += 4;
493       dst += 3;
494     }
495 }
496 
497 inline static void
G_PASTE(_cogl_pack_bgra_8888_,component_size)498 G_PASTE (_cogl_pack_bgra_8888_, component_size) (const component_type *src,
499                                                  uint8_t *dst,
500                                                  int width)
501 {
502   while (width-- > 0)
503     {
504       dst[2] = PACK_BYTE (src[0]);
505       dst[1] = PACK_BYTE (src[1]);
506       dst[0] = PACK_BYTE (src[2]);
507       dst[3] = PACK_BYTE (src[3]);
508       src += 4;
509       dst += 4;
510     }
511 }
512 
513 inline static void
G_PASTE(_cogl_pack_argb_8888_,component_size)514 G_PASTE (_cogl_pack_argb_8888_, component_size) (const component_type *src,
515                                                  uint8_t *dst,
516                                                  int width)
517 {
518   while (width-- > 0)
519     {
520       dst[1] = PACK_BYTE (src[0]);
521       dst[2] = PACK_BYTE (src[1]);
522       dst[3] = PACK_BYTE (src[2]);
523       dst[0] = PACK_BYTE (src[3]);
524       src += 4;
525       dst += 4;
526     }
527 }
528 
529 inline static void
G_PASTE(_cogl_pack_abgr_8888_,component_size)530 G_PASTE (_cogl_pack_abgr_8888_, component_size) (const component_type *src,
531                                                  uint8_t *dst,
532                                                  int width)
533 {
534   while (width-- > 0)
535     {
536       dst[3] = PACK_BYTE (src[0]);
537       dst[2] = PACK_BYTE (src[1]);
538       dst[1] = PACK_BYTE (src[2]);
539       dst[0] = PACK_BYTE (src[3]);
540       src += 4;
541       dst += 4;
542     }
543 }
544 
545 inline static void
G_PASTE(_cogl_pack_rgba_8888_,component_size)546 G_PASTE (_cogl_pack_rgba_8888_, component_size) (const component_type *src,
547                                                  uint8_t *dst,
548                                                  int width)
549 {
550   while (width-- > 0)
551     {
552       dst[0] = PACK_BYTE (src[0]);
553       dst[1] = PACK_BYTE (src[1]);
554       dst[2] = PACK_BYTE (src[2]);
555       dst[3] = PACK_BYTE (src[3]);
556       src += 4;
557       dst += 4;
558     }
559 }
560 
561 inline static void
G_PASTE(_cogl_pack_rgb_565_,component_size)562 G_PASTE (_cogl_pack_rgb_565_, component_size) (const component_type *src,
563                                                uint8_t *dst,
564                                                int width)
565 {
566   while (width-- > 0)
567     {
568       uint16_t *v = (uint16_t *) dst;
569 
570       *v = ((PACK_5 (src[0]) << 11) |
571             (PACK_6 (src[1]) << 5) |
572             PACK_5 (src[2]));
573       src += 4;
574       dst += 2;
575     }
576 }
577 
578 inline static void
G_PASTE(_cogl_pack_rgba_4444_,component_size)579 G_PASTE (_cogl_pack_rgba_4444_, component_size) (const component_type *src,
580                                                  uint8_t *dst,
581                                                  int width)
582 {
583   while (width-- > 0)
584     {
585       uint16_t *v = (uint16_t *) dst;
586 
587       *v = ((PACK_4 (src[0]) << 12) |
588             (PACK_4 (src[1]) << 8) |
589             (PACK_4 (src[2]) << 4) |
590             PACK_4 (src[3]));
591       src += 4;
592       dst += 2;
593     }
594 }
595 
596 inline static void
G_PASTE(_cogl_pack_rgba_5551_,component_size)597 G_PASTE (_cogl_pack_rgba_5551_, component_size) (const component_type *src,
598                                                  uint8_t *dst,
599                                                  int width)
600 {
601   while (width-- > 0)
602     {
603       uint16_t *v = (uint16_t *) dst;
604 
605       *v = ((PACK_5 (src[0]) << 11) |
606             (PACK_5 (src[1]) << 6) |
607             (PACK_5 (src[2]) << 1) |
608             PACK_1 (src[3]));
609       src += 4;
610       dst += 2;
611     }
612 }
613 
614 inline static void
G_PASTE(_cogl_pack_rgba_1010102_,component_size)615 G_PASTE (_cogl_pack_rgba_1010102_, component_size) (const component_type *src,
616                                                     uint8_t *dst,
617                                                     int width)
618 {
619   while (width-- > 0)
620     {
621       uint32_t *v = (uint32_t *) dst;
622 
623       *v = ((PACK_10 (src[0]) << 22) |
624             (PACK_10 (src[1]) << 12) |
625             (PACK_10 (src[2]) << 2) |
626             PACK_2 (src[3]));
627       src += 4;
628       dst += 4;
629     }
630 }
631 
632 inline static void
G_PASTE(_cogl_pack_bgra_1010102_,component_size)633 G_PASTE (_cogl_pack_bgra_1010102_, component_size) (const component_type *src,
634                                                     uint8_t *dst,
635                                                     int width)
636 {
637   while (width-- > 0)
638     {
639       uint32_t *v = (uint32_t *) dst;
640 
641       *v = ((PACK_10 (src[2]) << 22) |
642             (PACK_10 (src[1]) << 12) |
643             (PACK_10 (src[0]) << 2) |
644             PACK_2 (src[3]));
645       src += 4;
646       dst += 4;
647     }
648 }
649 
650 inline static void
G_PASTE(_cogl_pack_argb_2101010_,component_size)651 G_PASTE (_cogl_pack_argb_2101010_, component_size) (const component_type *src,
652                                                     uint8_t *dst,
653                                                     int width)
654 {
655   while (width-- > 0)
656     {
657       uint32_t *v = (uint32_t *) dst;
658 
659       *v = ((PACK_2 (src[3]) << 30) |
660             (PACK_10 (src[0]) << 20) |
661             (PACK_10 (src[1]) << 10) |
662             PACK_10 (src[2]));
663       src += 4;
664       dst += 4;
665     }
666 }
667 
668 inline static void
G_PASTE(_cogl_pack_abgr_2101010_,component_size)669 G_PASTE (_cogl_pack_abgr_2101010_, component_size) (const component_type *src,
670                                                     uint8_t *dst,
671                                                     int width)
672 {
673   while (width-- > 0)
674     {
675       uint32_t *v = (uint32_t *) dst;
676 
677       *v = ((PACK_2 (src[3]) << 30) |
678             (PACK_10 (src[2]) << 20) |
679             (PACK_10 (src[1]) << 10) |
680             PACK_10 (src[0]));
681       src += 4;
682       dst += 4;
683     }
684 }
685 
686 #undef PACK_SIZE
687 #undef PACK_1
688 #undef PACK_2
689 #undef PACK_4
690 #undef PACK_5
691 #undef PACK_6
692 #undef PACK_10
693 
694 inline static void
G_PASTE(_cogl_pack_,component_size)695 G_PASTE (_cogl_pack_, component_size) (CoglPixelFormat format,
696                                        const component_type *src,
697                                        uint8_t *dst,
698                                        int width)
699 {
700   switch (format)
701     {
702     case COGL_PIXEL_FORMAT_A_8:
703       G_PASTE (_cogl_pack_a_8_, component_size) (src, dst, width);
704       break;
705     case COGL_PIXEL_FORMAT_G_8:
706       G_PASTE (_cogl_pack_g_8_, component_size) (src, dst, width);
707       break;
708     case COGL_PIXEL_FORMAT_RG_88:
709       G_PASTE (_cogl_pack_rg_88_, component_size) (src, dst, width);
710       break;
711     case COGL_PIXEL_FORMAT_RGB_888:
712       G_PASTE (_cogl_pack_rgb_888_, component_size) (src, dst, width);
713       break;
714     case COGL_PIXEL_FORMAT_BGR_888:
715       G_PASTE (_cogl_pack_bgr_888_, component_size) (src, dst, width);
716       break;
717     case COGL_PIXEL_FORMAT_RGBA_8888:
718     case COGL_PIXEL_FORMAT_RGBA_8888_PRE:
719       G_PASTE (_cogl_pack_rgba_8888_, component_size) (src, dst, width);
720       break;
721     case COGL_PIXEL_FORMAT_BGRA_8888:
722     case COGL_PIXEL_FORMAT_BGRA_8888_PRE:
723       G_PASTE (_cogl_pack_bgra_8888_, component_size) (src, dst, width);
724       break;
725     case COGL_PIXEL_FORMAT_ARGB_8888:
726     case COGL_PIXEL_FORMAT_ARGB_8888_PRE:
727       G_PASTE (_cogl_pack_argb_8888_, component_size) (src, dst, width);
728       break;
729     case COGL_PIXEL_FORMAT_ABGR_8888:
730     case COGL_PIXEL_FORMAT_ABGR_8888_PRE:
731       G_PASTE (_cogl_pack_abgr_8888_, component_size) (src, dst, width);
732       break;
733     case COGL_PIXEL_FORMAT_RGB_565:
734       G_PASTE (_cogl_pack_rgb_565_, component_size) (src, dst, width);
735       break;
736     case COGL_PIXEL_FORMAT_RGBA_4444:
737     case COGL_PIXEL_FORMAT_RGBA_4444_PRE:
738       G_PASTE (_cogl_pack_rgba_4444_, component_size) (src, dst, width);
739       break;
740     case COGL_PIXEL_FORMAT_RGBA_5551:
741     case COGL_PIXEL_FORMAT_RGBA_5551_PRE:
742       G_PASTE (_cogl_pack_rgba_5551_, component_size) (src, dst, width);
743       break;
744     case COGL_PIXEL_FORMAT_RGBA_1010102:
745     case COGL_PIXEL_FORMAT_RGBA_1010102_PRE:
746       G_PASTE (_cogl_pack_rgba_1010102_, component_size) (src, dst, width);
747       break;
748     case COGL_PIXEL_FORMAT_BGRA_1010102:
749     case COGL_PIXEL_FORMAT_BGRA_1010102_PRE:
750       G_PASTE (_cogl_pack_bgra_1010102_, component_size) (src, dst, width);
751       break;
752     case COGL_PIXEL_FORMAT_ARGB_2101010:
753     case COGL_PIXEL_FORMAT_ARGB_2101010_PRE:
754       G_PASTE (_cogl_pack_argb_2101010_, component_size) (src, dst, width);
755       break;
756     case COGL_PIXEL_FORMAT_ABGR_2101010:
757     case COGL_PIXEL_FORMAT_ABGR_2101010_PRE:
758       G_PASTE (_cogl_pack_abgr_2101010_, component_size) (src, dst, width);
759       break;
760     case COGL_PIXEL_FORMAT_DEPTH_16:
761     case COGL_PIXEL_FORMAT_DEPTH_32:
762     case COGL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
763     case COGL_PIXEL_FORMAT_ANY:
764     case COGL_PIXEL_FORMAT_YUV:
765       g_assert_not_reached ();
766     }
767 }
768