Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved. |
| 3 | // Copyright (C) 2001 Constantin Kaplinsky. All Rights Reserved. |
| 4 | // Copyright (C) 2000 Tridia Corporation. All Rights Reserved. |
| 5 | // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 6 | // |
| 7 | // This is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 2 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // This software is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this software; if not, write to the Free Software |
| 19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 20 | // USA. |
| 21 | // |
| 22 | |
| 23 | import java.awt.*; |
| 24 | import java.awt.event.*; |
| 25 | import java.awt.image.*; |
| 26 | import java.io.*; |
| 27 | import java.lang.*; |
| 28 | import java.util.zip.*; |
| 29 | |
| 30 | |
| 31 | // |
| 32 | // VncCanvas is a subclass of Canvas which draws a VNC desktop on it. |
| 33 | // |
| 34 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 35 | class VncCanvas extends Canvas { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 36 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 37 | RfbPlayer player; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 38 | RfbProto rfb; |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 39 | ColorModel cm24; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 40 | |
| 41 | Image memImage; |
| 42 | Graphics memGraphics; |
| 43 | |
| 44 | Image rawPixelsImage; |
| 45 | MemoryImageSource pixelsSource; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 46 | int[] pixels24; |
| 47 | |
| 48 | // Zlib encoder's data. |
| 49 | byte[] zlibBuf; |
| 50 | int zlibBufLen = 0; |
| 51 | Inflater zlibInflater; |
| 52 | |
| 53 | // Tight encoder's data. |
| 54 | final static int tightZlibBufferSize = 512; |
| 55 | Inflater[] tightInflaters; |
| 56 | |
| 57 | // Since JPEG images are loaded asynchronously, we have to remember |
| 58 | // their position in the framebuffer. Also, this jpegRect object is |
| 59 | // used for synchronization between the rfbThread and a JVM's thread |
| 60 | // which decodes and loads JPEG images. |
| 61 | Rectangle jpegRect; |
| 62 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 63 | // |
| 64 | // The constructor. |
| 65 | // |
| 66 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 67 | VncCanvas(RfbPlayer player) throws IOException { |
| 68 | this.player = player; |
| 69 | rfb = player.rfb; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 70 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 71 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 72 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 73 | updateFramebufferSize(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // |
| 77 | // Callback methods to determine geometry of our Component. |
| 78 | // |
| 79 | |
| 80 | public Dimension getPreferredSize() { |
| 81 | return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight); |
| 82 | } |
| 83 | |
| 84 | public Dimension getMinimumSize() { |
| 85 | return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight); |
| 86 | } |
| 87 | |
| 88 | public Dimension getMaximumSize() { |
| 89 | return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight); |
| 90 | } |
| 91 | |
| 92 | // |
| 93 | // All painting is performed here. |
| 94 | // |
| 95 | |
| 96 | public void update(Graphics g) { |
| 97 | paint(g); |
| 98 | } |
| 99 | |
| 100 | public void paint(Graphics g) { |
| 101 | synchronized(memImage) { |
| 102 | g.drawImage(memImage, 0, 0, null); |
| 103 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // |
| 107 | // Override the ImageObserver interface method to handle drawing of |
| 108 | // JPEG-encoded data. |
| 109 | // |
| 110 | |
| 111 | public boolean imageUpdate(Image img, int infoflags, |
| 112 | int x, int y, int width, int height) { |
| 113 | if ((infoflags & (ALLBITS | ABORT)) == 0) { |
| 114 | return true; // We need more image data. |
| 115 | } else { |
| 116 | // If the whole image is available, draw it now. |
| 117 | if ((infoflags & ALLBITS) != 0) { |
| 118 | if (jpegRect != null) { |
| 119 | synchronized(jpegRect) { |
| 120 | memGraphics.drawImage(img, jpegRect.x, jpegRect.y, null); |
| 121 | scheduleRepaint(jpegRect.x, jpegRect.y, |
| 122 | jpegRect.width, jpegRect.height); |
| 123 | jpegRect.notify(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return false; // All image data was processed. |
| 128 | } |
| 129 | } |
| 130 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 131 | void updateFramebufferSize() { |
| 132 | |
| 133 | // Useful shortcuts. |
| 134 | int fbWidth = rfb.framebufferWidth; |
| 135 | int fbHeight = rfb.framebufferHeight; |
| 136 | |
| 137 | // Create new off-screen image either if it does not exist, or if |
| 138 | // its geometry should be changed. It's not necessary to replace |
| 139 | // existing image if only pixel format should be changed. |
| 140 | if (memImage == null) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 141 | memImage = player.createImage(fbWidth, fbHeight); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 142 | memGraphics = memImage.getGraphics(); |
| 143 | } else if (memImage.getWidth(null) != fbWidth || |
| 144 | memImage.getHeight(null) != fbHeight) { |
| 145 | synchronized(memImage) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 146 | memImage = player.createImage(fbWidth, fbHeight); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 147 | memGraphics = memImage.getGraphics(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Images with raw pixels should be re-allocated on every change |
| 152 | // of geometry or pixel format. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 153 | pixels24 = new int[fbWidth * fbHeight]; |
| 154 | pixelsSource = |
| 155 | new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 156 | pixelsSource.setAnimated(true); |
| 157 | rawPixelsImage = createImage(pixelsSource); |
| 158 | |
| 159 | // Update the size of desktop containers. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 160 | if (player.inSeparateFrame) { |
| 161 | if (player.desktopScrollPane != null) |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 162 | resizeDesktopFrame(); |
| 163 | } else { |
| 164 | setSize(fbWidth, fbHeight); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void resizeDesktopFrame() { |
| 169 | setSize(rfb.framebufferWidth, rfb.framebufferHeight); |
| 170 | |
| 171 | // FIXME: Find a better way to determine correct size of a |
| 172 | // ScrollPane. -- const |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 173 | Insets insets = player.desktopScrollPane.getInsets(); |
| 174 | player.desktopScrollPane.setSize(rfb.framebufferWidth + |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 175 | 2 * Math.min(insets.left, insets.right), |
| 176 | rfb.framebufferHeight + |
| 177 | 2 * Math.min(insets.top, insets.bottom)); |
| 178 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 179 | player.vncFrame.pack(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 180 | |
| 181 | // Try to limit the frame size to the screen size. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 182 | Dimension screenSize = player.vncFrame.getToolkit().getScreenSize(); |
| 183 | Dimension frameSize = player.vncFrame.getSize(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 184 | Dimension newSize = frameSize; |
| 185 | boolean needToResizeFrame = false; |
| 186 | if (frameSize.height > screenSize.height) { |
| 187 | newSize.height = screenSize.height; |
| 188 | needToResizeFrame = true; |
| 189 | } |
| 190 | if (frameSize.width > screenSize.width) { |
| 191 | newSize.width = screenSize.width; |
| 192 | needToResizeFrame = true; |
| 193 | } |
| 194 | if (needToResizeFrame) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 195 | player.vncFrame.setSize(newSize); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 198 | player.desktopScrollPane.doLayout(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // |
| 202 | // processNormalProtocol() - executed by the rfbThread to deal with the |
| 203 | // RFB socket. |
| 204 | // |
| 205 | |
| 206 | public void processNormalProtocol() throws IOException { |
| 207 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 208 | zlibInflater = new Inflater(); |
| 209 | tightInflaters = new Inflater[4]; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 210 | |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 211 | player.updatePos(); |
| 212 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 213 | // Main dispatch loop. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 214 | |
| 215 | while (true) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 216 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 217 | int msgType = rfb.readServerMessageType(); |
| 218 | |
| 219 | switch (msgType) { |
| 220 | case RfbProto.FramebufferUpdate: |
| 221 | rfb.readFramebufferUpdate(); |
| 222 | |
| 223 | for (int i = 0; i < rfb.updateNRects; i++) { |
| 224 | rfb.readFramebufferUpdateRectHdr(); |
| 225 | |
| 226 | if (rfb.updateRectEncoding == rfb.EncodingLastRect) |
| 227 | break; |
| 228 | |
| 229 | if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) { |
| 230 | rfb.setFramebufferSize(rfb.updateRectW, rfb.updateRectH); |
| 231 | updateFramebufferSize(); |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | if (rfb.updateRectEncoding == rfb.EncodingXCursor || |
| 236 | rfb.updateRectEncoding == rfb.EncodingRichCursor) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 237 | throw new IOException("Sorry, no support for" + |
| 238 | " cursor shape updates yet"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | switch (rfb.updateRectEncoding) { |
| 242 | |
| 243 | case RfbProto.EncodingRaw: |
| 244 | { |
| 245 | handleRawRect(rfb.updateRectX, rfb.updateRectY, |
| 246 | rfb.updateRectW, rfb.updateRectH); |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | case RfbProto.EncodingCopyRect: |
| 251 | { |
| 252 | rfb.readCopyRect(); |
| 253 | |
| 254 | int sx = rfb.copyRectSrcX, sy = rfb.copyRectSrcY; |
| 255 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 256 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
| 257 | |
| 258 | memGraphics.copyArea(sx, sy, rw, rh, rx - sx, ry - sy); |
| 259 | |
| 260 | scheduleRepaint(rx, ry, rw, rh); |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | case RfbProto.EncodingRRE: |
| 265 | { |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 266 | byte[] buf = new byte[4]; |
| 267 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 268 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 269 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
| 270 | int nSubrects = rfb.is.readInt(); |
| 271 | int x, y, w, h; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 272 | |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 273 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 274 | Color pixel = new Color(buf[2] & 0xFF, |
| 275 | buf[1] & 0xFF, |
| 276 | buf[0] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 277 | memGraphics.setColor(pixel); |
| 278 | memGraphics.fillRect(rx, ry, rw, rh); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 279 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 280 | for (int j = 0; j < nSubrects; j++) { |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 281 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 282 | pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 283 | x = rx + rfb.is.readUnsignedShort(); |
| 284 | y = ry + rfb.is.readUnsignedShort(); |
| 285 | w = rfb.is.readUnsignedShort(); |
| 286 | h = rfb.is.readUnsignedShort(); |
| 287 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 288 | memGraphics.setColor(pixel); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 289 | memGraphics.fillRect(x, y, w, h); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | scheduleRepaint(rx, ry, rw, rh); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | case RfbProto.EncodingCoRRE: |
| 297 | { |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 298 | byte[] buf = new byte[4]; |
| 299 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 300 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 301 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
| 302 | int nSubrects = rfb.is.readInt(); |
| 303 | int x, y, w, h; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 304 | |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 305 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 306 | Color pixel = new Color(buf[2] & 0xFF, |
| 307 | buf[1] & 0xFF, |
| 308 | buf[0] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 309 | memGraphics.setColor(pixel); |
| 310 | memGraphics.fillRect(rx, ry, rw, rh); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 311 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 312 | for (int j = 0; j < nSubrects; j++) { |
Constantin Kaplinsky | 2258f12 | 2002-05-20 13:10:44 +0000 | [diff] [blame] | 313 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 314 | pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 315 | x = rx + rfb.is.readUnsignedByte(); |
| 316 | y = ry + rfb.is.readUnsignedByte(); |
| 317 | w = rfb.is.readUnsignedByte(); |
| 318 | h = rfb.is.readUnsignedByte(); |
| 319 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 320 | memGraphics.setColor(pixel); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 321 | memGraphics.fillRect(x, y, w, h); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | scheduleRepaint(rx, ry, rw, rh); |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | case RfbProto.EncodingHextile: |
| 329 | { |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 330 | byte[] buf = new byte[256 * 4]; |
| 331 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 332 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 333 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 334 | Color bg = new Color(0, 0, 0), fg = new Color(0, 0, 0); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 335 | |
| 336 | for (int ty = ry; ty < ry + rh; ty += 16) { |
| 337 | |
| 338 | int th = 16; |
| 339 | if (ry + rh - ty < 16) |
| 340 | th = ry + rh - ty; |
| 341 | |
| 342 | for (int tx = rx; tx < rx + rw; tx += 16) { |
| 343 | |
| 344 | int tw = 16; |
| 345 | if (rx + rw - tx < 16) |
| 346 | tw = rx + rw - tx; |
| 347 | |
| 348 | int subencoding = rfb.is.readUnsignedByte(); |
| 349 | |
| 350 | // Is it a raw-encoded sub-rectangle? |
| 351 | if ((subencoding & rfb.HextileRaw) != 0) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 352 | int count, offset; |
| 353 | for (int j = ty; j < ty + th; j++) { |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 354 | rfb.is.readFully(buf, 0, tw * 4); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 355 | offset = j * rfb.framebufferWidth + tx; |
| 356 | for (count = 0; count < tw; count++) { |
| 357 | pixels24[offset + count] = |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 358 | (buf[count * 4 + 2] & 0xFF) << 16 | |
| 359 | (buf[count * 4 + 1] & 0xFF) << 8 | |
| 360 | (buf[count * 4] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | handleUpdatedPixels(tx, ty, tw, th); |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | // Read and draw the background if specified. |
| 368 | if ((subencoding & rfb.HextileBackgroundSpecified) != 0) { |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 369 | rfb.is.readFully(buf, 0, 4); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 370 | bg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 371 | } |
| 372 | memGraphics.setColor(bg); |
| 373 | memGraphics.fillRect(tx, ty, tw, th); |
| 374 | |
| 375 | // Read the foreground color if specified. |
| 376 | if ((subencoding & rfb.HextileForegroundSpecified) != 0) { |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 377 | rfb.is.readFully(buf, 0, 4); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 378 | fg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Done with this tile if there is no sub-rectangles. |
| 382 | if ((subencoding & rfb.HextileAnySubrects) == 0) |
| 383 | continue; |
| 384 | |
| 385 | int nSubrects = rfb.is.readUnsignedByte(); |
| 386 | |
| 387 | int b1, b2, sx, sy, sw, sh; |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 388 | if ((subencoding & rfb.HextileSubrectsColoured) != 0) { |
| 389 | for (int j = 0; j < nSubrects; j++) { |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 390 | rfb.is.readFully(buf, 0, 4); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 391 | fg = new Color(buf[2] & 0xFF, |
| 392 | buf[1] & 0xFF, |
| 393 | buf[0] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 394 | b1 = rfb.is.readUnsignedByte(); |
| 395 | b2 = rfb.is.readUnsignedByte(); |
| 396 | sx = tx + (b1 >> 4); |
| 397 | sy = ty + (b1 & 0xf); |
| 398 | sw = (b2 >> 4) + 1; |
| 399 | sh = (b2 & 0xf) + 1; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 400 | memGraphics.setColor(fg); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 401 | memGraphics.fillRect(sx, sy, sw, sh); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 402 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 403 | } else { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 404 | memGraphics.setColor(fg); |
| 405 | for (int j = 0; j < nSubrects; j++) { |
| 406 | b1 = rfb.is.readUnsignedByte(); |
| 407 | b2 = rfb.is.readUnsignedByte(); |
| 408 | sx = tx + (b1 >> 4); |
| 409 | sy = ty + (b1 & 0xf); |
| 410 | sw = (b2 >> 4) + 1; |
| 411 | sh = (b2 & 0xf) + 1; |
| 412 | memGraphics.fillRect(sx, sy, sw, sh); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 413 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | } |
| 417 | // Finished with a row of tiles, now let's show it. |
| 418 | scheduleRepaint(rx, ty, rw, th); |
| 419 | } |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | case RfbProto.EncodingZlib: |
| 424 | { |
| 425 | int nBytes = rfb.is.readInt(); |
| 426 | |
| 427 | if (zlibBuf == null || zlibBufLen < nBytes) { |
| 428 | zlibBufLen = nBytes * 2; |
| 429 | zlibBuf = new byte[zlibBufLen]; |
| 430 | } |
| 431 | |
| 432 | rfb.is.readFully(zlibBuf, 0, nBytes); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 433 | zlibInflater.setInput(zlibBuf, 0, nBytes); |
| 434 | |
| 435 | handleZlibRect(rfb.updateRectX, rfb.updateRectY, |
| 436 | rfb.updateRectW, rfb.updateRectH); |
| 437 | |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | case RfbProto.EncodingTight: |
| 442 | { |
| 443 | handleTightRect(rfb.updateRectX, rfb.updateRectY, |
| 444 | rfb.updateRectW, rfb.updateRectH); |
| 445 | |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | default: |
| 450 | throw new IOException("Unknown RFB rectangle encoding " + |
| 451 | rfb.updateRectEncoding); |
| 452 | } |
| 453 | |
| 454 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case RfbProto.SetColourMapEntries: |
| 458 | throw new IOException("Can't handle SetColourMapEntries message"); |
| 459 | |
| 460 | case RfbProto.Bell: |
| 461 | Toolkit.getDefaultToolkit().beep(); |
| 462 | break; |
| 463 | |
| 464 | case RfbProto.ServerCutText: |
| 465 | String s = rfb.readServerCutText(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 466 | break; |
| 467 | |
| 468 | default: |
| 469 | throw new IOException("Unknown RFB message type " + msgType); |
| 470 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 471 | |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 472 | player.updatePos(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | // |
| 478 | // Handle a raw rectangle. |
| 479 | // |
| 480 | |
| 481 | void handleRawRect(int x, int y, int w, int h) throws IOException { |
| 482 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 483 | byte[] buf = new byte[w * 4]; |
| 484 | int i, offset; |
| 485 | for (int dy = y; dy < y + h; dy++) { |
| 486 | rfb.is.readFully(buf); |
| 487 | offset = dy * rfb.framebufferWidth + x; |
| 488 | for (i = 0; i < w; i++) { |
| 489 | pixels24[offset + i] = |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 490 | (buf[i * 4 + 2] & 0xFF) << 16 | |
| 491 | (buf[i * 4 + 1] & 0xFF) << 8 | |
| 492 | (buf[i * 4] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | handleUpdatedPixels(x, y, w, h); |
| 497 | scheduleRepaint(x, y, w, h); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | // |
| 502 | // Handle a Zlib-encoded rectangle. |
| 503 | // |
| 504 | |
| 505 | void handleZlibRect(int x, int y, int w, int h) |
| 506 | throws IOException { |
| 507 | |
| 508 | try { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 509 | byte[] buf = new byte[w * 4]; |
| 510 | int i, offset; |
| 511 | for (int dy = y; dy < y + h; dy++) { |
| 512 | zlibInflater.inflate(buf); |
| 513 | offset = dy * rfb.framebufferWidth + x; |
| 514 | for (i = 0; i < w; i++) { |
| 515 | pixels24[offset + i] = |
Constantin Kaplinsky | a5fcd98 | 2002-05-20 13:06:33 +0000 | [diff] [blame] | 516 | (buf[i * 4 + 2] & 0xFF) << 16 | |
| 517 | (buf[i * 4 + 1] & 0xFF) << 8 | |
| 518 | (buf[i * 4] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | catch (DataFormatException dfe) { |
| 523 | throw new IOException(dfe.toString()); |
| 524 | } |
| 525 | |
| 526 | handleUpdatedPixels(x, y, w, h); |
| 527 | scheduleRepaint(x, y, w, h); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | // |
| 532 | // Handle a tight rectangle. |
| 533 | // |
| 534 | |
| 535 | void handleTightRect(int x, int y, int w, int h) throws IOException { |
| 536 | |
| 537 | int comp_ctl = rfb.is.readUnsignedByte(); |
| 538 | |
| 539 | // Flush zlib streams if we are told by the server to do so. |
| 540 | for (int stream_id = 0; stream_id < 4; stream_id++) { |
| 541 | if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) { |
| 542 | tightInflaters[stream_id] = null; |
| 543 | } |
| 544 | comp_ctl >>= 1; |
| 545 | } |
| 546 | |
| 547 | // Check correctness of subencoding value. |
| 548 | if (comp_ctl > rfb.TightMaxSubencoding) { |
| 549 | throw new IOException("Incorrect tight subencoding: " + comp_ctl); |
| 550 | } |
| 551 | |
| 552 | // Handle solid-color rectangles. |
| 553 | if (comp_ctl == rfb.TightFill) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 554 | byte[] buf = new byte[3]; |
| 555 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 556 | Color bg = new Color(buf[0] & 0xFF, buf[1] & 0xFF, buf[2] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 557 | memGraphics.setColor(bg); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 558 | memGraphics.fillRect(x, y, w, h); |
| 559 | scheduleRepaint(x, y, w, h); |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | if (comp_ctl == rfb.TightJpeg) { |
| 564 | |
| 565 | // Read JPEG data. |
| 566 | byte[] jpegData = new byte[rfb.readCompactLen()]; |
| 567 | rfb.is.readFully(jpegData); |
| 568 | |
| 569 | // Create an Image object from the JPEG data. |
| 570 | Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData); |
| 571 | |
| 572 | // Remember the rectangle where the image should be drawn. |
| 573 | jpegRect = new Rectangle(x, y, w, h); |
| 574 | |
| 575 | // Let the imageUpdate() method do the actual drawing, here just |
| 576 | // wait until the image is fully loaded and drawn. |
| 577 | synchronized(jpegRect) { |
| 578 | Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1, this); |
| 579 | try { |
| 580 | // Wait no longer than three seconds. |
| 581 | jpegRect.wait(3000); |
| 582 | } catch (InterruptedException e) { |
| 583 | throw new IOException("Interrupted while decoding JPEG image"); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // Done, jpegRect is not needed any more. |
| 588 | jpegRect = null; |
| 589 | return; |
| 590 | |
| 591 | } |
| 592 | |
| 593 | // Read filter id and parameters. |
| 594 | int numColors = 0, rowSize = w; |
| 595 | byte[] palette8 = new byte[2]; |
| 596 | int[] palette24 = new int[256]; |
| 597 | boolean useGradient = false; |
| 598 | if ((comp_ctl & rfb.TightExplicitFilter) != 0) { |
| 599 | int filter_id = rfb.is.readUnsignedByte(); |
| 600 | if (filter_id == rfb.TightFilterPalette) { |
| 601 | numColors = rfb.is.readUnsignedByte() + 1; |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 602 | byte[] buf = new byte[numColors * 3]; |
| 603 | rfb.is.readFully(buf); |
| 604 | for (int i = 0; i < numColors; i++) { |
| 605 | palette24[i] = ((buf[i * 3] & 0xFF) << 16 | |
| 606 | (buf[i * 3 + 1] & 0xFF) << 8 | |
| 607 | (buf[i * 3 + 2] & 0xFF)); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 608 | } |
| 609 | if (numColors == 2) |
| 610 | rowSize = (w + 7) / 8; |
| 611 | } else if (filter_id == rfb.TightFilterGradient) { |
| 612 | useGradient = true; |
| 613 | } else if (filter_id != rfb.TightFilterCopy) { |
| 614 | throw new IOException("Incorrect tight filter id: " + filter_id); |
| 615 | } |
| 616 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 617 | if (numColors == 0) |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 618 | rowSize *= 3; |
| 619 | |
| 620 | // Read, optionally uncompress and decode data. |
| 621 | int dataSize = h * rowSize; |
| 622 | if (dataSize < rfb.TightMinToCompress) { |
| 623 | // Data size is small - not compressed with zlib. |
| 624 | if (numColors != 0) { |
| 625 | // Indexed colors. |
| 626 | byte[] indexedData = new byte[dataSize]; |
| 627 | rfb.is.readFully(indexedData); |
| 628 | if (numColors == 2) { |
| 629 | // Two colors. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 630 | decodeMonoData(x, y, w, h, indexedData, palette24); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 631 | } else { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 632 | // 3..255 colors. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 633 | int i = 0; |
| 634 | for (int dy = y; dy < y + h; dy++) { |
| 635 | for (int dx = x; dx < x + w; dx++) { |
| 636 | pixels24[dy * rfb.framebufferWidth + dx] = |
| 637 | palette24[indexedData[i++] & 0xFF]; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | } else if (useGradient) { |
| 642 | // "Gradient"-processed data |
| 643 | byte[] buf = new byte[w * h * 3]; |
| 644 | rfb.is.readFully(buf); |
| 645 | decodeGradientData(x, y, w, h, buf); |
| 646 | } else { |
| 647 | // Raw truecolor data. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 648 | byte[] buf = new byte[w * 3]; |
| 649 | int i, offset; |
| 650 | for (int dy = y; dy < y + h; dy++) { |
| 651 | rfb.is.readFully(buf); |
| 652 | offset = dy * rfb.framebufferWidth + x; |
| 653 | for (i = 0; i < w; i++) { |
| 654 | pixels24[offset + i] = |
| 655 | (buf[i * 3] & 0xFF) << 16 | |
| 656 | (buf[i * 3 + 1] & 0xFF) << 8 | |
| 657 | (buf[i * 3 + 2] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
| 661 | } else { |
| 662 | // Data was compressed with zlib. |
| 663 | int zlibDataLen = rfb.readCompactLen(); |
| 664 | byte[] zlibData = new byte[zlibDataLen]; |
| 665 | rfb.is.readFully(zlibData); |
| 666 | int stream_id = comp_ctl & 0x03; |
| 667 | if (tightInflaters[stream_id] == null) { |
| 668 | tightInflaters[stream_id] = new Inflater(); |
| 669 | } |
| 670 | Inflater myInflater = tightInflaters[stream_id]; |
| 671 | myInflater.setInput(zlibData); |
| 672 | try { |
| 673 | if (numColors != 0) { |
| 674 | // Indexed colors. |
| 675 | byte[] indexedData = new byte[dataSize]; |
| 676 | myInflater.inflate(indexedData); |
| 677 | if (numColors == 2) { |
| 678 | // Two colors. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 679 | decodeMonoData(x, y, w, h, indexedData, palette24); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 680 | } else { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 681 | // More than two colors. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 682 | int i = 0; |
| 683 | for (int dy = y; dy < y + h; dy++) { |
| 684 | for (int dx = x; dx < x + w; dx++) { |
| 685 | pixels24[dy * rfb.framebufferWidth + dx] = |
| 686 | palette24[indexedData[i++] & 0xFF]; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } else if (useGradient) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 691 | // Compressed "Gradient"-filtered data. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 692 | byte[] buf = new byte[w * h * 3]; |
| 693 | myInflater.inflate(buf); |
| 694 | decodeGradientData(x, y, w, h, buf); |
| 695 | } else { |
| 696 | // Compressed truecolor data. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 697 | byte[] buf = new byte[w * 3]; |
| 698 | int i, offset; |
| 699 | for (int dy = y; dy < y + h; dy++) { |
| 700 | myInflater.inflate(buf); |
| 701 | offset = dy * rfb.framebufferWidth + x; |
| 702 | for (i = 0; i < w; i++) { |
| 703 | pixels24[offset + i] = |
| 704 | (buf[i * 3] & 0xFF) << 16 | |
| 705 | (buf[i * 3 + 1] & 0xFF) << 8 | |
| 706 | (buf[i * 3 + 2] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | catch(DataFormatException dfe) { |
| 712 | throw new IOException(dfe.toString()); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | handleUpdatedPixels(x, y, w, h); |
| 717 | scheduleRepaint(x, y, w, h); |
| 718 | } |
| 719 | |
| 720 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 721 | // Decode 1bpp-encoded bi-color rectangle. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 722 | // |
| 723 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 724 | void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette) |
| 725 | throws IOException { |
| 726 | |
| 727 | int dx, dy, n; |
| 728 | int i = y * rfb.framebufferWidth + x; |
| 729 | int rowBytes = (w + 7) / 8; |
| 730 | byte b; |
| 731 | |
| 732 | for (dy = 0; dy < h; dy++) { |
| 733 | for (dx = 0; dx < w / 8; dx++) { |
| 734 | b = src[dy*rowBytes+dx]; |
| 735 | for (n = 7; n >= 0; n--) |
| 736 | pixels24[i++] = palette[b >> n & 1]; |
| 737 | } |
| 738 | for (n = 7; n >= 8 - w % 8; n--) { |
| 739 | pixels24[i++] = palette[src[dy*rowBytes+dx] >> n & 1]; |
| 740 | } |
| 741 | i += (rfb.framebufferWidth - w); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // |
| 746 | // Decode data processed with the "Gradient" filter. |
| 747 | // |
| 748 | |
| 749 | void decodeGradientData (int x, int y, int w, int h, byte[] buf) |
| 750 | throws IOException { |
| 751 | |
| 752 | int dx, dy, c; |
| 753 | byte[] prevRow = new byte[w * 3]; |
| 754 | byte[] thisRow = new byte[w * 3]; |
| 755 | byte[] pix = new byte[3]; |
| 756 | int[] est = new int[3]; |
| 757 | |
| 758 | int offset = y * rfb.framebufferWidth + x; |
| 759 | |
| 760 | for (dy = 0; dy < h; dy++) { |
| 761 | |
| 762 | /* First pixel in a row */ |
| 763 | for (c = 0; c < 3; c++) { |
| 764 | pix[c] = (byte)(prevRow[c] + buf[dy * w * 3 + c]); |
| 765 | thisRow[c] = pix[c]; |
| 766 | } |
| 767 | pixels24[offset++] = |
| 768 | (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF); |
| 769 | |
| 770 | /* Remaining pixels of a row */ |
| 771 | for (dx = 1; dx < w; dx++) { |
| 772 | for (c = 0; c < 3; c++) { |
| 773 | est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) - |
| 774 | (prevRow[(dx-1) * 3 + c] & 0xFF)); |
| 775 | if (est[c] > 0xFF) { |
| 776 | est[c] = 0xFF; |
| 777 | } else if (est[c] < 0x00) { |
| 778 | est[c] = 0x00; |
| 779 | } |
| 780 | pix[c] = (byte)(est[c] + buf[(dy * w + dx) * 3 + c]); |
| 781 | thisRow[dx * 3 + c] = pix[c]; |
| 782 | } |
| 783 | pixels24[offset++] = |
| 784 | (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF); |
| 785 | } |
| 786 | |
| 787 | System.arraycopy(thisRow, 0, prevRow, 0, w * 3); |
| 788 | offset += (rfb.framebufferWidth - w); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | |
| 793 | // |
| 794 | // Display newly updated area of pixels. |
| 795 | // |
| 796 | |
| 797 | void handleUpdatedPixels(int x, int y, int w, int h) { |
| 798 | |
| 799 | // Draw updated pixels of the off-screen image. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 800 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 801 | pixelsSource.newPixels(x, y, w, h); |
| 802 | memGraphics.setClip(x, y, w, h); |
| 803 | memGraphics.drawImage(rawPixelsImage, 0, 0, null); |
| 804 | memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight); |
| 805 | } |
| 806 | |
| 807 | // |
| 808 | // Tell JVM to repaint specified desktop area. |
| 809 | // |
| 810 | |
| 811 | void scheduleRepaint(int x, int y, int w, int h) { |
Constantin Kaplinsky | 30f786a | 2002-05-29 10:59:52 +0000 | [diff] [blame] | 812 | // Request repaint if not in the seeking mode. |
| 813 | if (!player.fbsStream.isSeeking()) |
| 814 | repaint(player.deferScreenUpdates, x, y, w, h); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 817 | } |