-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathScratchPlugin.js
1254 lines (1132 loc) · 28 KB
/
ScratchPlugin.js
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
/* Smalltalk from Squeak4.5 with VMMaker 4.13.6 translated as JS source on 3 November 2014 1:52:23 pm */
/* Automatically generated by
JSPluginCodeGenerator VMMakerJS-bf.15 uuid: fd4e10f2-3773-4e80-8bb5-c4b471a014e5
from
ScratchPlugin VMMaker-bf.353 uuid: 8ae25e7e-8d2c-451e-8277-598b30e9c002
*/
module("users.bert.SqueakJS.plugins.ScratchPlugin").requires("users.bert.SqueakJS.vm").toRun(function() {
"use strict";
var VM_PROXY_MAJOR = 1;
var VM_PROXY_MINOR = 11;
/*** Functions ***/
function CLASSOF(obj) { return typeof obj === "number" ? interpreterProxy.classSmallInteger() : obj.sqClass }
function SIZEOF(obj) { return obj.pointers ? obj.pointers.length : obj.words ? obj.words.length : obj.bytes ? obj.bytes.length : 0 }
function BYTESIZEOF(obj) { return obj.bytes ? obj.bytes.length : obj.words ? obj.words.length * 4 : 0 }
function DIV(a, b) { return Math.floor(a / b) | 0; } // integer division
function MOD(a, b) { return a - DIV(a, b) * b | 0; } // signed modulus
function SHL(a, b) { return b > 31 ? 0 : a << b; } // fix JS shift
function SHR(a, b) { return b > 31 ? 0 : a >>> b; } // fix JS shift
function SHIFT(a, b) { return b < 0 ? (b < -31 ? 0 : a >>> (0-b) ) : (b > 31 ? 0 : a << b); }
/*** Variables ***/
var interpreterProxy = null;
var moduleName = "ScratchPlugin 3 November 2014 (e)";
function bitmapatputHsv(bitmap, i, hue, saturation, brightness) {
var hF;
var hI;
var outPix;
var p;
var q;
var t;
var v;
/* integer part of hue (0..5) */
hI = DIV(hue, 60);
/* fractional part ofhue */
hF = MOD(hue, 60);
p = (1000 - saturation) * brightness;
q = (1000 - (DIV((saturation * hF), 60))) * brightness;
t = (1000 - (DIV((saturation * (60 - hF)), 60))) * brightness;
v = DIV((brightness * 1000), 3922);
p = DIV(p, 3922);
q = DIV(q, 3922);
t = DIV(t, 3922);
if (0 === hI) {
outPix = ((v << 16) + (t << 8)) + p;
}
if (1 === hI) {
outPix = ((q << 16) + (v << 8)) + p;
}
if (2 === hI) {
outPix = ((p << 16) + (v << 8)) + t;
}
if (3 === hI) {
outPix = ((p << 16) + (q << 8)) + v;
}
if (4 === hI) {
outPix = ((t << 16) + (p << 8)) + v;
}
if (5 === hI) {
outPix = ((v << 16) + (p << 8)) + q;
}
if (outPix === 0) {
outPix = 1;
}
bitmap[i] = outPix;
return 0;
}
/* Return an unsigned int pointer to the first indexable word of oop, which must be a words object. */
function checkedFloatPtrOf(oop) {
interpreterProxy.success(interpreterProxy.isWordsOrBytes(oop));
if (interpreterProxy.failed()) {
return 0;
}
return oop.wordsAsFloat64Array();
}
/* Return an unsigned int pointer to the first indexable word of oop, which must be a words object. */
function checkedUnsignedIntPtrOf(oop) {
interpreterProxy.success(interpreterProxy.isWords(oop));
if (interpreterProxy.failed()) {
return 0;
}
return oop.words;
}
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
function getModuleName() {
return moduleName;
}
function halt() {
;
}
/* Answer the hue, an angle between 0 and 360. */
function hueFromRGBminmax(r, g, b, min, max) {
var result;
var span;
span = max - min;
if (span === 0) {
return 0;
}
if (r === max) {
result = DIV((60 * (g - b)), span);
} else {
if (g === max) {
result = 120 + (DIV((60 * (b - r)), span));
} else {
result = 240 + (DIV((60 * (r - g)), span));
}
}
if (result < 0) {
return result + 360;
}
return result;
}
/* Answer the interpolated pixel value between the given two pixel values. If either pixel is zero (transparent) answer the other pixel. If both pixels are transparent, answer transparent. The fraction is between 0 and 1023, out of a total range of 1024. */
function interpolateandfrac(pix1, pix2, frac2) {
var b;
var frac1;
var g;
var r;
var result;
if (pix1 === 0) {
return pix2;
}
if (pix2 === 0) {
return pix1;
}
frac1 = 1024 - frac2;
r = ((frac1 * ((pix1 >>> 16) & 255)) + (frac2 * ((pix2 >>> 16) & 255))) >> 10;
g = ((frac1 * ((pix1 >>> 8) & 255)) + (frac2 * ((pix2 >>> 8) & 255))) >> 10;
b = ((frac1 * (pix1 & 255)) + (frac2 * (pix2 & 255))) >> 10;
result = ((r << 16) + (g << 8)) + b;
if (result === 0) {
result = 1;
}
return result;
}
/* Answer the interpolated pixel value from the given bitmap at the given point. The x and y coordinates are fixed-point integers with 10 bits of fraction (i.e. they were multiplied by 1024, then truncated). If the given point is right on an edge, answer the nearest edge pixel value. If it is entirely outside of the image, answer 0 (transparent). */
function interpolatedFromxywidthheight(bitmap, xFixed, yFixed, w, h) {
var bottomPix;
var index;
var topPix;
var x;
var xFrac;
var y;
var yFrac;
x = xFixed >>> 10;
if ((x < -1) || (x >= w)) {
return 0;
}
y = yFixed >>> 10;
if ((y < -1) || (y >= h)) {
return 0;
}
xFrac = xFixed & 1023;
if (x === -1) {
x = 0;
xFrac = 0;
}
if (x === (w - 1)) {
xFrac = 0;
}
yFrac = yFixed & 1023;
if (y === -1) {
y = 0;
yFrac = 0;
}
if (y === (h - 1)) {
yFrac = 0;
}
/* for squeak: + 1 */
index = (y * w) + x;
topPix = bitmap[index] & 16777215;
if (xFrac > 0) {
topPix = interpolateandfrac(topPix, bitmap[index + 1] & 16777215, xFrac);
}
if (yFrac === 0) {
return topPix;
}
/* for squeak: + 1 */
index = ((y + 1) * w) + x;
bottomPix = bitmap[index] & 16777215;
if (xFrac > 0) {
bottomPix = interpolateandfrac(bottomPix, bitmap[index + 1] & 16777215, xFrac);
}
return interpolateandfrac(topPix, bottomPix, yFrac);
}
function primitiveBlur() {
var bTotal;
var dX;
var dY;
var gTotal;
var height;
var in_;
var inOop;
var n;
var out;
var outOop;
var outPix;
var pix;
var rTotal;
var sz;
var width;
var x;
var y;
inOop = interpreterProxy.stackValue(2);
outOop = interpreterProxy.stackValue(1);
width = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
out = checkedUnsignedIntPtrOf(outOop);
sz = SIZEOF(inOop);
interpreterProxy.success(SIZEOF(outOop) === sz);
if (interpreterProxy.failed()) {
return null;
}
height = DIV(sz, width);
for (y = 1; y <= (height - 2); y++) {
for (x = 1; x <= (width - 2); x++) {
n = (rTotal = (gTotal = (bTotal = 0)));
for (dY = -1; dY <= 1; dY++) {
for (dX = -1; dX <= 1; dX++) {
/* add 1 when testing in Squeak */
pix = in_[((y + dY) * width) + (x + dX)] & 16777215;
if (pix !== 0) {
/* skip transparent pixels */
rTotal += (pix >>> 16) & 255;
gTotal += (pix >>> 8) & 255;
bTotal += pix & 255;
++n;
}
}
}
if (n === 0) {
outPix = 0;
} else {
outPix = (((DIV(rTotal, n)) << 16) + ((DIV(gTotal, n)) << 8)) + (DIV(bTotal, n));
}
out[(y * width) + x] = outPix;
}
}
interpreterProxy.pop(3);
return 0;
}
function primitiveBrightnessShift() {
var b;
var brightness;
var g;
var hue;
var i;
var in_;
var inOop;
var max;
var min;
var out;
var outOop;
var pix;
var r;
var saturation;
var shift;
var sz;
inOop = interpreterProxy.stackValue(2);
outOop = interpreterProxy.stackValue(1);
shift = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
sz = SIZEOF(inOop);
out = checkedUnsignedIntPtrOf(outOop);
interpreterProxy.success(SIZEOF(outOop) === sz);
if (interpreterProxy.failed()) {
return null;
}
for (i = 0; i <= (sz - 1); i++) {
pix = in_[i] & 16777215;
if (pix !== 0) {
/* skip pixel values of 0 (transparent) */
r = (pix >>> 16) & 255;
g = (pix >>> 8) & 255;
/* find min and max color components */
b = pix & 255;
max = (min = r);
if (g > max) {
max = g;
}
if (b > max) {
max = b;
}
if (g < min) {
min = g;
}
if (b < min) {
min = b;
}
/* find current saturation and brightness with range 0 to 1000 */
hue = hueFromRGBminmax(r, g, b, min, max);
if (max === 0) {
saturation = 0;
} else {
saturation = DIV(((max - min) * 1000), max);
}
/* compute new brigthness */
brightness = DIV((max * 1000), 255);
brightness += shift * 10;
if (brightness > 1000) {
brightness = 1000;
}
if (brightness < 0) {
brightness = 0;
}
bitmapatputHsv(out, i, hue, saturation, brightness);
}
}
interpreterProxy.pop(3);
return 0;
}
function primitiveCondenseSound() {
var count;
var dst;
var dstOop;
var factor;
var i;
var j;
var max;
var src;
var srcOop;
var sz;
var v;
var _src = 0;
var _dst = 0;
srcOop = interpreterProxy.stackValue(2);
dstOop = interpreterProxy.stackValue(1);
factor = interpreterProxy.stackIntegerValue(0);
interpreterProxy.success(interpreterProxy.isWords(srcOop));
interpreterProxy.success(interpreterProxy.isWords(dstOop));
count = DIV((2 * SIZEOF(srcOop)), factor);
sz = 2 * SIZEOF(dstOop);
interpreterProxy.success(sz >= count);
if (interpreterProxy.failed()) {
return null;
}
src = srcOop.wordsAsInt16Array();
dst = dstOop.wordsAsInt16Array();
for (i = 1; i <= count; i++) {
max = 0;
for (j = 1; j <= factor; j++) {
v = src[_src++];
if (v < 0) {
v = 0 - v;
}
if (v > max) {
max = v;
}
}
dst[_dst++] = max;
}
interpreterProxy.pop(3);
return 0;
}
function primitiveDoubleSize() {
var baseIndex;
var dstX;
var dstY;
var i;
var in_;
var inH;
var inOop;
var inW;
var out;
var outH;
var outOop;
var outW;
var pix;
var x;
var y;
inOop = interpreterProxy.stackValue(7);
inW = interpreterProxy.stackIntegerValue(6);
inH = interpreterProxy.stackIntegerValue(5);
outOop = interpreterProxy.stackValue(4);
outW = interpreterProxy.stackIntegerValue(3);
outH = interpreterProxy.stackIntegerValue(2);
dstX = interpreterProxy.stackIntegerValue(1);
dstY = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
out = checkedUnsignedIntPtrOf(outOop);
interpreterProxy.success((dstX + (2 * inW)) < outW);
interpreterProxy.success((dstY + (2 * inH)) < outH);
if (interpreterProxy.failed()) {
return null;
}
for (y = 0; y <= (inH - 1); y++) {
baseIndex = ((dstY + (2 * y)) * outW) + dstX;
for (x = 0; x <= (inW - 1); x++) {
pix = in_[x + (y * inW)];
i = baseIndex + (2 * x);
out[i] = pix;
out[i + 1] = pix;
out[i + outW] = pix;
out[(i + outW) + 1] = pix;
}
}
interpreterProxy.pop(8);
return 0;
}
function primitiveExtractChannel() {
var dst;
var dstOop;
var i;
var rightFlag;
var src;
var srcOop;
var sz;
var _src = 0;
var _dst = 0;
srcOop = interpreterProxy.stackValue(2);
dstOop = interpreterProxy.stackValue(1);
rightFlag = interpreterProxy.booleanValueOf(interpreterProxy.stackValue(0));
interpreterProxy.success(interpreterProxy.isWords(srcOop));
interpreterProxy.success(interpreterProxy.isWords(dstOop));
sz = SIZEOF(srcOop);
interpreterProxy.success(SIZEOF(dstOop) >= (sz >> 1));
if (interpreterProxy.failed()) {
return null;
}
src = srcOop.wordsAsInt16Array();
dst = dstOop.wordsAsInt16Array();
if (rightFlag) {
_src++;
}
for (i = 1; i <= sz; i++) {
dst[_dst++] = src[_src]; _src += 2;
}
interpreterProxy.pop(3);
return 0;
}
function primitiveFisheye() {
var ang;
var centerX;
var centerY;
var dx;
var dy;
var height;
var in_;
var inOop;
var out;
var outOop;
var pix;
var power;
var r;
var scaledPower;
var srcX;
var srcY;
var sz;
var width;
var x;
var y;
inOop = interpreterProxy.stackValue(3);
outOop = interpreterProxy.stackValue(2);
width = interpreterProxy.stackIntegerValue(1);
power = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
out = checkedUnsignedIntPtrOf(outOop);
sz = SIZEOF(inOop);
interpreterProxy.success(SIZEOF(outOop) === sz);
if (interpreterProxy.failed()) {
return null;
}
height = DIV(sz, width);
centerX = width >> 1;
centerY = height >> 1;
height = DIV(sz, width);
centerX = width >> 1;
centerY = height >> 1;
scaledPower = power / 100.0;
for (x = 0; x <= (width - 1); x++) {
for (y = 0; y <= (height - 1); y++) {
dx = (x - centerX) / centerX;
dy = (y - centerY) / centerY;
r = Math.pow(Math.sqrt((dx * dx) + (dy * dy)),scaledPower);
if (r <= 1.0) {
ang = Math.atan2(dy,dx);
srcX = ((1024 * (centerX + ((r * Math.cos(ang)) * centerX)))|0);
srcY = ((1024 * (centerY + ((r * Math.sin(ang)) * centerY)))|0);
} else {
srcX = 1024 * x;
srcY = 1024 * y;
}
pix = interpolatedFromxywidthheight(in_, srcX, srcY, width, height);
out[(y * width) + x] = pix;
}
}
interpreterProxy.pop(4);
return 0;
}
function primitiveHalfSizeAverage() {
var b;
var dstH;
var dstIndex;
var dstW;
var dstX;
var dstY;
var g;
var in_;
var inH;
var inW;
var out;
var outH;
var outW;
var pixel;
var r;
var srcIndex;
var srcX;
var srcY;
var x;
var y;
in_ = checkedUnsignedIntPtrOf(interpreterProxy.stackValue(11));
inW = interpreterProxy.stackIntegerValue(10);
inH = interpreterProxy.stackIntegerValue(9);
out = checkedUnsignedIntPtrOf(interpreterProxy.stackValue(8));
outW = interpreterProxy.stackIntegerValue(7);
outH = interpreterProxy.stackIntegerValue(6);
srcX = interpreterProxy.stackIntegerValue(5);
srcY = interpreterProxy.stackIntegerValue(4);
dstX = interpreterProxy.stackIntegerValue(3);
dstY = interpreterProxy.stackIntegerValue(2);
dstW = interpreterProxy.stackIntegerValue(1);
dstH = interpreterProxy.stackIntegerValue(0);
interpreterProxy.success((srcX >= 0) && (srcY >= 0));
interpreterProxy.success((srcX + (2 * dstW)) <= inW);
interpreterProxy.success((srcY + (2 * dstH)) <= inH);
interpreterProxy.success((dstX >= 0) && (dstY >= 0));
interpreterProxy.success((dstX + dstW) <= outW);
interpreterProxy.success((dstY + dstH) <= outH);
if (interpreterProxy.failed()) {
return null;
}
for (y = 0; y <= (dstH - 1); y++) {
srcIndex = (inW * (srcY + (2 * y))) + srcX;
dstIndex = (outW * (dstY + y)) + dstX;
for (x = 0; x <= (dstW - 1); x++) {
pixel = in_[srcIndex];
r = pixel & 16711680;
g = pixel & 65280;
b = pixel & 255;
pixel = in_[srcIndex + 1];
r += pixel & 16711680;
g += pixel & 65280;
b += pixel & 255;
pixel = in_[srcIndex + inW];
r += pixel & 16711680;
g += pixel & 65280;
b += pixel & 255;
pixel = in_[(srcIndex + inW) + 1];
r += pixel & 16711680;
g += pixel & 65280;
/* store combined RGB into target bitmap */
b += pixel & 255;
out[dstIndex] = (((r >>> 2) & 16711680) | (((g >>> 2) & 65280) | (b >>> 2)));
srcIndex += 2;
++dstIndex;
}
}
interpreterProxy.pop(12);
return 0;
}
function primitiveHalfSizeDiagonal() {
var b;
var dstH;
var dstIndex;
var dstW;
var dstX;
var dstY;
var g;
var in_;
var inH;
var inW;
var out;
var outH;
var outW;
var p1;
var p2;
var r;
var srcIndex;
var srcX;
var srcY;
var x;
var y;
in_ = checkedUnsignedIntPtrOf(interpreterProxy.stackValue(11));
inW = interpreterProxy.stackIntegerValue(10);
inH = interpreterProxy.stackIntegerValue(9);
out = checkedUnsignedIntPtrOf(interpreterProxy.stackValue(8));
outW = interpreterProxy.stackIntegerValue(7);
outH = interpreterProxy.stackIntegerValue(6);
srcX = interpreterProxy.stackIntegerValue(5);
srcY = interpreterProxy.stackIntegerValue(4);
dstX = interpreterProxy.stackIntegerValue(3);
dstY = interpreterProxy.stackIntegerValue(2);
dstW = interpreterProxy.stackIntegerValue(1);
dstH = interpreterProxy.stackIntegerValue(0);
interpreterProxy.success((srcX >= 0) && (srcY >= 0));
interpreterProxy.success((srcX + (2 * dstW)) <= inW);
interpreterProxy.success((srcY + (2 * dstH)) <= inH);
interpreterProxy.success((dstX >= 0) && (dstY >= 0));
interpreterProxy.success((dstX + dstW) <= outW);
interpreterProxy.success((dstY + dstH) <= outH);
if (interpreterProxy.failed()) {
return null;
}
for (y = 0; y <= (dstH - 1); y++) {
srcIndex = (inW * (srcY + (2 * y))) + srcX;
dstIndex = (outW * (dstY + y)) + dstX;
for (x = 0; x <= (dstW - 1); x++) {
p1 = in_[srcIndex];
p2 = in_[(srcIndex + inW) + 1];
r = (((p1 & 16711680) + (p2 & 16711680)) >>> 1) & 16711680;
g = (((p1 & 65280) + (p2 & 65280)) >>> 1) & 65280;
/* store combined RGB into target bitmap */
b = ((p1 & 255) + (p2 & 255)) >>> 1;
out[dstIndex] = (r | (g | b));
srcIndex += 2;
++dstIndex;
}
}
interpreterProxy.pop(12);
return 0;
}
function primitiveHueShift() {
var b;
var brightness;
var g;
var hue;
var i;
var in_;
var inOop;
var max;
var min;
var out;
var outOop;
var pix;
var r;
var saturation;
var shift;
var sz;
inOop = interpreterProxy.stackValue(2);
outOop = interpreterProxy.stackValue(1);
shift = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
sz = SIZEOF(inOop);
out = checkedUnsignedIntPtrOf(outOop);
interpreterProxy.success(SIZEOF(outOop) === sz);
if (interpreterProxy.failed()) {
return null;
}
for (i = 0; i <= (sz - 1); i++) {
pix = in_[i] & 16777215;
if (pix !== 0) {
/* skip pixel values of 0 (transparent) */
r = (pix >>> 16) & 255;
g = (pix >>> 8) & 255;
/* find min and max color components */
b = pix & 255;
max = (min = r);
if (g > max) {
max = g;
}
if (b > max) {
max = b;
}
if (g < min) {
min = g;
}
if (b < min) {
min = b;
}
brightness = DIV((max * 1000), 255);
if (max === 0) {
saturation = 0;
} else {
saturation = DIV(((max - min) * 1000), max);
}
if (brightness < 110) {
/* force black to a very dark, saturated gray */
brightness = 110;
saturation = 1000;
}
if (saturation < 90) {
saturation = 90;
}
if ((brightness === 110) || (saturation === 90)) {
/* tint all blacks and grays the same */
hue = 0;
} else {
hue = hueFromRGBminmax(r, g, b, min, max);
}
/* compute new hue */
hue = MOD(((hue + shift) + 360000000), 360);
bitmapatputHsv(out, i, hue, saturation, brightness);
}
}
interpreterProxy.pop(3);
return 0;
}
function primitiveInterpolate() {
var in_;
var inOop;
var result;
var sz;
var width;
var xFixed;
var yFixed;
inOop = interpreterProxy.stackValue(3);
width = interpreterProxy.stackIntegerValue(2);
xFixed = interpreterProxy.stackIntegerValue(1);
yFixed = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
sz = SIZEOF(inOop);
if (interpreterProxy.failed()) {
return null;
}
result = interpolatedFromxywidthheight(in_, xFixed, yFixed, width, DIV(sz, width));
interpreterProxy.pop(5);
interpreterProxy.pushInteger(result);
return 0;
}
function primitiveSaturationShift() {
var b;
var brightness;
var g;
var hue;
var i;
var in_;
var inOop;
var max;
var min;
var out;
var outOop;
var pix;
var r;
var saturation;
var shift;
var sz;
inOop = interpreterProxy.stackValue(2);
outOop = interpreterProxy.stackValue(1);
shift = interpreterProxy.stackIntegerValue(0);
in_ = checkedUnsignedIntPtrOf(inOop);
sz = SIZEOF(inOop);
out = checkedUnsignedIntPtrOf(outOop);
interpreterProxy.success(SIZEOF(outOop) === sz);
if (interpreterProxy.failed()) {
return null;
}
for (i = 0; i <= (sz - 1); i++) {
pix = in_[i] & 16777215;
if (!(pix < 2)) {
/* skip pixel values of 0 (transparent) and 1 (black) */
r = (pix >>> 16) & 255;
g = (pix >>> 8) & 255;
/* find min and max color components */
b = pix & 255;
max = (min = r);
if (g > max) {
max = g;
}
if (b > max) {
max = b;
}
if (g < min) {
min = g;
}
if (b < min) {
min = b;
}
brightness = DIV((max * 1000), 255);
if (max === 0) {
saturation = 0;
} else {
saturation = DIV(((max - min) * 1000), max);
}
if (saturation > 0) {
/* do nothing if pixel is unsaturated (gray) */
/* compute new saturation */
hue = hueFromRGBminmax(r, g, b, min, max);
saturation += shift * 10;
if (saturation > 1000) {
saturation = 1000;
}
if (saturation < 0) {
saturation = 0;
}
bitmapatputHsv(out, i, hue, saturation, brightness);
}
}
}
interpreterProxy.pop(3);
return 0;
}
/* Scale using bilinear interpolation. */
function primitiveScale() {
var in_;
var inH;
var inOop;
var inW;
var inX;
var inY;
var out;
var outH;
var outOop;
var outPix;
var outW;
var outX;
var outY;
var p1;
var p2;
var p3;
var p4;
var t;
var tWeight;
var w1;
var w2;
var w3;
var w4;
var xIncr;
var yIncr;
inOop = interpreterProxy.stackValue(5);
inW = interpreterProxy.stackIntegerValue(4);
inH = interpreterProxy.stackIntegerValue(3);
outOop = interpreterProxy.stackValue(2);
outW = interpreterProxy.stackIntegerValue(1);
outH = interpreterProxy.stackIntegerValue(0);
interpreterProxy.success(SIZEOF(inOop) === (inW * inH));
interpreterProxy.success(SIZEOF(outOop) === (outW * outH));
in_ = checkedUnsignedIntPtrOf(inOop);
out = checkedUnsignedIntPtrOf(outOop);
if (interpreterProxy.failed()) {
return null;
}
/* source x and y, scaled by 1024 */
inX = (inY = 0);
/* source x increment, scaled by 1024 */
xIncr = DIV((inW * 1024), outW);
/* source y increment, scaled by 1024 */
yIncr = DIV((inH * 1024), outH);
for (outY = 0; outY <= (outH - 1); outY++) {
inX = 0;
for (outX = 0; outX <= (outW - 1); outX++) {
/* compute weights, scaled by 2^20 */
w1 = (1024 - (inX & 1023)) * (1024 - (inY & 1023));
w2 = (inX & 1023) * (1024 - (inY & 1023));
w3 = (1024 - (inX & 1023)) * (inY & 1023);
/* get source pixels */
w4 = (inX & 1023) * (inY & 1023);
t = ((inY >>> 10) * inW) + (inX >>> 10);
p1 = in_[t];
if ((inX >>> 10) < (inW - 1)) {
p2 = in_[t + 1];
} else {
p2 = p1;
}
if ((inY >>> 10) < (inH - 1)) {
t += inW;
}
p3 = in_[t];
if ((inX >>> 10) < (inW - 1)) {
p4 = in_[t + 1];
} else {
p4 = p3;
}
tWeight = 0;
if (p1 === 0) {
p1 = p2;
tWeight += w1;
}
if (p2 === 0) {
p2 = p1;
tWeight += w2;
}
if (p3 === 0) {
p3 = p4;
tWeight += w3;
}
if (p4 === 0) {
p4 = p3;
tWeight += w4;
}
if (p1 === 0) {
p1 = p3;
p2 = p4;
}
if (p3 === 0) {
p3 = p1;
p4 = p2;
}
outPix = 0;
if (tWeight < 500000) {
/* compute an (opaque) output pixel if less than 50% transparent */