3 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
4 * 2005 Lars Knoll & Zack Rusin, Trolltech
5 * 2008 Aaron Plattner, NVIDIA Corporation
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 #include "pixman-private.h"
36 #include "pixman-accessor.h"
38 #define CONVERT_RGB24_TO_Y15(s) \
39 (((((s) >> 16) & 0xff) * 153 + \
40 (((s) >> 8) & 0xff) * 301 + \
41 (((s) ) & 0xff) * 58) >> 2)
43 #define CONVERT_RGB24_TO_RGB15(s) \
44 ((((s) >> 3) & 0x001f) | \
45 (((s) >> 6) & 0x03e0) | \
46 (((s) >> 9) & 0x7c00))
48 #define RGB15_TO_ENTRY(mif,rgb15) \
51 #define RGB24_TO_ENTRY(mif,rgb24) \
52 RGB15_TO_ENTRY (mif,CONVERT_RGB24_TO_RGB15 (rgb24))
54 #define RGB24_TO_ENTRY_Y(mif,rgb24) \
55 ((mif)->ent[CONVERT_RGB24_TO_Y15 (rgb24)])
59 #define FETCH_8(img,l,o) (READ (img, (((uint8_t *)(l)) + ((o) >> 3))))
61 #ifdef WORDS_BIGENDIAN
62 #define FETCH_4(img,l,o) \
63 (((4 * (o)) & 4) ? (FETCH_8 (img,l, 4 * (o)) & 0xf) : (FETCH_8 (img,l,(4 * (o))) >> 4))
65 #define FETCH_4(img,l,o) \
66 (((4 * (o)) & 4) ? (FETCH_8 (img, l, 4 * (o)) >> 4) : (FETCH_8 (img, l, (4 * (o))) & 0xf))
71 #define STORE_8(img,l,o,v) (WRITE (img, (uint8_t *)(l) + ((o) >> 3), (v)))
73 #ifdef WORDS_BIGENDIAN
74 #define STORE_4(img,l,o,v) \
78 int v4 = (v) & 0x0f; \
80 STORE_8 (img, l, bo, ( \
82 (FETCH_8 (img, l, bo) & 0xf0) | (v4) : \
83 (FETCH_8 (img, l, bo) & 0x0f) | (v4 << 4))); \
86 #define STORE_4(img,l,o,v) \
90 int v4 = (v) & 0x0f; \
92 STORE_8 (img, l, bo, ( \
94 (FETCH_8 (img, l, bo) & 0x0f) | (v4 << 4) : \
95 (FETCH_8 (img, l, bo) & 0xf0) | (v4))); \
100 * YV12 setup and access macros
103 #define YV12_SETUP(image) \
104 bits_image_t *__bits_image = (bits_image_t *)image; \
105 uint32_t *bits = __bits_image->bits; \
106 int stride = __bits_image->rowstride; \
107 int offset0 = stride < 0 ? \
108 ((-stride) >> 1) * ((__bits_image->height - 1) >> 1) - stride : \
109 stride * __bits_image->height; \
110 int offset1 = stride < 0 ? \
111 offset0 + ((-stride) >> 1) * ((__bits_image->height) >> 1) : \
112 offset0 + (offset0 >> 2)
114 /* Note no trailing semicolon on the above macro; if it's there, then
115 * the typical usage of YV12_SETUP(image); will have an extra trailing ;
116 * that some compilers will interpret as a statement -- and then any further
117 * variable declarations will cause an error.
120 #define YV12_Y(line) \
121 ((uint8_t *) ((bits) + (stride) * (line)))
123 #define YV12_U(line) \
124 ((uint8_t *) ((bits) + offset1 + \
125 ((stride) >> 1) * ((line) >> 1)))
127 #define YV12_V(line) \
128 ((uint8_t *) ((bits) + offset0 + \
129 ((stride) >> 1) * ((line) >> 1)))
133 static force_inline void
134 get_shifts (pixman_format_code_t format,
140 switch (PIXMAN_FORMAT_TYPE (format))
149 case PIXMAN_TYPE_ARGB:
151 *g = *b + PIXMAN_FORMAT_B (format);
152 *r = *g + PIXMAN_FORMAT_G (format);
153 *a = *r + PIXMAN_FORMAT_R (format);
156 case PIXMAN_TYPE_ABGR:
158 *g = *r + PIXMAN_FORMAT_R (format);
159 *b = *g + PIXMAN_FORMAT_G (format);
160 *a = *b + PIXMAN_FORMAT_B (format);
163 case PIXMAN_TYPE_BGRA:
164 /* With BGRA formats we start counting at the high end of the pixel */
165 *b = PIXMAN_FORMAT_BPP (format) - PIXMAN_FORMAT_B (format);
166 *g = *b - PIXMAN_FORMAT_B (format);
167 *r = *g - PIXMAN_FORMAT_G (format);
168 *a = *r - PIXMAN_FORMAT_R (format);
171 case PIXMAN_TYPE_RGBA:
172 /* With BGRA formats we start counting at the high end of the pixel */
173 *r = PIXMAN_FORMAT_BPP (format) - PIXMAN_FORMAT_R (format);
174 *g = *r - PIXMAN_FORMAT_R (format);
175 *b = *g - PIXMAN_FORMAT_G (format);
176 *a = *b - PIXMAN_FORMAT_B (format);
185 static force_inline uint32_t
186 convert_channel (uint32_t pixel, uint32_t def_value,
187 int n_from_bits, int from_shift,
188 int n_to_bits, int to_shift)
192 if (n_from_bits && n_to_bits)
193 v = unorm_to_unorm (pixel >> from_shift, n_from_bits, n_to_bits);
199 return (v & ((1 << n_to_bits) - 1)) << to_shift;
202 static force_inline uint32_t
203 convert_pixel (pixman_format_code_t from, pixman_format_code_t to, uint32_t pixel)
205 int a_from_shift, r_from_shift, g_from_shift, b_from_shift;
206 int a_to_shift, r_to_shift, g_to_shift, b_to_shift;
209 get_shifts (from, &a_from_shift, &r_from_shift, &g_from_shift, &b_from_shift);
210 get_shifts (to, &a_to_shift, &r_to_shift, &g_to_shift, &b_to_shift);
212 a = convert_channel (pixel, ~0,
213 PIXMAN_FORMAT_A (from), a_from_shift,
214 PIXMAN_FORMAT_A (to), a_to_shift);
216 r = convert_channel (pixel, 0,
217 PIXMAN_FORMAT_R (from), r_from_shift,
218 PIXMAN_FORMAT_R (to), r_to_shift);
220 g = convert_channel (pixel, 0,
221 PIXMAN_FORMAT_G (from), g_from_shift,
222 PIXMAN_FORMAT_G (to), g_to_shift);
224 b = convert_channel (pixel, 0,
225 PIXMAN_FORMAT_B (from), b_from_shift,
226 PIXMAN_FORMAT_B (to), b_to_shift);
228 return a | r | g | b;
231 static force_inline uint32_t
232 convert_pixel_from_a8r8g8b8 (pixman_format_code_t format, uint32_t pixel)
234 return convert_pixel (PIXMAN_a8r8g8b8, format, pixel);
237 static force_inline uint32_t
238 convert_pixel_to_a8r8g8b8 (pixman_format_code_t format, uint32_t pixel)
240 return convert_pixel (format, PIXMAN_a8r8g8b8, pixel);
243 static force_inline uint32_t
244 fetch_and_convert_pixel (pixman_image_t * image,
245 const uint8_t * bits,
247 pixman_format_code_t format)
251 switch (PIXMAN_FORMAT_BPP (format))
254 pixel = FETCH_4 (image, bits, offset);
258 pixel = READ (image, bits + offset);
262 pixel = READ (image, ((uint16_t *)bits + offset));
266 pixel = READ (image, ((uint32_t *)bits + offset));
270 pixel = 0xffff00ff; /* As ugly as possible to detect the bug */
274 return convert_pixel_to_a8r8g8b8 (format, pixel);
277 static force_inline void
278 convert_and_store_pixel (bits_image_t * image,
281 pixman_format_code_t format,
284 uint32_t converted = convert_pixel_from_a8r8g8b8 (format, pixel);
286 switch (PIXMAN_FORMAT_BPP (format))
289 STORE_4 (image, dest, offset, converted & 0xf);
293 WRITE (image, (dest + offset), converted & 0xff);
297 WRITE (image, ((uint16_t *)dest + offset), converted & 0xffff);
301 WRITE (image, ((uint32_t *)dest + offset), converted);
310 #define MAKE_ACCESSORS(format) \
312 fetch_scanline_ ## format (pixman_image_t *image, \
317 const uint32_t *mask) \
320 (uint8_t *)(image->bits.bits + y * image->bits.rowstride); \
323 for (i = 0; i < width; ++i) \
326 fetch_and_convert_pixel (image, bits, x + i, PIXMAN_ ## format); \
331 store_scanline_ ## format (bits_image_t * image, \
335 const uint32_t *values) \
338 (uint8_t *)(image->bits + y * image->rowstride); \
341 for (i = 0; i < width; ++i) \
343 convert_and_store_pixel ( \
344 image, dest, i + x, PIXMAN_ ## format, values[i]); \
349 fetch_pixel_ ## format (bits_image_t *image, \
354 (uint8_t *)(image->bits + line * image->rowstride); \
356 return fetch_and_convert_pixel ((pixman_image_t *)image, \
357 bits, offset, PIXMAN_ ## format); \
360 static const void *const __dummy__ ## format
362 MAKE_ACCESSORS(a8r8g8b8);
363 MAKE_ACCESSORS(x8r8g8b8);
364 MAKE_ACCESSORS(a8b8g8r8);
365 MAKE_ACCESSORS(x8b8g8r8);
366 MAKE_ACCESSORS(x14r6g6b6);
367 MAKE_ACCESSORS(b8g8r8a8);
368 MAKE_ACCESSORS(b8g8r8x8);
369 MAKE_ACCESSORS(r8g8b8x8);
370 MAKE_ACCESSORS(r8g8b8a8);
371 MAKE_ACCESSORS(r5g6b5);
372 MAKE_ACCESSORS(b5g6r5);
373 MAKE_ACCESSORS(a1r5g5b5);
374 MAKE_ACCESSORS(x1r5g5b5);
375 MAKE_ACCESSORS(a1b5g5r5);
376 MAKE_ACCESSORS(x1b5g5r5);
377 MAKE_ACCESSORS(a4r4g4b4);
378 MAKE_ACCESSORS(x4r4g4b4);
379 MAKE_ACCESSORS(a4b4g4r4);
380 MAKE_ACCESSORS(x4b4g4r4);
382 MAKE_ACCESSORS(r3g3b2);
383 MAKE_ACCESSORS(b2g3r3);
384 MAKE_ACCESSORS(a2r2g2b2);
385 MAKE_ACCESSORS(a2b2g2r2);
386 MAKE_ACCESSORS(x4a4);
388 MAKE_ACCESSORS(r1g2b1);
389 MAKE_ACCESSORS(b1g2r1);
390 MAKE_ACCESSORS(a1r1g1b1);
391 MAKE_ACCESSORS(a1b1g1r1);
393 /********************************** Fetch ************************************/
395 /* Expects a uint64_t buffer */
397 fetch_scanline_a2r10g10b10 (pixman_image_t *image,
402 const uint32_t *mask)
404 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
405 const uint32_t *pixel = bits + x;
406 const uint32_t *end = pixel + width;
407 uint64_t *buffer = (uint64_t *)b;
411 uint32_t p = READ (image, pixel++);
412 uint64_t a = p >> 30;
413 uint64_t r = (p >> 20) & 0x3ff;
414 uint64_t g = (p >> 10) & 0x3ff;
415 uint64_t b = p & 0x3ff;
426 *buffer++ = a << 48 | r << 32 | g << 16 | b;
430 /* Expects a uint64_t buffer */
432 fetch_scanline_x2r10g10b10 (pixman_image_t *image,
437 const uint32_t *mask)
439 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
440 const uint32_t *pixel = (uint32_t *)bits + x;
441 const uint32_t *end = pixel + width;
442 uint64_t *buffer = (uint64_t *)b;
446 uint32_t p = READ (image, pixel++);
447 uint64_t r = (p >> 20) & 0x3ff;
448 uint64_t g = (p >> 10) & 0x3ff;
449 uint64_t b = p & 0x3ff;
455 *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
459 /* Expects a uint64_t buffer */
461 fetch_scanline_a2b10g10r10 (pixman_image_t *image,
466 const uint32_t *mask)
468 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
469 const uint32_t *pixel = bits + x;
470 const uint32_t *end = pixel + width;
471 uint64_t *buffer = (uint64_t *)b;
475 uint32_t p = READ (image, pixel++);
476 uint64_t a = p >> 30;
477 uint64_t b = (p >> 20) & 0x3ff;
478 uint64_t g = (p >> 10) & 0x3ff;
479 uint64_t r = p & 0x3ff;
490 *buffer++ = a << 48 | r << 32 | g << 16 | b;
494 /* Expects a uint64_t buffer */
496 fetch_scanline_x2b10g10r10 (pixman_image_t *image,
501 const uint32_t *mask)
503 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
504 const uint32_t *pixel = (uint32_t *)bits + x;
505 const uint32_t *end = pixel + width;
506 uint64_t *buffer = (uint64_t *)b;
510 uint32_t p = READ (image, pixel++);
511 uint64_t b = (p >> 20) & 0x3ff;
512 uint64_t g = (p >> 10) & 0x3ff;
513 uint64_t r = p & 0x3ff;
519 *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
524 fetch_scanline_r8g8b8 (pixman_image_t *image,
529 const uint32_t *mask)
531 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
532 const uint8_t *pixel = (const uint8_t *)bits + 3 * x;
533 const uint8_t *end = pixel + 3 * width;
537 uint32_t b = 0xff000000;
539 #ifdef WORDS_BIGENDIAN
540 b |= (READ (image, pixel++) << 16);
541 b |= (READ (image, pixel++) << 8);
542 b |= (READ (image, pixel++));
544 b |= (READ (image, pixel++));
545 b |= (READ (image, pixel++) << 8);
546 b |= (READ (image, pixel++) << 16);
554 fetch_scanline_b8g8r8 (pixman_image_t *image,
559 const uint32_t *mask)
561 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
562 const uint8_t *pixel = (const uint8_t *)bits + 3 * x;
563 const uint8_t *end = pixel + 3 * width;
567 uint32_t b = 0xff000000;
568 #ifdef WORDS_BIGENDIAN
569 b |= (READ (image, pixel++));
570 b |= (READ (image, pixel++) << 8);
571 b |= (READ (image, pixel++) << 16);
573 b |= (READ (image, pixel++) << 16);
574 b |= (READ (image, pixel++) << 8);
575 b |= (READ (image, pixel++));
582 fetch_scanline_c8 (pixman_image_t *image,
587 const uint32_t *mask)
589 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
590 const pixman_indexed_t * indexed = image->bits.indexed;
591 const uint8_t *pixel = (const uint8_t *)bits + x;
592 const uint8_t *end = pixel + width;
596 uint32_t p = READ (image, pixel++);
598 *buffer++ = indexed->rgba[p];
603 fetch_scanline_c4 (pixman_image_t *image,
608 const uint32_t *mask)
610 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
611 const pixman_indexed_t * indexed = image->bits.indexed;
614 for (i = 0; i < width; ++i)
616 uint32_t p = FETCH_4 (image, bits, i + x);
618 *buffer++ = indexed->rgba[p];
623 fetch_scanline_a1 (pixman_image_t *image,
628 const uint32_t *mask)
630 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
633 for (i = 0; i < width; ++i)
635 uint32_t p = READ (image, bits + ((i + x) >> 5));
638 #ifdef WORDS_BIGENDIAN
639 a = p >> (0x1f - ((i + x) & 0x1f));
641 a = p >> ((i + x) & 0x1f);
653 fetch_scanline_g1 (pixman_image_t *image,
658 const uint32_t *mask)
660 const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
661 const pixman_indexed_t * indexed = image->bits.indexed;
664 for (i = 0; i < width; ++i)
666 uint32_t p = READ (image, bits + ((i + x) >> 5));
669 #ifdef WORDS_BIGENDIAN
670 a = p >> (0x1f - ((i + x) & 0x1f));
672 a = p >> ((i + x) & 0x1f);
676 *buffer++ = indexed->rgba[a];
681 fetch_scanline_yuy2 (pixman_image_t *image,
686 const uint32_t *mask)
688 const uint32_t *bits = image->bits.bits + image->bits.rowstride * line;
691 for (i = 0; i < width; i++)
696 y = ((uint8_t *) bits)[(x + i) << 1] - 16;
697 u = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 1] - 128;
698 v = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 3] - 128;
700 /* R = 1.164(Y - 16) + 1.596(V - 128) */
701 r = 0x012b27 * y + 0x019a2e * v;
702 /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
703 g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
704 /* B = 1.164(Y - 16) + 2.018(U - 128) */
705 b = 0x012b27 * y + 0x0206a2 * u;
707 *buffer++ = 0xff000000 |
708 (r >= 0 ? r < 0x1000000 ? r & 0xff0000 : 0xff0000 : 0) |
709 (g >= 0 ? g < 0x1000000 ? (g >> 8) & 0x00ff00 : 0x00ff00 : 0) |
710 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
715 fetch_scanline_yv12 (pixman_image_t *image,
720 const uint32_t *mask)
723 uint8_t *y_line = YV12_Y (line);
724 uint8_t *u_line = YV12_U (line);
725 uint8_t *v_line = YV12_V (line);
728 for (i = 0; i < width; i++)
733 y = y_line[x + i] - 16;
734 u = u_line[(x + i) >> 1] - 128;
735 v = v_line[(x + i) >> 1] - 128;
737 /* R = 1.164(Y - 16) + 1.596(V - 128) */
738 r = 0x012b27 * y + 0x019a2e * v;
739 /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
740 g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
741 /* B = 1.164(Y - 16) + 2.018(U - 128) */
742 b = 0x012b27 * y + 0x0206a2 * u;
744 *buffer++ = 0xff000000 |
745 (r >= 0 ? r < 0x1000000 ? r & 0xff0000 : 0xff0000 : 0) |
746 (g >= 0 ? g < 0x1000000 ? (g >> 8) & 0x00ff00 : 0x00ff00 : 0) |
747 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
751 /**************************** Pixel wise fetching *****************************/
753 /* Despite the type, expects a uint64_t buffer */
755 fetch_pixel_a2r10g10b10 (bits_image_t *image,
759 uint32_t *bits = image->bits + line * image->rowstride;
760 uint32_t p = READ (image, bits + offset);
761 uint64_t a = p >> 30;
762 uint64_t r = (p >> 20) & 0x3ff;
763 uint64_t g = (p >> 10) & 0x3ff;
764 uint64_t b = p & 0x3ff;
775 return a << 48 | r << 32 | g << 16 | b;
778 /* Despite the type, this function expects a uint64_t buffer */
780 fetch_pixel_x2r10g10b10 (bits_image_t *image,
784 uint32_t *bits = image->bits + line * image->rowstride;
785 uint32_t p = READ (image, bits + offset);
786 uint64_t r = (p >> 20) & 0x3ff;
787 uint64_t g = (p >> 10) & 0x3ff;
788 uint64_t b = p & 0x3ff;
794 return 0xffffULL << 48 | r << 32 | g << 16 | b;
797 /* Despite the type, expects a uint64_t buffer */
799 fetch_pixel_a2b10g10r10 (bits_image_t *image,
803 uint32_t *bits = image->bits + line * image->rowstride;
804 uint32_t p = READ (image, bits + offset);
805 uint64_t a = p >> 30;
806 uint64_t b = (p >> 20) & 0x3ff;
807 uint64_t g = (p >> 10) & 0x3ff;
808 uint64_t r = p & 0x3ff;
819 return a << 48 | r << 32 | g << 16 | b;
822 /* Despite the type, this function expects a uint64_t buffer */
824 fetch_pixel_x2b10g10r10 (bits_image_t *image,
828 uint32_t *bits = image->bits + line * image->rowstride;
829 uint32_t p = READ (image, bits + offset);
830 uint64_t b = (p >> 20) & 0x3ff;
831 uint64_t g = (p >> 10) & 0x3ff;
832 uint64_t r = p & 0x3ff;
838 return 0xffffULL << 48 | r << 32 | g << 16 | b;
842 fetch_pixel_r8g8b8 (bits_image_t *image,
846 uint32_t *bits = image->bits + line * image->rowstride;
847 uint8_t *pixel = ((uint8_t *) bits) + (offset * 3);
849 #ifdef WORDS_BIGENDIAN
851 (READ (image, pixel + 0) << 16) |
852 (READ (image, pixel + 1) << 8) |
853 (READ (image, pixel + 2)));
856 (READ (image, pixel + 2) << 16) |
857 (READ (image, pixel + 1) << 8) |
858 (READ (image, pixel + 0)));
863 fetch_pixel_b8g8r8 (bits_image_t *image,
867 uint32_t *bits = image->bits + line * image->rowstride;
868 uint8_t *pixel = ((uint8_t *) bits) + (offset * 3);
869 #ifdef WORDS_BIGENDIAN
871 (READ (image, pixel + 2) << 16) |
872 (READ (image, pixel + 1) << 8) |
873 (READ (image, pixel + 0)));
876 (READ (image, pixel + 0) << 16) |
877 (READ (image, pixel + 1) << 8) |
878 (READ (image, pixel + 2)));
883 fetch_pixel_c8 (bits_image_t *image,
887 uint32_t *bits = image->bits + line * image->rowstride;
888 uint32_t pixel = READ (image, (uint8_t *) bits + offset);
889 const pixman_indexed_t * indexed = image->indexed;
891 return indexed->rgba[pixel];
895 fetch_pixel_c4 (bits_image_t *image,
899 uint32_t *bits = image->bits + line * image->rowstride;
900 uint32_t pixel = FETCH_4 (image, bits, offset);
901 const pixman_indexed_t * indexed = image->indexed;
903 return indexed->rgba[pixel];
907 fetch_pixel_a1 (bits_image_t *image,
911 uint32_t *bits = image->bits + line * image->rowstride;
912 uint32_t pixel = READ (image, bits + (offset >> 5));
915 #ifdef WORDS_BIGENDIAN
916 a = pixel >> (0x1f - (offset & 0x1f));
918 a = pixel >> (offset & 0x1f);
929 fetch_pixel_g1 (bits_image_t *image,
933 uint32_t *bits = image->bits + line * image->rowstride;
934 uint32_t pixel = READ (image, bits + (offset >> 5));
935 const pixman_indexed_t * indexed = image->indexed;
938 #ifdef WORDS_BIGENDIAN
939 a = pixel >> (0x1f - (offset & 0x1f));
941 a = pixel >> (offset & 0x1f);
945 return indexed->rgba[a];
949 fetch_pixel_yuy2 (bits_image_t *image,
953 const uint32_t *bits = image->bits + image->rowstride * line;
958 y = ((uint8_t *) bits)[offset << 1] - 16;
959 u = ((uint8_t *) bits)[((offset << 1) & - 4) + 1] - 128;
960 v = ((uint8_t *) bits)[((offset << 1) & - 4) + 3] - 128;
962 /* R = 1.164(Y - 16) + 1.596(V - 128) */
963 r = 0x012b27 * y + 0x019a2e * v;
965 /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
966 g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
968 /* B = 1.164(Y - 16) + 2.018(U - 128) */
969 b = 0x012b27 * y + 0x0206a2 * u;
972 (r >= 0 ? r < 0x1000000 ? r & 0xff0000 : 0xff0000 : 0) |
973 (g >= 0 ? g < 0x1000000 ? (g >> 8) & 0x00ff00 : 0x00ff00 : 0) |
974 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
978 fetch_pixel_yv12 (bits_image_t *image,
983 int16_t y = YV12_Y (line)[offset] - 16;
984 int16_t u = YV12_U (line)[offset >> 1] - 128;
985 int16_t v = YV12_V (line)[offset >> 1] - 128;
988 /* R = 1.164(Y - 16) + 1.596(V - 128) */
989 r = 0x012b27 * y + 0x019a2e * v;
991 /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
992 g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
994 /* B = 1.164(Y - 16) + 2.018(U - 128) */
995 b = 0x012b27 * y + 0x0206a2 * u;
998 (r >= 0 ? r < 0x1000000 ? r & 0xff0000 : 0xff0000 : 0) |
999 (g >= 0 ? g < 0x1000000 ? (g >> 8) & 0x00ff00 : 0x00ff00 : 0) |
1000 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1003 /*********************************** Store ************************************/
1006 store_scanline_a2r10g10b10 (bits_image_t * image,
1012 uint32_t *bits = image->bits + image->rowstride * y;
1013 uint32_t *pixel = bits + x;
1014 uint64_t *values = (uint64_t *)v;
1017 for (i = 0; i < width; ++i)
1019 WRITE (image, pixel++,
1020 ((values[i] >> 32) & 0xc0000000) |
1021 ((values[i] >> 18) & 0x3ff00000) |
1022 ((values[i] >> 12) & 0xffc00) |
1023 ((values[i] >> 6) & 0x3ff));
1028 store_scanline_x2r10g10b10 (bits_image_t * image,
1034 uint32_t *bits = image->bits + image->rowstride * y;
1035 uint64_t *values = (uint64_t *)v;
1036 uint32_t *pixel = bits + x;
1039 for (i = 0; i < width; ++i)
1041 WRITE (image, pixel++,
1042 ((values[i] >> 18) & 0x3ff00000) |
1043 ((values[i] >> 12) & 0xffc00) |
1044 ((values[i] >> 6) & 0x3ff));
1049 store_scanline_a2b10g10r10 (bits_image_t * image,
1055 uint32_t *bits = image->bits + image->rowstride * y;
1056 uint32_t *pixel = bits + x;
1057 uint64_t *values = (uint64_t *)v;
1060 for (i = 0; i < width; ++i)
1062 WRITE (image, pixel++,
1063 ((values[i] >> 32) & 0xc0000000) |
1064 ((values[i] >> 38) & 0x3ff) |
1065 ((values[i] >> 12) & 0xffc00) |
1066 ((values[i] << 14) & 0x3ff00000));
1071 store_scanline_x2b10g10r10 (bits_image_t * image,
1077 uint32_t *bits = image->bits + image->rowstride * y;
1078 uint64_t *values = (uint64_t *)v;
1079 uint32_t *pixel = bits + x;
1082 for (i = 0; i < width; ++i)
1084 WRITE (image, pixel++,
1085 ((values[i] >> 38) & 0x3ff) |
1086 ((values[i] >> 12) & 0xffc00) |
1087 ((values[i] << 14) & 0x3ff00000));
1092 store_scanline_r8g8b8 (bits_image_t * image,
1096 const uint32_t *values)
1098 uint32_t *bits = image->bits + image->rowstride * y;
1099 uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
1102 for (i = 0; i < width; ++i)
1104 uint32_t val = values[i];
1106 #ifdef WORDS_BIGENDIAN
1107 WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
1108 WRITE (image, pixel++, (val & 0x0000ff00) >> 8);
1109 WRITE (image, pixel++, (val & 0x000000ff) >> 0);
1111 WRITE (image, pixel++, (val & 0x000000ff) >> 0);
1112 WRITE (image, pixel++, (val & 0x0000ff00) >> 8);
1113 WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
1119 store_scanline_b8g8r8 (bits_image_t * image,
1123 const uint32_t *values)
1125 uint32_t *bits = image->bits + image->rowstride * y;
1126 uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
1129 for (i = 0; i < width; ++i)
1131 uint32_t val = values[i];
1133 #ifdef WORDS_BIGENDIAN
1134 WRITE (image, pixel++, (val & 0x000000ff) >> 0);
1135 WRITE (image, pixel++, (val & 0x0000ff00) >> 8);
1136 WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
1138 WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
1139 WRITE (image, pixel++, (val & 0x0000ff00) >> 8);
1140 WRITE (image, pixel++, (val & 0x000000ff) >> 0);
1146 store_scanline_c8 (bits_image_t * image,
1150 const uint32_t *values)
1152 uint32_t *bits = image->bits + image->rowstride * y;
1153 uint8_t *pixel = ((uint8_t *) bits) + x;
1154 const pixman_indexed_t *indexed = image->indexed;
1157 for (i = 0; i < width; ++i)
1158 WRITE (image, pixel++, RGB24_TO_ENTRY (indexed,values[i]));
1162 store_scanline_g8 (bits_image_t * image,
1166 const uint32_t *values)
1168 uint32_t *bits = image->bits + image->rowstride * y;
1169 uint8_t *pixel = ((uint8_t *) bits) + x;
1170 const pixman_indexed_t *indexed = image->indexed;
1173 for (i = 0; i < width; ++i)
1174 WRITE (image, pixel++, RGB24_TO_ENTRY_Y (indexed,values[i]));
1178 store_scanline_c4 (bits_image_t * image,
1182 const uint32_t *values)
1184 uint32_t *bits = image->bits + image->rowstride * y;
1185 const pixman_indexed_t *indexed = image->indexed;
1188 for (i = 0; i < width; ++i)
1192 pixel = RGB24_TO_ENTRY (indexed, values[i]);
1193 STORE_4 (image, bits, i + x, pixel);
1198 store_scanline_g4 (bits_image_t * image,
1202 const uint32_t *values)
1204 uint32_t *bits = image->bits + image->rowstride * y;
1205 const pixman_indexed_t *indexed = image->indexed;
1208 for (i = 0; i < width; ++i)
1212 pixel = RGB24_TO_ENTRY_Y (indexed, values[i]);
1213 STORE_4 (image, bits, i + x, pixel);
1218 store_scanline_a1 (bits_image_t * image,
1222 const uint32_t *values)
1224 uint32_t *bits = image->bits + image->rowstride * y;
1227 for (i = 0; i < width; ++i)
1229 uint32_t *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
1232 #ifdef WORDS_BIGENDIAN
1233 mask = 1 << (0x1f - ((i + x) & 0x1f));
1235 mask = 1 << ((i + x) & 0x1f);
1237 v = values[i] & 0x80000000 ? mask : 0;
1239 WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
1244 store_scanline_g1 (bits_image_t * image,
1248 const uint32_t *values)
1250 uint32_t *bits = image->bits + image->rowstride * y;
1251 const pixman_indexed_t *indexed = image->indexed;
1254 for (i = 0; i < width; ++i)
1256 uint32_t *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
1259 #ifdef WORDS_BIGENDIAN
1260 mask = 1 << (0x1f - ((i + x) & 0x1f));
1262 mask = 1 << ((i + x) & 0x1f);
1264 v = RGB24_TO_ENTRY_Y (indexed, values[i]) & 0x1 ? mask : 0;
1266 WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
1271 * Contracts a 64bpp image to 32bpp and then stores it using a regular 32-bit
1272 * store proc. Despite the type, this function expects a uint64_t buffer.
1275 store_scanline_generic_64 (bits_image_t * image,
1279 const uint32_t *values)
1281 uint32_t *argb8_pixels;
1283 assert (image->common.type == BITS);
1285 argb8_pixels = pixman_malloc_ab (width, sizeof(uint32_t));
1289 /* Contract the scanline. We could do this in place if values weren't
1292 pixman_contract (argb8_pixels, (uint64_t *)values, width);
1294 image->store_scanline_32 (image, x, y, width, argb8_pixels);
1296 free (argb8_pixels);
1299 /* Despite the type, this function expects both buffer
1300 * and mask to be uint64_t
1303 fetch_scanline_generic_64 (pixman_image_t *image,
1308 const uint32_t *mask)
1310 pixman_format_code_t format;
1312 /* Fetch the pixels into the first half of buffer and then expand them in
1315 image->bits.fetch_scanline_32 (image, x, y, width, buffer, NULL);
1317 format = image->bits.format;
1318 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR ||
1319 PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY)
1321 /* Indexed formats are mapped to a8r8g8b8 with full
1322 * precision, so when expanding we shouldn't correct
1323 * for the width of the channels
1326 format = PIXMAN_a8r8g8b8;
1329 pixman_expand ((uint64_t *)buffer, buffer, format, width);
1332 /* Despite the type, this function expects a uint64_t *buffer */
1334 fetch_pixel_generic_64 (bits_image_t *image,
1338 uint32_t pixel32 = image->fetch_pixel_32 (image, offset, line);
1340 pixman_format_code_t format;
1342 format = image->format;
1343 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR ||
1344 PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY)
1346 /* Indexed formats are mapped to a8r8g8b8 with full
1347 * precision, so when expanding we shouldn't correct
1348 * for the width of the channels
1351 format = PIXMAN_a8r8g8b8;
1354 pixman_expand ((uint64_t *)&result, &pixel32, format, 1);
1360 * XXX: The transformed fetch path only works at 32-bpp so far. When all
1361 * paths have wide versions, this can be removed.
1363 * WARNING: This function loses precision!
1366 fetch_pixel_generic_lossy_32 (bits_image_t *image,
1370 uint64_t pixel64 = image->fetch_pixel_64 (image, offset, line);
1373 pixman_contract (&result, &pixel64, 1);
1380 pixman_format_code_t format;
1381 fetch_scanline_t fetch_scanline_32;
1382 fetch_scanline_t fetch_scanline_64;
1383 fetch_pixel_32_t fetch_pixel_32;
1384 fetch_pixel_64_t fetch_pixel_64;
1385 store_scanline_t store_scanline_32;
1386 store_scanline_t store_scanline_64;
1389 #define FORMAT_INFO(format) \
1391 PIXMAN_ ## format, \
1392 fetch_scanline_ ## format, \
1393 fetch_scanline_generic_64, \
1394 fetch_pixel_ ## format, fetch_pixel_generic_64, \
1395 store_scanline_ ## format, store_scanline_generic_64 \
1398 static const format_info_t accessors[] =
1400 /* 32 bpp formats */
1401 FORMAT_INFO (a8r8g8b8),
1402 FORMAT_INFO (x8r8g8b8),
1403 FORMAT_INFO (a8b8g8r8),
1404 FORMAT_INFO (x8b8g8r8),
1405 FORMAT_INFO (b8g8r8a8),
1406 FORMAT_INFO (b8g8r8x8),
1407 FORMAT_INFO (r8g8b8a8),
1408 FORMAT_INFO (r8g8b8x8),
1409 FORMAT_INFO (x14r6g6b6),
1412 FORMAT_INFO (r8g8b8),
1413 FORMAT_INFO (b8g8r8),
1416 FORMAT_INFO (r5g6b5),
1417 FORMAT_INFO (b5g6r5),
1419 FORMAT_INFO (a1r5g5b5),
1420 FORMAT_INFO (x1r5g5b5),
1421 FORMAT_INFO (a1b5g5r5),
1422 FORMAT_INFO (x1b5g5r5),
1423 FORMAT_INFO (a4r4g4b4),
1424 FORMAT_INFO (x4r4g4b4),
1425 FORMAT_INFO (a4b4g4r4),
1426 FORMAT_INFO (x4b4g4r4),
1430 FORMAT_INFO (r3g3b2),
1431 FORMAT_INFO (b2g3r3),
1432 FORMAT_INFO (a2r2g2b2),
1433 FORMAT_INFO (a2b2g2r2),
1437 #define fetch_scanline_g8 fetch_scanline_c8
1438 #define fetch_pixel_g8 fetch_pixel_c8
1441 #define fetch_scanline_x4c4 fetch_scanline_c8
1442 #define fetch_pixel_x4c4 fetch_pixel_c8
1443 #define store_scanline_x4c4 store_scanline_c8
1446 #define fetch_scanline_x4g4 fetch_scanline_c8
1447 #define fetch_pixel_x4g4 fetch_pixel_c8
1448 #define store_scanline_x4g4 store_scanline_g8
1455 FORMAT_INFO (r1g2b1),
1456 FORMAT_INFO (b1g2r1),
1457 FORMAT_INFO (a1r1g1b1),
1458 FORMAT_INFO (a1b1g1r1),
1462 #define fetch_scanline_g4 fetch_scanline_c4
1463 #define fetch_pixel_g4 fetch_pixel_c4
1472 { PIXMAN_a2r10g10b10,
1473 NULL, fetch_scanline_a2r10g10b10,
1474 fetch_pixel_generic_lossy_32, fetch_pixel_a2r10g10b10,
1475 NULL, store_scanline_a2r10g10b10 },
1477 { PIXMAN_x2r10g10b10,
1478 NULL, fetch_scanline_x2r10g10b10,
1479 fetch_pixel_generic_lossy_32, fetch_pixel_x2r10g10b10,
1480 NULL, store_scanline_x2r10g10b10 },
1482 { PIXMAN_a2b10g10r10,
1483 NULL, fetch_scanline_a2b10g10r10,
1484 fetch_pixel_generic_lossy_32, fetch_pixel_a2b10g10r10,
1485 NULL, store_scanline_a2b10g10r10 },
1487 { PIXMAN_x2b10g10r10,
1488 NULL, fetch_scanline_x2b10g10r10,
1489 fetch_pixel_generic_lossy_32, fetch_pixel_x2b10g10r10,
1490 NULL, store_scanline_x2b10g10r10 },
1494 fetch_scanline_yuy2, fetch_scanline_generic_64,
1495 fetch_pixel_yuy2, fetch_pixel_generic_64,
1499 fetch_scanline_yv12, fetch_scanline_generic_64,
1500 fetch_pixel_yv12, fetch_pixel_generic_64,
1507 setup_accessors (bits_image_t *image)
1509 const format_info_t *info = accessors;
1511 while (info->format != PIXMAN_null)
1513 if (info->format == image->format)
1515 image->fetch_scanline_32 = info->fetch_scanline_32;
1516 image->fetch_scanline_64 = info->fetch_scanline_64;
1517 image->fetch_pixel_32 = info->fetch_pixel_32;
1518 image->fetch_pixel_64 = info->fetch_pixel_64;
1519 image->store_scanline_32 = info->store_scanline_32;
1520 image->store_scanline_64 = info->store_scanline_64;
1529 #ifndef PIXMAN_FB_ACCESSORS
1531 _pixman_bits_image_setup_accessors_accessors (bits_image_t *image);
1534 _pixman_bits_image_setup_accessors (bits_image_t *image)
1536 if (image->read_func || image->write_func)
1537 _pixman_bits_image_setup_accessors_accessors (image);
1539 setup_accessors (image);
1545 _pixman_bits_image_setup_accessors_accessors (bits_image_t *image)
1547 setup_accessors (image);