raw.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Copyright (C) 2018 Samuel Mannehed for Cendio AB
  5. * Copyright (C) 2018 Pierre Ossman for Cendio AB
  6. * Licensed under MPL 2.0 (see LICENSE.txt)
  7. *
  8. * See README.md for usage and integration instructions.
  9. *
  10. */
  11. export default class RawDecoder {
  12. constructor() {
  13. this._lines = 0;
  14. }
  15. decodeRect(x, y, width, height, sock, display, depth) {
  16. if (this._lines === 0) {
  17. this._lines = height;
  18. }
  19. const pixelSize = depth == 8 ? 1 : 4;
  20. const bytesPerLine = width * pixelSize;
  21. if (sock.rQwait("RAW", bytesPerLine)) {
  22. return false;
  23. }
  24. const cur_y = y + (height - this._lines);
  25. const curr_height = Math.min(this._lines,
  26. Math.floor(sock.rQlen / bytesPerLine));
  27. let data = sock.rQ;
  28. let index = sock.rQi;
  29. // Convert data if needed
  30. if (depth == 8) {
  31. const pixels = width * curr_height;
  32. const newdata = new Uint8Array(pixels * 4);
  33. for (let i = 0; i < pixels; i++) {
  34. newdata[i * 4 + 0] = ((data[index + i] >> 0) & 0x3) * 255 / 3;
  35. newdata[i * 4 + 1] = ((data[index + i] >> 2) & 0x3) * 255 / 3;
  36. newdata[i * 4 + 2] = ((data[index + i] >> 4) & 0x3) * 255 / 3;
  37. newdata[i * 4 + 4] = 0;
  38. }
  39. data = newdata;
  40. index = 0;
  41. }
  42. display.blitImage(x, cur_y, width, curr_height, data, index);
  43. sock.rQskipBytes(curr_height * bytesPerLine);
  44. this._lines -= curr_height;
  45. if (this._lines > 0) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. }