swf2js 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/package.json +2 -1
- package/src/cli.js +15 -6
- package/src/emitter/bundle-emitter.js +56 -16
- package/src/runtime/avm1-runtime.js +329 -84
- package/src/runtime/avm2-runtime.js +56 -6
- package/src/runtime/embedded-movie-runtime.js +6 -1
- package/src/runtime/index.js +2 -6
- package/src/runtime/movie-runtime.js +2 -1
- package/src/runtime/swflib/assemble.js +34 -0
- package/src/runtime/swflib/bitmap-image.js +4 -0
- package/src/runtime/swflib/bitmap.js +1004 -0
- package/src/runtime/swflib/builtins.js +156 -0
- package/src/runtime/swflib/bytearray.js +949 -0
- package/src/runtime/swflib/compiled-swf.js +3 -0
- package/src/runtime/swflib/context-menu.js +245 -0
- package/src/runtime/swflib/cursor-flag.js +12 -0
- package/src/runtime/swflib/dictionary.js +121 -0
- package/src/runtime/swflib/display.js +823 -0
- package/src/runtime/swflib/events.js +754 -0
- package/src/runtime/swflib/filters.js +298 -0
- package/src/runtime/swflib/flash.js +130 -0
- package/src/runtime/swflib/geom.js +759 -0
- package/src/runtime/swflib/glyph-metrics.js +14 -0
- package/src/runtime/swflib/graphics.js +735 -0
- package/src/runtime/swflib/host.js +2 -0
- package/src/runtime/swflib/inherit.js +22 -0
- package/src/runtime/swflib/input.js +1085 -0
- package/src/runtime/swflib/loader.js +687 -0
- package/src/runtime/swflib/marks.js +16 -0
- package/src/runtime/swflib/matrix3d.js +144 -0
- package/src/runtime/swflib/pointer-bridge.js +8 -0
- package/src/runtime/swflib/projection.js +158 -0
- package/src/runtime/swflib/render.js +1240 -0
- package/src/runtime/swflib/sound.js +1166 -0
- package/src/runtime/swflib/sprite-drag.js +116 -0
- package/src/runtime/swflib/stage-ref.js +9 -0
- package/src/runtime/swflib/stage.js +484 -0
- package/src/runtime/swflib/system.js +987 -0
- package/src/runtime/swflib/text-field.js +519 -0
- package/src/runtime/swflib/text-paint.js +797 -0
- package/src/runtime/swflib/text.js +1387 -0
- package/src/runtime/swflib/transform.js +133 -0
- package/src/runtime/swflib/xml.js +268 -0
- package/src/transpiler/avm1/avm1-ir.js +2 -1
- package/src/transpiler/avm1/avm1-rewrite.js +23 -2
- package/src/transpiler/avm1/avm1-transpiler.js +4 -1
- package/src/transpiler/avm2/avm2-transpiler.js +38 -7
- package/src/transpiler/timeline/timeline-codegen.js +366 -123
- package/src/runtime/swflib.js +0 -14050
|
@@ -0,0 +1,1004 @@
|
|
|
1
|
+
import { bitmapDisplayImage } from './bitmap-image.js';
|
|
2
|
+
import { ByteArray, Endian } from './bytearray.js';
|
|
3
|
+
export { bitmapDisplayImage };
|
|
4
|
+
export function registerAvm1BitmapSource(clip, draw) { avm1BitmapSources.set(clip, draw); }
|
|
5
|
+
import { DisplayObject } from './display.js';
|
|
6
|
+
import { StageQuality } from './events.js';
|
|
7
|
+
import { BevelFilter, BlurFilter, ColorMatrixFilter, ConvolutionFilter, DisplacementMapFilter, DropShadowFilter, GlowFilter, GradientBevelFilter, GradientGlowFilter } from './filters.js';
|
|
8
|
+
import { Matrix, Point, Rectangle, Vector } from './geom.js';
|
|
9
|
+
import { applyDisplayEffects, blendModeToCanvas, blurPixels, colorMatrixPixels, convolvePixels, displaceBitmapPixels, effectBlurPlan, effectContourPlan, effectShadowPlan, filterBitmapContour, filterBitmapShadow, makeEffectCanvas, renderDisplayTree, setCanvasMatrix } from './render.js';
|
|
10
|
+
|
|
11
|
+
export class BitmapData {
|
|
12
|
+
constructor(w, h, transparent, fillColor) {
|
|
13
|
+
this.width = Math.max(0, w | 0);
|
|
14
|
+
this.height = Math.max(0, h | 0);
|
|
15
|
+
this.transparent = transparent !== false;
|
|
16
|
+
this.rect = new Rectangle(0, 0, this.width, this.height);
|
|
17
|
+
this._canvas = null;
|
|
18
|
+
this._ctx = null;
|
|
19
|
+
this._img = null;
|
|
20
|
+
if (this.width && this.height && typeof document !== 'undefined') {
|
|
21
|
+
this._canvas = document.createElement('canvas');
|
|
22
|
+
this._canvas.width = this.width;
|
|
23
|
+
this._canvas.height = this.height;
|
|
24
|
+
this._ctx = this._canvas.getContext('2d', { willReadFrequently: true });
|
|
25
|
+
this._img = this._canvas;
|
|
26
|
+
if (fillColor != null) this.fillRect(this.rect, fillColor >>> 0);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
_ensure() {
|
|
30
|
+
if (this._ctx) return true;
|
|
31
|
+
if (!this.width || !this.height || typeof document === 'undefined') return false;
|
|
32
|
+
var backing = this._img;
|
|
33
|
+
this._canvas = document.createElement('canvas');
|
|
34
|
+
this._canvas.width = this.width;
|
|
35
|
+
this._canvas.height = this.height;
|
|
36
|
+
this._ctx = this._canvas.getContext('2d', { willReadFrequently: true });
|
|
37
|
+
if (!this._ctx) return false;
|
|
38
|
+
if (backing) this._ctx.drawImage(backing, 0, 0);
|
|
39
|
+
this._img = this._canvas;
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
clone() {
|
|
43
|
+
var b = new BitmapData(this.width, this.height, this.transparent);
|
|
44
|
+
if (this._canvas && b._ensure()) b._ctx.drawImage(this._canvas, 0, 0);
|
|
45
|
+
else if (this._img && b._ensure()) {
|
|
46
|
+
try { b._ctx.drawImage(this._img, 0, 0); } catch (_) {}
|
|
47
|
+
}
|
|
48
|
+
return b;
|
|
49
|
+
}
|
|
50
|
+
dispose() {
|
|
51
|
+
this._disposed = true;
|
|
52
|
+
this._lockedDisplayImage = null;
|
|
53
|
+
this._ctx = null;
|
|
54
|
+
this._canvas = null;
|
|
55
|
+
this._img = null;
|
|
56
|
+
this.width = 0;
|
|
57
|
+
this.height = 0;
|
|
58
|
+
}
|
|
59
|
+
_getDisplayImage() {
|
|
60
|
+
return bitmapDisplayImage(this);
|
|
61
|
+
}
|
|
62
|
+
lock() {
|
|
63
|
+
if (this._lockedDisplayImage || this._disposed || !this.width || !this.height) return;
|
|
64
|
+
var snapshot = makeEffectCanvas(this);
|
|
65
|
+
var context = snapshot?.getContext('2d');
|
|
66
|
+
if (!context) return;
|
|
67
|
+
var source = this._canvas || this._img;
|
|
68
|
+
try {
|
|
69
|
+
if (source) context.drawImage(source, 0, 0);
|
|
70
|
+
this._lockedDisplayImage = snapshot;
|
|
71
|
+
} catch (_) {}
|
|
72
|
+
}
|
|
73
|
+
unlock(changeRect) {
|
|
74
|
+
// changeRect is a dirty repaint hint. The Canvas renderer repaints the
|
|
75
|
+
// whole display list, so release the shared snapshot on every unlock.
|
|
76
|
+
this._lockedDisplayImage = null;
|
|
77
|
+
}
|
|
78
|
+
fillRect(rect, color) {
|
|
79
|
+
if (!this._ensure() || !rect) return;
|
|
80
|
+
var a = this.transparent ? ((color >>> 24) & 255) / 255 : 1;
|
|
81
|
+
var r = (color >>> 16) & 255;
|
|
82
|
+
var g = (color >>> 8) & 255;
|
|
83
|
+
var b = color & 255;
|
|
84
|
+
this._ctx.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
|
|
85
|
+
this._ctx.fillRect(rect.x || 0, rect.y || 0, rect.width || 0, rect.height || 0);
|
|
86
|
+
}
|
|
87
|
+
floodFill(x, y, color) {
|
|
88
|
+
x |= 0; y |= 0;
|
|
89
|
+
var width = this.width, height = this.height, transparent = this.transparent;
|
|
90
|
+
if (x < 0 || y < 0 || x >= width || y >= height || !this._ensure()) return;
|
|
91
|
+
var alpha = transparent ? color >>> 24 : 255;
|
|
92
|
+
var red = alpha ? (color >>> 16) & 255 : 0;
|
|
93
|
+
var green = alpha ? (color >>> 8) & 255 : 0;
|
|
94
|
+
var blue = alpha ? color & 255 : 0;
|
|
95
|
+
try {
|
|
96
|
+
var image = this._ctx.getImageData(0, 0, width, height);
|
|
97
|
+
var pixels = image.data, seed = (y * width + x) * 4;
|
|
98
|
+
var oldRed = pixels[seed], oldGreen = pixels[seed + 1], oldBlue = pixels[seed + 2];
|
|
99
|
+
var oldAlpha = transparent ? pixels[seed + 3] : 255;
|
|
100
|
+
if (red === oldRed && green === oldGreen && blue === oldBlue && alpha === oldAlpha) return;
|
|
101
|
+
var pending = [];
|
|
102
|
+
var minX = x, maxX = x, minY = y, maxY = y;
|
|
103
|
+
function matches(index) {
|
|
104
|
+
return pixels[index] === oldRed && pixels[index + 1] === oldGreen && pixels[index + 2] === oldBlue &&
|
|
105
|
+
(!transparent || pixels[index + 3] === oldAlpha);
|
|
106
|
+
}
|
|
107
|
+
function fillSpan(col, row) {
|
|
108
|
+
var start = col, end = col, offset = row * width * 4;
|
|
109
|
+
while (start > 0 && matches(offset + (start - 1) * 4)) start--;
|
|
110
|
+
while (end + 1 < width && matches(offset + (end + 1) * 4)) end++;
|
|
111
|
+
for (var p = offset + start * 4; p <= offset + end * 4; p += 4) {
|
|
112
|
+
pixels[p] = red; pixels[p + 1] = green; pixels[p + 2] = blue; pixels[p + 3] = alpha;
|
|
113
|
+
}
|
|
114
|
+
minX = Math.min(minX, start); maxX = Math.max(maxX, end);
|
|
115
|
+
minY = Math.min(minY, row); maxY = Math.max(maxY, row);
|
|
116
|
+
pending.push(start, end, row);
|
|
117
|
+
return end + 1;
|
|
118
|
+
}
|
|
119
|
+
fillSpan(x, y);
|
|
120
|
+
while (pending.length) {
|
|
121
|
+
var row = pending.pop(), end = pending.pop(), start = pending.pop();
|
|
122
|
+
for (var dy = -1; dy <= 1; dy += 2) {
|
|
123
|
+
var neighbor = row + dy;
|
|
124
|
+
if (neighbor < 0 || neighbor >= height) continue;
|
|
125
|
+
var offset = neighbor * width * 4;
|
|
126
|
+
for (var col = start; col <= end;) {
|
|
127
|
+
col = matches(offset + col * 4) ? fillSpan(col, neighbor) : col + 1;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this._ctx.putImageData(image, 0, 0, minX, minY, maxX - minX + 1, maxY - minY + 1);
|
|
132
|
+
} catch (_) {}
|
|
133
|
+
}
|
|
134
|
+
compare(other) {
|
|
135
|
+
if (!(other instanceof BitmapData)) throw new TypeError('Expected a BitmapData to compare');
|
|
136
|
+
if (this._disposed || other._disposed) {
|
|
137
|
+
var error = new Error('Invalid BitmapData');
|
|
138
|
+
error.name = 'ArgumentError';
|
|
139
|
+
error.errorID = 2015;
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
var width = this.width, height = this.height;
|
|
143
|
+
if (width !== other.width) return -3;
|
|
144
|
+
if (height !== other.height) return -4;
|
|
145
|
+
if (this === other || !width || !height) return 0;
|
|
146
|
+
if (!this._ensure() || !other._ensure()) throw new Error('BitmapData pixels are unavailable');
|
|
147
|
+
|
|
148
|
+
// Bound each readback to about one MiB when the stride permits it.
|
|
149
|
+
// Equal images need no result canvas, and untouched stripes start clear.
|
|
150
|
+
var rows = Math.max(1, Math.floor(262144 / width));
|
|
151
|
+
var result = null;
|
|
152
|
+
for (var y = 0; y < height; y += rows) {
|
|
153
|
+
var count = Math.min(rows, height - y);
|
|
154
|
+
var image = this._ctx.getImageData(0, y, width, count);
|
|
155
|
+
var pixels = image.data, right = other._ctx.getImageData(0, y, width, count).data;
|
|
156
|
+
var changed = false;
|
|
157
|
+
for (var i = 0; i < pixels.length; i += 4) {
|
|
158
|
+
var r = (pixels[i] - right[i]) & 255;
|
|
159
|
+
var g = (pixels[i + 1] - right[i + 1]) & 255;
|
|
160
|
+
var b = (pixels[i + 2] - right[i + 2]) & 255;
|
|
161
|
+
var a = ((this.transparent ? pixels[i + 3] : 255) - (other.transparent ? right[i + 3] : 255)) & 255;
|
|
162
|
+
var rgbChanged = !!(r || g || b);
|
|
163
|
+
if (rgbChanged || a) changed = true;
|
|
164
|
+
pixels[i] = rgbChanged ? r : a ? 255 : 0;
|
|
165
|
+
pixels[i + 1] = rgbChanged ? g : pixels[i];
|
|
166
|
+
pixels[i + 2] = rgbChanged ? b : pixels[i];
|
|
167
|
+
pixels[i + 3] = rgbChanged ? 255 : a;
|
|
168
|
+
}
|
|
169
|
+
if (!changed) continue;
|
|
170
|
+
if (!result) {
|
|
171
|
+
result = new BitmapData(width, height, true);
|
|
172
|
+
if (!result._ensure()) throw new Error('BitmapData result pixels are unavailable');
|
|
173
|
+
}
|
|
174
|
+
result._ctx.putImageData(image, 0, y);
|
|
175
|
+
}
|
|
176
|
+
return result || 0;
|
|
177
|
+
}
|
|
178
|
+
pixelDissolve(source, sourceRect, destPoint, randomSeed, numPixels, fillColor) {
|
|
179
|
+
if (!(source instanceof BitmapData) || !sourceRect || !destPoint) {
|
|
180
|
+
throw new TypeError('Expected a source bitmap, source rectangle and destination point');
|
|
181
|
+
}
|
|
182
|
+
numPixels |= 0;
|
|
183
|
+
if (numPixels < 0) throw new TypeError('numPixels must be nonnegative');
|
|
184
|
+
var fill = source === this;
|
|
185
|
+
var sx = sourceRect.x | 0, sy = sourceRect.y | 0;
|
|
186
|
+
var dx = fill ? sx : destPoint.x | 0, dy = fill ? sy : destPoint.y | 0;
|
|
187
|
+
var left = Math.max(0, -sx, -dx), top = Math.max(0, -sy, -dy);
|
|
188
|
+
var width = Math.min(sourceRect.width | 0, source.width - sx, this.width - dx) - left;
|
|
189
|
+
var height = Math.min(sourceRect.height | 0, source.height - sy, this.height - dy) - top;
|
|
190
|
+
if (width <= 0 || height <= 0 || !this._ensure() || !source._ensure()) return 0;
|
|
191
|
+
|
|
192
|
+
var length = width * height;
|
|
193
|
+
// The reference uses an even-bit power-of-two permutation. Advance the
|
|
194
|
+
// raw cursor until it hits the rectangle, leaving zero for the origin.
|
|
195
|
+
var half = 2 ** Math.max(1, Math.ceil(Math.log2(length) / 2));
|
|
196
|
+
var period = half * half;
|
|
197
|
+
var cursor = (randomSeed | 0) % period;
|
|
198
|
+
var steps = Math.min(numPixels, length);
|
|
199
|
+
var transparent = this.transparent, sourceAlpha = source.transparent;
|
|
200
|
+
var a = transparent ? fillColor >>> 24 : 255;
|
|
201
|
+
var r = a ? (fillColor >>> 16) & 255 : 0;
|
|
202
|
+
var g = a ? (fillColor >>> 8) & 255 : 0;
|
|
203
|
+
var b = a ? fillColor & 255 : 0;
|
|
204
|
+
try {
|
|
205
|
+
var readWidth = steps ? width : 1, readHeight = steps ? height : 1;
|
|
206
|
+
var destination = this._ctx.getImageData(dx + left, dy + top, readWidth, readHeight);
|
|
207
|
+
var pixels = destination.data;
|
|
208
|
+
var original = fill ? null : source._ctx.getImageData(sx + left, sy + top, readWidth, readHeight).data;
|
|
209
|
+
function replace(index) {
|
|
210
|
+
var p = index * 4;
|
|
211
|
+
pixels[p] = fill ? r : original[p];
|
|
212
|
+
pixels[p + 1] = fill ? g : original[p + 1];
|
|
213
|
+
pixels[p + 2] = fill ? b : original[p + 2];
|
|
214
|
+
pixels[p + 3] = fill ? a : transparent && sourceAlpha ? original[p + 3] : 255;
|
|
215
|
+
}
|
|
216
|
+
replace(0);
|
|
217
|
+
if (length > 1) {
|
|
218
|
+
for (var step = 0; step < steps; step++) {
|
|
219
|
+
var index;
|
|
220
|
+
do {
|
|
221
|
+
cursor = (cursor + 1 + period) % period;
|
|
222
|
+
var low = cursor % half, high = Math.floor(cursor / half);
|
|
223
|
+
index = (high ^ ((low * low + 1) % half)) * half + low;
|
|
224
|
+
} while (!index || index >= length);
|
|
225
|
+
replace(index);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
this._ctx.putImageData(destination, dx + left, dy + top);
|
|
229
|
+
} catch (_) {}
|
|
230
|
+
return cursor | 0;
|
|
231
|
+
}
|
|
232
|
+
generateFilterRect(sourceRect, filter) {
|
|
233
|
+
if (sourceRect == null || filter == null) throw new TypeError('Expected a source rectangle and filter');
|
|
234
|
+
var rect = new Rectangle(sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height);
|
|
235
|
+
if (rect.width > 0 && rect.height > 0) {
|
|
236
|
+
var effect = bitmapShadowPlan(filter) || bitmapContourPlan(filter);
|
|
237
|
+
if (effect?.inner) return rect.intersection(new Rectangle(0, 0, this.width, this.height));
|
|
238
|
+
var blur = effect?.blur || (filter instanceof BlurFilter ? effectBlurPlan(filter, false) : null);
|
|
239
|
+
var margin = bitmapFilterMargins(blur, effect);
|
|
240
|
+
rect.x -= margin.left; rect.y -= margin.top;
|
|
241
|
+
rect.width += margin.left + margin.right;
|
|
242
|
+
rect.height += margin.top + margin.bottom;
|
|
243
|
+
}
|
|
244
|
+
return rect;
|
|
245
|
+
}
|
|
246
|
+
applyFilter(source, sourceRect, destPoint, filter) {
|
|
247
|
+
if (source == null || sourceRect == null || destPoint == null || filter == null) {
|
|
248
|
+
throw new TypeError('Expected a source bitmap, source rectangle, destination point and filter');
|
|
249
|
+
}
|
|
250
|
+
var blur = filter instanceof BlurFilter ? effectBlurPlan(filter, false) : null;
|
|
251
|
+
var shadow = bitmapShadowPlan(filter);
|
|
252
|
+
var contour = bitmapContourPlan(filter);
|
|
253
|
+
var colorMatrix = filter instanceof ColorMatrixFilter;
|
|
254
|
+
var convolution = filter instanceof ConvolutionFilter;
|
|
255
|
+
var displacement = filter instanceof DisplacementMapFilter;
|
|
256
|
+
if (!blur && !shadow && !contour && !colorMatrix && !convolution && !displacement) return;
|
|
257
|
+
if (displacement && source.transparent !== this.transparent) {
|
|
258
|
+
throw new Error('DisplacementMapFilter requires matching bitmap transparency');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
var sx = Math.floor(+sourceRect.x), sy = Math.floor(+sourceRect.y);
|
|
262
|
+
var width = Math.floor(+sourceRect.width), height = Math.floor(+sourceRect.height);
|
|
263
|
+
var dx = Math.floor(+destPoint.x) - sx, dy = Math.floor(+destPoint.y) - sy;
|
|
264
|
+
if (![sx, sy, width, height, dx, dy].every(Number.isFinite) || width <= 0 || height <= 0) return;
|
|
265
|
+
var left = Math.max(0, sx), top = Math.max(0, sy);
|
|
266
|
+
var right = Math.min(source.width, sx + width), bottom = Math.min(source.height, sy + height);
|
|
267
|
+
if (!(left < right && top < bottom)) return;
|
|
268
|
+
|
|
269
|
+
var padX = blur ? Math.ceil(blur.radiusX) * blur.passes : 0;
|
|
270
|
+
var padY = blur ? Math.ceil(blur.radiusY) * blur.passes : 0;
|
|
271
|
+
var effect = shadow || contour;
|
|
272
|
+
var margin = bitmapFilterMargins(blur || effect?.blur, effect);
|
|
273
|
+
// Map the original source rectangle's origin to destPoint, intersecting
|
|
274
|
+
// only the affected output with the destination bitmap.
|
|
275
|
+
left = Math.max(left - margin.left, -dx);
|
|
276
|
+
right = Math.min(right + margin.right, this.width - dx);
|
|
277
|
+
top = Math.max(top - margin.top, -dy);
|
|
278
|
+
bottom = Math.min(bottom + margin.bottom, this.height - dy);
|
|
279
|
+
if (!(left < right && top < bottom) || !source._ensure?.() || !this._ensure()) return;
|
|
280
|
+
|
|
281
|
+
var haloX = convolution ? Math.max(0, (filter.matrixX | 0) >> 1) : padX;
|
|
282
|
+
var haloY = convolution ? Math.max(0, (filter.matrixY | 0) >> 1) : padY;
|
|
283
|
+
var readLeft = left - haloX, readTop = top - haloY;
|
|
284
|
+
var readRight = right + haloX, readBottom = bottom + haloY;
|
|
285
|
+
if (convolution) {
|
|
286
|
+
// Clamping refers to the source bitmap edges, even if some pixels are clear.
|
|
287
|
+
readLeft = Math.max(0, readLeft); readTop = Math.max(0, readTop);
|
|
288
|
+
readRight = Math.min(source.width, readRight); readBottom = Math.min(source.height, readBottom);
|
|
289
|
+
}
|
|
290
|
+
var readWidth = readRight - readLeft, readHeight = readBottom - readTop;
|
|
291
|
+
try {
|
|
292
|
+
// A snapshot lets overlapping self-filters read original pixels.
|
|
293
|
+
var image = source._ctx.getImageData(readLeft, readTop, readWidth, readHeight);
|
|
294
|
+
var data = image.data;
|
|
295
|
+
if (blur && (padX || padY)) blurPixels(data, readWidth, readHeight, blur);
|
|
296
|
+
else if (shadow) filterBitmapShadow(source._ctx, data, readLeft, readTop, readWidth, readHeight, shadow);
|
|
297
|
+
else if (contour) filterBitmapContour(source._ctx, data, readLeft, readTop, readWidth, readHeight, contour);
|
|
298
|
+
else if (colorMatrix) colorMatrixPixels(data, filter.matrix, true);
|
|
299
|
+
else if (convolution) convolvePixels(data, readWidth, filter, { x: 0, y: 0, width: readWidth, height: readHeight });
|
|
300
|
+
else if (displacement) displaceBitmapPixels(source, data, readLeft, readTop, readWidth, readHeight, filter);
|
|
301
|
+
if (!this.transparent) {
|
|
302
|
+
for (var row = top - readTop; row < bottom - readTop; row++) {
|
|
303
|
+
for (var col = left - readLeft; col < right - readLeft; col++) {
|
|
304
|
+
data[(row * readWidth + col) * 4 + 3] = 255;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
this._ctx.putImageData(image, readLeft + dx, readTop + dy,
|
|
309
|
+
left - readLeft, top - readTop, right - left, bottom - top);
|
|
310
|
+
} catch (_) {}
|
|
311
|
+
}
|
|
312
|
+
copyPixels(source, sourceRect, destPoint, alphaBitmapData, alphaPoint, mergeAlpha) {
|
|
313
|
+
if (!this._ensure() || !source || !sourceRect || !destPoint) return;
|
|
314
|
+
if (!source._ensure || !source._ensure() || !source._ctx) return;
|
|
315
|
+
var sourceRectX = Math.floor(+sourceRect.x || 0), sourceRectY = Math.floor(+sourceRect.y || 0);
|
|
316
|
+
var sx = sourceRectX, sy = sourceRectY;
|
|
317
|
+
var dx = Math.floor(+destPoint.x || 0), dy = Math.floor(+destPoint.y || 0);
|
|
318
|
+
var width = Math.floor(+sourceRect.width || 0), height = Math.floor(+sourceRect.height || 0);
|
|
319
|
+
if (width <= 0 || height <= 0) return;
|
|
320
|
+
// Clip source first while carrying the matching destination offset.
|
|
321
|
+
if (sx < 0) { var clipLeft = -sx; sx = 0; dx += clipLeft; width -= clipLeft; }
|
|
322
|
+
if (sy < 0) { var clipTop = -sy; sy = 0; dy += clipTop; height -= clipTop; }
|
|
323
|
+
width = Math.min(width, source.width - sx);
|
|
324
|
+
height = Math.min(height, source.height - sy);
|
|
325
|
+
// Then clip the destination and move the source origin by the same amount.
|
|
326
|
+
if (dx < 0) { var dstLeft = -dx; dx = 0; sx += dstLeft; width -= dstLeft; }
|
|
327
|
+
if (dy < 0) { var dstTop = -dy; dy = 0; sy += dstTop; height -= dstTop; }
|
|
328
|
+
width = Math.min(width, this.width - dx);
|
|
329
|
+
height = Math.min(height, this.height - dy);
|
|
330
|
+
if (width <= 0 || height <= 0) return;
|
|
331
|
+
var alphaOriginX = 0, alphaOriginY = 0;
|
|
332
|
+
if (alphaBitmapData) {
|
|
333
|
+
if (!alphaBitmapData._ensure || !alphaBitmapData._ensure() || !alphaBitmapData._ctx) return;
|
|
334
|
+
alphaOriginX = (alphaPoint ? Math.floor(+alphaPoint.x || 0) : 0) + (sx - sourceRectX);
|
|
335
|
+
alphaOriginY = (alphaPoint ? Math.floor(+alphaPoint.y || 0) : 0) + (sy - sourceRectY);
|
|
336
|
+
if (alphaOriginX < 0) { var maskLeft = -alphaOriginX; alphaOriginX = 0; sx += maskLeft; dx += maskLeft; width -= maskLeft; }
|
|
337
|
+
if (alphaOriginY < 0) { var maskTop = -alphaOriginY; alphaOriginY = 0; sy += maskTop; dy += maskTop; height -= maskTop; }
|
|
338
|
+
width = Math.min(width, alphaBitmapData.width - alphaOriginX);
|
|
339
|
+
height = Math.min(height, alphaBitmapData.height - alphaOriginY);
|
|
340
|
+
if (width <= 0 || height <= 0) return;
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
var sourceImage = source._ctx.getImageData(sx, sy, width, height);
|
|
344
|
+
var destImage = this._ctx.getImageData(dx, dy, width, height);
|
|
345
|
+
var sourcePixels = sourceImage.data, destPixels = destImage.data;
|
|
346
|
+
var alphaPixels = alphaBitmapData ? alphaBitmapData._ctx.getImageData(alphaOriginX, alphaOriginY, width, height).data : null;
|
|
347
|
+
for (var row = 0; row < height; row++) {
|
|
348
|
+
for (var col = 0; col < width; col++) {
|
|
349
|
+
var index = (row * width + col) * 4;
|
|
350
|
+
var sr = sourcePixels[index], sg = sourcePixels[index + 1], sb = sourcePixels[index + 2];
|
|
351
|
+
var sa = sourcePixels[index + 3] / 255;
|
|
352
|
+
if (alphaBitmapData) {
|
|
353
|
+
sa *= alphaPixels[index + 3] / 255;
|
|
354
|
+
}
|
|
355
|
+
if (mergeAlpha) {
|
|
356
|
+
var da = this.transparent ? destPixels[index + 3] / 255 : 1;
|
|
357
|
+
var outA = sa + da * (1 - sa);
|
|
358
|
+
if (outA > 0) {
|
|
359
|
+
destPixels[index] = (sr * sa + destPixels[index] * da * (1 - sa)) / outA;
|
|
360
|
+
destPixels[index + 1] = (sg * sa + destPixels[index + 1] * da * (1 - sa)) / outA;
|
|
361
|
+
destPixels[index + 2] = (sb * sa + destPixels[index + 2] * da * (1 - sa)) / outA;
|
|
362
|
+
} else destPixels[index] = destPixels[index + 1] = destPixels[index + 2] = 0;
|
|
363
|
+
destPixels[index + 3] = this.transparent ? Math.round(outA * 255) : 255;
|
|
364
|
+
} else {
|
|
365
|
+
destPixels[index] = sr; destPixels[index + 1] = sg; destPixels[index + 2] = sb;
|
|
366
|
+
// A direct copy replaces the destination pixel, including alpha.
|
|
367
|
+
// Opaque BitmapData still stores every resulting pixel as opaque.
|
|
368
|
+
destPixels[index + 3] = this.transparent ? Math.round(sa * 255) : 255;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
this._ctx.putImageData(destImage, dx, dy);
|
|
373
|
+
} catch (_) {}
|
|
374
|
+
}
|
|
375
|
+
copyChannel(source, sourceRect, destPoint, sourceChannel, destChannel) {
|
|
376
|
+
if (!source || !sourceRect || !destPoint) throw new TypeError('source, sourceRect, and destPoint are required');
|
|
377
|
+
var sourceIndex = bitmapChannelIndex(sourceChannel), destIndex = bitmapChannelIndex(destChannel);
|
|
378
|
+
if (sourceIndex < 0 || destIndex < 0) throw new RangeError('Invalid BitmapData channel');
|
|
379
|
+
if (!this._ensure() || !source._ensure || !source._ensure() || !source._ctx) return;
|
|
380
|
+
var sourceRectX = Math.floor(+sourceRect.x || 0), sourceRectY = Math.floor(+sourceRect.y || 0);
|
|
381
|
+
var sx = sourceRectX, sy = sourceRectY;
|
|
382
|
+
var dx = Math.floor(+destPoint.x || 0), dy = Math.floor(+destPoint.y || 0);
|
|
383
|
+
var width = Math.floor(+sourceRect.width || 0), height = Math.floor(+sourceRect.height || 0);
|
|
384
|
+
if (width <= 0 || height <= 0) return;
|
|
385
|
+
if (sx < 0) { var clipLeft = -sx; sx = 0; dx += clipLeft; width -= clipLeft; }
|
|
386
|
+
if (sy < 0) { var clipTop = -sy; sy = 0; dy += clipTop; height -= clipTop; }
|
|
387
|
+
width = Math.min(width, source.width - sx);
|
|
388
|
+
height = Math.min(height, source.height - sy);
|
|
389
|
+
if (dx < 0) { var dstLeft = -dx; dx = 0; sx += dstLeft; width -= dstLeft; }
|
|
390
|
+
if (dy < 0) { var dstTop = -dy; dy = 0; sy += dstTop; height -= dstTop; }
|
|
391
|
+
width = Math.min(width, this.width - dx);
|
|
392
|
+
height = Math.min(height, this.height - dy);
|
|
393
|
+
if (width <= 0 || height <= 0) return;
|
|
394
|
+
try {
|
|
395
|
+
var src = source._ctx.getImageData(sx, sy, width, height).data;
|
|
396
|
+
var dest = this._ctx.getImageData(dx, dy, width, height);
|
|
397
|
+
for (var i = sourceIndex, j = destIndex; i < src.length; i += 4, j += 4) {
|
|
398
|
+
dest.data[j] = src[i];
|
|
399
|
+
}
|
|
400
|
+
if (!this.transparent) {
|
|
401
|
+
for (var a = 3; a < dest.data.length; a += 4) dest.data[a] = 255;
|
|
402
|
+
}
|
|
403
|
+
this._ctx.putImageData(dest, dx, dy);
|
|
404
|
+
} catch (_) {}
|
|
405
|
+
}
|
|
406
|
+
threshold(source, sourceRect, destPoint, operation, threshold, color, mask, copySource) {
|
|
407
|
+
if (!source || !sourceRect || !destPoint) throw new TypeError('source, sourceRect, and destPoint are required');
|
|
408
|
+
var op = String(operation);
|
|
409
|
+
if (['<', '<=', '>', '>=', '==', '!='].indexOf(op) < 0) throw new RangeError('Invalid threshold operation: ' + op);
|
|
410
|
+
if (!this._ensure() || !source._ensure || !source._ensure() || !source._ctx) return 0;
|
|
411
|
+
var sx = Math.floor(+sourceRect.x || 0), sy = Math.floor(+sourceRect.y || 0);
|
|
412
|
+
var dx = Math.floor(+destPoint.x || 0), dy = Math.floor(+destPoint.y || 0);
|
|
413
|
+
var width = Math.floor(+sourceRect.width || 0), height = Math.floor(+sourceRect.height || 0);
|
|
414
|
+
if (width <= 0 || height <= 0) return 0;
|
|
415
|
+
if (sx < 0) { var srcLeft = -sx; sx = 0; dx += srcLeft; width -= srcLeft; }
|
|
416
|
+
if (sy < 0) { var srcTop = -sy; sy = 0; dy += srcTop; height -= srcTop; }
|
|
417
|
+
width = Math.min(width, source.width - sx);
|
|
418
|
+
height = Math.min(height, source.height - sy);
|
|
419
|
+
if (dx < 0) { var dstLeft = -dx; dx = 0; sx += dstLeft; width -= dstLeft; }
|
|
420
|
+
if (dy < 0) { var dstTop = -dy; dy = 0; sy += dstTop; height -= dstTop; }
|
|
421
|
+
width = Math.min(width, this.width - dx);
|
|
422
|
+
height = Math.min(height, this.height - dy);
|
|
423
|
+
if (width <= 0 || height <= 0) return 0;
|
|
424
|
+
var thresholdValue = Number(threshold) >>> 0;
|
|
425
|
+
var maskValue = mask == null ? 0xffffffff : Number(mask) >>> 0;
|
|
426
|
+
var colorValue = color == null ? 0 : Number(color) >>> 0;
|
|
427
|
+
var count = 0;
|
|
428
|
+
try {
|
|
429
|
+
var src = source._ctx.getImageData(sx, sy, width, height).data;
|
|
430
|
+
var dest = this._ctx.getImageData(dx, dy, width, height);
|
|
431
|
+
var dst = dest.data;
|
|
432
|
+
for (var i = 0; i < src.length; i += 4) {
|
|
433
|
+
var oldR = dst[i], oldG = dst[i + 1], oldB = dst[i + 2], oldA = dst[i + 3];
|
|
434
|
+
var sourcePixel = (((src[i + 3] << 24) | (src[i] << 16) | (src[i + 1] << 8) | src[i + 2]) >>> 0);
|
|
435
|
+
var lhs = (sourcePixel & maskValue) >>> 0, rhs = (thresholdValue & maskValue) >>> 0;
|
|
436
|
+
var matches = op === '<' ? lhs < rhs : op === '<=' ? lhs <= rhs :
|
|
437
|
+
op === '>' ? lhs > rhs : op === '>=' ? lhs >= rhs : op === '==' ? lhs === rhs : lhs !== rhs;
|
|
438
|
+
var out = i;
|
|
439
|
+
if (matches) {
|
|
440
|
+
dst[out] = (colorValue >>> 16) & 255;
|
|
441
|
+
dst[out + 1] = (colorValue >>> 8) & 255;
|
|
442
|
+
dst[out + 2] = colorValue & 255;
|
|
443
|
+
dst[out + 3] = this.transparent ? (colorValue >>> 24) & 255 : 255;
|
|
444
|
+
} else if (copySource) {
|
|
445
|
+
dst[out] = src[i]; dst[out + 1] = src[i + 1]; dst[out + 2] = src[i + 2];
|
|
446
|
+
dst[out + 3] = this.transparent ? src[i + 3] : 255;
|
|
447
|
+
} else continue;
|
|
448
|
+
if (dst[out] !== oldR || dst[out + 1] !== oldG || dst[out + 2] !== oldB || dst[out + 3] !== oldA) count++;
|
|
449
|
+
}
|
|
450
|
+
this._ctx.putImageData(dest, dx, dy);
|
|
451
|
+
} catch (_) {}
|
|
452
|
+
return count;
|
|
453
|
+
}
|
|
454
|
+
paletteMap(source, sourceRect, destPoint, redArray, greenArray, blueArray, alphaArray) {
|
|
455
|
+
if (!source || !sourceRect || !destPoint) throw new TypeError('source, sourceRect, and destPoint are required');
|
|
456
|
+
if (!this._ensure() || !source._ensure || !source._ensure() || !source._ctx) return;
|
|
457
|
+
var sx = Math.floor(+sourceRect.x || 0), sy = Math.floor(+sourceRect.y || 0);
|
|
458
|
+
var dx = Math.floor(+destPoint.x || 0), dy = Math.floor(+destPoint.y || 0);
|
|
459
|
+
var width = Math.floor(+sourceRect.width || 0), height = Math.floor(+sourceRect.height || 0);
|
|
460
|
+
if (width <= 0 || height <= 0) return;
|
|
461
|
+
if (sx < 0) { var srcLeft = -sx; sx = 0; dx += srcLeft; width -= srcLeft; }
|
|
462
|
+
if (sy < 0) { var srcTop = -sy; sy = 0; dy += srcTop; height -= srcTop; }
|
|
463
|
+
width = Math.min(width, source.width - sx);
|
|
464
|
+
height = Math.min(height, source.height - sy);
|
|
465
|
+
if (dx < 0) { var dstLeft = -dx; dx = 0; sx += dstLeft; width -= dstLeft; }
|
|
466
|
+
if (dy < 0) { var dstTop = -dy; dy = 0; sy += dstTop; height -= dstTop; }
|
|
467
|
+
width = Math.min(width, this.width - dx);
|
|
468
|
+
height = Math.min(height, this.height - dy);
|
|
469
|
+
if (width <= 0 || height <= 0) return;
|
|
470
|
+
function lookup(table, index, fallback) {
|
|
471
|
+
if (table == null) return fallback >>> 0;
|
|
472
|
+
var value = table[index];
|
|
473
|
+
return value == null ? 0 : Number(value) >>> 0;
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
var src = source._ctx.getImageData(sx, sy, width, height).data;
|
|
477
|
+
var dest = this._ctx.getImageData(dx, dy, width, height);
|
|
478
|
+
for (var i = 0; i < src.length; i += 4) {
|
|
479
|
+
var r = src[i], g = src[i + 1], b = src[i + 2], a = src[i + 3];
|
|
480
|
+
var mapped = (lookup(redArray, r, r << 16) + lookup(greenArray, g, g << 8) +
|
|
481
|
+
lookup(blueArray, b, b) + lookup(alphaArray, a, a << 24)) >>> 0;
|
|
482
|
+
dest.data[i] = (mapped >>> 16) & 255;
|
|
483
|
+
dest.data[i + 1] = (mapped >>> 8) & 255;
|
|
484
|
+
dest.data[i + 2] = mapped & 255;
|
|
485
|
+
dest.data[i + 3] = this.transparent ? (mapped >>> 24) & 255 : 255;
|
|
486
|
+
}
|
|
487
|
+
this._ctx.putImageData(dest, dx, dy);
|
|
488
|
+
} catch (_) {}
|
|
489
|
+
}
|
|
490
|
+
getPixels(rect) {
|
|
491
|
+
if (!rect) throw new TypeError('rect is required');
|
|
492
|
+
var x = Math.floor(+rect.x || 0), y = Math.floor(+rect.y || 0);
|
|
493
|
+
var width = Math.floor(+rect.width || 0), height = Math.floor(+rect.height || 0);
|
|
494
|
+
var left = Math.max(0, x), top = Math.max(0, y);
|
|
495
|
+
var right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
496
|
+
var result = new ByteArray();
|
|
497
|
+
result.endian = Endian.BIG_ENDIAN;
|
|
498
|
+
if (right <= left || bottom <= top || !this._ensure()) return result;
|
|
499
|
+
try {
|
|
500
|
+
var image = this._ctx.getImageData(left, top, right - left, bottom - top).data;
|
|
501
|
+
for (var i = 0; i < image.length; i += 4) {
|
|
502
|
+
var alpha = this.transparent ? image[i + 3] : 255;
|
|
503
|
+
result.writeUnsignedInt(((alpha << 24) | (image[i] << 16) | (image[i + 1] << 8) | image[i + 2]) >>> 0);
|
|
504
|
+
}
|
|
505
|
+
} catch (_) {}
|
|
506
|
+
return result;
|
|
507
|
+
}
|
|
508
|
+
setPixels(rect, inputByteArray) {
|
|
509
|
+
if (!rect || !inputByteArray) throw new TypeError('rect and inputByteArray are required');
|
|
510
|
+
if (typeof inputByteArray.readUnsignedInt !== 'function') throw new TypeError('inputByteArray must provide readUnsignedInt');
|
|
511
|
+
var x = Math.floor(+rect.x || 0), y = Math.floor(+rect.y || 0);
|
|
512
|
+
var width = Math.floor(+rect.width || 0), height = Math.floor(+rect.height || 0);
|
|
513
|
+
var left = Math.max(0, x), top = Math.max(0, y);
|
|
514
|
+
var right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
515
|
+
if (right <= left || bottom <= top || !this._ensure()) return;
|
|
516
|
+
var clippedWidth = right - left, clippedHeight = bottom - top;
|
|
517
|
+
var image;
|
|
518
|
+
try { image = this._ctx.getImageData(left, top, clippedWidth, clippedHeight); }
|
|
519
|
+
catch (_) { return; }
|
|
520
|
+
var values = image.data, completed = 0;
|
|
521
|
+
for (var row = 0; row < clippedHeight; row++) {
|
|
522
|
+
for (var col = 0; col < clippedWidth; col++) {
|
|
523
|
+
var available = inputByteArray.bytesAvailable;
|
|
524
|
+
if (available == null && inputByteArray.length != null && inputByteArray.position != null) {
|
|
525
|
+
available = inputByteArray.length - inputByteArray.position;
|
|
526
|
+
}
|
|
527
|
+
if (available != null && available < 4) {
|
|
528
|
+
this._ctx.putImageData(image, left, top);
|
|
529
|
+
var eof = new Error('ByteArray ended before all BitmapData pixels were set');
|
|
530
|
+
eof.name = 'EOFError';
|
|
531
|
+
throw eof;
|
|
532
|
+
}
|
|
533
|
+
var pixel = Number(inputByteArray.readUnsignedInt()) >>> 0;
|
|
534
|
+
var i = completed * 4;
|
|
535
|
+
values[i] = (pixel >>> 16) & 255;
|
|
536
|
+
values[i + 1] = (pixel >>> 8) & 255;
|
|
537
|
+
values[i + 2] = pixel & 255;
|
|
538
|
+
values[i + 3] = this.transparent ? (pixel >>> 24) & 255 : 255;
|
|
539
|
+
completed++;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
this._ctx.putImageData(image, left, top);
|
|
543
|
+
}
|
|
544
|
+
getVector(rect) {
|
|
545
|
+
if (!rect) throw new TypeError('rect is required');
|
|
546
|
+
var x = Math.floor(+rect.x || 0), y = Math.floor(+rect.y || 0);
|
|
547
|
+
var width = Math.floor(+rect.width || 0), height = Math.floor(+rect.height || 0);
|
|
548
|
+
var left = Math.max(0, x), top = Math.max(0, y);
|
|
549
|
+
var right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
550
|
+
var clippedWidth = Math.max(0, right - left), clippedHeight = Math.max(0, bottom - top);
|
|
551
|
+
var result = Vector(clippedWidth * clippedHeight, false);
|
|
552
|
+
if (!clippedWidth || !clippedHeight || !this._ensure()) return result;
|
|
553
|
+
try {
|
|
554
|
+
var image = this._ctx.getImageData(left, top, clippedWidth, clippedHeight).data;
|
|
555
|
+
for (var i = 0, out = 0; i < image.length; i += 4, out++) {
|
|
556
|
+
var alpha = this.transparent ? image[i + 3] : 255;
|
|
557
|
+
result[out] = ((alpha << 24) | (image[i] << 16) | (image[i + 1] << 8) | image[i + 2]) >>> 0;
|
|
558
|
+
}
|
|
559
|
+
} catch (_) {}
|
|
560
|
+
return result;
|
|
561
|
+
}
|
|
562
|
+
setVector(rect, inputVector) {
|
|
563
|
+
if (!rect || inputVector == null) throw new TypeError('rect and inputVector are required');
|
|
564
|
+
if (typeof inputVector.length !== 'number') throw new TypeError('inputVector must be array-like');
|
|
565
|
+
var x = Math.floor(+rect.x || 0), y = Math.floor(+rect.y || 0);
|
|
566
|
+
var width = Math.floor(+rect.width || 0), height = Math.floor(+rect.height || 0);
|
|
567
|
+
var left = Math.max(0, x), top = Math.max(0, y);
|
|
568
|
+
var right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
569
|
+
var clippedWidth = Math.max(0, right - left), clippedHeight = Math.max(0, bottom - top);
|
|
570
|
+
if (!clippedWidth || !clippedHeight || !this._ensure()) return;
|
|
571
|
+
var image;
|
|
572
|
+
try { image = this._ctx.getImageData(left, top, clippedWidth, clippedHeight); }
|
|
573
|
+
catch (_) { return; }
|
|
574
|
+
var count = clippedWidth * clippedHeight, available = Math.min(count, Math.max(0, inputVector.length | 0));
|
|
575
|
+
for (var i = 0; i < available; i++) {
|
|
576
|
+
var value = Number(inputVector[i]) >>> 0, at = i * 4;
|
|
577
|
+
image.data[at] = (value >>> 16) & 255;
|
|
578
|
+
image.data[at + 1] = (value >>> 8) & 255;
|
|
579
|
+
image.data[at + 2] = value & 255;
|
|
580
|
+
image.data[at + 3] = this.transparent ? (value >>> 24) & 255 : 255;
|
|
581
|
+
}
|
|
582
|
+
this._ctx.putImageData(image, left, top);
|
|
583
|
+
if (available < count) {
|
|
584
|
+
var rangeError = new RangeError('inputVector does not contain enough pixels');
|
|
585
|
+
throw rangeError;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
merge(source, sourceRect, destPoint, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier) {
|
|
589
|
+
if (!source || !sourceRect || !destPoint) throw new TypeError('source, sourceRect, and destPoint are required');
|
|
590
|
+
if (!this._ensure() || !source._ensure || !source._ensure() || !source._ctx) return;
|
|
591
|
+
var sx = Math.floor(+sourceRect.x || 0), sy = Math.floor(+sourceRect.y || 0);
|
|
592
|
+
var dx = Math.floor(+destPoint.x || 0), dy = Math.floor(+destPoint.y || 0);
|
|
593
|
+
var width = Math.floor(+sourceRect.width || 0), height = Math.floor(+sourceRect.height || 0);
|
|
594
|
+
if (width <= 0 || height <= 0) return;
|
|
595
|
+
if (sx < 0) { var srcLeft = -sx; sx = 0; dx += srcLeft; width -= srcLeft; }
|
|
596
|
+
if (sy < 0) { var srcTop = -sy; sy = 0; dy += srcTop; height -= srcTop; }
|
|
597
|
+
width = Math.min(width, source.width - sx);
|
|
598
|
+
height = Math.min(height, source.height - sy);
|
|
599
|
+
if (dx < 0) { var dstLeft = -dx; dx = 0; sx += dstLeft; width -= dstLeft; }
|
|
600
|
+
if (dy < 0) { var dstTop = -dy; dy = 0; sy += dstTop; height -= dstTop; }
|
|
601
|
+
width = Math.min(width, this.width - dx);
|
|
602
|
+
height = Math.min(height, this.height - dy);
|
|
603
|
+
if (width <= 0 || height <= 0) return;
|
|
604
|
+
function multiplier(value) { return Math.max(0, Math.min(256, Number(value) >>> 0)); }
|
|
605
|
+
var multipliers = [multiplier(redMultiplier), multiplier(greenMultiplier), multiplier(blueMultiplier), multiplier(alphaMultiplier)];
|
|
606
|
+
try {
|
|
607
|
+
var src = source._ctx.getImageData(sx, sy, width, height).data;
|
|
608
|
+
var dest = this._ctx.getImageData(dx, dy, width, height);
|
|
609
|
+
var dst = dest.data;
|
|
610
|
+
for (var i = 0; i < src.length; i += 4) {
|
|
611
|
+
for (var channel = 0; channel < 4; channel++) {
|
|
612
|
+
if (channel === 3 && !this.transparent) { dst[i + channel] = 255; continue; }
|
|
613
|
+
var weight = multipliers[channel];
|
|
614
|
+
dst[i + channel] = Math.floor((src[i + channel] * weight + dst[i + channel] * (256 - weight)) / 256);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
this._ctx.putImageData(dest, dx, dy);
|
|
618
|
+
} catch (_) {}
|
|
619
|
+
}
|
|
620
|
+
colorTransform(rect, ct) {
|
|
621
|
+
if (!this._ensure() || !rect || !ct) return;
|
|
622
|
+
try {
|
|
623
|
+
var img = this._ctx.getImageData(rect.x || 0, rect.y || 0, rect.width || 0, rect.height || 0);
|
|
624
|
+
var d = img.data;
|
|
625
|
+
for (var i = 0; i < d.length; i += 4) {
|
|
626
|
+
d[i] = d[i] * ct.redMultiplier + ct.redOffset;
|
|
627
|
+
d[i + 1] = d[i + 1] * ct.greenMultiplier + ct.greenOffset;
|
|
628
|
+
d[i + 2] = d[i + 2] * ct.blueMultiplier + ct.blueOffset;
|
|
629
|
+
d[i + 3] = d[i + 3] * ct.alphaMultiplier + ct.alphaOffset;
|
|
630
|
+
}
|
|
631
|
+
this._ctx.putImageData(img, rect.x || 0, rect.y || 0);
|
|
632
|
+
} catch (_) {}
|
|
633
|
+
}
|
|
634
|
+
getPixel(x, y) {
|
|
635
|
+
if (!this._ensure()) return 0;
|
|
636
|
+
try {
|
|
637
|
+
var d = this._ctx.getImageData(x | 0, y | 0, 1, 1).data;
|
|
638
|
+
return ((d[0] << 16) | (d[1] << 8) | d[2]) >>> 0;
|
|
639
|
+
} catch (_) { return 0; }
|
|
640
|
+
}
|
|
641
|
+
getPixel32(x, y) {
|
|
642
|
+
if (!this._ensure()) return 0;
|
|
643
|
+
try {
|
|
644
|
+
var d = this._ctx.getImageData(x | 0, y | 0, 1, 1).data;
|
|
645
|
+
return ((d[3] << 24) | (d[0] << 16) | (d[1] << 8) | d[2]) >>> 0;
|
|
646
|
+
} catch (_) { return 0; }
|
|
647
|
+
}
|
|
648
|
+
setPixel(x, y, color) {
|
|
649
|
+
this.setPixel32(x, y, 0xff000000 | (color & 0xffffff));
|
|
650
|
+
}
|
|
651
|
+
setPixel32(x, y, color) {
|
|
652
|
+
if (!this._ensure()) return;
|
|
653
|
+
var a = (color >>> 24) & 255;
|
|
654
|
+
var r = (color >>> 16) & 255;
|
|
655
|
+
var g = (color >>> 8) & 255;
|
|
656
|
+
var b = color & 255;
|
|
657
|
+
this._ctx.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + (a / 255) + ')';
|
|
658
|
+
this._ctx.fillRect(x | 0, y | 0, 1, 1);
|
|
659
|
+
}
|
|
660
|
+
getColorBoundsRect(mask, color, findColor) {
|
|
661
|
+
if (findColor == null) findColor = true;
|
|
662
|
+
var empty = new Rectangle(0, 0, 0, 0);
|
|
663
|
+
if (!this._ensure() || !this.width || !this.height) return empty;
|
|
664
|
+
var maskValue = Number(mask) >>> 0, colorValue = Number(color) >>> 0;
|
|
665
|
+
try {
|
|
666
|
+
var image = this._ctx.getImageData(0, 0, this.width, this.height).data;
|
|
667
|
+
var minX = this.width, minY = this.height, maxX = -1, maxY = -1;
|
|
668
|
+
for (var i = 0; i < image.length; i += 4) {
|
|
669
|
+
var alpha = this.transparent ? image[i + 3] : 255;
|
|
670
|
+
var value = ((alpha << 24) | (image[i] << 16) | (image[i + 1] << 8) | image[i + 2]) >>> 0;
|
|
671
|
+
var matches = ((value & maskValue) >>> 0) === colorValue;
|
|
672
|
+
if (matches !== !!findColor) continue;
|
|
673
|
+
var pixelIndex = i >> 2, x = pixelIndex % this.width, y = (pixelIndex / this.width) | 0;
|
|
674
|
+
if (x < minX) minX = x;
|
|
675
|
+
if (y < minY) minY = y;
|
|
676
|
+
if (x > maxX) maxX = x;
|
|
677
|
+
if (y > maxY) maxY = y;
|
|
678
|
+
}
|
|
679
|
+
if (maxX < minX || maxY < minY) return empty;
|
|
680
|
+
return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
|
|
681
|
+
} catch (_) { return empty; }
|
|
682
|
+
}
|
|
683
|
+
hitTest(firstPoint, firstAlphaThreshold, secondObject, secondBitmapDataPoint, secondAlphaThreshold) {
|
|
684
|
+
if (!firstPoint || !secondObject) return false;
|
|
685
|
+
var firstX = Math.floor(+firstPoint.x || 0), firstY = Math.floor(+firstPoint.y || 0);
|
|
686
|
+
var firstThreshold = Math.max(0, Math.min(255, firstAlphaThreshold | 0));
|
|
687
|
+
var secondThreshold = secondAlphaThreshold == null ? 1 : Math.max(0, Math.min(255, secondAlphaThreshold | 0));
|
|
688
|
+
function alphaAt(bitmap, x, y, image) {
|
|
689
|
+
if (!bitmap.transparent) return 255;
|
|
690
|
+
var index = ((y * bitmap.width + x) << 2) + 3;
|
|
691
|
+
return image[index] || 0;
|
|
692
|
+
}
|
|
693
|
+
function imageData(bitmap) {
|
|
694
|
+
if (!bitmap._ensure || !bitmap._ensure() || !bitmap._ctx) return null;
|
|
695
|
+
try { return bitmap._ctx.getImageData(0, 0, bitmap.width, bitmap.height).data; }
|
|
696
|
+
catch (_) { return null; }
|
|
697
|
+
}
|
|
698
|
+
var firstImage = imageData(this);
|
|
699
|
+
if (!firstImage) return false;
|
|
700
|
+
var second = secondObject instanceof Bitmap ? secondObject.bitmapData : secondObject;
|
|
701
|
+
if (second && second._ensure && typeof second.width === 'number' && typeof second.height === 'number') {
|
|
702
|
+
var secondImage = imageData(second);
|
|
703
|
+
if (!secondImage) return false;
|
|
704
|
+
var secondPoint = secondObject instanceof Bitmap
|
|
705
|
+
? { x: +secondObject.x || 0, y: +secondObject.y || 0 }
|
|
706
|
+
: (secondBitmapDataPoint || { x: 0, y: 0 });
|
|
707
|
+
var secondX = Math.floor(+secondPoint.x || 0), secondY = Math.floor(+secondPoint.y || 0);
|
|
708
|
+
var left = Math.max(firstX, secondX), top = Math.max(firstY, secondY);
|
|
709
|
+
var right = Math.min(firstX + this.width, secondX + second.width);
|
|
710
|
+
var bottom = Math.min(firstY + this.height, secondY + second.height);
|
|
711
|
+
for (var gy = top; gy < bottom; gy++) for (var gx = left; gx < right; gx++) {
|
|
712
|
+
var ax = gx - firstX, ay = gy - firstY, bx = gx - secondX, by = gy - secondY;
|
|
713
|
+
if (alphaAt(this, ax, ay, firstImage) >= firstThreshold && alphaAt(second, bx, by, secondImage) >= secondThreshold) return true;
|
|
714
|
+
}
|
|
715
|
+
return false;
|
|
716
|
+
}
|
|
717
|
+
if (secondObject && typeof secondObject.width === 'number' && typeof secondObject.height === 'number') {
|
|
718
|
+
var rectLeft = Math.floor(+secondObject.x || 0), rectTop = Math.floor(+secondObject.y || 0);
|
|
719
|
+
var rectRight = Math.ceil(rectLeft + (+secondObject.width || 0));
|
|
720
|
+
var rectBottom = Math.ceil(rectTop + (+secondObject.height || 0));
|
|
721
|
+
var overlapLeft = Math.max(firstX, rectLeft), overlapTop = Math.max(firstY, rectTop);
|
|
722
|
+
var overlapRight = Math.min(firstX + this.width, rectRight), overlapBottom = Math.min(firstY + this.height, rectBottom);
|
|
723
|
+
if (overlapLeft >= overlapRight || overlapTop >= overlapBottom) return false;
|
|
724
|
+
if (!this.transparent) return true;
|
|
725
|
+
for (var ry = overlapTop; ry < overlapBottom; ry++) for (var rx = overlapLeft; rx < overlapRight; rx++) {
|
|
726
|
+
if (alphaAt(this, rx - firstX, ry - firstY, firstImage) >= firstThreshold) return true;
|
|
727
|
+
}
|
|
728
|
+
return false;
|
|
729
|
+
}
|
|
730
|
+
if (typeof secondObject.x === 'number' && typeof secondObject.y === 'number') {
|
|
731
|
+
var px = Math.floor(secondObject.x) - firstX, py = Math.floor(secondObject.y) - firstY;
|
|
732
|
+
return px >= 0 && py >= 0 && px < this.width && py < this.height &&
|
|
733
|
+
alphaAt(this, px, py, firstImage) >= firstThreshold;
|
|
734
|
+
}
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
draw(source, matrix, colorTransform, blendMode, clipRect, smoothing) {
|
|
738
|
+
drawBitmapData(this, source, matrix, colorTransform, blendMode, clipRect, smoothing, null);
|
|
739
|
+
}
|
|
740
|
+
drawWithQuality(source, matrix, colorTransform, blendMode, clipRect, smoothing, quality) {
|
|
741
|
+
drawBitmapData(this, source, matrix, colorTransform, blendMode, clipRect, smoothing, quality);
|
|
742
|
+
}
|
|
743
|
+
noise(randomSeed, low, high, channelOptions, grayScale) {
|
|
744
|
+
if (!this._ensure()) return;
|
|
745
|
+
low = low === undefined ? 0 : low & 255;
|
|
746
|
+
high = high === undefined ? 255 : Math.max(low, high & 255);
|
|
747
|
+
channelOptions = channelOptions === undefined ? 7 : channelOptions | 0;
|
|
748
|
+
var state = randomSeed | 0;
|
|
749
|
+
if (state <= 0) state = 1 - state;
|
|
750
|
+
var range = high - low + 1;
|
|
751
|
+
function byte() {
|
|
752
|
+
// Park–Miller, whose integer product is exactly representable in JS.
|
|
753
|
+
state = (state * 16807) % 2147483647;
|
|
754
|
+
return low + state % range;
|
|
755
|
+
}
|
|
756
|
+
var noiseAlpha = this.transparent && (channelOptions & BitmapDataChannel.ALPHA);
|
|
757
|
+
try {
|
|
758
|
+
var image = this._ctx.createImageData(this.width, this.height), d = image.data;
|
|
759
|
+
for (var i = 0; i < d.length; i += 4) {
|
|
760
|
+
if (grayScale) d[i] = d[i + 1] = d[i + 2] = byte();
|
|
761
|
+
else {
|
|
762
|
+
d[i] = channelOptions & BitmapDataChannel.RED ? byte() : 0;
|
|
763
|
+
d[i + 1] = channelOptions & BitmapDataChannel.GREEN ? byte() : 0;
|
|
764
|
+
d[i + 2] = channelOptions & BitmapDataChannel.BLUE ? byte() : 0;
|
|
765
|
+
}
|
|
766
|
+
d[i + 3] = noiseAlpha ? byte() : 255;
|
|
767
|
+
}
|
|
768
|
+
this._ctx.putImageData(image, 0, 0);
|
|
769
|
+
} catch (_) {}
|
|
770
|
+
}
|
|
771
|
+
// Gradient noise follows the W3C SVG 1.1 feTurbulence reference.
|
|
772
|
+
// Copyright © 2015 W3C® (MIT, ERCIM, Keio, Beihang).
|
|
773
|
+
// https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
|
|
774
|
+
perlinNoise(baseX, baseY, numOctaves, randomSeed, stitch, fractalNoise, channelOptions, grayScale, offsets) {
|
|
775
|
+
if (!this._ensure()) return;
|
|
776
|
+
channelOptions = channelOptions === undefined ? 7 : channelOptions | 0;
|
|
777
|
+
var state = randomSeed | 0;
|
|
778
|
+
state = state <= 0 ? 1 - state % 2147483646 : Math.min(state, 2147483646);
|
|
779
|
+
function next() {
|
|
780
|
+
state = (state * 16807) % 2147483647;
|
|
781
|
+
return state;
|
|
782
|
+
}
|
|
783
|
+
var permutation = new Uint8Array(256);
|
|
784
|
+
var gradients = Array.from({ length: 4 }, function () { return new Float64Array(512); });
|
|
785
|
+
for (var channel = 0; channel < 4; channel++) {
|
|
786
|
+
for (var i = 0; i < 256; i++) {
|
|
787
|
+
var gx = next() % 512 - 256, gy = next() % 512 - 256;
|
|
788
|
+
var length = Math.sqrt(gx * gx + gy * gy);
|
|
789
|
+
gradients[channel][2 * i] = gx / length;
|
|
790
|
+
gradients[channel][2 * i + 1] = gy / length;
|
|
791
|
+
permutation[i] = i;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
for (var i = 255; i > 0; i--) {
|
|
795
|
+
var other = next() % 256, value = permutation[i];
|
|
796
|
+
permutation[i] = permutation[other];
|
|
797
|
+
permutation[other] = value;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
baseX = +baseX;
|
|
801
|
+
baseY = +baseY;
|
|
802
|
+
var freqX = baseX === 0 ? 0 : 1 / baseX;
|
|
803
|
+
var freqY = baseY === 0 ? 0 : 1 / baseY;
|
|
804
|
+
function snap(frequency, lowerFrequency, size) {
|
|
805
|
+
if (frequency === 0) return 0;
|
|
806
|
+
var low = Math.floor(size * lowerFrequency) / size;
|
|
807
|
+
var high = Math.ceil(size * frequency) / size;
|
|
808
|
+
return frequency / low < high / frequency ? low : high;
|
|
809
|
+
}
|
|
810
|
+
if (stitch) {
|
|
811
|
+
freqX = snap(freqX, freqX, this.width);
|
|
812
|
+
// The reference uses the adjusted X frequency for Y's lower candidate.
|
|
813
|
+
freqY = snap(freqY, freqX, this.height);
|
|
814
|
+
}
|
|
815
|
+
var periodX = stitch ? Math.trunc(this.width * freqX + 0.5) : 0;
|
|
816
|
+
var periodY = stitch ? Math.trunc(this.height * freqY + 0.5) : 0;
|
|
817
|
+
var octaves = [];
|
|
818
|
+
// Beyond 1024 doublings the frequency multiplier is no longer finite.
|
|
819
|
+
var count = Math.min(numOctaves >>> 0, 1024);
|
|
820
|
+
for (var octave = 0, ratio = 1; octave < count; octave++, ratio *= 2) {
|
|
821
|
+
var offset = Array.isArray(offsets) ? offsets[octave] : null;
|
|
822
|
+
octaves.push({
|
|
823
|
+
x: offset instanceof Point ? offset.x : 0,
|
|
824
|
+
y: offset instanceof Point ? offset.y : 0,
|
|
825
|
+
ratio: ratio, width: periodX * ratio, height: periodY * ratio,
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
function fade(t) { return t * t * (3 - 2 * t); }
|
|
829
|
+
function lerp(t, a, b) { return a + t * (b - a); }
|
|
830
|
+
function dot(gradient, index, x, y) { return x * gradient[2 * index] + y * gradient[2 * index + 1]; }
|
|
831
|
+
function lattice(fraction) {
|
|
832
|
+
return Math.trunc(Math.max(-2147483648, Math.min(2147483647, fraction))) || 0;
|
|
833
|
+
}
|
|
834
|
+
function wrap(coordinate, period) {
|
|
835
|
+
return (stitch && coordinate >= 4096 + period ? coordinate - period : coordinate) & 255;
|
|
836
|
+
}
|
|
837
|
+
function sample(gradient, x, y) {
|
|
838
|
+
var sum = 0;
|
|
839
|
+
for (var i = 0; i < octaves.length; i++) {
|
|
840
|
+
var step = octaves[i];
|
|
841
|
+
var tx = (x + step.x) * freqX * step.ratio + 4096;
|
|
842
|
+
var ty = (y + step.y) * freqY * step.ratio + 4096;
|
|
843
|
+
var ix = lattice(tx), iy = lattice(ty);
|
|
844
|
+
var rx = tx - ix, ry = ty - iy;
|
|
845
|
+
var px = permutation[wrap(ix, step.width)], nx = permutation[wrap(ix + 1, step.width)];
|
|
846
|
+
var py = wrap(iy, step.height), ny = wrap(iy + 1, step.height);
|
|
847
|
+
var sx = fade(rx), sy = fade(ry);
|
|
848
|
+
var top = lerp(sx, dot(gradient, permutation[(px + py) & 255], rx, ry),
|
|
849
|
+
dot(gradient, permutation[(nx + py) & 255], rx - 1, ry));
|
|
850
|
+
var bottom = lerp(sx, dot(gradient, permutation[(px + ny) & 255], rx, ry - 1),
|
|
851
|
+
dot(gradient, permutation[(nx + ny) & 255], rx - 1, ry - 1));
|
|
852
|
+
var value = lerp(sy, top, bottom);
|
|
853
|
+
sum += (fractalNoise ? value : Math.abs(value)) / step.ratio;
|
|
854
|
+
}
|
|
855
|
+
return Math.trunc(fractalNoise ? (sum * 255 + 255 + 0.5) / 2 : sum * 255 + 0.5);
|
|
856
|
+
}
|
|
857
|
+
try {
|
|
858
|
+
var image = this._ctx.createImageData(this.width, this.height), data = image.data;
|
|
859
|
+
var sampleAlpha = this.transparent && (channelOptions & BitmapDataChannel.ALPHA);
|
|
860
|
+
for (var y = 0, i = 0; y < this.height; y++) {
|
|
861
|
+
for (var x = 0; x < this.width; x++, i += 4) {
|
|
862
|
+
var bank = 0;
|
|
863
|
+
if (grayScale) {
|
|
864
|
+
data[i] = data[i + 1] = data[i + 2] = sample(gradients[bank++], x, y);
|
|
865
|
+
} else {
|
|
866
|
+
for (var c = 0; c < 3; c++) {
|
|
867
|
+
data[i + c] = channelOptions & (1 << c) ? sample(gradients[bank++], x, y) : 0;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
data[i + 3] = sampleAlpha ? sample(gradients[bank], x, y) : 255;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
this._ctx.putImageData(image, 0, 0);
|
|
874
|
+
} catch (_) {}
|
|
875
|
+
}
|
|
876
|
+
scroll(x, y) {
|
|
877
|
+
if (!this._ensure()) return;
|
|
878
|
+
try {
|
|
879
|
+
var img = this._ctx.getImageData(0, 0, this.width, this.height);
|
|
880
|
+
this._ctx.clearRect(0, 0, this.width, this.height);
|
|
881
|
+
this._ctx.putImageData(img, x | 0, y | 0);
|
|
882
|
+
} catch (_) {}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** DisplayObject wrapping a loaded HTMLImage / BitmapData. */
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
export function bitmapShadowPlan(filter) {
|
|
889
|
+
if (filter instanceof GlowFilter) return effectShadowPlan(filter, 'glow');
|
|
890
|
+
if (filter instanceof DropShadowFilter) return effectShadowPlan(filter, 'shadow');
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
export function bitmapContourPlan(filter) {
|
|
895
|
+
if (filter instanceof BevelFilter) return effectContourPlan(filter, 'bevel');
|
|
896
|
+
if (filter instanceof GradientBevelFilter) return effectContourPlan(filter, 'gradientBevel');
|
|
897
|
+
if (filter instanceof GradientGlowFilter) return effectContourPlan(filter, 'gradientGlow');
|
|
898
|
+
return null;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
export function bitmapFilterMargins(blur, effect) {
|
|
902
|
+
if (!blur || effect?.inner) return { left: 0, right: 0, top: 0, bottom: 0 };
|
|
903
|
+
var x = Math.ceil(blur.radiusX) * blur.passes, y = Math.ceil(blur.radiusY) * blur.passes;
|
|
904
|
+
var dx = effect?.dx || 0, dy = effect?.dy || 0;
|
|
905
|
+
if (effect?.twoDirections) {
|
|
906
|
+
x += Math.ceil(Math.abs(dx)); y += Math.ceil(Math.abs(dy));
|
|
907
|
+
return { left: x, right: x, top: y, bottom: y };
|
|
908
|
+
}
|
|
909
|
+
return { left: Math.max(0, x - Math.floor(dx)), right: Math.max(0, x + Math.ceil(dx)),
|
|
910
|
+
top: Math.max(0, y - Math.floor(dy)), bottom: Math.max(0, y + Math.ceil(dy)) };
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
export class BitmapDataChannel {
|
|
914
|
+
static RED = 1;
|
|
915
|
+
static GREEN = 2;
|
|
916
|
+
static BLUE = 4;
|
|
917
|
+
static ALPHA = 8;
|
|
918
|
+
}
|
|
919
|
+
export function bitmapChannelIndex(channel) {
|
|
920
|
+
return channel === BitmapDataChannel.RED ? 0 : channel === BitmapDataChannel.GREEN ? 1 :
|
|
921
|
+
channel === BitmapDataChannel.BLUE ? 2 : channel === BitmapDataChannel.ALPHA ? 3 : -1;
|
|
922
|
+
}
|
|
923
|
+
export function setBitmapDrawSmoothing(ctx, smoothing, quality) {
|
|
924
|
+
ctx.imageSmoothingEnabled = !!smoothing;
|
|
925
|
+
if (quality === StageQuality.LOW || quality === StageQuality.MEDIUM) {
|
|
926
|
+
ctx.imageSmoothingQuality = quality;
|
|
927
|
+
} else if (quality) {
|
|
928
|
+
ctx.imageSmoothingQuality = 'high';
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
export var avm1BitmapSources = new WeakMap();
|
|
932
|
+
export function drawBitmapData(target, source, matrix, colorTransform, blendMode, clipRect, smoothing, quality) {
|
|
933
|
+
if (!source || typeof source !== 'object' || !target._ensure()) return;
|
|
934
|
+
var left = 0, top = 0, right = target.width, bottom = target.height;
|
|
935
|
+
if (clipRect) {
|
|
936
|
+
left = Math.max(left, clipRect.x);
|
|
937
|
+
top = Math.max(top, clipRect.y);
|
|
938
|
+
right = Math.min(right, clipRect.x + clipRect.width);
|
|
939
|
+
bottom = Math.min(bottom, clipRect.y + clipRect.height);
|
|
940
|
+
}
|
|
941
|
+
if (!(right > left && bottom > top)) return;
|
|
942
|
+
matrix = matrix || new Matrix();
|
|
943
|
+
var displaySource = source instanceof DisplayObject;
|
|
944
|
+
var drawAvm1Source = avm1BitmapSources.get(source);
|
|
945
|
+
var backing = source.bitmapData;
|
|
946
|
+
var image = source._canvas || source._img || (backing && (backing._canvas || backing._img)) || source;
|
|
947
|
+
var rasterized = displaySource || drawAvm1Source || colorTransform || image === target._canvas;
|
|
948
|
+
var rasterX = Math.floor(left), rasterY = Math.floor(top);
|
|
949
|
+
// Isolate source effects and snapshot self-draws before modifying the destination.
|
|
950
|
+
if (rasterized) {
|
|
951
|
+
var scratch = makeEffectCanvas({
|
|
952
|
+
width: Math.ceil(right) - rasterX,
|
|
953
|
+
height: Math.ceil(bottom) - rasterY,
|
|
954
|
+
});
|
|
955
|
+
var sourceCtx = scratch && scratch.getContext('2d', colorTransform ? { willReadFrequently: true } : undefined);
|
|
956
|
+
if (!sourceCtx) return;
|
|
957
|
+
setBitmapDrawSmoothing(sourceCtx, smoothing, quality);
|
|
958
|
+
sourceCtx.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx - rasterX, matrix.ty - rasterY);
|
|
959
|
+
if (displaySource) renderDisplayTree(sourceCtx, source, 0, false, null, true, false, true);
|
|
960
|
+
else if (drawAvm1Source) drawAvm1Source(sourceCtx, source, {
|
|
961
|
+
a: matrix.a, b: matrix.b, c: matrix.c, d: matrix.d, tx: (matrix.tx - rasterX) * 20, ty: (matrix.ty - rasterY) * 20,
|
|
962
|
+
});
|
|
963
|
+
else {
|
|
964
|
+
try { sourceCtx.drawImage(image, 0, 0); } catch (_) { return; }
|
|
965
|
+
}
|
|
966
|
+
if (colorTransform) applyDisplayEffects(sourceCtx, colorTransform, null);
|
|
967
|
+
image = scratch;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
var ctx = target._ctx;
|
|
971
|
+
ctx.save();
|
|
972
|
+
try {
|
|
973
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
974
|
+
if (clipRect) {
|
|
975
|
+
ctx.beginPath();
|
|
976
|
+
ctx.rect(left, top, right - left, bottom - top);
|
|
977
|
+
ctx.clip();
|
|
978
|
+
}
|
|
979
|
+
ctx.globalCompositeOperation = blendModeToCanvas(blendMode) || 'source-over';
|
|
980
|
+
setBitmapDrawSmoothing(ctx, smoothing, quality);
|
|
981
|
+
if (rasterized) ctx.drawImage(image, rasterX, rasterY);
|
|
982
|
+
else {
|
|
983
|
+
setCanvasMatrix(ctx, matrix);
|
|
984
|
+
ctx.drawImage(image, 0, 0);
|
|
985
|
+
}
|
|
986
|
+
} catch (_) {
|
|
987
|
+
// Some DOM sources may not have loaded yet.
|
|
988
|
+
} finally {
|
|
989
|
+
ctx.restore();
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
export class Bitmap extends DisplayObject {
|
|
993
|
+
constructor(bitmapData) {
|
|
994
|
+
super();
|
|
995
|
+
this.bitmapData = bitmapData || null;
|
|
996
|
+
this.smoothing = true;
|
|
997
|
+
if (bitmapData) {
|
|
998
|
+
this.width = bitmapData.width || 0;
|
|
999
|
+
this.height = bitmapData.height || 0;
|
|
1000
|
+
this._swoofDisplayImage = bitmapDisplayImage(bitmapData);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|