1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Decky
|
---|
3 | * Copyright (c) 2011 Petr Koupy
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup softrend
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file Pixel conversion and mask functions.
|
---|
35 | *
|
---|
36 | * These functions write an ARGB pixel value to a memory location
|
---|
37 | * in a predefined format. The naming convention corresponds to
|
---|
38 | * the names of the visuals and the format created by these functions.
|
---|
39 | * The functions use the so called network bit order (i.e. big endian)
|
---|
40 | * with respect to their names.
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <byteorder.h>
|
---|
44 | #include "pixconv.h"
|
---|
45 |
|
---|
46 | void pixel2argb_8888(void *dst, pixel_t pix)
|
---|
47 | {
|
---|
48 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
49 | (ALPHA(pix) << 24) | (RED(pix) << 16) | (GREEN(pix) << 8) | (BLUE(pix)));
|
---|
50 | }
|
---|
51 |
|
---|
52 | void pixel2abgr_8888(void *dst, pixel_t pix)
|
---|
53 | {
|
---|
54 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
55 | (ALPHA(pix) << 24) | (BLUE(pix) << 16) | (GREEN(pix) << 8) | (RED(pix)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | void pixel2rgba_8888(void *dst, pixel_t pix)
|
---|
59 | {
|
---|
60 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
61 | (RED(pix) << 24) | (GREEN(pix) << 16) | (BLUE(pix) << 8) | (ALPHA(pix)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | void pixel2bgra_8888(void *dst, pixel_t pix)
|
---|
65 | {
|
---|
66 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
67 | (BLUE(pix) << 24) | (GREEN(pix) << 16) | (RED(pix) << 8) | (ALPHA(pix)));
|
---|
68 | }
|
---|
69 |
|
---|
70 | void pixel2rgb_0888(void *dst, pixel_t pix)
|
---|
71 | {
|
---|
72 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
73 | (RED(pix) << 16) | (GREEN(pix) << 8) | (BLUE(pix)));
|
---|
74 | }
|
---|
75 |
|
---|
76 | void pixel2bgr_0888(void *dst, pixel_t pix)
|
---|
77 | {
|
---|
78 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
79 | (BLUE(pix) << 16) | (GREEN(pix) << 8) | (RED(pix)));
|
---|
80 | }
|
---|
81 |
|
---|
82 | void pixel2rgb_8880(void *dst, pixel_t pix)
|
---|
83 | {
|
---|
84 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
85 | (RED(pix) << 24) | (GREEN(pix) << 16) | (BLUE(pix) << 8));
|
---|
86 | }
|
---|
87 |
|
---|
88 | void pixel2bgr_8880(void *dst, pixel_t pix)
|
---|
89 | {
|
---|
90 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
91 | (BLUE(pix) << 24) | (GREEN(pix) << 16) | (RED(pix) << 8));
|
---|
92 | }
|
---|
93 |
|
---|
94 | void pixel2rgb_888(void *dst, pixel_t pix)
|
---|
95 | {
|
---|
96 | ((uint8_t *) dst)[0] = RED(pix);
|
---|
97 | ((uint8_t *) dst)[1] = GREEN(pix);
|
---|
98 | ((uint8_t *) dst)[2] = BLUE(pix);
|
---|
99 | }
|
---|
100 |
|
---|
101 | void pixel2bgr_888(void *dst, pixel_t pix)
|
---|
102 | {
|
---|
103 | ((uint8_t *) dst)[0] = BLUE(pix);
|
---|
104 | ((uint8_t *) dst)[1] = GREEN(pix);
|
---|
105 | ((uint8_t *) dst)[2] = RED(pix);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void pixel2rgb_555_be(void *dst, pixel_t pix)
|
---|
109 | {
|
---|
110 | *((uint16_t *) dst) = host2uint16_t_be(
|
---|
111 | (NARROW(RED(pix), 5) << 10) | (NARROW(GREEN(pix), 5) << 5) | (NARROW(BLUE(pix), 5)));
|
---|
112 | }
|
---|
113 |
|
---|
114 | void pixel2rgb_555_le(void *dst, pixel_t pix)
|
---|
115 | {
|
---|
116 | *((uint16_t *) dst) = host2uint16_t_le(
|
---|
117 | (NARROW(RED(pix), 5) << 10) | (NARROW(GREEN(pix), 5) << 5) | (NARROW(BLUE(pix), 5)));
|
---|
118 | }
|
---|
119 |
|
---|
120 | void pixel2rgb_565_be(void *dst, pixel_t pix)
|
---|
121 | {
|
---|
122 | *((uint16_t *) dst) = host2uint16_t_be(
|
---|
123 | (NARROW(RED(pix), 5) << 11) | (NARROW(GREEN(pix), 6) << 5) | (NARROW(BLUE(pix), 5)));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void pixel2rgb_565_le(void *dst, pixel_t pix)
|
---|
127 | {
|
---|
128 | *((uint16_t *) dst) = host2uint16_t_le(
|
---|
129 | (NARROW(RED(pix), 5) << 11) | (NARROW(GREEN(pix), 6) << 5) | (NARROW(BLUE(pix), 5)));
|
---|
130 | }
|
---|
131 |
|
---|
132 | void pixel2bgr_323(void *dst, pixel_t pix)
|
---|
133 | {
|
---|
134 | *((uint8_t *) dst) =
|
---|
135 | ~((NARROW(RED(pix), 3) << 5) | (NARROW(GREEN(pix), 2) << 3) | NARROW(BLUE(pix), 3));
|
---|
136 | }
|
---|
137 |
|
---|
138 | void pixel2gray_8(void *dst, pixel_t pix)
|
---|
139 | {
|
---|
140 | uint32_t red = RED(pix) * 5034375;
|
---|
141 | uint32_t green = GREEN(pix) * 9886846;
|
---|
142 | uint32_t blue = BLUE(pix) * 1920103;
|
---|
143 |
|
---|
144 | *((uint8_t *) dst) = (red + green + blue) >> 24;
|
---|
145 | }
|
---|
146 |
|
---|
147 | void visual_mask_8888(void *dst, bool mask)
|
---|
148 | {
|
---|
149 | pixel2abgr_8888(dst, mask ? 0xffffffff : 0);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void visual_mask_0888(void *dst, bool mask)
|
---|
153 | {
|
---|
154 | pixel2bgr_0888(dst, mask ? 0xffffffff : 0);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void visual_mask_8880(void *dst, bool mask)
|
---|
158 | {
|
---|
159 | pixel2bgr_8880(dst, mask ? 0xffffffff : 0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | void visual_mask_888(void *dst, bool mask)
|
---|
163 | {
|
---|
164 | pixel2bgr_888(dst, mask ? 0xffffffff : 0);
|
---|
165 | }
|
---|
166 |
|
---|
167 | void visual_mask_555(void *dst, bool mask)
|
---|
168 | {
|
---|
169 | pixel2rgb_555_be(dst, mask ? 0xffffffff : 0);
|
---|
170 | }
|
---|
171 |
|
---|
172 | void visual_mask_565(void *dst, bool mask)
|
---|
173 | {
|
---|
174 | pixel2rgb_565_be(dst, mask ? 0xffffffff : 0);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void visual_mask_323(void *dst, bool mask)
|
---|
178 | {
|
---|
179 | pixel2bgr_323(dst, mask ? 0x0 : ~0x0);
|
---|
180 | }
|
---|
181 |
|
---|
182 | void visual_mask_8(void *dst, bool mask)
|
---|
183 | {
|
---|
184 | pixel2gray_8(dst, mask ? 0xffffffff : 0);
|
---|
185 | }
|
---|
186 |
|
---|
187 | pixel_t argb_8888_2pixel(void *src)
|
---|
188 | {
|
---|
189 | return (uint32_t_be2host(*((uint32_t *) src)));
|
---|
190 | }
|
---|
191 |
|
---|
192 | pixel_t abgr_8888_2pixel(void *src)
|
---|
193 | {
|
---|
194 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
195 | return ((val & 0xff000000) | ((val & 0xff0000) >> 16) | (val & 0xff00) | ((val & 0xff) << 16));
|
---|
196 | }
|
---|
197 |
|
---|
198 | pixel_t rgba_8888_2pixel(void *src)
|
---|
199 | {
|
---|
200 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
201 | return ((val << 24) | (val >> 8));
|
---|
202 | }
|
---|
203 |
|
---|
204 | pixel_t bgra_8888_2pixel(void *src)
|
---|
205 | {
|
---|
206 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
207 | return ((val >> 24) | ((val & 0xff0000) >> 8) | ((val & 0xff00) << 8) | (val << 24));
|
---|
208 | }
|
---|
209 |
|
---|
210 | pixel_t rgb_0888_2pixel(void *src)
|
---|
211 | {
|
---|
212 | return (0xff000000 | (uint32_t_be2host(*((uint32_t *) src)) & 0xffffff));
|
---|
213 | }
|
---|
214 |
|
---|
215 | pixel_t bgr_0888_2pixel(void *src)
|
---|
216 | {
|
---|
217 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
218 | return (0xff000000 | ((val & 0xff0000) >> 16) | (val & 0xff00) | ((val & 0xff) << 16));
|
---|
219 | }
|
---|
220 |
|
---|
221 | pixel_t rgb_8880_2pixel(void *src)
|
---|
222 | {
|
---|
223 | return (0xff000000 | (uint32_t_be2host(*((uint32_t *) src)) >> 8));
|
---|
224 | }
|
---|
225 |
|
---|
226 | pixel_t bgr_8880_2pixel(void *src)
|
---|
227 | {
|
---|
228 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
229 | return (0xff000000 | (val >> 24) | ((val & 0xff0000) >> 8) | ((val & 0xff00) << 8));
|
---|
230 | }
|
---|
231 |
|
---|
232 | pixel_t rgb_888_2pixel(void *src)
|
---|
233 | {
|
---|
234 | uint8_t red = ((uint8_t *) src)[0];
|
---|
235 | uint8_t green = ((uint8_t *) src)[1];
|
---|
236 | uint8_t blue = ((uint8_t *) src)[2];
|
---|
237 |
|
---|
238 | return (0xff000000 | (red << 16) | (green << 8) | (blue));
|
---|
239 | }
|
---|
240 |
|
---|
241 | pixel_t bgr_888_2pixel(void *src)
|
---|
242 | {
|
---|
243 | uint8_t blue = ((uint8_t *) src)[0];
|
---|
244 | uint8_t green = ((uint8_t *) src)[1];
|
---|
245 | uint8_t red = ((uint8_t *) src)[2];
|
---|
246 |
|
---|
247 | return (0xff000000 | (red << 16) | (green << 8) | (blue));
|
---|
248 | }
|
---|
249 |
|
---|
250 | pixel_t rgb_555_be_2pixel(void *src)
|
---|
251 | {
|
---|
252 | uint16_t val = uint16_t_be2host(*((uint16_t *) src));
|
---|
253 | return (0xff000000 | ((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
|
---|
254 | }
|
---|
255 |
|
---|
256 | pixel_t rgb_555_le_2pixel(void *src)
|
---|
257 | {
|
---|
258 | uint16_t val = uint16_t_le2host(*((uint16_t *) src));
|
---|
259 | return (0xff000000 | ((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
|
---|
260 | }
|
---|
261 |
|
---|
262 | pixel_t rgb_565_be_2pixel(void *src)
|
---|
263 | {
|
---|
264 | uint16_t val = uint16_t_be2host(*((uint16_t *) src));
|
---|
265 | return (0xff000000 | ((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
|
---|
266 | }
|
---|
267 |
|
---|
268 | pixel_t rgb_565_le_2pixel(void *src)
|
---|
269 | {
|
---|
270 | uint16_t val = uint16_t_le2host(*((uint16_t *) src));
|
---|
271 | return (0xff000000 | ((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
|
---|
272 | }
|
---|
273 |
|
---|
274 | pixel_t bgr_323_2pixel(void *src)
|
---|
275 | {
|
---|
276 | uint8_t val = ~(*((uint8_t *) src));
|
---|
277 | return (0xff000000 | ((val & 0xe0) << 16) | ((val & 0x18) << 11) | ((val & 0x7) << 5));
|
---|
278 | }
|
---|
279 |
|
---|
280 | pixel_t gray_8_2pixel(void *src)
|
---|
281 | {
|
---|
282 | uint8_t val = *((uint8_t *) src);
|
---|
283 | return (0xff000000 | (val << 16) | (val << 8) | (val));
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** @}
|
---|
287 | */
|
---|