summaryrefslogtreecommitdiff
path: root/im/src/im_format_tga.cpp
blob: 86e968aede96b8588a463cc00aea05cc9133d043 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
/** \file
 * \brief TGA - Truevision Graphics Adapter File
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_format_tga.cpp,v 1.2 2008/12/03 15:45:34 scuri Exp $
 */

#include "im_format.h"
#include "im_util.h"
#include "im_format_all.h"
#include "im_counter.h"
#include "im_math.h"

#include "im_binfile.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include <math.h>


/*
|--------|--------|------------------------------------------------------------|
|    0   |     1  |  Number of Characters in Identification Field.             |
|        |        |  This field is a one-byte unsigned integer, specifying     |
|        |        |  the length of the Image Identification Field.  Its range  |
|        |        |  is 0 to 255.  A value of 0 means that no Image            |
|        |        |  Identification Field is included.                         |
|--------|--------|------------------------------------------------------------|
|    1   |     1  |  Color Map Type.                                           |
|--------|--------|------------------------------------------------------------|
|    2   |     1  |  Image Type Code.                                          |
|--------|--------|------------------------------------------------------------|
|    3   |     5  |  Color Map Specification.                                  |
|    3   |     2  |  Color Map Origin.                                         |
|        |        |  Integer ( lo-hi ) index of first color map entry.         |
|    5   |     2  |  Color Map Length.                                         |
|        |        |  Integer ( lo-hi ) count of color map entries.             |
|    7   |     1  |  Color Map Entry Size.                                     |
|        |        |  Number of bits in each color map entry.  16 for           |
|        |        |  the Targa 16, 24 for the Targa 24, 32 for the Targa 32.   |
|--------|--------|------------------------------------------------------------|
|    8   |    10  |  Image Specification.                                      |
|    8   |     2  |  X Origin of Image.                                        |
|        |        |  Integer ( lo-hi ) X coordinate of the lower left corner   |
|        |        |  of the image.                                             |
|   10   |     2  |  Y Origin of Image.                                        |
|        |        |  Integer ( lo-hi ) Y coordinate of the lower left corner   |
|        |        |  of the image.                                             |
|   12   |     2  |  Width of Image.                                           |
|        |        |  Integer ( lo-hi ) width of the image in pixels.           |
|   14   |     2  |  Height of Image.                                          |
|        |        |  Integer ( lo-hi ) height of the image in pixels.          |
|   16   |     1  |  Image Pixel Size.                                         |
|        |        |  Number of bits in a stored pixel index.                   |
|   17   |     1  |  Image Descriptor Byte.                                    |
|        |        |  Bits 3-0 - number of attribute bits associated with each  |
|        |        |             pixel.                                         |
|        |        |  Bit 4    - reserved.  Must be set to 0.                   |
|        |        |  Bit 5    - screen origin bit.                             |
|        |        |             0 = Origin in lower left-hand corner.          |
|        |        |             1 = Origin in upper left-hand corner.          |
|        |        |             Must be 0 for Truevision images.               |
|        |        |  Bits 7-6 - Data storage interleaving flag.                |
|        |        |             00 = non-interleaved.                          |
|        |        |             01 = two-way (even/odd) interleaving.          |
|        |        |             10 = four way interleaving.                    |
|        |        |             11 = reserved.                                 |
|        |        |  This entire byte should be set to 0.  Don't ask me.       |
|--------|--------|------------------------------------------------------------|
|   18   | varies |  Image Identification Field.                               |
|        |        |  Contains a free-form identification field of the length   |
|        |        |  specified in byte 1 of the image record.  It's usually    |
|        |        |  omitted ( length in byte 1 = 0 ), but can be up to 255    |
|        |        |  characters.  If more identification information is        |
|        |        |  required, it can be stored after the image data.          |
|--------|--------|------------------------------------------------------------|

Extension Area:

* The inclusion of a scaled-down “postage stamp” copy of the image
* Date and Time of image file creation
* Author Name
* Author Comments
* Job Name
* Job Accumulated Time
* Gamma Value
* Correct Color LUT
* Pixel Aspect Ratio
* Scan Line Offset Table
* Key Color
* Software Package Name and Version Number
* Developer Definable Areas
* Attribute (Alpha) channel Type
* The ability for simple expansion
*/

