blob: 88c939630556d06cc7223cdd49af8ef9fc56f9b3 [file] [log] [blame]
Brian Hinzb213da62012-04-11 22:00:55 +00001/* Copyright (C) 2012 Brian P. Hinz
Brian Hinze4d46b62012-08-26 18:20:15 +00002 * Copyright (C) 2012 D. R. Commander. All Rights Reserved.
Brian Hinz28aa3a82012-04-05 02:08:49 +00003 *
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 Hinzb213da62012-04-11 22:00:55 +000016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
Brian Hinz28aa3a82012-04-05 02:08:49 +000017 * USA.
18 */
19
20package com.tigervnc.vncviewer;
21
22import java.awt.*;
23import java.awt.image.*;
24
25import com.tigervnc.rfb.*;
26import com.tigervnc.rfb.Exception;
27
Brian Hinzbbc038d2012-05-23 03:43:10 +000028public class BIPixelBuffer extends PlatformPixelBuffer implements ImageObserver
Brian Hinz28aa3a82012-04-05 02:08:49 +000029{
30 public BIPixelBuffer(int w, int h, CConn cc_, DesktopWindow desktop_) {
31 super(w, h, cc_, desktop_);
Brian Hinzbbc038d2012-05-23 03:43:10 +000032 clip = new Rectangle();
Brian Hinz28aa3a82012-04-05 02:08:49 +000033 }
34
Brian Hinzbbc038d2012-05-23 03:43:10 +000035 public void setPF(PixelFormat pf) {
36 super.setPF(pf);
Brian Hinzbbc038d2012-05-23 03:43:10 +000037 }
38
39 public void updateColourMap() {
40 cm = new IndexColorModel(8, nColours, reds, greens, blues);
Brian Hinzbbc038d2012-05-23 03:43:10 +000041 }
42
Brian Hinz28aa3a82012-04-05 02:08:49 +000043 // 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 Hinza64ced02012-05-19 13:28:43 +000053 image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
Brian Hinz28aa3a82012-04-05 02:08:49 +000054 image.setAccelerationPriority(1);
55 image.createGraphics();
Brian Hinze4d46b62012-08-26 18:20:15 +000056 WritableRaster wr = image.getRaster();
57 SinglePixelPackedSampleModel sm =
58 (SinglePixelPackedSampleModel)image.getSampleModel();
59 DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
60 data = db.getData();
Brian Hinz28aa3a82012-04-05 02:08:49 +000061 }
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 Hinz28aa3a82012-04-05 02:08:49 +000081 if (pix instanceof Image) {
Brian Hinzbbc038d2012-05-23 03:43:10 +000082 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 Hinz28aa3a82012-04-05 02:08:49 +000090 }
Brian Hinzbbc038d2012-05-23 03:43:10 +000091 }
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 Hinzbbc038d2012-05-23 03:43:10 +000097 }
Brian Hinz28aa3a82012-04-05 02:08:49 +000098 }
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 Hinzbbc038d2012-05-23 03:43:10 +0000110 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 Hinz28aa3a82012-04-05 02:08:49 +0000128 BufferedImage image;
Brian Hinzbbc038d2012-05-23 03:43:10 +0000129 Rectangle clip;
Brian Hinz28aa3a82012-04-05 02:08:49 +0000130
131 static LogWriter vlog = new LogWriter("BIPixelBuffer");
132}