[7c014d1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup imgmap
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <byteorder.h>
|
---|
| 38 | #include <align.h>
|
---|
| 39 | #include <bool.h>
|
---|
| 40 | #include <mem.h>
|
---|
| 41 | #include <as.h>
|
---|
| 42 | #include "imgmap.h"
|
---|
| 43 |
|
---|
| 44 | struct imgmap {
|
---|
| 45 | size_t size;
|
---|
| 46 | imgmap_flags_t flags;
|
---|
| 47 | sysarg_t width;
|
---|
| 48 | sysarg_t height;
|
---|
| 49 | visual_t visual;
|
---|
| 50 | uint8_t data[];
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | /** RGB conversion and mask functions.
|
---|
| 54 | *
|
---|
| 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.
|
---|
| 60 | */
|
---|
| 61 |
|
---|
| 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))
|
---|
| 65 |
|
---|
| 66 | void pixel2rgb_0888(void *dst, pixel_t pixel)
|
---|
| 67 | {
|
---|
| 68 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
| 69 | (RED(pixel, 8) << 16) | (GREEN(pixel, 8) << 8) | (BLUE(pixel, 8)));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void pixel2bgr_0888(void *dst, pixel_t pixel)
|
---|
| 73 | {
|
---|
| 74 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
| 75 | (BLUE(pixel, 8) << 16) | (GREEN(pixel, 8) << 8) | (RED(pixel, 8)));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void pixel2rgb_8880(void *dst, pixel_t pixel)
|
---|
| 79 | {
|
---|
| 80 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
| 81 | (RED(pixel, 8) << 24) | (GREEN(pixel, 8) << 16) | (BLUE(pixel, 8) << 8));
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void pixel2bgr_8880(void *dst, pixel_t pixel)
|
---|
| 85 | {
|
---|
| 86 | *((uint32_t *) dst) = host2uint32_t_be(
|
---|
| 87 | (BLUE(pixel, 8) << 24) | (GREEN(pixel, 8) << 16) | (RED(pixel, 8) << 8));
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void pixel2rgb_888(void *dst, pixel_t pixel)
|
---|
| 91 | {
|
---|
| 92 | ((uint8_t *) dst)[0] = RED(pixel, 8);
|
---|
| 93 | ((uint8_t *) dst)[1] = GREEN(pixel, 8);
|
---|
| 94 | ((uint8_t *) dst)[2] = BLUE(pixel, 8);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void pixel2bgr_888(void *dst, pixel_t pixel)
|
---|
| 98 | {
|
---|
| 99 | ((uint8_t *) dst)[0] = BLUE(pixel, 8);
|
---|
| 100 | ((uint8_t *) dst)[1] = GREEN(pixel, 8);
|
---|
| 101 | ((uint8_t *) dst)[2] = RED(pixel, 8);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void pixel2rgb_555_be(void *dst, pixel_t pixel)
|
---|
| 105 | {
|
---|
| 106 | *((uint16_t *) dst) = host2uint16_t_be(
|
---|
| 107 | (RED(pixel, 5) << 10) | (GREEN(pixel, 5) << 5) | (BLUE(pixel, 5)));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void pixel2rgb_555_le(void *dst, pixel_t pixel)
|
---|
| 111 | {
|
---|
| 112 | *((uint16_t *) dst) = host2uint16_t_le(
|
---|
| 113 | (RED(pixel, 5) << 10) | (GREEN(pixel, 5) << 5) | (BLUE(pixel, 5)));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void pixel2rgb_565_be(void *dst, pixel_t pixel)
|
---|
| 117 | {
|
---|
| 118 | *((uint16_t *) dst) = host2uint16_t_be(
|
---|
| 119 | (RED(pixel, 5) << 11) | (GREEN(pixel, 6) << 5) | (BLUE(pixel, 5)));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void pixel2rgb_565_le(void *dst, pixel_t pixel)
|
---|
| 123 | {
|
---|
| 124 | *((uint16_t *) dst) = host2uint16_t_le(
|
---|
| 125 | (RED(pixel, 5) << 11) | (GREEN(pixel, 6) << 5) | (BLUE(pixel, 5)));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void pixel2bgr_323(void *dst, pixel_t pixel)
|
---|
| 129 | {
|
---|
| 130 | *((uint8_t *) dst) =
|
---|
| 131 | ~((RED(pixel, 3) << 5) | (GREEN(pixel, 2) << 3) | BLUE(pixel, 3));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void pixel2gray_8(void *dst, pixel_t pixel)
|
---|
| 135 | {
|
---|
| 136 | uint32_t red = RED(pixel, 8) * 5034375;
|
---|
| 137 | uint32_t green = GREEN(pixel, 8) * 9886846;
|
---|
| 138 | uint32_t blue = BLUE(pixel, 8) * 1920103;
|
---|
| 139 |
|
---|
| 140 | *((uint8_t *) dst) = (red + green + blue) >> 24;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | void visual_mask_0888(void *dst, bool mask)
|
---|
| 144 | {
|
---|
| 145 | pixel2bgr_0888(dst, mask ? 0xffffff : 0);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void visual_mask_8880(void *dst, bool mask)
|
---|
| 149 | {
|
---|
| 150 | pixel2bgr_8880(dst, mask ? 0xffffff : 0);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | void visual_mask_888(void *dst, bool mask)
|
---|
| 154 | {
|
---|
| 155 | pixel2bgr_888(dst, mask ? 0xffffff : 0);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void visual_mask_555(void *dst, bool mask)
|
---|
| 159 | {
|
---|
| 160 | pixel2rgb_555_be(dst, mask ? 0xffffff : 0);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void visual_mask_565(void *dst, bool mask)
|
---|
| 164 | {
|
---|
| 165 | pixel2rgb_565_be(dst, mask ? 0xffffff : 0);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | void visual_mask_323(void *dst, bool mask)
|
---|
| 169 | {
|
---|
| 170 | pixel2bgr_323(dst, mask ? 0x0 : ~0x0);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void visual_mask_8(void *dst, bool mask)
|
---|
| 174 | {
|
---|
| 175 | pixel2gray_8(dst, mask ? 0xffffff : 0);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | pixel_t rgb_0888_2pixel(void *src)
|
---|
| 179 | {
|
---|
| 180 | return (uint32_t_be2host(*((uint32_t *) src)) & 0xffffff);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | pixel_t bgr_0888_2pixel(void *src)
|
---|
| 184 | {
|
---|
| 185 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
| 186 | return (((val & 0xff0000) >> 16) | (val & 0xff00) | ((val & 0xff) << 16));
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | pixel_t rgb_8880_2pixel(void *src)
|
---|
| 190 | {
|
---|
| 191 | return (uint32_t_be2host(*((uint32_t *) src)) >> 8);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | pixel_t bgr_8880_2pixel(void *src)
|
---|
| 195 | {
|
---|
| 196 | uint32_t val = uint32_t_be2host(*((uint32_t *) src));
|
---|
| 197 | return (((val & 0xff000000) >> 24) | ((val & 0xff0000) >> 8) | ((val & 0xff00) << 8));
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | pixel_t rgb_888_2pixel(void *src)
|
---|
| 201 | {
|
---|
| 202 | uint8_t red = ((uint8_t *) src)[0];
|
---|
| 203 | uint8_t green = ((uint8_t *) src)[1];
|
---|
| 204 | uint8_t blue = ((uint8_t *) src)[2];
|
---|
| 205 |
|
---|
| 206 | return ((red << 16) | (green << 8) | (blue));
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | pixel_t bgr_888_2pixel(void *src)
|
---|
| 210 | {
|
---|
| 211 | uint8_t blue = ((uint8_t *) src)[0];
|
---|
| 212 | uint8_t green = ((uint8_t *) src)[1];
|
---|
| 213 | uint8_t red = ((uint8_t *) src)[2];
|
---|
| 214 |
|
---|
| 215 | return ((red << 16) | (green << 8) | (blue));
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | pixel_t rgb_555_be_2pixel(void *src)
|
---|
| 219 | {
|
---|
| 220 | uint16_t val = uint16_t_be2host(*((uint16_t *) src));
|
---|
| 221 | return (((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | pixel_t rgb_555_le_2pixel(void *src)
|
---|
| 225 | {
|
---|
| 226 | uint16_t val = uint16_t_le2host(*((uint16_t *) src));
|
---|
| 227 | return (((val & 0x7c00) << 9) | ((val & 0x3e0) << 6) | ((val & 0x1f) << 3));
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | pixel_t rgb_565_be_2pixel(void *src)
|
---|
| 231 | {
|
---|
| 232 | uint16_t val = uint16_t_be2host(*((uint16_t *) src));
|
---|
| 233 | return (((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | pixel_t rgb_565_le_2pixel(void *src)
|
---|
| 237 | {
|
---|
| 238 | uint16_t val = uint16_t_le2host(*((uint16_t *) src));
|
---|
| 239 | return (((val & 0xf800) << 8) | ((val & 0x7e0) << 5) | ((val & 0x1f) << 3));
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | pixel_t bgr_323_2pixel(void *src)
|
---|
| 243 | {
|
---|
| 244 | uint8_t val = ~(*((uint8_t *) src));
|
---|
| 245 | return (((val & 0xe0) << 16) | ((val & 0x18) << 11) | ((val & 0x7) << 5));
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | pixel_t gray_8_2pixel(void *src)
|
---|
| 249 | {
|
---|
| 250 | uint8_t val = *((uint8_t *) src);
|
---|
| 251 | return ((val << 16) | (val << 8) | (val));
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | typedef struct {
|
---|
| 255 | uint8_t id_length;
|
---|
| 256 | uint8_t cmap_type;
|
---|
| 257 | uint8_t img_type;
|
---|
| 258 |
|
---|
| 259 | uint16_t cmap_first_entry;
|
---|
| 260 | uint16_t cmap_entries;
|
---|
| 261 | uint8_t cmap_bpp;
|
---|
| 262 |
|
---|
| 263 | uint16_t startx;
|
---|
| 264 | uint16_t starty;
|
---|
| 265 | uint16_t width;
|
---|
| 266 | uint16_t height;
|
---|
| 267 | uint8_t img_bpp;
|
---|
| 268 | uint8_t img_descr;
|
---|
| 269 | } __attribute__((packed)) tga_header_t;
|
---|
| 270 |
|
---|
| 271 | typedef enum {
|
---|
| 272 | CMAP_NOT_PRESENT = 0,
|
---|
| 273 | CMAP_PRESENT = 1,
|
---|
| 274 | CMAP_RESERVED_START = 2,
|
---|
| 275 | CMAP_PRIVATE_START = 128
|
---|
| 276 | } cmap_type_t;
|
---|
| 277 |
|
---|
| 278 | typedef enum {
|
---|
| 279 | IMG_EMTPY = 0,
|
---|
| 280 | IMG_CMAP = 1,
|
---|
| 281 | IMG_BGRA = 2,
|
---|
| 282 | IMG_GRAY = 3,
|
---|
| 283 | IMG_CMAP_RLE = 9,
|
---|
| 284 | IMG_BGRA_RLE = 10,
|
---|
| 285 | IMG_GRAY_RLE = 11
|
---|
| 286 | } img_type_t;
|
---|
| 287 |
|
---|
| 288 | typedef struct {
|
---|
| 289 | cmap_type_t cmap_type;
|
---|
| 290 | img_type_t img_type;
|
---|
| 291 |
|
---|
| 292 | uint16_t cmap_first_entry;
|
---|
| 293 | uint16_t cmap_entries;
|
---|
| 294 | uint8_t cmap_bpp;
|
---|
| 295 |
|
---|
| 296 | uint16_t startx;
|
---|
| 297 | uint16_t starty;
|
---|
| 298 | uint16_t width;
|
---|
| 299 | uint16_t height;
|
---|
| 300 | uint8_t img_bpp;
|
---|
| 301 | uint8_t img_alpha_bpp;
|
---|
| 302 | uint8_t img_alpha_dir;
|
---|
| 303 |
|
---|
| 304 | void *id_data;
|
---|
| 305 | size_t id_length;
|
---|
| 306 |
|
---|
| 307 | void *cmap_data;
|
---|
| 308 | size_t cmap_length;
|
---|
| 309 |
|
---|
| 310 | void *img_data;
|
---|
| 311 | size_t img_length;
|
---|
| 312 | } tga_t;
|
---|
| 313 |
|
---|
| 314 | /** Decode Truevision TGA header
|
---|
| 315 | *
|
---|
| 316 | * @param[in] data Memory representation of TGA.
|
---|
| 317 | * @param[in] size Size of the representation (in bytes).
|
---|
| 318 | * @param[out] tga Decoded TGA.
|
---|
| 319 | *
|
---|
| 320 | * @return True on succesful decoding.
|
---|
| 321 | * @return False on failure.
|
---|
| 322 | *
|
---|
| 323 | */
|
---|
| 324 | static bool decode_tga_header(void *data, size_t size, tga_t *tga)
|
---|
| 325 | {
|
---|
| 326 | /* Header sanity check */
|
---|
| 327 | if (size < sizeof(tga_header_t))
|
---|
| 328 | return false;
|
---|
| 329 |
|
---|
| 330 | tga_header_t *head = (tga_header_t *) data;
|
---|
| 331 |
|
---|
| 332 | /* Image ID field */
|
---|
| 333 | tga->id_data = data + sizeof(tga_header_t);
|
---|
| 334 | tga->id_length = head->id_length;
|
---|
| 335 |
|
---|
| 336 | if (size < sizeof(tga_header_t) + tga->id_length)
|
---|
| 337 | return false;
|
---|
| 338 |
|
---|
| 339 | /* Color map type */
|
---|
| 340 | tga->cmap_type = head->cmap_type;
|
---|
| 341 |
|
---|
| 342 | /* Image type */
|
---|
| 343 | tga->img_type = head->img_type;
|
---|
| 344 |
|
---|
| 345 | /* Color map specification */
|
---|
| 346 | tga->cmap_first_entry = uint16_t_le2host(head->cmap_first_entry);
|
---|
| 347 | tga->cmap_entries = uint16_t_le2host(head->cmap_entries);
|
---|
| 348 | tga->cmap_bpp = head->cmap_bpp;
|
---|
| 349 | tga->cmap_data = tga->id_data + tga->id_length;
|
---|
| 350 | tga->cmap_length = ALIGN_UP(tga->cmap_entries * tga->cmap_bpp, 8) >> 3;
|
---|
| 351 |
|
---|
| 352 | if (size < sizeof(tga_header_t) + tga->id_length +
|
---|
| 353 | tga->cmap_length)
|
---|
| 354 | return false;
|
---|
| 355 |
|
---|
| 356 | /* Image specification */
|
---|
| 357 | tga->startx = uint16_t_le2host(head->startx);
|
---|
| 358 | tga->starty = uint16_t_le2host(head->starty);
|
---|
| 359 | tga->width = uint16_t_le2host(head->width);
|
---|
| 360 | tga->height = uint16_t_le2host(head->height);
|
---|
| 361 | tga->img_bpp = head->img_bpp;
|
---|
| 362 | tga->img_alpha_bpp = head->img_descr & 0x0f;
|
---|
| 363 | tga->img_alpha_dir = (head->img_descr & 0xf0) >> 4;
|
---|
| 364 | tga->img_data = tga->cmap_data + tga->cmap_length;
|
---|
| 365 | tga->img_length = ALIGN_UP(tga->width * tga->height * tga->img_bpp, 8) >> 3;
|
---|
| 366 |
|
---|
| 367 | if (size < sizeof(tga_header_t) + tga->id_length +
|
---|
| 368 | tga->cmap_length + tga->img_length)
|
---|
| 369 | return false;
|
---|
| 370 |
|
---|
| 371 | return true;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | void imgmap_put_pixel(imgmap_t *imgmap, sysarg_t x, sysarg_t y, pixel_t pixel)
|
---|
| 375 | {
|
---|
| 376 | if ((x >= imgmap->width) || (y >= imgmap->height))
|
---|
| 377 | return;
|
---|
| 378 |
|
---|
| 379 | size_t offset = y * imgmap->width + x;
|
---|
| 380 |
|
---|
| 381 | switch (imgmap->visual) {
|
---|
| 382 | case VISUAL_RGB_0_8_8_8:
|
---|
| 383 | pixel2rgb_0888(((uint32_t *) imgmap->data) + offset, pixel);
|
---|
| 384 | break;
|
---|
| 385 | default:
|
---|
| 386 | break;
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | pixel_t imgmap_get_pixel(imgmap_t *imgmap, sysarg_t x, sysarg_t y)
|
---|
| 391 | {
|
---|
| 392 | if ((x >= imgmap->width) || (y >= imgmap->height))
|
---|
| 393 | return 0;
|
---|
| 394 |
|
---|
| 395 | size_t offset = y * imgmap->width + x;
|
---|
| 396 |
|
---|
| 397 | switch (imgmap->visual) {
|
---|
| 398 | case VISUAL_RGB_0_8_8_8:
|
---|
| 399 | return rgb_0888_2pixel(((uint32_t *) imgmap->data) + offset);
|
---|
| 400 | default:
|
---|
| 401 | return 0;
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | imgmap_t *imgmap_create(sysarg_t width, sysarg_t height, visual_t visual,
|
---|
| 406 | imgmap_flags_t flags)
|
---|
| 407 | {
|
---|
| 408 | size_t bsize;
|
---|
| 409 |
|
---|
| 410 | switch (visual) {
|
---|
| 411 | case VISUAL_RGB_0_8_8_8:
|
---|
| 412 | bsize = (width * height) << 2;
|
---|
| 413 | break;
|
---|
| 414 | default:
|
---|
| 415 | return NULL;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | size_t size = sizeof(imgmap_t) + bsize;
|
---|
| 419 | imgmap_t *imgmap;
|
---|
| 420 |
|
---|
| 421 | if ((flags & IMGMAP_FLAG_SHARED) == IMGMAP_FLAG_SHARED) {
|
---|
[fbcdeb8] | 422 | imgmap = (imgmap_t *) as_area_create((void *) -1, size,
|
---|
| 423 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
| 424 | if (imgmap == (void *) -1)
|
---|
[7c014d1] | 425 | return NULL;
|
---|
| 426 | } else {
|
---|
| 427 | imgmap = (imgmap_t *) malloc(size);
|
---|
| 428 | if (imgmap == NULL)
|
---|
| 429 | return NULL;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | imgmap->size = size;
|
---|
| 433 | imgmap->flags = flags;
|
---|
| 434 | imgmap->width = width;
|
---|
| 435 | imgmap->height = height;
|
---|
| 436 | imgmap->visual = visual;
|
---|
| 437 |
|
---|
| 438 | memset(imgmap->data, 0, bsize);
|
---|
| 439 |
|
---|
| 440 | return imgmap;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | /** Decode Truevision TGA format
|
---|
| 444 | *
|
---|
| 445 | * Decode Truevision TGA format and create an image map
|
---|
| 446 | * from it. The supported variants of TGA are currently
|
---|
| 447 | * limited to uncompressed 24 bit true-color images without
|
---|
| 448 | * alpha channel.
|
---|
| 449 | *
|
---|
| 450 | * @param[in] data Memory representation of TGA.
|
---|
| 451 | * @param[in] size Size of the representation (in bytes).
|
---|
| 452 | * @param[in] flags Image map creation flags.
|
---|
| 453 | *
|
---|
| 454 | * @return Newly allocated image map.
|
---|
| 455 | * @return NULL on error or unsupported format.
|
---|
| 456 | *
|
---|
| 457 | */
|
---|
| 458 | imgmap_t *imgmap_decode_tga(void *data, size_t size, imgmap_flags_t flags)
|
---|
| 459 | {
|
---|
| 460 | tga_t tga;
|
---|
| 461 | if (!decode_tga_header(data, size, &tga))
|
---|
| 462 | return NULL;
|
---|
| 463 |
|
---|
| 464 | /*
|
---|
| 465 | * Check for unsupported features.
|
---|
| 466 | */
|
---|
| 467 |
|
---|
| 468 | switch (tga.cmap_type) {
|
---|
| 469 | case CMAP_NOT_PRESENT:
|
---|
| 470 | break;
|
---|
| 471 | default:
|
---|
| 472 | /* Unsupported */
|
---|
| 473 | return NULL;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | switch (tga.img_type) {
|
---|
| 477 | case IMG_BGRA:
|
---|
| 478 | if (tga.img_bpp != 24)
|
---|
| 479 | return NULL;
|
---|
| 480 | break;
|
---|
| 481 | case IMG_GRAY:
|
---|
| 482 | if (tga.img_bpp != 8)
|
---|
| 483 | return NULL;
|
---|
| 484 | break;
|
---|
| 485 | default:
|
---|
| 486 | /* Unsupported */
|
---|
| 487 | return NULL;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | if (tga.img_alpha_bpp != 0)
|
---|
| 491 | return NULL;
|
---|
| 492 |
|
---|
| 493 | sysarg_t twidth = tga.startx + tga.width;
|
---|
| 494 | sysarg_t theight = tga.starty + tga.height;
|
---|
| 495 |
|
---|
| 496 | imgmap_t *imgmap = imgmap_create(twidth, theight, VISUAL_RGB_0_8_8_8,
|
---|
| 497 | flags);
|
---|
| 498 | if (imgmap == NULL)
|
---|
| 499 | return NULL;
|
---|
| 500 |
|
---|
| 501 | /*
|
---|
| 502 | * TGA is encoded in a bottom-up manner, the true-color
|
---|
| 503 | * variant is in BGR 8:8:8 encoding.
|
---|
| 504 | */
|
---|
| 505 |
|
---|
| 506 | switch (tga.img_type) {
|
---|
| 507 | case IMG_BGRA:
|
---|
| 508 | for (sysarg_t y = tga.starty; y < theight; y++) {
|
---|
| 509 | for (sysarg_t x = tga.startx; x < twidth; x++) {
|
---|
| 510 | size_t offset =
|
---|
| 511 | ((y - tga.starty) * tga.width + (x - tga.startx)) * 3;
|
---|
| 512 |
|
---|
| 513 | pixel_t pixel =
|
---|
| 514 | bgr_888_2pixel(((uint8_t *) tga.img_data) + offset);
|
---|
| 515 | imgmap_put_pixel(imgmap, x, theight - y - 1, pixel);
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
| 518 | break;
|
---|
| 519 | case IMG_GRAY:
|
---|
| 520 | for (sysarg_t y = tga.starty; y < theight; y++) {
|
---|
| 521 | for (sysarg_t x = tga.startx; x < twidth; x++) {
|
---|
| 522 | size_t offset =
|
---|
| 523 | (y - tga.starty) * tga.width + (x - tga.startx);
|
---|
| 524 |
|
---|
| 525 | pixel_t pixel =
|
---|
| 526 | gray_8_2pixel(((uint8_t *) tga.img_data) + offset);
|
---|
| 527 | imgmap_put_pixel(imgmap, x, theight - y - 1, pixel);
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 | break;
|
---|
| 531 | default:
|
---|
| 532 | break;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | return imgmap;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | void imgmap_get_resolution(imgmap_t *imgmap, sysarg_t *width, sysarg_t *height)
|
---|
| 539 | {
|
---|
| 540 | assert(width);
|
---|
| 541 | assert(height);
|
---|
| 542 |
|
---|
| 543 | *width = imgmap->width;
|
---|
| 544 | *height = imgmap->height;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /** @}
|
---|
| 548 | */
|
---|