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
|
/* 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.
*/
#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 "engines/wintermute/graphics/transparent_surface.h"
#include "engines/wintermute/graphics/transform_tools.h"
namespace Wintermute {
#ifdef ENABLE_BILINEAR
void TransparentSurface::copyPixelBilinear(float projX, float projY, int dstX, int dstY, const Common::Rect &srcRect, const Common::Rect &dstRect, const TransparentSurface *src, TransparentSurface *dst) {
int srcW = srcRect.width();
int srcH = srcRect.height();
int dstW = dstRect.width();
int dstH = dstRect.height();
assert(dstX >= 0 && dstX < dstW);
assert(dstY >= 0 && dstY < dstH);
float x1 = floor(projX);
float x2 = ceil(projX);
float y1 = floor(projY);
float y2 = ceil(projY);
uint32 Q11, Q12, Q21, Q22;
if (x1 >= srcW || x1 < 0 || y1 >= srcH || y1 < 0) {
Q11 = 0;
} else {
Q11 = READ_UINT32((const byte *)src->getBasePtr((int)(x1 + srcRect.left),(int)(y1 + srcRect.top)));
}
if (x1 >= srcW || x1 < 0 || y2 >= srcH || y2 < 0) {
Q12 = 0;
} else {
Q12 = READ_UINT32((const byte *)src->getBasePtr((int)(x1 + srcRect.left), (int)(y2 + srcRect.top)));
}
if (x2 >= srcW || x2 < 0 || y1 >= srcH || y1 < 0) {
Q21 = 0;
} else {
Q21 = READ_UINT32((const byte *)src->getBasePtr((int)(x2 + srcRect.left), (int)(y1 + srcRect.top)));
}
if (x2 >= srcW || x2 < 0 || y2 >= srcH || y2 < 0) {
Q22 = 0;
} else {
Q22 = READ_UINT32((const byte *)src->getBasePtr((int)(x2 + srcRect.left), (int)(y2 + srcRect.top)));
}
byte *Q11s = (byte *)&Q11;
byte *Q12s = (byte *)&Q12;
byte *Q21s = (byte *)&Q21;
byte *Q22s = (byte *)&Q22;
uint32 color;
byte *dest = (byte *)&color;
float q11x = (x2 - projX);
float q11y = (y2 - projY);
float q21x = (projX - x1);
float q21y = (y2 - projY);
float q12x = (x2 - projX);
float q12y = (projY - y1);
if (x1 == x2 && y1 == y2) {
for (int c = 0; c < 4; c++) {
dest[c] = ((float)Q11s[c]);
}
} else {
if (x1 == x2) {
q11x = 0.5;
q12x = 0.5;
q21x = 0.5;
} else if (y1 == y2) {
q11y = 0.5;
q12y = 0.5;
q21y = 0.5;
}
for (int c = 0; c < 4; c++) {
dest[c] = (byte)(
((float)Q11s[c]) * q11x * q11y +
((float)Q21s[c]) * q21x * q21y +
((float)Q12s[c]) * q12x * q12y +
((float)Q22s[c]) * (1.0 -
q11x * q11y -
q21x * q21y -
q12x * q12y)
);
}
}
WRITE_UINT32((byte *)dst->getBasePtr(dstX + dstRect.left, dstY + dstRect.top), color);
}
#else
void TransparentSurface::copyPixelNearestNeighbor(float projX, float projY, int dstX, int dstY, const Common::Rect &srcRect, const Common::Rect &dstRect, const TransparentSurface *src, TransparentSurface *dst) {
int srcW = srcRect.width();
int srcH = srcRect.height();
int dstW = dstRect.width();
int dstH = dstRect.height();
assert(dstX >= 0 && dstX < dstW);
assert(dstY >= 0 && dstY < dstH);
uint32 color;
if (projX >= srcW || projX < 0 || projY >= srcH || projY < 0) {
color = 0;
} else {
color = READ_UINT32((const byte *)src->getBasePtr((int)projX, (int)projY));
}
WRITE_UINT32((byte *)dst->getBasePtr(dstX, dstY), color);
}
#endif
byte *TransparentSurface::_lookup = nullptr;
void TransparentSurface::destroyLookup() {
delete[] _lookup;
_lookup = nullptr;
}
TransparentSurface::TransparentSurface() : Surface(), _enableAlphaBlit(true) {}
TransparentSurface::TransparentSurface(const Surface &surf, bool copyData) : Surface(), _enableAlphaBlit(true) {
if (copyData) {
copyFrom(surf);
} else {
w = surf.w;
h = surf.h;
pitch = surf.pitch;
format = surf.format;
pixels = surf.pixels;
}
}
void doBlitOpaque(byte *ino, byte* outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep) {
byte *in, *out;
#ifdef SCUMM_LITTLE_ENDIAN
const int aIndex = 3;
#else
const int aIndex = 0;
#endif
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
memcpy(out, in, width * 4);
for (uint32 j = 0; j < width; j++) {
out[aIndex] = 0xFF;
out += 4;
}
outo += pitch;
ino += inoStep;
}
}
void TransparentSurface::generateLookup() {
_lookup = new byte[256 * 256];
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
_lookup[(i << 8) + j] = (i * j) >> 8;
}
}
}
void TransparentSurface::doBlitAlpha(byte *ino, byte* outo, uint32 width, uint32 height, uint32 pitch, int32 inStep, int32 inoStep) {
byte *in, *out;
if (!_lookup) {
generateLookup();
}
#ifdef SCUMM_LITTLE_ENDIAN
const int aIndex = 3;
const int bIndex = 0;
const int gIndex = 1;
const int rIndex = 2;
#else
const int aIndex = 0;
const int bIndex = 3;
const int gIndex = 2;
const int rIndex = 1;
#endif
const int bShift = 0;//img->format.bShift;
const int gShift = 8;//img->format.gShift;
const int rShift = 16;//img->format.rShift;
const int aShift = 24;//img->format.aShift;
const int bShiftTarget = 0;//target.format.bShift;
const int gShiftTarget = 8;//target.format.gShift;
const int rShiftTarget = 16;//target.format.rShift;
for (uint32 i = 0; i < height; i++) {
out = outo;
in = ino;
for (uint32 j = 0; j < width; j++) {
uint32 pix = *(uint32 *)in;
uint32 oPix = *(uint32 *) out;
int b = (pix >> bShift) & 0xff;
int g = (pix >> gShift) & 0xff;
int r = (pix >> rShift) & 0xff;
int a = (pix >> aShift) & 0xff;
int outb, outg, outr, outa;
in += inStep;
switch (a) {
case 0: // Full transparency
out += 4;
break;
case 255: // Full opacity
outb = b;
outg = g;
outr = r;
outa = a;
out[aIndex] = outa;
out[bIndex] = outb;
out[gIndex] = outg;
out[rIndex] = outr;
out += 4;
break;
default: // alpha blending
outa = 255;
outb = _lookup[(((oPix >> bShiftTarget) & 0xff)) + ((255 - a) << 8)];
outg = _lookup[(((oPix >> gShiftTarget) & 0xff)) + ((255 - a) << 8)];
outr = _lookup[(((oPix >> rShiftTarget) & 0xff)) + ((255 - a) << 8)];
outb += _lookup[b + (a << 8)];
outg += _lookup[g + (a << 8)];
outr += _lookup[r + (a << 8)];
out[aIndex] = outa;
out[bIndex] = outb;
out[gIndex] = outg;
out[rIndex] = outr;
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) {
int ca = (color >> 24) & 0xff;
Common::Rect retSize;
retSize.top = 0;
retSize.left = 0;
retSize.setWidth(0);
retSize.setHeight(0);
// Check if we need to draw anything at all
if (ca == 0)
return retSize;
int cr = (color >> 16) & 0xff;
int cg = (color >> 8) & 0xff;
int cb = (color >> 0) & 0xff;
// Compensate for transparency. Since we're coming
// down to 255 alpha, we just compensate for the colors here
if (ca != 255) {
cr = cr * ca >> 8;
cg = cg * ca >> 8;
cb = cb * ca >> 8;
}
// 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 32 bpp images");
return retSize;
}
if (pPartRect) {
srcImage.pixels = &((char *)pixels)[pPartRect->top * srcImage.pitch + pPartRect->left * 4];
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->pixels;
} else {
img = &srcImage;
}
// Handle off-screen clipping
if (posY < 0) {
img->h = MAX(0, (int)img->h - -posY);
img->pixels = (byte *)img->pixels + img->pitch * -posY;
posY = 0;
}
if (posX < 0) {
img->w = MAX(0, (int)img->w - -posX);
img->pixels = (byte *)img->pixels + (-posX * 4);
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 & TransparentSurface::FLIP_H) {
inStep = -inStep;
xp = img->w - 1;
}
if (flipping & TransparentSurface::FLIP_V) {
inoStep = -inoStep;
yp = img->h - 1;
}
byte *ino = (byte *)img->getBasePtr(xp, yp);
byte *outo = (byte *)target.getBasePtr(posX, posY);
byte *in, *out;
#ifdef SCUMM_LITTLE_ENDIAN
const int aIndex = 3;
const int bIndex = 0;
const int gIndex = 1;
const int rIndex = 2;
#else
const int aIndex = 0;
const int bIndex = 3;
const int gIndex = 2;
const int rIndex = 1;
#endif
const int bShift = 0;//img->format.bShift;
const int gShift = 8;//img->format.gShift;
const int rShift = 16;//img->format.rShift;
const int aShift = 24;//img->format.aShift;
const int bShiftTarget = 0;//target.format.bShift;
const int gShiftTarget = 8;//target.format.gShift;
const int rShiftTarget = 16;//target.format.rShift;
if (ca == 255 && cb == 255 && cg == 255 && cr == 255) {
if (_enableAlphaBlit) {
doBlitAlpha(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
} else {
doBlitOpaque(ino, outo, img->w, img->h, target.pitch, inStep, inoStep);
}
} else {
for (int i = 0; i < img->h; i++) {
out = outo;
in = ino;
for (int j = 0; j < img->w; j++) {
uint32 pix = *(uint32 *)in;
uint32 o_pix = *(uint32 *) out;
int b = (pix >> bShift) & 0xff;
int g = (pix >> gShift) & 0xff;
int r = (pix >> rShift) & 0xff;
int a = (pix >> aShift) & 0xff;
int outb, outg, outr, outa;
in += inStep;
if (ca != 255) {
a = a * ca >> 8;
}
switch (a) {
case 0: // Full transparency
out += 4;
break;
case 255: // Full opacity
if (cb != 255)
outb = (b * cb) >> 8;
else
outb = b;
if (cg != 255)
outg = (g * cg) >> 8;
else
outg = g;
if (cr != 255)
outr = (r * cr) >> 8;
else
outr = r;
outa = a;
out[aIndex] = outa;
out[bIndex] = outb;
out[gIndex] = outg;
out[rIndex] = outr;
out += 4;
break;
default: // alpha blending
outa = 255;
outb = (o_pix >> bShiftTarget) & 0xff;
outg = (o_pix >> gShiftTarget) & 0xff;
outr = (o_pix >> rShiftTarget) & 0xff;
if (cb == 0)
outb = 0;
else if (cb != 255)
outb += ((b - outb) * a * cb) >> 16;
else
outb += ((b - outb) * a) >> 8;
if (cg == 0)
outg = 0;
else if (cg != 255)
outg += ((g - outg) * a * cg) >> 16;
else
outg += ((g - outg) * a) >> 8;
if (cr == 0)
outr = 0;
else if (cr != 255)
outr += ((r - outr) * a * cr) >> 16;
else
outr += ((r - outr) * a) >> 8;
out[aIndex] = outa;
out[bIndex] = outb;
out[gIndex] = outg;
out[rIndex] = outr;
out += 4;
}
}
outo += target.pitch;
ino += inoStep;
}
}
}
retSize.setWidth(img->w);
retSize.setHeight(img->h);
if (imgScaled) {
imgScaled->pixels = savedPixels;
imgScaled->free();
delete imgScaled;
}
return retSize;
}
TransparentSurface *TransparentSurface::rotoscale(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.
Point32 newHotspot;
Common::Rect srcRect(0, 0, (int16)w, (int16)h);
Rect32 rect = TransformTools::newRect(Rect32 (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 dstW = dstRect.width();
int dstH = dstRect.height();
target->create((uint16)dstW, (uint16)dstH, this->format);
uint32 invAngle = 360 - (transform._angle % 360);
float invCos = cos(invAngle * M_PI / 180.0);
float invSin = sin(invAngle * M_PI / 180.0);
float targX;
float targY;
for (int y = 0; y < dstH; y++) {
for (int x = 0; x < dstW; x++) {
int x1 = x - newHotspot.x;
int y1 = y - newHotspot.y;
targX = ((x1 * invCos - y1 * invSin)) * kDefaultZoomX / transform._zoom.x + srcRect.left;
targY = ((x1 * invSin + y1 * invCos)) * kDefaultZoomY / transform._zoom.y + srcRect.top;
targX += transform._hotspot.x;
targY += transform._hotspot.y;
#ifdef ENABLE_BILINEAR
copyPixelBilinear(targX, targY, x, y, srcRect, dstRect, this, target);
#else
copyPixelNearestNeighbor(targX, targY, x, y, srcRect, dstRect, this, target);
#endif
}
}
return target;
}
TransparentSurface *TransparentSurface::scale(uint16 newWidth, uint16 newHeight) const {
Common::Rect srcRect(0, 0, (int16)w, (int16)h);
Common::Rect dstRect(0, 0, (int16)newWidth, (int16)newHeight);
TransparentSurface *target = new TransparentSurface();
assert(format.bytesPerPixel == 4);
int srcW = srcRect.width();
int srcH = srcRect.height();
int dstW = dstRect.width();
int dstH = dstRect.height();
target->create((uint16)dstW, (uint16)dstH, this->format);
float projX;
float projY;
for (int y = 0; y < dstH; y++) {
for (int x = 0; x < dstW; x++) {
projX = x / (float)dstW * srcW;
projY = y / (float)dstH * srcH;
#ifdef ENABLE_BILINEAR
copyPixelBilinear(projX, projY, x, y, srcRect, dstRect, this, target);
#else
copyPixelNearestNeighbor(projX, projY, x, y, srcRect, dstRect, this, target);
#endif
}
}
return target;
}
/**
* 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);
}
}
}
}
} // End of namespace Graphics
|