forked from SerumColor/ColorizingDMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserum-decode.cpp
1566 lines (1460 loc) · 55.6 KB
/
serum-decode.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
#define __STDC_WANT_LIB_EXT1_ 1
#include "serum-decode.h"
#include <miniz/miniz.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <chrono>
#include "serum-version.h"
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif
#if not defined(__STDC_LIB_EXT1__)
// trivial implementation of the secure string functions if not directly
// upported by the compiler these do not perform all security checks and can be
// improved for sure
int strcpy_s(char* dest, int destsz, const char* src)
{
if ((dest == NULL) || (src == NULL)) return 1;
if (strlen(src) >= destsz) return 1;
strcpy(dest, src);
return 0;
}
int strcat_s(char* dest, int destsz, const char* src)
{
if ((dest == NULL) || (src == NULL)) return 1;
if (strlen(dest) + strlen(src) >= destsz) return 1;
strcat(dest, src);
return 0;
}
#endif
#pragma warning(disable : 4996)
const int pathbuflen = 4096;
const int IDENTIFY_SAME_FRAME = -2;
const int IDENTIFY_NO_FRAME = -1;
// header
char rname[64];
bool newformat = false;
UINT32 fwidth, fheight;
UINT32 fwidthx, fheightx;
UINT16 nframes;
UINT32 nocolors, nccolors;
UINT32 ncompmasks, nmovmasks;
UINT32 nsprites;
UINT16 nbackgrounds;
// data
UINT32* hashcodes = NULL;
UINT8* shapecompmode = NULL;
UINT8* compmaskID = NULL;
UINT8* movrctID = NULL;
UINT8* compmasks = NULL;
UINT8* movrcts = NULL;
UINT8* cpal = NULL;
UINT8* isextraframe = NULL;
UINT8* cframes = NULL;
UINT16* cframesn = NULL;
UINT16* cframesnx = NULL;
UINT8* dynamasks = NULL;
UINT8* dynamasksx = NULL;
UINT8* dyna4cols = NULL;
UINT16* dyna4colsn = NULL;
UINT16* dyna4colsnx = NULL;
UINT8* framesprites = NULL;
UINT8* spritedescriptionso = NULL;
UINT8* spritedescriptionsc = NULL;
UINT8* isextrasprite = NULL;
UINT8* spriteoriginal = NULL;
UINT8* spritemaskx = NULL;
UINT16* spritecolored = NULL;
UINT16* spritecoloredx = NULL;
UINT8* activeframes = NULL;
UINT8* colorrotations = NULL;
UINT16* colorrotationsn = NULL;
UINT16* colorrotationsnx = NULL;
UINT16* spritedetareas = NULL;
UINT32* spritedetdwords = NULL;
UINT16* spritedetdwordpos = NULL;
UINT32* triggerIDs = NULL;
UINT16* framespriteBB = NULL;
UINT8* isextrabackground = NULL;
UINT8* backgroundframes = NULL;
UINT16* backgroundframesn = NULL;
UINT16* backgroundframesnx = NULL;
UINT16* backgroundIDs = NULL;
UINT16* backgroundBB = NULL;
UINT8* backgroundmask = NULL;
UINT8* backgroundmaskx = NULL;
// variables
bool cromloaded = false; // is there a crom loaded?
UINT16 lastfound = 0; // last frame ID identified
UINT8* lastframe = NULL; // last frame content identified
UINT16* lastframe32 = NULL, * lastframe64 = NULL; // last frame content in new version
UINT lastwidth, lasheight; // what were the dimension of the last frame
UINT32 lastframe_found = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
UINT32 lastframe_full_crc = 0;
UINT8* lastpalette = NULL; // last palette identified
UINT8* lastrotations = NULL; // last colour rotations identified
UINT16* lastrotations32 = NULL; // last colour rotations identified new version
UINT16* lastrotations64 = NULL; // last colour rotations identified new version
UINT16* lastrotationsinframe32 = NULL; // last precalculated pixels of the frame that are part of a rotation
UINT16* lastrotationsinframe64 = NULL; // last precalculated pixels of the frame that are part of a rotation
UINT8 lastsprite[MAX_SPRITE_TO_DETECT]; // last sprites identified
UINT lastnsprites = 0; // last amount of sprites detected
UINT16 lastfrx[MAX_SPRITE_TO_DETECT], lastfry[MAX_SPRITE_TO_DETECT]; // last position in the frame of the sprite
UINT16 lastspx[MAX_SPRITE_TO_DETECT], lastspy[MAX_SPRITE_TO_DETECT]; // last top left of the sprite to display
UINT16 lastwid[MAX_SPRITE_TO_DETECT], lasthei[MAX_SPRITE_TO_DETECT]; // last dimensions of the sprite to display
UINT32 lasttriggerID = 0xFFFFFFFF; // last trigger ID found
bool isrotation = true; // are there rotations to send
bool crc32_ready = false; // is the crc32 table filled?
UINT32 crc32_table[256]; // initial table
bool* framechecked = NULL; // are these frames checked?
UINT16 ignoreUnknownFramesTimeout = 0;
UINT8 maxFramesToSkip = 0;
UINT8 framesSkippedCounter = 0;
UINT8 standardPalette[PALETTE_SIZE];
UINT8 standardPaletteLength = 0;
UINT32 colorshifts[MAX_COLOR_ROTATIONS]; // how many color we shifted
UINT32 colorshiftinittime[MAX_COLOR_ROTATIONS]; // when was the tick for this
UINT32 colorshifts32[MAX_COLOR_ROTATIONN]; // how many color we shifted for extra res
UINT32 colorshiftinittime32[MAX_COLOR_ROTATIONN]; // when was the tick for this for extra res
UINT32 colorshifts64[MAX_COLOR_ROTATIONN]; // how many color we shifted for extra res
UINT32 colorshiftinittime64[MAX_COLOR_ROTATIONN]; // when was the tick for this for extra res
// rotation
bool enabled = true; // is colorization enabled?
UINT Serum_flags; // flags sent by the caller in Serum_Load
bool isoriginalrequested = true; // are the original resolution frames requested by the caller
bool isextrarequested = false; // are the extra resolution frames requested by the caller
void Free_element(void* pElement)
{
// free a malloc block and set its pointer to NULL
if (pElement)
{
free(pElement);
pElement = NULL;
}
}
void Serum_free(void)
{
// Free the memory for a full Serum whatever the format version
Free_element(hashcodes);
Free_element(shapecompmode);
Free_element(compmaskID);
Free_element(movrctID);
Free_element(compmasks);
Free_element(movrcts);
Free_element(cpal);
Free_element(isextraframe);
Free_element(cframesn);
Free_element(cframesnx);
Free_element(cframes);
Free_element(dynamasks);
Free_element(dynamasksx);
Free_element(dyna4cols);
Free_element(dyna4colsn);
Free_element(dyna4colsnx);
Free_element(framesprites);
Free_element(spritedescriptionso);
Free_element(spritedescriptionsc);
Free_element(isextrasprite);
Free_element(spriteoriginal);
Free_element(spritemaskx);
Free_element(spritecolored);
Free_element(spritecoloredx);
Free_element(activeframes);
Free_element(colorrotations);
Free_element(colorrotationsn);
Free_element(colorrotationsnx);
Free_element(spritedetareas);
Free_element(spritedetdwords);
Free_element(spritedetdwordpos);
Free_element(triggerIDs);
Free_element(framespriteBB);
Free_element(isextrabackground);
Free_element(backgroundframes);
Free_element(backgroundframesn);
Free_element(backgroundframesnx);
Free_element(backgroundIDs);
Free_element(backgroundBB);
Free_element(backgroundmask);
Free_element(backgroundmaskx);
Free_element(lastframe);
Free_element(lastframe32);
Free_element(lastframe64);
Free_element(lastpalette);
Free_element(lastrotations);
Free_element(lastrotations32);
Free_element(lastrotations64);
Free_element(lastrotationsinframe32);
Free_element(lastrotationsinframe64);
Free_element(framechecked);
cromloaded = false;
}
SERUM_API const char* Serum_GetVersion() { return SERUM_VERSION; }
SERUM_API const char* Serum_GetMinorVersion() { return SERUM_MINOR_VERSION; }
void CRC32encode(void) // initiating the CRC table, must be called at startup
{
for (int i = 0; i < 256; i++)
{
UINT32 ch = i;
UINT32 crc = 0;
for (int j = 0; j < 8; j++)
{
UINT32 b = (ch ^ crc) & 1;
crc >>= 1;
if (b != 0) crc = crc ^ 0xEDB88320;
ch >>= 1;
}
crc32_table[i] = crc;
}
crc32_ready = true;
}
UINT32 crc32_fast(UINT8* s, UINT n, UINT8 ShapeMode)
// computing a buffer CRC32, "CRC32encode()" must have been called before the first use
{
UINT32 crc = 0xFFFFFFFF;
for (int i = 0; i < (int)n; i++)
{
UINT8 val = s[i];
if ((ShapeMode == 1) && (val > 1)) val = 1;
crc = (crc >> 8) ^ crc32_table[(val ^ crc) & 0xFF];
}
return ~crc;
}
UINT32 crc32_fast_mask(UINT8* source, UINT8* mask, UINT n, UINT8 ShapeMode)
// computing a buffer CRC32 on the non-masked area,
// "CRC32encode()" must have been called before the first use
// take into account if we are in shape mode
{
UINT32 crc = 0xFFFFFFFF;
for (UINT i = 0; i < n; i++)
{
if (mask[i] == 0)
{
UINT8 val = source[i];
if ((ShapeMode == 1) && (val > 1)) val = 1;
crc = (crc >> 8) ^ crc32_table[(val ^ crc) & 0xFF];
}
}
return ~crc;
}
bool unzip_crz(const char* const filename, const char* const extractpath, char* cromname, int cromsize)
{
bool ok = true;
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_reader_init_file(&zip_archive, filename, 0))
{
return false;
}
int num_files = mz_zip_reader_get_num_files(&zip_archive);
if (num_files == 0 || !mz_zip_reader_get_filename(&zip_archive, 0, cromname, cromsize))
{
mz_zip_reader_end(&zip_archive);
return false;
}
for (int i = 0; i < num_files; i++)
{
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) continue;
char dstPath[pathbuflen];
if (strcpy_s(dstPath, pathbuflen, extractpath)) goto fail;
if (strcat_s(dstPath, pathbuflen, file_stat.m_filename)) goto fail;
mz_zip_reader_extract_file_to_file(&zip_archive, file_stat.m_filename,
dstPath, 0);
}
goto nofail;
fail:
ok = false;
nofail:
mz_zip_reader_end(&zip_archive);
return ok;
}
void Reset_ColorRotations(void)
{
memset(colorshifts, 0, MAX_COLOR_ROTATIONS * sizeof(UINT32));
colorshiftinittime[0] = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
for (int ti = 1; ti < MAX_COLOR_ROTATIONS; ti++) colorshiftinittime[ti] = colorshiftinittime[0];
memset(colorshifts32, 0, MAX_COLOR_ROTATIONN * sizeof(UINT32));
memset(colorshifts64, 0, MAX_COLOR_ROTATIONN * sizeof(UINT32));
for (int ti = 0; ti < MAX_COLOR_ROTATIONN; ti++)
{
colorshiftinittime32[ti] = colorshiftinittime[0];
colorshiftinittime64[ti] = colorshiftinittime[0];
}
}
UINT max(UINT v1, UINT v2)
{
if (v1 > v2) return v1;
return v2;
}
UINT min(UINT v1, UINT v2)
{
if (v1 < v2) return v1;
return v2;
}
long serum_file_length;
long long ac_pos_in_file;
FILE* fconsole;
const bool IS_DEBUG_READ = false;
size_t my_fread(void* pBuffer, size_t sizeElement, size_t nElements, FILE* stream)
{
size_t readelem = fread(pBuffer, sizeElement, nElements, stream);
ac_pos_in_file += readelem * sizeElement;
if (IS_DEBUG_READ) printf("sent elements: %llu / written elements: %llu / written bytes: %llu / current position: %llu\r", nElements, readelem, readelem * sizeElement, ac_pos_in_file);
return readelem;
}
SERUM_API bool Serum_LoadNewFile(FILE* pfile, unsigned int* pnocolors, unsigned int* pntriggers, bool uncompressedCROM, char* pathbuf, UINT8 flags, UINT* width32, UINT* width64)
{
newformat = true;
my_fread(&fwidth, 4, 1, pfile);
my_fread(&fheight, 4, 1, pfile);
my_fread(&fwidthx, 4, 1, pfile);
my_fread(&fheightx, 4, 1, pfile);
isoriginalrequested = false;
isextrarequested = false;
if (fheight == 32)
{
if (Serum_flags & FLAG_REQUEST_32P_FRAMES) isoriginalrequested = true;
if (Serum_flags & FLAG_REQUEST_64P_FRAMES) isextrarequested = true;
}
else
{
if (Serum_flags & FLAG_REQUEST_64P_FRAMES) isoriginalrequested = true;
if (Serum_flags & FLAG_REQUEST_32P_FRAMES) isextrarequested = true;
}
my_fread(&nframes, 4, 1, pfile);
my_fread(&nocolors, 4, 1, pfile);
if ((fwidth == 0) || (fheight == 0) || (nframes == 0) || (nocolors == 0))
{
// incorrect file format
fclose(pfile);
enabled = false;
return false;
}
my_fread(&ncompmasks, 4, 1, pfile);
my_fread(&nsprites, 4, 1, pfile);
my_fread(&nbackgrounds, 2, 1, pfile); // nbackgrounds is a UINT16
hashcodes = (UINT32*)malloc(sizeof(UINT32) * nframes);
shapecompmode = (UINT8*)malloc(nframes);
compmaskID = (UINT8*)malloc(nframes);
compmasks = (UINT8*)malloc(ncompmasks * fwidth * fheight);
isextraframe = (UINT8*)malloc(nframes);
cframesn = (UINT16*)malloc(nframes * fwidth * fheight * sizeof(UINT16));
cframesnx = (UINT16*)malloc(nframes * fwidthx * fheightx * sizeof(UINT16));
dynamasks = (UINT8*)malloc(nframes * fwidth * fheight);
dynamasksx = (UINT8*)malloc(nframes * fwidthx * fheightx);
dyna4colsn = (UINT16*)malloc(nframes * MAX_DYNA_SETS_PER_FRAMEN * nocolors * sizeof(UINT16));
dyna4colsnx = (UINT16*)malloc(nframes * MAX_DYNA_SETS_PER_FRAMEN * nocolors * sizeof(UINT16));
isextrasprite = (UINT8*)malloc(nsprites);
framesprites = (UINT8*)malloc(nframes * MAX_SPRITES_PER_FRAME);
framespriteBB = (UINT16*)malloc(nframes * MAX_SPRITES_PER_FRAME * 4 * sizeof(UINT16));
spriteoriginal = (UINT8*)malloc(nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT);
spritecolored = (UINT16*)malloc(nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT * sizeof(UINT16));
spritemaskx = (UINT8*)malloc(nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT);
spritecoloredx = (UINT16*)malloc(nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT * sizeof(UINT16));
activeframes = (UINT8*)malloc(nframes);
colorrotationsn = (UINT16*)malloc(nframes * MAX_LENGTH_COLOR_ROTATION * MAX_COLOR_ROTATIONN * sizeof(UINT16));
colorrotationsnx = (UINT16*)malloc(nframes * MAX_LENGTH_COLOR_ROTATION * MAX_COLOR_ROTATIONN * sizeof(UINT16));
spritedetdwords = (UINT32*)malloc(nsprites * sizeof(UINT32) * MAX_SPRITE_DETECT_AREAS);
spritedetdwordpos = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS);
spritedetareas = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS * 4);
triggerIDs = (UINT32*)malloc(nframes * sizeof(UINT32));
isextrabackground = (UINT8*)malloc(nbackgrounds);
backgroundframesn = (UINT16*)malloc(nbackgrounds * fwidth * fheight * sizeof(UINT16));
backgroundframesnx = (UINT16*)malloc(nbackgrounds * fwidthx * fheightx * sizeof(UINT16));
backgroundIDs = (UINT16*)malloc(nframes * sizeof(UINT16));
backgroundmask = (UINT8*)malloc(nframes * fwidth * fheight);
backgroundmaskx = (UINT8*)malloc(nframes * fwidthx * fheightx);
if (!hashcodes || !shapecompmode || !compmaskID || (ncompmasks > 0 && !compmasks) || !isextraframe ||
!cframesn || !cframesnx || !dynamasks || !dynamasksx || !dyna4colsn || !dyna4colsnx ||
(nsprites > 0 && (!isextrasprite || !spriteoriginal || !spritecolored || !spritemaskx ||
!spritecoloredx || !spritedetdwords || !spritedetdwordpos || !spritedetareas)) ||
!framesprites || !framespriteBB || !activeframes || !colorrotationsn || !colorrotationsnx ||
!triggerIDs || (nbackgrounds > 0 && (!isextrabackground || !backgroundframesn ||
!backgroundframesnx)) || !backgroundIDs || !backgroundmask || !backgroundmaskx)
{
Serum_free();
fclose(pfile);
enabled = false;
return false;
}
my_fread(hashcodes, 4, nframes, pfile);
my_fread(shapecompmode, 1, nframes, pfile);
my_fread(compmaskID, 1, nframes, pfile);
my_fread(compmasks, 1, ncompmasks * fwidth * fheight, pfile);
my_fread(isextraframe, 1, nframes, pfile);
my_fread(cframesn, 2, nframes * fwidth * fheight, pfile);
my_fread(cframesnx, 2, nframes * fwidthx * fheightx, pfile);
my_fread(dynamasks, 1, nframes * fwidth * fheight, pfile);
my_fread(dynamasksx, 1, nframes * fwidthx * fheightx, pfile);
my_fread(dyna4colsn, 2, nframes * MAX_DYNA_SETS_PER_FRAMEN * nocolors, pfile);
my_fread(dyna4colsnx, 2, nframes * MAX_DYNA_SETS_PER_FRAMEN * nocolors, pfile);
my_fread(isextrasprite, 1, nsprites, pfile);
my_fread(framesprites, 1, nframes * MAX_SPRITES_PER_FRAME, pfile);
my_fread(spriteoriginal, 1, nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT, pfile);
my_fread(spritecolored, 2, nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT, pfile);
my_fread(spritemaskx, 1, nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT, pfile);
my_fread(spritecoloredx, 2, nsprites * MAX_SPRITE_WIDTH * MAX_SPRITE_HEIGHT, pfile);
my_fread(activeframes, 1, nframes, pfile);
my_fread(colorrotationsn, 2, nframes * MAX_LENGTH_COLOR_ROTATION * MAX_COLOR_ROTATIONN, pfile);
my_fread(colorrotationsnx, 2, nframes * MAX_LENGTH_COLOR_ROTATION * MAX_COLOR_ROTATIONN, pfile);
my_fread(spritedetdwords, 4, nsprites * MAX_SPRITE_DETECT_AREAS, pfile);
my_fread(spritedetdwordpos, 2, nsprites * MAX_SPRITE_DETECT_AREAS, pfile);
my_fread(spritedetareas, 2, nsprites * 4 * MAX_SPRITE_DETECT_AREAS, pfile);
my_fread(triggerIDs, 4, nframes, pfile);
my_fread(framespriteBB, 2, nframes * MAX_SPRITES_PER_FRAME * 4, pfile);
my_fread(isextrabackground, 1, nbackgrounds, pfile);
my_fread(backgroundframesn, 2, nbackgrounds * fwidth * fheight, pfile);
my_fread(backgroundframesnx, 2, nbackgrounds * fwidthx * fheightx, pfile);
my_fread(backgroundIDs, 2, nframes, pfile);
my_fread(backgroundmask, 1, nframes * fwidth * fheight, pfile);
my_fread(backgroundmaskx, 1, nframes * fwidthx * fheightx, pfile);
fclose(pfile);
if (pntriggers) // check the number of PuP triggers in the file
{
*pntriggers = 0;
for (UINT ti = 0; ti < nframes; ti++)
{
if (triggerIDs[ti] != 0xFFFFFFFF) (*pntriggers)++;
}
}
// allocate memory for previous detected frame
lastframe32 = (UINT16*)malloc(min(fwidth, fwidthx) * 32 * 2);
lastframe64 = (UINT16*)malloc(max(fwidth, fwidthx) * 64 * 2);
lastrotations32 = (UINT16*)malloc(MAX_COLOR_ROTATIONN * MAX_LENGTH_COLOR_ROTATION * 2);
lastrotations64 = (UINT16*)malloc(MAX_COLOR_ROTATIONN * MAX_LENGTH_COLOR_ROTATION * 2);
lastrotationsinframe32 = (UINT16*)malloc(min(fwidth, fwidthx) * 32 * 2 * 2);
lastrotationsinframe64 = (UINT16*)malloc(max(fwidth, fwidthx) * 64 * 2 * 2);
framechecked = (bool*)malloc(sizeof(bool) * nframes);
if (!lastframe32 || !lastframe64 || !lastrotations32 || !lastrotations64 || !lastrotationsinframe32 || !lastrotationsinframe64 || !framechecked)
{
Serum_free();
enabled = false;
return false;
}
memset(lastframe32, 0, min(fwidth, fwidthx) * 32 * 2);
memset(lastframe64, 0, max(fwidth, fwidthx) * 64 * 2);
memset(lastrotations32, 0, MAX_COLOR_ROTATIONN * MAX_LENGTH_COLOR_ROTATION * 2);
memset(lastrotations64, 0, MAX_COLOR_ROTATIONN * MAX_LENGTH_COLOR_ROTATION * 2);
memset(lastrotationsinframe32, 0xff, min(fwidth, fwidthx) * 32 * 2 * 2);
memset(lastrotationsinframe64, 0xff, max(fwidth, fwidthx) * 64 * 2 * 2);
memset(lastsprite, 255, MAX_SPRITES_PER_FRAME);
if (flags & FLAG_REQUEST_32P_FRAMES)
{
if (fheight == 32) *width32 = fwidth;
else *width32 = fwidthx;
}
else *width32 = 0;
if (flags & FLAG_REQUEST_64P_FRAMES)
{
if (fheight == 32) *width64 = fwidthx;
else *width64 = fwidth;
}
else *width64 = 0;
if (pnocolors) *pnocolors = nocolors;
Reset_ColorRotations();
cromloaded = true;
if (!uncompressedCROM)
{
// remove temporary file that had been extracted from compressed CRZ file
remove(pathbuf);
}
if (IS_DEBUG_READ) fclose(fconsole);
enabled = true;
return true;
}
SERUM_API bool Serum_LoadFile(const char* const filename, unsigned int* pnocolors, unsigned int* pntriggers, UINT8 flags, UINT* width32, UINT* width64)
{
char pathbuf[pathbuflen];
ac_pos_in_file = 0;
if (!crc32_ready) CRC32encode();
// check if we're using an uncompressed cROM file
const char* ext;
bool uncompressedCROM = false;
if ((ext = strrchr(filename, '.')) != NULL)
{
if (strcmp(ext, ".cROM") == 0)
{
uncompressedCROM = true;
if (strcpy_s(pathbuf, pathbuflen, filename)) return false;
}
}
// extract file if it is compressed
if (!uncompressedCROM)
{
char cromname[pathbuflen];
#if !(defined(__APPLE__) && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_TV) && TARGET_OS_TV)))
if (strcpy_s(pathbuf, pathbuflen, filename)) return false;
#else
if (strcpy_s(pathbuf, pathbuflen, getenv("TMPDIR"))) return false;
if (strcat_s(pathbuf, pathbuflen, "/")) return false;
#endif
if (!unzip_crz(filename, pathbuf, cromname, pathbuflen)) return false;
if (strcat_s(pathbuf, pathbuflen, cromname)) return false;
}
// Open cRom
FILE* pfile;
pfile = fopen(pathbuf, "rb");
if (!pfile)
{
enabled = false;
return false;
}
if (IS_DEBUG_READ)
{
fconsole = freopen("e:\\output.txt", "w", stdout);
fseek(pfile, 0, SEEK_END);
serum_file_length = ftell(pfile);
fseek(pfile, 0, SEEK_SET);
}
// read the header to know how much memory is needed
my_fread(rname, 1, 64, pfile);
UINT32 sizeheader;
my_fread(&sizeheader, 4, 1, pfile);
// if this is a new format file, we load with Serum_LoadNewFile()
if (sizeheader >= 14 * sizeof(UINT)) return Serum_LoadNewFile(pfile, pnocolors, pntriggers, uncompressedCROM, pathbuf, flags, width32, width64);
newformat = false;
fread(&fwidth, 4, 1, pfile);
fread(&fheight, 4, 1, pfile);
// The serum file stored the number of frames as UINT32, but in fact, the
// number of frames will never exceed the size of UINT16 (65535)
UINT32 nframes32;
fread(&nframes32, 4, 1, pfile);
nframes = (UINT16)nframes32;
fread(&nocolors, 4, 1, pfile);
fread(&nccolors, 4, 1, pfile);
if ((fwidth == 0) || (fheight == 0) || (nframes == 0) || (nocolors == 0) || (nccolors == 0))
{
// incorrect file format
fclose(pfile);
enabled = false;
return false;
}
fread(&ncompmasks, 4, 1, pfile);
fread(&nmovmasks, 4, 1, pfile);
fread(&nsprites, 4, 1, pfile);
if (sizeheader >= 13 * sizeof(UINT))
fread(&nbackgrounds, 2, 1, pfile);
else
nbackgrounds = 0;
// allocate memory for the serum format
hashcodes = (UINT32*)malloc(sizeof(UINT32) * nframes);
shapecompmode = (UINT8*)malloc(nframes);
compmaskID = (UINT8*)malloc(nframes);
movrctID = (UINT8*)malloc(nframes);
compmasks = (UINT8*)malloc(ncompmasks * fwidth * fheight);
movrcts = (UINT8*)malloc(nmovmasks * 4);
cpal = (UINT8*)malloc(nframes * 3 * nccolors);
cframes = (UINT8*)malloc(nframes * fwidth * fheight);
dynamasks = (UINT8*)malloc(nframes * fwidth * fheight);
dyna4cols = (UINT8*)malloc(nframes * MAX_DYNA_4COLS_PER_FRAME * nocolors);
framesprites = (UINT8*)malloc(nframes * MAX_SPRITES_PER_FRAME);
framespriteBB = (UINT16*)malloc(nframes * MAX_SPRITES_PER_FRAME * 4 * sizeof(UINT16));
spritedescriptionso = (UINT8*)malloc(nsprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE);
spritedescriptionsc = (UINT8*)malloc(nsprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE);
activeframes = (UINT8*)malloc(nframes);
colorrotations = (UINT8*)malloc(nframes * 3 * MAX_COLOR_ROTATIONS);
spritedetdwords = (UINT32*)malloc(nsprites * sizeof(UINT32) * MAX_SPRITE_DETECT_AREAS);
spritedetdwordpos = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS);
spritedetareas = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS * 4);
triggerIDs = (UINT32*)malloc(nframes * sizeof(UINT32));
backgroundframes = (UINT8*)malloc(nbackgrounds * fwidth * fheight);
backgroundIDs = (UINT16*)malloc(nframes * sizeof(UINT16));
backgroundBB = (UINT16*)malloc(nframes * 4 * sizeof(UINT16));
if (!hashcodes || !shapecompmode || !compmaskID || !movrctID || !cpal ||
!cframes || !dynamasks || !dyna4cols || !framesprites || !framespriteBB ||
!activeframes || !colorrotations || !triggerIDs ||
(!compmasks && ncompmasks > 0) || (!movrcts && nmovmasks > 0) ||
((nsprites > 0) &&
(!spritedescriptionso || !spritedescriptionsc || !spritedetdwords ||
!spritedetdwordpos || !spritedetareas)) ||
((nbackgrounds > 0) &&
(!backgroundframes || !backgroundIDs || !backgroundBB))) {
Serum_free();
fclose(pfile);
enabled = false;
return false;
}
// read the cRom file
fread(hashcodes, sizeof(UINT32), nframes, pfile);
fread(shapecompmode, 1, nframes, pfile);
fread(compmaskID, 1, nframes, pfile);
fread(movrctID, 1, nframes, pfile);
fread(compmasks, 1, ncompmasks * fwidth * fheight, pfile);
fread(movrcts, 1, nmovmasks * fwidth * fheight, pfile);
fread(cpal, 1, nframes * 3 * nccolors, pfile);
fread(cframes, 1, nframes * fwidth * fheight, pfile);
fread(dynamasks, 1, nframes * fwidth * fheight, pfile);
fread(dyna4cols, 1, nframes * MAX_DYNA_4COLS_PER_FRAME * nocolors, pfile);
fread(framesprites, 1, nframes * MAX_SPRITES_PER_FRAME, pfile);
for (int ti = 0; ti < (int)nsprites * MAX_SPRITE_SIZE * MAX_SPRITE_SIZE; ti++)
{
fread(&spritedescriptionsc[ti], 1, 1, pfile);
fread(&spritedescriptionso[ti], 1, 1, pfile);
}
fread(activeframes, 1, nframes, pfile);
fread(colorrotations, 1, nframes * 3 * MAX_COLOR_ROTATIONS, pfile);
fread(spritedetdwords, sizeof(UINT32), nsprites * MAX_SPRITE_DETECT_AREAS, pfile);
fread(spritedetdwordpos, sizeof(UINT16), nsprites * MAX_SPRITE_DETECT_AREAS, pfile);
fread(spritedetareas, sizeof(UINT16), nsprites * 4 * MAX_SPRITE_DETECT_AREAS, pfile);
if (sizeheader >= 11 * sizeof(UINT)) fread(triggerIDs, sizeof(UINT32), nframes, pfile);
else memset(triggerIDs, 0xFF, sizeof(UINT32) * nframes);
if (sizeheader >= 12 * sizeof(UINT)) fread(framespriteBB, sizeof(UINT16), nframes * MAX_SPRITES_PER_FRAME * 4, pfile);
else
{
for (UINT tj = 0; tj < nframes; tj++)
{
for (UINT ti = 0; ti < MAX_SPRITES_PER_FRAME; ti++)
{
framespriteBB[tj * MAX_SPRITES_PER_FRAME * 4 + ti * 4] = 0;
framespriteBB[tj * MAX_SPRITES_PER_FRAME * 4 + ti * 4 + 1] = 0;
framespriteBB[tj * MAX_SPRITES_PER_FRAME * 4 + ti * 4 + 2] = fwidth - 1;
framespriteBB[tj * MAX_SPRITES_PER_FRAME * 4 + ti * 4 + 3] =
fheight - 1;
}
}
}
if (sizeheader >= 13 * sizeof(UINT))
{
fread(backgroundframes, fwidth * fheight, nbackgrounds, pfile);
fread(backgroundIDs, sizeof(UINT16), nframes, pfile);
fread(backgroundBB, 4 * sizeof(UINT16), nframes, pfile);
}
else memset(backgroundIDs, 0xFF, nframes * sizeof(UINT16));
fclose(pfile);
if (IS_DEBUG_READ) fclose(fconsole);
if (pntriggers)
{
*pntriggers = 0;
for (UINT ti = 0; ti < nframes; ti++)
{
if (triggerIDs[ti] != 0xFFFFFFFF) (*pntriggers)++;
}
}
// allocate memory for previous detected frame
lastframe = (UINT8*)malloc(fwidth * fheight);
lastpalette = (UINT8*)malloc(nccolors * 3);
lastrotations = (UINT8*)malloc(3 * MAX_COLOR_ROTATIONS);
framechecked = (bool*)malloc(sizeof(bool) * nframes);
if (!lastframe || !lastpalette || !lastrotations || !framechecked) {
Serum_free();
enabled = false;
return false;
}
memset(lastframe, 0, fwidth * fheight);
memset(lastpalette, 0, nccolors * 3);
memset(lastrotations, 255, 3 * MAX_COLOR_ROTATIONS);
memset(lastsprite, 255, MAX_SPRITE_TO_DETECT);
if (fheight == 64)
{
*width64 = fwidth;
*width32 = 0;
}
else
{
*width32 = fwidth;
*width64 = 0;
}
if (pnocolors)
{
*pnocolors = nocolors;
}
Reset_ColorRotations();
cromloaded = true;
if (!uncompressedCROM)
{
// remove temporary file that had been extracted from compressed CRZ file
remove(pathbuf);
}
enabled = true;
return true;
}
SERUM_API bool Serum_Load(const char* const altcolorpath, const char* const romname, unsigned int* pnocolors, unsigned int* pntriggers, UINT8 flags, UINT* width32, UINT* width64, UINT8* isnewformat)
{
Serum_flags = flags;
char pathbuf[pathbuflen];
if (strcpy_s(pathbuf, pathbuflen, altcolorpath) || ((pathbuf[strlen(pathbuf) - 1] != '\\') && (pathbuf[strlen(pathbuf) - 1] != '/') &&
strcat_s(pathbuf, pathbuflen, "/")) || strcat_s(pathbuf, pathbuflen, romname) || strcat_s(pathbuf, pathbuflen, "/") ||
strcat_s(pathbuf, pathbuflen, romname) || strcat_s(pathbuf, pathbuflen, ".cRZ"))
{
enabled = false;
return false;
}
bool bres = Serum_LoadFile(pathbuf, pnocolors, pntriggers, flags, width32, width64);
if (newformat) *isnewformat = 1; else *isnewformat = 0;
return bres;
}
SERUM_API void Serum_Dispose(void)
{
Serum_free();
}
int32_t Identify_Frame(UINT8* frame)
{
// Usually the first frame has the ID 0, but lastfound is also initialized
// with 0. So we need a helper to be able to detect frame 0 as new.
static bool first_match = true;
if (!cromloaded) return IDENTIFY_NO_FRAME;
UINT8* pmask;
memset(framechecked, false, nframes);
UINT16 tj = lastfound; // we start from the frame we last found
UINT pixels = fwidth * fheight;
do
{
if (!framechecked[tj])
{
// calculate the hashcode for the generated frame with the mask and
// shapemode of the current crom frame
UINT8 mask = compmaskID[tj];
UINT8 Shape = shapecompmode[tj];
UINT32 Hashc;
if (mask < 255)
{
pmask = &compmasks[mask * pixels];
Hashc = crc32_fast_mask(frame, pmask, pixels, Shape);
}
else Hashc = crc32_fast(frame, pixels, Shape);
// now we can compare with all the crom frames that share these same mask
// and shapemode
UINT16 ti = tj;
do
{
if (!framechecked[ti])
{
if ((compmaskID[ti] == mask) && (shapecompmode[ti] == Shape))
{
if (Hashc == hashcodes[ti])
{
if (first_match || ti != lastfound || mask < 255)
{
Reset_ColorRotations();
lastfound = ti;
lastframe_full_crc = crc32_fast(frame, pixels, 0);
first_match = false;
return ti; // we found the frame, we return it
}
UINT32 full_crc = crc32_fast(frame, pixels, 0);
if (full_crc != lastframe_full_crc)
{
lastframe_full_crc = full_crc;
return ti; // we found the same frame with shape as before, but
// the full frame is different
}
return IDENTIFY_SAME_FRAME; // we found the frame, but it is the
// same full frame as before (no
// mask)
}
framechecked[ti] = true;
}
}
if (++ti >= nframes) ti = 0;
} while (ti != tj);
}
if (++tj >= nframes) tj = 0;
} while (tj != lastfound);
return IDENTIFY_NO_FRAME; // we found no corresponding frame
}
void GetSpriteSize(int nospr, int* pswid, int* pshei, UINT8* pspro, int sswid, int sshei)
{
*pswid = *pshei = 0;
if (nospr >= nsprites) return;
for (int tj = 0; tj < sshei; tj++)
{
for (int ti = 0; ti < sswid; ti++)
{
if (pspro[nospr * sswid * sshei + tj * sswid + ti] < 255)
{
if (tj > *pshei) *pshei = tj;
if (ti > *pswid) *pswid = ti;
}
}
}
(*pshei)++;
(*pswid)++;
}
bool Check_Sprites(UINT8* Frame, int quelleframe, UINT8* pquelsprites, UINT8* nspr, UINT16* pfrx, UINT16* pfry, UINT16* pspx, UINT16* pspy, UINT16* pwid, UINT16* phei)
{
UINT8 ti = 0;
UINT32 mdword;
*nspr = 0;
int spr_width, spr_height;
UINT8* pspro;
if (newformat)
{
spr_width = MAX_SPRITE_WIDTH;
spr_height = MAX_SPRITE_HEIGHT;
pspro = spriteoriginal;
}
else
{
spr_width = MAX_SPRITE_SIZE;
spr_height = MAX_SPRITE_SIZE;
pspro = spritedescriptionso;
}
while ((ti < MAX_SPRITES_PER_FRAME) && (framesprites[quelleframe * MAX_SPRITES_PER_FRAME + ti] < 255))
{
UINT8 qspr = framesprites[quelleframe * MAX_SPRITES_PER_FRAME + ti];
int spw, sph;
GetSpriteSize(qspr, &spw, &sph, pspro, spr_width, spr_height);
short minxBB = (short)framespriteBB[(quelleframe * MAX_SPRITES_PER_FRAME + ti) * 4];
short minyBB = (short)framespriteBB[(quelleframe * MAX_SPRITES_PER_FRAME + ti) * 4 + 1];
short maxxBB = (short)framespriteBB[(quelleframe * MAX_SPRITES_PER_FRAME + ti) * 4 + 2];
short maxyBB = (short)framespriteBB[(quelleframe * MAX_SPRITES_PER_FRAME + ti) * 4 + 3];
for (UINT32 tm = 0; tm < MAX_SPRITE_DETECT_AREAS; tm++)
{
if (spritedetareas[qspr * MAX_SPRITE_DETECT_AREAS * 4 + tm * 4] == 0xffff) continue;
// we look for the sprite in the frame sent
for (short ty = minyBB; ty <= maxyBB; ty++)
{
mdword = (UINT32)(Frame[ty * fwidth + minxBB] << 8) | (UINT32)(Frame[ty * fwidth + minxBB + 1] << 16) |
(UINT32)(Frame[ty * fwidth + minxBB + 2] << 24);
for (short tx = minxBB; tx <= maxxBB - 3; tx++)
{
UINT tj = ty * fwidth + tx;
mdword = (mdword >> 8) | (UINT32)(Frame[tj + 3] << 24);
// we look for the magic dword first:
UINT16 sddp = spritedetdwordpos[qspr * MAX_SPRITE_DETECT_AREAS + tm];
if (mdword == spritedetdwords[qspr * MAX_SPRITE_DETECT_AREAS + tm])
{
short frax = (short)tx; // position in the frame of the detection dword
short fray = (short)ty;
short sprx = (short)(sddp % spr_width); // position in the sprite of the detection dword
short spry = (short)(sddp / spr_width);
// details of the det area:
short detx = (short)spritedetareas[qspr * MAX_SPRITE_DETECT_AREAS * 4 + tm * 4]; // position of the detection area in the sprite
short dety = (short)spritedetareas[qspr * MAX_SPRITE_DETECT_AREAS * 4 + tm * 4 + 1];
short detw = (short)spritedetareas[qspr * MAX_SPRITE_DETECT_AREAS * 4 + tm * 4 + 2]; // size of the detection area
short deth = (short)spritedetareas[qspr * MAX_SPRITE_DETECT_AREAS * 4 + tm * 4 + 3];
// if the detection area starts before the frame (left or top), continue:
if ((frax - minxBB < sprx - detx) || (fray - minyBB < spry - dety)) continue;
// position of the detection area in the frame
int offsx = frax - sprx + detx;
int offsy = fray - spry + dety;
// if the detection area extends beyond the bounding box (right or bottom), continue:
if ((offsx + detw > (int)maxxBB) || (offsy + deth > (int)maxyBB)) continue;
// we can now check if the full detection area is around the found detection dword
bool notthere = false;
for (UINT16 tk = 0; tk < deth; tk++)
{
for (UINT16 tl = 0; tl < detw; tl++)
{
UINT8 val = pspro[qspr * spr_height * spr_width + (tk + dety) * spr_width + tl + detx];
if (val == 255) continue;
if (val != Frame[(tk + offsy) * fwidth + tl + offsx])
{
notthere = true;
break;
}
}
if (notthere == true) break;
}
if (!notthere)
{
pquelsprites[*nspr] = qspr;
if (frax - minxBB < sprx)
{
pspx[*nspr] = (UINT16)(sprx - (frax - minxBB)); // display sprite from point
pfrx[*nspr] = (UINT16)minxBB;
pwid[*nspr] = MIN((UINT16)(maxxBB - minxBB + 1), (UINT16)(spw - pspx[*nspr]));
}
else
{
pspx[*nspr] = 0;
pfrx[*nspr] = (UINT16)(frax - sprx);
pwid[*nspr] = MIN((UINT16)(maxxBB - minxBB + 1), (UINT16)(maxxBB - frax + sprx));
//pwid[*nspr] = MIN((UINT16)(maxxBB + 1 - pfrx[*nspr]), (UINT16)(spr_width - pfrx[*nspr]));
}
if (fray - minyBB < spry)
{
pspy[*nspr] = (UINT16)(spry - (fray - minyBB));
pfry[*nspr] = (UINT16)minyBB;
phei[*nspr] = MIN((UINT16)(maxyBB - minyBB + 1), (UINT16)(sph - fray + spry));
//phei[*nspr] = MIN((UINT16)(maxyBB - minyBB + 1), (UINT16)(spw - pspy[*nspr]));
}
else
{
pspy[*nspr] = 0;
pfry[*nspr] = (UINT16)(fray - spry);
phei[*nspr] = MIN((UINT16)(maxyBB + 1 - pfry[*nspr]), (UINT16)(spr_height - pfry[*nspr]));
}
// we check the identical sprites as there may be duplicate due to
// the multi detection zones
bool identicalfound = false;
for (UINT8 tk = 0; tk < *nspr; tk++)
{
if ((pquelsprites[*nspr] == pquelsprites[tk]) &&
(pfrx[*nspr] == pfrx[tk]) && (pfry[*nspr] == pfry[tk]) &&
(pwid[*nspr] == pwid[tk]) && (phei[*nspr] == phei[tk]))
identicalfound = true;
}
if (!identicalfound)
{
(*nspr)++;
if (*nspr == MAX_SPRITE_TO_DETECT) return true;
}
}
}
}
}
}
ti++;
}
if (*nspr > 0) return true;
return false;
}
void Colorize_Frame(UINT8* inboundframe, UINT8* colorizedframe, int IDfound)
{
UINT16 tj, ti;
// Generate the colorized version of a frame once identified in the crom
// frames
for (tj = 0; tj < fheight; tj++)
{
for (ti = 0; ti < fwidth; ti++)
{
UINT16 tk = tj * fwidth + ti;
if ((backgroundIDs[IDfound] < nbackgrounds) && (inboundframe[tk] == 0) && (ti >= backgroundBB[IDfound * 4]) &&
(tj >= backgroundBB[IDfound * 4 + 1]) && (ti <= backgroundBB[IDfound * 4 + 2]) &&
(tj <= backgroundBB[IDfound * 4 + 3]))
colorizedframe[tk] = backgroundframes[backgroundIDs[IDfound] * fwidth * fheight + tk];
else
{
UINT8 dynacouche = dynamasks[IDfound * fwidth * fheight + tk];
if (dynacouche == 255) colorizedframe[tk] = cframes[IDfound * fwidth * fheight + tk];
else colorizedframe[tk] = dyna4cols[IDfound * MAX_DYNA_4COLS_PER_FRAME * nocolors + dynacouche * nocolors + inboundframe[tk]];
}
}
}
}
bool CheckExtraFrameAvailable(UINT frID)