Mercurial > hg > Applications > TreeVNC
comparison src/main/java/com/glavsoft/rfb/encoding/decoder/TightDecoder.java @ 0:4689cc86d6cb
create TreeViewer2 Repository
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 13:20:49 +0900 |
parents | |
children | 0c08cdc4b572 17b702648079 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4689cc86d6cb |
---|---|
1 // Copyright (C) 2010, 2011 GlavSoft LLC. | |
2 // All rights reserved. | |
3 // | |
4 //------------------------------------------------------------------------- | |
5 // This file is part of the TightVNC software. Please visit our Web site: | |
6 // | |
7 // http://www.tightvnc.com/ | |
8 // | |
9 // This program is free software; you can redistribute it and/or modify | |
10 // it under the terms of the GNU General Public License as published by | |
11 // the Free Software Foundation; either version 2 of the License, or | |
12 // (at your option) any later version. | |
13 // | |
14 // This program is distributed in the hope that it will be useful, | |
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 // GNU General Public License for more details. | |
18 // | |
19 // You should have received a copy of the GNU General Public License along | |
20 // with this program; if not, write to the Free Software Foundation, Inc., | |
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 //------------------------------------------------------------------------- | |
23 // | |
24 | |
25 package com.glavsoft.rfb.encoding.decoder; | |
26 | |
27 import java.util.logging.Logger; | |
28 import java.util.zip.DataFormatException; | |
29 import java.util.zip.Inflater; | |
30 | |
31 import com.glavsoft.drawing.ColorDecoder; | |
32 import com.glavsoft.drawing.Renderer; | |
33 import com.glavsoft.exceptions.TransportException; | |
34 import com.glavsoft.transport.Reader; | |
35 | |
36 /** | |
37 * Tight protocol extention decoder | |
38 */ | |
39 public class TightDecoder extends Decoder { | |
40 private static Logger logger = Logger.getLogger("com.glavsoft.rfb.encoding.decoder"); | |
41 | |
42 private static final int FILL_TYPE = 0x08; | |
43 private static final int JPEG_TYPE = 0x09; | |
44 | |
45 private static final int FILTER_ID_MASK = 0x40; | |
46 private static final int STREAM_ID_MASK = 0x30; | |
47 | |
48 private static final int BASIC_FILTER = 0x00; | |
49 private static final int PALETTE_FILTER = 0x01; | |
50 private static final int GRADIENT_FILTER = 0x02; | |
51 private static final int MIN_SIZE_TO_COMPRESS = 12; | |
52 | |
53 static final int DECODERS_NUM = 4; | |
54 Inflater[] decoders; | |
55 | |
56 private int decoderId; | |
57 | |
58 final static int tightZlibBufferSize = 512; | |
59 | |
60 public TightDecoder() { | |
61 reset(); | |
62 } | |
63 | |
64 @Override | |
65 public void decode(Reader reader, Renderer renderer, | |
66 FramebufferUpdateRectangle rect) throws TransportException { | |
67 int bytesPerPixel = renderer.getBytesPerPixelSignificant(); | |
68 | |
69 /** | |
70 * bits | |
71 * 7 - FILL or JPEG type | |
72 * 6 - filter presence flag | |
73 * 5, 4 - decoder to use when Basic type (bit 7 not set) | |
74 * or | |
75 * 4 - JPEG type when set bit 7 | |
76 * 3 - reset decoder #3 | |
77 * 2 - reset decoder #2 | |
78 * 1 - reset decoder #1 | |
79 * 0 - reset decoder #0 | |
80 */ | |
81 int compControl = reader.readUInt8(); | |
82 resetDecoders(compControl); | |
83 | |
84 int compType = compControl >> 4 & 0x0F; | |
85 switch (compType) { | |
86 case FILL_TYPE: | |
87 int color = renderer.readTightPixelColor(reader); | |
88 renderer.fillRect(color, rect); | |
89 break; | |
90 case JPEG_TYPE: | |
91 if (bytesPerPixel != 3) { | |
92 // throw new EncodingException( | |
93 // "Tight doesn't support JPEG subencoding while depth not equal to 24bpp is used"); | |
94 } | |
95 processJpegType(reader, renderer, rect); | |
96 break; | |
97 default: | |
98 if (compType > JPEG_TYPE) { | |
99 // throw new EncodingException( | |
100 // "Compression control byte is incorrect!"); | |
101 } else { | |
102 processBasicType(compControl, reader, renderer, rect); | |
103 } | |
104 } | |
105 } | |
106 | |
107 private void processBasicType(int compControl, Reader reader, | |
108 Renderer renderer, FramebufferUpdateRectangle rect) throws TransportException { | |
109 decoderId = (compControl & STREAM_ID_MASK) >> 4; | |
110 | |
111 int filterId = 0; | |
112 if ((compControl & FILTER_ID_MASK) > 0) { // filter byte presence | |
113 filterId = reader.readUInt8(); | |
114 } | |
115 int bytesPerCPixel = renderer.getBytesPerPixelSignificant(); | |
116 int lengthCurrentbpp = bytesPerCPixel * rect.width * rect.height; | |
117 byte [] buffer; | |
118 switch (filterId) { | |
119 case BASIC_FILTER: | |
120 buffer = readTightData(lengthCurrentbpp, reader); | |
121 renderer.drawTightBytes(buffer, 0, rect.x, rect.y, rect.width, rect.height); | |
122 break; | |
123 case PALETTE_FILTER: | |
124 int paletteSize = reader.readUInt8() + 1; | |
125 int[] palette = readPalette(paletteSize, reader, renderer); | |
126 int dataLength = paletteSize == 2 ? | |
127 rect.height * ((rect.width + 7) / 8) : | |
128 rect.width * rect.height; | |
129 buffer = readTightData(dataLength, reader); | |
130 renderer.drawBytesWithPalette(buffer, rect, palette); | |
131 break; | |
132 case GRADIENT_FILTER: | |
133 /* | |
134 * The "gradient" filter pre-processes pixel data with a simple algorithm | |
135 * which converts each color component to a difference between a "predicted" | |
136 * intensity and the actual intensity. Such a technique does not affect | |
137 * uncompressed data size, but helps to compress photo-like images better. | |
138 * Pseudo-code for converting intensities to differences is the following: | |
139 * | |
140 * P[i,j] := V[i-1,j] + V[i,j-1] - V[i-1,j-1]; | |
141 * if (P[i,j] < 0) then P[i,j] := 0; | |
142 * if (P[i,j] > MAX) then P[i,j] := MAX; | |
143 * D[i,j] := V[i,j] - P[i,j]; | |
144 * | |
145 * Here V[i,j] is the intensity of a color component for a pixel at | |
146 * coordinates (i,j). MAX is the maximum value of intensity for a color | |
147 * component.*/ | |
148 buffer = readTightData(bytesPerCPixel * rect.width * rect.height, reader); | |
149 byte [][] opRows = new byte[2][rect.width * 3 + 3]; | |
150 int opRowIndex = 0; | |
151 byte [] components = new byte[3]; | |
152 int pixelOffset = 0; | |
153 ColorDecoder colorDecoder = renderer.getColorDecoder(); | |
154 for (int i = 0; i < rect.height; ++i) { | |
155 // exchange thisRow and prevRow: | |
156 byte [] thisRow = opRows[opRowIndex]; | |
157 byte [] prevRow = opRows[opRowIndex = (opRowIndex + 1) % 2]; | |
158 for (int j = 3; j < rect.width * 3 + 3; j += 3) { | |
159 colorDecoder.fillRawComponents(components, buffer, pixelOffset); | |
160 pixelOffset += bytesPerCPixel; | |
161 int | |
162 d = (0xff & prevRow[j + 0]) + // "upper" pixel (from prev row) | |
163 (0xff & thisRow[j + 0 - 3]) - // prev pixel | |
164 (0xff & prevRow[j + 0 - 3]); // "diagonal" prev pixel | |
165 thisRow[j + 0] = (byte) (components[0] + (d < 0 ? 0 : d > colorDecoder.redMax ? colorDecoder.redMax: d) & colorDecoder.redMax); | |
166 d = (0xff & prevRow[j + 1]) + | |
167 (0xff & thisRow[j + 1 - 3]) - | |
168 (0xff & prevRow[j + 1 - 3]); | |
169 thisRow[j + 1] = (byte) (components[1] + (d < 0 ? 0 : d > colorDecoder.greenMax ? colorDecoder.greenMax: d) & colorDecoder.greenMax); | |
170 d = (0xff & prevRow[j + 2]) + | |
171 (0xff & thisRow[j + 2 - 3]) - | |
172 (0xff & prevRow[j + 2 - 3]); | |
173 thisRow[j + 2] = (byte) (components[2] + (d < 0 ? 0 : d > colorDecoder.blueMax ? colorDecoder.blueMax: d) & colorDecoder.blueMax); | |
174 } | |
175 renderer.drawUncaliberedRGBLine(thisRow, rect.x, rect.y + i, rect.width); | |
176 } | |
177 | |
178 break; | |
179 default: | |
180 break; | |
181 } | |
182 } | |
183 | |
184 /** | |
185 * Read palette from reader | |
186 */ | |
187 private int[] readPalette(int paletteSize, Reader reader, Renderer renderer) throws TransportException { | |
188 /** | |
189 * When bytesPerPixel == 1 && paletteSize == 2 read 2 bytes of palette | |
190 * When bytesPerPixel == 1 && paletteSize != 2 - error | |
191 * When bytesPerPixel == 3 (4) read (paletteSize * 3) bytes of palette | |
192 * so use renderer.readPixelColor | |
193 */ | |
194 int[] palette = new int[paletteSize]; | |
195 for (int i = 0; i < palette.length; ++i) { | |
196 palette[i] = renderer.readTightPixelColor(reader); | |
197 } | |
198 return palette; | |
199 } | |
200 | |
201 /** | |
202 * Reads compressed (expected length >= MIN_SIZE_TO_COMPRESS) or | |
203 * uncompressed data. When compressed decompresses it. | |
204 * | |
205 * @param expectedLength expected data length in bytes | |
206 * @param reader | |
207 * @return result data | |
208 * @throws TransportException | |
209 */ | |
210 private byte[] readTightData(int expectedLength, Reader reader) throws TransportException { | |
211 if (expectedLength < MIN_SIZE_TO_COMPRESS) { | |
212 byte [] buffer = ByteBuffer.getInstance().getBuffer(expectedLength); | |
213 reader.readBytes(buffer, 0, expectedLength); | |
214 return buffer; | |
215 } else | |
216 return readCompressedData(expectedLength, reader); | |
217 } | |
218 | |
219 /** | |
220 * Reads compressed data length, then read compressed data into rawBuffer | |
221 * and decompress data with expected length == length | |
222 * | |
223 * Note: returned data contains not only decompressed data but raw data at array tail | |
224 * which need to be ignored. Use only first expectedLength bytes. | |
225 * | |
226 * @param expectedLength expected data length | |
227 * @param reader | |
228 * @return decompressed data (length == expectedLength) / + followed raw data (ignore, please) | |
229 * @throws TransportException | |
230 */ | |
231 private byte[] readCompressedData(int expectedLength, Reader reader) throws TransportException { | |
232 int rawDataLength = readCompactSize(reader); | |
233 | |
234 byte [] buffer = ByteBuffer.getInstance().getBuffer(expectedLength + rawDataLength); | |
235 // read compressed (raw) data behind space allocated for decompressed data | |
236 reader.readBytes(buffer, expectedLength, rawDataLength); | |
237 if (null == decoders[decoderId]) { | |
238 decoders[decoderId] = new Inflater(); | |
239 } | |
240 Inflater decoder = decoders[decoderId]; | |
241 decoder.setInput(buffer, expectedLength, rawDataLength); | |
242 try { | |
243 decoder.inflate(buffer, 0, expectedLength); | |
244 } catch (DataFormatException e) { | |
245 logger.throwing("TightDecoder", "readCompressedData", e); | |
246 throw new TransportException("cannot inflate tight compressed data", e); | |
247 } | |
248 return buffer; | |
249 } | |
250 | |
251 private void processJpegType(Reader reader, Renderer renderer, | |
252 FramebufferUpdateRectangle rect) throws TransportException { | |
253 int jpegBufferLength = readCompactSize(reader); | |
254 byte [] bytes = ByteBuffer.getInstance().getBuffer(jpegBufferLength); | |
255 reader.readBytes(bytes, 0, jpegBufferLength); | |
256 renderer.drawJpegImage(bytes, 0, jpegBufferLength, rect); | |
257 } | |
258 | |
259 /** | |
260 * Read an integer from reader in compact representation (from 1 to 3 bytes). | |
261 * Highest bit of read byte set to 1 means next byte contains data. | |
262 * Lower 7 bit of each byte contains significant data. Max bytes = 3. | |
263 * Less significant bytes first order. | |
264 * | |
265 * @param reader | |
266 * @return int value | |
267 * @throws TransportException | |
268 */ | |
269 private int readCompactSize(Reader reader) throws TransportException { | |
270 int b = reader.readUInt8(); | |
271 int size = b & 0x7F; | |
272 if ((b & 0x80) != 0) { | |
273 b = reader.readUInt8(); | |
274 size += (b & 0x7F) << 7; | |
275 if ((b & 0x80) != 0) { | |
276 size += reader.readUInt8() << 14; | |
277 } | |
278 } | |
279 return size; | |
280 } | |
281 | |
282 /** | |
283 * Flush (reset) zlib decoders when bits 3, 2, 1, 0 of compControl is set | |
284 * @param compControl | |
285 */ | |
286 private void resetDecoders(int compControl) { | |
287 for (int i=0; i < DECODERS_NUM; ++i) { | |
288 if ((compControl & 1) != 0 && decoders[i] != null) { | |
289 decoders[i].reset(); | |
290 } | |
291 compControl >>= 1; | |
292 } | |
293 | |
294 } | |
295 | |
296 @Override | |
297 public void reset() { | |
298 decoders = new Inflater[DECODERS_NUM]; | |
299 } | |
300 | |
301 } |