-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathtgaloader.cpp
1001 lines (839 loc) · 27 KB
/
tgaloader.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include <stdio.h>
#include "bitmap/tgaloader.h"
#include "tier0/dbg.h"
#include "basetypes.h"
#include <math.h>
#include "tier1/utlvector.h"
#include "tier1/utlbuffer.h"
#include "filesystem.h"
#include "tier2/tier2.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
namespace TGALoader
{
//-----------------------------------------------------------------------------
// Format of the TGA header on disk
//-----------------------------------------------------------------------------
#pragma pack (1)
struct TGAHeader_t
{
unsigned char id_length;
unsigned char colormap_type;
unsigned char image_type;
unsigned short colormap_index;
unsigned short colormap_length;
unsigned char colormap_size;
unsigned short x_origin;
unsigned short y_origin;
unsigned short width;
unsigned short height;
unsigned char pixel_size;
unsigned char attributes;
};
//-----------------------------------------------------------------------------
// read a row into an RGBA8888 array.
//-----------------------------------------------------------------------------
typedef void (*ReadRowFunc_t)( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDstMemory );
//-----------------------------------------------------------------------------
// output a RGBA8888 row into the destination format.
//-----------------------------------------------------------------------------
typedef void (*OutputRowFunc_t)( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDstMemory );
//-----------------------------------------------------------------------------
// Important constants
//-----------------------------------------------------------------------------
#define TGA_MAX_COLORMAP_SIZE ( 256 * 4 )
#define TGA_MAX_ROW_LENGTH_IN_PIXELS IMAGE_MAX_DIM
//-----------------------------------------------------------------------------
// Globals... blech
//-----------------------------------------------------------------------------
static unsigned char g_ColorMap[TGA_MAX_COLORMAP_SIZE];
// run-length state from row to row for RLE images
static bool g_IsRunLengthPacket;
static int g_PixelsLeftInPacket;
static unsigned char g_SrcGammaTable[256];
static unsigned char g_DstGammaTable[256];
typedef CUtlMemory<unsigned char> CTempImage;
//-----------------------------------------------------------------------------
// Reads in a file, sticks it into a UtlVector
//-----------------------------------------------------------------------------
static bool ReadFile( char const* pFileName, CTempImage& image, int maxbytes = -1 )
{
Assert( pFileName );
Assert( g_pFullFileSystem );
if( !g_pFullFileSystem )
{
return false;
}
FileHandle_t fileHandle;
fileHandle = g_pFullFileSystem->Open( pFileName, "rb" );
if( !fileHandle )
return false;
// How big is the file?
long pos;
if (maxbytes < 0)
{
pos = g_pFullFileSystem->Size( fileHandle );
}
else
{
pos = maxbytes;
}
// Allocate enough space
image.EnsureCapacity( pos );
// Back to the start of the file
g_pFullFileSystem->Seek( fileHandle, 0, FILESYSTEM_SEEK_HEAD );
// Read the file into the vector memory
int len = g_pFullFileSystem->Read( image.Base(), pos, fileHandle );
// Close the file
g_pFullFileSystem->Close( fileHandle );
// It's an error if we didn't read in enough goodies
return len == pos;
}
//-----------------------------------------------------------------------------
// Reads in the TGA Header
//-----------------------------------------------------------------------------
static void ReadHeader( CUtlBuffer& buf, TGAHeader_t& header )
{
buf.Get( &header, sizeof(TGAHeader_t) );
}
//-----------------------------------------------------------------------------
// Figures out TGA information
//-----------------------------------------------------------------------------
bool GetInfo( CUtlBuffer& buf, int *width, int *height,
ImageFormat *imageFormat, float *sourceGamma )
{
TGAHeader_t header;
ReadHeader( buf, header );
switch( header.image_type )
{
case 1: // 8 bit uncompressed TGA image
case 3: // 8 bit monochrome uncompressed TGA image
case 9: // 8 bit compressed TGA image
*imageFormat = IMAGE_FORMAT_I8;
break;
case 2: // 24/32 bit uncompressed TGA image
case 10: // 24/32 bit compressed TGA image
if( header.pixel_size == 32 )
{
*imageFormat = IMAGE_FORMAT_ABGR8888;
}
else if( header.pixel_size == 24 )
{
*imageFormat = IMAGE_FORMAT_BGR888;
}
else
{
return false;
}
break;
default:
return false;
break;
}
*width = header.width;
*height = header.height;
*sourceGamma = ARTWORK_GAMMA;
return true;
}
//-----------------------------------------------------------------------------
// Returns the minimum amount you have to load to get information about the TGA file
//-----------------------------------------------------------------------------
int TGAHeaderSize()
{
return sizeof( TGAHeader_t );
}
//-----------------------------------------------------------------------------
// Gets info about a TGA file
//-----------------------------------------------------------------------------
bool GetInfo( char const* pFileName, int *width, int *height,
ImageFormat *imageFormat, float *sourceGamma )
{
// temporary memory
CTempImage image;
// try to read in the file
if (!ReadFile( pFileName, image, sizeof(TGAHeader_t) ))
return false;
// Serialization buffer
CUtlBuffer buf( image.Base(), image.NumAllocated(), CUtlBuffer::READ_ONLY );
return GetInfo( buf, width, height, imageFormat, sourceGamma );
}
//-----------------------------------------------------------------------------
// Various output methods
//-----------------------------------------------------------------------------
void OutputRowRGBA8888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[0];
pDst[1] = pSrc[1];
pDst[2] = pSrc[2];
pDst[3] = pSrc[3];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowABGR8888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[3] = pSrc[0];
pDst[2] = pSrc[1];
pDst[1] = pSrc[2];
pDst[0] = pSrc[3];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowRGB888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 3 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[0];
pDst[1] = pSrc[1];
pDst[2] = pSrc[2];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGR888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 3 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[2] = pSrc[0];
pDst[1] = pSrc[1];
pDst[0] = pSrc[2];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowRGB565( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
Assert( 0 );
}
void OutputRowI8( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, ++pDst )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
if( ( pSrc[0] == pSrc[1] ) && ( pSrc[1] == pSrc[2] ) )
{
pDst[0] = pSrc[0];
}
else
{
pDst[0] = ( unsigned char )( 0.299f * pSrc[0] + 0.587f * pSrc[1] + 0.114f * pSrc[2] );
}
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowIA88( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 2 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
if( ( pSrc[0] == pSrc[1] ) && ( pSrc[1] == pSrc[2] ) )
{
pDst[0] = pSrc[0];
}
else
{
pDst[0] = ( unsigned char )( 0.299f * pSrc[0] + 0.587f * pSrc[1] + 0.114f * pSrc[2] );
}
pDst[1] = pSrc[3];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowA8( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, ++pDst )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[3];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowRGB888BlueScreen( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 3 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = (unsigned char)(( ( int )pSrc[0] * ( int )pSrc[3] ) >> 8);
pDst[1] = (unsigned char)(( ( int )pSrc[1] * ( int )pSrc[3] ) >> 8);
pDst[2] = (( ( ( ( int )pSrc[2] * ( int )pSrc[3] ) ) >> 8 ) + ( 255 - pSrc[3] ));
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGR888BlueScreen( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 3 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[2] = (unsigned char)(( ( int )pSrc[0] * ( int )pSrc[3] ) >> 8);
pDst[1] = (unsigned char)(( ( int )pSrc[1] * ( int )pSrc[3] ) >> 8);
pDst[0] = (unsigned char)(( ( ( ( int )pSrc[2] * ( int )pSrc[3] ) ) >> 8 ) + ( 255 - pSrc[3] ));
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowARGB8888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[3];
pDst[1] = pSrc[0];
pDst[2] = pSrc[1];
pDst[3] = pSrc[2];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGRA8888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[2];
pDst[1] = pSrc[1];
pDst[2] = pSrc[0];
pDst[3] = pSrc[3];
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGRX8888( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
pDst[0] = pSrc[2];
pDst[1] = pSrc[1];
pDst[2] = pSrc[0];
pDst[3] = 255;
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGR565( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 2 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
unsigned short rgba = (pSrc[2] & 0x1F) | ((pSrc[1] & 0x3F) << 5) |
((pSrc[0] & 0x1F) << 11);
pDst[0] = rgba & 0xFF;
pDst[1] = rgba >> 8;
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
void OutputRowBGRX5551( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 2 )
{
unsigned char* pSrc = (unsigned char*)buf.PeekGet();
unsigned short rgba = (pSrc[2] & 0x1F) | ((pSrc[1] & 0x1F) << 5) |
((pSrc[0] & 0x1F) << 10) | 0x8000;
pDst[0] = rgba & 0xFF;
pDst[1] = rgba >> 8;
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 4 );
}
}
static OutputRowFunc_t GetOutputRowFunc( ImageFormat imageFormat )
{
switch( imageFormat )
{
case IMAGE_FORMAT_RGBA8888:
return &OutputRowRGBA8888;
case IMAGE_FORMAT_ABGR8888:
return &OutputRowABGR8888;
case IMAGE_FORMAT_RGB888:
return &OutputRowRGB888;
case IMAGE_FORMAT_BGR888:
return &OutputRowBGR888;
case IMAGE_FORMAT_RGB565:
return &OutputRowRGB565;
case IMAGE_FORMAT_I8:
return &OutputRowI8;
case IMAGE_FORMAT_IA88:
return &OutputRowIA88;
case IMAGE_FORMAT_A8:
return &OutputRowA8;
case IMAGE_FORMAT_RGB888_BLUESCREEN:
return &OutputRowRGB888BlueScreen;
case IMAGE_FORMAT_BGR888_BLUESCREEN:
return &OutputRowBGR888BlueScreen;
case IMAGE_FORMAT_ARGB8888:
return &OutputRowARGB8888;
case IMAGE_FORMAT_BGRA8888:
return &OutputRowBGRA8888;
case IMAGE_FORMAT_BGRX8888:
return &OutputRowBGRX8888;
case IMAGE_FORMAT_BGR565:
return &OutputRowBGR565;
case IMAGE_FORMAT_BGRX5551:
return &OutputRowBGRX5551;
#ifdef _X360
case IMAGE_FORMAT_LINEAR_RGB888:
return &OutputRowRGB888;
case IMAGE_FORMAT_LINEAR_BGR888:
return &OutputRowBGR888;
#endif
default:
return NULL;
break;
}
}
#if 0
static void InitSourceGammaConversionTable( float srcGamma )
{
static float lastSrcGamma = -1;
if (lastSrcGamma == srcGamma)
return;
lastSrcGamma = srcGamma;
ImageLoader::ConstructGammaTable( g_SrcGammaTable, srcGamma, 1.0f );
}
static void InitDestGammaConversionTable( float dstGamma )
{
static float lastDstGamma = -1;
if (lastDstGamma == dstGamma)
return;
lastDstGamma = dstGamma;
ImageLoader::ConstructGammaTable( g_DstGammaTable, 1.0f, dstGamma );
}
#endif
//-----------------------------------------------------------------------------
// Reads an 8-bit palettized TGA image
//-----------------------------------------------------------------------------
void ReadRow8BitUncompressedWithColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
int i;
unsigned char* colormapEntry;
switch( header.colormap_size )
{
case 8:
for( i = 0; i < header.width; ++i, pDst += 4 )
{
int pal = buf.GetUnsignedChar();
colormapEntry = &g_ColorMap[pal];
pDst[0] = colormapEntry[0];
pDst[1] = colormapEntry[0];
pDst[2] = colormapEntry[0];
pDst[3] = 255;
}
break;
case 24:
for( i = 0; i < header.width; ++i, pDst += 4 )
{
int pal = buf.GetUnsignedChar();
colormapEntry = &g_ColorMap[pal * 3];
pDst[0] = colormapEntry[2];
pDst[1] = colormapEntry[1];
pDst[2] = colormapEntry[0];
pDst[3] = 255;
}
break;
case 32:
for( i = 0; i < header.width; ++i, pDst += 4 )
{
int pal = buf.GetUnsignedChar();
colormapEntry = &g_ColorMap[pal * 4];
pDst[0] = colormapEntry[3];
pDst[1] = colormapEntry[2];
pDst[2] = colormapEntry[1];
pDst[3] = colormapEntry[0];
}
break;
default:
Assert( 0 );
break;
}
}
//-----------------------------------------------------------------------------
// Reads an 8-bit greyscale TGA image
//-----------------------------------------------------------------------------
void ReadRow8BitUncompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
pDst[0] = pDst[1] = pDst[2] = buf.GetUnsignedChar();
pDst[3] = 255;
}
}
//-----------------------------------------------------------------------------
// Reads a 24-bit TGA image
//-----------------------------------------------------------------------------
void ReadRow24BitUncompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
pDst[2] = buf.GetUnsignedChar();
pDst[1] = buf.GetUnsignedChar();
pDst[0] = buf.GetUnsignedChar();
pDst[3] = 255;
}
}
//-----------------------------------------------------------------------------
// Reads a 32-bit TGA image
//-----------------------------------------------------------------------------
void ReadRow32BitUncompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
for( int i = 0; i < header.width; ++i, pDst += 4 )
{
pDst[2] = buf.GetUnsignedChar();
pDst[1] = buf.GetUnsignedChar();
pDst[0] = buf.GetUnsignedChar();
pDst[3] = buf.GetUnsignedChar();
}
}
//-----------------------------------------------------------------------------
// Decompresses a run-length encoded row of bytes
//-----------------------------------------------------------------------------
static void DecompressRow( CUtlBuffer& buf, TGAHeader_t const& header, unsigned char* pDst )
{
int bytesPerPixel = header.pixel_size >> 3;
int pixelsLeftInRow = header.width;
int numPixelsToProcess;
#ifdef DBGFLAG_ASSERT
unsigned char *pLast = pDst + header.width * bytesPerPixel;
#endif
unsigned char repeat[4] = {};
do
{
if( !g_PixelsLeftInPacket )
{
// start a new packet.
unsigned char packetHeader = buf.GetUnsignedChar();
g_PixelsLeftInPacket = 1 + ( packetHeader & 0x7f );
if( packetHeader & 0x80 )
{
g_IsRunLengthPacket = true;
// Read what I'm supposed to repeat
for (int i = 0; i < bytesPerPixel; ++i)
{
repeat[i] = buf.GetUnsignedChar();
}
}
else
{
g_IsRunLengthPacket = false;
}
}
// already in the middle of a packet of data.
numPixelsToProcess = g_PixelsLeftInPacket;
if( numPixelsToProcess > pixelsLeftInRow )
{
numPixelsToProcess = pixelsLeftInRow;
}
if( g_IsRunLengthPacket )
{
for( int i = numPixelsToProcess; --i >= 0; pDst += bytesPerPixel )
{
for (int j = 0; j < bytesPerPixel; ++j )
{
pDst[j] = repeat[j];
}
}
}
else
{
buf.Get( pDst, numPixelsToProcess * bytesPerPixel );
pDst += numPixelsToProcess * bytesPerPixel;
}
g_PixelsLeftInPacket -= numPixelsToProcess;
pixelsLeftInRow -= numPixelsToProcess;
} while( pixelsLeftInRow );
Assert( pDst == pLast );
}
//-----------------------------------------------------------------------------
// Reads a compressed 8-bit palettized TGA image
//-----------------------------------------------------------------------------
void ReadRow8BitCompressedWithColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
unsigned char rowI_8[TGA_MAX_ROW_LENGTH_IN_PIXELS];
DecompressRow( buf, header, rowI_8 );
CUtlBuffer uncompressedBuf( rowI_8, TGA_MAX_ROW_LENGTH_IN_PIXELS, CUtlBuffer::READ_ONLY );
ReadRow8BitUncompressedWithColormap( uncompressedBuf, header, pDst );
}
//-----------------------------------------------------------------------------
// Reads a compressed 8-bit greyscale TGA image
//-----------------------------------------------------------------------------
void ReadRow8BitCompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
unsigned char rowI_8[TGA_MAX_ROW_LENGTH_IN_PIXELS];
DecompressRow( buf, header, rowI_8 );
CUtlBuffer uncompressedBuf( rowI_8, TGA_MAX_ROW_LENGTH_IN_PIXELS, CUtlBuffer::READ_ONLY );
ReadRow8BitUncompressedWithoutColormap( uncompressedBuf, header, pDst );
}
//-----------------------------------------------------------------------------
// Reads a compressed 24-bit TGA image
//-----------------------------------------------------------------------------
void ReadRow24BitCompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
unsigned char rowBGR_888[TGA_MAX_ROW_LENGTH_IN_PIXELS * 3];
DecompressRow( buf, header, rowBGR_888 );
CUtlBuffer uncompressedBuf( rowBGR_888, TGA_MAX_ROW_LENGTH_IN_PIXELS * 3, CUtlBuffer::READ_ONLY );
ReadRow24BitUncompressedWithoutColormap( uncompressedBuf, header, pDst );
}
//-----------------------------------------------------------------------------
// Reads a compressed 32-bit TGA image
//-----------------------------------------------------------------------------
void ReadRow32BitCompressedWithoutColormap( CUtlBuffer& buf,
TGAHeader_t const& header, unsigned char* pDst )
{
unsigned char rowBGRA_8888[TGA_MAX_ROW_LENGTH_IN_PIXELS << 2];
DecompressRow( buf, header, rowBGRA_8888 );
CUtlBuffer uncompressedBuf( rowBGRA_8888, TGA_MAX_ROW_LENGTH_IN_PIXELS << 2, CUtlBuffer::READ_ONLY );
ReadRow32BitUncompressedWithoutColormap( uncompressedBuf, header, pDst );
}
//-----------------------------------------------------------------------------
// Method used to read the TGA
//-----------------------------------------------------------------------------
static ReadRowFunc_t GetReadRowFunc( TGAHeader_t const& header )
{
switch( header.image_type )
{
case 1: // 8 bit uncompressed TGA image
case 3: // 8 bit monochrome uncompressed TGA image
if( header.colormap_length )
{
return &ReadRow8BitUncompressedWithColormap;
}
else
{
return &ReadRow8BitUncompressedWithoutColormap;
}
case 9: // 8 bit compressed TGA image
if( header.colormap_length )
{
return &ReadRow8BitCompressedWithColormap;
}
else
{
return &ReadRow8BitCompressedWithoutColormap;
}
case 2: // 24/32 bit uncompressed TGA image
switch( header.pixel_size )
{
case 24:
return &ReadRow24BitUncompressedWithoutColormap;
break;
case 32:
return &ReadRow32BitUncompressedWithoutColormap;
break;
default:
//Error( "unsupported tga colordepth: %d", TGAHeader_t.pixel_size" );
return 0;
break;
}
case 10: // 24/32 bit compressed TGA image
if( header.colormap_length )
{
// Error( "colormaps not support with 24/32 bit TGAs." );
return 0;
}
else
{
switch( header.pixel_size )
{
case 24:
return &ReadRow24BitCompressedWithoutColormap;
break;
case 32:
return &ReadRow32BitCompressedWithoutColormap;
break;
default:
//Error( "unsupported tga colordepth: %d", TGAHeader_t.pixel_size" );
return NULL;
break;
}
}
default:
// Error( "unsupported tga pixel format" );
return 0;
break;
}
}
//-----------------------------------------------------------------------------
// Reads the color map
//-----------------------------------------------------------------------------
static bool ReadColormap( CUtlBuffer& buf, TGAHeader_t const& header )
{
int numColormapBytes = header.colormap_length * ( header.colormap_size >> 3 );
if( numColormapBytes > TGA_MAX_COLORMAP_SIZE )
{
// Error( "colormap bigger than TGA_MAX_COLORMAP_SIZE" );
return false;
}
// read colormap
buf.Get( g_ColorMap, numColormapBytes );
return true;
}
//-----------------------------------------------------------------------------
// Reads the source image
//-----------------------------------------------------------------------------
static bool ReadSourceImage( CUtlBuffer& buf, TGAHeader_t& header, CTempImage& image )
{
// Figure out our reading and riting
ReadRowFunc_t ReadRowFunc = GetReadRowFunc( header );
if( !ReadRowFunc )
return false;
// HACK: Fixme: We really shouldn't be using globals here
// Init RLE vars
g_PixelsLeftInPacket = 0;
// Only allocate the memory once
int memRequired = ImageLoader::GetMemRequired( header.width, header.height, 1,
IMAGE_FORMAT_RGBA8888, false );
image.EnsureCapacity( memRequired );
// read each row and process it. Note the image is upside-down from
// the way we want it.
unsigned char* pDstBits;
// flip the image vertically if necessary.
if (header.attributes & 0x20)
{
for( int row = 0; row < header.height; ++row )
{
pDstBits = image.Base() +
row * header.width * ImageLoader::SizeInBytes(IMAGE_FORMAT_RGBA8888);
ReadRowFunc( buf, header, pDstBits );
}
}
else
{
for( int row = header.height; --row >= 0; )
{
pDstBits = image.Base() +
row * header.width * ImageLoader::SizeInBytes(IMAGE_FORMAT_RGBA8888);
ReadRowFunc( buf, header, pDstBits );
}
}
return true;
}
#if 0
//-----------------------------------------------------------------------------
// Outputs the final image
//-----------------------------------------------------------------------------
static bool OutputImage( CTempImage& image, TGAHeader_t& header,
ImageFormat imageFormat, unsigned char* pDst )
{
// How do we write?
OutputRowFunc_t OutputRowFunc = GetOutputRowFunc( imageFormat );
if( !OutputRowFunc )
return false;
CUtlBuffer buf( image.Base(), image.NumAllocated(), CUtlBuffer::READ_ONLY );
unsigned char* pDstBits;
for( int row = 0; row < header.height; ++row )
{
pDstBits = pDst +
row * header.width * ImageLoader::SizeInBytes(imageFormat);
OutputRowFunc( buf, header, pDstBits );
}
return true;
}
#endif
//-----------------------------------------------------------------------------
// Parses the lovely bits previously read from disk
//-----------------------------------------------------------------------------
bool Load( unsigned char *pOutputImage, CUtlBuffer& buf, int width,
int height, ImageFormat imageFormat, float targetGamma, bool mipmap )
{
TGAHeader_t header;
// Read the TGA header
ReadHeader( buf, header );
// skip TARGA image comment
if( header.id_length != 0 )
{
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, header.id_length );
}
// Read the color map for palettized images
if( header.colormap_length != 0 )
{
if (!ReadColormap( buf, header ))
return false;
}
// Stores the RGBA8888 temp version of the image which we'll use to
// to do mipmapping...
CTempImage tmpImage;
if (!ReadSourceImage( buf, header, tmpImage ))
return false;
// Erg... what if header.width * header.height > width * height?
// Then don't do anything, this is an error condition...
if ((width * height) < (header.width * header.height))
return false;
// Now that we've got the source image, generate the mip-map levels
ImageLoader::GenerateMipmapLevels( tmpImage.Base(), pOutputImage,
header.width, header.height, 1, imageFormat, ARTWORK_GAMMA, targetGamma,
mipmap ? 0 : 1 );
return true;
}
//-----------------------------------------------------------------------------
// Reads a TGA image from a file
//-----------------------------------------------------------------------------
bool Load( unsigned char *pOutputImage, const char *pFileName, int width, int height,
ImageFormat imageFormat, float targetGamma, bool mipmap )
{
Assert( pOutputImage && pFileName );
// memory for the file
CTempImage vec;
// Read that puppy in!
if (!ReadFile( pFileName, vec ))
return false;
// Make an unserialization buffer
CUtlBuffer buf( vec.Base(), vec.NumAllocated(), CUtlBuffer::READ_ONLY );
// Do the dirty deed
return Load( pOutputImage, buf, width, height, imageFormat, targetGamma, mipmap );
}
//-----------------------------------------------------------------------------
// Creates a map in linear space
//-----------------------------------------------------------------------------
bool LoadRGBA8888( CUtlBuffer& buf, CUtlMemory<unsigned char> &outputData, int &outWidth, int &outHeight )
{
TGAHeader_t header;
// Read the TGA header
ReadHeader( buf, header );
// skip TARGA image comment
if( header.id_length != 0 )
{
buf.SeekGet( CUtlBuffer::SEEK_CURRENT, header.id_length );
}
// Read the color map for palettized images
if( header.colormap_length != 0 )
{
if (!ReadColormap( buf, header ))
return false;
}
// Stores the RGBA8888 temp version of the image which we'll use to
// to do mipmapping...
int memSize = ImageLoader::GetMemRequired(
header.width, header.height, 1, IMAGE_FORMAT_RGBA8888, false );
outputData.EnsureCapacity( memSize );
if (!ReadSourceImage( buf, header, outputData ))
return false;
outWidth = header.width;
outHeight = header.height;
return true;
}
//-----------------------------------------------------------------------------
// Reads a TGA, keeps it in RGBA8888
//-----------------------------------------------------------------------------
bool LoadRGBA8888( const char *pFileName, CUtlMemory<unsigned char> &outputData, int &outWidth, int &outHeight )
{
Assert( pFileName );
// memory for the file
CTempImage vec;
// Read that puppy in!
if (!ReadFile( pFileName, vec ))
return false;
// Make an unserialization buffer
CUtlBuffer buf( vec.Base(), vec.NumAllocated(), CUtlBuffer::READ_ONLY );
// Do the dirty deed
return LoadRGBA8888( buf, outputData, outWidth, outHeight );
}
} // end namespace TGALoader