annotate src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java @ 17:61d95bdc2bdb

change Alice PUT API new Version
author sugi
date Sun, 02 Nov 2014 18:26:41 +0900
parents 0415badf417f
children 80b4d7efba08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
2 // All rights reserved.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
3 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
4 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
5 // This file is part of the TightVNC software. Please visit our Web site:
daa24f8a557b TightVNC original
YU
parents:
diff changeset
6 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
7 // http://www.tightvnc.com/
daa24f8a557b TightVNC original
YU
parents:
diff changeset
8 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
9 // This program is free software; you can redistribute it and/or modify
daa24f8a557b TightVNC original
YU
parents:
diff changeset
10 // it under the terms of the GNU General Public License as published by
daa24f8a557b TightVNC original
YU
parents:
diff changeset
11 // the Free Software Foundation; either version 2 of the License, or
daa24f8a557b TightVNC original
YU
parents:
diff changeset
12 // (at your option) any later version.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
13 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
14 // This program is distributed in the hope that it will be useful,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
daa24f8a557b TightVNC original
YU
parents:
diff changeset
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
daa24f8a557b TightVNC original
YU
parents:
diff changeset
17 // GNU General Public License for more details.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
18 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
19 // You should have received a copy of the GNU General Public License along
daa24f8a557b TightVNC original
YU
parents:
diff changeset
20 // with this program; if not, write to the Free Software Foundation, Inc.,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
22 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
23 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
24
daa24f8a557b TightVNC original
YU
parents:
diff changeset
25 package com.glavsoft.rfb.encoding.decoder;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
26
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
27 import alice.datasegment.DataSegment;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
28
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
29 import com.glavsoft.drawing.Renderer;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
30 import com.glavsoft.exceptions.TransportException;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
31 import com.glavsoft.transport.Reader;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
32
daa24f8a557b TightVNC original
YU
parents:
diff changeset
33 public class ZRLEDecoder extends ZlibDecoder {
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
34 private static final int MAX_TILE_SIZE = 64;
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
35 private int[] decodedBitmap;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
36 private int[] palette;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
37
daa24f8a557b TightVNC original
YU
parents:
diff changeset
38 @Override
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
39 public void decode(Reader reader, Renderer renderer,
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
40 FramebufferUpdateRectangle rect) throws TransportException {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
41 int zippedLength = (int) reader.readUInt32();
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
42 if (0 == zippedLength) return;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
43 int length = rect.width * rect.height * renderer.getBytesPerPixel();
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
44 byte[] bytes = unzip(reader, zippedLength, length);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
45 int offset = zippedLength;
17
61d95bdc2bdb change Alice PUT API new Version
sugi
parents: 12
diff changeset
46
7
9abf7f305bac refactor
YU
parents: 5
diff changeset
47 AliceVNCMessage message = new AliceVNCMessage();
9abf7f305bac refactor
YU
parents: 5
diff changeset
48 message.setRectangle(rect);
12
0415badf417f bug fix (ArrayIndexOutOfBoundsException)
YU
parents: 7
diff changeset
49 message.buf = bytes.clone();
7
9abf7f305bac refactor
YU
parents: 5
diff changeset
50 message.offset = offset;
17
61d95bdc2bdb change Alice PUT API new Version
sugi
parents: 12
diff changeset
51 DataSegment.getLocal().put("aliceVNCMessage", message, null);
61d95bdc2bdb change Alice PUT API new Version
sugi
parents: 12
diff changeset
52 decode(renderer, rect, bytes, offset);
7
9abf7f305bac refactor
YU
parents: 5
diff changeset
53 }
9abf7f305bac refactor
YU
parents: 5
diff changeset
54
17
61d95bdc2bdb change Alice PUT API new Version
sugi
parents: 12
diff changeset
55 public void decode(Renderer renderer,
7
9abf7f305bac refactor
YU
parents: 5
diff changeset
56 FramebufferUpdateRectangle rect, byte[] bytes, int offset)
9abf7f305bac refactor
YU
parents: 5
diff changeset
57 throws TransportException {
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
58 int maxX = rect.x + rect.width;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
59 int maxY = rect.y + rect.height;
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
60 if (null == palette) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
61 palette = new int [128];
daa24f8a557b TightVNC original
YU
parents:
diff changeset
62 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
63 if (null == decodedBitmap) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
64 decodedBitmap = new int[MAX_TILE_SIZE * MAX_TILE_SIZE];
daa24f8a557b TightVNC original
YU
parents:
diff changeset
65 }
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
66 for (int tileY = rect.y; tileY < maxY; tileY += MAX_TILE_SIZE) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
67 int tileHeight = Math.min(maxY - tileY, MAX_TILE_SIZE);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
68
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
69 for (int tileX = rect.x; tileX < maxX; tileX += MAX_TILE_SIZE) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
70 int tileWidth = Math.min(maxX - tileX, MAX_TILE_SIZE);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
71 int subencoding = bytes[offset++] & 0x0ff;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
72 // 128 -plain RLE, 130-255 - Palette RLE
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
73 boolean isRle = (subencoding & 128) != 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
74 // 2 to 16 for raw packed palette data, 130 to 255 for Palette RLE (subencoding - 128)
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
75 int paletteSize = subencoding & 127;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
76 offset += readPalette(bytes, offset, renderer, paletteSize);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
77 if (1 == subencoding) { // A solid tile consisting of a single colour
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
78 renderer.fillRect(palette[0], tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
79 continue;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
80 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
81 if (isRle) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
82 if (0 == paletteSize) { // subencoding == 128 (or paletteSize == 0) - Plain RLE
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
83 offset += decodePlainRle(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
84 } else {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
85 offset += decodePaletteRle(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
86 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
87 } else {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
88 if (0 == paletteSize) { // subencoding == 0 (or paletteSize == 0) - raw CPIXEL data
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
89 offset += decodeRaw(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
90 } else {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
91 offset += decodePacked(bytes, offset, renderer, paletteSize, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
92 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
93 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
94 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
95 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
96 }
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
97
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
98 private int decodePlainRle(byte[] bytes, int offset, Renderer renderer,
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
99 int tileX, int tileY, int tileWidth, int tileHeight) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
100 int bytesPerCPixel = renderer.getBytesPerCPixel();
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
101 int decodedOffset = 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
102 int decodedEnd = tileWidth * tileHeight;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
103 int index = offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
104 while (decodedOffset < decodedEnd) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
105 int color = renderer.getCompactPixelColor(bytes, index);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
106 index += bytesPerCPixel;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
107 int rlength = 1;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
108 do {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
109 rlength += bytes[index] & 0x0ff;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
110 } while ((bytes[index++] & 0x0ff) == 255);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
111 assert rlength <= decodedEnd - decodedOffset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
112 renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, rlength, color);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
113 decodedOffset += rlength;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
114 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
115 renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
116 return index - offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
117 }
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
118
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
119 private int decodePaletteRle(byte[] bytes, int offset, Renderer renderer,
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
120 int tileX, int tileY, int tileWidth, int tileHeight) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
121 int decodedOffset = 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
122 int decodedEnd = tileWidth * tileHeight;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
123 int index = offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
124 while (decodedOffset < decodedEnd) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
125 int colorIndex = bytes[index++];
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
126 int color = palette[colorIndex & 127];
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
127 int rlength = 1;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
128 if ((colorIndex & 128) != 0) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
129 do {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
130 rlength += bytes[index] & 0x0ff;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
131 } while (bytes[index++] == (byte) 255);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
132 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
133 assert rlength <= decodedEnd - decodedOffset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
134 renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, rlength, color);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
135 decodedOffset += rlength;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
136 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
137 renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
138 return index - offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
139 }
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
140
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
141 private int decodePacked(byte[] bytes, int offset, Renderer renderer,
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
142 int paletteSize, int tileX, int tileY, int tileWidth, int tileHeight) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
143 int bitsPerPalletedPixel = paletteSize > 16 ? 8 : paletteSize > 4 ? 4
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
144 : paletteSize > 2 ? 2 : 1;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
145 int packedOffset = offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
146 int decodedOffset = 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
147 for (int i = 0; i < tileHeight; ++i) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
148 int decodedRowEnd = decodedOffset + tileWidth;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
149 int byteProcessed = 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
150 int bitsRemain = 0;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
151
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
152 while (decodedOffset < decodedRowEnd) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
153 if (bitsRemain == 0) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
154 byteProcessed = bytes[packedOffset++];
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
155 bitsRemain = 8;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
156 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
157 bitsRemain -= bitsPerPalletedPixel;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
158 int index = byteProcessed >> bitsRemain & (1 << bitsPerPalletedPixel) - 1 & 127;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
159 int color = palette[index];
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
160 renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, 1, color);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
161 ++decodedOffset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
162 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
163 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
164 renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
165 return packedOffset - offset;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
166 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
167
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
168 private int decodeRaw(byte[] bytes, int offset, Renderer renderer,
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
169 int tileX, int tileY, int tileWidth, int tileHeight) throws TransportException {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
170 return renderer.drawCompactBytes(bytes, offset, tileX, tileY, tileWidth, tileHeight);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
171 }
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
172
5
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
173 private int readPalette(byte[] bytes, int offset, Renderer renderer, int paletteSize) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
174 final int bytesPerCPixel = renderer.getBytesPerCPixel();
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
175 for (int i=0; i<paletteSize; ++i) {
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
176 palette[i] = renderer.getCompactPixelColor(bytes, offset + i* bytesPerCPixel);
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
177 }
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
178 return paletteSize * bytesPerCPixel;
3a5b44b08540 add Decode Method
YU
parents: 0
diff changeset
179 }
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
180 }