static int iTGADecodeScanLine(imBinFile* handle, imbyte *DecodedBuffer, int width, int pixel_size)
{
  int i=0;
  unsigned char runcount; /* repetition count field */
  imbyte pixel_buffer[4];
  
  while (i < width) 
  { 
    imBinFileRead(handle, &runcount, 1, 1);  

    if (runcount & 0x80)
    { 
      imBinFileRead(handle, pixel_buffer, pixel_size, 1); 
      runcount &= 0x7F;

      if (imBinFileError(handle))
        return IM_ERR_ACCESS;     

      runcount++;      
      while (runcount-- && i < width)
      {
        memcpy(DecodedBuffer, pixel_buffer, pixel_size);
        i++;
        DecodedBuffer += pixel_size;
      }
    } 
    else
    { 
      runcount++;
      while (runcount-- && i < width)
      {
        imBinFileRead(handle, pixel_buffer, pixel_size, 1);
        memcpy(DecodedBuffer, pixel_buffer, pixel_size);
        i++;
        DecodedBuffer += pixel_size;
      }

      if (imBinFileError(handle))
        return IM_ERR_ACCESS;     
    }
  }

  return IM_ERR_NONE;
}

static inline int iTGAEqualPixel(const imbyte* Buffer1, const imbyte* Buffer2, int pixel_size)
{
  while(pixel_size--)
  {
    if (*Buffer1++ != *Buffer2++)
      return 0;
  }
  return 1;
}

static int iTGAEncodeScanLine(imbyte* EncodedBuffer, const imbyte* DecodedBuffer, int width, int pixel_size)
{
  imbyte pixel_buffer[4];
  unsigned char runcount; /* Length of encoded pixel run          */
  int x = 0;              /* Index into uncompressed data buffer  */
  imbyte* StartBuffer = EncodedBuffer;

  while (x < width)
  {
    runcount = 1;
    memcpy(pixel_buffer, &DecodedBuffer[x*pixel_size], pixel_size);

    // count equal pixels
    while (x+runcount < width && runcount < 128 && 
           iTGAEqualPixel(pixel_buffer, &DecodedBuffer[(x+runcount)*pixel_size], pixel_size))
      runcount++; 

    if (runcount == 1)
    {
      // count different pixels
      while (x+runcount+1 < width && runcount < 128)
      {
        memcpy(pixel_buffer, &DecodedBuffer[(x+runcount)*pixel_size], pixel_size);

        if (!iTGAEqualPixel(pixel_buffer, &DecodedBuffer[(x+runcount+1)*pixel_size], pixel_size))
          runcount++; 
        else
          break;
      }

      *EncodedBuffer++ = (imbyte)(runcount-1);

      memcpy(EncodedBuffer, &DecodedBuffer[x*pixel_size], runcount*pixel_size);
      EncodedBuffer += runcount*pixel_size;
    }
    else
    {
      *EncodedBuffer++ = (imbyte)(0x80 | (runcount-1));

      memcpy(EncodedBuffer, pixel_buffer, pixel_size);
      EncodedBuffer += pixel_size;
    }

    x += runcount;
  }

  return EncodedBuffer-StartBuffer;      /* Return the number of unsigned chars written to buffer */
}

static const char* iTGACompTable[2] = 
{
  "NONE",
  "RLE"
};

class imFileFormatTGA: public imFileFormatBase
{
  imBinFile* handle;          /* the binary file handle */
  unsigned char id_lenght;
  unsigned char map_type, image_type, map_bpp, bpp;

  int ReadPalette();
  int WritePalette();
  void FixRGB();
  int LoadExtensionArea();
  int SaveExtensionArea();

public:
  imFileFormatTGA(const imFormat* _iformat): imFileFormatBase(_iformat) {}
  ~imFileFormatTGA() {}

  int Open(const char* file_name);
  int New(const char* file_name);
  void Close();
  void* Handle(int index);
  int ReadImageInfo(int index);
  int ReadImageData(void* data);
  int WriteImageInfo();
  int WriteImageData(void* data);
};

