source: mainline/uspace/lib/draw/codec/tga.c@ 207e8880

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

cherrypick GUI implementation (originally by Petr Koupy), with several major changes

  • for character-oriented devices a new output server and output protocol was created based on the original fb server
  • DDF visualizer drivers are pixel-oriented only
  • console and compositor can coexist in the same build
  • terminal widget is self-sufficient, no strange console nesting is needed
  • Property mode set to 100644
File size: 6.8 KB
Line 
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 draw
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdlib.h>
38#include <byteorder.h>
39#include <align.h>
40#include <bool.h>
41#include <pixconv.h>
42#include "tga.h"
43
44typedef struct {
45 uint8_t id_length;
46 uint8_t cmap_type;
47 uint8_t img_type;
48
49 uint16_t cmap_first_entry;
50 uint16_t cmap_entries;
51 uint8_t cmap_bpp;
52
53 uint16_t startx;
54 uint16_t starty;
55 uint16_t width;
56 uint16_t height;
57 uint8_t img_bpp;
58 uint8_t img_descr;
59} __attribute__((packed)) tga_header_t;
60
61typedef enum {
62 CMAP_NOT_PRESENT = 0,
63 CMAP_PRESENT = 1,
64 CMAP_RESERVED_START = 2,
65 CMAP_PRIVATE_START = 128
66} cmap_type_t;
67
68typedef enum {
69 IMG_EMTPY = 0,
70 IMG_CMAP = 1,
71 IMG_BGRA = 2,
72 IMG_GRAY = 3,
73 IMG_CMAP_RLE = 9,
74 IMG_BGRA_RLE = 10,
75 IMG_GRAY_RLE = 11
76} img_type_t;
77
78typedef struct {
79 cmap_type_t cmap_type;
80 img_type_t img_type;
81
82 uint16_t cmap_first_entry;
83 uint16_t cmap_entries;
84 uint8_t cmap_bpp;
85
86 uint16_t startx;
87 uint16_t starty;
88 uint16_t width;
89 uint16_t height;
90 uint8_t img_bpp;
91 uint8_t img_alpha_bpp;
92 uint8_t img_alpha_dir;
93
94 void *id_data;
95 size_t id_length;
96
97 void *cmap_data;
98 size_t cmap_length;
99
100 void *img_data;
101 size_t img_length;
102} tga_t;
103
104/** Decode Truevision TGA header
105 *
106 * @param[in] data Memory representation of TGA.
107 * @param[in] size Size of the representation (in bytes).
108 * @param[out] tga Decoded TGA.
109 *
110 * @return True on succesful decoding.
111 * @return False on failure.
112 */
113static 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 a surface
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 * @param[in] flags Surface creation flags.
173 *
174 * @return Newly allocated surface with the decoded content.
175 * @return NULL on error or unsupported format.
176 */
177surface_t *decode_tga(void *data, size_t size, surface_flags_t flags)
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
215 surface_t *surface = surface_create(twidth, theight, NULL, flags);
216 if (surface == NULL)
217 return NULL;
218
219 /*
220 * TGA is encoded in a bottom-up manner, the true-color
221 * variant is in BGR 8:8:8 encoding.
222 */
223
224 switch (tga.img_type) {
225 case IMG_BGRA:
226 for (sysarg_t y = tga.starty; y < theight; y++) {
227 for (sysarg_t x = tga.startx; x < twidth; x++) {
228 size_t offset =
229 ((y - tga.starty) * tga.width + (x - tga.startx)) * 3;
230
231 pixel_t pixel =
232 bgr_888_2pixel(((uint8_t *) tga.img_data) + offset);
233 surface_put_pixel(surface, x, theight - y - 1, pixel);
234 }
235 }
236 break;
237 case IMG_GRAY:
238 for (sysarg_t y = tga.starty; y < theight; y++) {
239 for (sysarg_t x = tga.startx; x < twidth; x++) {
240 size_t offset =
241 (y - tga.starty) * tga.width + (x - tga.startx);
242
243 pixel_t pixel =
244 gray_8_2pixel(((uint8_t *) tga.img_data) + offset);
245 surface_put_pixel(surface, x, theight - y - 1, pixel);
246 }
247 }
248 break;
249 default:
250 break;
251 }
252
253 return surface;
254}
255
256/** Encode Truevision TGA format
257 *
258 * Encode Truevision TGA format into an array.
259 *
260 * @param[in] surface Surface to be encoded into TGA.
261 * @param[out] pdata Pointer to the resulting array.
262 * @param[out] psize Pointer to the size of the resulting array.
263 *
264 * @return True on succesful encoding.
265 * @return False on failure.
266 */
267bool encode_tga(surface_t *surface, void **pdata, size_t *psize)
268{
269 // TODO
270 return false;
271}
272
273/** @}
274 */
Note: See TracBrowser for help on using the repository browser.