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 | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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 | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | public void updateColourMap() { |
| 40 | cm = new IndexColorModel(8, nColours, reds, greens, blues); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 43 | // resize() resizes the image, preserving the image data where possible. |
| 44 | public void resize(int w, int h) { |
| 45 | if (w == width() && h == height()) return; |
| 46 | |
| 47 | width_ = w; |
| 48 | height_ = h; |
| 49 | GraphicsEnvironment ge = |
| 50 | GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 51 | GraphicsDevice gd = ge.getDefaultScreenDevice(); |
| 52 | GraphicsConfiguration gc = gd.getDefaultConfiguration(); |
Brian Hinz | a64ced0 | 2012-05-19 13:28:43 +0000 | [diff] [blame] | 53 | image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 54 | image.setAccelerationPriority(1); |
| 55 | image.createGraphics(); |
Brian Hinz | e4d46b6 | 2012-08-26 18:20:15 +0000 | [diff] [blame^] | 56 | WritableRaster wr = image.getRaster(); |
| 57 | SinglePixelPackedSampleModel sm = |
| 58 | (SinglePixelPackedSampleModel)image.getSampleModel(); |
| 59 | DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); |
| 60 | data = db.getData(); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public void fillRect(int x, int y, int w, int h, int pix) { |
| 64 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
| 65 | switch (format.depth) { |
| 66 | case 24: |
| 67 | graphics.setColor(new Color(pix)); |
| 68 | graphics.fillRect(x, y, w, h); |
| 69 | break; |
| 70 | default: |
| 71 | Color color = new Color((0xff << 24) | (cm.getRed(pix) << 16) | |
| 72 | (cm.getGreen(pix) << 8) | (cm.getBlue(pix))); |
| 73 | graphics.setColor(color); |
| 74 | graphics.fillRect(x, y, w, h); |
| 75 | break; |
| 76 | } |
| 77 | graphics.dispose(); |
| 78 | } |
| 79 | |
| 80 | 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] | 81 | if (pix instanceof Image) { |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 82 | Image img = (Image)pix; |
| 83 | clip = new Rectangle(x, y, w, h); |
| 84 | synchronized(clip) { |
| 85 | tk.prepareImage(img, -1, -1, this); |
| 86 | try { |
| 87 | clip.wait(1000); |
| 88 | } catch (InterruptedException e) { |
| 89 | throw new Exception("Error decoding JPEG data"); |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 90 | } |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 91 | } |
| 92 | clip = null; |
| 93 | img.flush(); |
| 94 | } else { |
| 95 | for (int j = 0; j < h; j++) |
| 96 | System.arraycopy(pix, (w*j), data, width_ * (y + j) + x, w); |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 97 | } |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public void copyRect(int x, int y, int w, int h, int srcX, int srcY) { |
| 101 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
| 102 | graphics.copyArea(srcX, srcY, w, h, x - srcX, y - srcY); |
| 103 | graphics.dispose(); |
| 104 | } |
| 105 | |
| 106 | public Image getImage() { |
| 107 | return (Image)image; |
| 108 | } |
| 109 | |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 110 | public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) { |
| 111 | if ((infoflags & (ALLBITS | ABORT)) == 0) { |
| 112 | return true; |
| 113 | } else { |
| 114 | if ((infoflags & ALLBITS) != 0) { |
| 115 | if (clip != null) { |
| 116 | synchronized(clip) { |
| 117 | Graphics2D graphics = (Graphics2D)image.getGraphics(); |
| 118 | graphics.drawImage(img, clip.x, clip.y, clip.width, clip.height, null); |
| 119 | graphics.dispose(); |
| 120 | clip.notify(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 128 | BufferedImage image; |
Brian Hinz | bbc038d | 2012-05-23 03:43:10 +0000 | [diff] [blame] | 129 | Rectangle clip; |
Brian Hinz | 28aa3a8 | 2012-04-05 02:08:49 +0000 | [diff] [blame] | 130 | |
| 131 | static LogWriter vlog = new LogWriter("BIPixelBuffer"); |
| 132 | } |