class imFormatTGA: public imFormat
{
public:
  imFormatTGA()
    :imFormat("TGA", 
              "Truevision Graphics Adapter File", 
              "*.tga;*.icb;*.vst;*.tpic;", 
              iTGACompTable, 
              2, 
              0)
    {}
  ~imFormatTGA() {}

  imFileFormatBase* Create(void) const { return new imFileFormatTGA(this); }
  int CanWrite(const char* compression, int color_mode, int data_type) const;
};

void imFormatRegisterTGA(void)
{
  imFormatRegister(new imFormatTGA());
}

int imFileFormatTGA::Open(const char* file_name)
{
  /* opens the binary file for reading with intel byte order */
  handle = imBinFileOpen(file_name);
  if (!handle)
    return IM_ERR_OPEN;

  imBinFileByteOrder(handle, IM_LITTLEENDIAN); 
  
  imBinFileRead(handle, &this->id_lenght, 1, 1);
  imBinFileRead(handle, &this->map_type, 1, 1);
  imBinFileRead(handle, &this->image_type, 1, 1);

  if (imBinFileError(handle))
  {
    imBinFileClose(handle);
    return IM_ERR_ACCESS;
  }
  
  if (this->image_type != 1 && this->image_type != 2 && this->image_type != 3 && 
      this->image_type != 9 && this->image_type != 10 && this->image_type != 11)
  {
    imBinFileClose(handle);
    return IM_ERR_FORMAT;
  }
  
  if (this->map_type != 0 && this->map_type != 1)
  {
    imBinFileClose(handle);
    return IM_ERR_FORMAT;
  }
  
  if (this->map_type == 0 && (this->image_type == 1 || this->image_type == 9))
  {
    imBinFileClose(handle);
    return IM_ERR_FORMAT;
  }
  
  if (this->image_type == 9 || this->image_type == 10 || this->image_type == 11)
    strcpy(this->compression, "RLE");
  else
    strcpy(this->compression, "NONE");

  this->image_count = 1;

  return IM_ERR_NONE;
}

int imFileFormatTGA::New(const char* file_name)
{
  /* opens the binary file for writing with intel byte order */
  handle = imBinFileNew(file_name);
  if (!handle)
    return IM_ERR_OPEN;

  imBinFileByteOrder(handle, IM_LITTLEENDIAN); 

  return IM_ERR_NONE;
}

void imFileFormatTGA::Close()
{
  imBinFileClose(handle);
}

void* imFileFormatTGA::Handle(int index)
{
  if (index == 0)
    return (void*)this->handle;
  else
    return NULL;
}

