Adam Tkac | f53e62a | 2009-03-13 13:20:26 +0000 | [diff] [blame] | 1 | package com.tigervnc.decoder; |
enikey | d336f40 | 2008-12-19 05:43:25 +0000 | [diff] [blame] | 2 | |
Adam Tkac | f53e62a | 2009-03-13 13:20:26 +0000 | [diff] [blame] | 3 | import com.tigervnc.vncviewer.RfbInputStream; |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 4 | import java.awt.Graphics; |
| 5 | import java.awt.Color; |
| 6 | import java.io.ByteArrayInputStream; |
| 7 | import java.io.DataInputStream; |
| 8 | import java.io.IOException; |
enikey | d336f40 | 2008-12-19 05:43:25 +0000 | [diff] [blame] | 9 | |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 10 | // |
| 11 | // Class that used for decoding RRE encoded data. |
| 12 | // |
| 13 | |
| 14 | public class RREDecoder extends RawDecoder { |
| 15 | |
enikey | 6558a30 | 2008-12-24 05:14:30 +0000 | [diff] [blame] | 16 | final static int EncodingRRE = 2; |
| 17 | |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 18 | 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 { |
enikey | 2f0294e | 2008-12-24 08:18:54 +0000 | [diff] [blame] | 33 | |
| 34 | // |
| 35 | // Write encoding ID to record output stream |
| 36 | // |
| 37 | |
| 38 | if (dos != null) { |
| 39 | dos.writeInt(RREDecoder.EncodingRRE); |
| 40 | } |
| 41 | |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 42 | 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 | // |
enikey | 8beb778 | 2008-12-24 04:37:56 +0000 | [diff] [blame] | 58 | // Save decoded data to data output stream |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 59 | // |
enikey | 8beb778 | 2008-12-24 04:37:56 +0000 | [diff] [blame] | 60 | if (dos != null) { |
| 61 | dos.writeInt(nSubrects); |
| 62 | dos.write(bg_buf); |
| 63 | dos.write(buf); |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 64 | } |
| 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 | } |
enikey | d336f40 | 2008-12-19 05:43:25 +0000 | [diff] [blame] | 85 | } |