2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
10export default class JPEGDecoder {
12 // RealVNC will reuse the quantization tables
13 // and Huffman tables, so we need to cache them.
14 this._cachedQuantTables = [];
15 this._cachedHuffmanTables = [];
20 decodeRect(x, y, width, height, sock, display, depth) {
21 // A rect of JPEG encodings is simply a JPEG file
23 let segment = this._readSegment(sock);
24 if (segment === null) {
27 this._segments.push(segment);
29 if (segment[1] === 0xD9) {
34 let huffmanTables = [];
36 for (let segment of this._segments) {
37 let type = segment[1];
40 huffmanTables.push(segment);
41 } else if (type === 0xDB) {
42 // Quantization tables
43 quantTables.push(segment);
47 const sofIndex = this._segments.findIndex(
48 x => x[1] == 0xC0 || x[1] == 0xC2
51 throw new Error("Illegal JPEG image without SOF");
54 if (quantTables.length === 0) {
55 this._segments.splice(sofIndex+1, 0,
56 ...this._cachedQuantTables);
58 if (huffmanTables.length === 0) {
59 this._segments.splice(sofIndex+1, 0,
60 ...this._cachedHuffmanTables);
64 for (let segment of this._segments) {
65 length += segment.length;
68 let data = new Uint8Array(length);
70 for (let segment of this._segments) {
71 data.set(segment, length);
72 length += segment.length;
75 display.imageRect(x, y, width, height, "image/jpeg", data);
77 if (huffmanTables.length !== 0) {
78 this._cachedHuffmanTables = huffmanTables;
80 if (quantTables.length !== 0) {
81 this._cachedQuantTables = quantTables;
90 if (sock.rQwait("JPEG", 2)) {
94 let marker = sock.rQshift8();
96 throw new Error("Illegal JPEG marker received (byte: " +
99 let type = sock.rQshift8();
100 if (type >= 0xD0 && type <= 0xD9 || type == 0x01) {
101 // No length after marker
102 return new Uint8Array([marker, type]);
105 if (sock.rQwait("JPEG", 2, 2)) {
109 let length = sock.rQshift16();
111 throw new Error("Illegal JPEG length received (length: " +
115 if (sock.rQwait("JPEG", length-2, 4)) {
124 if (sock.rQwait("JPEG", length-2+extra, 4)) {
127 let data = sock.rQpeekBytes(length-2+extra, false);
128 if (data.at(-2) === 0xFF && data.at(-1) !== 0x00 &&
129 !(data.at(-1) >= 0xD0 && data.at(-1) <= 0xD7)) {
137 let segment = new Uint8Array(2 + length + extra);
140 segment[2] = length >> 8;
142 segment.set(sock.rQshiftBytes(length-2+extra, false), 4);