int imFileFormatTGA::ReadImageInfo(int index)
{
  (void)index;
  unsigned char byte_value;
  unsigned short word_value;

  this->file_data_type = IM_BYTE;
  
  if (this->image_type == 1 || this->image_type == 9)
    this->file_color_mode = IM_MAP;
  else if (this->image_type == 2 || this->image_type == 10)
  {
    this->file_color_mode = IM_RGB;
    this->file_color_mode |= IM_PACKED;
  }
  else if (this->image_type == 3 || this->image_type == 11)
    this->file_color_mode = IM_GRAY;
  else
    return IM_ERR_DATA;

  if (this->map_type == 0)
    imBinFileSeekOffset(handle, 5);  // jump color map information
  else
  {
    /* jump 2 bytes (first entry index) */
    imBinFileSeekOffset(handle, 2);
    
    imBinFileRead(handle, &word_value, 1, 2);
    this->palette_count = word_value;
    
    imBinFileRead(handle, &this->map_bpp, 1, 1);

    if (this->map_bpp == 15) this->map_bpp = 16;

	  if (this->map_bpp != 16 && this->map_bpp != 24 && this->map_bpp != 32)
      return IM_ERR_DATA;
  }
  
  /* jump 4 bytes (X-Origin, Y-Origin) */
  unsigned short xmin, ymin;
  imBinFileRead(handle, &xmin, 1, 2);
  imBinFileRead(handle, &ymin, 1, 2);

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  imAttribTable* attrib_table = AttribTable();

  if (xmin && ymin)
  {
    attrib_table->Set("XScreen", IM_USHORT, 1, &xmin);
    attrib_table->Set("YScreen", IM_USHORT, 1, &ymin);
  }
  
  /* reads the image width */
  imBinFileRead(handle, &word_value, 1, 2);
  this->width = word_value;
  
  /* reads the image height */
  imBinFileRead(handle, &word_value, 1, 2);
  this->height = word_value;

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;
  
  imBinFileRead(handle, &this->bpp, 1, 1);
  
  if (this->bpp > 8 && imColorModeSpace(this->file_color_mode) != IM_RGB)
    return IM_ERR_DATA;
  
  if (this->bpp == 15) this->bpp = 16;

	if (this->bpp != 8 && this->bpp != 16 && 
      this->bpp != 24 && this->bpp != 32)
    return IM_ERR_DATA;

  if (this->bpp == 32)
    this->file_color_mode |= IM_ALPHA;

  // image descriptor
  imBinFileRead(handle, &byte_value, 1, 1);
  
  if (byte_value & 0x20)
    this->file_color_mode |= IM_TOPDOWN;
  
  // image ID
  if (this->id_lenght)
  {
    char desc[256];
    imBinFileRead(handle, desc, this->id_lenght, 1);
    desc[this->id_lenght] = 0;
    attrib_table->Set("Title", IM_BYTE, this->id_lenght+1, desc);
  }
  
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;
  
  if (this->map_type)
  {
    if (!ReadPalette())
      return IM_ERR_ACCESS;
  }

  long cur_offset = imBinFileTell(handle);
  imBinFileSeekFrom(handle, -18);  
  char ext_sig[18];
  imBinFileRead(handle, ext_sig, 18, 1);
  if (ext_sig[17] == 0 && imStrEqual(ext_sig, "TRUEVISION-XFILE."))
  {
    if (!LoadExtensionArea())
      return IM_ERR_ACCESS;
  }
  imBinFileSeekTo(handle, cur_offset);  
  
  return IM_ERR_NONE;
}

int imFileFormatTGA::WriteImageInfo()
{
  unsigned char byte_value;
  unsigned short word_value;

  this->map_bpp = 0;
  this->map_type = 0;

  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  switch (this->file_color_mode)
  {
  case IM_BINARY:
    this->convert_bpp = -1; // expand 1 to 255
  case IM_GRAY:
    this->bpp = 8;
    if (imStrEqual(this->compression, "RLE"))
      this->image_type = 11;
    else
      this->image_type = 3;
    break;
  case IM_MAP:
    this->bpp = 8;
    this->map_bpp = 24;
    this->map_type = 1;
    if (imStrEqual(this->compression, "RLE"))
      this->image_type = 9;
    else
      this->image_type = 1;
    break;
  case IM_RGB:
    this->bpp = 24;
    this->file_color_mode |= IM_PACKED;
    if (imStrEqual(this->compression, "RLE"))
      this->image_type = 10;
    else
      this->image_type = 2;
    break;
  }

  if (this->image_type > 3)
  {
    // allocates more than enough since compression algoritm can be ineficient
    this->line_buffer_extra += 2*this->width*imColorModeDepth(this->file_color_mode);
  }
  
  imAttribTable* attrib_table = AttribTable();

  /* writes the TGA file header */

  int length = 0;
  const char* desc_attrib = (const char*)attrib_table->Get("Title", NULL, &length);
  if (desc_attrib)
  {
    if (length > 255)
      this->id_lenght = 255;
    else
      this->id_lenght = (imbyte)length;
  }
  else
    this->id_lenght = 0;
  
  /* IDLength */
  imBinFileWrite(handle, &this->id_lenght, 1, 1); 
  
  /* Color Map Type */
  imBinFileWrite(handle, &this->map_type, 1, 1);
  
  /* Image Type */
  imBinFileWrite(handle, &this->image_type, 1, 1);
  
  /* Color Map Specification - 1st entry index */
  word_value = 0;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Color map length */
  word_value = (unsigned short) this->palette_count;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Color Map Entry size */
  byte_value = this->map_type? this->map_bpp: (imbyte)0;
  imBinFileWrite(handle, &byte_value, 1, 1); 

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;
  
  unsigned short xmin = 0, ymin = 0;
  const void* attrib_data = attrib_table->Get("XScreen");
  if (attrib_data) xmin = *(unsigned short*)attrib_data;
  attrib_data = attrib_table->Get("YScreen");
  if (attrib_data) ymin = *(unsigned short*)attrib_data;

  /* X-orign of image */
  word_value = xmin;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Y-orign of image */
  word_value = ymin;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Image Width */
  word_value = (imushort)this->width;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Image Height */
  word_value = (imushort)this->height;
  imBinFileWrite(handle, &word_value, 1, 2); 
  
  /* Pixel Depth */
  imBinFileWrite(handle, &this->bpp, 1, 1);  
  
  /* Image Descriptor */
  byte_value = 0x00;
  imBinFileWrite(handle, &byte_value, 1, 1);  
  
  /* image ID */
  if (this->id_lenght)
  {
    if (length > 255)
    {
      imBinFileWrite(handle, (void*)desc_attrib, 254, 1);  
      byte_value = 0x00;
      imBinFileWrite(handle, &byte_value, 1, 1);  
    }
    else
      imBinFileWrite(handle, (void*)desc_attrib, this->id_lenght, 1);  
  }
  
  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->map_type)
  {
    if (!WritePalette())
      return IM_ERR_ACCESS;
  }
  
  return IM_ERR_NONE;
}

