| 1 | /* |
| 2 | * Copyright (c) 2015 Sylvain Peyrefitte |
| 3 | * |
| 4 | * This file is part of mstsc.js. |
| 5 | * |
| 6 | * mstsc.js is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | (function() { |
| 21 | |
| 22 | /** |
| 23 | * decompress bitmap from RLE algorithm |
| 24 | * @param bitmap {object} bitmap object of bitmap event of node-rdpjs |
| 25 | */ |
| 26 | function decompress (bitmap) { |
| 27 | var fName = null; |
| 28 | switch (bitmap.bitsPerPixel) { |
| 29 | case 15: |
| 30 | fName = 'bitmap_decompress_15'; |
| 31 | break; |
| 32 | case 16: |
| 33 | fName = 'bitmap_decompress_16'; |
| 34 | break; |
| 35 | case 24: |
| 36 | fName = 'bitmap_decompress_24'; |
| 37 | break; |
| 38 | case 32: |
| 39 | fName = 'bitmap_decompress_32'; |
| 40 | break; |
| 41 | default: |
| 42 | throw 'invalid bitmap data format'; |
| 43 | } |
| 44 | |
| 45 | var input = new Uint8Array(bitmap.data); |
| 46 | var inputPtr = Module._malloc(input.length); |
| 47 | var inputHeap = new Uint8Array(Module.HEAPU8.buffer, inputPtr, input.length); |
| 48 | inputHeap.set(input); |
| 49 | |
| 50 | var output_width = bitmap.destRight - bitmap.destLeft + 1; |
| 51 | var output_height = bitmap.destBottom - bitmap.destTop + 1; |
| 52 | var ouputSize = output_width * output_height * 4; |
| 53 | var outputPtr = Module._malloc(ouputSize); |
| 54 | |
| 55 | var outputHeap = new Uint8Array(Module.HEAPU8.buffer, outputPtr, ouputSize); |
| 56 | |
| 57 | var res = Module.ccall(fName, |
| 58 | 'number', |
| 59 | ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'], |
| 60 | [outputHeap.byteOffset, output_width, output_height, bitmap.width, bitmap.height, inputHeap.byteOffset, input.length] |
| 61 | ); |
| 62 | |
| 63 | var output = new Uint8ClampedArray(outputHeap.buffer, outputHeap.byteOffset, ouputSize); |
| 64 | |
| 65 | Module._free(inputPtr); |
| 66 | Module._free(outputPtr); |
| 67 | |
| 68 | return { width : output_width, height : output_height, data : output }; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Un compress bitmap are reverse in y axis |
| 73 | */ |
| 74 | function reverse (bitmap) { |
| 75 | return { width : bitmap.width, height : bitmap.height, data : new Uint8ClampedArray(bitmap.data) }; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Canvas renderer |
| 80 | * @param canvas {canvas} use for rendering |
| 81 | */ |
| 82 | function Canvas(canvas) { |
| 83 | this.canvas = canvas; |
| 84 | this.ctx = canvas.getContext("2d"); |
| 85 | } |
| 86 | |
| 87 | Canvas.prototype = { |
| 88 | /** |
| 89 | * update canvas with new bitmap |
| 90 | * @param bitmap {object} |
| 91 | */ |
| 92 | update : function (bitmap) { |
| 93 | var output = null; |
| 94 | if (bitmap.isCompress) { |
| 95 | output = decompress(bitmap); |
| 96 | } |
| 97 | else { |
| 98 | output = reverse(bitmap); |
| 99 | } |
| 100 | |
| 101 | // use image data to use asm.js |
| 102 | var imageData = this.ctx.createImageData(output.width, output.height); |
| 103 | imageData.data.set(output.data); |
| 104 | this.ctx.putImageData(imageData, bitmap.destLeft, bitmap.destTop); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Module export |
| 110 | */ |
| 111 | Mstsc.Canvas = { |
| 112 | create : function (canvas) { |
| 113 | return new Canvas(canvas); |
| 114 | } |
| 115 | } |
| 116 | })(); |