blob: 8a471a24bf77ec2b729abe18da60ef7208a17b52 [file] [log] [blame]
enikeybb9f0942008-12-19 06:21:34 +00001package com.tightvnc.decoder;
2
3import com.tightvnc.vncviewer.RfbInputStream;
4import java.awt.Graphics;
5import java.io.IOException;
6import java.util.zip.DataFormatException;
7import java.util.zip.Inflater;
8
9//
10// Class that used for decoding ZLib encoded data.
11//
12
13public class ZlibDecoder extends RawDecoder {
14
15 public ZlibDecoder(Graphics g, RfbInputStream is) {
16 super(g, is);
17 }
18
19 public ZlibDecoder(Graphics g, RfbInputStream is, int frameBufferW,
20 int frameBufferH) {
21 super(g, is, frameBufferW, frameBufferH);
22 }
23
24 //
25 // Override handleRect method to decode ZLib encoded data insted of
26 // raw pixel data.
27 //
28
29 public void handleRect(int x, int y, int w, int h) throws IOException {
30 int nBytes = rfbis.readU32();
31
32 if (zlibBuf == null || zlibBufLen < nBytes) {
33 zlibBufLen = nBytes * 2;
34 zlibBuf = new byte[zlibBufLen];
35 }
36
37 rfbis.readFully(zlibBuf, 0, nBytes);
38
39 //
40 // Save decoded data to RecordInterface
41 //
42
43 if (rec.canWrite() && rec.isRecordFromBeginning()) {
44 rec.writeIntBE(nBytes);
45 rec.write(zlibBuf, 0, nBytes);
46 }
47
48 if (zlibInflater == null) {
49 zlibInflater = new Inflater();
50 }
51 zlibInflater.setInput(zlibBuf, 0, nBytes);
52
53 try {
54 if (bytesPerPixel == 1) {
55 for (int dy = y; dy < y + h; dy++) {
56 zlibInflater.inflate(pixels8, dy * framebufferWidth + x, w);
57
58 //
59 // Save decoded data to RecordInterface
60 //
61
62 if (rec.canWrite() && !rec.isRecordFromBeginning())
63 rec.write(pixels8, dy * framebufferWidth + x, w);
64 }
65 } else {
66 byte[] buf = new byte[w * 4];
67 int i, offset;
68 for (int dy = y; dy < y + h; dy++) {
69 zlibInflater.inflate(buf);
70 offset = dy * framebufferWidth + x;
71 for (i = 0; i < w; i++) {
72 RawDecoder.pixels24[offset + i] =
73 (buf[i * 4 + 2] & 0xFF) << 16 |
74 (buf[i * 4 + 1] & 0xFF) << 8 |
75 (buf[i * 4] & 0xFF);
76 }
77
78 //
79 // Save decoded data to RecordInterface
80 //
81
82 if (rec.canWrite() && !rec.isRecordFromBeginning())
83 rec.write(buf);
84 }
85 }
86 } catch (DataFormatException ex) {
87 ex.printStackTrace();
88 }
89 handleUpdatedPixels(x, y, w, h);
90 }
91
92 //
93 // Zlib encoder's data.
94 //
95
96 protected byte[] zlibBuf;
97 protected int zlibBufLen = 0;
98 protected Inflater zlibInflater;
99}