MCPcopy
hub / github.com/dbolya/yolact / load_RLE

Function load_RLE

web/scripts/utils.js:1–41  ·  view source on GitHub ↗
(rle_obj, fillColor=[255, 255, 255], alpha=255)

Source from the content-addressed store, hash-verified

1function load_RLE(rle_obj, fillColor=[255, 255, 255], alpha=255) {
2 var h = rle_obj.size[0], w = rle_obj.size[1];
3 var counts = uncompress_RLE(rle_obj.counts);
4
5 var buffer_size = (w*h*4);
6 var buffer = new Uint8ClampedArray(w*h*4);
7 var bufferIdx = 0;
8
9 for (var countsIdx = 0; countsIdx < counts.length; countsIdx++) {
10 while (counts[countsIdx] > 0) {
11 // Kind of transpose the image as we go
12 if (bufferIdx >= buffer_size)
13 bufferIdx = (bufferIdx % buffer_size) + 4;
14
15 buffer[bufferIdx+0] = fillColor[0];
16 buffer[bufferIdx+1] = fillColor[1];
17 buffer[bufferIdx+2] = fillColor[2];
18 buffer[bufferIdx+3] = alpha * (countsIdx % 2);
19
20 bufferIdx += 4*w;
21 counts[countsIdx]--;
22 }
23 }
24
25 // Load into an off-screen canvas and return an image with that data
26 var canvas = document.createElement('canvas');
27 var ctx = canvas.getContext('2d');
28
29 canvas.width = w;
30 canvas.height = h;
31
32 var idata = ctx.createImageData(w, h);
33 idata.data.set(buffer);
34
35 ctx.putImageData(idata, 0, 0);
36
37 var img = new Image();
38 img.src = canvas.toDataURL();
39
40 return img;
41}
42
43function uncompress_RLE(rle_str) {
44 // Don't ask me how this works--I'm just transcribing from the pycocotools c api.

Callers 1

renderFunction · 0.85

Calls 1

uncompress_RLEFunction · 0.85

Tested by

no test coverage detected