2 * Copyright (c) 2015 Sylvain Peyrefitte
4 * This file is part of mstsc.js.
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.
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.
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/>.
23 * decompress bitmap from RLE algorithm
24 * @param bitmap {object} bitmap object of bitmap event of node-rdpjs
26 function decompress (bitmap) {
28 switch (bitmap.bitsPerPixel) {
30 fName = 'bitmap_decompress_15';
33 fName = 'bitmap_decompress_16';
36 fName = 'bitmap_decompress_24';
39 fName = 'bitmap_decompress_32';
42 throw 'invalid bitmap data format';
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);
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);
55 var outputHeap = new Uint8Array(Module.HEAPU8.buffer, outputPtr, ouputSize);
57 var res = Module.ccall(fName,
59 ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'],
60 [outputHeap.byteOffset, output_width, output_height, bitmap.width, bitmap.height, inputHeap.byteOffset, input.length]
63 var output = new Uint8ClampedArray(outputHeap.buffer, outputHeap.byteOffset, ouputSize);
65 Module._free(inputPtr);
66 Module._free(outputPtr);
68 return { width : output_width, height : output_height, data : output };
72 * Un compress bitmap are reverse in y axis
74 function reverse (bitmap) {
75 return { width : bitmap.width, height : bitmap.height, data : new Uint8ClampedArray(bitmap.data) };
80 * @param canvas {canvas} use for rendering
82 function Canvas(canvas) {
84 this.ctx = canvas.getContext("2d");
89 * update canvas with new bitmap
90 * @param bitmap {object}
92 update : function (bitmap) {
94 if (bitmap.isCompress) {
95 output = decompress(bitmap);
98 output = reverse(bitmap);
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);
112 create : function (canvas) {
113 return new Canvas(canvas);