-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRenderFacility.java
More file actions
239 lines (229 loc) · 10.6 KB
/
RenderFacility.java
File metadata and controls
239 lines (229 loc) · 10.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
*------------------------------------------------------------------------------
* Copyright (C) 2025-2026 University of Dundee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*------------------------------------------------------------------------------
*/
package omero.gateway.facility;
import omero.ServerError;
import omero.api.RenderingEnginePrx;
import omero.gateway.Gateway;
import omero.gateway.SecurityContext;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
import omero.gateway.model.ImageData;
import omero.romio.PlaneDef;
import java.awt.image.BufferedImage;
public class RenderFacility extends Facility {
/**
* Creates a new instance
*
* @param gateway Reference to the {@link Gateway}
*/
RenderFacility(Gateway gateway) {
super(gateway);
}
/**
* Get a RenderingEngine for an image.
*
* @param ctx The security context.
* @param imageId The image ID
* @param initDefaultSettings Flag to create default rendering settings if the
* image doesn't have any for the current user.
* @return A RenderingEngine for the image
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public RenderingEnginePrx getRenderingEngine(SecurityContext ctx, long imageId,
boolean initDefaultSettings)
throws DSOutOfServiceException, DSAccessException, ServerError {
return getRenderingEngine(ctx, imageId, initDefaultSettings, true);
}
/**
* Gets a RenderingEngine for an image.
*
* @param ctx The security context.
* @param imageId The image ID
* @param initDefaultSettings Flag to create default rendering settings if the
* image doesn't have any for the current user (if set to false
* and the image doesn't have any rendering settings, null is
* returned)
* @param closeRE Close the rendering engine again
* @return A RenderingEngine for the image or null if RenderingEngine couldn't be initialised.
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public RenderingEnginePrx getRenderingEngine(SecurityContext ctx, long imageId,
boolean initDefaultSettings, boolean closeRE)
throws DSOutOfServiceException, DSAccessException {
RenderingEnginePrx re = null;
try {
long pixelsId = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId).getDefaultPixels().getId();
re = gateway.getRenderingService(ctx, pixelsId);
if (!re.lookupRenderingDef(pixelsId)) {
if (initDefaultSettings) {
re.resetDefaultSettings(true);
re.lookupRenderingDef(pixelsId);
} else {
return null;
}
}
re.load();
return re;
} catch (Throwable t) {
handleException(this, t, "Could not load RenderingEngine.");
} finally {
if (closeRE && re != null) {
try {
re.close();
} catch (ServerError e) {
// just ignore
}
}
}
return null;
}
/**
* Tries to determine if an image is an RGB(A) image.
* (Note: This does not necessarily give the same results as Bioformats isRGB()!
* FormatReader.isRGB() only checks if the image has more than one channel)
*
* @param ctx The security context.
* @param imageId The image ID
* @return True if the image is RGB(A)
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public boolean isRGB(SecurityContext ctx, long imageId) throws DSOutOfServiceException, DSAccessException {
try {
ImageData img = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId);
int nChannels = img.getDefaultPixels().getSizeC();
if (nChannels < 3 || nChannels > 4)
return false;
boolean r = false, g = false, b = false;
RenderingEnginePrx re = getRenderingEngine(ctx, imageId, true);
for (int i = 0; i < nChannels; i++) {
int[] ch = re.getRGBA(i);
if (!r && ch[0] == 255 && ch[1] == 0 && ch[2] == 0)
r = true;
if (!g && ch[1] == 255 && ch[0] == 0 && ch[2] == 0)
g = true;
if (!b && ch[2] == 255 && ch[0] == 0 && ch[1] == 0)
b = true;
}
return r && g && b;
} catch (Throwable t) {
handleException(this, t, "Could not check image.");
}
return false;
}
/**
* Renders the selected z, t plane of the given image as RGB image.
* @param ctx The security context.
* @param imageId The image ID
* @param z The z plane
* @param t The time point
* @return An RGB image ready to be displayed on screen.
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public int[] renderPlane(SecurityContext ctx, long imageId, int z, int t)
throws DSOutOfServiceException, DSAccessException {
return renderPlane(ctx, imageId, z, t, true);
}
/**
* Renders the selected z, t plane of the given image as RGB image.
* @param ctx The security context.
* @param imageId The image ID
* @param z The z plane
* @param t The time point
* @param closeRE Close the rendering engine again
* @return An RGB image ready to be displayed on screen.
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public int[] renderPlane(SecurityContext ctx, long imageId, int z, int t, boolean closeRE)
throws DSOutOfServiceException, DSAccessException {
RenderingEnginePrx re = null;
try {
re = getRenderingEngine(ctx, imageId, true, closeRE);
PlaneDef plane = new PlaneDef(omeis.providers.re.data.PlaneDef.XY, 0,
0, z, t, null, -1);
return re.renderAsPackedInt(plane);
} catch (Throwable e) {
handleException(this, e, "Could not render plane.");
} finally {
if (closeRE && re != null) {
try {
re.close();
} catch (ServerError e) {
// just ignore
}
}
}
return null;
}
/**
* Renders the selected z, t plane of the given image as BufferedImage.
* @param ctx The security context.
* @param imageId The image ID
* @param z The z plane
* @param t The time point
* @return A BufferedImage ready to be displayed on screen.
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public BufferedImage renderPlaneAsBufferedImage(SecurityContext ctx, long imageId, int z, int t)
throws DSOutOfServiceException, DSAccessException {
return renderPlaneAsBufferedImage(ctx, imageId, z, t, true);
}
/**
* Renders the selected z, t plane of the given image as BufferedImage.
* @param ctx The security context.
* @param imageId The image ID
* @param z The z plane
* @param t The time point
* @param closeRE Close the rendering engine again
* @return A BufferedImage ready to be displayed on screen.
* @throws DSOutOfServiceException If the connection is broken, or not logged in
* @throws DSAccessException If an error occurred while trying to retrieve data from OMERO
* service.
*/
public BufferedImage renderPlaneAsBufferedImage(SecurityContext ctx, long imageId, int z, int t,
boolean closeRE)
throws DSOutOfServiceException, DSAccessException {
try {
ImageData img = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId);
int[] pixels = renderPlane(ctx, imageId, 0, 0, closeRE);
int w = img.getDefaultPixels().getSizeX();
int h = img.getDefaultPixels().getSizeY();
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, w, h, pixels, 0, w);
return image;
} catch (Throwable e) {
handleException(this, e, "Could not render plane.");
}
return null;
}
}