1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The bottom part of this is file is adapted from SDL_rotozoom.c. The
* relevant copyright notice for those specific functions can be found at the
* top of that section.
*
*/
#include "common/algorithm.h"
#include "common/endian.h"
#include "common/util.h"
#include "common/rect.h"
#include "common/math.h"
#include "common/textconsole.h"
#include "graphics/primitives.h"
#include "graphics/transparent_surface.h"
#include "graphics/transform_tools.h"
namespace Graphics {
static const int kBModShift = 0;//img->format.bShift;
static const int kGModShift = 8;//img->format.gShift;
static const int kRModShift = 16;//img->format.rShift;
static const int kAModShift = 24;//img->format.aShift;
#ifdef SCUMM_LITTLE_ENDIAN
static const int kAIndex = 0;
static const int kBIndex = 1;
static const int kGIndex = 2;
static const int kRIndex = 3;
#else
static const int kAIndex = 3;
static const int kBIndex = 2;
static const int kGIndex = 1;
static const int kRIndex = 0;
#endif
void doBlitOpaqueFast(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep);
void doBlitBinaryFast(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep);
void doBlitAlphaBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color);
void doBlitAdditiveBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color);
void doBlitSubtractiveBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color);
TransparentSurface::TransparentSurface() : Surface(), _alphaMode(ALPHA_FULL) {}
TransparentSurface::TransparentSurface(const Surface &surf, bool copyData) : Surface(), _alphaMode(ALPHA_FULL) {
if (copyData) {
copyFrom(surf);
} else {
w = surf.w;
h = surf.h;
pitch = surf.pitch;
format = surf.format;
// We need to cast the const qualifier away here because 'pixels'
// always needs to be writable. 'surf' however is a constant Surface,
// thus getPixels will always return const pixel data.
pixels = const_cast<void *>(surf.getPixels());
}
}
/**
* Optimized version of doBlit to be used w/opaque blitting (no alpha).
*/
void doBlitOpaqueFast(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep) {
byte *in;
byte *out;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
memcpy(out, in, width * 4);
for (uint32 j = 0; j < width; j++) {
out[kAIndex] = 0xFF;
out += 4;
}
outo += pitch;
ino += inoStep;
}
}
/**
* Optimized version of doBlit to be used w/binary blitting (blit or no-blit, no blending).
*/
void doBlitBinaryFast(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep) {
byte *in;
byte *out;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
uint32 pix = *(uint32 *)in;
int a = in[kAIndex];
if (a != 0) { // Full opacity (Any value not exactly 0 is Opaque here)
*(uint32 *)out = pix;
out[kAIndex] = 0xFF;
}
out += 4;
in += inStep;
}
outo += pitch;
ino += inoStep;
}
}
/**
* Optimized version of doBlit to be used with alpha blended blitting
* @param ino a pointer to the input surface
* @param outo a pointer to the output surface
* @param width width of the input surface
* @param height height of the input surface
* @param pitch pitch of the output surface - that is, width in bytes of every row, usually bpp * width of the TARGET surface (the area we are blitting to might be smaller, do the math)
* @inStep size in bytes to skip to address each pixel, usually bpp of the source surface
* @inoStep width in bytes of every row on the *input* surface / kind of like pitch
* @color colormod in 0xAARRGGBB format - 0xFFFFFFFF for no colormod
*/
void doBlitAlphaBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color) {
byte *in;
byte *out;
if (color == 0xffffffff) {
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
if (in[kAIndex] != 0) {
out[kAIndex] = 255;
out[kRIndex] = ((in[kRIndex] * in[kAIndex]) + out[kRIndex] * (255 - in[kAIndex])) >> 8;
out[kGIndex] = ((in[kGIndex] * in[kAIndex]) + out[kGIndex] * (255 - in[kAIndex])) >> 8;
out[kBIndex] = ((in[kBIndex] * in[kAIndex]) + out[kBIndex] * (255 - in[kAIndex])) >> 8;
}
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
} else {
byte ca = (color >> kAModShift) & 0xFF;
byte cr = (color >> kRModShift) & 0xFF;
byte cg = (color >> kGModShift) & 0xFF;
byte cb = (color >> kBModShift) & 0xFF;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
uint32 ina = in[kAIndex] * ca >> 8;
out[kAIndex] = 255;
out[kBIndex] = (out[kBIndex] * (255 - ina) >> 8);
out[kGIndex] = (out[kGIndex] * (255 - ina) >> 8);
out[kRIndex] = (out[kRIndex] * (255 - ina) >> 8);
out[kBIndex] = out[kBIndex] + (in[kBIndex] * ina * cb >> 16);
out[kGIndex] = out[kGIndex] + (in[kGIndex] * ina * cg >> 16);
out[kRIndex] = out[kRIndex] + (in[kRIndex] * ina * cr >> 16);
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
}
}
/**
* Optimized version of doBlit to be used with additive blended blitting
*/
void doBlitAdditiveBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color) {
byte *in;
byte *out;
if (color == 0xffffffff) {
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
if (in[kAIndex] != 0) {
out[kRIndex] = MIN((in[kRIndex] * in[kAIndex] >> 8) + out[kRIndex], 255);
out[kGIndex] = MIN((in[kGIndex] * in[kAIndex] >> 8) + out[kGIndex], 255);
out[kBIndex] = MIN((in[kBIndex] * in[kAIndex] >> 8) + out[kBIndex], 255);
}
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
} else {
byte ca = (color >> kAModShift) & 0xFF;
byte cr = (color >> kRModShift) & 0xFF;
byte cg = (color >> kGModShift) & 0xFF;
byte cb = (color >> kBModShift) & 0xFF;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
uint32 ina = in[kAIndex] * ca >> 8;
if (cb != 255) {
out[kBIndex] = MIN<uint>(out[kBIndex] + ((in[kBIndex] * cb * ina) >> 16), 255u);
} else {
out[kBIndex] = MIN<uint>(out[kBIndex] + (in[kBIndex] * ina >> 8), 255u);
}
if (cg != 255) {
out[kGIndex] = MIN<uint>(out[kGIndex] + ((in[kGIndex] * cg * ina) >> 16), 255u);
} else {
out[kGIndex] = MIN<uint>(out[kGIndex] + (in[kGIndex] * ina >> 8), 255u);
}
if (cr != 255) {
out[kRIndex] = MIN<uint>(out[kRIndex] + ((in[kRIndex] * cr * ina) >> 16), 255u);
} else {
out[kRIndex] = MIN<uint>(out[kRIndex] + (in[kRIndex] * ina >> 8), 255u);
}
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
}
}
/**
* Optimized version of doBlit to be used with subtractive blended blitting
*/
void doBlitSubtractiveBlend(byte *ino, byte *outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep, uint32 color) {
byte *in;
byte *out;
if (color == 0xffffffff) {
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
if (in[kAIndex] != 0) {
out[kRIndex] = MAX(out[kRIndex] - ((in[kRIndex] * out[kRIndex]) * in[kAIndex] >> 16), 0);
out[kGIndex] = MAX(out[kGIndex] - ((in[kGIndex] * out[kGIndex]) * in[kAIndex] >> 16), 0);
out[kBIndex] = MAX(out[kBIndex] - ((in[kBIndex] * out[kBIndex]) * in[kAIndex] >> 16), 0);
}
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
} else {
byte cr = (color >> kRModShift) & 0xFF;
byte cg = (color >> kGModShift) & 0xFF;
byte cb = (color >> kBModShift) & 0xFF;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
out[kAIndex] = 255;
if (cb != 255) {
out[kBIndex] = MAX(out[kBIndex] - ((in[kBIndex] * cb * (out[kBIndex]) * in[kAIndex]) >> 24), 0);
} else {
out[kBIndex] = MAX(out[kBIndex] - (in[kBIndex] * (out[kBIndex]) * in[kAIndex] >> 16), 0);
}
if (cg != 255) {
out[kGIndex] = MAX(out[kGIndex] - ((in[kGIndex] * cg * (out[kGIndex]) * in[kAIndex]) >> 24), 0);
} else {
out[kGIndex] = MAX(out[kGIndex] - (in[kGIndex] * (out[kGIndex]) * in[kAIndex] >> 16), 0);
}
if (cr != 255) {
out[kRIndex] = MAX(out[kRIndex] - ((in[kRIndex] * cr * (out[kRIndex]) * in[kAIndex]) >> 24), 0);
} else {
out[kRIndex] = MAX(out[kRIndex] - (in[kRIndex] * (out[kRIndex]) * in[kAIndex] >> 16), 0);
}
in += inStep;
out += 4;
}
outo += pitch;
ino += inoStep;
}
}
}
Common::Rect TransparentSurface::blit(Graphics::Surface &target, int posX, int posY, int flipping, Common::Rect *pPartRect, uint color, int width, int height, TSpriteBlendMode blendMode) {
Common::Rect retSize;
retSize.top = 0;
retSize.left = 0;
retSize.setWidth(0);
retSize.setHeight(0);
// Check if we need to draw anything at all
int ca = (color >> kAModShift) & 0xff;
if (ca == 0) {
return retSize;
}
// Create an encapsulating surface for the data
TransparentSurface srcImage(*this, false);
// TODO: Is the data really in the screen format?
if (format.bytesPerPixel != 4) {
warning("TransparentSurface can only blit 32bpp images, but got %d", format.bytesPerPixel * 8);
return retSize;
}
if (pPartRect) {
int xOffset = pPartRect->left;
int yOffset = pPartRect->top;
if (flipping & FLIP_V) {
yOffset = srcImage.h - pPartRect->bottom;
}
if (flipping & FLIP_H) {
xOffset = srcImage.w - pPartRect->right;
}
srcImage.pixels = getBasePtr(xOffset, yOffset);
srcImage.w = pPartRect->width();
srcImage.h = pPartRect->height();
debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping,
pPartRect->left, pPartRect->top, pPartRect->width(), pPartRect->height(), color, width, height);
} else {
debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping, 0, 0,
srcImage.w, srcImage.h, color, width, height);
}
if (width == -1) {
width = srcImage.w;
}
if (height == -1) {
height = srcImage.h;
}
#ifdef SCALING_TESTING
// Hardcode scaling to 66% to test scaling
width = width * 2 / 3;
height = height * 2 / 3;
#endif
Graphics::Surface *img = nullptr;
Graphics::Surface *imgScaled = nullptr;
byte *savedPixels = nullptr;
if ((width != srcImage.w) || (height != srcImage.h)) {
// Scale the image
img = imgScaled = srcImage.scale(width, height);
savedPixels = (byte *)img->getPixels();
} else {
img = &srcImage;
}
// Handle off-screen clipping
if (posY < 0) {
img->h = MAX(0, (int)img->h - -posY);
img->setPixels((byte *)img->getBasePtr(0, -posY));
posY = 0;
}
if (posX < 0) {
img->w = MAX(0, (int)img->w - -posX);
img->setPixels((byte *)img->getBasePtr(-posX, 0));
posX = 0;
}
img->w = CLIP((int)img->w, 0, (int)MAX((int)target.w - posX, 0));
img->h = CLIP((int)img->h, 0, (int)MAX((int)target.h - posY, 0));
if ((img->w > 0) && (img->h > 0)) {
int xp = 0, yp = 0;
int inStep = 4;
int inoStep = img->pitch;
if (flipping & FLIP_H) {
inStep = -inStep;
xp = img->w - 1;
}
if (flipping & FLIP_V) {
inoStep = -inoStep;
yp = img->h - 1;
}
byte *ino = (byte *)img->getBasePtr(xp, yp);
byte *outo = (byte *)target.getBasePtr(posX, posY);
if (color == 0xFFFFFFFF && blendMode == BLEND_NORMAL && _alphaMode == ALPHA_OPAQUE) {
doBlitOpaqueFast(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
} else if (color == 0xFFFFFFFF && blendMode == BLEND_NORMAL && _alphaMode == ALPHA_BINARY) {
doBlitBinaryFast(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
} else {
if (blendMode == BLEND_ADDITIVE) {
doBlitAdditiveBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
} else if (blendMode == BLEND_SUBTRACTIVE) {
doBlitSubtractiveBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
} else {
assert(blendMode == BLEND_NORMAL);
doBlitAlphaBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
}
}
}
retSize.setWidth(img->w);
retSize.setHeight(img->h);
if (imgScaled) {
imgScaled->setPixels(savedPixels);
imgScaled->free();
delete imgScaled;
}
return retSize;
}
Common::Rect TransparentSurface::blitClip(Graphics::Surface &target, Common::Rect clippingArea, int posX, int posY, int flipping, Common::Rect *pPartRect, uint color, int width, int height, TSpriteBlendMode blendMode) {
Common::Rect retSize;
retSize.top = 0;
retSize.left = 0;
retSize.setWidth(0);
retSize.setHeight(0);
// Check if we need to draw anything at all
int ca = (color >> kAModShift) & 0xff;
if (ca == 0) {
return retSize;
}
// Create an encapsulating surface for the data
TransparentSurface srcImage(*this, false);
// TODO: Is the data really in the screen format?
if (format.bytesPerPixel != 4) {
warning("TransparentSurface can only blit 32bpp images, but got %d", format.bytesPerPixel * 8);
return retSize;
}
if (pPartRect) {
int xOffset = pPartRect->left;
int yOffset = pPartRect->top;
if (flipping & FLIP_V) {
yOffset = srcImage.h - pPartRect->bottom;
}
if (flipping & FLIP_H) {
xOffset = srcImage.w - pPartRect->right;
}
srcImage.pixels = getBasePtr(xOffset, yOffset);
srcImage.w = pPartRect->width();
srcImage.h = pPartRect->height();
debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping,
pPartRect->left, pPartRect->top, pPartRect->width(), pPartRect->height(), color, width, height);
} else {
debug(6, "Blit(%d, %d, %d, [%d, %d, %d, %d], %08x, %d, %d)", posX, posY, flipping, 0, 0,
srcImage.w, srcImage.h, color, width, height);
}
if (width == -1) {
width = srcImage.w;
}
if (height == -1) {
height = srcImage.h;
}
#ifdef SCALING_TESTING
// Hardcode scaling to 66% to test scaling
width = width * 2 / 3;
height = height * 2 / 3;
#endif
Graphics::Surface *img = nullptr;
Graphics::Surface *imgScaled = nullptr;
byte *savedPixels = nullptr;
if ((width != srcImage.w) || (height != srcImage.h)) {
// Scale the image
img = imgScaled = srcImage.scale(width, height);
savedPixels = (byte *)img->getPixels();
} else {
img = &srcImage;
}
// Handle off-screen clipping
if (posY < clippingArea.top) {
img->h = MAX(0, (int)img->h - (clippingArea.top - posY));
img->setPixels((byte *)img->getBasePtr(0, clippingArea.top - posY));
posY = clippingArea.top;
}
if (posX < clippingArea.left) {
img->w = MAX(0, (int)img->w - (clippingArea.left - posX));
img->setPixels((byte *)img->getBasePtr(clippingArea.left - posX, 0));
posX = clippingArea.left;
}
img->w = CLIP((int)img->w, 0, (int)MAX((int)clippingArea.right - posX, 0));
img->h = CLIP((int)img->h, 0, (int)MAX((int)clippingArea.bottom - posY, 0));
if ((img->w > 0) && (img->h > 0)) {
int xp = 0, yp = 0;
int inStep = 4;
int inoStep = img->pitch;
if (flipping & FLIP_H) {
inStep = -inStep;
xp = img->w - 1;
}
if (flipping & FLIP_V) {
inoStep = -inoStep;
yp = img->h - 1;
}
byte *ino = (byte *)img->getBasePtr(xp, yp);
byte *outo = (byte *)target.getBasePtr(posX, posY);
if (color == 0xFFFFFFFF && blendMode == BLEND_NORMAL && _alphaMode == ALPHA_OPAQUE) {
doBlitOpaqueFast(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
} else if (color == 0xFFFFFFFF && blendMode == BLEND_NORMAL && _alphaMode == ALPHA_BINARY) {
doBlitBinaryFast(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
} else {
if (blendMode == BLEND_ADDITIVE) {
doBlitAdditiveBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
} else if (blendMode == BLEND_SUBTRACTIVE) {
doBlitSubtractiveBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
} else {
assert(blendMode == BLEND_NORMAL);
doBlitAlphaBlend(ino, outo, img->w, img->h, target.pitch, inStep, inoStep, color);
}
}
}
retSize.setWidth(img->w);
retSize.setHeight(img->h);
if (imgScaled) {
imgScaled->setPixels(savedPixels);
imgScaled->free();
delete imgScaled;
}
return retSize;
}
/**
* Writes a color key to the alpha channel of the surface
* @param rKey the red component of the color key
* @param gKey the green component of the color key
* @param bKey the blue component of the color key
* @param overwriteAlpha if true, all other alpha will be set fully opaque
*/
void TransparentSurface::applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha) {
assert(format.bytesPerPixel == 4);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
uint32 pix = ((uint32 *)pixels)[i * w + j];
uint8 r, g, b, a;
format.colorToARGB(pix, a, r, g, b);
if (r == rKey && g == gKey && b == bKey) {
a = 0;
((uint32 *)pixels)[i * w + j] = format.ARGBToColor(a, r, g, b);
} else if (overwriteAlpha) {
a = 255;
((uint32 *)pixels)[i * w + j] = format.ARGBToColor(a, r, g, b);
}
}
}
}
AlphaType TransparentSurface::getAlphaMode() const {
return _alphaMode;
}
void TransparentSurface::setAlphaMode(AlphaType mode) {
_alphaMode = mode;
}
/*
The below two functions are adapted from SDL_rotozoom.c,
taken from SDL_gfx-2.0.18.
Its copyright notice:
=============================================================================
SDL_rotozoom.c: rotozoomer, zoomer and shrinker for 32bit or 8bit surfaces
Copyright (C) 2001-2012 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
=============================================================================
The functions have been adapted for different structures and coordinate
systems.
*/
struct tColorRGBA { byte r; byte g; byte b; byte a; };
template <TFilteringMode filteringMode>
TransparentSurface *TransparentSurface::rotoscaleT(const TransformStruct &transform) const {
assert(transform._angle != 0); // This would not be ideal; rotoscale() should never be called in conditional branches where angle = 0 anyway.
Common::Point newHotspot;
Common::Rect srcRect(0, 0, (int16)w, (int16)h);
Common::Rect rect = TransformTools::newRect(Common::Rect(srcRect), transform, &newHotspot);
Common::Rect dstRect(0, 0, (int16)(rect.right - rect.left), (int16)(rect.bottom - rect.top));
TransparentSurface *target = new TransparentSurface();
assert(format.bytesPerPixel == 4);
int srcW = w;
int srcH = h;
int dstW = dstRect.width();
int dstH = dstRect.height();
target->create((uint16)dstW, (uint16)dstH, this->format);
if (transform._zoom.x == 0 || transform._zoom.y == 0) {
return target;
}
uint32 invAngle = 360 - (transform._angle % 360);
float invCos = cos(invAngle * M_PI / 180.0);
float invSin = sin(invAngle * M_PI / 180.0);
int icosx = (int)(invCos * (65536.0f * kDefaultZoomX / transform._zoom.x));
int isinx = (int)(invSin * (65536.0f * kDefaultZoomX / transform._zoom.x));
int icosy = (int)(invCos * (65536.0f * kDefaultZoomY / transform._zoom.y));
int isiny = (int)(invSin * (65536.0f * kDefaultZoomY / transform._zoom.y));
bool flipx = false, flipy = false; // TODO: See mirroring comment in RenderTicket ctor
int xd = (srcRect.left + transform._hotspot.x) << 16;
int yd = (srcRect.top + transform._hotspot.y) << 16;
int cx = newHotspot.x;
int cy = newHotspot.y;
int ax = -icosx * cx;
int ay = -isiny * cx;
int sw = srcW - 1;
int sh = srcH - 1;
tColorRGBA *pc = (tColorRGBA*)target->getBasePtr(0, 0);
for (int y = 0; y < dstH; y++) {
int t = cy - y;
int sdx = ax + (isinx * t) + xd;
int sdy = ay - (icosy * t) + yd;
for (int x = 0; x < dstW; x++) {
int dx = (sdx >> 16);
int dy = (sdy >> 16);
if (flipx) {
dx = sw - dx;
}
if (flipy) {
dy = sh - dy;
}
if (filteringMode == FILTER_BILINEAR) {
if ((dx > -1) && (dy > -1) && (dx < sw) && (dy < sh)) {
const tColorRGBA *sp = (const tColorRGBA *)getBasePtr(dx, dy);
tColorRGBA c00, c01, c10, c11, cswap;
c00 = *sp;
sp += 1;
c01 = *sp;
sp += (this->pitch / 4);
c11 = *sp;
sp -= 1;
c10 = *sp;
if (flipx) {
cswap = c00; c00=c01; c01=cswap;
cswap = c10; c10=c11; c11=cswap;
}
if (flipy) {
cswap = c00; c00=c10; c10=cswap;
cswap = c01; c01=c11; c11=cswap;
}
/*
* Interpolate colors
*/
int ex = (sdx & 0xffff);
int ey = (sdy & 0xffff);
int t1, t2;
t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
pc->r = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
pc->g = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
pc->b = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
pc->a = (((t2 - t1) * ey) >> 16) + t1;
}
} else {
if ((dx >= 0) && (dy >= 0) && (dx < srcW) && (dy < srcH)) {
const tColorRGBA *sp = (const tColorRGBA *)getBasePtr(dx, dy);
*pc = *sp;
}
}
sdx += icosx;
sdy += isiny;
pc++;
}
}
return target;
}
template <TFilteringMode filteringMode>
TransparentSurface *TransparentSurface::scaleT(uint16 newWidth, uint16 newHeight) const {
TransparentSurface *target = new TransparentSurface();
int srcW = w;
int srcH = h;
int dstW = newWidth;
int dstH = newHeight;
target->create((uint16)dstW, (uint16)dstH, format);
if (filteringMode == FILTER_BILINEAR) {
assert(format.bytesPerPixel == 4);
bool flipx = false, flipy = false; // TODO: See mirroring comment in RenderTicket ctor
int *sax = new int[dstW + 1];
int *say = new int[dstH + 1];
assert(sax && say);
/*
* Precalculate row increments
*/
int spixelw = (srcW - 1);
int spixelh = (srcH - 1);
int sx = (int) (65536.0f * (float) spixelw / (float) (dstW - 1));
int sy = (int) (65536.0f * (float) spixelh / (float) (dstH - 1));
/* Maximum scaled source size */
int ssx = (srcW << 16) - 1;
int ssy = (srcH << 16) - 1;
/* Precalculate horizontal row increments */
int csx = 0;
int *csax = sax;
for (int x = 0; x <= dstW; x++) {
*csax = csx;
csax++;
csx += sx;
/* Guard from overflows */
if (csx > ssx) {
csx = ssx;
}
}
/* Precalculate vertical row increments */
int csy = 0;
int *csay = say;
for (int y = 0; y <= dstH; y++) {
*csay = csy;
csay++;
csy += sy;
/* Guard from overflows */
if (csy > ssy) {
csy = ssy;
}
}
const tColorRGBA *sp = (const tColorRGBA *) getBasePtr(0, 0);
tColorRGBA *dp = (tColorRGBA *) target->getBasePtr(0, 0);
int spixelgap = srcW;
if (flipx) {
sp += spixelw;
}
if (flipy) {
sp += spixelgap * spixelh;
}
csay = say;
for (int y = 0; y < dstH; y++) {
const tColorRGBA *csp = sp;
csax = sax;
for (int x = 0; x < dstW; x++) {
/*
* Setup color source pointers
*/
int ex = (*csax & 0xffff);
int ey = (*csay & 0xffff);
int cx = (*csax >> 16);
int cy = (*csay >> 16);
const tColorRGBA *c00, *c01, *c10, *c11;
c00 = sp;
c01 = sp;
c10 = sp;
if (cy < spixelh) {
if (flipy) {
c10 -= spixelgap;
} else {
c10 += spixelgap;
}
}
c11 = c10;
if (cx < spixelw) {
if (flipx) {
c01--;
c11--;
} else {
c01++;
c11++;
}
}
/*
* Draw and interpolate colors
*/
int t1, t2;
t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
dp->r = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff;
t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff;
dp->g = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff;
t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff;
dp->b = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff;
t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff;
dp->a = (((t2 - t1) * ey) >> 16) + t1;
/*
* Advance source pointer x
*/
int *salastx = csax;
csax++;
int sstepx = (*csax >> 16) - (*salastx >> 16);
if (flipx) {
sp -= sstepx;
} else {
sp += sstepx;
}
/*
* Advance destination pointer x
*/
dp++;
}
/*
* Advance source pointer y
*/
int *salasty = csay;
csay++;
int sstepy = (*csay >> 16) - (*salasty >> 16);
sstepy *= spixelgap;
if (flipy) {
sp = csp - sstepy;
} else {
sp = csp + sstepy;
}
}
delete[] sax;
delete[] say;
} else {
int *scaleCacheX = new int[dstW];
for (int x = 0; x < dstW; x++) {
scaleCacheX[x] = (x * srcW) / dstW;
}
switch (format.bytesPerPixel) {
case 1:
scaleNN<uint8>(scaleCacheX, target);
break;
case 2:
scaleNN<uint16>(scaleCacheX, target);
break;
case 4:
scaleNN<uint32>(scaleCacheX, target);
break;
default:
error("Can only scale 8bpp, 16bpp, and 32bpp");
}
delete[] scaleCacheX;
}
return target;
}
TransparentSurface *TransparentSurface::convertTo(const PixelFormat &dstFormat, const byte *palette) const {
assert(pixels);
TransparentSurface *surface = new TransparentSurface();
// If the target format is the same, just copy
if (format == dstFormat) {
surface->copyFrom(*this);
return surface;
}
if (format.bytesPerPixel == 0 || format.bytesPerPixel > 4)
error("Surface::convertTo(): Can only convert from 1Bpp, 2Bpp, 3Bpp, and 4Bpp");
if (dstFormat.bytesPerPixel != 2 && dstFormat.bytesPerPixel != 4)
error("Surface::convertTo(): Can only convert to 2Bpp and 4Bpp");
surface->create(w, h, dstFormat);
if (format.bytesPerPixel == 1) {
// Converting from paletted to high color
assert(palette);
for (int y = 0; y < h; y++) {
const byte *srcRow = (const byte *)getBasePtr(0, y);
byte *dstRow = (byte *)surface->getBasePtr(0, y);
for (int x = 0; x < w; x++) {
byte index = *srcRow++;
byte r = palette[index * 3];
byte g = palette[index * 3 + 1];
byte b = palette[index * 3 + 2];
uint32 color = dstFormat.RGBToColor(r, g, b);
if (dstFormat.bytesPerPixel == 2)
*((uint16 *)dstRow) = color;
else
*((uint32 *)dstRow) = color;
dstRow += dstFormat.bytesPerPixel;
}
}
} else {
// Converting from high color to high color
for (int y = 0; y < h; y++) {
const byte *srcRow = (const byte *)getBasePtr(0, y);
byte *dstRow = (byte *)surface->getBasePtr(0, y);
for (int x = 0; x < w; x++) {
uint32 srcColor;
if (format.bytesPerPixel == 2)
srcColor = READ_UINT16(srcRow);
else if (format.bytesPerPixel == 3)
srcColor = READ_UINT24(srcRow);
else
srcColor = READ_UINT32(srcRow);
srcRow += format.bytesPerPixel;
// Convert that color to the new format
byte r, g, b, a;
format.colorToARGB(srcColor, a, r, g, b);
uint32 color = dstFormat.ARGBToColor(a, r, g, b);
if (dstFormat.bytesPerPixel == 2)
*((uint16 *)dstRow) = color;
else
*((uint32 *)dstRow) = color;
dstRow += dstFormat.bytesPerPixel;
}
}
}
return surface;
}
template <typename Size>
void TransparentSurface::scaleNN(int *scaleCacheX, TransparentSurface *target) const {
for (int y = 0; y < target->h; y++) {
Size *destP = (Size *)target->getBasePtr(0, y);
const Size *srcP = (const Size *)getBasePtr(0, (y * h) / target->h);
for (int x = 0; x < target->w; x++) {
*destP++ = srcP[scaleCacheX[x]];
}
}
}
template TransparentSurface *TransparentSurface::rotoscaleT<FILTER_NEAREST>(const TransformStruct &transform) const;
template TransparentSurface *TransparentSurface::rotoscaleT<FILTER_BILINEAR>(const TransformStruct &transform) const;
template TransparentSurface *TransparentSurface::scaleT<FILTER_NEAREST>(uint16 newWidth, uint16 newHeight) const;
template TransparentSurface *TransparentSurface::scaleT<FILTER_BILINEAR>(uint16 newWidth, uint16 newHeight) const;
template void TransparentSurface::scaleNN<uint8>(int *scaleCacheX, TransparentSurface *target) const;
template void TransparentSurface::scaleNN<uint16>(int *scaleCacheX, TransparentSurface *target) const;
template void TransparentSurface::scaleNN<uint32>(int *scaleCacheX, TransparentSurface *target) const;
TransparentSurface *TransparentSurface::rotoscale(const TransformStruct &transform) const {
return rotoscaleT<FILTER_BILINEAR>(transform);
}
TransparentSurface *TransparentSurface::scale(uint16 newWidth, uint16 newHeight) const {
return scaleT<FILTER_NEAREST>(newWidth, newHeight);
}
} // End of namespace Graphics
|