blob: 9a167b11312b7ecadb046e6c06f0a4542cdc4780 [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
enikey6558a302008-12-24 05:14:30 +000015 final static int EncodingZlib = 6;
16
enikeybb9f0942008-12-19 06:21:34 +000017 public ZlibDecoder(Graphics g, RfbInputStream is) {
18 super(g, is);
19 }
20
21 public ZlibDecoder(Graphics g, RfbInputStream is, int frameBufferW,
22 int frameBufferH) {
23 super(g, is, frameBufferW, frameBufferH);
24 }
25
26 //
27 // Override handleRect method to decode ZLib encoded data insted of
28 // raw pixel data.
29 //
30
31 public void handleRect(int x, int y, int w, int h) throws IOException {
32 int nBytes = rfbis.readU32();
33
34 if (zlibBuf == null || zlibBufLen < nBytes) {
35 zlibBufLen = nBytes * 2;
36 zlibBuf = new byte[zlibBufLen];
37 }
38
39 rfbis.readFully(zlibBuf, 0, nBytes);
40
41 //
enikey6558a302008-12-24 05:14:30 +000042 // FIXME: Now we think that isRecordFromBeggining
43 // always returns false and this part of code will be never
44 // executed.
enikeybb9f0942008-12-19 06:21:34 +000045 //
46
enikey6558a302008-12-24 05:14:30 +000047 //if (rec.canWrite() && rec.isRecordFromBeginning()) {
48 // rec.writeIntBE(nBytes);
49 // rec.write(zlibBuf, 0, nBytes);
50 //}
enikeybb9f0942008-12-19 06:21:34 +000051
52 if (zlibInflater == null) {
53 zlibInflater = new Inflater();
54 }
55 zlibInflater.setInput(zlibBuf, 0, nBytes);
56
57 try {
58 if (bytesPerPixel == 1) {
59 for (int dy = y; dy < y + h; dy++) {
60 zlibInflater.inflate(pixels8, dy * framebufferWidth + x, w);
61
62 //
enikey6558a302008-12-24 05:14:30 +000063 // Save decoded data to data output stream
enikeybb9f0942008-12-19 06:21:34 +000064 //
65
enikey6558a302008-12-24 05:14:30 +000066 //if (rec.canWrite() && !rec.isRecordFromBeginning())
67 //if (dos != null)
68 // dos.write(pixels8, dy * framebufferWidth + x, w);
enikeybb9f0942008-12-19 06:21:34 +000069 }
70 } else {
71 byte[] buf = new byte[w * 4];
72 int i, offset;
73 for (int dy = y; dy < y + h; dy++) {
74 zlibInflater.inflate(buf);
75 offset = dy * framebufferWidth + x;
76 for (i = 0; i < w; i++) {
77 RawDecoder.pixels24[offset + i] =
78 (buf[i * 4 + 2] & 0xFF) << 16 |
79 (buf[i * 4 + 1] & 0xFF) << 8 |
80 (buf[i * 4] & 0xFF);
81 }
82
83 //
enikey6558a302008-12-24 05:14:30 +000084 // Save decoded data to data output stream
enikeybb9f0942008-12-19 06:21:34 +000085 //
86
enikey6558a302008-12-24 05:14:30 +000087 //if (rec.canWrite() && !rec.isRecordFromBeginning())
88 //if (dos != null)
89 // dos.write(buf);
enikeybb9f0942008-12-19 06:21:34 +000090 }
91 }
92 } catch (DataFormatException ex) {
93 ex.printStackTrace();
94 }
95 handleUpdatedPixels(x, y, w, h);
96 }
97
98 //
99 // Zlib encoder's data.
100 //
101
102 protected byte[] zlibBuf;
103 protected int zlibBufLen = 0;
104 protected Inflater zlibInflater;
105}