Debian Codegraph @

src/bniutils/tga.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2000 Marco Ziech ([email protected])
3 Copyright (C) 2000 Ross Combs ([email protected])
4
5 This program is xfree software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "common/setup_before.h"
20 #include "tga.h"
21
22 #include <cerrno>
23 #include <cstdint>
24 #include <cstring>
25
26 #include "common/xalloc.h"
27 #include "fileio.h"
28 #include "common/setup_after.h"
29
30 namespace pvpgn
31 {
32
33 namespace bni
34 {
35
36 namespace
37 {
38
39 int rotate_updown(t_tgaimg *img) {
40 unsigned char *ndata;
41 int pixelsize;
42 int y;
43 if (img == NULL) return -1;
44 if (img->data == NULL) return -1;
45 pixelsize = getpixelsize(img);
46 if (pixelsize == 0) return -1;
47 ndata = (unsigned char *)xmalloc(img->width*img->height*pixelsize);
48 for (y = 0; y < img->height; y++) {
49 std::memcpy(ndata + (y*img->width*pixelsize),
50 img->data + ((img->width*img->height*pixelsize) - ((y + 1)*img->width*pixelsize)),
51 img->width*pixelsize);
52 }
53 xfree(img->data);
54 img->data = ndata;
55 return 0;
56 }
57
58 int rotate_leftright(t_tgaimg *img) {
59 unsigned char *ndata, *datap;
60 int pixelsize;
61 int y, x;
62 std::fprintf(stderr, "WARNING: rotate_leftright: this function is untested!\n");
63 if (img == NULL) return -1;
64 if (img->data == NULL) return -1;
65 pixelsize = getpixelsize(img);
66 if (pixelsize == 0) return -1;
67 ndata = (unsigned char *)xmalloc(img->width*img->height*pixelsize);
68 datap = img->data;
69 for (y = 0; y < img->height; y++) {
70 unsigned char *linep = (ndata + (((y + 1)*img->width*pixelsize) - pixelsize));
71 for (x = 0; x < img->width; x++) {
72 std::memcpy(linep, datap, pixelsize);
73 linep -= pixelsize;
74 datap += pixelsize;
75 }
76 }
77 xfree(img->data);
78 img->data = ndata;
79
80 return 0;
81 }
82
83 int RLE_decompress(std::FILE *f, void *buf, int bufsize, int pixelsize) {
84 unsigned char pt;
85 unsigned char *bufp;
86 unsigned char temp[8]; /* MAXPIXELSIZE */
87 int bufi;
88 int count;
89
90 file_rpush(f);
91 bufp = (unsigned char*)buf;
92 for (bufi = 0; bufi<bufsize;) {
93 pt = file_readb();
94 if (std::feof(f)) {
95 std::fprintf(stderr, "RLE_decompress: after final packet only got %d of %d bytes\n", bufi, bufsize);
96 file_rpop();
97 return -1;
98 }
99 count = (pt & 0x7f) + 1;
100 if (bufi + count*pixelsize>bufsize) {
101 std::fprintf(stderr, "RLE_decompress: buffer too short for next packet (need %d bytes, have %d)\n", bufi + count*pixelsize, bufsize);
102 file_rpop();
103 return -1;
104 }
105 if ((pt & 0x80) == 0) { /* RAW PACKET */
106 if (std::fread(bufp, pixelsize, count, f) < (unsigned)count) {
107 if (std::feof(f))
108 std::fprintf(stderr, "RLE_decompress: short RAW packet (expected %d bytes) (EOF)\n", pixelsize*count);
109 else
110 std::fprintf(stderr, "RLE_decompress: short RAW packet (expected %d bytes) (std::fread: %s)\n", pixelsize*count, std::strerror(errno));
111 #if 0
112 file_rpop();
113 return -1;
114 #endif
115 }
116 bufp += count*pixelsize;
117 bufi += count*pixelsize;
118 }
119 else { /* RLE PACKET */
120 if (std::fread(temp, pixelsize, 1, f) < 1) {
121 if (std::feof(f))
122 std::fprintf(stderr, "RLE_decompress: short RLE packet (expected %d bytes) (EOF)\n", pixelsize);
123 else
124 std::fprintf(stderr, "RLE_decompress: short RLE packet (expected %d bytes) (std::fread: %s)\n", pixelsize, std::strerror(errno));
125 #if 0
126 file_rpop();
127 return -1;
128 #endif
129 }
130 if (count<2) {
131 std::fprintf(stderr, "RLE_decompress: suspicious RLE repetition count %d\n", count);
132 }
133 for (; count > 0; count--) {
134 std::memcpy(bufp, temp, pixelsize);
135 bufp += pixelsize;
136 bufi += pixelsize;
137 }
138 }
139 }
140 file_rpop();
141 return 0;
142 }
143
144 void RLE_write_pkt(std::FILE *f, t_tgapkttype pkttype, int len, void *data, int pixelsize) {
145 unsigned char count;
146
147 if (len<1 || len>128) {
148 std::fprintf(stderr, "RLE_write_pkt: packet has bad length (%d bytes)\n", len);
149 return;
150 }
151 if (pkttype == RLE) {
152 if (len < 2) {
153 std::fprintf(stderr, "RLE_write_pkt: RLE packet has bad length (%d bytes)\n", len);
154 return;
155 }
156 count = (unsigned char)(0x80 | (len - 1));
157 if (std::fwrite(&count, 1, 1, f) < 1)
158 std::fprintf(stderr, "RLE_write_pkt: could not write RLE pixel count (std::fwrite: %s)\n", std::strerror(errno));
159 if (std::fwrite(data, pixelsize, 1, f) < 1)
160 std::fprintf(stderr, "RLE_write_pkt: could not write RLE pixel value (std::fwrite: %s)\n", std::strerror(errno));
161 }
162 else {
163 count = (unsigned char)(len - 1);
164 if (std::fwrite(&count, 1, 1, f) < 1)
165 std::fprintf(stderr, "RLE_write_pkt: could not write RAW pixel count (std::fwrite: %s)\n", std::strerror(errno));
166 if (std::fwrite(data, pixelsize, len, f) < (unsigned)len)
167 std::fprintf(stderr, "RLE_write_pkt: could not write %d RAW pixels (std::fwrite: %s)\n", len, std::strerror(errno));
168 }
169 }
170
171
172 int RLE_compress(std::FILE *f, t_tgaimg const *img) {
173 int pixelsize;
174 unsigned char const *datap;
175 unsigned char *pktdata;
176 unsigned int pktlen;
177 t_tgapkttype pkttype;
178 unsigned char *pktdatap;
179 unsigned int actual = 0, perceived = 0;
180 int i;
181
182 pkttype = RAW;
183 pktdatap = NULL;
184
185 if (img == NULL) return -1;
186 if (img->data == NULL) return -1;
187 pixelsize = getpixelsize(img);
188 if (pixelsize == 0) return -1;
189
190 datap = img->data;
191 pktdata = (unsigned char*)xmalloc(img->width*img->height*pixelsize);
192 pktlen = 0;
193
194 for (i = 0; i < img->width*img->height;) {
195 if (pktlen == 0) {
196 pktdatap = pktdata;
197 std::memcpy(pktdatap, datap, pixelsize);
198 pktlen++;
199 i++;
200 pktdatap += pixelsize;
201 datap += pixelsize;
202 pkttype = RAW;
203 continue;
204 }
205 if (pktlen == 1) {
206 if (std::memcmp(datap - pixelsize, datap, pixelsize) == 0) {
207 pkttype = RLE;
208 }
209 }
210 if (pkttype == RLE) {
211 if (std::memcmp(datap - pixelsize, datap, pixelsize) != 0 || pktlen >= 128) {
212 RLE_write_pkt(f, pkttype, pktlen, pktdata, pixelsize);
213 actual += 1 + pixelsize;
214 perceived += pixelsize*pktlen;
215 pktlen = 0;
216 }
217 else {
218 pktlen++;
219 i++;
220 datap += pixelsize;
221 }
222 }
223 else {
224 if (std::memcmp(datap - pixelsize, datap, pixelsize) == 0 || pktlen >= 129) {
225 datap -= pixelsize; /* push back last pixel */
226 i--;
227 if (i < 0) std::fprintf(stderr, "BUG!\n");
228 pktlen--;
229 RLE_write_pkt(f, pkttype, pktlen, pktdata, pixelsize);
230 actual += 1 + pixelsize*pktlen;
231 perceived += pixelsize*pktlen;
232 pktlen = 0;
233 }
234 else {
235 std::memcpy(pktdatap, datap, pixelsize);
236 pktlen++;
237 i++;
238 pktdatap += pixelsize;
239 datap += pixelsize;
240 }
241 }
242 }
243 if (pktlen) {
244 RLE_write_pkt(f, pkttype, pktlen, pktdata, pixelsize);
245 if (pkttype == RLE) {
246 actual += 1 + pixelsize;
247 perceived += pixelsize*pktlen;
248 }
249 else {
250 actual += 1 + pixelsize*pktlen;
251 perceived += pixelsize*pktlen;
252 }
253 pktlen = 0;
254 }
255 std::fprintf(stderr, "RLE_compress: wrote %u bytes (%u uncompressed)\n", actual, perceived);
256 xfree((void*)pktdata);
257 return 0;
258 }
259
260 }
261
262 extern int getpixelsize(t_tgaimg const *img) {
263 switch (img->bpp) {
264 case 8:
265 return 1;
266 case 15:
267 case 16:
268 return 2;
269 case 24:
270 return 3;
271 case 32:
272 return 4;
273 default:
274 std::fprintf(stderr, "load_tga: color depth %u is not supported!\n", img->bpp);
275 return 0;
276 }
277 }
278
279
280 extern t_tgaimg * new_tgaimg(unsigned int width, unsigned int height, unsigned int bpp, t_tgaimgtype imgtype) {
281 t_tgaimg *img;
282
283 img = (t_tgaimg*)xmalloc(sizeof(t_tgaimg));
284 img->idlen = 0;
285 img->cmaptype = tgacmap_none;
286 img->imgtype = imgtype;
287 img->cmapfirst = 0;
288 img->cmaplen = 0;
289 img->cmapes = 0;
290 img->xorigin = 0;
291 img->yorigin = 0;
292 img->width = width;
293 img->height = height;
294 img->bpp = bpp;
295 img->desc = 0; /* no attribute bits, top, left, and zero reserved */
296 img->data = NULL;
297 img->extareaoff = 0;
298 img->devareaoff = 0;
299
300 return img;
301 }
302
303 extern t_tgaimg * load_tgaheader(void) {
304 t_tgaimg *img;
305
306 img = (t_tgaimg*)xmalloc(sizeof(t_tgaimg));
307 img->idlen = file_readb();
308 img->cmaptype = file_readb();
309 img->imgtype = file_readb();
310 img->cmapfirst = file_readw_le();
311 img->cmaplen = file_readw_le();
312 img->cmapes = file_readb();
313 img->xorigin = file_readw_le();
314 img->yorigin = file_readw_le();
315 img->width = file_readw_le();
316 img->height = file_readw_le();
317 img->bpp = file_readb();
318 img->desc = file_readb();
319 img->data = NULL;
320 img->extareaoff = 0; /* ignored when reading */
321 img->devareaoff = 0; /* ignored when reading */
322
323 return img;
324 }
325
326 extern t_tgaimg * load_tga(std::FILE *f) {
327 t_tgaimg *img;
328 int pixelsize;
329
330 file_rpush(f);
331 img = load_tgaheader();
332
333 /* make sure we understand the header fields */
334 if (img->cmaptype != tgacmap_none) {
335 std::fprintf(stderr, "load_tga: Color-mapped images are not (yet?) supported!\n");
336 xfree(img);
337 return NULL;
338 }
339 if (img->imgtype != tgaimgtype_uncompressed_truecolor && img->imgtype != tgaimgtype_rlecompressed_truecolor) {
340 std::fprintf(stderr, "load_tga: imagetype %u is not supported. (only 2 and 10 are supported)\n", img->imgtype);
341 xfree(img);
342 return NULL;
343 }
344
345 pixelsize = getpixelsize(img);
346 if (pixelsize == 0) {
347 xfree(img);
348 return NULL;
349 }
350 /* Skip the ID if there is one */
351 if (img->idlen > 0) {
352 std::fprintf(stderr, "load_tga: ID present, skipping %d bytes\n", img->idlen);
353 if (std::fseek(f, img->idlen, SEEK_CUR) < 0)
354 std::fprintf(stderr, "load_tga: could not seek %u bytes forward (std::fseek: %s)\n", img->idlen, std::strerror(errno));
355 }
356
357 /* Now, we can alloc img->data */
358 img->data = (std::uint8_t*)xmalloc(img->width*img->height*pixelsize);
359 if (img->imgtype == tgaimgtype_uncompressed_truecolor) {
360 if (std::fread(img->data, pixelsize, img->width*img->height, f) < (unsigned)(img->width*img->height)) {
361 std::fprintf(stderr, "load_tga: error while reading data!\n");
362 xfree(img->data);
363 xfree(img);
364 return NULL;
365 }
366 }
367 else { /* == tgaimgtype_rlecompressed_truecolor */
368 if (RLE_decompress(f, img->data, img->width*img->height*pixelsize, pixelsize) < 0) {
369 std::fprintf(stderr, "load_tga: error while decompressing data!\n");
370 xfree(img->data);
371 xfree(img);
372 return NULL;
373 }
374 }
375 file_rpop();
376 if ((img->desc & tgadesc_horz) == 1) { /* right, want left */
377 if (rotate_leftright(img) < 0) {
378 std::fprintf(stderr, "ERROR: rotate_leftright failed!\n");
379 }
380 }
381 if ((img->desc & tgadesc_vert) == 0) { /* bottom, want top */
382 if (rotate_updown(img) < 0) {
383 std::fprintf(stderr, "ERROR: rotate_updown failed!\n");
384 }
385 }
386 return img;
387 }
388
389 extern int write_tga(std::FILE *f, t_tgaimg *img) {
390 if (f == NULL) return -1;
391 if (img == NULL) return -1;
392 if (img->data == NULL) return -1;
393 if (img->idlen != 0) return -1;
394 if (img->cmaptype != tgacmap_none) return -1;
395 if (img->imgtype != tgaimgtype_uncompressed_truecolor && img->imgtype != tgaimgtype_rlecompressed_truecolor) return -1;
396 file_wpush(f);
397
398 file_writeb(img->idlen);
399 file_writeb(img->cmaptype);
400 file_writeb(img->imgtype);
401 file_writew_le(img->cmapfirst);
402 file_writew_le(img->cmaplen);
403 file_writeb(img->cmapes);
404 file_writew_le(img->xorigin);
405 file_writew_le(img->yorigin);
406 file_writew_le(img->width);
407 file_writew_le(img->height);
408 file_writeb(img->bpp);
409 file_writeb(img->desc);
410
411 if ((img->desc&tgadesc_horz) == 1) { /* right, want left */
412 std::fprintf(stderr, "write_tga: flipping horizontally\n");
413 if (rotate_leftright(img) < 0) {
414 std::fprintf(stderr, "ERROR: rotate_updown failed!\n");
415 }
416 }
417 if ((img->desc&tgadesc_vert) == 0) { /* bottom, want top */
418 std::fprintf(stderr, "write_tga: flipping vertically\n");
419 if (rotate_updown(img) < 0) {
420 std::fprintf(stderr, "ERROR: rotate_updown failed!\n");
421 }
422 }
423 if (img->imgtype == tgaimgtype_uncompressed_truecolor) {
424 int pixelsize;
425
426 pixelsize = getpixelsize(img);
427 if (pixelsize == 0) return -1;
428 if (std::fwrite(img->data, pixelsize, img->width*img->height, f) < (unsigned)(img->width*img->height)) {
429 std::fprintf(stderr, "write_tga: could not write %d pixels (std::fwrite: %s)\n", img->width*img->height, std::strerror(errno));
430 file_wpop();
431 return -1;
432 }
433 }
434 else if (img->imgtype == tgaimgtype_rlecompressed_truecolor) {
435 std::fprintf(stderr, "write_tga: using RLE compression\n");
436 if (RLE_compress(f, img) < 0) {
437 std::fprintf(stderr, "write_tga: RLE compression failed.\n");
438 }
439 }
440 /* Write the file-footer */
441 file_writed_le(img->extareaoff);
442 file_writed_le(img->devareaoff);
443 if (std::fwrite(TGAMAGIC, std::strlen(TGAMAGIC) + 1, 1, f) < 1)
444 std::fprintf(stderr, "write_tga: could not write TGA footer magic (std::fwrite: %s)\n", std::strerror(errno));
445 /* Ready */
446 file_wpop();
447 return 0;
448 }
449
450 extern void destroy_img(t_tgaimg * img) {
451 if (img == NULL) return;
452
453 if (img->data)
454 xfree(img->data);
455 xfree(img);
456 }
457
458 extern void print_tga_info(t_tgaimg const * img, std::FILE * fp) {
459 unsigned int interleave;
460 unsigned int attrbits;
461 char const * typestr;
462 char const * cmapstr;
463 char const * horzstr;
464 char const * vertstr;
465 char const * intlstr;
466
467 if (!img || !fp)
468 return;
469
470 interleave = ((img->desc&tgadesc_interleave1) != 0) * 2 + ((img->desc&tgadesc_interleave2) != 0);
471 attrbits = img->desc&(tgadesc_attrbits0 | tgadesc_attrbits1 | tgadesc_attrbits2 | tgadesc_attrbits3);
472 switch (img->imgtype) {
473 case tgaimgtype_empty:
474 typestr = "No Image Data Included";
475 break;
476 case tgaimgtype_uncompressed_mapped:
477 typestr = "Uncompressed, Color-mapped Image";
478 break;
479 case tgaimgtype_uncompressed_truecolor:
480 typestr = "Uncompressed, True-color Image";
481 break;
482 case tgaimgtype_uncompressed_monochrome:
483 typestr = "Uncompressed, Black-and-white image";
484 break;
485 case tgaimgtype_rlecompressed_mapped:
486 typestr = "Run-length encoded, Color-mapped Image";
487 break;
488 case tgaimgtype_rlecompressed_truecolor:
489 typestr = "Run-length encoded, True-color Image";
490 break;
491 case tgaimgtype_rlecompressed_monochrome:
492 typestr = "Run-length encoded, Black-and-white image";
493 break;
494 case tgaimgtype_huffman_mapped:
495 typestr = "Huffman encoded, Color-mapped image";
496 break;
497 case tgaimgtype_huffman_4pass_mapped:
498 typestr = "Four-pass Huffman encoded, Color-mapped image";
499 break;
500 default:
501 typestr = "unknown";
502 }
503 switch (img->cmaptype) {
504 case tgacmap_none:
505 cmapstr = "None";
506 break;
507 case tgacmap_included:
508 cmapstr = "Included";
509 break;
510 default:
511 cmapstr = "Unknown";
512 }
513 if ((img->desc&tgadesc_horz) == 0) {
514 horzstr = "left";
515 }
516 else {
517 horzstr = "right";
518 }
519 if ((img->desc&tgadesc_vert) == 0) {
520 vertstr = "bottom";
521 }
522 else {
523 vertstr = "top";
524 }
525 switch (interleave) {
526 case 0:
527 intlstr = "none";
528 break;
529 case 2:
530 intlstr = "two way";
531 break;
532 case 3:
533 intlstr = "four way";
534 break;
535 case 4:
536 default:
537 intlstr = "unknown";
538 break;
539 }
540
541 std::fprintf(fp, "TGAHeader: IDLength=%u ColorMapType=%u(%s)\n", img->idlen, img->cmaptype, cmapstr);
542 std::fprintf(fp, "TGAHeader: ImageType=%u(%s)\n", img->imgtype, typestr);
543 std::fprintf(fp, "TGAHeader: ColorMap: FirstEntryIndex=%u ColorMapLength=%u\n", img->cmapfirst, img->cmaplen);
544 std::fprintf(fp, "TGAHeader: ColorMap: ColorMapEntrySize=%ubits\n", img->cmapes);
545 std::fprintf(fp, "TGAHeader: X-origin=%u Y-origin=%u Width=%u(0x%x) Height=%u(0x%x)\n", img->xorigin, img->yorigin, img->width, img->width, img->height, img->height);
546 std::fprintf(fp, "TGAHeader: PixelDepth=%ubits ImageDescriptor=0x%02x(%u attribute bits, origin is %s %s, interleave=%s)\n", img->bpp, img->desc, attrbits, vertstr, horzstr, intlstr);
547 }
548
549 }
550
551 }