blob: 0eeaef5f30781efaecde642bdc69f2e2e47559d4 [file] [log] [blame]
Adam Tkacf53e62a2009-03-13 13:20:26 +00001package com.tigervnc.decoder;
enikeybb9f0942008-12-19 06:21:34 +00002
Adam Tkacf53e62a2009-03-13 13:20:26 +00003import com.tigervnc.vncviewer.RfbInputStream;
enikeybb9f0942008-12-19 06:21:34 +00004import 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 {
enikey2f0294e2008-12-24 08:18:54 +000032
33 //
34 // Write encoding ID to record output stream.
35 // Remark: we forced changed encoding from zlib to raw
36 // cause at this moment we cannot save data in zlib encoding.
37 //
38
39 if (dos != null) {
40 dos.writeInt(RawDecoder.EncodingRaw);
41 }
42
enikeybb9f0942008-12-19 06:21:34 +000043 int nBytes = rfbis.readU32();
44
45 if (zlibBuf == null || zlibBufLen < nBytes) {
46 zlibBufLen = nBytes * 2;
47 zlibBuf = new byte[zlibBufLen];
48 }
49
50 rfbis.readFully(zlibBuf, 0, nBytes);
51
enikeybb9f0942008-12-19 06:21:34 +000052 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 //
enikey2f0294e2008-12-24 08:18:54 +000063 // Save decoded raw data to data output stream
enikeybb9f0942008-12-19 06:21:34 +000064 //
65
enikey2f0294e2008-12-24 08:18:54 +000066 if (dos != null)
67 dos.write(pixels8, dy * framebufferWidth + x, w);
enikeybb9f0942008-12-19 06:21:34 +000068 }
69 } else {
70 byte[] buf = new byte[w * 4];
71 int i, offset;
72 for (int dy = y; dy < y + h; dy++) {
73 zlibInflater.inflate(buf);
74 offset = dy * framebufferWidth + x;
75 for (i = 0; i < w; i++) {
76 RawDecoder.pixels24[offset + i] =
77 (buf[i * 4 + 2] & 0xFF) << 16 |
78 (buf[i * 4 + 1] & 0xFF) << 8 |
79 (buf[i * 4] & 0xFF);
80 }
81
82 //
enikey2f0294e2008-12-24 08:18:54 +000083 // Save decoded raw data to data output stream
enikeybb9f0942008-12-19 06:21:34 +000084 //
85
enikey2f0294e2008-12-24 08:18:54 +000086 if (dos != null)
87 dos.write(buf);
enikeybb9f0942008-12-19 06:21:34 +000088 }
89 }
90 } catch (DataFormatException ex) {
91 ex.printStackTrace();
92 }
93 handleUpdatedPixels(x, y, w, h);
94 }
95
96 //
97 // Zlib encoder's data.
98 //
99
100 protected byte[] zlibBuf;
101 protected int zlibBufLen = 0;
102 protected Inflater zlibInflater;
103}