blob: 1634ebd1ab2cce4c946cae039ab4b36867cf7b81 [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 Hinzc0a36092013-05-12 15:46:09 +00003 *
Brian Hinz28aa3a82012-04-05 02:08:49 +00004 * 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 Hinzc0a36092013-05-12 15:46:09 +00008 *
Brian Hinz28aa3a82012-04-05 02:08:49 +00009 * 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 Hinzc0a36092013-05-12 15:46:09 +000013 *
Brian Hinz28aa3a82012-04-05 02:08:49 +000014 * 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{
Brian P. Hinzc4563872016-08-27 17:33:04 -040030 public BIPixelBuffer(PixelFormat pf, int w, int h, DesktopWindow desktop_) {
31 super(pf, w, h, 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 Hinz54711702012-09-01 02:00:51 +000037 createImage(width(), height());
Brian Hinzbbc038d2012-05-23 03:43:10 +000038 }
39
40 public void updateColourMap() {
Brian Hinzb0367962012-12-06 02:25:49 +000041 super.updateColourMap();
Brian Hinz54711702012-09-01 02:00:51 +000042 createImage(width_, height_);
Brian Hinzbbc038d2012-05-23 03:43:10 +000043 }
Brian Hinzc0a36092013-05-12 15:46:09 +000044
Brian Hinz28aa3a82012-04-05 02:08:49 +000045 // resize() resizes the image, preserving the image data where possible.
46 public void resize(int w, int h) {
Brian Hinz54711702012-09-01 02:00:51 +000047 if (w == width() && h == height())
48 return;
Brian Hinz28aa3a82012-04-05 02:08:49 +000049
50 width_ = w;
51 height_ = h;
Brian Hinz54711702012-09-01 02:00:51 +000052 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 Hinz28aa3a82012-04-05 02:08:49 +000064 }
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 Hinzc0a36092013-05-12 15:46:09 +000070 graphics.setColor(new Color(pix));
71 graphics.fillRect(x, y, w, h);
Brian Hinz28aa3a82012-04-05 02:08:49 +000072 break;
73 default:
74 Color color = new Color((0xff << 24) | (cm.getRed(pix) << 16) |
75 (cm.getGreen(pix) << 8) | (cm.getBlue(pix)));
Brian Hinzc0a36092013-05-12 15:46:09 +000076 graphics.setColor(color);
77 graphics.fillRect(x, y, w, h);
Brian Hinz28aa3a82012-04-05 02:08:49 +000078 break;
79 }
80 graphics.dispose();
81 }
82
83 public void imageRect(int x, int y, int w, int h, Object pix) {
Brian Hinz28aa3a82012-04-05 02:08:49 +000084 if (pix instanceof Image) {
Brian Hinzbbc038d2012-05-23 03:43:10 +000085 Image img = (Image)pix;
Brian Hinzc0a36092013-05-12 15:46:09 +000086 clip = new Rectangle(x, y, w, h);
Brian Hinzbbc038d2012-05-23 03:43:10 +000087 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 Hinz28aa3a82012-04-05 02:08:49 +000093 }
Brian Hinzc0a36092013-05-12 15:46:09 +000094 }
Brian Hinzbbc038d2012-05-23 03:43:10 +000095 clip = null;
96 img.flush();
97 } else {
Brian Hinz54711702012-09-01 02:00:51 +000098 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 Hinzc0a36092013-05-12 15:46:09 +0000104 image.getSampleModel().setDataElements(x, y, w, h, pix, db);
Brian Hinzbbc038d2012-05-23 03:43:10 +0000105 }
Brian Hinz28aa3a82012-04-05 02:08:49 +0000106 }
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 Hinzbbc038d2012-05-23 03:43:10 +0000118 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 Hinzc0a36092013-05-12 15:46:09 +0000126 graphics.drawImage(img, clip.x, clip.y, clip.width, clip.height, null);
Brian Hinzbbc038d2012-05-23 03:43:10 +0000127 graphics.dispose();
128 clip.notify();
129 }
130 }
131 }
132 return false;
133 }
134 }
135
Brian Hinz28aa3a82012-04-05 02:08:49 +0000136 BufferedImage image;
Brian Hinz54711702012-09-01 02:00:51 +0000137 DataBuffer db;
Brian Hinzbbc038d2012-05-23 03:43:10 +0000138 Rectangle clip;
Brian Hinz28aa3a82012-04-05 02:08:49 +0000139
140 static LogWriter vlog = new LogWriter("BIPixelBuffer");
141}