enikey | d336f40 | 2008-12-19 05:43:25 +0000 | [diff] [blame] | 1 | package com.tightvnc.decoder; |
| 2 | |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 3 | import com.tightvnc.vncviewer.RfbInputStream; |
| 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 { |
| 33 | int nSubrects = rfbis.readU32(); |
| 34 | byte[] bg_buf = new byte[bytesPerPixel]; |
| 35 | rfbis.readFully(bg_buf); |
| 36 | Color pixel; |
| 37 | if (bytesPerPixel == 1) { |
| 38 | pixel = getColor256()[bg_buf[0] & 0xFF]; |
| 39 | } else { |
| 40 | pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, bg_buf[0] & 0xFF); |
| 41 | } |
| 42 | graphics.setColor(pixel); |
| 43 | graphics.fillRect(x, y, w, h); |
| 44 | byte[] buf = new byte[nSubrects * (bytesPerPixel + 8)]; |
| 45 | rfbis.readFully(buf); |
| 46 | DataInputStream ds = new DataInputStream(new ByteArrayInputStream(buf)); |
| 47 | |
| 48 | // |
enikey | 8beb778 | 2008-12-24 04:37:56 +0000 | [diff] [blame] | 49 | // Save decoded data to data output stream |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 50 | // |
enikey | 8beb778 | 2008-12-24 04:37:56 +0000 | [diff] [blame] | 51 | if (dos != null) { |
| 52 | dos.writeInt(nSubrects); |
| 53 | dos.write(bg_buf); |
| 54 | dos.write(buf); |
enikey | ccf6e7c | 2008-12-19 06:00:18 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int sx, sy, sw, sh; |
| 58 | for (int j = 0; j < nSubrects; j++) { |
| 59 | if (bytesPerPixel == 1) { |
| 60 | pixel = getColor256()[ds.readUnsignedByte()]; |
| 61 | } else { |
| 62 | ds.skip(4); |
| 63 | pixel = new Color(buf[j*12+2] & 0xFF, |
| 64 | buf[j*12+1] & 0xFF, |
| 65 | buf[j*12] & 0xFF); |
| 66 | } |
| 67 | sx = x + ds.readUnsignedShort(); |
| 68 | sy = y + ds.readUnsignedShort(); |
| 69 | sw = ds.readUnsignedShort(); |
| 70 | sh = ds.readUnsignedShort(); |
| 71 | |
| 72 | graphics.setColor(pixel); |
| 73 | graphics.fillRect(sx, sy, sw, sh); |
| 74 | } |
| 75 | } |
enikey | d336f40 | 2008-12-19 05:43:25 +0000 | [diff] [blame] | 76 | } |