Brian Hinz | b213da6 | 2012-04-11 22:00:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2012 Brian P. Hinz |
Brian Hinz | e4d46b6 | 2012-08-26 18:20:15 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012 D. R. Commander. All Rights Reserved. |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 3 | * |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 8 | * |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 13 | * |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
Brian Hinz | b213da6 | 2012-04-11 22:00:55 +0000 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | package com.tigervnc.vncviewer; |
| 21 | |
| 22 | import java.awt.*; |
| 23 | import java.awt.image.*; |
| 24 | |
| 25 | import com.tigervnc.rfb.*; |
| 26 | import com.tigervnc.rfb.Exception; |
| 27 | |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 28 | public class BIPixelBuffer extends PlatformPixelBuffer implements ImageObserver |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 29 | { |
| 30 | public BIPixelBuffer(int w, int h, CConn cc_, DesktopWindow desktop_) { |
| 31 | super(w, h, cc_, desktop_); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 32 | clip = new Rectangle(); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 35 | public void setPF(PixelFormat pf) { |
| 36 | super.setPF(pf); |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 37 | createImage(width(), height()); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | public void updateColourMap() { |
Brian Hinz | b036796 | 2012-12-06 02:25:49 +0000 | [diff] [blame] | 41 | super.updateColourMap(); |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 42 | createImage(width_, height_); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 43 | } |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 44 | |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 45 | // resize() resizes the image, preserving the image data where possible. |
| 46 | public void resize(int w, int h) { |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 47 | if (w == width() && h == height()) |
| 48 | return; |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 49 | |
| 50 | width_ = w; |
| 51 | height_ = h; |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 52 | createImage(w, h); |
| 53 | } |
| 54 | |
| 55 | private void createImage(int w, int h) { |
| 56 | if (w == 0 || h == 0) return; |
| 57 | WritableRaster wr; |
| 58 | if (cm instanceof IndexColorModel) |
| 59 | wr = ((IndexColorModel)cm).createCompatibleWritableRaster(w, h); |
| 60 | else |
| 61 | wr = ((DirectColorModel)cm).createCompatibleWritableRaster(w, h); |
| 62 | image = new BufferedImage(cm, wr, true, null); |
| 63 | db = wr.getDataBuffer(); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public void fillRect(int x, int y, int w, int h, int pix) { |
| 67 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
| 68 | switch (format.depth) { |
| 69 | case 24: |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 70 | graphics.setColor(new Color(pix)); |
| 71 | graphics.fillRect(x, y, w, h); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 72 | break; |
| 73 | default: |
| 74 | Color color = new Color((0xff << 24) | (cm.getRed(pix) << 16) | |
| 75 | (cm.getGreen(pix) << 8) | (cm.getBlue(pix))); |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 76 | graphics.setColor(color); |
| 77 | graphics.fillRect(x, y, w, h); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | graphics.dispose(); |
| 81 | } |
| 82 | |
| 83 | public void imageRect(int x, int y, int w, int h, Object pix) { |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 84 | if (pix instanceof Image) { |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 85 | Image img = (Image)pix; |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 86 | clip = new Rectangle(x, y, w, h); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 87 | synchronized(clip) { |
| 88 | tk.prepareImage(img, -1, -1, this); |
| 89 | try { |
| 90 | clip.wait(1000); |
| 91 | } catch (InterruptedException e) { |
| 92 | throw new Exception("Error decoding JPEG data"); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 93 | } |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 94 | } |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 95 | clip = null; |
| 96 | img.flush(); |
| 97 | } else { |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 98 | if (image.getSampleModel().getTransferType() == DataBuffer.TYPE_BYTE) { |
| 99 | byte[] bytes = new byte[((int[])pix).length]; |
| 100 | for (int i = 0; i < bytes.length; i++) |
| 101 | bytes[i] = (byte)((int[])pix)[i]; |
| 102 | pix = bytes; |
| 103 | } |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 104 | image.getSampleModel().setDataElements(x, y, w, h, pix, db); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 105 | } |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public void copyRect(int x, int y, int w, int h, int srcX, int srcY) { |
| 109 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
| 110 | graphics.copyArea(srcX, srcY, w, h, x - srcX, y - srcY); |
| 111 | graphics.dispose(); |
| 112 | } |
| 113 | |
| 114 | public Image getImage() { |
| 115 | return (Image)image; |
| 116 | } |
| 117 | |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 118 | public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) { |
| 119 | if ((infoflags & (ALLBITS | ABORT)) == 0) { |
| 120 | return true; |
| 121 | } else { |
| 122 | if ((infoflags & ALLBITS) != 0) { |
| 123 | if (clip != null) { |
| 124 | synchronized(clip) { |
| 125 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame^] | 126 | graphics.drawImage(img, clip.x, clip.y, clip.width, clip.height, null); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 127 | graphics.dispose(); |
| 128 | clip.notify(); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 136 | BufferedImage image; |
Brian Hinz | 5471170 | 2012-09-01 02:00:51 +0000 | [diff] [blame] | 137 | DataBuffer db; |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 138 | Rectangle clip; |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 139 | |
| 140 | static LogWriter vlog = new LogWriter("BIPixelBuffer"); |
| 141 | } |