forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedImageCanvas.java
More file actions
150 lines (124 loc) · 4.86 KB
/
BufferedImageCanvas.java
File metadata and controls
150 lines (124 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.consoleproxy.vnc;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import com.cloud.consoleproxy.util.ImageHelper;
import com.cloud.consoleproxy.util.TileInfo;
/**
* A <code>BuffereImageCanvas</code> component represents frame buffer image on
* the screen. It also notifies its subscribers when screen is repainted.
*/
public class BufferedImageCanvas extends Canvas implements FrameBufferCanvas {
private static final long serialVersionUID = 1L;
// Offline screen buffer
private BufferedImage offlineImage;
// Cached Graphics2D object for offline screen buffer
private Graphics2D graphics;
private PaintNotificationListener listener;
public BufferedImageCanvas(PaintNotificationListener listener, int width, int height) {
super();
this.listener = listener;
setBackground(Color.black);
setFocusable(true);
// Don't intercept TAB key
setFocusTraversalKeysEnabled(false);
setCanvasSize(width, height);
}
public void setCanvasSize(int width, int height) {
this.offlineImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = offlineImage.createGraphics();
setSize(offlineImage.getWidth(), offlineImage.getHeight());
}
@Override
public void update(Graphics g) {
// Call paint() directly, without clearing screen first
paint(g);
}
@Override
public void paint(Graphics g) {
// Only part of image, requested with repaint(Rectangle), will be
// painted on screen.
synchronized (offlineImage) {
g.drawImage(offlineImage, 0, 0, this);
}
// Notify server that update is painted on screen
listener.imagePaintedOnScreen();
}
public BufferedImage getOfflineImage() {
return offlineImage;
}
public Graphics2D getOfflineGraphics() {
return graphics;
}
public void copyTile(Graphics2D g, int x, int y, Rectangle rc) {
synchronized (offlineImage) {
g.drawImage(offlineImage, x, y, x + rc.width, y + rc.height, rc.x, rc.y, rc.x + rc.width, rc.y + rc.height, null);
}
}
@Override
public Image getFrameBufferScaledImage(int width, int height) {
if (offlineImage != null)
return offlineImage.getScaledInstance(width, height, Image.SCALE_DEFAULT);
return null;
}
@Override
public byte[] getFrameBufferJpeg() {
int width = 800;
int height = 600;
width = offlineImage.getWidth();
height = offlineImage.getHeight();
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = bufferedImage.createGraphics();
synchronized (offlineImage) {
g.drawImage(offlineImage, 0, 0, width, height, 0, 0, width, height, null);
}
byte[] imgBits = null;
try {
imgBits = ImageHelper.jpegFromImage(bufferedImage);
} catch (IOException e) {
}
return imgBits;
}
@Override
public byte[] getTilesMergedJpeg(List<TileInfo> tileList, int tileWidth, int tileHeight) {
int width = Math.max(tileWidth, tileWidth * tileList.size());
BufferedImage bufferedImage = new BufferedImage(width, tileHeight, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = bufferedImage.createGraphics();
synchronized (offlineImage) {
int i = 0;
for (TileInfo tile : tileList) {
Rectangle rc = tile.getTileRect();
g.drawImage(offlineImage, i * tileWidth, 0, i * tileWidth + rc.width, rc.height, rc.x, rc.y, rc.x + rc.width, rc.y + rc.height, null);
i++;
}
}
byte[] imgBits = null;
try {
imgBits = ImageHelper.jpegFromImage(bufferedImage);
} catch (IOException e) {
}
return imgBits;
}
}