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