enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 1 | package com.tightvnc.decoder; |
| 2 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 3 | import com.tightvnc.vncviewer.RecordInterface; |
| 4 | import com.tightvnc.vncviewer.RfbInputStream; |
enikey | 4f92da4 | 2008-12-19 05:28:12 +0000 | [diff] [blame] | 5 | import java.io.IOException; |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 6 | import java.awt.Graphics; |
| 7 | import java.awt.Image; |
| 8 | import java.awt.image.ColorModel; |
| 9 | import java.awt.image.DirectColorModel; |
| 10 | import java.awt.image.MemoryImageSource; |
| 11 | import java.awt.Color; |
enikey | 663025d | 2008-12-19 05:14:15 +0000 | [diff] [blame] | 12 | import java.awt.Toolkit; |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 13 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 14 | // |
| 15 | // This is base decoder class. |
| 16 | // Other classes will be childs of RawDecoder. |
| 17 | // |
enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 18 | public class RawDecoder { |
| 19 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 20 | public RawDecoder(Graphics g, RfbInputStream is) { |
| 21 | setGraphics(g); |
| 22 | setRfbInputStream(is); |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 23 | // FIXME: cm24 created in getColorModel24. |
| 24 | // Remove if no bugs |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 25 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 26 | } |
| 27 | |
| 28 | public RawDecoder(Graphics g, RfbInputStream is, int frameBufferW, |
| 29 | int frameBufferH) { |
| 30 | setGraphics(g); |
| 31 | setRfbInputStream(is); |
| 32 | setFrameBufferSize(frameBufferW, frameBufferH); |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 33 | // FIXME: cm24 created in getColorModel24. |
| 34 | // Remove if no bugs |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 35 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 36 | } |
| 37 | |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 38 | // |
| 39 | // Set methods to set value of non-static protected members of class |
| 40 | // |
| 41 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 42 | public void setRfbInputStream(RfbInputStream is) { |
| 43 | rfbis = is; |
| 44 | } |
| 45 | |
| 46 | public void setGraphics(Graphics g) { |
| 47 | graphics = g; |
| 48 | } |
| 49 | |
| 50 | public void setBPP(int bpp) { |
| 51 | bytesPerPixel = bpp; |
| 52 | } |
| 53 | |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 54 | public void setFrameBufferSize(int w, int h) { |
| 55 | framebufferWidth = w; |
| 56 | framebufferHeight = h; |
| 57 | } |
| 58 | |
enikey | 292cf9d | 2008-12-19 05:30:11 +0000 | [diff] [blame] | 59 | public void setSessionRecorder(RecordInterface ri) { |
| 60 | rec = ri; |
| 61 | } |
| 62 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 63 | // |
| 64 | // FIXME: This method may be useless in future, remove if so |
| 65 | // |
| 66 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 67 | public int getBPP() { |
| 68 | return bytesPerPixel; |
| 69 | } |
| 70 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 71 | // |
enikey | 4f92da4 | 2008-12-19 05:28:12 +0000 | [diff] [blame] | 72 | // Decodes Raw Pixels data and draw it into graphics |
| 73 | // |
| 74 | |
| 75 | public void handleRect(int x, int y, int w, int h) throws IOException, Exception { |
| 76 | if (bytesPerPixel == 1) { |
| 77 | for (int dy = y; dy < y + h; dy++) { |
| 78 | if (pixels8 != null) { |
| 79 | rfbis.readFully(pixels8, dy * framebufferWidth + x, w); |
| 80 | } |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 81 | // |
| 82 | // Save decoded data to RecordInterface |
| 83 | // |
enikey | 4f92da4 | 2008-12-19 05:28:12 +0000 | [diff] [blame] | 84 | if (rec.canWrite()) { |
| 85 | rec.write(pixels8, dy * framebufferWidth + x, w); |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | byte[] buf = new byte[w * 4]; |
| 90 | int i, offset; |
| 91 | for (int dy = y; dy < y + h; dy++) { |
| 92 | rfbis.readFully(buf); |
| 93 | // |
| 94 | // Save decoded data to RecordInterface |
| 95 | // |
| 96 | if (rec.canWrite()) { |
| 97 | rec.write(buf); |
| 98 | } |
| 99 | offset = dy * framebufferWidth + x; |
| 100 | if (pixels24 != null) { |
| 101 | for (i = 0; i < w; i++) { |
| 102 | pixels24[offset + i] = |
| 103 | (buf[i * 4 + 2] & 0xFF) << 16 | |
| 104 | (buf[i * 4 + 1] & 0xFF) << 8 | |
| 105 | (buf[i * 4] & 0xFF); |
| 106 | } //for |
| 107 | } // if |
| 108 | } // for |
| 109 | } // else |
| 110 | handleUpdatedPixels(x, y, w, h); |
| 111 | } // void |
| 112 | |
| 113 | // |
| 114 | // Display newly updated area of pixels. |
| 115 | // |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 116 | |
enikey | 4f92da4 | 2008-12-19 05:28:12 +0000 | [diff] [blame] | 117 | protected void handleUpdatedPixels(int x, int y, int w, int h) { |
| 118 | // Draw updated pixels of the off-screen image. |
| 119 | pixelsSource.newPixels(x, y, w, h); |
| 120 | graphics.setClip(x, y, w, h); |
| 121 | graphics.drawImage(rawPixelsImage, 0, 0, null); |
| 122 | graphics.setClip(0, 0, framebufferWidth, framebufferHeight); |
| 123 | } |
| 124 | |
| 125 | // |
enikey | 663025d | 2008-12-19 05:14:15 +0000 | [diff] [blame] | 126 | // Updates pixels data. |
enikey | 4f92da4 | 2008-12-19 05:28:12 +0000 | [diff] [blame] | 127 | // This method must be called when framebuffer is resized |
enikey | 663025d | 2008-12-19 05:14:15 +0000 | [diff] [blame] | 128 | // or BPP is changed. |
| 129 | // |
| 130 | |
| 131 | public void update() { |
| 132 | // Images with raw pixels should be re-allocated on every change |
| 133 | // of geometry or pixel format. |
| 134 | int fbWidth = framebufferWidth; |
| 135 | int fbHeight = framebufferHeight; |
| 136 | |
| 137 | if (bytesPerPixel == 1) { |
| 138 | pixels24 = null; |
| 139 | pixels8 = new byte[fbWidth * fbHeight]; |
| 140 | pixelsSource = new MemoryImageSource(fbWidth, fbHeight, getColorModel8(), |
| 141 | pixels8, 0, fbWidth); |
| 142 | } else { |
| 143 | pixels8 = null; |
| 144 | pixels24 = new int[fbWidth * fbHeight]; |
| 145 | pixelsSource = |
| 146 | new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth); |
| 147 | } |
| 148 | pixelsSource.setAnimated(true); |
| 149 | rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource); |
| 150 | } |
| 151 | |
| 152 | // |
enikey | 1924c44 | 2008-12-19 05:36:36 +0000 | [diff] [blame^] | 153 | // Private static members access methods |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 154 | // |
| 155 | |
| 156 | protected ColorModel getColorModel8() { |
| 157 | if (cm8 == null) { |
| 158 | cm8 = cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6)); |
| 159 | } |
| 160 | return cm8; |
| 161 | } |
| 162 | |
| 163 | protected ColorModel getColorModel24() { |
| 164 | if (cm24 == null) { |
| 165 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 166 | } |
| 167 | return cm24; |
| 168 | } |
| 169 | |
| 170 | protected Color[]getColor256() { |
| 171 | if (color256 == null) { |
| 172 | color256 = new Color[256]; |
| 173 | for (int i = 0; i < 256; i++) |
| 174 | color256[i] = new Color(cm8.getRGB(i)); |
| 175 | } |
| 176 | return color256; |
| 177 | } |
| 178 | |
| 179 | // |
| 180 | // Unique data for every decoder (? maybe not ?) |
| 181 | // |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 182 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 183 | protected int bytesPerPixel = 4; |
| 184 | protected int framebufferWidth = 0; |
| 185 | protected int framebufferHeight = 0; |
| 186 | protected RfbInputStream rfbis = null; |
| 187 | protected Graphics graphics = null; |
| 188 | protected RecordInterface rec = null; |
| 189 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 190 | // |
| 191 | // This data must be shared between decoders |
| 192 | // |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 193 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 194 | protected static byte []pixels8 = null; |
| 195 | protected static int []pixels24 = null; |
| 196 | protected static MemoryImageSource pixelsSource = null; |
| 197 | protected static Image rawPixelsImage = null; |
| 198 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 199 | // |
| 200 | // Access to this static members only though protected methods |
| 201 | // |
enikey | 2c23ba1 | 2008-12-19 05:34:17 +0000 | [diff] [blame] | 202 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 203 | private static ColorModel cm8 = null; |
| 204 | private static ColorModel cm24 = null; |
| 205 | private static Color []color256 = null; |
enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 206 | } |