| 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 "imgmap.h"
|
|---|
| 42 |
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | uint8_t id_length;
|
|---|
| 45 | uint8_t cmap_type;
|
|---|
| 46 | uint8_t img_type;
|
|---|
| 47 |
|
|---|
| 48 | uint16_t cmap_first_entry;
|
|---|
| 49 | uint16_t cmap_entries;
|
|---|
| 50 | uint8_t cmap_bpp;
|
|---|
| 51 |
|
|---|
| 52 | uint16_t startx;
|
|---|
| 53 | uint16_t starty;
|
|---|
| 54 | uint16_t width;
|
|---|
| 55 | uint16_t height;
|
|---|
| 56 | uint8_t img_bpp;
|
|---|
| 57 | uint8_t img_descr;
|
|---|
| 58 | } __attribute__((packed)) tga_header_t;
|
|---|
| 59 |
|
|---|
| 60 | typedef enum {
|
|---|
| 61 | CMAP_NOT_PRESENT = 0,
|
|---|
| 62 | CMAP_PRESENT = 1,
|
|---|
| 63 | CMAP_RESERVED_START = 2,
|
|---|
| 64 | CMAP_PRIVATE_START = 128
|
|---|
| 65 | } cmap_type_t;
|
|---|
| 66 |
|
|---|
| 67 | typedef enum {
|
|---|
| 68 | IMG_EMTPY = 0,
|
|---|
| 69 | IMG_CMAP = 1,
|
|---|
| 70 | IMG_BGRA = 2,
|
|---|
| 71 | IMG_GRAY = 3,
|
|---|
| 72 | IMG_CMAP_RLE = 9,
|
|---|
| 73 | IMG_BGRA_RLE = 10,
|
|---|
| 74 | IMG_GRAY_RLE = 11
|
|---|
| 75 | } img_type_t;
|
|---|
| 76 |
|
|---|
| 77 | typedef struct {
|
|---|
| 78 | cmap_type_t cmap_type;
|
|---|
| 79 | img_type_t img_type;
|
|---|
| 80 |
|
|---|
| 81 | uint16_t cmap_first_entry;
|
|---|
| 82 | uint16_t cmap_entries;
|
|---|
| 83 | uint8_t cmap_bpp;
|
|---|
| 84 |
|
|---|
| 85 | uint16_t startx;
|
|---|
| 86 | uint16_t starty;
|
|---|
| 87 | uint16_t width;
|
|---|
| 88 | uint16_t height;
|
|---|
| 89 | uint8_t img_bpp;
|
|---|
| 90 | uint8_t img_alpha_bpp;
|
|---|
| 91 | uint8_t img_alpha_dir;
|
|---|
| 92 |
|
|---|
| 93 | void *id_data;
|
|---|
| 94 | size_t id_length;
|
|---|
| 95 |
|
|---|
| 96 | void *cmap_data;
|
|---|
| 97 | size_t cmap_length;
|
|---|
| 98 |
|
|---|
| 99 | void *img_data;
|
|---|
| 100 | size_t img_length;
|
|---|
| 101 | } tga_t;
|
|---|
| 102 |
|
|---|
| 103 | /** Decode Truevision TGA header
|
|---|
| 104 | *
|
|---|
| 105 | * @param[in] data Memory representation of TGA.
|
|---|
| 106 | * @param[in] size Size of the representation (in bytes).
|
|---|
| 107 | * @param[out] tga Decoded TGA.
|
|---|
| 108 | *
|
|---|
| 109 | * @return True on succesful decoding.
|
|---|
| 110 | * @return False on failure.
|
|---|
| 111 | *
|
|---|
| 112 | */
|
|---|
| 113 | static bool decode_tga_header(void *data, size_t size, tga_t *tga)
|
|---|
| 114 | {
|
|---|
| 115 | /* Header sanity check */
|
|---|
| 116 | if (size < sizeof(tga_header_t))
|
|---|
| 117 | return false;
|
|---|
| 118 |
|
|---|
| 119 | tga_header_t *head = (tga_header_t *) data;
|
|---|
| 120 |
|
|---|
| 121 | /* Image ID field */
|
|---|
| 122 | tga->id_data = data + sizeof(tga_header_t);
|
|---|
| 123 | tga->id_length = head->id_length;
|
|---|
| 124 |
|
|---|
| 125 | if (size < sizeof(tga_header_t) + tga->id_length)
|
|---|
| 126 | return false;
|
|---|
| 127 |
|
|---|
| 128 | /* Color map type */
|
|---|
| 129 | tga->cmap_type = head->cmap_type;
|
|---|
| 130 |
|
|---|
| 131 | /* Image type */
|
|---|
| 132 | tga->img_type = head->img_type;
|
|---|
| 133 |
|
|---|
| 134 | /* Color map specification */
|
|---|
| 135 | tga->cmap_first_entry = uint16_t_le2host(head->cmap_first_entry);
|
|---|
| 136 | tga->cmap_entries = uint16_t_le2host(head->cmap_entries);
|
|---|
| 137 | tga->cmap_bpp = head->cmap_bpp;
|
|---|
| 138 | tga->cmap_data = tga->id_data + tga->id_length;
|
|---|
| 139 | tga->cmap_length = ALIGN_UP(tga->cmap_entries * tga->cmap_bpp, 8) >> 3;
|
|---|
| 140 |
|
|---|
| 141 | if (size < sizeof(tga_header_t) + tga->id_length +
|
|---|
| 142 | tga->cmap_length)
|
|---|
| 143 | return false;
|
|---|
| 144 |
|
|---|
| 145 | /* Image specification */
|
|---|
| 146 | tga->startx = uint16_t_le2host(head->startx);
|
|---|
| 147 | tga->starty = uint16_t_le2host(head->starty);
|
|---|
| 148 | tga->width = uint16_t_le2host(head->width);
|
|---|
| 149 | tga->height = uint16_t_le2host(head->height);
|
|---|
| 150 | tga->img_bpp = head->img_bpp;
|
|---|
| 151 | tga->img_alpha_bpp = head->img_descr & 0x0f;
|
|---|
| 152 | tga->img_alpha_dir = (head->img_descr & 0xf0) >> 4;
|
|---|
| 153 | tga->img_data = tga->cmap_data + tga->cmap_length;
|
|---|
| 154 | tga->img_length = ALIGN_UP(tga->width * tga->height * tga->img_bpp, 8) >> 3;
|
|---|
| 155 |
|
|---|
| 156 | if (size < sizeof(tga_header_t) + tga->id_length +
|
|---|
| 157 | tga->cmap_length + tga->img_length)
|
|---|
| 158 | return false;
|
|---|
| 159 |
|
|---|
| 160 | return true;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /** Decode Truevision TGA format
|
|---|
| 164 | *
|
|---|
| 165 | * Decode Truevision TGA format and create an image map
|
|---|
| 166 | * from it. The supported variants of TGA are currently
|
|---|
| 167 | * limited to uncompressed 24 bit true-color images without
|
|---|
| 168 | * alpha channel.
|
|---|
| 169 | *
|
|---|
| 170 | * @param[in] data Memory representation of TGA.
|
|---|
| 171 | * @param[in] size Size of the representation (in bytes).
|
|---|
| 172 | *
|
|---|
| 173 | * @return Newly allocated image map.
|
|---|
| 174 | * @return NULL on error or unsupported format.
|
|---|
| 175 | *
|
|---|
| 176 | */
|
|---|
| 177 | imgmap_t *imgmap_decode_tga(void *data, size_t size)
|
|---|
| 178 | {
|
|---|
| 179 | tga_t tga;
|
|---|
| 180 | if (!decode_tga_header(data, size, &tga))
|
|---|
| 181 | return NULL;
|
|---|
| 182 |
|
|---|
| 183 | /*
|
|---|
| 184 | * Check for unsupported features.
|
|---|
| 185 | */
|
|---|
| 186 |
|
|---|
| 187 | switch (tga.cmap_type) {
|
|---|
| 188 | case CMAP_NOT_PRESENT:
|
|---|
| 189 | break;
|
|---|
| 190 | default:
|
|---|
| 191 | /* Unsupported */
|
|---|
| 192 | return NULL;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | switch (tga.img_type) {
|
|---|
| 196 | case IMG_BGRA:
|
|---|
| 197 | if (tga.img_bpp != 24)
|
|---|
| 198 | return NULL;
|
|---|
| 199 | break;
|
|---|
| 200 | case IMG_GRAY:
|
|---|
| 201 | if (tga.img_bpp != 8)
|
|---|
| 202 | return NULL;
|
|---|
| 203 | break;
|
|---|
| 204 | default:
|
|---|
| 205 | /* Unsupported */
|
|---|
| 206 | return NULL;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | if (tga.img_alpha_bpp != 0)
|
|---|
| 210 | return NULL;
|
|---|
| 211 |
|
|---|
| 212 | sysarg_t twidth = tga.startx + tga.width;
|
|---|
| 213 | sysarg_t theight = tga.starty + tga.height;
|
|---|
| 214 | size_t bsize = twidth * theight * 3;
|
|---|
| 215 |
|
|---|
| 216 | imgmap_t *imgmap = (imgmap_t *) malloc(sizeof(imgmap_t) + bsize);
|
|---|
| 217 | if (imgmap == NULL)
|
|---|
| 218 | return NULL;
|
|---|
| 219 |
|
|---|
| 220 | imgmap->size = sizeof(imgmap_t) + bsize;
|
|---|
| 221 | imgmap->width = twidth;
|
|---|
| 222 | imgmap->height = theight;
|
|---|
| 223 | imgmap->visual = VISUAL_BGR_8_8_8;
|
|---|
| 224 |
|
|---|
| 225 | memset(imgmap->data, 0, bsize);
|
|---|
| 226 |
|
|---|
| 227 | /*
|
|---|
| 228 | * TGA is encoded in a bottom-up manner.
|
|---|
| 229 | */
|
|---|
| 230 |
|
|---|
| 231 | switch (tga.img_type) {
|
|---|
| 232 | case IMG_BGRA:
|
|---|
| 233 | for (sysarg_t y = tga.starty; y < theight; y++) {
|
|---|
| 234 | size_t src_offset = (y - tga.starty) * tga.width * 3;
|
|---|
| 235 | size_t dst_offset = ((theight - y - 1) * twidth + tga.startx) * 3;
|
|---|
| 236 |
|
|---|
| 237 | memcpy(imgmap->data + dst_offset, tga.img_data + src_offset,
|
|---|
| 238 | tga.width * 3);
|
|---|
| 239 | }
|
|---|
| 240 | break;
|
|---|
| 241 | case IMG_GRAY:
|
|---|
| 242 | for (sysarg_t y = tga.starty; y < theight; y++) {
|
|---|
| 243 | for (sysarg_t x = tga.startx; x < twidth; x++) {
|
|---|
| 244 | uint8_t val =
|
|---|
| 245 | ((uint8_t *) tga.img_data)[(y - tga.starty) * tga.width + (x - tga.startx)];
|
|---|
| 246 | size_t dst_offset = ((theight - y - 1) * twidth + x) * 3;
|
|---|
| 247 | memset(imgmap->data + dst_offset, val, 3);
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | break;
|
|---|
| 251 | default:
|
|---|
| 252 | break;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | return imgmap;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /** @}
|
|---|
| 259 | */
|
|---|