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