MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / send

Method send

modules/commodetto/commodettoRLEOut.js:60–146  ·  view source on GitHub ↗
(pixels, offsetIn = 0, count = pixels.byteLength - offsetIn)

Source from the content-addressed store, hash-verified

58 this.scratch = new Uint8Array(this.pixelsToBytes(width) + width);
59 }
60 send(pixels, offsetIn = 0, count = pixels.byteLength - offsetIn) {
61 let width = this.windowWidth;
62 let mask = new Uint8Array(this.pixelsToBytes(width))
63 pixels = (16 === this.depth) ? new Uint16Array(pixels) : new Uint8Array(pixels);;
64 let lines = Math.floor(count / width);
65 let bytes = this.scratch;
66 let key = this.key;
67 if (undefined === key)
68 mask.fill(1);
69 for (let line = 0, offset = offsetIn; line < lines; line++, offset += width) {
70 if (undefined !== key) {
71 // build mask
72 for (let x = 0; x < width; x++) {
73 let pixel = pixels[offset + x];
74 mask[x] = (key != pixel);
75 }
76 }
77
78 let position = 0;
79 for (let x = 0; x < width; ) {
80 let maskRunLength;
81
82 if (undefined !== key) {
83 maskRunLength = 1;
84 while ((maskRunLength < 64) && ((x + maskRunLength) < width) && (mask[x] == mask[x + maskRunLength])) //@@ 64 is conservative... depends on run type
85 maskRunLength += 1;
86 }
87 else
88 maskRunLength = width;
89
90 if (mask[x]) {
91 // visible pixels
92 while (maskRunLength) {
93 // is the start of this run a repeat?
94 if (maskRunLength >= 3) {
95 let repeatLength;
96 for (repeatLength = 0; repeatLength < maskRunLength; repeatLength++) {
97 if (pixels[offset + x] != pixels[offset + x + repeatLength])
98 break;
99 }
100 if (repeatLength >= 3) {
101 if (repeatLength > 64)
102 repeatLength = 64; // maximum repeat length
103 bytes[position++] = 0x80 | (repeatLength - 1); // repeat op-code
104 bytes[position++] = pixels[offset + x] & 0xff; // repeat value
105 if (16 == this.depth)
106 bytes[position++] = pixels[offset + x] >> 8; // repeat value
107 maskRunLength -= repeatLength;
108 x += repeatLength;
109 continue;
110 }
111 }
112
113 // literal. find next repeat, if any, to see for how long the literal lasts.
114 let repeatStart = maskRunLength;
115 for (let j = 0; j < maskRunLength - 2; j++) {
116 if ((pixels[offset + x + j] != pixels[offset + x + j + 1]) || (pixels[offset + x + j] != pixels[offset + x + j + 2]))
117 continue;

Callers

nothing calls this directly

Calls 4

pixelsToBytesMethod · 0.95
fillMethod · 0.65
sliceMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected