forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicsdata.cpp
1097 lines (939 loc) · 25.8 KB
/
graphicsdata.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 "SafeProgramming.h"
#include "GraphicsClasses.h"
#include "Object.h"
#include <windows.h>
#include "GameDefines.h"
CGraphicsData::CGraphicsData()
{
Data = NULL;
DataWidth = 0;
DataHeight = 0;
PixelSize = 0;
// FileName is pre-initialised by its constructor.
}
CGraphicsData::~CGraphicsData()
{
SAFELY_DELETE_ARRAY(Data);
}
#define MASK_COLOUR ((0xFF << 16) | 0xFF)
// ----
// For loading a tga resource.
//#define IMAGE_EXTENSION ".tga"
//#define IMAGE_DATA_EXTENSION ".tgd" // targa description.
// ----
// Load data / auto dither down to supported display depth.
// Passing in a valid CPlatform object instructs the method to prepend the images directory to the file name.
// If you do not wish the file name to be modified, pass NULL instead. (This is the case for the CFonr::Init method).
bool CGraphicsData::Init(CGraphics* _GraphicsObject,
CString* _FileName,
CPlatform* _PlatformRef)
{
FILE* FH;
char* TempDataBuffer;
if ( (_GraphicsObject)
&& (_FileName)
&& (_FileName->GetStringLength() > 0))
{
CString ImagePath;
if (_PlatformRef)
ImagePath = _PlatformRef->GetPath(EP_ImageData);
ImagePath += _FileName;
ImagePath += ".cbd";
// Load the data, etc.
FH = FOPEN(ImagePath.GetString(), "rb");
if (FH)
{
fread(&ImageInfo, sizeof(ImageInfo), 1, FH);
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 2;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 11) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 3;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 10) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
int R, G, B;
R = (ImageInfo.TransparentKey >> 16 ) & 0xFF;
G = (ImageInfo.TransparentKey >> 8 ) & 0xFF;
B = (ImageInfo.TransparentKey ) & 0xFF;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
R = R >> 5;
G = G >> 5;
B = B >> 6;
// Update for palettized display
ImageInfo.TransparentKey = (R << 5) + (G << 2) + B;
}
else
{
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
ImageInfo.TransparentKey = _GraphicsObject->Match8BitPalette(InColour);//GetNearestPaletteIndex(_GraphicsObject->GetPalette(), InColour);
}
}
// Read header
TGAHeader header;
fread(&header, sizeof(header), 1, FH);
// Store width and height info
DataWidth = header.mSizeX;
DataHeight = header.mSizeY;
int TotalCount;
fread(&TotalCount, sizeof(TotalCount), 1, FH);
// Read data into buffer, before processing.
TempDataBuffer = new char[TotalCount * 4];
if (TempDataBuffer)
{
fread(TempDataBuffer, TotalCount * 4, 1, FH);
fclose(FH);
Data = NULL;
switch (_GraphicsObject->GetDisplay()->cBPP)
{
case 8:
Data = new char[DataWidth * DataHeight * 1];
PixelSize = 1;
break;
case 16:
Data = new char[DataWidth * DataHeight * 2];
PixelSize = 2;
break;
case 24:
case 32:
Data = new char[DataWidth * DataHeight * 4]; // The data is stored in the format xRGB, reguardless of whether the screen is 24 or 32 bit.
PixelSize = 4;
break;
default:
Data = NULL;
PixelSize = 0;
return false;
}
if (Data)
{
// Right, got source and destination buffers.
// Time to process data.
char* Source = TempDataBuffer;
//Source += (DataHeight - 1) * DataWidth * 3;
char* Dest = Data;
unsigned char R, G, B;
unsigned char Count;
EMachine MType;
int ReadIn = 0;
if (_GraphicsObject->GetPlatform())
MType = _GraphicsObject->GetPlatform()->GetMachineType();
else
return false;
for (int Index = TotalCount; Index--;)
{
// Read pixel.
Count = *Source; Source++;
R = *Source; Source++;
G = *Source; Source++;
B = *Source; Source++;
ReadIn += Count;
int Loop;
// Process pixels.
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
R = (R >> 3);
G = (G >> 3);
B = (B >> 3);
WORD Value = (R << 10) + (G << 5) + B;
for (Loop = Count; Loop--;)
{
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
}
else if(_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
// 565 is in the format RRRRRxGGGGGBBBBB - (This
// information was not provided with the
// documentation, but is in an article on Microsoft's
// website). - Unless, of course, you don't stick
// to that format and use another one, such as
// Casio. They use RRRRRGGGGGGBBBBB. (Which is what
// you would expect, but counter to what Microsoft
// say on their website. God know which is correct.
R = (R >> 3);
G = (G >> 2); // Don't really care about the lowest bit too much.
B = (B >> 3);
WORD Value = (R << 11) + (G << 5) + B;
for (Loop = Count; Loop--;)
{
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect888)
{
DWORD Value = (R << 16) + (G << 8) + B;
for (Loop = Count; Loop--;)
{
*(DWORD*)Dest = Value;
Dest += sizeof(DWORD);
}
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
// Direct 8 bit mode.
R = (R >> 5);
G = (G >> 5);
B = (B >> 6);
unsigned char Value = (R << 5) + (G << 2) + B;
for (Loop = Count; Loop--;)
{
*Dest = Value;
Dest++;
}
}
else
{
// Palettized 8 bit mode.
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
UINT mapcol = _GraphicsObject->Match8BitPalette(InColour);
//if (mapcol != CLR_INVALID)
{
for (Loop = Count; Loop--;)
{
*(unsigned char*)Dest = mapcol;
Dest++;
}
}
/*else
{
for (Loop = Count; Loop--;)
{
*Dest = 0;
Dest++;
}
}*/
}
}
else
{
// This display mode is currently not supported.
return false;
}
}
SAFELY_DELETE_ARRAY(TempDataBuffer);
ErrorCode = OBJECT_NO_ERROR;
ASSERT(ReadIn == (DataWidth * DataHeight));
return true;
}
SAFELY_DELETE_ARRAY(TempDataBuffer);
}
else
{
fclose(FH);
}
}
else
{
ODS("Failed to load image: ");
ODCSN((&ImagePath));
}
}
ErrorCode = GetLastError();
return false;
}
#if (defined(PROJECT) && (PROJECT == POPULOUS))
// ----
// Load up Populous specific data - yes it would be better
// to split this functionality off into a DLL, but I ain't
// got the time.
bool CGraphicsData::InitAsPopulous( CGraphics* _GraphicsObject,
FILE* _FH,
int _DataLength,
int _ImageWidth,
bool _DataHasMask)
{
char* TempBuffer;
char* Index;
LONGLONG Plane[4];
int i, j;
unsigned char R, G, B;
char* Dest;
int StripSize;
int StripWidth;
if (_DataHasMask)
StripWidth = 5;
else
StripWidth = 4;
// These comments only apply to 32 pixel wide images
// File format looks like this... (as far as i can see...).
// Long block of data.
// 4 bytes for mask (4 * 8 = 32 - 32 pixels).
// 4 bytes for plane0
// 4 bytes for plane1
// 4 bytes for plane2
// 4 bytes for plane3
// Repeat until data runs out. (i.e. _DataLength -= (5 * 4);)
SAFELY_DELETE_ARRAY(Data);
if ( (_GraphicsObject)
&& (_FH)
&& ( (_ImageWidth == 64)
|| (_ImageWidth == 32)
|| (_ImageWidth == 16)
|| (_ImageWidth == 8))
)
{
// StripSize is the number of bytes per plane,
// per horizontal line of the sprite.
StripSize = (_ImageWidth >> 3);
TempBuffer = new char[_DataLength];
if (!TempBuffer)
return false;
DataWidth = _ImageWidth;
DataHeight = _DataLength / (StripSize * StripWidth);
// Create end buffer.
Data = NULL;
switch (_GraphicsObject->GetDisplay()->cBPP)
{
case 8:
Data = new char[DataWidth * DataHeight * 1];
PixelSize = 1;
break;
case 16:
Data = new char[DataWidth * DataHeight * 2];
PixelSize = 2;
break;
case 24:
case 32:
Data = new char[DataWidth * DataHeight * 4]; // The data is stored in the format xRGB, reguardless of whether the screen is 24 or 32 bit.
PixelSize = 4;
ImageInfo.TransparentKey = (0xFF << 16) + 0xFF; // Magenta
break;
default:
Data = NULL;
PixelSize = 0;
return false;
}
// Hmmm, this file format uses masks, which doesn't
// fit my way of doing things - could change the
// graphics data on loading (i.e. convert the mask
// bit to a mask colour...) (This is not implemented).
// Magenta (or colours very close to it) are always
// going to be transparent
ImageInfo.TransparentKey = MASK_COLOUR;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 2;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 11) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 3;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 10) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
int R, G, B;
R = (ImageInfo.TransparentKey >> 16 ) & 0xFF;
G = (ImageInfo.TransparentKey >> 8 ) & 0xFF;
B = (ImageInfo.TransparentKey ) & 0xFF;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
R = R >> 5;
G = G >> 5;
B = B >> 6;
// Update for palettized display
ImageInfo.TransparentKey = (R << 5) + (G << 2) + B;
}
else
{
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
ImageInfo.TransparentKey = _GraphicsObject->Match8BitPalette(InColour);//GetNearestPaletteIndex(_GraphicsObject->GetPalette(), InColour);
}
}
if (!Data)
return false;
// Load the data, etc.
if (fread(TempBuffer, _DataLength, 1, _FH) != 1)
return false;
// ----
Dest = Data;
Index = TempBuffer;
while (_DataLength >= (StripSize * StripWidth))
{
LONGLONG Mask = 0;
if (_DataHasMask)
{
for (i = 0; i < StripSize; i++)
{
Mask = (Mask << 8) | *(unsigned char*)Index;
Index++;
}
}
for (i = 0; i < 4; i++) // Data is in 4 planes
{
Plane[i] = 0;
for (j = 0; j < StripSize; j++) // Count across the image width.
{
Plane[i] = ((Plane[i] << 8) | (*(unsigned char*)Index));
Index++;
}
}
// We now have 32 pixels worth of data, in four planes (16 bit colour).
// Need to map from plane colour to direct mode colour.
// Now loop through extracting / mapping plane info.
for (i = _ImageWidth; i--;)
{
bool MaskBit = (Mask >> i) & 0x01;
DWORD Colour;
if (!MaskBit)
{
int PaletteIndex = ( (((Plane[3] >> i) & 0x01) << 3)
| (((Plane[2] >> i) & 0x01) << 2)
| (((Plane[1] >> i) & 0x01) << 1)
| (((Plane[0] >> i) & 0x01) << 0)
);
// Now map the PaletteIndex to a colour, via the palette.
Colour = _GraphicsObject->PopulousToPal(PaletteIndex);
}
else
Colour = MASK_COLOUR; // Mask by colour.
// Read pixel.
R = (Colour >> 16) & 0xFF;
G = (Colour >> 8) & 0xFF;
B = Colour & 0xFF;
// Process pixels.
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
R = (R >> 3);
G = (G >> 3);
B = (B >> 3);
WORD Value = (R << 10) + (G << 5) + B;
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
else if(_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
R = (R >> 3);
G = (G >> 3);
B = (B >> 3);
WORD Value = (R << 11) + (G << 6) + B;
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect888)
{
DWORD Value = (R << 16) + (G << 8) + B;
*(DWORD*)Dest = Value;
Dest += sizeof(DWORD);
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
// Direct 8 bit mode.
R = (R >> 5);
G = (G >> 5);
B = (B >> 6);
unsigned char Value = (R << 5) + (G << 2) + B;
*Dest = Value;
Dest++;
}
else
{
// Palettized 8 bit mode.
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
UINT mapcol = _GraphicsObject->Match8BitPalette(InColour);
//if (mapcol != CLR_INVALID)
{
*(unsigned char*)Dest = mapcol;
Dest++;
}
}
}
else
{
// This display mode is currently not supported.
return false;
}
}
// Increment / decrement counters
//DataHeight++;
_DataLength -= (StripSize * StripWidth);
}
return true;
}
return false;
}
// ----
bool CGraphicsData::InitAsPopulous32(CGraphics* _GraphicsObject,
FILE* _FH,
int _DataLength)
{
char* TempBuffer;
char* Index;
LONGLONG Plane[4];
int i, j;
unsigned char R, G, B;
char* Dest;
int StripSize;
int StripWidth;
StripWidth = 5;
int ImageWidth = 32;
SAFELY_DELETE_ARRAY(Data);
if ( (_GraphicsObject)
&& (_FH))
{
// StripSize is the number of bytes per plane,
// per horizontal line of the sprite.
StripSize = (ImageWidth >> 4);
TempBuffer = new char[_DataLength];
if (!TempBuffer)
return false;
DataWidth = ImageWidth;
DataHeight = _DataLength / (StripSize * StripWidth);
// Create end buffer.
Data = NULL;
switch (_GraphicsObject->GetDisplay()->cBPP)
{
case 8:
Data = new char[DataWidth * DataHeight * 1];
PixelSize = 1;
break;
case 16:
Data = new char[DataWidth * DataHeight * 2];
PixelSize = 2;
break;
case 24:
case 32:
Data = new char[DataWidth * DataHeight * 4]; // The data is stored in the format xRGB, reguardless of whether the screen is 24 or 32 bit.
PixelSize = 4;
ImageInfo.TransparentKey = (0xFF << 16) + 0xFF; // Magenta
break;
default:
Data = NULL;
PixelSize = 0;
return false;
}
// Hmmm, this file format uses masks, which doesn't
// fit my way of doing things - could change the
// graphics data on loading (i.e. convert the mask
// bit to a mask colour...) (This is not implemented).
// Magenta (or colours very close to it) are always
// going to be transparent
ImageInfo.TransparentKey = MASK_COLOUR;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 2;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 11) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 3;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 10) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
int R, G, B;
R = (ImageInfo.TransparentKey >> 16 ) & 0xFF;
G = (ImageInfo.TransparentKey >> 8 ) & 0xFF;
B = (ImageInfo.TransparentKey ) & 0xFF;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
R = R >> 5;
G = G >> 5;
B = B >> 6;
// Update for palettized display
ImageInfo.TransparentKey = (R << 5) + (G << 2) + B;
}
else
{
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
ImageInfo.TransparentKey = _GraphicsObject->Match8BitPalette(InColour);//GetNearestPaletteIndex(_GraphicsObject->GetPalette(), InColour);
}
}
if (!Data)
return false;
// Load the data, etc.
if (fread(TempBuffer, _DataLength, 1, _FH) != 1)
return false;
// ----
Dest = Data;
Index = TempBuffer;
while (_DataLength >= ((StripSize * StripWidth)))
{
// ODS("Processing Sprite32 chuck at offset ");
// ODIN((int)(Index - TempBuffer));
// Sleep(1);
LONGLONG Mask = 0;
for (i = 0; i < StripSize; i++)
{
Mask = (Mask << 8) | *(unsigned char*)Index;
Index++;
}
/*
for (i = 0; i < 4; i++)
{
Plane[i] = 0;
for (j = 0; j < StripSize; j++) // Count across the image width.
{
Plane[i] = ((Plane[i] << 8) | (*(unsigned char*)Index));
Index++;
}
}*/
// We now have 32 pixels worth of data, in four planes (16 bit colour).
// Need to map from plane colour to direct mode colour.
// Now loop through extracting / mapping plane info.
for (i = ImageWidth >> 1; i--;)
{
bool MaskBit = (Mask >> i) & 0x01;
DWORD Colour;
if ((i > 0) && !((i + 1) % 8))
{
for (j = 0; j < 4; j++)
{
Plane[j] = (*(unsigned char*)Index);
Index++;
}
}
/*
if ((i) && (i % 2))
{
Plane[0] = (*(unsigned char*)Index);
Index++;
}
else
{
Plane[0] = Plane[0] >> 4;
}*/
if (!MaskBit)
{
int pia = (i % 8);
int piaa[4];
piaa[0] = Plane[0] >> pia;
piaa[1] = Plane[1] >> pia;
piaa[2] = Plane[2] >> pia;
piaa[3] = Plane[3] >> pia;
int PaletteIndex = ( ((piaa[3] & 0x01) << 3)
| ((piaa[2] & 0x01) << 2)
| ((piaa[1] & 0x01) << 1)
| ((piaa[0] & 0x01) << 0)
) /*Plane[0] & 0x0F*/;
// Now map the PaletteIndex to a colour, via the palette.
Colour = _GraphicsObject->PopulousToPal(PaletteIndex);
}
else
Colour = MASK_COLOUR; // Mask by colour.
// Read pixel.
R = (Colour >> 16) & 0xFF;
G = (Colour >> 8) & 0xFF;
B = Colour & 0xFF;
// Process pixels.
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
R = (R >> 3);
G = (G >> 3);
B = (B >> 3);
WORD Value = (R << 10) + (G << 5) + B;
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
else if(_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
R = (R >> 3);
G = (G >> 3);
B = (B >> 3);
WORD Value = (R << 11) + (G << 6) + B;
*(WORD*)Dest = Value;
Dest += sizeof(WORD);
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect888)
{
DWORD Value = (R << 16) + (G << 8) + B;
*(DWORD*)Dest = Value;
Dest += sizeof(DWORD);
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
// Direct 8 bit mode.
R = (R >> 5);
G = (G >> 5);
B = (B >> 6);
unsigned char Value = (R << 5) + (G << 2) + B;
*Dest = Value;
Dest++;
}
else
{
// Palettized 8 bit mode.
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
UINT mapcol = _GraphicsObject->Match8BitPalette(InColour);
//if (mapcol != CLR_INVALID)
{
*(unsigned char*)Dest = mapcol;
Dest++;
}
}
}
else
{
// This display mode is currently not supported.
return false;
}
}
// Increment / decrement counters
//DataHeight++;
_DataLength -= (StripSize * StripWidth);
}
return true;
}
return false;
}
// Loads and processes a Populous screenm graphic.
bool CGraphicsData::InitAsPopulousScreen(CGraphics* _GraphicsObject,
FILE* _FH,
int _DataLength,
int _ImageWidth,
int _ColourDepth)
{
char* TempBuffer;
char* Index;
int i;
unsigned char R, G, B;
char* Dest;
int DataRemaining;
// These comments only apply to 32 pixel wide images
// File format looks like this... (as far as i can see...).
// Long block of data.
// 4 bytes for plane0
// 4 bytes for plane1
// 4 bytes for plane2
// 4 bytes for plane3
// Repeat until data runs out. (i.e. _DataLength -= (4 * 4);)
SAFELY_DELETE_ARRAY(Data);
if ( (_ColourDepth != 16)
&& (_ColourDepth != 256))
return false;
if ( (_GraphicsObject)
&& (_FH))
{
DataWidth = _ImageWidth;
DataHeight = (_DataLength / _ImageWidth) * 2;
DataRemaining = _DataLength;
TempBuffer = new char[DataRemaining];
if (!TempBuffer)
return false;
// Create end buffer.
Data = NULL;
switch (_GraphicsObject->GetDisplay()->cBPP)
{
case 8:
Data = new char[DataWidth * DataHeight * 1];
PixelSize = 1;
break;
case 16:
Data = new char[DataWidth * DataHeight * 2];
PixelSize = 2;
break;
case 24:
case 32:
Data = new char[DataWidth * DataHeight * 4]; // The data is stored in the format xRGB, reguardless of whether the screen is 24 or 32 bit.
PixelSize = 4;
break;
default:
Data = NULL;
PixelSize = 0;
return false;
}
if (!Data)
return false;
// Load the data, etc.
if (fread(TempBuffer, DataRemaining, 1, _FH) != 1)
return false;
ImageInfo.TransparentKey = MASK_COLOUR;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect565)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 2;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 11) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
int R, G, B;
R = ((ImageInfo.TransparentKey >> 16) & 0xFF) >> 3;
G = ((ImageInfo.TransparentKey >> 8 ) & 0xFF) >> 3;
B = ((ImageInfo.TransparentKey ) & 0xFF) >> 3;
ImageInfo.TransparentKey = (R << 10) + (G << 5) + B;
}
else if (_GraphicsObject->GetDisplay()->cBPP == 8)
{
int R, G, B;
R = (ImageInfo.TransparentKey >> 16 ) & 0xFF;
G = (ImageInfo.TransparentKey >> 8 ) & 0xFF;
B = (ImageInfo.TransparentKey ) & 0xFF;
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect)
{
R = R >> 5;
G = G >> 5;
B = B >> 6;
// Update for palettized display
ImageInfo.TransparentKey = (R << 5) + (G << 2) + B;
}
else
{
COLORREF InColour;
InColour = (B << 16) + (G << 8) + R;
// Making the assumption that the palette is a maximum of 256 entries!!!
ImageInfo.TransparentKey = _GraphicsObject->Match8BitPalette(InColour);//GetNearestPaletteIndex(_GraphicsObject->GetPalette(), InColour);
}
}
// ----
Dest = Data;
Index = TempBuffer;
int iii;
iii = 1 + ((_ColourDepth == 16)?1:0);
while (DataRemaining >= 1)
{
unsigned char TwoPixelColour = *(unsigned char*)Index;
Index++;
for (i = iii; i--;)
{
int PaletteIndex;
if (_ColourDepth == 16)
{
if (i)
PaletteIndex = TwoPixelColour & 0x0F;
else
PaletteIndex = (TwoPixelColour & 0xF0) >> 4;
}
else
{
PaletteIndex = (TwoPixelColour & 0x0F);// - ((TwoPixelColour & 0xF0) >> 4);
}
// Now map the PaletteIndex to a colour, via the palette.
DWORD Colour = _GraphicsObject->PopulousToPal(PaletteIndex);
// Read pixel.
R = (Colour >> 16) & 0xFF;
G = (Colour >> 8) & 0xFF;
B = Colour & 0xFF;
// Process pixels.
if (_GraphicsObject->GetDisplay()->ffFormat & kfDirect555)
{
R = (R >> 3);