blob: d5ec7b6b4039b2b4b18afdd5a989263c1866473e [file] [log] [blame]
Brian Hinzb213da62012-04-11 22:00:55 +00001/* Copyright (C) 2012 Brian P. Hinz
Brian Hinz28aa3a82012-04-05 02:08:49 +00002 *
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 Hinzb213da62012-04-11 22:00:55 +000015 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
Brian Hinz28aa3a82012-04-05 02:08:49 +000016 * USA.
17 */
18
19package com.tigervnc.vncviewer;
20
21import java.awt.*;
22import java.awt.image.*;
23
24import com.tigervnc.rfb.*;
25import com.tigervnc.rfb.Exception;
26
Brian Hinzbbc038d2012-05-23 03:43:10 +000027public class BIPixelBuffer extends PlatformPixelBuffer implements ImageObserver
Brian Hinz28aa3a82012-04-05 02:08:49 +000028{
29 public BIPixelBuffer(int w, int h, CConn cc_, DesktopWindow desktop_) {
30 super(w, h, cc_, desktop_);
Brian Hinzbbc038d2012-05-23 03:43:10 +000031 clip = new Rectangle();
Brian Hinz28aa3a82012-04-05 02:08:49 +000032 }
33
Brian Hinzbbc038d2012-05-23 03:43:10 +000034 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 Hinz28aa3a82012-04-05 02:08:49 +000046 // 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 Hinza64ced02012-05-19 13:28:43 +000056 image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
Brian Hinz28aa3a82012-04-05 02:08:49 +000057 image.setAccelerationPriority(1);
58 image.createGraphics();
Brian Hinzbbc038d2012-05-23 03:43:10 +000059 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 Hinz28aa3a82012-04-05 02:08:49 +000065 }
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 Hinz28aa3a82012-04-05 02:08:49 +000085 if (pix instanceof Image) {
Brian Hinzbbc038d2012-05-23 03:43:10 +000086 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 Hinz28aa3a82012-04-05 02:08:49 +000094 }
Brian Hinzbbc038d2012-05-23 03:43:10 +000095 }
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 Hinz28aa3a82012-04-05 02:08:49 +0000108 }
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 Hinzbbc038d2012-05-23 03:43:10 +0000120 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 Hinz28aa3a82012-04-05 02:08:49 +0000138 BufferedImage image;
Brian Hinzbbc038d2012-05-23 03:43:10 +0000139 MemoryImageSource source;
140 int[] data;
141 Image sourceImage;
142 Rectangle clip;
Brian Hinz28aa3a82012-04-05 02:08:49 +0000143
144 static LogWriter vlog = new LogWriter("BIPixelBuffer");
145}