static long iTGARGB2Color(int c, unsigned char *colors, int map_bpp)
{
  unsigned char r,g,b;
  
  if (map_bpp == 16)
  {
    unsigned short word_value = ((unsigned short*)colors)[c];
    
    r = (imbyte)(((word_value & 0x7C00) >> 10)*8);
    g = (imbyte)(((word_value & 0x03E0) >>  5)*8);
    b = (imbyte)( (word_value & 0x001F)       *8);
  }
  else // 24 or 32
  {
    int i = c * (map_bpp / 8);
    
    r = colors[i+2];
    g = colors[i+1];
    b = colors[i];
  }
  
  return imColorEncode(r, g, b);
}

int imFileFormatTGA::ReadPalette()
{
  int map_size = imFileLineSizeAligned(this->palette_count, this->map_bpp, 1);
  unsigned char* tga_colors = (unsigned char*) malloc(map_size);
  
  /* reads the color palette */
  imBinFileRead(handle, tga_colors, map_size, 1);
  if (imBinFileError(handle))
    return 0;

  if (imBinCPUByteOrder() == IM_BIGENDIAN && this->map_bpp == 16)
	  imBinSwapBytes2(tga_colors, map_size/2);
  
  /* convert the color map to the IM format */
  for (int c = 0; c < this->palette_count; c++)
    this->palette[c] = iTGARGB2Color(c, tga_colors, this->map_bpp);
  
  free(tga_colors);
  
  return 1;
}

int imFileFormatTGA::WritePalette()
{
  unsigned char tga_color[256*3];
  
  /* convert the color map from the IM format */
  for (int c = 0; c < this->palette_count; c++)
  {
    int i = 3*c;
    imColorDecode(&tga_color[i+2], &tga_color[i+1], &tga_color[i], this->palette[c]);
  }
  
  /* writes the color palette */
  imBinFileWrite(handle, tga_color, this->palette_count * 3, 1);

  if (imBinFileError(handle))
    return 0;
  
  return 1;
}

