blob: f6a67aece9ae9dfc48be411c662ba490c3b34ca2 [file] [log] [blame]
Adam Tkacf53e62a2009-03-13 13:20:26 +00001package com.tigervnc.decoder;
enikeyd336f402008-12-19 05:43:25 +00002
Adam Tkacf53e62a2009-03-13 13:20:26 +00003import com.tigervnc.vncviewer.RfbInputStream;
enikeyccf6e7c2008-12-19 06:00:18 +00004import java.awt.Graphics;
5import java.awt.Color;
6import java.io.ByteArrayInputStream;
7import java.io.DataInputStream;
8import java.io.IOException;
enikeyd336f402008-12-19 05:43:25 +00009
enikeyccf6e7c2008-12-19 06:00:18 +000010//
11// Class that used for decoding RRE encoded data.
12//
13
14public class RREDecoder extends RawDecoder {
15
enikey6558a302008-12-24 05:14:30 +000016 final static int EncodingRRE = 2;
17
enikeyccf6e7c2008-12-19 06:00:18 +000018 public RREDecoder(Graphics g, RfbInputStream is) {
19 super(g, is);
20 }
21
22 public RREDecoder(Graphics g, RfbInputStream is, int frameBufferW,
23 int frameBufferH) {
24 super(g, is, frameBufferW, frameBufferH);
25 }
26
27 //
28 // Override handleRect method to decode RRE encoded data insted of
29 // raw pixel data.
30 //
31
32 public void handleRect(int x, int y, int w, int h) throws IOException {
enikey2f0294e2008-12-24 08:18:54 +000033
34 //
35 // Write encoding ID to record output stream
36 //
37
38 if (dos != null) {
39 dos.writeInt(RREDecoder.EncodingRRE);
40 }
41
enikeyccf6e7c2008-12-19 06:00:18 +000042 int nSubrects = rfbis.readU32();
43 byte[] bg_buf = new byte[bytesPerPixel];
44 rfbis.readFully(bg_buf);
45 Color pixel;
46 if (bytesPerPixel == 1) {
47 pixel = getColor256()[bg_buf[0] & 0xFF];
48 } else {
49 pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, bg_buf[0] & 0xFF);
50 }
51 graphics.setColor(pixel);
52 graphics.fillRect(x, y, w, h);
53 byte[] buf = new byte[nSubrects * (bytesPerPixel + 8)];
54 rfbis.readFully(buf);
55 DataInputStream ds = new DataInputStream(new ByteArrayInputStream(buf));
56
57 //
enikey8beb7782008-12-24 04:37:56 +000058 // Save decoded data to data output stream
enikeyccf6e7c2008-12-19 06:00:18 +000059 //
enikey8beb7782008-12-24 04:37:56 +000060 if (dos != null) {
61 dos.writeInt(nSubrects);
62 dos.write(bg_buf);
63 dos.write(buf);
enikeyccf6e7c2008-12-19 06:00:18 +000064 }
65
66 int sx, sy, sw, sh;
67 for (int j = 0; j < nSubrects; j++) {
68 if (bytesPerPixel == 1) {
69 pixel = getColor256()[ds.readUnsignedByte()];
70 } else {
71 ds.skip(4);
72 pixel = new Color(buf[j*12+2] & 0xFF,
73 buf[j*12+1] & 0xFF,
74 buf[j*12] & 0xFF);
75 }
76 sx = x + ds.readUnsignedShort();
77 sy = y + ds.readUnsignedShort();
78 sw = ds.readUnsignedShort();
79 sh = ds.readUnsignedShort();
80
81 graphics.setColor(pixel);
82 graphics.fillRect(sx, sy, sw, sh);
83 }
84 }
enikeyd336f402008-12-19 05:43:25 +000085}