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

Method send

modules/commodetto/commodettoRLE4Out.js:59–122  ·  view source on GitHub ↗
(pixels, offsetIn = 0, count = pixels.byteLength - offsetIn)

Source from the content-addressed store, hash-verified

57 this.output.firstNybble = true;
58 }
59 send(pixels, offsetIn = 0, count = pixels.byteLength - offsetIn) {
60 let width = this.width;
61 let halfWidth = (width + 1) >> 1;
62
63 let lines = Math.floor((count * 2) / width);
64 let scan = this.scan;
65 const solid = 0;
66 const skip = 15;
67
68 pixels = new Uint8Array(pixels);
69 for (let line = 0, offset = offsetIn; line < lines; line++, offset += halfWidth) {
70 // expand incoming packed 4-bit gray pixels into bytes (if width is odd, expands one extra (unused) pixel)
71 for (let i = 0, pos = offset; i < width; i += 2, pos += 1) {
72 scan[i + 0] = pixels[pos] >> 4;
73 scan[i + 1] = pixels[pos] & 0x0F;
74 }
75
76 // encode one run
77 for (let remain = width, pos = 0; remain > 0; ) {
78 if (remain >= 2) {
79 // check for skip run
80 if ((skip === scan[pos]) && (skip === scan[pos + 1])) {
81 let count = 2;
82 while (((remain - count) > 0) && (count < 9) && (skip === scan[pos + count]))
83 count += 1;
84 this.writeNybble(count - 2);
85 remain -= count;
86 pos += count;
87 continue;
88 }
89
90 // check for solid run
91 if ((solid === scan[pos]) && (solid === scan[pos + 1])) {
92 let count = 2;
93 while (((remain - count) > 0) && (count < 5) && (solid === scan[pos + count]))
94 count += 1;
95 this.writeNybble(0x08 | (count - 2));
96 remain -= count;
97 pos += count;
98 continue;
99 }
100 }
101
102 // quote
103 let quoteLen = 1;
104 while (quoteLen < 4) {
105 if (remain < (quoteLen + 2)) {
106 quoteLen = Math.min(remain, 4);
107 break;
108 }
109 if ((scan[pos + quoteLen] === scan[pos + quoteLen + 1]) &&
110 ((solid === scan[pos + quoteLen]) || (skip === scan[pos + quoteLen])))
111 break;
112 quoteLen += 1;
113 }
114
115 this.writeNybble(0x0C | (quoteLen - 1));
116 for (let i = 0; i < quoteLen; i++)

Callers 2

runMethod · 0.95
runMethod · 0.95

Calls 1

writeNybbleMethod · 0.95

Tested by

no test coverage detected