-
Notifications
You must be signed in to change notification settings - Fork 0
/
libtarga.cpp
1256 lines (880 loc) · 31.8 KB
/
libtarga.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdint>
#include "libtarga.h"
#include <algorithm>
/**************************************************************************
** Simplified TARGA library for Intro to Graphics Classes
**
** This is a simple library for reading and writing image files in
** the TARGA file format (which is a simple format).
** The routines are intentionally designed to be simple for use in
** into to graphics assignments - a more full-featured targa library
** also exists for other uses.
**
** This library was originally written by Alex Mohr who has assigned
** copyright to Michael Gleicher. The code is made available under an
** "MIT" Open Source license.
**/
/**
** Copyright (c) 2005 Michael L. Gleicher
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use, copy,
** modify, merge, publish, distribute, sublicense, and/or sell copies
** of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
**/
/*
** libtarga.c -- routines for reading targa files.
*/
/*
Modified by yu-chi because of initialization of variables at tga_load
09-16-2005
*/
#include <cstdio>
#include <malloc.h>
#define TGA_IMG_NODATA (0)
#define TGA_IMG_UNC_PALETTED (1)
#define TGA_IMG_UNC_TRUECOLOR (2)
#define TGA_IMG_UNC_GRAYSCALE (3)
#define TGA_IMG_RLE_PALETTED (9)
#define TGA_IMG_RLE_TRUECOLOR (10)
#define TGA_IMG_RLE_GRAYSCALE (11)
#define TGA_LOWER_LEFT (0)
#define TGA_LOWER_RIGHT (1)
#define TGA_UPPER_LEFT (2)
#define TGA_UPPER_RIGHT (3)
#define HDR_LENGTH (18)
#define HDR_IDLEN (0)
#define HDR_CMAP_TYPE (1)
#define HDR_IMAGE_TYPE (2)
#define HDR_CMAP_FIRST (3)
#define HDR_CMAP_LENGTH (5)
#define HDR_CMAP_ENTRY_SIZE (7)
#define HDR_IMG_SPEC_XORIGIN (8)
#define HDR_IMG_SPEC_YORIGIN (10)
#define HDR_IMG_SPEC_WIDTH (12)
#define HDR_IMG_SPEC_HEIGHT (14)
#define HDR_IMG_SPEC_PIX_DEPTH (16)
#define HDR_IMG_SPEC_IMG_DESC (17)
#define TGA_ERR_NONE (0)
#define TGA_ERR_BAD_HEADER (1)
#define TGA_ERR_OPEN_FAILS (2)
#define TGA_ERR_BAD_FORMAT (3)
#define TGA_ERR_UNEXPECTED_EOF (4)
#define TGA_ERR_NODATA_IMAGE (5)
#define TGA_ERR_COLORMAP_FOR_GRAY (6)
#define TGA_ERR_BAD_COLORMAP_ENTRY_SIZE (7)
#define TGA_ERR_BAD_COLORMAP (8)
#define TGA_ERR_READ_FAILS (9)
#define TGA_ERR_BAD_IMAGE_TYPE (10)
#define TGA_ERR_BAD_DIMENSIONS (11)
static unsigned int TargaError;
static int16_t ttohs( int16_t val );
static int16_t htots( int16_t val );
static int32_t ttohl( int32_t val );
static int32_t htotl( int32_t val );
static uint32_t tga_get_pixel( FILE * tga, ubyte bytes_per_pix,
ubyte * colormap, ubyte cmap_bytes_entry );
static uint32_t tga_convert_color( uint32_t pixel, uint32_t bpp_in, ubyte alphabits, uint32_t format_out );
static void tga_write_pixel_to_mem( ubyte * dat, ubyte img_spec, uint32_t number,
uint32_t w, uint32_t h, uint32_t pixel, uint32_t format );
/* returns the last error encountered */
int tga_get_last_error() {
return( TargaError );
}
/* returns a pointer to the string for an error code */
const char * tga_error_string( int error_code ) {
switch( error_code ) {
case TGA_ERR_NONE:
return( "no error" );
case TGA_ERR_BAD_HEADER:
return( "bad image header" );
case TGA_ERR_OPEN_FAILS:
return( "cannot open file" );
case TGA_ERR_BAD_FORMAT:
return( "bad format argument" );
case TGA_ERR_UNEXPECTED_EOF:
return( "unexpected end-of-file" );
case TGA_ERR_NODATA_IMAGE:
return( "image contains no data" );
case TGA_ERR_COLORMAP_FOR_GRAY:
return( "found colormap for a grayscale image" );
case TGA_ERR_BAD_COLORMAP_ENTRY_SIZE:
return( "unsupported colormap entry size" );
case TGA_ERR_BAD_COLORMAP:
return( "bad colormap" );
case TGA_ERR_READ_FAILS:
return( "cannot read from file" );
case TGA_ERR_BAD_IMAGE_TYPE:
return( "unknown image type" );
case TGA_ERR_BAD_DIMENSIONS:
return( "image has size 0 width or height (or both)" );
default:
return( "unknown error" );
}
// shut up compiler..
return( NULL );
}
/* creates a targa image of the desired format */
void * tga_create( int width, int height, unsigned int format ) {
switch( format ) {
case TGA_TRUECOLOR_32:
return( (void *)malloc( width * height * 4 ) );
case TGA_TRUECOLOR_24:
return( (void *)malloc( width * height * 3 ) );
default:
TargaError = TGA_ERR_BAD_FORMAT;
break;
}
return( NULL );
}
/* loads and converts a targa from disk */
void * tga_load( const char * filename,
int * width, int * height, unsigned int format ) {
ubyte idlen; // length of the image_id string below.
ubyte cmap_type; // paletted image <=> cmap_type
ubyte image_type; // can be any of the IMG_TYPE constants above.
uint16_t cmap_first; //
uint16_t cmap_length; // how long the colormap is
ubyte cmap_entry_size; // how big a palette entry is.
uint16_t img_spec_xorig; // the x origin of the image in the image data.
uint16_t img_spec_yorig; // the y origin of the image in the image data.
uint16_t img_spec_width; // the width of the image.
uint16_t img_spec_height; // the height of the image.
ubyte img_spec_pix_depth; // the depth of a pixel in the image.
ubyte img_spec_img_desc; // the image descriptor.
FILE * targafile;
ubyte * tga_hdr = NULL;
ubyte * colormap = NULL;
//***********************************************************************
// Add by Yu-Chi because of variable initialization.
// Add all = 0 to all the following variables
//***********************************************************************
ubyte cmap_bytes_entry = 0;
uint32_t cmap_bytes = 0;
uint32_t tmp_col = 0;
uint32_t tmp_int32_t = 0;
ubyte tmp_byte = 0;
ubyte alphabits = 0;
uint32_t num_pixels = 0;
uint32_t i = 0;
uint32_t j = 0;
ubyte * image_data = 0;
uint32_t img_dat_len = 0;
ubyte bytes_per_pix = 0;
ubyte true_bits_per_pixel = 0;
uint32_t bytes_total = 0;
ubyte packet_header = 0;
ubyte repcount = 0;
switch( format ) {
case TGA_TRUECOLOR_24:
case TGA_TRUECOLOR_32:
break;
default:
TargaError = TGA_ERR_BAD_FORMAT;
return( NULL );
}
/* open binary image file */
targafile = fopen( filename, "rb" );
if( targafile == NULL ) {
TargaError = TGA_ERR_OPEN_FAILS;
return( NULL );
}
/* allocate memory for the header */
tga_hdr = (ubyte *)malloc( HDR_LENGTH );
/* read the header in. */
if( fread( (void *)tga_hdr, 1, HDR_LENGTH, targafile ) != HDR_LENGTH ) {
free( tga_hdr );
TargaError = TGA_ERR_BAD_HEADER;
return( NULL );
}
/* byte order is important here. */
idlen = (ubyte)tga_hdr[HDR_IDLEN];
image_type = (ubyte)tga_hdr[HDR_IMAGE_TYPE];
cmap_type = (ubyte)tga_hdr[HDR_CMAP_TYPE];
cmap_first = ttohs( *(uint16_t *)(&tga_hdr[HDR_CMAP_FIRST]) );
cmap_length = ttohs( *(uint16_t *)(&tga_hdr[HDR_CMAP_LENGTH]) );
cmap_entry_size = (ubyte)tga_hdr[HDR_CMAP_ENTRY_SIZE];
img_spec_xorig = ttohs( *(uint16_t *)(&tga_hdr[HDR_IMG_SPEC_XORIGIN]) );
img_spec_yorig = ttohs( *(uint16_t *)(&tga_hdr[HDR_IMG_SPEC_YORIGIN]) );
img_spec_width = ttohs( *(uint16_t *)(&tga_hdr[HDR_IMG_SPEC_WIDTH]) );
img_spec_height = ttohs( *(uint16_t *)(&tga_hdr[HDR_IMG_SPEC_HEIGHT]) );
img_spec_pix_depth = (ubyte)tga_hdr[HDR_IMG_SPEC_PIX_DEPTH];
img_spec_img_desc = (ubyte)tga_hdr[HDR_IMG_SPEC_IMG_DESC];
free( tga_hdr );
num_pixels = img_spec_width * img_spec_height;
if( num_pixels == 0 ) {
TargaError = TGA_ERR_BAD_DIMENSIONS;
return( NULL );
}
alphabits = img_spec_img_desc & 0x0F;
/* seek past the image id, if there is one */
if( idlen ) {
if( fseek( targafile, idlen, SEEK_CUR ) ) {
TargaError = TGA_ERR_UNEXPECTED_EOF;
return( NULL );
}
}
/* if this is a 'nodata' image, just jump out. */
if( image_type == TGA_IMG_NODATA ) {
TargaError = TGA_ERR_NODATA_IMAGE;
return( NULL );
}
/* now we're starting to get into the meat of the matter. */
/* deal with the colormap, if there is one. */
if( cmap_type ) {
switch( image_type ) {
case TGA_IMG_UNC_PALETTED:
case TGA_IMG_RLE_PALETTED:
break;
case TGA_IMG_UNC_TRUECOLOR:
case TGA_IMG_RLE_TRUECOLOR:
// this should really be an error, but some really old
// crusty targas might actually be like this (created by TrueVision, no less!)
// so, we'll hack our way through it.
break;
case TGA_IMG_UNC_GRAYSCALE:
case TGA_IMG_RLE_GRAYSCALE:
TargaError = TGA_ERR_COLORMAP_FOR_GRAY;
return( NULL );
}
/* ensure colormap entry size is something we support */
if( !(cmap_entry_size == 15 ||
cmap_entry_size == 16 ||
cmap_entry_size == 24 ||
cmap_entry_size == 32) ) {
TargaError = TGA_ERR_BAD_COLORMAP_ENTRY_SIZE;
return( NULL );
}
/* allocate memory for a colormap */
if( cmap_entry_size & 0x07 ) {
cmap_bytes_entry = (((8 - (cmap_entry_size & 0x07)) + cmap_entry_size) >> 3);
} else {
cmap_bytes_entry = (cmap_entry_size >> 3);
}
cmap_bytes = cmap_bytes_entry * cmap_length;
colormap = (ubyte *)malloc( cmap_bytes );
for( i = 0; i < cmap_length; i++ ) {
/* seek ahead to first entry used */
if( cmap_first != 0 ) {
fseek( targafile, cmap_first * cmap_bytes_entry, SEEK_CUR );
}
tmp_int32_t = 0;
for( j = 0; j < cmap_bytes_entry; j++ ) {
if( !fread( &tmp_byte, 1, 1, targafile ) ) {
free( colormap );
TargaError = TGA_ERR_BAD_COLORMAP;
return( NULL );
}
tmp_int32_t += tmp_byte << (j * 8);
}
// byte order correct.
tmp_int32_t = ttohl( tmp_int32_t );
for( j = 0; j < cmap_bytes_entry; j++ ) {
colormap[i * cmap_bytes_entry + j] = (tmp_int32_t >> (8 * j)) & 0xFF;
}
}
}
// compute number of bytes in an image data unit (either index or BGR triple)
if( img_spec_pix_depth & 0x07 ) {
bytes_per_pix = (((8 - (img_spec_pix_depth & 0x07)) + img_spec_pix_depth) >> 3);
} else {
bytes_per_pix = (img_spec_pix_depth >> 3);
}
/* assume that there's one byte per pixel */
if( bytes_per_pix == 0 ) {
bytes_per_pix = 1;
}
/* compute how many bytes of storage we need for the image */
bytes_total = img_spec_width * img_spec_height * format;
image_data = (ubyte *)malloc( bytes_total );
img_dat_len = img_spec_width * img_spec_height * bytes_per_pix;
// compute the true number of bits per pixel
true_bits_per_pixel = cmap_type ? cmap_entry_size : img_spec_pix_depth;
switch( image_type ) {
case TGA_IMG_UNC_TRUECOLOR:
case TGA_IMG_UNC_GRAYSCALE:
case TGA_IMG_UNC_PALETTED:
/* FIXME: support grayscale */
for( i = 0; i < num_pixels; i++ ) {
// get the color value.
tmp_col = tga_get_pixel( targafile, bytes_per_pix, colormap, cmap_bytes_entry );
tmp_col = tga_convert_color( tmp_col, true_bits_per_pixel, alphabits, format );
// now write the data out.
tga_write_pixel_to_mem( image_data, img_spec_img_desc,
i, img_spec_width, img_spec_height, tmp_col, format );
}
break;
case TGA_IMG_RLE_TRUECOLOR:
case TGA_IMG_RLE_GRAYSCALE:
case TGA_IMG_RLE_PALETTED:
// FIXME: handle grayscale..
for( i = 0; i < num_pixels; ) {
/* a bit of work to do to read the data.. */
if( fread( &packet_header, 1, 1, targafile ) < 1 ) {
// well, just let them fill the rest with null pixels then...
packet_header = 1;
}
if( packet_header & 0x80 ) {
/* run length packet */
tmp_col = tga_get_pixel( targafile, bytes_per_pix, colormap, cmap_bytes_entry );
tmp_col = tga_convert_color( tmp_col, true_bits_per_pixel, alphabits, format );
repcount = (packet_header & 0x7F) + 1;
/* write all the data out */
for( j = 0; j < repcount; j++ ) {
tga_write_pixel_to_mem( image_data, img_spec_img_desc,
i + j, img_spec_width, img_spec_height, tmp_col, format );
}
i += repcount;
} else {
/* raw packet */
/* get pixel from file */
repcount = (packet_header & 0x7F) + 1;
for( j = 0; j < repcount; j++ ) {
tmp_col = tga_get_pixel( targafile, bytes_per_pix, colormap, cmap_bytes_entry );
tmp_col = tga_convert_color( tmp_col, true_bits_per_pixel, alphabits, format );
tga_write_pixel_to_mem( image_data, img_spec_img_desc,
i + j, img_spec_width, img_spec_height, tmp_col, format );
}
i += repcount;
}
}
break;
default:
TargaError = TGA_ERR_BAD_IMAGE_TYPE;
return( NULL );
}
fclose( targafile );
*width = img_spec_width;
*height = img_spec_height;
return( (void *)image_data );
}
int tga_write_raw( const char * file, int width, int height, unsigned char * dat, unsigned int format ) {
FILE * tga;
uint32_t i, j;
uint32_t size = width * height;
float red, green, blue, alpha;
char id[] = "written with libtarga";
ubyte idlen = 21;
ubyte zeroes[5] = { 0, 0, 0, 0, 0 };
uint32_t pixbuf;
ubyte one = 1;
ubyte cmap_type = 0;
ubyte img_type = 2; // 2 - uncompressed truecolor 10 - RLE truecolor
uint16_t xorigin = 0;
uint16_t yorigin = 0;
ubyte pixdepth = format * 8; // bpp
ubyte img_desc;
switch( format ) {
case TGA_TRUECOLOR_24:
img_desc = 0;
break;
case TGA_TRUECOLOR_32:
img_desc = 8;
break;
default:
TargaError = TGA_ERR_BAD_FORMAT;
return( 0 );
break;
}
tga = fopen( file, "wb" );
if( tga == NULL ) {
TargaError = TGA_ERR_OPEN_FAILS;
return( 0 );
}
// write id length
fwrite( &idlen, 1, 1, tga );
// write colormap type
fwrite( &cmap_type, 1, 1, tga );
// write image type
fwrite( &img_type, 1, 1, tga );
// write cmap spec.
fwrite( &zeroes, 5, 1, tga );
// write image spec.
fwrite( &xorigin, 2, 1, tga );
fwrite( &yorigin, 2, 1, tga );
fwrite( &width, 2, 1, tga );
fwrite( &height, 2, 1, tga );
fwrite( &pixdepth, 1, 1, tga );
fwrite( &img_desc, 1, 1, tga );
// write image id.
fwrite( &id, idlen, 1, tga );
// color correction -- data is in RGB, need BGR.
for( i = 0; i < size; i++ ) {
pixbuf = 0;
for( j = 0; j < format; j++ ) {
pixbuf += dat[i*format+j] << (8 * j);
}
switch( format ) {
case TGA_TRUECOLOR_24:
pixbuf = std::reverse((char*)pixbuf,(char*)pixbuf +3);
pixbuf = htotl( pixbuf );
fwrite( &pixbuf, 3, 1, tga );
break;
case TGA_TRUECOLOR_32:
/* need to un-premultiply alpha.. */
red = (pixbuf & 0xFF) / 255.0f;
green = ((pixbuf & 0xFF00) >> 8) / 255.0f;
blue = ((pixbuf & 0xFF0000) >> 16) / 255.0f;
alpha = ((pixbuf & 0xFF000000) >> 24) / 255.0f;
if( alpha > 0.0001 ) {
red /= alpha;
green /= alpha;
blue /= alpha;
}
/* clamp to 1.0f */
red = red > 1.0f ? 255.0f : red * 255.0f;
green = green > 1.0f ? 255.0f : green * 255.0f;
blue = blue > 1.0f ? 255.0f : blue * 255.0f;
alpha = alpha > 1.0f ? 255.0f : alpha * 255.0f;
pixbuf = (ubyte)blue + (((ubyte)green) << 8) +
(((ubyte)red) << 16) + (((ubyte)alpha) << 24);
pixbuf = htotl( pixbuf );
fwrite( &pixbuf, 4, 1, tga );
break;
}
}
fclose( tga );
return( 1 );
}
int tga_write_rle( const char * file, int width, int height, unsigned char * dat, unsigned int format ) {
FILE * tga;
uint32_t i, j;
uint32_t oc, nc;
enum RLE_STATE { INIT, NONE, RLP, RAWP };
int state = INIT;
uint32_t size = width * height;
uint16_t shortwidth = (uint16_t)width;
uint16_t shortheight = (uint16_t)height;
ubyte repcount;
float red, green, blue, alpha;
int idx, row, column;
// have to buffer a whole line for raw packets.
unsigned char * rawbuf = (unsigned char *)malloc( width * format );
char id[] = "written with libtarga";
ubyte idlen = 21;
ubyte zeroes[5] = { 0, 0, 0, 0, 0 };
uint32_t pixbuf;
ubyte one = 1;
ubyte cmap_type = 0;
ubyte img_type = 10; // 2 - uncompressed truecolor 10 - RLE truecolor
uint16_t xorigin = 0;
uint16_t yorigin = 0;
ubyte pixdepth = format * 8; // bpp
ubyte img_desc = format == TGA_TRUECOLOR_32 ? 8 : 0;
switch( format ) {
case TGA_TRUECOLOR_24:
case TGA_TRUECOLOR_32:
break;
default:
TargaError = TGA_ERR_BAD_FORMAT;
return( 0 );
}
tga = fopen( file, "wb" );
if( tga == NULL ) {
TargaError = TGA_ERR_OPEN_FAILS;
return( 0 );
}
// write id length
fwrite( &idlen, 1, 1, tga );
// write colormap type
fwrite( &cmap_type, 1, 1, tga );
// write image type
fwrite( &img_type, 1, 1, tga );
// write cmap spec.
fwrite( &zeroes, 5, 1, tga );
// write image spec.
fwrite( &xorigin, 2, 1, tga );
fwrite( &yorigin, 2, 1, tga );
fwrite( &shortwidth, 2, 1, tga );
fwrite( &shortheight, 2, 1, tga );
fwrite( &pixdepth, 1, 1, tga );
fwrite( &img_desc, 1, 1, tga );
// write image id.
fwrite( &id, idlen, 1, tga );
// initial color values -- just to shut up the compiler.
nc = 0;
// color correction -- data is in RGB, need BGR.
// also run-length-encoding.
for( i = 0; i < size; i++ ) {
idx = i * format;
row = i / width;
column = i % width;
//printf( "row: %d, col: %d\n", row, column );
pixbuf = 0;
for( j = 0; j < format; j++ ) {
pixbuf += dat[idx+j] << (8 * j);
}
switch( format ) {
case TGA_TRUECOLOR_24:
pixbuf = ((pixbuf & 0xFF) << 16) +
(pixbuf & 0xFF00) +
((pixbuf & 0xFF0000) >> 16);
pixbuf = htotl( pixbuf );
break;
case TGA_TRUECOLOR_32:
/* need to un-premultiply alpha.. */
red = (pixbuf & 0xFF) / 255.0f;
green = ((pixbuf & 0xFF00) >> 8) / 255.0f;
blue = ((pixbuf & 0xFF0000) >> 16) / 255.0f;
alpha = ((pixbuf & 0xFF000000) >> 24) / 255.0f;
if( alpha > 0.0001 ) {
red /= alpha;
green /= alpha;
blue /= alpha;
}
/* clamp to 1.0f */
red = red > 1.0f ? 255.0f : red * 255.0f;
green = green > 1.0f ? 255.0f : green * 255.0f;
blue = blue > 1.0f ? 255.0f : blue * 255.0f;
alpha = alpha > 1.0f ? 255.0f : alpha * 255.0f;
pixbuf = (ubyte)blue + (((ubyte)green) << 8) +
(((ubyte)red) << 16) + (((ubyte)alpha) << 24);
pixbuf = htotl( pixbuf );
break;
}
oc = nc;
nc = pixbuf;
switch( state ) {
case INIT:
// this is just used to make sure we have 2 pixel values to consider.
state = NONE;
break;
case NONE:
if( column == 0 ) {
// write a 1 pixel raw packet for the old pixel, then go thru again.
repcount = 0;
fwrite( &repcount, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
state = NONE;
break;
}
if( nc == oc ) {
repcount = 0;
state = RLP;
} else {
repcount = 0;
state = RAWP;
for( j = 0; j < format; j++ ) {
#ifdef WORDS_BIGENDIAN
rawbuf[(repcount * format) + j] = (ubyte)(*((&oc)+format-j-1));
#else
rawbuf[(repcount * format) + j] = *(((ubyte *)(&oc)) + j);
#endif
}
}
break;
case RLP:
repcount++;
if( column == 0 ) {
// finish off rlp.
repcount |= 0x80;
fwrite( &repcount, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
state = NONE;
break;
}
if( repcount == 127 ) {
// finish off rlp.
repcount |= 0x80;
fwrite( &repcount, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
state = NONE;
break;
}
if( nc != oc ) {
// finish off rlp
repcount |= 0x80;
fwrite( &repcount, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
state = NONE;
}
break;
case RAWP:
repcount++;
if( column == 0 ) {
// finish off rawp.
for( j = 0; j < format; j++ ) {
#ifdef WORDS_BIGENDIAN
rawbuf[(repcount * format) + j] = (ubyte)(*((&oc)+format-j-1));
#else
rawbuf[(repcount * format) + j] = *(((ubyte *)(&oc)) + j);
#endif
}
fwrite( &repcount, 1, 1, tga );
fwrite( rawbuf, (repcount + 1) * format, 1, tga );
state = NONE;
break;
}
if( repcount == 127 ) {
// finish off rawp.
for( j = 0; j < format; j++ ) {
#ifdef WORDS_BIGENDIAN
rawbuf[(repcount * format) + j] = (ubyte)(*((&oc)+format-j-1));
#else
rawbuf[(repcount * format) + j] = *(((ubyte *)(&oc)) + j);
#endif
}
fwrite( &repcount, 1, 1, tga );
fwrite( rawbuf, (repcount + 1) * format, 1, tga );
state = NONE;
break;
}
if( nc == oc ) {
// finish off rawp
repcount--;
fwrite( &repcount, 1, 1, tga );
fwrite( rawbuf, (repcount + 1) * format, 1, tga );
// start new rlp
repcount = 0;
state = RLP;
break;
}
// continue making rawp
for( j = 0; j < format; j++ ) {
#ifdef WORDS_BIGENDIAN
rawbuf[(repcount * format) + j] = (ubyte)(*((&oc)+format-j-1));
#else
rawbuf[(repcount * format) + j] = *(((ubyte *)(&oc)) + j);
#endif
}
break;
}
}
// clean up state.
switch( state ) {
case INIT:
break;
case NONE:
// write the last 2 pixels in a raw packet.
fwrite( &one, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
#ifdef WORDS_BIGENDIAN
fwrite( (&nc)+4, format, 1, tga ); // byte order..
#else
fwrite( &nc, format, 1, tga );
#endif
break;
case RLP:
repcount++;
repcount |= 0x80;
fwrite( &repcount, 1, 1, tga );
#ifdef WORDS_BIGENDIAN
fwrite( (&oc)+4, format, 1, tga ); // byte order..
#else
fwrite( &oc, format, 1, tga );
#endif
break;
case RAWP:
repcount++;
for( j = 0; j < format; j++ ) {
#ifdef WORDS_BIGENDIAN
rawbuf[(repcount * format) + j] = (ubyte)(*((&oc)+format-j-1));
#else
rawbuf[(repcount * format) + j] = *(((ubyte *)(&oc)) + j);
#endif
}
fwrite( &repcount, 1, 1, tga );
fwrite( rawbuf, (repcount + 1) * 3, 1, tga );