int imFileFormatTGA::LoadExtensionArea()
{
  unsigned int dword_value;
  imBinFileSeekFrom(handle, -26);  

  // extension offset
  imBinFileRead(handle, &dword_value, 1, 4);
  if (imBinFileError(handle))
    return 0;

  imBinFileSeekTo(handle, dword_value);  
  if (imBinFileError(handle))
    return 0;

  unsigned short word_value;
  imbyte buffer[512];
  imAttribTable* attrib_table = AttribTable();

  // extension size
  imBinFileSeekOffset(handle, 2);  

  // author name
  imBinFileRead(handle, buffer, 41, 1);
  if (buffer[0] != 0)
    attrib_table->Set("Author", IM_BYTE, imStrNLen((char*)buffer, 41)+1, buffer);

  // author comments
  imBinFileRead(handle, buffer, 324, 1);
  if (buffer[0] != 0)
  {
    int size1 = imStrNLen((char*)buffer, 81);
    for (int i = 1; i < 4; i++)
    {
      int sizei = imStrNLen((char*)buffer + i*81, 81);
      if (sizei) 
      {
        memcpy(buffer + size1, buffer + i*81, sizei);
        size1 += sizei;
      }
    }
    buffer[size1] = 0;

    attrib_table->Set("Description", IM_BYTE, size1+1, buffer);
  }

  if (imBinFileError(handle))
    return 0;

  {
    tm ttm;
    ttm.tm_wday = 0;
    ttm.tm_yday = 0;
    ttm.tm_isdst = -1;

    int valid = 0;
    imBinFileRead(handle, &word_value, 1, 2); // moth
    ttm.tm_mon = word_value-1;
    if (word_value) valid = 1;
    imBinFileRead(handle, &word_value, 1, 2); // day
    ttm.tm_mday = word_value;
    if (word_value) valid = 1;
    imBinFileRead(handle, &word_value, 1, 2); // year
    ttm.tm_year = word_value-1900;
    if (word_value) valid = 1;
    imBinFileRead(handle, &word_value, 1, 2); // hour
    ttm.tm_hour = word_value;
    imBinFileRead(handle, &word_value, 1, 2); // minute
    ttm.tm_min = word_value;
    imBinFileRead(handle, &word_value, 1, 2); // seconds
    ttm.tm_sec = word_value;

    if (imBinFileError(handle))
      return 0;

    if (valid)
    {
      time_t tt = mktime(&ttm);
      char* str = ctime(&tt);
      if (str) 
      {
        int size = strlen(str);
        str[size-1] = 0;   // remove "\n"
        attrib_table->Set("DateTimeModified", IM_BYTE, size, str);
      }
    }
  }

  // job name
  imBinFileRead(handle, buffer, 41, 1);
  if (buffer[0] != 0)
    attrib_table->Set("JobName", IM_BYTE, imStrNLen((char*)buffer, 41)+1, buffer);

  // job time
  imBinFileSeekOffset(handle, 6);  

  // Software
  imBinFileRead(handle, buffer, 41, 1);
  if (buffer[0] != 0)
    attrib_table->Set("Software", IM_BYTE, imStrNLen((char*)buffer, 41)+1, buffer);

  if (imBinFileError(handle))
    return 0;

  // Software Version
  imBinFileRead(handle, &word_value, 1, 2);
  if (word_value)
  {
    int size = sprintf((char*)buffer, "%f", (double)word_value / 100.0);
    imBinFileRead(handle, &buffer[size], 1, 1);
    buffer[size+1] = 0;
    attrib_table->Set("SoftwareVersion", IM_BYTE, size+1, buffer);
  }

  // key color, aspect ratio
  imBinFileSeekOffset(handle, 8); 

  // gamma
  imBinFileRead(handle, &word_value, 1, 2); // num
  if (word_value)
  {
    float gamma = (float)word_value;
    imBinFileRead(handle, &word_value, 1, 2); // den
    if (word_value)
    {
      gamma /= (float)word_value;
      attrib_table->Set("Gamma", IM_FLOAT, 1, &gamma);
    }
  }

  if (imBinFileError(handle))
    return 0;

  return 1;
}

static void iGetRational(float fvalue, int *num, int *den)
{
  if (floorf(fvalue) == fvalue)
  {
    *num = (int)floorf(fvalue);
    *den = 1;
    return;
  }

  float ivalue = 1.0f/fvalue;
  if (floorf(ivalue) == ivalue)
  {
    *den = (int)floorf(ivalue);
    *num = 1;
    return;
  }

	*den = 1;
	if (fvalue > 0) 
  {
		while (fvalue < 1L<<(31-3) && *den < 1L<<(31-3))
    {
			fvalue *= 1<<3;
      *den *= 1<<3;
    }
	}

	*num = imRound(fvalue);
}

