Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *
  3.  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
  4.  *             2005 Lars Knoll & Zack Rusin, Trolltech
  5.  *             2008 Aaron Plattner, NVIDIA Corporation
  6.  *
  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.
  16.  *
  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
  24.  * SOFTWARE.
  25.  */
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <assert.h>
  34.  
  35. #include "pixman-private.h"
  36. #include "pixman-accessor.h"
  37.  
  38. #define CONVERT_RGB24_TO_Y15(s)                                         \
  39.     (((((s) >> 16) & 0xff) * 153 +                                      \
  40.       (((s) >>  8) & 0xff) * 301 +                                      \
  41.       (((s)      ) & 0xff) * 58) >> 2)
  42.  
  43. #define CONVERT_RGB24_TO_RGB15(s)                                       \
  44.     ((((s) >> 3) & 0x001f) |                                            \
  45.      (((s) >> 6) & 0x03e0) |                                            \
  46.      (((s) >> 9) & 0x7c00))
  47.  
  48. #define RGB15_TO_ENTRY(mif,rgb15)                                       \
  49.     ((mif)->ent[rgb15])
  50.  
  51. #define RGB24_TO_ENTRY(mif,rgb24)                                       \
  52.     RGB15_TO_ENTRY (mif,CONVERT_RGB24_TO_RGB15 (rgb24))
  53.  
  54. #define RGB24_TO_ENTRY_Y(mif,rgb24)                                     \
  55.     ((mif)->ent[CONVERT_RGB24_TO_Y15 (rgb24)])
  56.  
  57. /*
  58.  * YV12 setup and access macros
  59.  */
  60.  
  61. #define YV12_SETUP(image)                                               \
  62.     bits_image_t *__bits_image = (bits_image_t *)image;                 \
  63.     uint32_t *bits = __bits_image->bits;                                \
  64.     int stride = __bits_image->rowstride;                               \
  65.     int offset0 = stride < 0 ?                                          \
  66.     ((-stride) >> 1) * ((__bits_image->height - 1) >> 1) - stride :     \
  67.     stride * __bits_image->height;                                      \
  68.     int offset1 = stride < 0 ?                                          \
  69.     offset0 + ((-stride) >> 1) * ((__bits_image->height) >> 1) :        \
  70.         offset0 + (offset0 >> 2)
  71.  
  72. /* Note no trailing semicolon on the above macro; if it's there, then
  73.  * the typical usage of YV12_SETUP(image); will have an extra trailing ;
  74.  * that some compilers will interpret as a statement -- and then any further
  75.  * variable declarations will cause an error.
  76.  */
  77.  
  78. #define YV12_Y(line)                                                    \
  79.     ((uint8_t *) ((bits) + (stride) * (line)))
  80.  
  81. #define YV12_U(line)                                                    \
  82.     ((uint8_t *) ((bits) + offset1 +                                    \
  83.                   ((stride) >> 1) * ((line) >> 1)))
  84.  
  85. #define YV12_V(line)                                                    \
  86.     ((uint8_t *) ((bits) + offset0 +                                    \
  87.                   ((stride) >> 1) * ((line) >> 1)))
  88.  
  89. /********************************** Fetch ************************************/
  90.  
  91. static void
  92. fetch_scanline_a8r8g8b8 (pixman_image_t *image,
  93.                          int             x,
  94.                          int             y,
  95.                          int             width,
  96.                          uint32_t *      buffer,
  97.                          const uint32_t *mask)
  98. {
  99.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  100.    
  101.     MEMCPY_WRAPPED (image,
  102.                     buffer, (const uint32_t *)bits + x,
  103.                     width * sizeof(uint32_t));
  104. }
  105.  
  106. static void
  107. fetch_scanline_x8r8g8b8 (pixman_image_t *image,
  108.                          int             x,
  109.                          int             y,
  110.                          int             width,
  111.                          uint32_t *      buffer,
  112.                          const uint32_t *mask)
  113. {
  114.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  115.     const uint32_t *pixel = (const uint32_t *)bits + x;
  116.     const uint32_t *end = pixel + width;
  117.    
  118.     while (pixel < end)
  119.         *buffer++ = READ (image, pixel++) | 0xff000000;
  120. }
  121.  
  122. static void
  123. fetch_scanline_a8b8g8r8 (pixman_image_t *image,
  124.                          int             x,
  125.                          int             y,
  126.                          int             width,
  127.                          uint32_t *      buffer,
  128.                          const uint32_t *mask)
  129. {
  130.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  131.     const uint32_t *pixel = (uint32_t *)bits + x;
  132.     const uint32_t *end = pixel + width;
  133.    
  134.     while (pixel < end)
  135.     {
  136.         uint32_t p = READ (image, pixel++);
  137.        
  138.         *buffer++ = (p & 0xff00ff00)    |
  139.             ((p >> 16) & 0xff)          |
  140.             ((p & 0xff) << 16);
  141.     }
  142. }
  143.  
  144. static void
  145. fetch_scanline_x8b8g8r8 (pixman_image_t *image,
  146.                          int             x,
  147.                          int             y,
  148.                          int             width,
  149.                          uint32_t *      buffer,
  150.                          const uint32_t *mask)
  151. {
  152.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  153.     const uint32_t *pixel = (uint32_t *)bits + x;
  154.     const uint32_t *end = pixel + width;
  155.    
  156.     while (pixel < end)
  157.     {
  158.         uint32_t p = READ (image, pixel++);
  159.        
  160.         *buffer++ = 0xff000000          |
  161.             (p & 0x0000ff00)            |
  162.             ((p >> 16) & 0xff)          |
  163.             ((p & 0xff) << 16);
  164.     }
  165. }
  166.  
  167. static void
  168. fetch_scanline_b8g8r8a8 (pixman_image_t *image,
  169.                          int             x,
  170.                          int             y,
  171.                          int             width,
  172.                          uint32_t *      buffer,
  173.                          const uint32_t *mask)
  174. {
  175.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  176.     const uint32_t *pixel = (uint32_t *)bits + x;
  177.     const uint32_t *end = pixel + width;
  178.  
  179.     while (pixel < end)
  180.     {
  181.         uint32_t p = READ (image, pixel++);
  182.  
  183.         *buffer++ = (((p & 0xff000000) >> 24)   |
  184.                      ((p & 0x00ff0000) >> 8)    |
  185.                      ((p & 0x0000ff00) << 8)    |
  186.                      ((p & 0x000000ff) << 24));
  187.     }
  188. }
  189.  
  190. static void
  191. fetch_scanline_b8g8r8x8 (pixman_image_t *image,
  192.                          int             x,
  193.                          int             y,
  194.                          int             width,
  195.                          uint32_t *      buffer,
  196.                          const uint32_t *mask)
  197. {
  198.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  199.     const uint32_t *pixel = (uint32_t *)bits + x;
  200.     const uint32_t *end = pixel + width;
  201.    
  202.     while (pixel < end)
  203.     {
  204.         uint32_t p = READ (image, pixel++);
  205.        
  206.         *buffer++ = (0xff000000 |
  207.                      ((p & 0xff000000) >> 24)   |
  208.                      ((p & 0x00ff0000) >> 8)    |
  209.                      ((p & 0x0000ff00) << 8));
  210.     }
  211. }
  212.  
  213. static void
  214. fetch_scanline_x14r6g6b6 (pixman_image_t *image,
  215.                           int             x,
  216.                           int             y,
  217.                           int             width,
  218.                           uint32_t *      buffer,
  219.                           const uint32_t *mask)
  220. {
  221.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  222.     const uint32_t *pixel = (const uint32_t *)bits + x;
  223.     const uint32_t *end = pixel + width;
  224.  
  225.     while (pixel < end)
  226.     {
  227.         uint32_t p = READ (image, pixel++);
  228.         uint32_t r, g, b;
  229.  
  230.         r = ((p & 0x3f000) << 6) | ((p & 0x30000));
  231.         g = ((p & 0x00fc0) << 4) | ((p & 0x00c00) >> 2);
  232.         b = ((p & 0x0003f) << 2) | ((p & 0x00030) >> 4);
  233.  
  234.         *buffer++ = 0xff000000 | r | g | b;
  235.     }
  236. }
  237.  
  238. /* Expects a uint64_t buffer */
  239. static void
  240. fetch_scanline_a2r10g10b10 (pixman_image_t *image,
  241.                             int             x,
  242.                             int             y,
  243.                             int             width,
  244.                             uint32_t *      b,
  245.                             const uint32_t *mask)
  246. {
  247.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  248.     const uint32_t *pixel = bits + x;
  249.     const uint32_t *end = pixel + width;
  250.     uint64_t *buffer = (uint64_t *)b;
  251.  
  252.     while (pixel < end)
  253.     {
  254.         uint32_t p = READ (image, pixel++);
  255.         uint64_t a = p >> 30;
  256.         uint64_t r = (p >> 20) & 0x3ff;
  257.         uint64_t g = (p >> 10) & 0x3ff;
  258.         uint64_t b = p & 0x3ff;
  259.  
  260.         r = r << 6 | r >> 4;
  261.         g = g << 6 | g >> 4;
  262.         b = b << 6 | b >> 4;
  263.  
  264.         a <<= 14;
  265.         a |= a >> 2;
  266.         a |= a >> 4;
  267.         a |= a >> 8;
  268.  
  269.         *buffer++ = a << 48 | r << 32 | g << 16 | b;
  270.     }
  271. }
  272.  
  273. /* Expects a uint64_t buffer */
  274. static void
  275. fetch_scanline_x2r10g10b10 (pixman_image_t *image,
  276.                             int             x,
  277.                             int             y,
  278.                             int             width,
  279.                             uint32_t *      b,
  280.                             const uint32_t *mask)
  281. {
  282.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  283.     const uint32_t *pixel = (uint32_t *)bits + x;
  284.     const uint32_t *end = pixel + width;
  285.     uint64_t *buffer = (uint64_t *)b;
  286.    
  287.     while (pixel < end)
  288.     {
  289.         uint32_t p = READ (image, pixel++);
  290.         uint64_t r = (p >> 20) & 0x3ff;
  291.         uint64_t g = (p >> 10) & 0x3ff;
  292.         uint64_t b = p & 0x3ff;
  293.        
  294.         r = r << 6 | r >> 4;
  295.         g = g << 6 | g >> 4;
  296.         b = b << 6 | b >> 4;
  297.        
  298.         *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
  299.     }
  300. }
  301.  
  302. /* Expects a uint64_t buffer */
  303. static void
  304. fetch_scanline_a2b10g10r10 (pixman_image_t *image,
  305.                             int             x,
  306.                             int             y,
  307.                             int             width,
  308.                             uint32_t *      b,
  309.                             const uint32_t *mask)
  310. {
  311.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  312.     const uint32_t *pixel = bits + x;
  313.     const uint32_t *end = pixel + width;
  314.     uint64_t *buffer = (uint64_t *)b;
  315.    
  316.     while (pixel < end)
  317.     {
  318.         uint32_t p = READ (image, pixel++);
  319.         uint64_t a = p >> 30;
  320.         uint64_t b = (p >> 20) & 0x3ff;
  321.         uint64_t g = (p >> 10) & 0x3ff;
  322.         uint64_t r = p & 0x3ff;
  323.        
  324.         r = r << 6 | r >> 4;
  325.         g = g << 6 | g >> 4;
  326.         b = b << 6 | b >> 4;
  327.        
  328.         a <<= 14;
  329.         a |= a >> 2;
  330.         a |= a >> 4;
  331.         a |= a >> 8;
  332.  
  333.         *buffer++ = a << 48 | r << 32 | g << 16 | b;
  334.     }
  335. }
  336.  
  337. /* Expects a uint64_t buffer */
  338. static void
  339. fetch_scanline_x2b10g10r10 (pixman_image_t *image,
  340.                             int             x,
  341.                             int             y,
  342.                             int             width,
  343.                             uint32_t *      b,
  344.                             const uint32_t *mask)
  345. {
  346.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  347.     const uint32_t *pixel = (uint32_t *)bits + x;
  348.     const uint32_t *end = pixel + width;
  349.     uint64_t *buffer = (uint64_t *)b;
  350.    
  351.     while (pixel < end)
  352.     {
  353.         uint32_t p = READ (image, pixel++);
  354.         uint64_t b = (p >> 20) & 0x3ff;
  355.         uint64_t g = (p >> 10) & 0x3ff;
  356.         uint64_t r = p & 0x3ff;
  357.        
  358.         r = r << 6 | r >> 4;
  359.         g = g << 6 | g >> 4;
  360.         b = b << 6 | b >> 4;
  361.        
  362.         *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
  363.     }
  364. }
  365.  
  366. static void
  367. fetch_scanline_r8g8b8 (pixman_image_t *image,
  368.                        int             x,
  369.                        int             y,
  370.                        int             width,
  371.                        uint32_t *      buffer,
  372.                        const uint32_t *mask)
  373. {
  374.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  375.     const uint8_t *pixel = (const uint8_t *)bits + 3 * x;
  376.     const uint8_t *end = pixel + 3 * width;
  377.    
  378.     while (pixel < end)
  379.     {
  380.         uint32_t b = 0xff000000;
  381.        
  382. #ifdef WORDS_BIGENDIAN
  383.         b |= (READ (image, pixel++) << 16);
  384.         b |= (READ (image, pixel++) << 8);
  385.         b |= (READ (image, pixel++));
  386. #else
  387.         b |= (READ (image, pixel++));
  388.         b |= (READ (image, pixel++) << 8);
  389.         b |= (READ (image, pixel++) << 16);
  390. #endif
  391.        
  392.         *buffer++ = b;
  393.     }
  394. }
  395.  
  396. static void
  397. fetch_scanline_b8g8r8 (pixman_image_t *image,
  398.                        int             x,
  399.                        int             y,
  400.                        int             width,
  401.                        uint32_t *      buffer,
  402.                        const uint32_t *mask)
  403. {
  404.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  405.     const uint8_t *pixel = (const uint8_t *)bits + 3 * x;
  406.     const uint8_t *end = pixel + 3 * width;
  407.    
  408.     while (pixel < end)
  409.     {
  410.         uint32_t b = 0xff000000;
  411. #ifdef WORDS_BIGENDIAN
  412.         b |= (READ (image, pixel++));
  413.         b |= (READ (image, pixel++) << 8);
  414.         b |= (READ (image, pixel++) << 16);
  415. #else
  416.         b |= (READ (image, pixel++) << 16);
  417.         b |= (READ (image, pixel++) << 8);
  418.         b |= (READ (image, pixel++));
  419. #endif
  420.         *buffer++ = b;
  421.     }
  422. }
  423.  
  424. static void
  425. fetch_scanline_r5g6b5 (pixman_image_t *image,
  426.                        int             x,
  427.                        int             y,
  428.                        int             width,
  429.                        uint32_t *      buffer,
  430.                        const uint32_t *mask)
  431. {
  432.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  433.     const uint16_t *pixel = (const uint16_t *)bits + x;
  434.     const uint16_t *end = pixel + width;
  435.    
  436.     while (pixel < end)
  437.     {
  438.         uint32_t p = READ (image, pixel++);
  439.         uint32_t r = (((p) << 3) & 0xf8) |
  440.             (((p) << 5) & 0xfc00) |
  441.             (((p) << 8) & 0xf80000);
  442.        
  443.         r |= (r >> 5) & 0x70007;
  444.         r |= (r >> 6) & 0x300;
  445.        
  446.         *buffer++ = 0xff000000 | r;
  447.     }
  448. }
  449.  
  450. static void
  451. fetch_scanline_b5g6r5 (pixman_image_t *image,
  452.                        int             x,
  453.                        int             y,
  454.                        int             width,
  455.                        uint32_t *      buffer,
  456.                        const uint32_t *mask)
  457. {
  458.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  459.     const uint16_t *pixel = (const uint16_t *)bits + x;
  460.     const uint16_t *end = pixel + width;
  461.    
  462.     while (pixel < end)
  463.     {
  464.         uint32_t p = READ (image, pixel++);
  465.         uint32_t r, g, b;
  466.        
  467.         b = ((p & 0xf800) | ((p & 0xe000) >> 5)) >> 8;
  468.         g = ((p & 0x07e0) | ((p & 0x0600) >> 6)) << 5;
  469.         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
  470.        
  471.         *buffer++ = 0xff000000 | r | g | b;
  472.     }
  473. }
  474.  
  475. static void
  476. fetch_scanline_a1r5g5b5 (pixman_image_t *image,
  477.                          int             x,
  478.                          int             y,
  479.                          int             width,
  480.                          uint32_t *      buffer,
  481.                          const uint32_t *mask)
  482. {
  483.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  484.     const uint16_t *pixel = (const uint16_t *)bits + x;
  485.     const uint16_t *end = pixel + width;
  486.    
  487.     while (pixel < end)
  488.     {
  489.         uint32_t p = READ (image, pixel++);
  490.         uint32_t r, g, b, a;
  491.        
  492.         a = (uint32_t) ((uint8_t) (0 - ((p & 0x8000) >> 15))) << 24;
  493.         r = ((p & 0x7c00) | ((p & 0x7000) >> 5)) << 9;
  494.         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
  495.         b = ((p & 0x001c) | ((p & 0x001f) << 5)) >> 2;
  496.        
  497.         *buffer++ = a | r | g | b;
  498.     }
  499. }
  500.  
  501. static void
  502. fetch_scanline_x1r5g5b5 (pixman_image_t *image,
  503.                          int             x,
  504.                          int             y,
  505.                          int             width,
  506.                          uint32_t *      buffer,
  507.                          const uint32_t *mask)
  508. {
  509.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  510.     const uint16_t *pixel = (const uint16_t *)bits + x;
  511.     const uint16_t *end = pixel + width;
  512.    
  513.     while (pixel < end)
  514.     {
  515.         uint32_t p = READ (image, pixel++);
  516.         uint32_t r, g, b;
  517.        
  518.         r = ((p & 0x7c00) | ((p & 0x7000) >> 5)) << 9;
  519.         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
  520.         b = ((p & 0x001c) | ((p & 0x001f) << 5)) >> 2;
  521.        
  522.         *buffer++ = 0xff000000 | r | g | b;
  523.     }
  524. }
  525.  
  526. static void
  527. fetch_scanline_a1b5g5r5 (pixman_image_t *image,
  528.                          int             x,
  529.                          int             y,
  530.                          int             width,
  531.                          uint32_t *      buffer,
  532.                          const uint32_t *mask)
  533. {
  534.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  535.     const uint16_t *pixel = (const uint16_t *)bits + x;
  536.     const uint16_t *end = pixel + width;
  537.     uint32_t r, g, b, a;
  538.    
  539.     while (pixel < end)
  540.     {
  541.         uint32_t p = READ (image, pixel++);
  542.        
  543.         a = (uint32_t) ((uint8_t) (0 - ((p & 0x8000) >> 15))) << 24;
  544.         b = ((p & 0x7c00) | ((p & 0x7000) >> 5)) >> 7;
  545.         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
  546.         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
  547.        
  548.         *buffer++ = a | r | g | b;
  549.     }
  550. }
  551.  
  552. static void
  553. fetch_scanline_x1b5g5r5 (pixman_image_t *image,
  554.                          int             x,
  555.                          int             y,
  556.                          int             width,
  557.                          uint32_t *      buffer,
  558.                          const uint32_t *mask)
  559. {
  560.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  561.     const uint16_t *pixel = (const uint16_t *)bits + x;
  562.     const uint16_t *end = pixel + width;
  563.    
  564.     while (pixel < end)
  565.     {
  566.         uint32_t p = READ (image, pixel++);
  567.         uint32_t r, g, b;
  568.        
  569.         b = ((p & 0x7c00) | ((p & 0x7000) >> 5)) >> 7;
  570.         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
  571.         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
  572.        
  573.         *buffer++ = 0xff000000 | r | g | b;
  574.     }
  575. }
  576.  
  577. static void
  578. fetch_scanline_a4r4g4b4 (pixman_image_t *image,
  579.                          int             x,
  580.                          int             y,
  581.                          int             width,
  582.                          uint32_t *      buffer,
  583.                          const uint32_t *mask)
  584. {
  585.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  586.     const uint16_t *pixel = (const uint16_t *)bits + x;
  587.     const uint16_t *end = pixel + width;
  588.    
  589.     while (pixel < end)
  590.     {
  591.         uint32_t p = READ (image, pixel++);
  592.         uint32_t r, g, b, a;
  593.        
  594.         a = ((p & 0xf000) | ((p & 0xf000) >> 4)) << 16;
  595.         r = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) << 12;
  596.         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
  597.         b = ((p & 0x000f) | ((p & 0x000f) << 4));
  598.        
  599.         *buffer++ = a | r | g | b;
  600.     }
  601. }
  602.  
  603. static void
  604. fetch_scanline_x4r4g4b4 (pixman_image_t *image,
  605.                          int             x,
  606.                          int             y,
  607.                          int             width,
  608.                          uint32_t *      buffer,
  609.                          const uint32_t *mask)
  610. {
  611.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  612.     const uint16_t *pixel = (const uint16_t *)bits + x;
  613.     const uint16_t *end = pixel + width;
  614.    
  615.     while (pixel < end)
  616.     {
  617.         uint32_t p = READ (image, pixel++);
  618.         uint32_t r, g, b;
  619.        
  620.         r = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) << 12;
  621.         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
  622.         b = ((p & 0x000f) | ((p & 0x000f) << 4));
  623.        
  624.         *buffer++ = 0xff000000 | r | g | b;
  625.     }
  626. }
  627.  
  628. static void
  629. fetch_scanline_a4b4g4r4 (pixman_image_t *image,
  630.                          int             x,
  631.                          int             y,
  632.                          int             width,
  633.                          uint32_t *      buffer,
  634.                          const uint32_t *mask)
  635. {
  636.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  637.     const uint16_t *pixel = (const uint16_t *)bits + x;
  638.     const uint16_t *end = pixel + width;
  639.    
  640.     while (pixel < end)
  641.     {
  642.         uint32_t p = READ (image, pixel++);
  643.         uint32_t r, g, b, a;
  644.        
  645.         a = ((p & 0xf000) | ((p & 0xf000) >> 4)) << 16;
  646.         b = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) >> 4;
  647.         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
  648.         r = ((p & 0x000f) | ((p & 0x000f) << 4)) << 16;
  649.        
  650.         *buffer++ = a | r | g | b;
  651.     }
  652. }
  653.  
  654. static void
  655. fetch_scanline_x4b4g4r4 (pixman_image_t *image,
  656.                          int             x,
  657.                          int             y,
  658.                          int             width,
  659.                          uint32_t *      buffer,
  660.                          const uint32_t *mask)
  661. {
  662.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  663.     const uint16_t *pixel = (const uint16_t *)bits + x;
  664.     const uint16_t *end = pixel + width;
  665.    
  666.     while (pixel < end)
  667.     {
  668.         uint32_t p = READ (image, pixel++);
  669.         uint32_t r, g, b;
  670.        
  671.         b = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) >> 4;
  672.         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
  673.         r = ((p & 0x000f) | ((p & 0x000f) << 4)) << 16;
  674.        
  675.         *buffer++ = 0xff000000 | r | g | b;
  676.     }
  677. }
  678.  
  679. static void
  680. fetch_scanline_a8 (pixman_image_t *image,
  681.                    int             x,
  682.                    int             y,
  683.                    int             width,
  684.                    uint32_t *      buffer,
  685.                    const uint32_t *mask)
  686. {
  687.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  688.     const uint8_t *pixel = (const uint8_t *)bits + x;
  689.     const uint8_t *end = pixel + width;
  690.    
  691.     while (pixel < end)
  692.         *buffer++ = READ (image, pixel++) << 24;
  693. }
  694.  
  695. static void
  696. fetch_scanline_r3g3b2 (pixman_image_t *image,
  697.                        int             x,
  698.                        int             y,
  699.                        int             width,
  700.                        uint32_t *      buffer,
  701.                        const uint32_t *mask)
  702. {
  703.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  704.     const uint8_t *pixel = (const uint8_t *)bits + x;
  705.     const uint8_t *end = pixel + width;
  706.    
  707.     while (pixel < end)
  708.     {
  709.         uint32_t p = READ (image, pixel++);
  710.         uint32_t r, g, b;
  711.        
  712.         r = ((p & 0xe0) | ((p & 0xe0) >> 3) | ((p & 0xc0) >> 6)) << 16;
  713.         g = ((p & 0x1c) | ((p & 0x18) >> 3) | ((p & 0x1c) << 3)) << 8;
  714.         b = (((p & 0x03)     ) |
  715.              ((p & 0x03) << 2) |
  716.              ((p & 0x03) << 4) |
  717.              ((p & 0x03) << 6));
  718.        
  719.         *buffer++ = 0xff000000 | r | g | b;
  720.     }
  721. }
  722.  
  723. static void
  724. fetch_scanline_b2g3r3 (pixman_image_t *image,
  725.                        int             x,
  726.                        int             y,
  727.                        int             width,
  728.                        uint32_t *      buffer,
  729.                        const uint32_t *mask)
  730. {
  731.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  732.     const uint8_t *pixel = (const uint8_t *)bits + x;
  733.     const uint8_t *end = pixel + width;
  734.  
  735.     while (pixel < end)
  736.     {
  737.         uint32_t p = READ (image, pixel++);
  738.         uint32_t r, g, b;
  739.  
  740.         b  = p & 0xc0;
  741.         b |= b >> 2;
  742.         b |= b >> 4;
  743.         b &= 0xff;
  744.  
  745.         g  = (p & 0x38) << 10;
  746.         g |= g >> 3;
  747.         g |= g >> 6;
  748.         g &= 0xff00;
  749.  
  750.         r  = (p & 0x7) << 21;
  751.         r |= r >> 3;
  752.         r |= r >> 6;
  753.         r &= 0xff0000;
  754.  
  755.         *buffer++ = 0xff000000 | r | g | b;
  756.     }
  757. }
  758.  
  759. static void
  760. fetch_scanline_a2r2g2b2 (pixman_image_t *image,
  761.                          int             x,
  762.                          int             y,
  763.                          int             width,
  764.                          uint32_t *      buffer,
  765.                          const uint32_t *mask)
  766. {
  767.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  768.     const uint8_t *pixel = (const uint8_t *)bits + x;
  769.     const uint8_t *end = pixel + width;
  770.    
  771.     while (pixel < end)
  772.     {
  773.         uint32_t p = READ (image, pixel++);
  774.         uint32_t a, r, g, b;
  775.        
  776.         a = ((p & 0xc0) * 0x55) << 18;
  777.         r = ((p & 0x30) * 0x55) << 12;
  778.         g = ((p & 0x0c) * 0x55) << 6;
  779.         b = ((p & 0x03) * 0x55);
  780.        
  781.         *buffer++ = a | r | g | b;
  782.     }
  783. }
  784.  
  785. static void
  786. fetch_scanline_a2b2g2r2 (pixman_image_t *image,
  787.                          int             x,
  788.                          int             y,
  789.                          int             width,
  790.                          uint32_t *      buffer,
  791.                          const uint32_t *mask)
  792. {
  793.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  794.     const uint8_t *pixel = (const uint8_t *)bits + x;
  795.     const uint8_t *end = pixel + width;
  796.    
  797.     while (pixel < end)
  798.     {
  799.         uint32_t p = READ (image, pixel++);
  800.         uint32_t a, r, g, b;
  801.        
  802.         a = ((p & 0xc0) * 0x55) << 18;
  803.         b = ((p & 0x30) * 0x55) >> 4;
  804.         g = ((p & 0x0c) * 0x55) << 6;
  805.         r = ((p & 0x03) * 0x55) << 16;
  806.        
  807.         *buffer++ = a | r | g | b;
  808.     }
  809. }
  810.  
  811. static void
  812. fetch_scanline_c8 (pixman_image_t *image,
  813.                    int             x,
  814.                    int             y,
  815.                    int             width,
  816.                    uint32_t *      buffer,
  817.                    const uint32_t *mask)
  818. {
  819.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  820.     const pixman_indexed_t * indexed = image->bits.indexed;
  821.     const uint8_t *pixel = (const uint8_t *)bits + x;
  822.     const uint8_t *end = pixel + width;
  823.    
  824.     while (pixel < end)
  825.     {
  826.         uint32_t p = READ (image, pixel++);
  827.        
  828.         *buffer++ = indexed->rgba[p];
  829.     }
  830. }
  831.  
  832. static void
  833. fetch_scanline_x4a4 (pixman_image_t *image,
  834.                      int             x,
  835.                      int             y,
  836.                      int             width,
  837.                      uint32_t *      buffer,
  838.                      const uint32_t *mask)
  839. {
  840.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  841.     const uint8_t *pixel = (const uint8_t *)bits + x;
  842.     const uint8_t *end = pixel + width;
  843.    
  844.     while (pixel < end)
  845.     {
  846.         uint8_t p = READ (image, pixel++) & 0xf;
  847.  
  848.         *buffer++ = (p | (p << 4)) << 24;
  849.     }
  850. }
  851.  
  852. #define FETCH_8(img,l,o)    (READ (img, (((uint8_t *)(l)) + ((o) >> 3))))
  853. #ifdef WORDS_BIGENDIAN
  854. #define FETCH_4(img,l,o)                                                \
  855.     (((4 * (o)) & 4) ? (FETCH_8 (img,l, 4 * (o)) & 0xf) : (FETCH_8 (img,l,(4 * (o))) >> 4))
  856. #else
  857. #define FETCH_4(img,l,o)                                                \
  858.     (((4 * (o)) & 4) ? (FETCH_8 (img, l, 4 * (o)) >> 4) : (FETCH_8 (img, l, (4 * (o))) & 0xf))
  859. #endif
  860.  
  861. static void
  862. fetch_scanline_a4 (pixman_image_t *image,
  863.                    int             x,
  864.                    int             y,
  865.                    int             width,
  866.                    uint32_t *      buffer,
  867.                    const uint32_t *mask)
  868. {
  869.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  870.     int i;
  871.  
  872.     for (i = 0; i < width; ++i)
  873.     {
  874.         uint32_t p = FETCH_4 (image, bits, i + x);
  875.  
  876.         p |= p << 4;
  877.  
  878.         *buffer++ = p << 24;
  879.     }
  880. }
  881.  
  882. static void
  883. fetch_scanline_r1g2b1 (pixman_image_t *image,
  884.                        int             x,
  885.                        int             y,
  886.                        int             width,
  887.                        uint32_t *      buffer,
  888.                        const uint32_t *mask)
  889. {
  890.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  891.     int i;
  892.    
  893.     for (i = 0; i < width; ++i)
  894.     {
  895.         uint32_t p = FETCH_4 (image, bits, i + x);
  896.         uint32_t r, g, b;
  897.        
  898.         r = ((p & 0x8) * 0xff) << 13;
  899.         g = ((p & 0x6) * 0x55) << 7;
  900.         b = ((p & 0x1) * 0xff);
  901.        
  902.         *buffer++ = 0xff000000 | r | g | b;
  903.     }
  904. }
  905.  
  906. static void
  907. fetch_scanline_b1g2r1 (pixman_image_t *image,
  908.                        int             x,
  909.                        int             y,
  910.                        int             width,
  911.                        uint32_t *      buffer,
  912.                        const uint32_t *mask)
  913. {
  914.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  915.     int i;
  916.    
  917.     for (i = 0; i < width; ++i)
  918.     {
  919.         uint32_t p = FETCH_4 (image, bits, i + x);
  920.         uint32_t r, g, b;
  921.        
  922.         b = ((p & 0x8) * 0xff) >> 3;
  923.         g = ((p & 0x6) * 0x55) << 7;
  924.         r = ((p & 0x1) * 0xff) << 16;
  925.  
  926.         *buffer++ = 0xff000000 | r | g | b;
  927.     }
  928. }
  929.  
  930. static void
  931. fetch_scanline_a1r1g1b1 (pixman_image_t *image,
  932.                          int             x,
  933.                          int             y,
  934.                          int             width,
  935.                          uint32_t *      buffer,
  936.                          const uint32_t *mask)
  937. {
  938.     uint32_t a, r, g, b;
  939.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  940.     int i;
  941.  
  942.     for (i = 0; i < width; ++i)
  943.     {
  944.         uint32_t p = FETCH_4 (image, bits, i + x);
  945.  
  946.         a = ((p & 0x8) * 0xff) << 21;
  947.         r = ((p & 0x4) * 0xff) << 14;
  948.         g = ((p & 0x2) * 0xff) << 7;
  949.         b = ((p & 0x1) * 0xff);
  950.  
  951.         *buffer++ = a | r | g | b;
  952.     }
  953. }
  954.  
  955. static void
  956. fetch_scanline_a1b1g1r1 (pixman_image_t *image,
  957.                          int             x,
  958.                          int             y,
  959.                          int             width,
  960.                          uint32_t *      buffer,
  961.                          const uint32_t *mask)
  962. {
  963.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  964.     int i;
  965.  
  966.     for (i = 0; i < width; ++i)
  967.     {
  968.         uint32_t p = FETCH_4 (image, bits, i + x);
  969.         uint32_t a, r, g, b;
  970.  
  971.         a = ((p & 0x8) * 0xff) << 21;
  972.         b = ((p & 0x4) * 0xff) >> 2;
  973.         g = ((p & 0x2) * 0xff) << 7;
  974.         r = ((p & 0x1) * 0xff) << 16;
  975.  
  976.         *buffer++ = a | r | g | b;
  977.     }
  978. }
  979.  
  980. static void
  981. fetch_scanline_c4 (pixman_image_t *image,
  982.                    int             x,
  983.                    int             y,
  984.                    int             width,
  985.                    uint32_t *      buffer,
  986.                    const uint32_t *mask)
  987. {
  988.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  989.     const pixman_indexed_t * indexed = image->bits.indexed;
  990.     int i;
  991.    
  992.     for (i = 0; i < width; ++i)
  993.     {
  994.         uint32_t p = FETCH_4 (image, bits, i + x);
  995.        
  996.         *buffer++ = indexed->rgba[p];
  997.     }
  998. }
  999.  
  1000. static void
  1001. fetch_scanline_a1 (pixman_image_t *image,
  1002.                    int             x,
  1003.                    int             y,
  1004.                    int             width,
  1005.                    uint32_t *      buffer,
  1006.                    const uint32_t *mask)
  1007. {
  1008.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  1009.     int i;
  1010.    
  1011.     for (i = 0; i < width; ++i)
  1012.     {
  1013.         uint32_t p = READ (image, bits + ((i + x) >> 5));
  1014.         uint32_t a;
  1015.        
  1016. #ifdef WORDS_BIGENDIAN
  1017.         a = p >> (0x1f - ((i + x) & 0x1f));
  1018. #else
  1019.         a = p >> ((i + x) & 0x1f);
  1020. #endif
  1021.         a = a & 1;
  1022.         a |= a << 1;
  1023.         a |= a << 2;
  1024.         a |= a << 4;
  1025.        
  1026.         *buffer++ = a << 24;
  1027.     }
  1028. }
  1029.  
  1030. static void
  1031. fetch_scanline_g1 (pixman_image_t *image,
  1032.                    int             x,
  1033.                    int             y,
  1034.                    int             width,
  1035.                    uint32_t *      buffer,
  1036.                    const uint32_t *mask)
  1037. {
  1038.     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
  1039.     const pixman_indexed_t * indexed = image->bits.indexed;
  1040.     int i;
  1041.    
  1042.     for (i = 0; i < width; ++i)
  1043.     {
  1044.         uint32_t p = READ (image, bits + ((i + x) >> 5));
  1045.         uint32_t a;
  1046.        
  1047. #ifdef WORDS_BIGENDIAN
  1048.         a = p >> (0x1f - ((i + x) & 0x1f));
  1049. #else
  1050.         a = p >> ((i + x) & 0x1f);
  1051. #endif
  1052.         a = a & 1;
  1053.        
  1054.         *buffer++ = indexed->rgba[a];
  1055.     }
  1056. }
  1057.  
  1058. static void
  1059. fetch_scanline_yuy2 (pixman_image_t *image,
  1060.                      int             x,
  1061.                      int             line,
  1062.                      int             width,
  1063.                      uint32_t *      buffer,
  1064.                      const uint32_t *mask)
  1065. {
  1066.     const uint32_t *bits = image->bits.bits + image->bits.rowstride * line;
  1067.     int i;
  1068.    
  1069.     for (i = 0; i < width; i++)
  1070.     {
  1071.         int16_t y, u, v;
  1072.         int32_t r, g, b;
  1073.        
  1074.         y = ((uint8_t *) bits)[(x + i) << 1] - 16;
  1075.         u = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 1] - 128;
  1076.         v = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 3] - 128;
  1077.        
  1078.         /* R = 1.164(Y - 16) + 1.596(V - 128) */
  1079.         r = 0x012b27 * y + 0x019a2e * v;
  1080.         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
  1081.         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
  1082.         /* B = 1.164(Y - 16) + 2.018(U - 128) */
  1083.         b = 0x012b27 * y + 0x0206a2 * u;
  1084.        
  1085.         *buffer++ = 0xff000000 |
  1086.             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
  1087.             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
  1088.             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
  1089.     }
  1090. }
  1091.  
  1092. static void
  1093. fetch_scanline_yv12 (pixman_image_t *image,
  1094.                      int             x,
  1095.                      int             line,
  1096.                      int             width,
  1097.                      uint32_t *      buffer,
  1098.                      const uint32_t *mask)
  1099. {
  1100.     YV12_SETUP (image);
  1101.     uint8_t *y_line = YV12_Y (line);
  1102.     uint8_t *u_line = YV12_U (line);
  1103.     uint8_t *v_line = YV12_V (line);
  1104.     int i;
  1105.    
  1106.     for (i = 0; i < width; i++)
  1107.     {
  1108.         int16_t y, u, v;
  1109.         int32_t r, g, b;
  1110.  
  1111.         y = y_line[x + i] - 16;
  1112.         u = u_line[(x + i) >> 1] - 128;
  1113.         v = v_line[(x + i) >> 1] - 128;
  1114.  
  1115.         /* R = 1.164(Y - 16) + 1.596(V - 128) */
  1116.         r = 0x012b27 * y + 0x019a2e * v;
  1117.         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
  1118.         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
  1119.         /* B = 1.164(Y - 16) + 2.018(U - 128) */
  1120.         b = 0x012b27 * y + 0x0206a2 * u;
  1121.  
  1122.         *buffer++ = 0xff000000 |
  1123.             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
  1124.             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
  1125.             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
  1126.     }
  1127. }
  1128.  
  1129. /**************************** Pixel wise fetching *****************************/
  1130.  
  1131. /* Despite the type, expects a uint64_t buffer */
  1132. static uint64_t
  1133. fetch_pixel_a2r10g10b10 (bits_image_t *image,
  1134.                          int              offset,
  1135.                          int           line)
  1136. {
  1137.     uint32_t *bits = image->bits + line * image->rowstride;
  1138.     uint32_t p = READ (image, bits + offset);
  1139.     uint64_t a = p >> 30;
  1140.     uint64_t r = (p >> 20) & 0x3ff;
  1141.     uint64_t g = (p >> 10) & 0x3ff;
  1142.     uint64_t b = p & 0x3ff;
  1143.  
  1144.     r = r << 6 | r >> 4;
  1145.     g = g << 6 | g >> 4;
  1146.     b = b << 6 | b >> 4;
  1147.  
  1148.     a <<= 14;
  1149.     a |= a >> 2;
  1150.     a |= a >> 4;
  1151.     a |= a >> 8;
  1152.  
  1153.     return a << 48 | r << 32 | g << 16 | b;
  1154. }
  1155.  
  1156. /* Despite the type, this function expects a uint64_t buffer */
  1157. static uint64_t
  1158. fetch_pixel_x2r10g10b10 (bits_image_t *image,
  1159.                          int       offset,
  1160.                          int           line)
  1161. {
  1162.     uint32_t *bits = image->bits + line * image->rowstride;
  1163.     uint32_t p = READ (image, bits + offset);
  1164.     uint64_t r = (p >> 20) & 0x3ff;
  1165.     uint64_t g = (p >> 10) & 0x3ff;
  1166.     uint64_t b = p & 0x3ff;
  1167.    
  1168.     r = r << 6 | r >> 4;
  1169.     g = g << 6 | g >> 4;
  1170.     b = b << 6 | b >> 4;
  1171.    
  1172.     return 0xffffULL << 48 | r << 32 | g << 16 | b;
  1173. }
  1174.  
  1175. /* Despite the type, expects a uint64_t buffer */
  1176. static uint64_t
  1177. fetch_pixel_a2b10g10r10 (bits_image_t *image,
  1178.                          int           offset,
  1179.                          int           line)
  1180. {
  1181.     uint32_t *bits = image->bits + line * image->rowstride;
  1182.     uint32_t p = READ (image, bits + offset);
  1183.     uint64_t a = p >> 30;
  1184.     uint64_t b = (p >> 20) & 0x3ff;
  1185.     uint64_t g = (p >> 10) & 0x3ff;
  1186.     uint64_t r = p & 0x3ff;
  1187.    
  1188.     r = r << 6 | r >> 4;
  1189.     g = g << 6 | g >> 4;
  1190.     b = b << 6 | b >> 4;
  1191.    
  1192.     a <<= 14;
  1193.     a |= a >> 2;
  1194.     a |= a >> 4;
  1195.     a |= a >> 8;
  1196.    
  1197.     return a << 48 | r << 32 | g << 16 | b;
  1198. }
  1199.  
  1200. /* Despite the type, this function expects a uint64_t buffer */
  1201. static uint64_t
  1202. fetch_pixel_x2b10g10r10 (bits_image_t *image,
  1203.                          int           offset,
  1204.                          int           line)
  1205. {
  1206.     uint32_t *bits = image->bits + line * image->rowstride;
  1207.     uint32_t p = READ (image, bits + offset);
  1208.     uint64_t b = (p >> 20) & 0x3ff;
  1209.     uint64_t g = (p >> 10) & 0x3ff;
  1210.     uint64_t r = p & 0x3ff;
  1211.    
  1212.     r = r << 6 | r >> 4;
  1213.     g = g << 6 | g >> 4;
  1214.     b = b << 6 | b >> 4;
  1215.    
  1216.     return 0xffffULL << 48 | r << 32 | g << 16 | b;
  1217. }
  1218.  
  1219. static uint32_t
  1220. fetch_pixel_a8r8g8b8 (bits_image_t *image,
  1221.                       int           offset,
  1222.                       int           line)
  1223. {
  1224.     uint32_t *bits = image->bits + line * image->rowstride;
  1225.     return READ (image, (uint32_t *)bits + offset);
  1226. }
  1227.  
  1228. static uint32_t
  1229. fetch_pixel_x8r8g8b8 (bits_image_t *image,
  1230.                       int           offset,
  1231.                       int           line)
  1232. {
  1233.     uint32_t *bits = image->bits + line * image->rowstride;
  1234.  
  1235.     return READ (image, (uint32_t *)bits + offset) | 0xff000000;
  1236. }
  1237.  
  1238. static uint32_t
  1239. fetch_pixel_a8b8g8r8 (bits_image_t *image,
  1240.                       int           offset,
  1241.                       int           line)
  1242. {
  1243.     uint32_t *bits = image->bits + line * image->rowstride;
  1244.     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
  1245.    
  1246.     return ((pixel & 0xff000000) |
  1247.             ((pixel >> 16) & 0xff) |
  1248.             (pixel & 0x0000ff00) |
  1249.             ((pixel & 0xff) << 16));
  1250. }
  1251.  
  1252. static uint32_t
  1253. fetch_pixel_x8b8g8r8 (bits_image_t *image,
  1254.                       int           offset,
  1255.                       int           line)
  1256. {
  1257.     uint32_t *bits = image->bits + line * image->rowstride;
  1258.     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
  1259.    
  1260.     return ((0xff000000) |
  1261.             ((pixel >> 16) & 0xff) |
  1262.             (pixel & 0x0000ff00) |
  1263.             ((pixel & 0xff) << 16));
  1264. }
  1265.  
  1266. static uint32_t
  1267. fetch_pixel_b8g8r8a8 (bits_image_t *image,
  1268.                       int           offset,
  1269.                       int           line)
  1270. {
  1271.     uint32_t *bits = image->bits + line * image->rowstride;
  1272.     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
  1273.    
  1274.     return ((pixel & 0xff000000) >> 24 |
  1275.             (pixel & 0x00ff0000) >> 8 |
  1276.             (pixel & 0x0000ff00) << 8 |
  1277.             (pixel & 0x000000ff) << 24);
  1278. }
  1279.  
  1280. static uint32_t
  1281. fetch_pixel_b8g8r8x8 (bits_image_t *image,
  1282.                       int           offset,
  1283.                       int           line)
  1284. {
  1285.     uint32_t *bits = image->bits + line * image->rowstride;
  1286.     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
  1287.    
  1288.     return ((0xff000000) |
  1289.             (pixel & 0xff000000) >> 24 |
  1290.             (pixel & 0x00ff0000) >> 8 |
  1291.             (pixel & 0x0000ff00) << 8);
  1292. }
  1293.  
  1294. static uint32_t
  1295. fetch_pixel_x14r6g6b6 (bits_image_t *image,
  1296.                        int           offset,
  1297.                        int           line)
  1298. {
  1299.     uint32_t *bits = image->bits + line * image->rowstride;
  1300.     uint32_t pixel = READ (image, (uint32_t *) bits + offset);
  1301.     uint32_t r, g, b;
  1302.  
  1303.     r = ((pixel & 0x3f000) << 6) | ((pixel & 0x30000));
  1304.     g = ((pixel & 0x00fc0) << 4) | ((pixel & 0x00c00) >> 2);
  1305.     b = ((pixel & 0x0003f) << 2) | ((pixel & 0x00030) >> 4);
  1306.  
  1307.     return 0xff000000 | r | g | b;
  1308. }
  1309.  
  1310. static uint32_t
  1311. fetch_pixel_r8g8b8 (bits_image_t *image,
  1312.                     int           offset,
  1313.                     int           line)
  1314. {
  1315.     uint32_t *bits = image->bits + line * image->rowstride;
  1316.     uint8_t   *pixel = ((uint8_t *) bits) + (offset * 3);
  1317.    
  1318. #ifdef WORDS_BIGENDIAN
  1319.     return (0xff000000 |
  1320.             (READ (image, pixel + 0) << 16) |
  1321.             (READ (image, pixel + 1) << 8) |
  1322.             (READ (image, pixel + 2)));
  1323. #else
  1324.     return (0xff000000 |
  1325.             (READ (image, pixel + 2) << 16) |
  1326.             (READ (image, pixel + 1) << 8) |
  1327.             (READ (image, pixel + 0)));
  1328. #endif
  1329. }
  1330.  
  1331. static uint32_t
  1332. fetch_pixel_b8g8r8 (bits_image_t *image,
  1333.                     int           offset,
  1334.                     int           line)
  1335. {
  1336.     uint32_t *bits = image->bits + line * image->rowstride;
  1337.     uint8_t   *pixel = ((uint8_t *) bits) + (offset * 3);
  1338. #ifdef WORDS_BIGENDIAN
  1339.     return (0xff000000 |
  1340.             (READ (image, pixel + 2) << 16) |
  1341.             (READ (image, pixel + 1) << 8) |
  1342.             (READ (image, pixel + 0)));
  1343. #else
  1344.     return (0xff000000 |
  1345.             (READ (image, pixel + 0) << 16) |
  1346.             (READ (image, pixel + 1) << 8) |
  1347.             (READ (image, pixel + 2)));
  1348. #endif
  1349. }
  1350.  
  1351. static uint32_t
  1352. fetch_pixel_r5g6b5 (bits_image_t *image,
  1353.                     int           offset,
  1354.                     int           line)
  1355. {
  1356.     uint32_t *bits = image->bits + line * image->rowstride;
  1357.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1358.     uint32_t r, g, b;
  1359.    
  1360.     r = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) << 8;
  1361.     g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
  1362.     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
  1363.    
  1364.     return (0xff000000 | r | g | b);
  1365. }
  1366.  
  1367. static uint32_t
  1368. fetch_pixel_b5g6r5 (bits_image_t *image,
  1369.                     int           offset,
  1370.                     int           line)
  1371. {
  1372.     uint32_t r, g, b;
  1373.     uint32_t *bits = image->bits + line * image->rowstride;
  1374.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1375.    
  1376.     b = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) >> 8;
  1377.     g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
  1378.     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
  1379.    
  1380.     return (0xff000000 | r | g | b);
  1381. }
  1382.  
  1383. static uint32_t
  1384. fetch_pixel_a1r5g5b5 (bits_image_t *image,
  1385.                       int           offset,
  1386.                       int           line)
  1387. {
  1388.     uint32_t *bits = image->bits + line * image->rowstride;
  1389.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1390.     uint32_t a, r, g, b;
  1391.    
  1392.     a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
  1393.     r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
  1394.     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
  1395.     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
  1396.    
  1397.     return (a | r | g | b);
  1398. }
  1399.  
  1400. static uint32_t
  1401. fetch_pixel_x1r5g5b5 (bits_image_t *image,
  1402.                       int           offset,
  1403.                       int           line)
  1404. {
  1405.     uint32_t *bits = image->bits + line * image->rowstride;
  1406.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1407.     uint32_t r, g, b;
  1408.    
  1409.     r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
  1410.     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
  1411.     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
  1412.    
  1413.     return (0xff000000 | r | g | b);
  1414. }
  1415.  
  1416. static uint32_t
  1417. fetch_pixel_a1b5g5r5 (bits_image_t *image,
  1418.                       int           offset,
  1419.                       int           line)
  1420. {
  1421.     uint32_t *bits = image->bits + line * image->rowstride;
  1422.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1423.     uint32_t a, r, g, b;
  1424.    
  1425.     a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
  1426.     b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
  1427.     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
  1428.     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
  1429.    
  1430.     return (a | r | g | b);
  1431. }
  1432.  
  1433. static uint32_t
  1434. fetch_pixel_x1b5g5r5 (bits_image_t *image,
  1435.                       int           offset,
  1436.                       int           line)
  1437. {
  1438.     uint32_t *bits = image->bits + line * image->rowstride;
  1439.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1440.     uint32_t r, g, b;
  1441.    
  1442.     b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
  1443.     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
  1444.     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
  1445.    
  1446.     return (0xff000000 | r | g | b);
  1447. }
  1448.  
  1449. static uint32_t
  1450. fetch_pixel_a4r4g4b4 (bits_image_t *image,
  1451.                       int           offset,
  1452.                       int           line)
  1453. {
  1454.     uint32_t *bits = image->bits + line * image->rowstride;
  1455.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1456.     uint32_t a, r, g, b;
  1457.    
  1458.     a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
  1459.     r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
  1460.     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
  1461.     b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
  1462.    
  1463.     return (a | r | g | b);
  1464. }
  1465.  
  1466. static uint32_t
  1467. fetch_pixel_x4r4g4b4 (bits_image_t *image,
  1468.                       int           offset,
  1469.                       int           line)
  1470. {
  1471.     uint32_t *bits = image->bits + line * image->rowstride;
  1472.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1473.     uint32_t r, g, b;
  1474.    
  1475.     r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
  1476.     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
  1477.     b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
  1478.    
  1479.     return (0xff000000 | r | g | b);
  1480. }
  1481.  
  1482. static uint32_t
  1483. fetch_pixel_a4b4g4r4 (bits_image_t *image,
  1484.                       int           offset,
  1485.                       int           line)
  1486. {
  1487.     uint32_t *bits = image->bits + line * image->rowstride;
  1488.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1489.     uint32_t a, r, g, b;
  1490.    
  1491.     a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
  1492.     b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
  1493.     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
  1494.     r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
  1495.    
  1496.     return (a | r | g | b);
  1497. }
  1498.  
  1499. static uint32_t
  1500. fetch_pixel_x4b4g4r4 (bits_image_t *image,
  1501.                       int           offset,
  1502.                       int           line)
  1503. {
  1504.     uint32_t *bits = image->bits + line * image->rowstride;
  1505.     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
  1506.     uint32_t r, g, b;
  1507.    
  1508.     b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
  1509.     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
  1510.     r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
  1511.    
  1512.     return (0xff000000 | r | g | b);
  1513. }
  1514.  
  1515. static uint32_t
  1516. fetch_pixel_a8 (bits_image_t *image,
  1517.                 int           offset,
  1518.                 int           line)
  1519. {
  1520.     uint32_t *bits = image->bits + line * image->rowstride;
  1521.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1522.    
  1523.     return pixel << 24;
  1524. }
  1525.  
  1526. static uint32_t
  1527. fetch_pixel_r3g3b2 (bits_image_t *image,
  1528.                     int           offset,
  1529.                     int           line)
  1530. {
  1531.     uint32_t *bits = image->bits + line * image->rowstride;
  1532.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1533.     uint32_t r, g, b;
  1534.    
  1535.     r = ((pixel & 0xe0) |
  1536.          ((pixel & 0xe0) >> 3) |
  1537.          ((pixel & 0xc0) >> 6)) << 16;
  1538.    
  1539.     g = ((pixel & 0x1c) |
  1540.          ((pixel & 0x18) >> 3) |
  1541.          ((pixel & 0x1c) << 3)) << 8;
  1542.    
  1543.     b = (((pixel & 0x03)     ) |
  1544.          ((pixel & 0x03) << 2) |
  1545.          ((pixel & 0x03) << 4) |
  1546.          ((pixel & 0x03) << 6));
  1547.    
  1548.     return (0xff000000 | r | g | b);
  1549. }
  1550.  
  1551. static uint32_t
  1552. fetch_pixel_b2g3r3 (bits_image_t *image,
  1553.                     int           offset,
  1554.                     int           line)
  1555. {
  1556.     uint32_t *bits = image->bits + line * image->rowstride;
  1557.     uint32_t p = READ (image, (uint8_t *) bits + offset);
  1558.     uint32_t r, g, b;
  1559.  
  1560.     b  = p & 0xc0;
  1561.     b |= b >> 2;
  1562.     b |= b >> 4;
  1563.     b &= 0xff;
  1564.  
  1565.     g  = (p & 0x38) << 10;
  1566.     g |= g >> 3;
  1567.     g |= g >> 6;
  1568.     g &= 0xff00;
  1569.  
  1570.     r  = (p & 0x7) << 21;
  1571.     r |= r >> 3;
  1572.     r |= r >> 6;
  1573.     r &= 0xff0000;
  1574.  
  1575.     return 0xff000000 | r | g | b;
  1576. }
  1577.  
  1578. static uint32_t
  1579. fetch_pixel_a2r2g2b2 (bits_image_t *image,
  1580.                       int           offset,
  1581.                       int           line)
  1582. {
  1583.     uint32_t *bits = image->bits + line * image->rowstride;
  1584.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1585.     uint32_t a, r, g, b;
  1586.    
  1587.     a = ((pixel & 0xc0) * 0x55) << 18;
  1588.     r = ((pixel & 0x30) * 0x55) << 12;
  1589.     g = ((pixel & 0x0c) * 0x55) << 6;
  1590.     b = ((pixel & 0x03) * 0x55);
  1591.    
  1592.     return a | r | g | b;
  1593. }
  1594.  
  1595. static uint32_t
  1596. fetch_pixel_a2b2g2r2 (bits_image_t *image,
  1597.                       int           offset,
  1598.                       int           line)
  1599. {
  1600.     uint32_t *bits = image->bits + line * image->rowstride;
  1601.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1602.     uint32_t a, r, g, b;
  1603.    
  1604.     a = ((pixel & 0xc0) * 0x55) << 18;
  1605.     b = ((pixel & 0x30) * 0x55) >> 4;
  1606.     g = ((pixel & 0x0c) * 0x55) << 6;
  1607.     r = ((pixel & 0x03) * 0x55) << 16;
  1608.    
  1609.     return a | r | g | b;
  1610. }
  1611.  
  1612. static uint32_t
  1613. fetch_pixel_c8 (bits_image_t *image,
  1614.                 int           offset,
  1615.                 int           line)
  1616. {
  1617.     uint32_t *bits = image->bits + line * image->rowstride;
  1618.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1619.     const pixman_indexed_t * indexed = image->indexed;
  1620.    
  1621.     return indexed->rgba[pixel];
  1622. }
  1623.  
  1624. static uint32_t
  1625. fetch_pixel_x4a4 (bits_image_t *image,
  1626.                   int           offset,
  1627.                   int           line)
  1628. {
  1629.     uint32_t *bits = image->bits + line * image->rowstride;
  1630.     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
  1631.    
  1632.     return ((pixel & 0xf) | ((pixel & 0xf) << 4)) << 24;
  1633. }
  1634.  
  1635. static uint32_t
  1636. fetch_pixel_a4 (bits_image_t *image,
  1637.                 int           offset,
  1638.                 int           line)
  1639. {
  1640.     uint32_t *bits = image->bits + line * image->rowstride;
  1641.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1642.    
  1643.     pixel |= pixel << 4;
  1644.     return pixel << 24;
  1645. }
  1646.  
  1647. static uint32_t
  1648. fetch_pixel_r1g2b1 (bits_image_t *image,
  1649.                     int           offset,
  1650.                     int           line)
  1651. {
  1652.     uint32_t *bits = image->bits + line * image->rowstride;
  1653.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1654.     uint32_t r, g, b;
  1655.    
  1656.     r = ((pixel & 0x8) * 0xff) << 13;
  1657.     g = ((pixel & 0x6) * 0x55) << 7;
  1658.     b = ((pixel & 0x1) * 0xff);
  1659.    
  1660.     return 0xff000000 | r | g | b;
  1661. }
  1662.  
  1663. static uint32_t
  1664. fetch_pixel_b1g2r1 (bits_image_t *image,
  1665.                     int           offset,
  1666.                     int           line)
  1667. {
  1668.     uint32_t *bits = image->bits + line * image->rowstride;
  1669.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1670.     uint32_t r, g, b;
  1671.    
  1672.     b = ((pixel & 0x8) * 0xff) >> 3;
  1673.     g = ((pixel & 0x6) * 0x55) << 7;
  1674.     r = ((pixel & 0x1) * 0xff) << 16;
  1675.    
  1676.     return 0xff000000 | r | g | b;
  1677. }
  1678.  
  1679. static uint32_t
  1680. fetch_pixel_a1r1g1b1 (bits_image_t *image,
  1681.                       int           offset,
  1682.                       int           line)
  1683. {
  1684.     uint32_t *bits = image->bits + line * image->rowstride;
  1685.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1686.     uint32_t a, r, g, b;
  1687.  
  1688.     a = ((pixel & 0x8) * 0xff) << 21;
  1689.     r = ((pixel & 0x4) * 0xff) << 14;
  1690.     g = ((pixel & 0x2) * 0xff) << 7;
  1691.     b = ((pixel & 0x1) * 0xff);
  1692.  
  1693.     return a | r | g | b;
  1694. }
  1695.  
  1696. static uint32_t
  1697. fetch_pixel_a1b1g1r1 (bits_image_t *image,
  1698.                       int           offset,
  1699.                       int           line)
  1700. {
  1701.     uint32_t *bits = image->bits + line * image->rowstride;
  1702.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1703.     uint32_t a, r, g, b;
  1704.  
  1705.     a = ((pixel & 0x8) * 0xff) << 21;
  1706.     b = ((pixel & 0x4) * 0xff) >> 2;
  1707.     g = ((pixel & 0x2) * 0xff) << 7;
  1708.     r = ((pixel & 0x1) * 0xff) << 16;
  1709.  
  1710.     return a | r | g | b;
  1711. }
  1712.  
  1713. static uint32_t
  1714. fetch_pixel_c4 (bits_image_t *image,
  1715.                 int           offset,
  1716.                 int           line)
  1717. {
  1718.     uint32_t *bits = image->bits + line * image->rowstride;
  1719.     uint32_t pixel = FETCH_4 (image, bits, offset);
  1720.     const pixman_indexed_t * indexed = image->indexed;
  1721.  
  1722.     return indexed->rgba[pixel];
  1723. }
  1724.  
  1725. static uint32_t
  1726. fetch_pixel_a1 (bits_image_t *image,
  1727.                 int           offset,
  1728.                 int           line)
  1729. {
  1730.     uint32_t *bits = image->bits + line * image->rowstride;
  1731.     uint32_t pixel = READ (image, bits + (offset >> 5));
  1732.     uint32_t a;
  1733.    
  1734. #ifdef WORDS_BIGENDIAN
  1735.     a = pixel >> (0x1f - (offset & 0x1f));
  1736. #else
  1737.     a = pixel >> (offset & 0x1f);
  1738. #endif
  1739.     a = a & 1;
  1740.     a |= a << 1;
  1741.     a |= a << 2;
  1742.     a |= a << 4;
  1743.    
  1744.     return a << 24;
  1745. }
  1746.  
  1747. static uint32_t
  1748. fetch_pixel_g1 (bits_image_t *image,
  1749.                 int           offset,
  1750.                 int           line)
  1751. {
  1752.     uint32_t *bits = image->bits + line * image->rowstride;
  1753.     uint32_t pixel = READ (image, bits + (offset >> 5));
  1754.     const pixman_indexed_t * indexed = image->indexed;
  1755.     uint32_t a;
  1756.    
  1757. #ifdef WORDS_BIGENDIAN
  1758.     a = pixel >> (0x1f - (offset & 0x1f));
  1759. #else
  1760.     a = pixel >> (offset & 0x1f);
  1761. #endif
  1762.     a = a & 1;
  1763.    
  1764.     return indexed->rgba[a];
  1765. }
  1766.  
  1767. static uint32_t
  1768. fetch_pixel_yuy2 (bits_image_t *image,
  1769.                   int           offset,
  1770.                   int           line)
  1771. {
  1772.     const uint32_t *bits = image->bits + image->rowstride * line;
  1773.    
  1774.     int16_t y, u, v;
  1775.     int32_t r, g, b;
  1776.    
  1777.     y = ((uint8_t *) bits)[offset << 1] - 16;
  1778.     u = ((uint8_t *) bits)[((offset << 1) & - 4) + 1] - 128;
  1779.     v = ((uint8_t *) bits)[((offset << 1) & - 4) + 3] - 128;
  1780.    
  1781.     /* R = 1.164(Y - 16) + 1.596(V - 128) */
  1782.     r = 0x012b27 * y + 0x019a2e * v;
  1783.    
  1784.     /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
  1785.     g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
  1786.    
  1787.     /* B = 1.164(Y - 16) + 2.018(U - 128) */
  1788.     b = 0x012b27 * y + 0x0206a2 * u;
  1789.    
  1790.     return 0xff000000 |
  1791.         (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
  1792.         (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
  1793.         (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
  1794. }
  1795.  
  1796. static uint32_t
  1797. fetch_pixel_yv12 (bits_image_t *image,
  1798.                   int           offset,
  1799.                   int           line)
  1800. {
  1801.     YV12_SETUP (image);
  1802.     int16_t y = YV12_Y (line)[offset] - 16;
  1803.     int16_t u = YV12_U (line)[offset >> 1] - 128;
  1804.     int16_t v = YV12_V (line)[offset >> 1] - 128;
  1805.     int32_t r, g, b;
  1806.    
  1807.     /* R = 1.164(Y - 16) + 1.596(V - 128) */
  1808.     r = 0x012b27 * y + 0x019a2e * v;
  1809.    
  1810.     /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
  1811.     g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
  1812.    
  1813.     /* B = 1.164(Y - 16) + 2.018(U - 128) */
  1814.     b = 0x012b27 * y + 0x0206a2 * u;
  1815.    
  1816.     return 0xff000000 |
  1817.         (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
  1818.         (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
  1819.         (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
  1820. }
  1821.  
  1822. /*********************************** Store ************************************/
  1823.  
  1824. #define SPLIT_A(v)              \
  1825.     uint32_t a = ((v) >> 24),   \
  1826.         r = ((v) >> 16) & 0xff, \
  1827.         g = ((v) >> 8) & 0xff,  \
  1828.         b = (v) & 0xff
  1829.  
  1830. #define SPLIT(v)                     \
  1831.     uint32_t r = ((v) >> 16) & 0xff, \
  1832.         g = ((v) >> 8) & 0xff,       \
  1833.         b = (v) & 0xff
  1834.  
  1835. static void
  1836. store_scanline_a2r10g10b10 (bits_image_t *  image,
  1837.                             int             x,
  1838.                             int             y,
  1839.                             int             width,
  1840.                             const uint32_t *v)
  1841. {
  1842.     uint32_t *bits = image->bits + image->rowstride * y;
  1843.     uint32_t *pixel = bits + x;
  1844.     uint64_t *values = (uint64_t *)v;
  1845.     int i;
  1846.    
  1847.     for (i = 0; i < width; ++i)
  1848.     {
  1849.         WRITE (image, pixel++,
  1850.                ((values[i] >> 32) & 0xc0000000) |
  1851.                ((values[i] >> 18) & 0x3ff00000) |
  1852.                ((values[i] >> 12) & 0xffc00) |
  1853.                ((values[i] >> 6) & 0x3ff));    
  1854.     }
  1855. }
  1856.  
  1857. static void
  1858. store_scanline_x2r10g10b10 (bits_image_t *  image,
  1859.                             int             x,
  1860.                             int             y,
  1861.                             int             width,
  1862.                             const uint32_t *v)
  1863. {
  1864.     uint32_t *bits = image->bits + image->rowstride * y;
  1865.     uint64_t *values = (uint64_t *)v;
  1866.     uint32_t *pixel = bits + x;
  1867.     int i;
  1868.    
  1869.     for (i = 0; i < width; ++i)
  1870.     {
  1871.         WRITE (image, pixel++,
  1872.                ((values[i] >> 18) & 0x3ff00000) |
  1873.                ((values[i] >> 12) & 0xffc00) |
  1874.                ((values[i] >> 6) & 0x3ff));
  1875.     }
  1876. }
  1877.  
  1878. static void
  1879. store_scanline_a2b10g10r10 (bits_image_t *  image,
  1880.                             int             x,
  1881.                             int             y,
  1882.                             int             width,
  1883.                             const uint32_t *v)
  1884. {
  1885.     uint32_t *bits = image->bits + image->rowstride * y;
  1886.     uint32_t *pixel = bits + x;
  1887.     uint64_t *values = (uint64_t *)v;
  1888.     int i;
  1889.    
  1890.     for (i = 0; i < width; ++i)
  1891.     {
  1892.         WRITE (image, pixel++,
  1893.                ((values[i] >> 32) & 0xc0000000) |
  1894.                ((values[i] >> 38) & 0x3ff) |
  1895.                ((values[i] >> 12) & 0xffc00) |
  1896.                ((values[i] << 14) & 0x3ff00000));
  1897.     }
  1898. }
  1899.  
  1900. static void
  1901. store_scanline_x2b10g10r10 (bits_image_t *  image,
  1902.                             int             x,
  1903.                             int             y,
  1904.                             int             width,
  1905.                             const uint32_t *v)
  1906. {
  1907.     uint32_t *bits = image->bits + image->rowstride * y;
  1908.     uint64_t *values = (uint64_t *)v;
  1909.     uint32_t *pixel = bits + x;
  1910.     int i;
  1911.    
  1912.     for (i = 0; i < width; ++i)
  1913.     {
  1914.         WRITE (image, pixel++,
  1915.                ((values[i] >> 38) & 0x3ff) |
  1916.                ((values[i] >> 12) & 0xffc00) |
  1917.                ((values[i] << 14) & 0x3ff00000));
  1918.     }
  1919. }
  1920.  
  1921. static void
  1922. store_scanline_a8r8g8b8 (bits_image_t *  image,
  1923.                          int             x,
  1924.                          int             y,
  1925.                          int             width,
  1926.                          const uint32_t *values)
  1927. {
  1928.     uint32_t *bits = image->bits + image->rowstride * y;
  1929.    
  1930.     MEMCPY_WRAPPED (image, ((uint32_t *)bits) + x, values,
  1931.                     width * sizeof(uint32_t));
  1932. }
  1933.  
  1934. static void
  1935. store_scanline_x8r8g8b8 (bits_image_t *  image,
  1936.                          int             x,
  1937.                          int             y,
  1938.                          int             width,
  1939.                          const uint32_t *values)
  1940. {
  1941.     uint32_t *bits = image->bits + image->rowstride * y;
  1942.     uint32_t *pixel = (uint32_t *)bits + x;
  1943.     int i;
  1944.    
  1945.     for (i = 0; i < width; ++i)
  1946.         WRITE (image, pixel++, values[i] & 0xffffff);
  1947. }
  1948.  
  1949. static void
  1950. store_scanline_a8b8g8r8 (bits_image_t *  image,
  1951.                          int             x,
  1952.                          int             y,
  1953.                          int             width,
  1954.                          const uint32_t *values)
  1955. {
  1956.     uint32_t *bits = image->bits + image->rowstride * y;
  1957.     uint32_t *pixel = (uint32_t *)bits + x;
  1958.     int i;
  1959.    
  1960.     for (i = 0; i < width; ++i)
  1961.     {
  1962.         WRITE (image, pixel++,
  1963.                (values[i] & 0xff00ff00)         |
  1964.                ((values[i] >> 16) & 0xff)       |
  1965.                ((values[i] & 0xff) << 16));
  1966.     }
  1967. }
  1968.  
  1969. static void
  1970. store_scanline_x8b8g8r8 (bits_image_t *  image,
  1971.                          int             x,
  1972.                          int             y,
  1973.                          int             width,
  1974.                          const uint32_t *values)
  1975. {
  1976.     uint32_t *bits = image->bits + image->rowstride * y;
  1977.     uint32_t *pixel = (uint32_t *)bits + x;
  1978.     int i;
  1979.    
  1980.     for (i = 0; i < width; ++i)
  1981.     {
  1982.         WRITE (image, pixel++,
  1983.                (values[i] & 0x0000ff00)         |
  1984.                ((values[i] >> 16) & 0xff)       |
  1985.                ((values[i] & 0xff) << 16));
  1986.     }
  1987. }
  1988.  
  1989. static void
  1990. store_scanline_b8g8r8a8 (bits_image_t *  image,
  1991.                          int             x,
  1992.                          int             y,
  1993.                          int             width,
  1994.                          const uint32_t *values)
  1995. {
  1996.     uint32_t *bits = image->bits + image->rowstride * y;
  1997.     uint32_t *pixel = (uint32_t *)bits + x;
  1998.     int i;
  1999.    
  2000.     for (i = 0; i < width; ++i)
  2001.     {
  2002.         WRITE (image, pixel++,
  2003.                ((values[i] >> 24) & 0x000000ff) |
  2004.                ((values[i] >>  8) & 0x0000ff00) |
  2005.                ((values[i] <<  8) & 0x00ff0000) |
  2006.                ((values[i] << 24) & 0xff000000));
  2007.     }
  2008. }
  2009.  
  2010. static void
  2011. store_scanline_b8g8r8x8 (bits_image_t *  image,
  2012.                          int             x,
  2013.                          int             y,
  2014.                          int             width,
  2015.                          const uint32_t *values)
  2016. {
  2017.     uint32_t *bits = image->bits + image->rowstride * y;
  2018.     uint32_t *pixel = (uint32_t *)bits + x;
  2019.     int i;
  2020.    
  2021.     for (i = 0; i < width; ++i)
  2022.     {
  2023.         WRITE (image, pixel++,
  2024.                ((values[i] >>  8) & 0x0000ff00) |
  2025.                ((values[i] <<  8) & 0x00ff0000) |
  2026.                ((values[i] << 24) & 0xff000000));
  2027.     }
  2028. }
  2029.  
  2030. static void
  2031. store_scanline_x14r6g6b6 (bits_image_t *  image,
  2032.                           int             x,
  2033.                           int             y,
  2034.                           int             width,
  2035.                           const uint32_t *values)
  2036. {
  2037.     uint32_t *bits = image->bits + image->rowstride * y;
  2038.     uint32_t *pixel = ((uint32_t *) bits) + x;
  2039.     int i;
  2040.  
  2041.     for (i = 0; i < width; ++i)
  2042.     {
  2043.         uint32_t s = values[i];
  2044.         uint32_t r, g, b;
  2045.  
  2046.         r = (s & 0xfc0000) >> 6;
  2047.         g = (s & 0x00fc00) >> 4;
  2048.         b = (s & 0x0000fc) >> 2;
  2049.  
  2050.         WRITE (image, pixel++, r | g | b);
  2051.     }
  2052. }
  2053.  
  2054. static void
  2055. store_scanline_r8g8b8 (bits_image_t *  image,
  2056.                        int             x,
  2057.                        int             y,
  2058.                        int             width,
  2059.                        const uint32_t *values)
  2060. {
  2061.     uint32_t *bits = image->bits + image->rowstride * y;
  2062.     uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
  2063.     int i;
  2064.    
  2065.     for (i = 0; i < width; ++i)
  2066.     {
  2067.         uint32_t val = values[i];
  2068.        
  2069. #ifdef WORDS_BIGENDIAN
  2070.         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
  2071.         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
  2072.         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
  2073. #else
  2074.         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
  2075.         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
  2076.         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
  2077. #endif
  2078.     }
  2079. }
  2080.  
  2081. static void
  2082. store_scanline_b8g8r8 (bits_image_t *  image,
  2083.                        int             x,
  2084.                        int             y,
  2085.                        int             width,
  2086.                        const uint32_t *values)
  2087. {
  2088.     uint32_t *bits = image->bits + image->rowstride * y;
  2089.     uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
  2090.     int i;
  2091.    
  2092.     for (i = 0; i < width; ++i)
  2093.     {
  2094.         uint32_t val = values[i];
  2095.        
  2096. #ifdef WORDS_BIGENDIAN
  2097.         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
  2098.         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
  2099.         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
  2100. #else
  2101.         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
  2102.         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
  2103.         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
  2104. #endif
  2105.     }
  2106. }
  2107.  
  2108. static void
  2109. store_scanline_r5g6b5 (bits_image_t *  image,
  2110.                        int             x,
  2111.                        int             y,
  2112.                        int             width,
  2113.                        const uint32_t *values)
  2114. {
  2115.     uint32_t *bits = image->bits + image->rowstride * y;
  2116.     uint16_t *pixel = ((uint16_t *) bits) + x;
  2117.     int i;
  2118.    
  2119.     for (i = 0; i < width; ++i)
  2120.     {
  2121.         uint32_t s = values[i];
  2122.        
  2123.         WRITE (image, pixel++,
  2124.                ((s >> 3) & 0x001f) |
  2125.                ((s >> 5) & 0x07e0) |
  2126.                ((s >> 8) & 0xf800));
  2127.     }
  2128. }
  2129.  
  2130. static void
  2131. store_scanline_b5g6r5 (bits_image_t *  image,
  2132.                        int             x,
  2133.                        int             y,
  2134.                        int             width,
  2135.                        const uint32_t *values)
  2136. {
  2137.     uint32_t *bits = image->bits + image->rowstride * y;
  2138.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2139.     int i;
  2140.    
  2141.     for (i = 0; i < width; ++i)
  2142.     {
  2143.         SPLIT (values[i]);
  2144.        
  2145.         WRITE (image, pixel++,
  2146.                ((b << 8) & 0xf800) |
  2147.                ((g << 3) & 0x07e0) |
  2148.                ((r >> 3)         ));
  2149.     }
  2150. }
  2151.  
  2152. static void
  2153. store_scanline_a1r5g5b5 (bits_image_t *  image,
  2154.                          int             x,
  2155.                          int             y,
  2156.                          int             width,
  2157.                          const uint32_t *values)
  2158. {
  2159.     uint32_t *bits = image->bits + image->rowstride * y;
  2160.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2161.     int i;
  2162.    
  2163.     for (i = 0; i < width; ++i)
  2164.     {
  2165.         SPLIT_A (values[i]);
  2166.        
  2167.         WRITE (image, pixel++,
  2168.                ((a << 8) & 0x8000) |
  2169.                ((r << 7) & 0x7c00) |
  2170.                ((g << 2) & 0x03e0) |
  2171.                ((b >> 3)         ));
  2172.     }
  2173. }
  2174.  
  2175. static void
  2176. store_scanline_x1r5g5b5 (bits_image_t *  image,
  2177.                          int             x,
  2178.                          int             y,
  2179.                          int             width,
  2180.                          const uint32_t *values)
  2181. {
  2182.     uint32_t *bits = image->bits + image->rowstride * y;
  2183.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2184.     int i;
  2185.    
  2186.     for (i = 0; i < width; ++i)
  2187.     {
  2188.         SPLIT (values[i]);
  2189.        
  2190.         WRITE (image, pixel++,
  2191.                ((r << 7) & 0x7c00) |
  2192.                ((g << 2) & 0x03e0) |
  2193.                ((b >> 3)         ));
  2194.     }
  2195. }
  2196.  
  2197. static void
  2198. store_scanline_a1b5g5r5 (bits_image_t *  image,
  2199.                          int             x,
  2200.                          int             y,
  2201.                          int             width,
  2202.                          const uint32_t *values)
  2203. {
  2204.     uint32_t *bits = image->bits + image->rowstride * y;
  2205.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2206.     int i;
  2207.    
  2208.     for (i = 0; i < width; ++i)
  2209.     {
  2210.         SPLIT_A (values[i]);
  2211.        
  2212.         WRITE (image, pixel++,
  2213.                ((a << 8) & 0x8000) |
  2214.                ((b << 7) & 0x7c00) |
  2215.                ((g << 2) & 0x03e0) |
  2216.                ((r >> 3)         ));
  2217.     }
  2218. }
  2219.  
  2220. static void
  2221. store_scanline_x1b5g5r5 (bits_image_t *  image,
  2222.                          int             x,
  2223.                          int             y,
  2224.                          int             width,
  2225.                          const uint32_t *values)
  2226. {
  2227.     uint32_t *bits = image->bits + image->rowstride * y;
  2228.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2229.     int i;
  2230.    
  2231.     for (i = 0; i < width; ++i)
  2232.     {
  2233.         SPLIT (values[i]);
  2234.        
  2235.         WRITE (image, pixel++, ((b << 7) & 0x7c00) |
  2236.                ((g << 2) & 0x03e0) |
  2237.                ((r >> 3)         ));
  2238.     }
  2239. }
  2240.  
  2241. static void
  2242. store_scanline_a4r4g4b4 (bits_image_t *  image,
  2243.                          int             x,
  2244.                          int             y,
  2245.                          int             width,
  2246.                          const uint32_t *values)
  2247. {
  2248.     uint32_t *bits = image->bits + image->rowstride * y;
  2249.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2250.     int i;
  2251.    
  2252.     for (i = 0; i < width; ++i)
  2253.     {
  2254.         SPLIT_A (values[i]);
  2255.        
  2256.         WRITE (image, pixel++,
  2257.                ((a << 8) & 0xf000) |
  2258.                ((r << 4) & 0x0f00) |
  2259.                ((g     ) & 0x00f0) |
  2260.                ((b >> 4)         ));
  2261.     }
  2262. }
  2263.  
  2264. static void
  2265. store_scanline_x4r4g4b4 (bits_image_t *  image,
  2266.                          int             x,
  2267.                          int             y,
  2268.                          int             width,
  2269.                          const uint32_t *values)
  2270. {
  2271.     uint32_t *bits = image->bits + image->rowstride * y;
  2272.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2273.     int i;
  2274.    
  2275.     for (i = 0; i < width; ++i)
  2276.     {
  2277.         SPLIT (values[i]);
  2278.        
  2279.         WRITE (image, pixel++,
  2280.                ((r << 4) & 0x0f00) |
  2281.                ((g     ) & 0x00f0) |
  2282.                ((b >> 4)         ));
  2283.     }
  2284. }
  2285.  
  2286. static void
  2287. store_scanline_a4b4g4r4 (bits_image_t *  image,
  2288.                          int             x,
  2289.                          int             y,
  2290.                          int             width,
  2291.                          const uint32_t *values)
  2292. {
  2293.     uint32_t *bits = image->bits + image->rowstride * y;
  2294.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2295.     int i;
  2296.    
  2297.     for (i = 0; i < width; ++i)
  2298.     {
  2299.         SPLIT_A (values[i]);
  2300.         WRITE (image, pixel++, ((a << 8) & 0xf000) |
  2301.                ((b << 4) & 0x0f00) |
  2302.                ((g     ) & 0x00f0) |
  2303.                ((r >> 4)         ));
  2304.     }
  2305. }
  2306.  
  2307. static void
  2308. store_scanline_x4b4g4r4 (bits_image_t *  image,
  2309.                          int             x,
  2310.                          int             y,
  2311.                          int             width,
  2312.                          const uint32_t *values)
  2313. {
  2314.     uint32_t *bits = image->bits + image->rowstride * y;
  2315.     uint16_t  *pixel = ((uint16_t *) bits) + x;
  2316.     int i;
  2317.    
  2318.     for (i = 0; i < width; ++i)
  2319.     {
  2320.         SPLIT (values[i]);
  2321.        
  2322.         WRITE (image, pixel++,
  2323.                ((b << 4) & 0x0f00) |
  2324.                ((g     ) & 0x00f0) |
  2325.                ((r >> 4)         ));
  2326.     }
  2327. }
  2328.  
  2329. static void
  2330. store_scanline_a8 (bits_image_t *  image,
  2331.                    int             x,
  2332.                    int             y,
  2333.                    int             width,
  2334.                    const uint32_t *values)
  2335. {
  2336.     uint32_t *bits = image->bits + image->rowstride * y;
  2337.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2338.     int i;
  2339.    
  2340.     for (i = 0; i < width; ++i)
  2341.     {
  2342.         WRITE (image, pixel++, values[i] >> 24);
  2343.     }
  2344. }
  2345.  
  2346. static void
  2347. store_scanline_r3g3b2 (bits_image_t *  image,
  2348.                        int             x,
  2349.                        int             y,
  2350.                        int             width,
  2351.                        const uint32_t *values)
  2352. {
  2353.     uint32_t *bits = image->bits + image->rowstride * y;
  2354.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2355.     int i;
  2356.    
  2357.     for (i = 0; i < width; ++i)
  2358.     {
  2359.         SPLIT (values[i]);
  2360.        
  2361.         WRITE (image, pixel++,
  2362.                ((r     ) & 0xe0) |
  2363.                ((g >> 3) & 0x1c) |
  2364.                ((b >> 6)       ));
  2365.     }
  2366. }
  2367.  
  2368. static void
  2369. store_scanline_b2g3r3 (bits_image_t *  image,
  2370.                        int             x,
  2371.                        int             y,
  2372.                        int             width,
  2373.                        const uint32_t *values)
  2374. {
  2375.     uint32_t *bits = image->bits + image->rowstride * y;
  2376.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2377.     int i;
  2378.    
  2379.     for (i = 0; i < width; ++i)
  2380.     {
  2381.         SPLIT (values[i]);
  2382.        
  2383.         WRITE (image, pixel++,
  2384.                ((b     ) & 0xc0) |
  2385.                ((g >> 2) & 0x38) |
  2386.                ((r >> 5)       ));
  2387.     }
  2388. }
  2389.  
  2390. static void
  2391. store_scanline_a2r2g2b2 (bits_image_t *  image,
  2392.                          int             x,
  2393.                          int             y,
  2394.                          int             width,
  2395.                          const uint32_t *values)
  2396. {
  2397.     uint32_t *bits = image->bits + image->rowstride * y;
  2398.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2399.     int i;
  2400.    
  2401.     for (i = 0; i < width; ++i)
  2402.     {
  2403.         SPLIT_A (values[i]);
  2404.        
  2405.         WRITE (image, pixel++,
  2406.                ((a     ) & 0xc0) |
  2407.                ((r >> 2) & 0x30) |
  2408.                ((g >> 4) & 0x0c) |
  2409.                ((b >> 6)       ));
  2410.     }
  2411. }
  2412.  
  2413. static void
  2414. store_scanline_a2b2g2r2 (bits_image_t *  image,
  2415.                          int             x,
  2416.                          int             y,
  2417.                          int             width,
  2418.                          const uint32_t *values)
  2419. {
  2420.     uint32_t *bits = image->bits + image->rowstride * y;
  2421.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2422.     int i;
  2423.    
  2424.     for (i = 0; i < width; ++i)
  2425.     {
  2426.         SPLIT_A (values[i]);
  2427.        
  2428.         WRITE (image, pixel++,
  2429.                ((a     ) & 0xc0) |
  2430.                ((b >> 2) & 0x30) |
  2431.                ((g >> 4) & 0x0c) |
  2432.                ((r >> 6)       ));
  2433.     }
  2434. }
  2435.  
  2436. static void
  2437. store_scanline_c8 (bits_image_t *  image,
  2438.                    int             x,
  2439.                    int             y,
  2440.                    int             width,
  2441.                    const uint32_t *values)
  2442. {
  2443.     uint32_t *bits = image->bits + image->rowstride * y;
  2444.     uint8_t *pixel = ((uint8_t *) bits) + x;
  2445.     const pixman_indexed_t *indexed = image->indexed;
  2446.     int i;
  2447.    
  2448.     for (i = 0; i < width; ++i)
  2449.         WRITE (image, pixel++, RGB24_TO_ENTRY (indexed,values[i]));
  2450. }
  2451.  
  2452. static void
  2453. store_scanline_g8 (bits_image_t *  image,
  2454.                    int             x,
  2455.                    int             y,
  2456.                    int             width,
  2457.                    const uint32_t *values)
  2458. {
  2459.     uint32_t *bits = image->bits + image->rowstride * y;
  2460.     uint8_t *pixel = ((uint8_t *) bits) + x;
  2461.     const pixman_indexed_t *indexed = image->indexed;
  2462.     int i;
  2463.  
  2464.     for (i = 0; i < width; ++i)
  2465.         WRITE (image, pixel++, RGB24_TO_ENTRY_Y (indexed,values[i]));
  2466. }
  2467.  
  2468. static void
  2469. store_scanline_x4a4 (bits_image_t *  image,
  2470.                      int             x,
  2471.                      int             y,
  2472.                      int             width,
  2473.                      const uint32_t *values)
  2474. {
  2475.     uint32_t *bits = image->bits + image->rowstride * y;
  2476.     uint8_t   *pixel = ((uint8_t *) bits) + x;
  2477.     int i;
  2478.  
  2479.     for (i = 0; i < width; ++i)
  2480.         WRITE (image, pixel++, values[i] >> 28);
  2481. }
  2482.  
  2483. #define STORE_8(img,l,o,v)  (WRITE (img, (uint8_t *)(l) + ((o) >> 3), (v)))
  2484. #ifdef WORDS_BIGENDIAN
  2485.  
  2486. #define STORE_4(img,l,o,v)                                              \
  2487.     do                                                                  \
  2488.     {                                                                   \
  2489.         int bo = 4 * (o);                                               \
  2490.         int v4 = (v) & 0x0f;                                            \
  2491.                                                                         \
  2492.         STORE_8 (img, l, bo, (                                          \
  2493.                      bo & 4 ?                                           \
  2494.                      (FETCH_8 (img, l, bo) & 0xf0) | (v4) :             \
  2495.                      (FETCH_8 (img, l, bo) & 0x0f) | (v4 << 4)));       \
  2496.     } while (0)
  2497. #else
  2498.  
  2499. #define STORE_4(img,l,o,v)                                              \
  2500.     do                                                                  \
  2501.     {                                                                   \
  2502.         int bo = 4 * (o);                                               \
  2503.         int v4 = (v) & 0x0f;                                            \
  2504.                                                                         \
  2505.         STORE_8 (img, l, bo, (                                          \
  2506.                      bo & 4 ?                                           \
  2507.                      (FETCH_8 (img, l, bo) & 0x0f) | (v4 << 4) :        \
  2508.                      (FETCH_8 (img, l, bo) & 0xf0) | (v4)));            \
  2509.     } while (0)
  2510. #endif
  2511.  
  2512. static void
  2513. store_scanline_a4 (bits_image_t *  image,
  2514.                    int             x,
  2515.                    int             y,
  2516.                    int             width,
  2517.                    const uint32_t *values)
  2518. {
  2519.     uint32_t *bits = image->bits + image->rowstride * y;
  2520.     int i;
  2521.  
  2522.     for (i = 0; i < width; ++i)
  2523.         STORE_4 (image, bits, i + x, values[i] >> 28);
  2524. }
  2525.  
  2526. static void
  2527. store_scanline_r1g2b1 (bits_image_t *  image,
  2528.                        int             x,
  2529.                        int             y,
  2530.                        int             width,
  2531.                        const uint32_t *values)
  2532. {
  2533.     uint32_t *bits = image->bits + image->rowstride * y;
  2534.     int i;
  2535.  
  2536.     for (i = 0; i < width; ++i)
  2537.     {
  2538.         uint32_t pixel;
  2539.  
  2540.         SPLIT (values[i]);
  2541.         pixel = (((r >> 4) & 0x8) |
  2542.                  ((g >> 5) & 0x6) |
  2543.                  ((b >> 7)      ));
  2544.         STORE_4 (image, bits, i + x, pixel);
  2545.     }
  2546. }
  2547.  
  2548. static void
  2549. store_scanline_b1g2r1 (bits_image_t *  image,
  2550.                        int             x,
  2551.                        int             y,
  2552.                        int             width,
  2553.                        const uint32_t *values)
  2554. {
  2555.     uint32_t *bits = image->bits + image->rowstride * y;
  2556.     int i;
  2557.  
  2558.     for (i = 0; i < width; ++i)
  2559.     {
  2560.         uint32_t pixel;
  2561.  
  2562.         SPLIT (values[i]);
  2563.         pixel = (((b >> 4) & 0x8) |
  2564.                  ((g >> 5) & 0x6) |
  2565.                  ((r >> 7)      ));
  2566.         STORE_4 (image, bits, i + x, pixel);
  2567.     }
  2568. }
  2569.  
  2570. static void
  2571. store_scanline_a1r1g1b1 (bits_image_t *  image,
  2572.                          int             x,
  2573.                          int             y,
  2574.                          int             width,
  2575.                          const uint32_t *values)
  2576. {
  2577.     uint32_t *bits = image->bits + image->rowstride * y;
  2578.     int i;
  2579.  
  2580.     for (i = 0; i < width; ++i)
  2581.     {
  2582.         uint32_t pixel;
  2583.  
  2584.         SPLIT_A (values[i]);
  2585.         pixel = (((a >> 4) & 0x8) |
  2586.                  ((r >> 5) & 0x4) |
  2587.                  ((g >> 6) & 0x2) |
  2588.                  ((b >> 7)      ));
  2589.  
  2590.         STORE_4 (image, bits, i + x, pixel);
  2591.     }
  2592. }
  2593.  
  2594. static void
  2595. store_scanline_a1b1g1r1 (bits_image_t *  image,
  2596.                          int             x,
  2597.                          int             y,
  2598.                          int             width,
  2599.                          const uint32_t *values)
  2600. {
  2601.     uint32_t *bits = image->bits + image->rowstride * y;
  2602.     int i;
  2603.  
  2604.     for (i = 0; i < width; ++i)
  2605.     {
  2606.         uint32_t pixel;
  2607.  
  2608.         SPLIT_A (values[i]);
  2609.         pixel = (((a >> 4) & 0x8) |
  2610.                  ((b >> 5) & 0x4) |
  2611.                  ((g >> 6) & 0x2) |
  2612.                  ((r >> 7)      ));
  2613.  
  2614.         STORE_4 (image, bits, i + x, pixel);
  2615.     }
  2616. }
  2617.  
  2618. static void
  2619. store_scanline_c4 (bits_image_t *  image,
  2620.                    int             x,
  2621.                    int             y,
  2622.                    int             width,
  2623.                    const uint32_t *values)
  2624. {
  2625.     uint32_t *bits = image->bits + image->rowstride * y;
  2626.     const pixman_indexed_t *indexed = image->indexed;
  2627.     int i;
  2628.    
  2629.     for (i = 0; i < width; ++i)
  2630.     {
  2631.         uint32_t pixel;
  2632.        
  2633.         pixel = RGB24_TO_ENTRY (indexed, values[i]);
  2634.         STORE_4 (image, bits, i + x, pixel);
  2635.     }
  2636. }
  2637.  
  2638. static void
  2639. store_scanline_g4 (bits_image_t *  image,
  2640.                    int             x,
  2641.                    int             y,
  2642.                    int             width,
  2643.                    const uint32_t *values)
  2644. {
  2645.     uint32_t *bits = image->bits + image->rowstride * y;
  2646.     const pixman_indexed_t *indexed = image->indexed;
  2647.     int i;
  2648.    
  2649.     for (i = 0; i < width; ++i)
  2650.     {
  2651.         uint32_t pixel;
  2652.        
  2653.         pixel = RGB24_TO_ENTRY_Y (indexed, values[i]);
  2654.         STORE_4 (image, bits, i + x, pixel);
  2655.     }
  2656. }
  2657.  
  2658. static void
  2659. store_scanline_a1 (bits_image_t *  image,
  2660.                    int             x,
  2661.                    int             y,
  2662.                    int             width,
  2663.                    const uint32_t *values)
  2664. {
  2665.     uint32_t *bits = image->bits + image->rowstride * y;
  2666.     int i;
  2667.    
  2668.     for (i = 0; i < width; ++i)
  2669.     {
  2670.         uint32_t  *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
  2671.         uint32_t mask, v;
  2672.        
  2673. #ifdef WORDS_BIGENDIAN
  2674.         mask = 1 << (0x1f - ((i + x) & 0x1f));
  2675. #else
  2676.         mask = 1 << ((i + x) & 0x1f);
  2677. #endif
  2678.         v = values[i] & 0x80000000 ? mask : 0;
  2679.        
  2680.         WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
  2681.     }
  2682. }
  2683.  
  2684. static void
  2685. store_scanline_g1 (bits_image_t *  image,
  2686.                    int             x,
  2687.                    int             y,
  2688.                    int             width,
  2689.                    const uint32_t *values)
  2690. {
  2691.     uint32_t *bits = image->bits + image->rowstride * y;
  2692.     const pixman_indexed_t *indexed = image->indexed;
  2693.     int i;
  2694.    
  2695.     for (i = 0; i < width; ++i)
  2696.     {
  2697.         uint32_t  *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
  2698.         uint32_t mask, v;
  2699.        
  2700. #ifdef WORDS_BIGENDIAN
  2701.         mask = 1 << (0x1f - ((i + x) & 0x1f));
  2702. #else
  2703.         mask = 1 << ((i + x) & 0x1f);
  2704. #endif
  2705.         v = RGB24_TO_ENTRY_Y (indexed, values[i]) & 0x1 ? mask : 0;
  2706.        
  2707.         WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
  2708.     }
  2709. }
  2710.  
  2711. /*
  2712.  * Contracts a 64bpp image to 32bpp and then stores it using a regular 32-bit
  2713.  * store proc. Despite the type, this function expects a uint64_t buffer.
  2714.  */
  2715. static void
  2716. store_scanline_generic_64 (bits_image_t *  image,
  2717.                            int             x,
  2718.                            int             y,
  2719.                            int             width,
  2720.                            const uint32_t *values)
  2721. {
  2722.     uint32_t *argb8_pixels;
  2723.    
  2724.     assert (image->common.type == BITS);
  2725.    
  2726.     argb8_pixels = pixman_malloc_ab (width, sizeof(uint32_t));
  2727.     if (!argb8_pixels)
  2728.         return;
  2729.    
  2730.     /* Contract the scanline.  We could do this in place if values weren't
  2731.      * const.
  2732.      */
  2733.     pixman_contract (argb8_pixels, (uint64_t *)values, width);
  2734.    
  2735.     image->store_scanline_32 (image, x, y, width, argb8_pixels);
  2736.    
  2737.     free (argb8_pixels);
  2738. }
  2739.  
  2740. /* Despite the type, this function expects both buffer
  2741.  * and mask to be uint64_t
  2742.  */
  2743. static void
  2744. fetch_scanline_generic_64 (pixman_image_t *image,
  2745.                            int             x,
  2746.                            int             y,
  2747.                            int             width,
  2748.                            uint32_t *      buffer,
  2749.                            const uint32_t *mask)
  2750. {
  2751.     pixman_format_code_t format;
  2752.    
  2753.     /* Fetch the pixels into the first half of buffer and then expand them in
  2754.      * place.
  2755.      */
  2756.     image->bits.fetch_scanline_32 (image, x, y, width, buffer, NULL);
  2757.  
  2758.     format = image->bits.format;
  2759.     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR        ||
  2760.         PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY)
  2761.     {
  2762.         /* Indexed formats are mapped to a8r8g8b8 with full
  2763.          * precision, so when expanding we shouldn't correct
  2764.          * for the width of the channels
  2765.          */
  2766.        
  2767.         format = PIXMAN_a8r8g8b8;
  2768.     }
  2769.    
  2770.     pixman_expand ((uint64_t *)buffer, buffer, format, width);
  2771. }
  2772.  
  2773. /* Despite the type, this function expects a uint64_t *buffer */
  2774. static uint64_t
  2775. fetch_pixel_generic_64 (bits_image_t *image,
  2776.                         int           offset,
  2777.                         int           line)
  2778. {
  2779.     uint32_t pixel32 = image->fetch_pixel_32 (image, offset, line);
  2780.     uint64_t result;
  2781.     pixman_format_code_t format;
  2782.  
  2783.     format = image->format;
  2784.     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_COLOR        ||
  2785.         PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_GRAY)
  2786.     {
  2787.         /* Indexed formats are mapped to a8r8g8b8 with full
  2788.          * precision, so when expanding we shouldn't correct
  2789.          * for the width of the channels
  2790.          */
  2791.        
  2792.         format = PIXMAN_a8r8g8b8;
  2793.     }
  2794.    
  2795.     pixman_expand ((uint64_t *)&result, &pixel32, format, 1);
  2796.  
  2797.     return result;
  2798. }
  2799.  
  2800. /*
  2801.  * XXX: The transformed fetch path only works at 32-bpp so far.  When all
  2802.  * paths have wide versions, this can be removed.
  2803.  *
  2804.  * WARNING: This function loses precision!
  2805.  */
  2806. static uint32_t
  2807. fetch_pixel_generic_lossy_32 (bits_image_t *image,
  2808.                               int           offset,
  2809.                               int           line)
  2810. {
  2811.     uint64_t pixel64 = image->fetch_pixel_64 (image, offset, line);
  2812.     uint32_t result;
  2813.    
  2814.     pixman_contract (&result, &pixel64, 1);
  2815.  
  2816.     return result;
  2817. }
  2818.  
  2819. typedef struct
  2820. {
  2821.     pixman_format_code_t        format;
  2822.     fetch_scanline_t            fetch_scanline_32;
  2823.     fetch_scanline_t            fetch_scanline_64;
  2824.     fetch_pixel_32_t            fetch_pixel_32;
  2825.     fetch_pixel_64_t            fetch_pixel_64;
  2826.     store_scanline_t            store_scanline_32;
  2827.     store_scanline_t            store_scanline_64;
  2828. } format_info_t;
  2829.  
  2830. #define FORMAT_INFO(format)                                             \
  2831.     {                                                                   \
  2832.         PIXMAN_ ## format,                                              \
  2833.             fetch_scanline_ ## format,                                  \
  2834.             fetch_scanline_generic_64,                                  \
  2835.             fetch_pixel_ ## format, fetch_pixel_generic_64,             \
  2836.             store_scanline_ ## format, store_scanline_generic_64        \
  2837.     }
  2838.  
  2839. static const format_info_t accessors[] =
  2840. {
  2841. /* 32 bpp formats */
  2842.     FORMAT_INFO (a8r8g8b8),
  2843.     FORMAT_INFO (x8r8g8b8),
  2844.     FORMAT_INFO (a8b8g8r8),
  2845.     FORMAT_INFO (x8b8g8r8),
  2846.     FORMAT_INFO (b8g8r8a8),
  2847.     FORMAT_INFO (b8g8r8x8),
  2848.     FORMAT_INFO (x14r6g6b6),
  2849.  
  2850. /* 24bpp formats */
  2851.     FORMAT_INFO (r8g8b8),
  2852.     FORMAT_INFO (b8g8r8),
  2853.    
  2854. /* 16bpp formats */
  2855.     FORMAT_INFO (r5g6b5),
  2856.     FORMAT_INFO (b5g6r5),
  2857.    
  2858.     FORMAT_INFO (a1r5g5b5),
  2859.     FORMAT_INFO (x1r5g5b5),
  2860.     FORMAT_INFO (a1b5g5r5),
  2861.     FORMAT_INFO (x1b5g5r5),
  2862.     FORMAT_INFO (a4r4g4b4),
  2863.     FORMAT_INFO (x4r4g4b4),
  2864.     FORMAT_INFO (a4b4g4r4),
  2865.     FORMAT_INFO (x4b4g4r4),
  2866.    
  2867. /* 8bpp formats */
  2868.     FORMAT_INFO (a8),
  2869.     FORMAT_INFO (r3g3b2),
  2870.     FORMAT_INFO (b2g3r3),
  2871.     FORMAT_INFO (a2r2g2b2),
  2872.     FORMAT_INFO (a2b2g2r2),
  2873.    
  2874.     FORMAT_INFO (c8),
  2875.    
  2876. #define fetch_scanline_g8 fetch_scanline_c8
  2877. #define fetch_pixel_g8 fetch_pixel_c8
  2878.     FORMAT_INFO (g8),
  2879.    
  2880. #define fetch_scanline_x4c4 fetch_scanline_c8
  2881. #define fetch_pixel_x4c4 fetch_pixel_c8
  2882. #define store_scanline_x4c4 store_scanline_c8
  2883.     FORMAT_INFO (x4c4),
  2884.    
  2885. #define fetch_scanline_x4g4 fetch_scanline_c8
  2886. #define fetch_pixel_x4g4 fetch_pixel_c8
  2887. #define store_scanline_x4g4 store_scanline_g8
  2888.     FORMAT_INFO (x4g4),
  2889.    
  2890.     FORMAT_INFO (x4a4),
  2891.    
  2892. /* 4bpp formats */
  2893.     FORMAT_INFO (a4),
  2894.     FORMAT_INFO (r1g2b1),
  2895.     FORMAT_INFO (b1g2r1),
  2896.     FORMAT_INFO (a1r1g1b1),
  2897.     FORMAT_INFO (a1b1g1r1),
  2898.    
  2899.     FORMAT_INFO (c4),
  2900.    
  2901. #define fetch_scanline_g4 fetch_scanline_c4
  2902. #define fetch_pixel_g4 fetch_pixel_c4
  2903.     FORMAT_INFO (g4),
  2904.    
  2905. /* 1bpp formats */
  2906.     FORMAT_INFO (a1),
  2907.     FORMAT_INFO (g1),
  2908.    
  2909. /* Wide formats */
  2910.    
  2911.     { PIXMAN_a2r10g10b10,
  2912.       NULL, fetch_scanline_a2r10g10b10,
  2913.       fetch_pixel_generic_lossy_32, fetch_pixel_a2r10g10b10,
  2914.       NULL, store_scanline_a2r10g10b10 },
  2915.    
  2916.     { PIXMAN_x2r10g10b10,
  2917.       NULL, fetch_scanline_x2r10g10b10,
  2918.       fetch_pixel_generic_lossy_32, fetch_pixel_x2r10g10b10,
  2919.       NULL, store_scanline_x2r10g10b10 },
  2920.    
  2921.     { PIXMAN_a2b10g10r10,
  2922.       NULL, fetch_scanline_a2b10g10r10,
  2923.       fetch_pixel_generic_lossy_32, fetch_pixel_a2b10g10r10,
  2924.       NULL, store_scanline_a2b10g10r10 },
  2925.    
  2926.     { PIXMAN_x2b10g10r10,
  2927.       NULL, fetch_scanline_x2b10g10r10,
  2928.       fetch_pixel_generic_lossy_32, fetch_pixel_x2b10g10r10,
  2929.       NULL, store_scanline_x2b10g10r10 },
  2930.    
  2931. /* YUV formats */
  2932.     { PIXMAN_yuy2,
  2933.       fetch_scanline_yuy2, fetch_scanline_generic_64,
  2934.       fetch_pixel_yuy2, fetch_pixel_generic_64,
  2935.       NULL, NULL },
  2936.    
  2937.     { PIXMAN_yv12,
  2938.       fetch_scanline_yv12, fetch_scanline_generic_64,
  2939.       fetch_pixel_yv12, fetch_pixel_generic_64,
  2940.       NULL, NULL },
  2941.    
  2942.     { PIXMAN_null },
  2943. };
  2944.  
  2945. static void
  2946. setup_accessors (bits_image_t *image)
  2947. {
  2948.     const format_info_t *info = accessors;
  2949.    
  2950.     while (info->format != PIXMAN_null)
  2951.     {
  2952.         if (info->format == image->format)
  2953.         {
  2954.             image->fetch_scanline_32 = info->fetch_scanline_32;
  2955.             image->fetch_scanline_64 = info->fetch_scanline_64;
  2956.             image->fetch_pixel_32 = info->fetch_pixel_32;
  2957.             image->fetch_pixel_64 = info->fetch_pixel_64;
  2958.             image->store_scanline_32 = info->store_scanline_32;
  2959.             image->store_scanline_64 = info->store_scanline_64;
  2960.            
  2961.             return;
  2962.         }
  2963.        
  2964.         info++;
  2965.     }
  2966. }
  2967.  
  2968. #ifndef PIXMAN_FB_ACCESSORS
  2969. void
  2970. _pixman_bits_image_setup_accessors_accessors (bits_image_t *image);
  2971.  
  2972. void
  2973. _pixman_bits_image_setup_accessors (bits_image_t *image)
  2974. {
  2975.     if (image->read_func || image->write_func)
  2976.         _pixman_bits_image_setup_accessors_accessors (image);
  2977.     else
  2978.         setup_accessors (image);
  2979. }
  2980.  
  2981. #else
  2982.  
  2983. void
  2984. _pixman_bits_image_setup_accessors_accessors (bits_image_t *image)
  2985. {
  2986.     setup_accessors (image);
  2987. }
  2988.  
  2989. #endif
  2990.