canvas.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. (function() {
  20. /**
  21. * decompress bitmap from RLE algorithm
  22. * @param bitmap {object} bitmap object of bitmap event of node-rdpjs
  23. */
  24. function decompress (bitmap) {
  25. var fName = null;
  26. switch (bitmap.bitsPerPixel) {
  27. case 15:
  28. fName = 'bitmap_decompress_15';
  29. break;
  30. case 16:
  31. fName = 'bitmap_decompress_16';
  32. break;
  33. case 24:
  34. fName = 'bitmap_decompress_24';
  35. break;
  36. case 32:
  37. fName = 'bitmap_decompress_32';
  38. break;
  39. default:
  40. throw 'invalid bitmap data format';
  41. }
  42. var input = new Uint8Array(bitmap.data);
  43. var inputPtr = Module._malloc(input.length);
  44. var inputHeap = new Uint8Array(Module.HEAPU8.buffer, inputPtr, input.length);
  45. inputHeap.set(input);
  46. var output_width = bitmap.destRight - bitmap.destLeft + 1;
  47. var output_height = bitmap.destBottom - bitmap.destTop + 1;
  48. var ouputSize = output_width * output_height * 4;
  49. var outputPtr = Module._malloc(ouputSize);
  50. var outputHeap = new Uint8Array(Module.HEAPU8.buffer, outputPtr, ouputSize);
  51. var res = Module.ccall(fName,
  52. 'number',
  53. ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'],
  54. [outputHeap.byteOffset, output_width, output_height, bitmap.width, bitmap.height, inputHeap.byteOffset, input.length]
  55. );
  56. var output = new Uint8ClampedArray(outputHeap.buffer, outputHeap.byteOffset, ouputSize);
  57. Module._free(inputPtr);
  58. Module._free(outputPtr);
  59. return { width : output_width, height : output_height, data : output };
  60. }
  61. /**
  62. * Un compress bitmap are reverse in y axis
  63. */
  64. function reverse (bitmap) {
  65. return { width : bitmap.width, height : bitmap.height, data : new Uint8ClampedArray(bitmap.data) };
  66. }
  67. /**
  68. * Canvas renderer
  69. * @param canvas {canvas} use for rendering
  70. */
  71. function Canvas(canvas) {
  72. this.canvas = canvas;
  73. this.ctx = canvas.getContext("2d");
  74. }
  75. Canvas.prototype = {
  76. /**
  77. * update canvas with new bitmap
  78. * @param bitmap {object}
  79. */
  80. update : function (bitmap) {
  81. var output = null;
  82. if (bitmap.isCompress) {
  83. output = decompress(bitmap);
  84. }
  85. else {
  86. output = reverse(bitmap);
  87. }
  88. // use image data to use asm.js
  89. var imageData = this.ctx.createImageData(output.width, output.height);
  90. imageData.data.set(output.data);
  91. this.ctx.putImageData(imageData, bitmap.destLeft, bitmap.destTop);
  92. }
  93. }
  94. /**
  95. * Module export
  96. */
  97. Mstsc.Canvas = {
  98. create : function (canvas) {
  99. return new Canvas(canvas);
  100. }
  101. }
  102. })();