37
38
#include <byteorder.h>
53
/** RGB conversion and mask functions.
55
* These functions write an RGB pixel value to a memory location
56
* in a predefined format. The naming convention corresponds to
57
* the names of the visuals and the format created by these functions.
58
* The functions use the so called network bit order (i.e. big endian)
59
* with respect to their names.
62
#define RED(pixel, bits) (((pixel) >> (8 + 8 + 8 - (bits))) & ((1 << (bits)) - 1))
63
#define GREEN(pixel, bits) (((pixel) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
64
#define BLUE(pixel, bits) (((pixel) >> (8 - (bits))) & ((1 << (bits)) - 1))
66
void pixel2rgb_0888(void *dst, pixel_t pixel)
68
*((uint32_t *) dst) = host2uint32_t_be(
69
(RED(pixel, 8) << 16) | (GREEN(pixel, 8) << 8) | (BLUE(pixel, 8)));
72
void pixel2bgr_0888(void *dst, pixel_t pixel)
74
*((uint32_t *) dst) = host2uint32_t_be(
75
(BLUE(pixel, 8) << 16) | (GREEN(pixel, 8) << 8) | (RED(pixel, 8)));
78
void pixel2rgb_8880(void *dst, pixel_t pixel)
80
*((uint32_t *) dst) = host2uint32_t_be(
81
(RED(pixel, 8) << 24) | (GREEN(pixel, 8) << 16) | (BLUE(pixel, 8) << 8));
84
void pixel2bgr_8880(void *dst, pixel_t pixel)
86
*((uint32_t *) dst) = host2uint32_t_be(
87
(BLUE(pixel, 8) << 24) | (GREEN(pixel, 8) << 16) | (RED(pixel, 8) << 8));
90
void pixel2rgb_888(void *dst, pixel_t pixel)
92
((uint8_t *) dst)[0] = RED(pixel, 8);
93
((uint8_t *) dst)[1] = GREEN(pixel, 8);
94
((uint8_t *) dst)[2] = BLUE(pixel, 8);
97
void pixel2bgr_888(void *dst, pixel_t pixel)
99
((uint8_t *) dst)[0] = BLUE(pixel, 8);
100
((uint8_t *) dst)[1] = GREEN(pixel, 8);
101
((uint8_t *) dst)[2] = RED(pixel, 8);
104
void pixel2rgb_555_be(void *dst, pixel_t pixel)
106
*((uint16_t *) dst) = host2uint16_t_be(
107
(RED(pixel, 5) << 10) | (GREEN(pixel, 5) << 5) | (BLUE(pixel, 5)));
110
void pixel2rgb_555_le(void *dst, pixel_t pixel)
112
*((uint16_t *) dst) = host2uint16_t_le(
113
(RED(pixel, 5) << 10) | (GREEN(pixel, 5) << 5) | (BLUE(pixel, 5)));
116
void pixel2rgb_565_be(void *dst, pixel_t pixel)
118
*((uint16_t *) dst) = host2uint16_t_be(
119
(RED(pixel, 5) << 11) | (GREEN(pixel, 6) << 5) | (BLUE(pixel, 5)));
122
void pixel2rgb_565_le(void *dst, pixel_t pixel)
124
*((uint16_t *) dst) = host2uint16_t_le(
125
(RED(pixel, 5) << 11) | (GREEN(pixel, 6) << 5) | (BLUE(pixel, 5)));
128
void pixel2bgr_323(void *dst, pixel_t pixel)
131
~((RED(pixel, 3) << 5) | (GREEN(pixel, 2) << 3) | BLUE(pixel, 3));
134
void pixel2gray_8(void *dst, pixel_t pixel)
136
uint32_t red = RED(pixel, 8) * 5034375;
137
uint32_t green = GREEN(pixel, 8) * 9886846;
138
uint32_t blue = BLUE(pixel, 8) * 1920103;
140
*((uint8_t *) dst) = (red + green + blue) >> 24;
143
void visual_mask_0888(void *dst, bool mask)
145
pixel2bgr_0888(dst, mask ? 0xffffff : 0);
148
void visual_mask_8880(void *dst, bool mask)
150
pixel2bgr_8880(dst, mask ? 0xffffff : 0);
153
void visual_mask_888(void *dst, bool mask)
155
pixel2bgr_888(dst, mask ? 0xffffff : 0);
158
void visual_mask_555(void *dst, bool mask)
160
pixel2rgb_555_be(dst, mask ? 0xffffff : 0);
163
void visual_mask_565(void *dst, bool mask)
165
pixel2rgb_565_be(dst, mask ? 0xffffff : 0);
168
void visual_mask_323(void *dst, bool mask)
170
pixel2bgr_323(dst, mask ? 0x0 : ~0x0);
173
void visual_mask_8(void *dst, bool mask)
175
pixel2gray_8(dst, mask ? 0xffffff : 0);
178
pixel_t rgb_0888_2pixel(void *src)
180
return (uint32_t_be2host(*((uint32_t *) src)) & 0xffffff);
183
pixel_t bgr_0888_2pixel(void *src)
185
uint32_t val = uint32_t_be2host(*((uint32_t *) src));
186
return (((val & 0xff0000) >> 16) | (val & 0xff00) | ((val & 0xff) << 16));
189
pixel_t rgb_8880_2pixel(void *src)
191
return (uint32_t_be2host(*((uint32_t *) src)) >> 8);
194
pixel_t bgr_8880_2pixel(void *src)
196
uint32_t val = uint32_t_be2host(*((uint32_t *) src));
197
return (((val & 0xff000000) >> 24) | ((val & 0xff0000) >> 8) | ((val & 0xff00) << 8));
200
pixel_t rgb_888_2pixel(void *src)
202
uint8_t red = ((uint8_t *) src)[0];
203
uint8_t green = ((uint8_t *) src)[1];
204
uint8_t blue = ((uint8_t *) src)[2];
206
return ((red << 16) | (green << 8) | (blue));
209
pixel_t bgr_888_2pixel(void *src)
211
uint8_t blue = ((uint8_t *) src)[0];
212
uint8_t green = ((uint8_t *) src)[1];
213
uint8_t red = ((uint8_t *) src)[2];
215
return ((red << 16) | (green << 8) | (blue));
218
pixel_t rgb_555_be_2pixel(void *src)
220
uint16_t val = uint16_t_be2host(*((uint16_t *) src));
221
return (((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
224
pixel_t rgb_555_le_2pixel(void *src)
226
uint16_t val = uint16_t_le2host(*((uint16_t *) src));
227
return (((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
230
pixel_t rgb_565_be_2pixel(void *src)
232
uint16_t val = uint16_t_be2host(*((uint16_t *) src));
233
return (((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
236
pixel_t rgb_565_le_2pixel(void *src)
238
uint16_t val = uint16_t_le2host(*((uint16_t *) src));
239
return (((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
242
pixel_t bgr_323_2pixel(void *src)
244
uint8_t val = ~(*((uint8_t *) src));
245
return (((val & 0xe0) << 16) | ((val & 0x18) << 11) | ((val & 0x7) << 5));
248
pixel_t gray_8_2pixel(void *src)
250
uint8_t val = *((uint8_t *) src);
251
return ((val << 16) | (val << 8) | (val));
255
45
uint8_t id_length;
374
void imgmap_put_pixel(imgmap_t *imgmap, sysarg_t x, sysarg_t y, pixel_t pixel)
376
if ((x >= imgmap->width) || (y >= imgmap->height))
379
size_t offset = y * imgmap->width + x;
381
switch (imgmap->visual) {
382
case VISUAL_RGB_0_8_8_8:
383
pixel2rgb_0888(((uint32_t *) imgmap->data) + offset, pixel);
390
pixel_t imgmap_get_pixel(imgmap_t *imgmap, sysarg_t x, sysarg_t y)
392
if ((x >= imgmap->width) || (y >= imgmap->height))
395
size_t offset = y * imgmap->width + x;
397
switch (imgmap->visual) {
398
case VISUAL_RGB_0_8_8_8:
399
return rgb_0888_2pixel(((uint32_t *) imgmap->data) + offset);
405
imgmap_t *imgmap_create(sysarg_t width, sysarg_t height, visual_t visual,
406
imgmap_flags_t flags)
411
case VISUAL_RGB_0_8_8_8:
412
bsize = (width * height) << 2;
418
size_t size = sizeof(imgmap_t) + bsize;
421
if ((flags & IMGMAP_FLAG_SHARED) == IMGMAP_FLAG_SHARED) {
422
imgmap = (imgmap_t *) as_area_create(AS_AREA_ANY, size,
423
AS_AREA_READ | AS_AREA_WRITE);
424
if (imgmap == AS_MAP_FAILED)
427
imgmap = (imgmap_t *) malloc(size);
433
imgmap->flags = flags;
434
imgmap->width = width;
435
imgmap->height = height;
436
imgmap->visual = visual;
438
memset(imgmap->data, 0, bsize);
443
163
/** Decode Truevision TGA format
445
* Decode Truevision TGA format and create an image map
165
* Decode Truevision TGA format and create a surface
446
166
* from it. The supported variants of TGA are currently
447
167
* limited to uncompressed 24 bit true-color images without
450
170
* @param[in] data Memory representation of TGA.
451
171
* @param[in] size Size of the representation (in bytes).
452
* @param[in] flags Image map creation flags.
172
* @param[in] flags Surface creation flags.
454
* @return Newly allocated image map.
174
* @return Newly allocated surface with the decoded content.
455
175
* @return NULL on error or unsupported format.
458
imgmap_t *imgmap_decode_tga(void *data, size_t size, imgmap_flags_t flags)
177
surface_t *decode_tga(void *data, size_t size, surface_flags_t flags)
461
180
if (!decode_tga_header(data, size, &tga))