1#ifndef __XCB_PIXEL_H__
2#define __XCB_PIXEL_H__
3
4/* Copyright (C) 2007 Bart Massey
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the names of the authors or their
24 * institutions shall not be used in advertising or otherwise to promote the
25 * sale, use or other dealings in this Software without prior written
26 * authorization from the authors.
27 */
28
29#include <inttypes.h>
30#include <X11/Xfuncproto.h>
31#ifndef BUILD
32#include <xcb/xcb_bitops.h>
33#include <xcb/xcb_image.h>
34#endif
35
36/**
37 * XCB Image fast pixel ops.
38 *
39 * Fast inline versions of xcb_image_get_pixel() and
40 * xcb_image_put_pixel() for various common cases.
41 * The naming convention is xcb_image_put_pixel_FUB()
42 * where F is the format and is either XY for bitmaps
43 * or Z for pixmaps, U is the bitmap unit size or pixmap
44 * bits-per-pixel, and B is the endianness (if needed)
45 * and is either M for most-significant-first or L for
46 * least-significant-first.  Note that no checking
47 * is done on the arguments to these routines---caller beware.
48 * Also note that the pixel type is chosen to be appropriate
49 * to the unit; bitmaps use int and pixmaps use the appropriate
50 * size of unsigned.
51 * @ingroup xcb__image_t
52 */
53
54_X_INLINE static void
55xcb_image_put_pixel_XY32M (xcb_image_t *image,
56			  uint32_t x,
57			  uint32_t y,
58			  int pixel)
59{
60  uint32_t   unit = (x >> 3) & ~xcb_mask(2);
61  uint32_t   byte = xcb_mask(2) - ((x >> 3) & xcb_mask(2));
62  uint32_t   bit = xcb_mask(3) - (x & xcb_mask(3));
63  uint8_t    m = 1 << bit;
64  uint8_t    p = pixel << bit;
65  uint8_t *  bp = image->data + (y * image->stride) + (unit | byte);
66  *bp = (*bp & ~m) | p;
67}
68
69_X_INLINE static void
70xcb_image_put_pixel_XY32L (xcb_image_t *image,
71			  uint32_t x,
72			  uint32_t y,
73			  int pixel)
74{
75  uint32_t   bit = x & xcb_mask(3);
76  uint8_t    m = 1 << bit;
77  uint8_t    p = pixel << bit;
78  uint8_t *  bp = image->data + (y * image->stride) + (x >> 3);
79  *bp = (*bp & ~m) | p;
80}
81
82_X_INLINE static int
83xcb_image_get_pixel_XY32M (xcb_image_t *image,
84			  uint32_t x,
85			  uint32_t y)
86{
87  uint32_t   unit = (x >> 3) & ~xcb_mask(2);
88  uint32_t   byte = xcb_mask(2) - ((x >> 3) & xcb_mask(2));
89  uint32_t   bit = xcb_mask(3) - (x & xcb_mask(3));
90  uint8_t *  bp = image->data + (y * image->stride) + (unit | byte);
91  return (*bp >> bit) & 1;
92}
93
94_X_INLINE static int
95xcb_image_get_pixel_XY32L (xcb_image_t *image,
96			  uint32_t x,
97			  uint32_t y)
98{
99  uint32_t   bit = x & xcb_mask(3);
100  uint8_t *  bp = image->data + (y * image->stride) + (x >> 3);
101  return (*bp >> bit) & 1;
102}
103
104_X_INLINE static void
105xcb_image_put_pixel_Z8 (xcb_image_t *image,
106			uint32_t x,
107			uint32_t y,
108			uint8_t pixel)
109{
110  image->data[x + y * image->stride] = pixel;
111}
112
113_X_INLINE static uint8_t
114xcb_image_get_pixel_Z8 (xcb_image_t *image,
115			uint32_t x,
116			uint32_t y)
117{
118  return image->data[x + y * image->stride];
119}
120
121_X_INLINE static void
122xcb_image_put_pixel_Z32M (xcb_image_t *image,
123			  uint32_t x,
124			  uint32_t y,
125			  uint32_t pixel)
126{
127  uint8_t *  row = image->data + (y * image->stride);
128  row[x << 2] = pixel >> 24;
129  row[(x << 2) + 1] = pixel >> 16;
130  row[(x << 2) + 2] = pixel >> 8;
131  row[(x << 2) + 3] = pixel;
132}
133
134_X_INLINE static void
135xcb_image_put_pixel_Z32L (xcb_image_t *image,
136			  uint32_t x,
137			  uint32_t y,
138			  uint32_t pixel)
139{
140  uint8_t *  row = image->data + (y * image->stride);
141  row[x << 2] = pixel;
142  row[(x << 2) + 1] = pixel >> 8;
143  row[(x << 2) + 2] = pixel >> 16;
144  row[(x << 2) + 3] = pixel >> 24;
145}
146
147_X_INLINE static uint32_t
148xcb_image_get_pixel_Z32M (xcb_image_t *image,
149			  uint32_t x,
150			  uint32_t y)
151{
152  uint8_t *  row = image->data + (y * image->stride);
153  uint32_t    pixel = row[x << 2] << 24;
154  pixel |= row[(x << 2) + 1] << 16;
155  pixel |= row[(x << 2) + 2] << 8;
156  return pixel | row[(x << 2) + 3];
157}
158
159_X_INLINE static uint32_t
160xcb_image_get_pixel_Z32L (xcb_image_t *image,
161			  uint32_t x,
162			  uint32_t y)
163{
164  uint8_t *  row = image->data + (y * image->stride);
165  uint32_t    pixel = row[x << 2];
166  pixel |= row[(x << 2) + 1] << 8;
167  pixel |= row[(x << 2) + 2] << 16;
168  return pixel | row[(x << 2) + 3] << 24;
169}
170
171#endif /* __XCB_PIXEL_H__ */
172