int imFileFormatTGA::SaveExtensionArea()
{
  unsigned int dword_value;
  unsigned short word_value;

  // get offset before write
  long ext_offset = imBinFileTell(handle);

  imbyte buffer[512];
  memset(buffer, 0, 512);

  imAttribTable* attrib_table = AttribTable();

  // extension size
  word_value = 495;
  imBinFileWrite(handle, &word_value, 1, 2);

  // author name
  int attrib_size;
  const void* attrib_data = attrib_table->Get("Author", NULL, &attrib_size);
  if (attrib_data)
  {
    int size = attrib_size > 41? 40: attrib_size;
    imBinFileWrite(handle, (void*)attrib_data, size, 1);
    if (size < 41)
      imBinFileWrite(handle, buffer, 41-size, 1);
  }
  else
    imBinFileWrite(handle, buffer, 41, 1);

  // author comments
  attrib_data = attrib_table->Get("Description", NULL, &attrib_size);
  if (attrib_data)
  {
    int size = 0, size2 = 0, i = 0;
    while(attrib_size && i < 4)
    {
      int line_size;
      if (attrib_size > 81)
        line_size = 80;
      else
        line_size = attrib_size;

      memcpy(buffer + size, (imbyte*)attrib_data + size2, line_size);

      attrib_size -= line_size;
      size2 += line_size;
      size += line_size;
      i++;

      int remain = 81-line_size;
      if (remain)
      {
        memset(buffer + size, 0, remain);
        size += remain;
      }
    }

    imBinFileWrite(handle, buffer, 324, 1);
    memset(buffer, 0, 512);
  }
  else
    imBinFileWrite(handle, buffer, 324, 1);

  if (imBinFileError(handle))
    return 0;

  attrib_data = attrib_table->Get("DateTimeModified");
  if (attrib_data)
  {
    time_t cur_time;
    time(&cur_time);
    tm* ttm = localtime(&cur_time);

    word_value = (imushort)ttm->tm_mon+1;
    imBinFileWrite(handle, &word_value, 1, 2); // moth
    word_value = (imushort)ttm->tm_mday;
    imBinFileWrite(handle, &word_value, 1, 2); // day
    word_value = (imushort)ttm->tm_year+1900;
    imBinFileWrite(handle, &word_value, 1, 2); // year
    word_value = (imushort)ttm->tm_hour;
    imBinFileWrite(handle, &word_value, 1, 2); // hour
    word_value = (imushort)ttm->tm_min;
    imBinFileWrite(handle, &word_value, 1, 2); // minute
    word_value = (imushort)ttm->tm_sec;
    imBinFileWrite(handle, &word_value, 1, 2); // seconds

    if (imBinFileError(handle))
      return 0;
  }
  else
    imBinFileWrite(handle, buffer, 12, 1);

  // job name
  attrib_data = attrib_table->Get("JobName", NULL, &attrib_size);
  if (attrib_data)
  {
    int size = attrib_size > 41? 40: attrib_size;
    imBinFileWrite(handle, (void*)attrib_data, size, 1);
    if (size < 41)
      imBinFileWrite(handle, buffer, 41-size, 1);
  }
  else
    imBinFileWrite(handle, buffer, 41, 1);

  // job time
  imBinFileWrite(handle, buffer, 6, 1);

  // Software
  attrib_data = attrib_table->Get("Software", NULL, &attrib_size);
  if (attrib_data)
  {
    int size = attrib_size > 41? 40: attrib_size;
    imBinFileWrite(handle, (void*)attrib_data, size, 1);
    if (size < 41)
      imBinFileWrite(handle, buffer, 41-size, 1);
  }
  else
    imBinFileWrite(handle, buffer, 41, 1);

  if (imBinFileError(handle))
    return 0;

  // Software Version, key color, aspect ratio
  imBinFileWrite(handle, buffer, 11, 1);

  // gamma
  attrib_data = attrib_table->Get("Gamma");
  if (attrib_data)
  {
    float gamma = *(float*)attrib_data;

    int num, den;
    iGetRational(gamma, &num, &den);

    word_value = (imushort)num;
    imBinFileWrite(handle, &word_value, 1, 2); // num
    word_value = (imushort)den;
    imBinFileWrite(handle, &word_value, 1, 2); // den
  }
  else
    imBinFileWrite(handle, buffer, 4, 1);

  // Color Correction, Postage Stamp, Scanline Offset, Attributes Type
  imBinFileWrite(handle, buffer, 13, 1);

  // FOOTER

  // extension offset
  dword_value = ext_offset;
  imBinFileWrite(handle, &dword_value, 1, 4);

  // Developer Directory Offset
  imBinFileWrite(handle, buffer, 4, 1);

  // signature, reserved, zero string terminator
  imBinFileWrite(handle, (void*)"TRUEVISION-XFILE.\0", 18, 1);

  if (imBinFileError(handle))
    return 0;

  return 1;
}

