-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathChannelProcessor.java
523 lines (481 loc) · 16.4 KB
/
ChannelProcessor.java
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* ome.formats.model.ChannelProcessor
*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2008 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 ome.formats.model;
import static omero.rtypes.rint;
import static omero.rtypes.rstring;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import loci.formats.FormatTools;
import loci.formats.IFormatReader;
import ome.util.LSID;
import omero.RInt;
import omero.RString;
import omero.model.Channel;
import omero.model.Filter;
import omero.model.Image;
import omero.model.Laser;
import omero.model.Length;
import omero.model.LightSource;
import omero.model.LogicalChannel;
import omero.model.Pixels;
import omero.model.TransmittanceRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.math.DoubleMath;
/**
* Processes the pixels sets of an IObjectContainerStore and ensures
* that LogicalChannel containers are present in the container cache, and
* populating channel name and colour where appropriate.
*
* @author Chris Allan <callan at blackcat dot ca>
* @author Jean-Marie <jburel at dundee dot ac dot uk>
*
*/
public class ChannelProcessor implements ModelProcessor {
/** Name of the <code>red</code> component when it is a graphics image. */
public static final String RED_TEXT = "Red";
/** Name of the <code>green</code> component when it is a graphics image. */
public static final String GREEN_TEXT = "Green";
/** Name of the <code>blue</code> component when it is a graphics image. */
public static final String BLUE_TEXT = "Blue";
/** Name of the <code>alpha</code> component when it is a graphics image. */
public static final String ALPHA_TEXT = "Alpha";
/** Logger for this class */
private Logger log = LoggerFactory.getLogger(ChannelProcessor.class);
/** Container store we're currently working with. */
private IObjectContainerStore store;
/** Bio-Formats reader implementation we're currently working with. */
private IFormatReader reader;
/**
* Returns the name from the wavelength.
*
* @param value The value to handle.
* @return See above.
*/
private String getNameFromWavelength(Length value) {
if (value == null) return null;
//Check that the value is an int
if (DoubleMath.isMathematicalInteger(value.getValue())) {
return ""+value.getValue();
}
return value.toString();
}
/**
* Sets the default color if it is a single channel image.
*
* @param channelData Channel data to use to set the color.
*/
private void setSingleChannel(ChannelData channelData) {
int channelIndex = channelData.getChannelIndex();
Channel channel = channelData.getChannel();
RString name;
if (hasColor(channel)) {
//color already set by Bio-formats
return;
}
int[] defaultColor = ColorsFactory.newGreyColor();
channel.setRed(rint(defaultColor[ColorsFactory.RED_INDEX]));
channel.setGreen(rint(defaultColor[ColorsFactory.GREEN_INDEX]));
channel.setBlue(rint(defaultColor[ColorsFactory.BLUE_INDEX]));
channel.setAlpha(rint(defaultColor[ColorsFactory.ALPHA_INDEX]));
}
/**
* Populates the default color for the channel if one does not already
* exist.
*
* @param channelData Channel data to use to set the color.
* @param isGraphicsDomaind Whether or not the image is in the graphics
* domain according to Bio-Formats.
*/
private void populateDefault(
ChannelData channelData, boolean isGraphicsDomain) {
int channelIndex = channelData.getChannelIndex();
Channel channel = channelData.getChannel();
LogicalChannel lc = channelData.getLogicalChannel();
int[] defaultColor;
if (isGraphicsDomain) {
log.debug("Setting color channel to RGB.");
setDefaultChannelColor(channel, channelIndex);
switch (channelIndex) {
case 0: //red
if (lc.getName() == null)
lc.setName(rstring(RED_TEXT));
break;
case 1: //green
if (lc.getName() == null)
lc.setName(rstring(GREEN_TEXT));
break;
case 2: //blue
if (lc.getName() == null)
lc.setName(rstring(BLUE_TEXT));
break;
case 3: //alpha, reset transparent
channel.setRed(rint(0));
channel.setGreen(rint(0));
channel.setBlue(rint(0));
channel.setAlpha(rint(0)); //transparent
if (lc.getName() == null)
lc.setName(rstring(ALPHA_TEXT));
}
return;
}
RString name;
if (hasColor(channel)) {
//color already set by Bio-formats
//Try to set the name.
log.debug("Already set in BF.");
if (lc.getName() == null) {
name = getChannelName(channelData);
if (name != null) lc.setName(name);
}
return;
}
//not set by
//First we check the emission wavelength.
Length valueWavelength = lc.getEmissionWave();
if (valueWavelength != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueWavelength));
if (lc.getName() == null) {
lc.setName(rstring(getNameFromWavelength(valueWavelength)));
}
return;
}
Length valueFilter = null;
//First check the emission filter.
//First check if filter
Filter f = getValidFilter(channelData.getLightPathEmissionFilters(), true);
if (f != null)
valueFilter = ColorsFactory.getValueFromFilter(f, true);
if (valueFilter != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueFilter));
if (lc.getName() == null) {
name = getNameFromFilter(f);
if (name != null) lc.setName(name);
}
return;
}
f = channelData.getFilterSetEmissionFilter();
valueFilter = ColorsFactory.getValueFromFilter(f, true);
if (valueFilter != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueFilter));
if (lc.getName() == null) {
name = getNameFromFilter(f);
if (name != null) lc.setName(name);
}
return;
}
//Laser
if (channelData.getLightSource() != null) {
LightSource ls = channelData.getLightSource();
if (ls instanceof Laser) {
valueWavelength = ((Laser) ls).getWavelength();
if (valueWavelength != null) {
setChannelColor(
channel, channelIndex,
ColorsFactory.determineColor(valueWavelength));
if (lc.getName() == null) {
lc.setName(rstring(getNameFromWavelength(valueWavelength)));
}
return;
}
}
}
//Excitation
valueWavelength = lc.getExcitationWave();
if (valueWavelength != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueWavelength));
if (lc.getName() == null) {
lc.setName(rstring(getNameFromWavelength(valueWavelength)));
}
return;
}
f = getValidFilter(channelData.getLightPathExcitationFilters(), false);
if (f != null)
valueFilter = ColorsFactory.getValueFromFilter(f, false);
if (valueFilter != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueFilter));
if (lc.getName() == null) {
name = getNameFromFilter(f);
if (name != null) lc.setName(name);
}
return;
}
f = channelData.getFilterSetExcitationFilter();
valueFilter = ColorsFactory.getValueFromFilter(f, false);
if (valueFilter != null) {
setChannelColor(
channel, channelIndex, ColorsFactory.determineColor(valueFilter));
if (lc.getName() == null) {
name = getNameFromFilter(f);
if (name != null) lc.setName(name);
}
return;
}
//not been able to set the color
setDefaultChannelColor(channel, channelIndex);
}
/**
* Checks if a valid color is already set on the Channel.
* All four color components (red, green, blue, alpha)
* must be non-null.
*
* @param channel the Channel to check for a color
* @return true if a valid color was found; false otherwise
*/
private boolean hasColor(Channel channel) {
Integer red = getValue(channel.getRed());
Integer green = getValue(channel.getGreen());
Integer blue = getValue(channel.getBlue());
Integer alpha = getValue(channel.getAlpha());
return red != null && green != null && blue != null && alpha != null;
}
/**
* Returns the first filter from the list with a <code>not null</code>
* value.
*
* @param filters The collection to handle.
* @param emission Passed <code>true</code> to indicate that the filter
* is an emission filter, <code>false</code> otherwise.
* @return See above.
*/
private Filter getValidFilter(List<Filter> filters, boolean emission) {
if (filters == null) return null;
Iterator<Filter> i = filters.iterator();
Length value = null;
Filter f;
while (i.hasNext()) {
f = i.next();
value = ColorsFactory.getValueFromFilter(f, emission);
if (value != null) return f;
}
return null;
}
/**
* Sets the default color of the channel.
*
* @param channel The channel to handle.
* @param index The index of the channel.
*/
private void setDefaultChannelColor(Channel channel, int index) {
//not been able to set the color
int value = index % 3;
int[] defaultColor;
switch (value) {
case 0: //red
defaultColor = ColorsFactory.newRedColor();
break;
case 1: //green
defaultColor = ColorsFactory.newGreenColor();
break;
default: //blue
defaultColor = ColorsFactory.newBlueColor();
}
channel.setRed(rint(defaultColor[ColorsFactory.RED_INDEX]));
channel.setGreen(rint(defaultColor[ColorsFactory.GREEN_INDEX]));
channel.setBlue(rint(defaultColor[ColorsFactory.BLUE_INDEX]));
channel.setAlpha(rint(defaultColor[ColorsFactory.ALPHA_INDEX]));
}
/**
* Sets the color of the channel.
*
* @param channel The channel to handle.
* @param index The index of the channel.
* @param rgba The color to set.
*/
private void setChannelColor(Channel channel, int index, int[] rgba) {
if (rgba == null) {
setDefaultChannelColor(channel, index);
return;
}
channel.setRed(rint(rgba[0]));
channel.setGreen(rint(rgba[1]));
channel.setBlue(rint(rgba[2]));
channel.setAlpha(rint(rgba[3]));
}
/**
* Returns a channel name string from a given filter.
*
* @param filter Filter to retrieve a channel name from.
* @return See above.
*/
private RString getNameFromFilter(Filter filter) {
if (filter == null) {
return null;
}
TransmittanceRange t = filter.getTransmittanceRange();
return t == null? null :
rstring(String.valueOf(t.getCutIn().getValue()));
}
/**
* Returns the concrete value of an OMERO rtype.
*
* @param value OMERO rtype to get the value of.
* @return Concrete value of <code>value</code> or <code>null</code> if
* <code>value == null</code>.
*/
private Integer getValue(RInt value) {
return value == null? null : value.getValue();
}
/**
* Determines the name of the channel.
* This method should only be invoked when a color was assigned by
* Bio-formats.
*
* @param channelData The channel to handle.
* @return See above.
*/
private RString getChannelName(ChannelData channelData) {
LogicalChannel lc = channelData.getLogicalChannel();
Length value = lc.getEmissionWave();
RString name;
if (value != null) {
return rstring(getNameFromWavelength(value));
}
Iterator<Filter> i;
List<Filter> filters = channelData.getLightPathEmissionFilters();
if (filters != null) {
i = filters.iterator();
while (i.hasNext()) {
name = getNameFromFilter(i.next());
if (name != null) return name;
}
}
name = getNameFromFilter(channelData.getFilterSetEmissionFilter());
if (name != null) {
return name;
}
//Laser
LightSource ls = channelData.getLightSource();
if (ls != null) {
if (ls instanceof Laser) {
Laser laser = (Laser) ls;
value = laser.getWavelength();
if (value != null) {
return rstring(getNameFromWavelength(value));
}
}
}
value = lc.getExcitationWave();
if (value != null) {
return rstring(getNameFromWavelength(value));
}
filters = channelData.getLightPathExcitationFilters();
if (filters != null) {
i = filters.iterator();
while (i.hasNext()) {
name = getNameFromFilter(i.next());
if (name != null) return name;
}
}
return getNameFromFilter(channelData.getFilterSetExcitationFilter());
}
/**
* Processes the OMERO client side metadata store.
* @param store OMERO metadata store to process.
* @throws ModelException If there is an error during processing.
*/
public void process(IObjectContainerStore store) throws ModelException {
this.store = store;
reader = this.store.getReader();
if (reader == null) {
log.warn("Unexpected null reader.");
return;
}
List<Image> images = store.getSourceObjects(Image.class);
String[] domains = reader.getDomains();
boolean isGraphicsDomain = false;
for (String domain : domains) {
if (FormatTools.GRAPHICS_DOMAIN.equals(domain)) {
log.debug("Images are of the graphics domain.");
isGraphicsDomain = true;
break;
}
}
log.debug("isGraphicsDomain: "+isGraphicsDomain);
int count;
boolean v;
Map<ChannelData, Boolean> m;
Pixels pixels;
int sizeC;
ChannelData channelData;
Iterator<ChannelData> k;
for (int i = 0; i < images.size(); i++) {
pixels = (Pixels) store.getSourceObject(new LSID(Pixels.class, i));
if (pixels == null) {
throw new ModelException("Unable to locate Pixels:" + i);
}
//Require to reset transmitted light
count = 0;
m = new HashMap<ChannelData, Boolean>();
//Think of strategy for images with high number of channels
//i.e. > 6
sizeC = pixels.getSizeC().getValue();
if (sizeC == 1) {
channelData = ChannelData.fromObjectContainerStore(store, i, 0);
setSingleChannel(channelData);
} else {
for (int c = 0; c < sizeC; c++) {
channelData = ChannelData.fromObjectContainerStore(store, i, c);
boolean hasBFColor = hasColor(channelData.getChannel());
//Color section
populateDefault(channelData, isGraphicsDomain);
//only retrieve if not graphics
if (!isGraphicsDomain) {
//Determine if the channel same emission wavelength.
// don't allow the color to be reset to white if a color
// was set by Bio-Formats
v = ColorsFactory.hasEmissionData(channelData) || hasBFColor;
if (!v) {
count++;
}
m.put(channelData, v);
}
}
}
//Need to reset the color of transmitted light
//i.e. images with several "emission channels"
//check if 0 size
if (count > 0 && count != m.size()) {
k = m.keySet().iterator();
while (k.hasNext()) {
channelData = k.next();
if (!m.get(channelData)) {
int[] defaultColor = ColorsFactory.newWhiteColor();
Channel channel = channelData.getChannel();
channel.setRed(rint(defaultColor[ColorsFactory.RED_INDEX]));
channel.setGreen(rint(defaultColor[ColorsFactory.GREEN_INDEX]));
channel.setBlue(rint(defaultColor[ColorsFactory.BLUE_INDEX]));
channel.setAlpha(rint(defaultColor[ColorsFactory.ALPHA_INDEX]));
}
}
}
}
}
}