source: mainline/uspace/lib/imgmap/imgmap.c@ 67b05ff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 67b05ff was 67b05ff, checked in by Martin Decky <martin@…>, 14 years ago

cherrypick from fb server rewrite: store images as Truevision TGA (instead of PPM) and use a library to decode them into image maps

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
43typedef 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
60typedef 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
67typedef enum {
68 IMG_EMTPY = 0,
69 IMG_CMAP = 1,
70 IMG_BGRA = 2,
71 IMG_BW = 3,
72 IMG_CMAP_RLE = 9,
73 IMG_BGRA_RLE = 10,
74 IMG_BW_RLE = 11
75} img_type_t;
76
77typedef 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[out] tga Decoded TGA.
107 *
108 * @return True on succesful decoding.
109 * @return False on failure.
110 *
111 */
112static bool decode_tga_header(void *data, tga_t *tga)
113{
114 tga_header_t *head = (tga_header_t *) data;
115
116 /* Image ID field */
117 tga->id_data = data + sizeof(tga_header_t);
118 tga->id_length = head->id_length;
119
120 /* Color map type */
121 tga->cmap_type = head->cmap_type;
122
123 /* Image type */
124 tga->img_type = head->img_type;
125
126 /* Color map specification */
127 tga->cmap_first_entry = uint16_t_le2host(head->cmap_first_entry);
128 tga->cmap_entries = uint16_t_le2host(head->cmap_entries);
129 tga->cmap_bpp = head->cmap_bpp;
130 tga->cmap_data = tga->id_data + tga->id_length;
131 tga->cmap_length = ALIGN_UP(tga->cmap_entries * tga->cmap_bpp, 8) >> 3;
132
133 /* Image specification */
134 tga->startx = uint16_t_le2host(head->startx);
135 tga->starty = uint16_t_le2host(head->starty);
136 tga->width = uint16_t_le2host(head->width);
137 tga->height = uint16_t_le2host(head->height);
138 tga->img_bpp = head->img_bpp;
139 tga->img_alpha_bpp = head->img_descr & 0x0f;
140 tga->img_alpha_dir = (head->img_descr & 0xf0) >> 4;
141 tga->img_data = tga->cmap_data + tga->cmap_length;
142 tga->img_length = ALIGN_UP(tga->width * tga->height * tga->img_bpp, 8) >> 3;
143
144 return true;
145}
146
147/** Decode Truevision TGA format
148 *
149 * Decode Truevision TGA format and create an image map
150 * from it. The supported variants of TGA are currently
151 * limited to uncompressed 24 bit true-color images without
152 * alpha channel.
153 *
154 * @param data[in] Memory representation of TGA.
155 *
156 * @return Newly allocated image map.
157 * @return NULL on error or unsupported format.
158 *
159 */
160imgmap_t *imgmap_decode_tga(void *data)
161{
162 tga_t tga;
163 if (!decode_tga_header(data, &tga))
164 return NULL;
165
166 /*
167 * Check for unsupported features.
168 */
169
170 switch (tga.cmap_type) {
171 case CMAP_NOT_PRESENT:
172 break;
173 default:
174 /* Unsupported */
175 return NULL;
176 }
177
178 switch (tga.img_type) {
179 case IMG_BGRA:
180 break;
181 default:
182 /* Unsupported */
183 return NULL;
184 }
185
186 if (tga.img_bpp != 24)
187 return NULL;
188
189 if (tga.img_alpha_bpp != 0)
190 return NULL;
191
192 sysarg_t twidth = tga.startx + tga.width;
193 sysarg_t theight = tga.starty + tga.height;
194 size_t size = twidth * theight * 3;
195
196 imgmap_t *imgmap = (imgmap_t *) malloc(sizeof(imgmap_t) + size);
197 if (imgmap == NULL)
198 return NULL;
199
200 imgmap->tag = 'I';
201 imgmap->size = sizeof(imgmap_t) + size;
202 imgmap->width = twidth;
203 imgmap->height = theight;
204 imgmap->visual = VISUAL_BGR_8_8_8;
205
206 memset(imgmap->data, 0, size);
207
208 for (sysarg_t y = tga.starty; y < theight; y++) {
209 size_t src_offset = (y - tga.starty) * tga.width * 3;
210 size_t dst_offset = ((theight - y - 1) * twidth + tga.startx) * 3;
211
212 memcpy(imgmap->data + dst_offset, tga.img_data + src_offset,
213 tga.width * 3);
214 }
215
216 return imgmap;
217}
218
219/** @}
220 */
Note: See TracBrowser for help on using the repository browser.