void imFileFormatTGA::FixRGB()
{
  int x;
  imbyte* byte_data = (imbyte*)this->line_buffer;

  if (this->bpp == 16)
  {
    /* inverts the WORD values if not intel */
    if (imBinCPUByteOrder() == IM_BIGENDIAN)
      imBinSwapBytes2(this->line_buffer, this->width);

    imushort* word_data = (imushort*)this->line_buffer;

    // from end to start
    for (x = this->width-1; x >= 0; x--)
    {
      imushort word_value = word_data[x];
      int c = x*3;
      byte_data[c]   = (imbyte)(((word_value & 0x7C00) >> 10)*8);
      byte_data[c+1] = (imbyte)(((word_value & 0x03E0) >>  5)*8);
      byte_data[c+2] = (imbyte)( (word_value & 0x001F)       *8);
    }
  }
  else  // 24 and 32
  {
    // convert BGR <-> RGB
    // convert BGRA <-> RGBA
    imbyte* byte_data = (imbyte*)this->line_buffer;
    int planes = this->bpp/8;
    for (x = 0; x < this->width; x++)
    {
      int c = x*planes;
      imbyte temp = byte_data[c];     // swap R and B
      byte_data[c] = byte_data[c+2];
      byte_data[c+2] = temp;
    }
  }
}

int imFileFormatTGA::ReadImageData(void* data)
{
  imCounterTotal(this->counter, this->height, "Reading TGA...");

  int line_size = this->line_buffer_size;
  if (this->bpp == 16)
    line_size = this->width*2;

  for (int row = 0; row < this->height; row++)
  {
    if (this->image_type > 3)
    {
      if (iTGADecodeScanLine(handle, (imbyte*)this->line_buffer, this->width, this->bpp/8) == IM_ERR_ACCESS)
        return IM_ERR_ACCESS;     
    }
    else
    {
      imBinFileRead(handle, this->line_buffer, line_size, 1);
      if (imBinFileError(handle))
        return IM_ERR_ACCESS;     
    }

    if (this->bpp > 8)
      FixRGB();
  
    imFileLineBufferRead(this, data, row, 0);

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;
  }
  
  return IM_ERR_NONE;
}

int imFileFormatTGA::WriteImageData(void* data)
{
  imCounterTotal(this->counter, this->height, "Writing TGA...");

  imbyte* compressed_buffer = NULL;
  if (this->image_type > 3)  // point to the extra buffer
    compressed_buffer = (imbyte*)this->line_buffer + this->line_buffer_size;

  for (int row = 0; row < this->height; row++)
  {
    imFileLineBufferWrite(this, data, row, 0);

    if (this->bpp > 8)
      FixRGB();

    if (this->image_type > 3)
    {
      int compressed_size = iTGAEncodeScanLine(compressed_buffer, (imbyte*)this->line_buffer, this->width, this->bpp/8);
      imBinFileWrite(handle, compressed_buffer, compressed_size, 1);
    }
    else
    {
      imBinFileWrite(handle, this->line_buffer, this->line_buffer_size, 1);
    }

    if (imBinFileError(handle))
      return IM_ERR_ACCESS;     

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;
  }

  if (!SaveExtensionArea())
    return IM_ERR_ACCESS;     

  return IM_ERR_NONE;
}

int imFormatTGA::CanWrite(const char* compression, int color_mode, int data_type) const
{
  int color_space = imColorModeSpace(color_mode);

  if (color_space == IM_YCBCR || color_space == IM_LAB || 
      color_space == IM_LUV || color_space == IM_XYZ ||
      color_space == IM_CMYK)
    return IM_ERR_DATA;                       
                                              
  if (data_type != IM_BYTE)
    return IM_ERR_DATA;

  if (!compression || compression[0] == 0)
    return IM_ERR_NONE;

  if (!imStrEqual(compression, "NONE") && !imStrEqual(compression, "RLE"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}