(rle_obj, fillColor=[255, 255, 255], alpha=255)
| 1 | function 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 | |
| 43 | function uncompress_RLE(rle_str) { |
| 44 | // Don't ask me how this works--I'm just transcribing from the pycocotools c api. |
no test coverage detected