pxt-microbit 7.0.35 → 7.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/built/block-tests.js +1 -1
- package/built/sim.d.ts +87 -0
- package/built/sim.js +1045 -0
- package/built/target-strings.json +1 -1
- package/built/target.js +1 -1
- package/built/target.json +1 -1
- package/built/targetlight.json +1 -1
- package/built/web/rtlsemantic.css +1 -1
- package/built/web/semantic.css +1 -1
- package/docs/reference/music/built-in-melody.md +51 -0
- package/docs/reference/music/built-in-playable-melody.md +50 -0
- package/docs/reference/pins/analog-pin.md +32 -0
- package/docs/reference/pins/digital-pin.md +43 -0
- package/package.json +2 -2
- package/pxtarget.json +9 -0
package/built/sim.js
CHANGED
|
@@ -593,6 +593,1051 @@ var pxsim;
|
|
|
593
593
|
pxsim.AccelerometerState = AccelerometerState;
|
|
594
594
|
})(pxsim || (pxsim = {}));
|
|
595
595
|
var pxsim;
|
|
596
|
+
(function (pxsim) {
|
|
597
|
+
var pxtcore;
|
|
598
|
+
(function (pxtcore) {
|
|
599
|
+
function updateScreen(img) {
|
|
600
|
+
}
|
|
601
|
+
pxtcore.updateScreen = updateScreen;
|
|
602
|
+
function updateStats(s) {
|
|
603
|
+
}
|
|
604
|
+
pxtcore.updateStats = updateStats;
|
|
605
|
+
function setPalette(b) {
|
|
606
|
+
}
|
|
607
|
+
pxtcore.setPalette = setPalette;
|
|
608
|
+
function setScreenBrightness(b) {
|
|
609
|
+
}
|
|
610
|
+
pxtcore.setScreenBrightness = setScreenBrightness;
|
|
611
|
+
function displayHeight() {
|
|
612
|
+
return 120;
|
|
613
|
+
}
|
|
614
|
+
pxtcore.displayHeight = displayHeight;
|
|
615
|
+
function displayWidth() {
|
|
616
|
+
return 160;
|
|
617
|
+
}
|
|
618
|
+
pxtcore.displayWidth = displayWidth;
|
|
619
|
+
function displayPresent() {
|
|
620
|
+
return true;
|
|
621
|
+
}
|
|
622
|
+
pxtcore.displayPresent = displayPresent;
|
|
623
|
+
})(pxtcore = pxsim.pxtcore || (pxsim.pxtcore = {}));
|
|
624
|
+
})(pxsim || (pxsim = {}));
|
|
625
|
+
var pxsim;
|
|
626
|
+
(function (pxsim) {
|
|
627
|
+
class RefImage extends pxsim.RefObject {
|
|
628
|
+
constructor(w, h, bpp) {
|
|
629
|
+
super();
|
|
630
|
+
this.isStatic = true;
|
|
631
|
+
this.revision = 0;
|
|
632
|
+
this.data = new Uint8Array(w * h);
|
|
633
|
+
this._width = w;
|
|
634
|
+
this._height = h;
|
|
635
|
+
this._bpp = bpp;
|
|
636
|
+
}
|
|
637
|
+
scan(mark) { }
|
|
638
|
+
gcKey() { return "Image"; }
|
|
639
|
+
gcSize() { return 4 + (this.data.length + 3 >> 3); }
|
|
640
|
+
gcIsStatic() { return this.isStatic; }
|
|
641
|
+
pix(x, y) {
|
|
642
|
+
return (x | 0) + (y | 0) * this._width;
|
|
643
|
+
}
|
|
644
|
+
inRange(x, y) {
|
|
645
|
+
return 0 <= (x | 0) && (x | 0) < this._width &&
|
|
646
|
+
0 <= (y | 0) && (y | 0) < this._height;
|
|
647
|
+
}
|
|
648
|
+
color(c) {
|
|
649
|
+
return c & 0xff;
|
|
650
|
+
}
|
|
651
|
+
clamp(x, y) {
|
|
652
|
+
x |= 0;
|
|
653
|
+
y |= 0;
|
|
654
|
+
if (x < 0)
|
|
655
|
+
x = 0;
|
|
656
|
+
else if (x >= this._width)
|
|
657
|
+
x = this._width - 1;
|
|
658
|
+
if (y < 0)
|
|
659
|
+
y = 0;
|
|
660
|
+
else if (y >= this._height)
|
|
661
|
+
y = this._height - 1;
|
|
662
|
+
return [x, y];
|
|
663
|
+
}
|
|
664
|
+
makeWritable() {
|
|
665
|
+
this.revision++;
|
|
666
|
+
this.isStatic = false;
|
|
667
|
+
}
|
|
668
|
+
toDebugString() {
|
|
669
|
+
return this._width + "x" + this._height;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
pxsim.RefImage = RefImage;
|
|
673
|
+
})(pxsim || (pxsim = {}));
|
|
674
|
+
(function (pxsim) {
|
|
675
|
+
var BitmapMethods;
|
|
676
|
+
(function (BitmapMethods) {
|
|
677
|
+
function XX(x) { return (x << 16) >> 16; }
|
|
678
|
+
BitmapMethods.XX = XX;
|
|
679
|
+
function YY(x) { return x >> 16; }
|
|
680
|
+
BitmapMethods.YY = YY;
|
|
681
|
+
function __buffer(img) {
|
|
682
|
+
return new pxsim.RefBuffer(img.data); // no clone for now
|
|
683
|
+
}
|
|
684
|
+
BitmapMethods.__buffer = __buffer;
|
|
685
|
+
function width(img) { return img._width; }
|
|
686
|
+
BitmapMethods.width = width;
|
|
687
|
+
function height(img) { return img._height; }
|
|
688
|
+
BitmapMethods.height = height;
|
|
689
|
+
function isMono(img) { return img._bpp == 1; }
|
|
690
|
+
BitmapMethods.isMono = isMono;
|
|
691
|
+
function isStatic(img) { return img.gcIsStatic(); }
|
|
692
|
+
BitmapMethods.isStatic = isStatic;
|
|
693
|
+
function revision(img) { return img.revision; }
|
|
694
|
+
BitmapMethods.revision = revision;
|
|
695
|
+
function setPixel(img, x, y, c) {
|
|
696
|
+
img.makeWritable();
|
|
697
|
+
if (img.inRange(x, y))
|
|
698
|
+
img.data[img.pix(x, y)] = img.color(c);
|
|
699
|
+
}
|
|
700
|
+
BitmapMethods.setPixel = setPixel;
|
|
701
|
+
function getPixel(img, x, y) {
|
|
702
|
+
if (img.inRange(x, y))
|
|
703
|
+
return img.data[img.pix(x, y)];
|
|
704
|
+
return 0;
|
|
705
|
+
}
|
|
706
|
+
BitmapMethods.getPixel = getPixel;
|
|
707
|
+
function fill(img, c) {
|
|
708
|
+
img.makeWritable();
|
|
709
|
+
img.data.fill(img.color(c));
|
|
710
|
+
}
|
|
711
|
+
BitmapMethods.fill = fill;
|
|
712
|
+
function fillRect(img, x, y, w, h, c) {
|
|
713
|
+
if (w == 0 || h == 0 || x >= img._width || y >= img._height || x + w - 1 < 0 || y + h - 1 < 0)
|
|
714
|
+
return;
|
|
715
|
+
img.makeWritable();
|
|
716
|
+
let [x2, y2] = img.clamp(x + w - 1, y + h - 1);
|
|
717
|
+
[x, y] = img.clamp(x, y);
|
|
718
|
+
let p = img.pix(x, y);
|
|
719
|
+
w = x2 - x + 1;
|
|
720
|
+
h = y2 - y + 1;
|
|
721
|
+
let d = img._width - w;
|
|
722
|
+
c = img.color(c);
|
|
723
|
+
while (h-- > 0) {
|
|
724
|
+
for (let i = 0; i < w; ++i)
|
|
725
|
+
img.data[p++] = c;
|
|
726
|
+
p += d;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
BitmapMethods.fillRect = fillRect;
|
|
730
|
+
function _fillRect(img, xy, wh, c) {
|
|
731
|
+
fillRect(img, XX(xy), YY(xy), XX(wh), YY(wh), c);
|
|
732
|
+
}
|
|
733
|
+
BitmapMethods._fillRect = _fillRect;
|
|
734
|
+
function mapRect(img, x, y, w, h, c) {
|
|
735
|
+
if (c.data.length < 16)
|
|
736
|
+
return;
|
|
737
|
+
img.makeWritable();
|
|
738
|
+
let [x2, y2] = img.clamp(x + w - 1, y + h - 1);
|
|
739
|
+
[x, y] = img.clamp(x, y);
|
|
740
|
+
let p = img.pix(x, y);
|
|
741
|
+
w = x2 - x + 1;
|
|
742
|
+
h = y2 - y + 1;
|
|
743
|
+
let d = img._width - w;
|
|
744
|
+
while (h-- > 0) {
|
|
745
|
+
for (let i = 0; i < w; ++i) {
|
|
746
|
+
img.data[p] = c.data[img.data[p]];
|
|
747
|
+
p++;
|
|
748
|
+
}
|
|
749
|
+
p += d;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
BitmapMethods.mapRect = mapRect;
|
|
753
|
+
function _mapRect(img, xy, wh, c) {
|
|
754
|
+
mapRect(img, XX(xy), YY(xy), XX(wh), YY(wh), c);
|
|
755
|
+
}
|
|
756
|
+
BitmapMethods._mapRect = _mapRect;
|
|
757
|
+
function equals(img, other) {
|
|
758
|
+
if (!other || img._bpp != other._bpp || img._width != other._width || img._height != other._height) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
let imgData = img.data;
|
|
762
|
+
let otherData = other.data;
|
|
763
|
+
let len = imgData.length;
|
|
764
|
+
for (let i = 0; i < len; i++) {
|
|
765
|
+
if (imgData[i] != otherData[i]) {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
BitmapMethods.equals = equals;
|
|
772
|
+
function getRows(img, x, dst) {
|
|
773
|
+
x |= 0;
|
|
774
|
+
if (!img.inRange(x, 0))
|
|
775
|
+
return;
|
|
776
|
+
let dp = 0;
|
|
777
|
+
let len = Math.min(dst.data.length, (img._width - x) * img._height);
|
|
778
|
+
let sp = x;
|
|
779
|
+
let hh = 0;
|
|
780
|
+
while (len--) {
|
|
781
|
+
if (hh++ >= img._height) {
|
|
782
|
+
hh = 1;
|
|
783
|
+
sp = ++x;
|
|
784
|
+
}
|
|
785
|
+
dst.data[dp++] = img.data[sp];
|
|
786
|
+
sp += img._width;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
BitmapMethods.getRows = getRows;
|
|
790
|
+
function setRows(img, x, src) {
|
|
791
|
+
x |= 0;
|
|
792
|
+
if (!img.inRange(x, 0))
|
|
793
|
+
return;
|
|
794
|
+
let sp = 0;
|
|
795
|
+
let len = Math.min(src.data.length, (img._width - x) * img._height);
|
|
796
|
+
let dp = x;
|
|
797
|
+
let hh = 0;
|
|
798
|
+
while (len--) {
|
|
799
|
+
if (hh++ >= img._height) {
|
|
800
|
+
hh = 1;
|
|
801
|
+
dp = ++x;
|
|
802
|
+
}
|
|
803
|
+
img.data[dp] = src.data[sp++];
|
|
804
|
+
dp += img._width;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
BitmapMethods.setRows = setRows;
|
|
808
|
+
function clone(img) {
|
|
809
|
+
let r = new pxsim.RefImage(img._width, img._height, img._bpp);
|
|
810
|
+
r.data.set(img.data);
|
|
811
|
+
return r;
|
|
812
|
+
}
|
|
813
|
+
BitmapMethods.clone = clone;
|
|
814
|
+
function flipX(img) {
|
|
815
|
+
img.makeWritable();
|
|
816
|
+
const w = img._width;
|
|
817
|
+
const h = img._height;
|
|
818
|
+
for (let i = 0; i < h; ++i) {
|
|
819
|
+
img.data.subarray(i * w, (i + 1) * w).reverse();
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
BitmapMethods.flipX = flipX;
|
|
823
|
+
function flipY(img) {
|
|
824
|
+
img.makeWritable();
|
|
825
|
+
const w = img._width;
|
|
826
|
+
const h = img._height;
|
|
827
|
+
const d = img.data;
|
|
828
|
+
for (let i = 0; i < w; ++i) {
|
|
829
|
+
let top = i;
|
|
830
|
+
let bot = i + (h - 1) * w;
|
|
831
|
+
while (top < bot) {
|
|
832
|
+
let c = d[top];
|
|
833
|
+
d[top] = d[bot];
|
|
834
|
+
d[bot] = c;
|
|
835
|
+
top += w;
|
|
836
|
+
bot -= w;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
BitmapMethods.flipY = flipY;
|
|
841
|
+
function transposed(img) {
|
|
842
|
+
const w = img._width;
|
|
843
|
+
const h = img._height;
|
|
844
|
+
const d = img.data;
|
|
845
|
+
const r = new pxsim.RefImage(h, w, img._bpp);
|
|
846
|
+
const n = r.data;
|
|
847
|
+
let src = 0;
|
|
848
|
+
for (let i = 0; i < h; ++i) {
|
|
849
|
+
let dst = i;
|
|
850
|
+
for (let j = 0; j < w; ++j) {
|
|
851
|
+
n[dst] = d[src++];
|
|
852
|
+
dst += w;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return r;
|
|
856
|
+
}
|
|
857
|
+
BitmapMethods.transposed = transposed;
|
|
858
|
+
function copyFrom(img, from) {
|
|
859
|
+
if (img._width != from._width || img._height != from._height ||
|
|
860
|
+
img._bpp != from._bpp)
|
|
861
|
+
return;
|
|
862
|
+
img.data.set(from.data);
|
|
863
|
+
}
|
|
864
|
+
BitmapMethods.copyFrom = copyFrom;
|
|
865
|
+
function scroll(img, dx, dy) {
|
|
866
|
+
img.makeWritable();
|
|
867
|
+
dx |= 0;
|
|
868
|
+
dy |= 0;
|
|
869
|
+
if (dx != 0) {
|
|
870
|
+
const img2 = clone(img);
|
|
871
|
+
img.data.fill(0);
|
|
872
|
+
drawTransparentBitmap(img, img2, dx, dy);
|
|
873
|
+
}
|
|
874
|
+
else if (dy < 0) {
|
|
875
|
+
dy = -dy;
|
|
876
|
+
if (dy < img._height)
|
|
877
|
+
img.data.copyWithin(0, dy * img._width);
|
|
878
|
+
else
|
|
879
|
+
dy = img._height;
|
|
880
|
+
img.data.fill(0, (img._height - dy) * img._width);
|
|
881
|
+
}
|
|
882
|
+
else if (dy > 0) {
|
|
883
|
+
if (dy < img._height)
|
|
884
|
+
img.data.copyWithin(dy * img._width, 0);
|
|
885
|
+
else
|
|
886
|
+
dy = img._height;
|
|
887
|
+
img.data.fill(0, 0, dy * img._width);
|
|
888
|
+
}
|
|
889
|
+
// TODO implement dx
|
|
890
|
+
}
|
|
891
|
+
BitmapMethods.scroll = scroll;
|
|
892
|
+
function replace(img, from, to) {
|
|
893
|
+
to &= 0xf;
|
|
894
|
+
const d = img.data;
|
|
895
|
+
for (let i = 0; i < d.length; ++i)
|
|
896
|
+
if (d[i] == from)
|
|
897
|
+
d[i] = to;
|
|
898
|
+
}
|
|
899
|
+
BitmapMethods.replace = replace;
|
|
900
|
+
function doubledX(img) {
|
|
901
|
+
const w = img._width;
|
|
902
|
+
const h = img._height;
|
|
903
|
+
const d = img.data;
|
|
904
|
+
const r = new pxsim.RefImage(w * 2, h, img._bpp);
|
|
905
|
+
const n = r.data;
|
|
906
|
+
let dst = 0;
|
|
907
|
+
for (let src = 0; src < d.length; ++src) {
|
|
908
|
+
let c = d[src];
|
|
909
|
+
n[dst++] = c;
|
|
910
|
+
n[dst++] = c;
|
|
911
|
+
}
|
|
912
|
+
return r;
|
|
913
|
+
}
|
|
914
|
+
BitmapMethods.doubledX = doubledX;
|
|
915
|
+
function doubledY(img) {
|
|
916
|
+
const w = img._width;
|
|
917
|
+
const h = img._height;
|
|
918
|
+
const d = img.data;
|
|
919
|
+
const r = new pxsim.RefImage(w, h * 2, img._bpp);
|
|
920
|
+
const n = r.data;
|
|
921
|
+
let src = 0;
|
|
922
|
+
let dst0 = 0;
|
|
923
|
+
let dst1 = w;
|
|
924
|
+
for (let i = 0; i < h; ++i) {
|
|
925
|
+
for (let j = 0; j < w; ++j) {
|
|
926
|
+
let c = d[src++];
|
|
927
|
+
n[dst0++] = c;
|
|
928
|
+
n[dst1++] = c;
|
|
929
|
+
}
|
|
930
|
+
dst0 += w;
|
|
931
|
+
dst1 += w;
|
|
932
|
+
}
|
|
933
|
+
return r;
|
|
934
|
+
}
|
|
935
|
+
BitmapMethods.doubledY = doubledY;
|
|
936
|
+
function doubled(img) {
|
|
937
|
+
return doubledX(doubledY(img));
|
|
938
|
+
}
|
|
939
|
+
BitmapMethods.doubled = doubled;
|
|
940
|
+
function drawImageCore(img, from, x, y, clear, check) {
|
|
941
|
+
x |= 0;
|
|
942
|
+
y |= 0;
|
|
943
|
+
const w = from._width;
|
|
944
|
+
let h = from._height;
|
|
945
|
+
const sh = img._height;
|
|
946
|
+
const sw = img._width;
|
|
947
|
+
if (x + w <= 0)
|
|
948
|
+
return false;
|
|
949
|
+
if (x >= sw)
|
|
950
|
+
return false;
|
|
951
|
+
if (y + h <= 0)
|
|
952
|
+
return false;
|
|
953
|
+
if (y >= sh)
|
|
954
|
+
return false;
|
|
955
|
+
if (clear)
|
|
956
|
+
fillRect(img, x, y, from._width, from._height, 0);
|
|
957
|
+
else if (!check)
|
|
958
|
+
img.makeWritable();
|
|
959
|
+
const len = x < 0 ? Math.min(sw, w + x) : Math.min(sw - x, w);
|
|
960
|
+
const fdata = from.data;
|
|
961
|
+
const tdata = img.data;
|
|
962
|
+
for (let p = 0; h--; y++, p += w) {
|
|
963
|
+
if (0 <= y && y < sh) {
|
|
964
|
+
let dst = y * sw;
|
|
965
|
+
let src = p;
|
|
966
|
+
if (x < 0)
|
|
967
|
+
src += -x;
|
|
968
|
+
else
|
|
969
|
+
dst += x;
|
|
970
|
+
for (let i = 0; i < len; ++i) {
|
|
971
|
+
const v = fdata[src++];
|
|
972
|
+
if (v) {
|
|
973
|
+
if (check) {
|
|
974
|
+
if (tdata[dst])
|
|
975
|
+
return true;
|
|
976
|
+
}
|
|
977
|
+
else {
|
|
978
|
+
tdata[dst] = v;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
dst++;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
return false;
|
|
986
|
+
}
|
|
987
|
+
function drawBitmap(img, from, x, y) {
|
|
988
|
+
drawImageCore(img, from, x, y, true, false);
|
|
989
|
+
}
|
|
990
|
+
BitmapMethods.drawBitmap = drawBitmap;
|
|
991
|
+
function drawTransparentBitmap(img, from, x, y) {
|
|
992
|
+
drawImageCore(img, from, x, y, false, false);
|
|
993
|
+
}
|
|
994
|
+
BitmapMethods.drawTransparentBitmap = drawTransparentBitmap;
|
|
995
|
+
function overlapsWith(img, other, x, y) {
|
|
996
|
+
return drawImageCore(img, other, x, y, false, true);
|
|
997
|
+
}
|
|
998
|
+
BitmapMethods.overlapsWith = overlapsWith;
|
|
999
|
+
function drawLineLow(img, x0, y0, x1, y1, c) {
|
|
1000
|
+
let dx = x1 - x0;
|
|
1001
|
+
let dy = y1 - y0;
|
|
1002
|
+
let yi = img._width;
|
|
1003
|
+
if (dy < 0) {
|
|
1004
|
+
yi = -yi;
|
|
1005
|
+
dy = -dy;
|
|
1006
|
+
}
|
|
1007
|
+
let D = 2 * dy - dx;
|
|
1008
|
+
dx <<= 1;
|
|
1009
|
+
dy <<= 1;
|
|
1010
|
+
c = img.color(c);
|
|
1011
|
+
let ptr = img.pix(x0, y0);
|
|
1012
|
+
for (let x = x0; x <= x1; ++x) {
|
|
1013
|
+
img.data[ptr] = c;
|
|
1014
|
+
if (D > 0) {
|
|
1015
|
+
ptr += yi;
|
|
1016
|
+
D -= dx;
|
|
1017
|
+
}
|
|
1018
|
+
D += dy;
|
|
1019
|
+
ptr++;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
function drawLineHigh(img, x0, y0, x1, y1, c) {
|
|
1023
|
+
let dx = x1 - x0;
|
|
1024
|
+
let dy = y1 - y0;
|
|
1025
|
+
let xi = 1;
|
|
1026
|
+
if (dx < 0) {
|
|
1027
|
+
xi = -1;
|
|
1028
|
+
dx = -dx;
|
|
1029
|
+
}
|
|
1030
|
+
let D = 2 * dx - dy;
|
|
1031
|
+
dx <<= 1;
|
|
1032
|
+
dy <<= 1;
|
|
1033
|
+
c = img.color(c);
|
|
1034
|
+
let ptr = img.pix(x0, y0);
|
|
1035
|
+
for (let y = y0; y <= y1; ++y) {
|
|
1036
|
+
img.data[ptr] = c;
|
|
1037
|
+
if (D > 0) {
|
|
1038
|
+
ptr += xi;
|
|
1039
|
+
D -= dy;
|
|
1040
|
+
}
|
|
1041
|
+
D += dx;
|
|
1042
|
+
ptr += img._width;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
function _drawLine(img, xy, wh, c) {
|
|
1046
|
+
drawLine(img, XX(xy), YY(xy), XX(wh), YY(wh), c);
|
|
1047
|
+
}
|
|
1048
|
+
BitmapMethods._drawLine = _drawLine;
|
|
1049
|
+
function drawLine(img, x0, y0, x1, y1, c) {
|
|
1050
|
+
x0 |= 0;
|
|
1051
|
+
y0 |= 0;
|
|
1052
|
+
x1 |= 0;
|
|
1053
|
+
y1 |= 0;
|
|
1054
|
+
if (x1 < x0) {
|
|
1055
|
+
drawLine(img, x1, y1, x0, y0, c);
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
let w = x1 - x0;
|
|
1059
|
+
let h = y1 - y0;
|
|
1060
|
+
if (h == 0) {
|
|
1061
|
+
if (w == 0)
|
|
1062
|
+
setPixel(img, x0, y0, c);
|
|
1063
|
+
else
|
|
1064
|
+
fillRect(img, x0, y0, w + 1, 1, c);
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
if (w == 0) {
|
|
1068
|
+
if (h > 0)
|
|
1069
|
+
fillRect(img, x0, y0, 1, h + 1, c);
|
|
1070
|
+
else
|
|
1071
|
+
fillRect(img, x0, y1, 1, -h + 1, c);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
if (x1 < 0 || x0 >= img._width)
|
|
1075
|
+
return;
|
|
1076
|
+
if (x0 < 0) {
|
|
1077
|
+
y0 -= (h * x0 / w) | 0;
|
|
1078
|
+
x0 = 0;
|
|
1079
|
+
}
|
|
1080
|
+
if (x1 >= img._width) {
|
|
1081
|
+
let d = (img._width - 1) - x1;
|
|
1082
|
+
y1 += (h * d / w) | 0;
|
|
1083
|
+
x1 = img._width - 1;
|
|
1084
|
+
}
|
|
1085
|
+
if (y0 < y1) {
|
|
1086
|
+
if (y0 >= img._height || y1 < 0)
|
|
1087
|
+
return;
|
|
1088
|
+
if (y0 < 0) {
|
|
1089
|
+
x0 -= (w * y0 / h) | 0;
|
|
1090
|
+
y0 = 0;
|
|
1091
|
+
}
|
|
1092
|
+
if (y1 >= img._height) {
|
|
1093
|
+
let d = (img._height - 1) - y1;
|
|
1094
|
+
x1 += (w * d / h) | 0;
|
|
1095
|
+
y1 = img._height;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
if (y1 >= img._height || y0 < 0)
|
|
1100
|
+
return;
|
|
1101
|
+
if (y1 < 0) {
|
|
1102
|
+
x1 -= (w * y1 / h) | 0;
|
|
1103
|
+
y1 = 0;
|
|
1104
|
+
}
|
|
1105
|
+
if (y0 >= img._height) {
|
|
1106
|
+
let d = (img._height - 1) - y0;
|
|
1107
|
+
x0 += (w * d / h) | 0;
|
|
1108
|
+
y0 = img._height;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
img.makeWritable();
|
|
1112
|
+
if (h < 0) {
|
|
1113
|
+
h = -h;
|
|
1114
|
+
if (h < w)
|
|
1115
|
+
drawLineLow(img, x0, y0, x1, y1, c);
|
|
1116
|
+
else
|
|
1117
|
+
drawLineHigh(img, x1, y1, x0, y0, c);
|
|
1118
|
+
}
|
|
1119
|
+
else {
|
|
1120
|
+
if (h < w)
|
|
1121
|
+
drawLineLow(img, x0, y0, x1, y1, c);
|
|
1122
|
+
else
|
|
1123
|
+
drawLineHigh(img, x0, y0, x1, y1, c);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
BitmapMethods.drawLine = drawLine;
|
|
1127
|
+
function drawIcon(img, icon, x, y, color) {
|
|
1128
|
+
const src = icon.data;
|
|
1129
|
+
if (!pxsim.bitmap.isValidImage(icon))
|
|
1130
|
+
return;
|
|
1131
|
+
if (src[1] != 1)
|
|
1132
|
+
return; // only mono
|
|
1133
|
+
let width = pxsim.bitmap.bufW(src);
|
|
1134
|
+
let height = pxsim.bitmap.bufH(src);
|
|
1135
|
+
let byteH = pxsim.bitmap.byteHeight(height, 1);
|
|
1136
|
+
x |= 0;
|
|
1137
|
+
y |= 0;
|
|
1138
|
+
const destHeight = img._height;
|
|
1139
|
+
const destWidth = img._width;
|
|
1140
|
+
if (x + width <= 0)
|
|
1141
|
+
return;
|
|
1142
|
+
if (x >= destWidth)
|
|
1143
|
+
return;
|
|
1144
|
+
if (y + height <= 0)
|
|
1145
|
+
return;
|
|
1146
|
+
if (y >= destHeight)
|
|
1147
|
+
return;
|
|
1148
|
+
img.makeWritable();
|
|
1149
|
+
let srcPointer = 8;
|
|
1150
|
+
color = img.color(color);
|
|
1151
|
+
const screen = img.data;
|
|
1152
|
+
for (let i = 0; i < width; ++i) {
|
|
1153
|
+
let destX = x + i;
|
|
1154
|
+
if (0 <= destX && destX < destWidth) {
|
|
1155
|
+
let destIndex = destX + y * destWidth;
|
|
1156
|
+
let srcIndex = srcPointer;
|
|
1157
|
+
let destY = y;
|
|
1158
|
+
let destEnd = Math.min(destHeight, height + y);
|
|
1159
|
+
if (y < 0) {
|
|
1160
|
+
srcIndex += ((-y) >> 3);
|
|
1161
|
+
destY += ((-y) >> 3) * 8;
|
|
1162
|
+
destIndex += (destY - y) * destWidth;
|
|
1163
|
+
}
|
|
1164
|
+
let mask = 0x01;
|
|
1165
|
+
let srcByte = src[srcIndex++];
|
|
1166
|
+
while (destY < destEnd) {
|
|
1167
|
+
if (destY >= 0 && (srcByte & mask)) {
|
|
1168
|
+
screen[destIndex] = color;
|
|
1169
|
+
}
|
|
1170
|
+
mask <<= 1;
|
|
1171
|
+
if (mask == 0x100) {
|
|
1172
|
+
mask = 0x01;
|
|
1173
|
+
srcByte = src[srcIndex++];
|
|
1174
|
+
}
|
|
1175
|
+
destIndex += destWidth;
|
|
1176
|
+
destY++;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
srcPointer += byteH;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
BitmapMethods.drawIcon = drawIcon;
|
|
1183
|
+
function _drawIcon(img, icon, xy, color) {
|
|
1184
|
+
drawIcon(img, icon, XX(xy), YY(xy), color);
|
|
1185
|
+
}
|
|
1186
|
+
BitmapMethods._drawIcon = _drawIcon;
|
|
1187
|
+
function fillCircle(img, cx, cy, r, c) {
|
|
1188
|
+
let x = r - 1;
|
|
1189
|
+
let y = 0;
|
|
1190
|
+
let dx = 1;
|
|
1191
|
+
let dy = 1;
|
|
1192
|
+
let err = dx - (r << 1);
|
|
1193
|
+
while (x >= y) {
|
|
1194
|
+
fillRect(img, cx + x, cy - y, 1, 1 + (y << 1), c);
|
|
1195
|
+
fillRect(img, cx + y, cy - x, 1, 1 + (x << 1), c);
|
|
1196
|
+
fillRect(img, cx - x, cy - y, 1, 1 + (y << 1), c);
|
|
1197
|
+
fillRect(img, cx - y, cy - x, 1, 1 + (x << 1), c);
|
|
1198
|
+
if (err <= 0) {
|
|
1199
|
+
y++;
|
|
1200
|
+
err += dy;
|
|
1201
|
+
dy += 2;
|
|
1202
|
+
}
|
|
1203
|
+
if (err > 0) {
|
|
1204
|
+
x--;
|
|
1205
|
+
dx += 2;
|
|
1206
|
+
err += dx - (r << 1);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
BitmapMethods.fillCircle = fillCircle;
|
|
1211
|
+
function _fillCircle(img, cxy, r, c) {
|
|
1212
|
+
fillCircle(img, XX(cxy), YY(cxy), r, c);
|
|
1213
|
+
}
|
|
1214
|
+
BitmapMethods._fillCircle = _fillCircle;
|
|
1215
|
+
function nextYRange_Low(x, line, yRange) {
|
|
1216
|
+
while (line.x === x && line.x <= line.x1 && line.x < line.W) {
|
|
1217
|
+
if (0 <= line.x) {
|
|
1218
|
+
if (line.y < yRange.min)
|
|
1219
|
+
yRange.min = line.y;
|
|
1220
|
+
if (line.y > yRange.max)
|
|
1221
|
+
yRange.max = line.y;
|
|
1222
|
+
}
|
|
1223
|
+
if (line.D > 0) {
|
|
1224
|
+
line.y += line.yi;
|
|
1225
|
+
line.D -= line.dx;
|
|
1226
|
+
}
|
|
1227
|
+
line.D += line.dy;
|
|
1228
|
+
++line.x;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
function nextYRange_HighUp(x, line, yRange) {
|
|
1232
|
+
while (line.x == x && line.y >= line.y1 && line.x < line.W) {
|
|
1233
|
+
if (0 <= line.x) {
|
|
1234
|
+
if (line.y < yRange.min)
|
|
1235
|
+
yRange.min = line.y;
|
|
1236
|
+
if (line.y > yRange.max)
|
|
1237
|
+
yRange.max = line.y;
|
|
1238
|
+
}
|
|
1239
|
+
if (line.D > 0) {
|
|
1240
|
+
line.x += line.xi;
|
|
1241
|
+
line.D += line.dy;
|
|
1242
|
+
}
|
|
1243
|
+
line.D += line.dx;
|
|
1244
|
+
--line.y;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
function nextYRange_HighDown(x, line, yRange) {
|
|
1248
|
+
while (line.x == x && line.y <= line.y1 && line.x < line.W) {
|
|
1249
|
+
if (0 <= line.x) {
|
|
1250
|
+
if (line.y < yRange.min)
|
|
1251
|
+
yRange.min = line.y;
|
|
1252
|
+
if (line.y > yRange.max)
|
|
1253
|
+
yRange.max = line.y;
|
|
1254
|
+
}
|
|
1255
|
+
if (line.D > 0) {
|
|
1256
|
+
line.x += line.xi;
|
|
1257
|
+
line.D -= line.dy;
|
|
1258
|
+
}
|
|
1259
|
+
line.D += line.dx;
|
|
1260
|
+
++line.y;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
function initYRangeGenerator(X0, Y0, X1, Y1) {
|
|
1264
|
+
const line = {
|
|
1265
|
+
x: X0,
|
|
1266
|
+
y: Y0,
|
|
1267
|
+
x0: X0,
|
|
1268
|
+
y0: Y0,
|
|
1269
|
+
x1: X1,
|
|
1270
|
+
y1: Y1,
|
|
1271
|
+
W: 0,
|
|
1272
|
+
H: 0,
|
|
1273
|
+
dx: X1 - X0,
|
|
1274
|
+
dy: Y1 - Y0,
|
|
1275
|
+
yi: 0,
|
|
1276
|
+
xi: 0,
|
|
1277
|
+
D: 0,
|
|
1278
|
+
nextFuncIndex: 0,
|
|
1279
|
+
};
|
|
1280
|
+
if ((line.dy < 0 ? -line.dy : line.dy) < line.dx) {
|
|
1281
|
+
line.yi = 1;
|
|
1282
|
+
if (line.dy < 0) {
|
|
1283
|
+
line.yi = -1;
|
|
1284
|
+
line.dy = -line.dy;
|
|
1285
|
+
}
|
|
1286
|
+
line.D = 2 * line.dy - line.dx;
|
|
1287
|
+
line.dx = line.dx << 1;
|
|
1288
|
+
line.dy = line.dy << 1;
|
|
1289
|
+
line.nextFuncIndex = 0;
|
|
1290
|
+
return line;
|
|
1291
|
+
}
|
|
1292
|
+
else {
|
|
1293
|
+
line.xi = 1;
|
|
1294
|
+
if (line.dy < 0) {
|
|
1295
|
+
line.D = 2 * line.dx + line.dy;
|
|
1296
|
+
line.dx = line.dx << 1;
|
|
1297
|
+
line.dy = line.dy << 1;
|
|
1298
|
+
line.nextFuncIndex = 1;
|
|
1299
|
+
return line;
|
|
1300
|
+
}
|
|
1301
|
+
else {
|
|
1302
|
+
line.D = 2 * line.dx - line.dy;
|
|
1303
|
+
line.dx = line.dx << 1;
|
|
1304
|
+
line.dy = line.dy << 1;
|
|
1305
|
+
line.nextFuncIndex = 2;
|
|
1306
|
+
return line;
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
function fillTriangle(img, x0, y0, x1, y1, x2, y2, c) {
|
|
1311
|
+
if (x1 < x0) {
|
|
1312
|
+
[x1, x0] = [x0, x1];
|
|
1313
|
+
[y1, y0] = [y0, y1];
|
|
1314
|
+
}
|
|
1315
|
+
if (x2 < x1) {
|
|
1316
|
+
[x2, x1] = [x1, x2];
|
|
1317
|
+
[y2, y1] = [y1, y2];
|
|
1318
|
+
}
|
|
1319
|
+
if (x1 < x0) {
|
|
1320
|
+
[x1, x0] = [x0, x1];
|
|
1321
|
+
[y1, y0] = [y0, y1];
|
|
1322
|
+
}
|
|
1323
|
+
const lines = [
|
|
1324
|
+
initYRangeGenerator(x0, y0, x2, y2),
|
|
1325
|
+
initYRangeGenerator(x0, y0, x1, y1),
|
|
1326
|
+
initYRangeGenerator(x1, y1, x2, y2)
|
|
1327
|
+
];
|
|
1328
|
+
lines[0].W = lines[1].W = lines[2].W = width(img);
|
|
1329
|
+
lines[0].H = lines[1].H = lines[2].H = height(img);
|
|
1330
|
+
const nextFuncList = [
|
|
1331
|
+
nextYRange_Low,
|
|
1332
|
+
nextYRange_HighUp,
|
|
1333
|
+
nextYRange_HighDown
|
|
1334
|
+
];
|
|
1335
|
+
const fpNext0 = nextFuncList[lines[0].nextFuncIndex];
|
|
1336
|
+
const fpNext1 = nextFuncList[lines[1].nextFuncIndex];
|
|
1337
|
+
const fpNext2 = nextFuncList[lines[2].nextFuncIndex];
|
|
1338
|
+
const yRange = {
|
|
1339
|
+
min: lines[0].H,
|
|
1340
|
+
max: -1
|
|
1341
|
+
};
|
|
1342
|
+
for (let x = lines[1].x0; x <= lines[1].x1; x++) {
|
|
1343
|
+
yRange.min = lines[0].H;
|
|
1344
|
+
yRange.max = -1;
|
|
1345
|
+
fpNext0(x, lines[0], yRange);
|
|
1346
|
+
fpNext1(x, lines[1], yRange);
|
|
1347
|
+
fillRect(img, x, yRange.min, 1, yRange.max - yRange.min + 1, c);
|
|
1348
|
+
}
|
|
1349
|
+
fpNext2(lines[2].x0, lines[2], yRange);
|
|
1350
|
+
for (let x = lines[2].x0 + 1; x <= lines[2].x1; x++) {
|
|
1351
|
+
yRange.min = lines[0].H;
|
|
1352
|
+
yRange.max = -1;
|
|
1353
|
+
fpNext0(x, lines[0], yRange);
|
|
1354
|
+
fpNext2(x, lines[2], yRange);
|
|
1355
|
+
fillRect(img, x, yRange.min, 1, yRange.max - yRange.min + 1, c);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
BitmapMethods.fillTriangle = fillTriangle;
|
|
1359
|
+
function _fillTriangle(img, args) {
|
|
1360
|
+
fillTriangle(img, args.getAt(0) | 0, args.getAt(1) | 0, args.getAt(2) | 0, args.getAt(3) | 0, args.getAt(4) | 0, args.getAt(5) | 0, args.getAt(6) | 0);
|
|
1361
|
+
}
|
|
1362
|
+
BitmapMethods._fillTriangle = _fillTriangle;
|
|
1363
|
+
function fillPolygon4(img, x0, y0, x1, y1, x2, y2, x3, y3, c) {
|
|
1364
|
+
const lines = [
|
|
1365
|
+
(x0 < x1) ? initYRangeGenerator(x0, y0, x1, y1) : initYRangeGenerator(x1, y1, x0, y0),
|
|
1366
|
+
(x1 < x2) ? initYRangeGenerator(x1, y1, x2, y2) : initYRangeGenerator(x2, y2, x1, y1),
|
|
1367
|
+
(x2 < x3) ? initYRangeGenerator(x2, y2, x3, y3) : initYRangeGenerator(x3, y3, x2, y2),
|
|
1368
|
+
(x0 < x3) ? initYRangeGenerator(x0, y0, x3, y3) : initYRangeGenerator(x3, y3, x0, y0)
|
|
1369
|
+
];
|
|
1370
|
+
lines[0].W = lines[1].W = lines[2].W = lines[3].W = width(img);
|
|
1371
|
+
lines[0].H = lines[1].H = lines[2].H = lines[3].H = height(img);
|
|
1372
|
+
let minX = Math.min(Math.min(x0, x1), Math.min(x2, x3));
|
|
1373
|
+
let maxX = Math.min(Math.max(Math.max(x0, x1), Math.max(x2, x3)), lines[0].W - 1);
|
|
1374
|
+
const nextFuncList = [
|
|
1375
|
+
nextYRange_Low,
|
|
1376
|
+
nextYRange_HighUp,
|
|
1377
|
+
nextYRange_HighDown
|
|
1378
|
+
];
|
|
1379
|
+
const fpNext0 = nextFuncList[lines[0].nextFuncIndex];
|
|
1380
|
+
const fpNext1 = nextFuncList[lines[1].nextFuncIndex];
|
|
1381
|
+
const fpNext2 = nextFuncList[lines[2].nextFuncIndex];
|
|
1382
|
+
const fpNext3 = nextFuncList[lines[3].nextFuncIndex];
|
|
1383
|
+
const yRange = {
|
|
1384
|
+
min: lines[0].H,
|
|
1385
|
+
max: -1
|
|
1386
|
+
};
|
|
1387
|
+
for (let x = minX; x <= maxX; x++) {
|
|
1388
|
+
yRange.min = lines[0].H;
|
|
1389
|
+
yRange.max = -1;
|
|
1390
|
+
fpNext0(x, lines[0], yRange);
|
|
1391
|
+
fpNext1(x, lines[1], yRange);
|
|
1392
|
+
fpNext2(x, lines[2], yRange);
|
|
1393
|
+
fpNext3(x, lines[3], yRange);
|
|
1394
|
+
fillRect(img, x, yRange.min, 1, yRange.max - yRange.min + 1, c);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
BitmapMethods.fillPolygon4 = fillPolygon4;
|
|
1398
|
+
function _fillPolygon4(img, args) {
|
|
1399
|
+
fillPolygon4(img, args.getAt(0) | 0, args.getAt(1) | 0, args.getAt(2) | 0, args.getAt(3) | 0, args.getAt(4) | 0, args.getAt(5) | 0, args.getAt(6) | 0, args.getAt(7) | 0, args.getAt(8) | 0);
|
|
1400
|
+
}
|
|
1401
|
+
BitmapMethods._fillPolygon4 = _fillPolygon4;
|
|
1402
|
+
function _blitRow(img, xy, from, xh) {
|
|
1403
|
+
blitRow(img, XX(xy), YY(xy), from, XX(xh), YY(xh));
|
|
1404
|
+
}
|
|
1405
|
+
BitmapMethods._blitRow = _blitRow;
|
|
1406
|
+
function blitRow(img, x, y, from, fromX, fromH) {
|
|
1407
|
+
x |= 0;
|
|
1408
|
+
y |= 0;
|
|
1409
|
+
fromX |= 0;
|
|
1410
|
+
fromH |= 0;
|
|
1411
|
+
if (!img.inRange(x, 0) || !img.inRange(fromX, 0) || fromH <= 0)
|
|
1412
|
+
return;
|
|
1413
|
+
let fy = 0;
|
|
1414
|
+
let stepFY = ((from._width << 16) / fromH) | 0;
|
|
1415
|
+
let endY = y + fromH;
|
|
1416
|
+
if (endY > img._height)
|
|
1417
|
+
endY = img._height;
|
|
1418
|
+
if (y < 0) {
|
|
1419
|
+
fy += -y * stepFY;
|
|
1420
|
+
y = 0;
|
|
1421
|
+
}
|
|
1422
|
+
while (y < endY) {
|
|
1423
|
+
img.data[img.pix(x, y)] = from.data[from.pix(fromX, fy >> 16)];
|
|
1424
|
+
y++;
|
|
1425
|
+
fy += stepFY;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
BitmapMethods.blitRow = blitRow;
|
|
1429
|
+
function _blit(img, src, args) {
|
|
1430
|
+
return blit(img, src, args);
|
|
1431
|
+
}
|
|
1432
|
+
BitmapMethods._blit = _blit;
|
|
1433
|
+
function blit(dst, src, args) {
|
|
1434
|
+
const xDst = args.getAt(0);
|
|
1435
|
+
const yDst = args.getAt(1);
|
|
1436
|
+
const wDst = args.getAt(2);
|
|
1437
|
+
const hDst = args.getAt(3);
|
|
1438
|
+
const xSrc = args.getAt(4);
|
|
1439
|
+
const ySrc = args.getAt(5);
|
|
1440
|
+
const wSrc = args.getAt(6);
|
|
1441
|
+
const hSrc = args.getAt(7);
|
|
1442
|
+
const transparent = args.getAt(8);
|
|
1443
|
+
const check = args.getAt(9);
|
|
1444
|
+
const xSrcStep = ((wSrc << 16) / wDst) | 0;
|
|
1445
|
+
const ySrcStep = ((hSrc << 16) / hDst) | 0;
|
|
1446
|
+
const xDstClip = Math.abs(Math.min(0, xDst));
|
|
1447
|
+
const yDstClip = Math.abs(Math.min(0, yDst));
|
|
1448
|
+
const xDstStart = xDst + xDstClip;
|
|
1449
|
+
const yDstStart = yDst + yDstClip;
|
|
1450
|
+
const xDstEnd = Math.min(dst._width, xDst + wDst);
|
|
1451
|
+
const yDstEnd = Math.min(dst._height, yDst + hDst);
|
|
1452
|
+
const xSrcStart = Math.max(0, (xSrc << 16) + xDstClip * xSrcStep);
|
|
1453
|
+
const ySrcStart = Math.max(0, (ySrc << 16) + yDstClip * ySrcStep);
|
|
1454
|
+
const xSrcEnd = Math.min(src._width, xSrc + wSrc) << 16;
|
|
1455
|
+
const ySrcEnd = Math.min(src._height, ySrc + hSrc) << 16;
|
|
1456
|
+
if (!check)
|
|
1457
|
+
dst.makeWritable();
|
|
1458
|
+
for (let yDstCur = yDstStart, ySrcCur = ySrcStart; yDstCur < yDstEnd && ySrcCur < ySrcEnd; ++yDstCur, ySrcCur += ySrcStep) {
|
|
1459
|
+
const ySrcCurI = ySrcCur >> 16;
|
|
1460
|
+
for (let xDstCur = xDstStart, xSrcCur = xSrcStart; xDstCur < xDstEnd && xSrcCur < xSrcEnd; ++xDstCur, xSrcCur += xSrcStep) {
|
|
1461
|
+
const xSrcCurI = xSrcCur >> 16;
|
|
1462
|
+
const cSrc = getPixel(src, xSrcCurI, ySrcCurI);
|
|
1463
|
+
if (check && cSrc) {
|
|
1464
|
+
const cDst = getPixel(dst, xDstCur, yDstCur);
|
|
1465
|
+
if (cDst) {
|
|
1466
|
+
return true;
|
|
1467
|
+
}
|
|
1468
|
+
continue;
|
|
1469
|
+
}
|
|
1470
|
+
if (!transparent || cSrc) {
|
|
1471
|
+
setPixel(dst, xDstCur, yDstCur, cSrc);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
return false;
|
|
1476
|
+
}
|
|
1477
|
+
BitmapMethods.blit = blit;
|
|
1478
|
+
})(BitmapMethods = pxsim.BitmapMethods || (pxsim.BitmapMethods = {}));
|
|
1479
|
+
})(pxsim || (pxsim = {}));
|
|
1480
|
+
(function (pxsim) {
|
|
1481
|
+
var bitmap;
|
|
1482
|
+
(function (bitmap) {
|
|
1483
|
+
function byteHeight(h, bpp) {
|
|
1484
|
+
if (bpp == 1)
|
|
1485
|
+
return h * bpp + 7 >> 3;
|
|
1486
|
+
else
|
|
1487
|
+
return ((h * bpp + 31) >> 5) << 2;
|
|
1488
|
+
}
|
|
1489
|
+
bitmap.byteHeight = byteHeight;
|
|
1490
|
+
function isLegacyImage(buf) {
|
|
1491
|
+
if (!buf || buf.data.length < 5)
|
|
1492
|
+
return false;
|
|
1493
|
+
if (buf.data[0] != 0xe1 && buf.data[0] != 0xe4)
|
|
1494
|
+
return false;
|
|
1495
|
+
const bpp = buf.data[0] & 0xf;
|
|
1496
|
+
const sz = buf.data[1] * byteHeight(buf.data[2], bpp);
|
|
1497
|
+
if (4 + sz != buf.data.length)
|
|
1498
|
+
return false;
|
|
1499
|
+
return true;
|
|
1500
|
+
}
|
|
1501
|
+
function bufW(data) {
|
|
1502
|
+
return data[2] | (data[3] << 8);
|
|
1503
|
+
}
|
|
1504
|
+
bitmap.bufW = bufW;
|
|
1505
|
+
function bufH(data) {
|
|
1506
|
+
return data[4] | (data[5] << 8);
|
|
1507
|
+
}
|
|
1508
|
+
bitmap.bufH = bufH;
|
|
1509
|
+
function isValidImage(buf) {
|
|
1510
|
+
if (!buf || buf.data.length < 5)
|
|
1511
|
+
return false;
|
|
1512
|
+
if (buf.data[0] != 0x87)
|
|
1513
|
+
return false;
|
|
1514
|
+
if (buf.data[1] != 1 && buf.data[1] != 4)
|
|
1515
|
+
return false;
|
|
1516
|
+
const bpp = buf.data[1];
|
|
1517
|
+
const sz = bufW(buf.data) * byteHeight(bufH(buf.data), bpp);
|
|
1518
|
+
if (8 + sz != buf.data.length)
|
|
1519
|
+
return false;
|
|
1520
|
+
return true;
|
|
1521
|
+
}
|
|
1522
|
+
bitmap.isValidImage = isValidImage;
|
|
1523
|
+
function create(w, h) {
|
|
1524
|
+
// truncate decimal sizes
|
|
1525
|
+
w |= 0;
|
|
1526
|
+
h |= 0;
|
|
1527
|
+
return new pxsim.RefImage(w, h, 4);
|
|
1528
|
+
}
|
|
1529
|
+
bitmap.create = create;
|
|
1530
|
+
// TODO: move to image namespace
|
|
1531
|
+
function ofBuffer(buf) {
|
|
1532
|
+
const src = buf.data;
|
|
1533
|
+
let srcP = 4;
|
|
1534
|
+
let w = 0, h = 0, bpp = 0;
|
|
1535
|
+
if (isLegacyImage(buf)) {
|
|
1536
|
+
w = src[1];
|
|
1537
|
+
h = src[2];
|
|
1538
|
+
bpp = src[0] & 0xf;
|
|
1539
|
+
// console.log("using legacy image")
|
|
1540
|
+
}
|
|
1541
|
+
else if (isValidImage(buf)) {
|
|
1542
|
+
srcP = 8;
|
|
1543
|
+
w = bufW(src);
|
|
1544
|
+
h = bufH(src);
|
|
1545
|
+
bpp = src[1];
|
|
1546
|
+
}
|
|
1547
|
+
if (w == 0 || h == 0)
|
|
1548
|
+
return null;
|
|
1549
|
+
const r = new pxsim.RefImage(w, h, bpp);
|
|
1550
|
+
const dst = r.data;
|
|
1551
|
+
r.isStatic = buf.isStatic;
|
|
1552
|
+
if (bpp == 1) {
|
|
1553
|
+
for (let i = 0; i < w; ++i) {
|
|
1554
|
+
let dstP = i;
|
|
1555
|
+
let mask = 0x01;
|
|
1556
|
+
let v = src[srcP++];
|
|
1557
|
+
for (let j = 0; j < h; ++j) {
|
|
1558
|
+
if (mask == 0x100) {
|
|
1559
|
+
mask = 0x01;
|
|
1560
|
+
v = src[srcP++];
|
|
1561
|
+
}
|
|
1562
|
+
if (v & mask)
|
|
1563
|
+
dst[dstP] = 1;
|
|
1564
|
+
dstP += w;
|
|
1565
|
+
mask <<= 1;
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
else if (bpp == 4) {
|
|
1570
|
+
for (let i = 0; i < w; ++i) {
|
|
1571
|
+
let dstP = i;
|
|
1572
|
+
for (let j = 0; j < h >> 1; ++j) {
|
|
1573
|
+
const v = src[srcP++];
|
|
1574
|
+
dst[dstP] = v & 0xf;
|
|
1575
|
+
dstP += w;
|
|
1576
|
+
dst[dstP] = v >> 4;
|
|
1577
|
+
dstP += w;
|
|
1578
|
+
}
|
|
1579
|
+
if (h & 1)
|
|
1580
|
+
dst[dstP] = src[srcP++] & 0xf;
|
|
1581
|
+
srcP = (srcP + 3) & ~3;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
return r;
|
|
1585
|
+
}
|
|
1586
|
+
bitmap.ofBuffer = ofBuffer;
|
|
1587
|
+
function toBuffer(img) {
|
|
1588
|
+
let col = byteHeight(img._height, img._bpp);
|
|
1589
|
+
let sz = 8 + img._width * col;
|
|
1590
|
+
let r = new Uint8Array(sz);
|
|
1591
|
+
r[0] = 0x87;
|
|
1592
|
+
r[1] = img._bpp;
|
|
1593
|
+
r[2] = img._width & 0xff;
|
|
1594
|
+
r[3] = img._width >> 8;
|
|
1595
|
+
r[4] = img._height & 0xff;
|
|
1596
|
+
r[5] = img._height >> 8;
|
|
1597
|
+
let dstP = 8;
|
|
1598
|
+
const w = img._width;
|
|
1599
|
+
const h = img._height;
|
|
1600
|
+
const data = img.data;
|
|
1601
|
+
for (let i = 0; i < w; ++i) {
|
|
1602
|
+
if (img._bpp == 4) {
|
|
1603
|
+
let p = i;
|
|
1604
|
+
for (let j = 0; j < h; j += 2) {
|
|
1605
|
+
r[dstP++] = ((data[p + w] & 0xf) << 4) | ((data[p] || 0) & 0xf);
|
|
1606
|
+
p += 2 * w;
|
|
1607
|
+
}
|
|
1608
|
+
dstP = (dstP + 3) & ~3;
|
|
1609
|
+
}
|
|
1610
|
+
else if (img._bpp == 1) {
|
|
1611
|
+
let mask = 0x01;
|
|
1612
|
+
let p = i;
|
|
1613
|
+
for (let j = 0; j < h; j++) {
|
|
1614
|
+
if (data[p])
|
|
1615
|
+
r[dstP] |= mask;
|
|
1616
|
+
mask <<= 1;
|
|
1617
|
+
p += w;
|
|
1618
|
+
if (mask == 0x100) {
|
|
1619
|
+
mask = 0x01;
|
|
1620
|
+
dstP++;
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
if (mask != 0x01)
|
|
1624
|
+
dstP++;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
return new pxsim.RefBuffer(r);
|
|
1628
|
+
}
|
|
1629
|
+
bitmap.toBuffer = toBuffer;
|
|
1630
|
+
function doubledIcon(buf) {
|
|
1631
|
+
let img = ofBuffer(buf);
|
|
1632
|
+
if (!img)
|
|
1633
|
+
return null;
|
|
1634
|
+
img = pxsim.BitmapMethods.doubled(img);
|
|
1635
|
+
return toBuffer(img);
|
|
1636
|
+
}
|
|
1637
|
+
bitmap.doubledIcon = doubledIcon;
|
|
1638
|
+
})(bitmap = pxsim.bitmap || (pxsim.bitmap = {}));
|
|
1639
|
+
})(pxsim || (pxsim = {}));
|
|
1640
|
+
var pxsim;
|
|
596
1641
|
(function (pxsim) {
|
|
597
1642
|
var input;
|
|
598
1643
|
(function (input) {
|