forked from ome/openmicroscopy
/
OmeroImageServiceImpl.java
1918 lines (1829 loc) · 60.9 KB
/
OmeroImageServiceImpl.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* org.openmicroscopy.shoola.env.data.OmeroImageServiceImpl
*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2014 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 org.openmicroscopy.shoola.env.data;
//Java import
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileFilter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
//Third-party libraries
import loci.common.RandomAccessInputStream;
import loci.formats.ImageReader;
import loci.formats.tiff.TiffParser;
import loci.formats.tiff.TiffSaver;
import com.sun.opengl.util.texture.TextureData;
//Application-internal dependencies
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportContainer;
import omero.api.RenderingEnginePrx;
import omero.api.ThumbnailStorePrx;
import omero.model.Annotation;
import omero.model.Channel;
import omero.model.Dataset;
import omero.model.IObject;
import omero.model.Image;
import omero.model.Pixels;
import omero.model.Project;
import omero.model.ProjectDatasetLink;
import omero.model.RenderingDef;
import omero.model.Screen;
import omero.model.ScreenI;
import omero.model.TagAnnotation;
import omero.romio.PlaneDef;
import omero.sys.Parameters;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.openmicroscopy.shoola.env.LookupNames;
import org.openmicroscopy.shoola.env.config.Registry;
import org.openmicroscopy.shoola.env.data.login.UserCredentials;
import org.openmicroscopy.shoola.env.data.model.ImportableFile;
import org.openmicroscopy.shoola.env.data.model.ImportableObject;
import org.openmicroscopy.shoola.env.data.model.MovieExportParam;
import org.openmicroscopy.shoola.env.data.model.ProjectionParam;
import org.openmicroscopy.shoola.env.data.model.ROIResult;
import org.openmicroscopy.shoola.env.data.model.FigureParam;
import org.openmicroscopy.shoola.env.data.model.SaveAsParam;
import org.openmicroscopy.shoola.env.data.model.ScriptObject;
import org.openmicroscopy.shoola.env.data.util.ModelMapper;
import org.openmicroscopy.shoola.env.data.util.PojoMapper;
import org.openmicroscopy.shoola.env.data.util.SecurityContext;
import org.openmicroscopy.shoola.env.data.util.StatusLabel;
import org.openmicroscopy.shoola.env.data.util.Target;
import org.openmicroscopy.shoola.env.log.LogMessage;
import org.openmicroscopy.shoola.env.rnd.RenderingControl;
import org.openmicroscopy.shoola.env.rnd.RenderingServiceException;
import org.openmicroscopy.shoola.env.rnd.PixelsServicesFactory;
import org.openmicroscopy.shoola.env.rnd.RndProxyDef;
import org.openmicroscopy.shoola.util.filter.file.OMETIFFFilter;
import org.openmicroscopy.shoola.util.filter.file.XMLFilter;
import org.openmicroscopy.shoola.util.image.geom.Factory;
import org.openmicroscopy.shoola.util.image.io.WriterImage;
import pojos.ChannelData;
import pojos.DataObject;
import pojos.DatasetData;
import pojos.ExperimenterData;
import pojos.FileAnnotationData;
import pojos.ImageData;
import pojos.PixelsData;
import pojos.ROIData;
import pojos.ScreenData;
import pojos.TagAnnotationData;
import pojos.WorkflowData;
/**
* Implementation of the {@link OmeroImageService} I/F.
*
* @author Jean-Marie Burel
* <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
* @author Donald MacDonald
* <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
* @since OME3.0
*/
class OmeroImageServiceImpl
implements OmeroImageService
{
/** The collection of supported file filters. */
private FileFilter[] filters;
/** Uses it to gain access to the container's services. */
private Registry context;
/** Reference to the entry point to access the <i>OMERO</i> services. */
private OMEROGateway gateway;
/**
* Returns the number of rendering engines to initialize or reload.
*
* @param ctx The security context.
* @param pixelsID The id of pixels set.
* @return See above.
* @throws DSOutOfServiceException If the connection is broken, or logged
* in.
* @throws DSAccessException If an error occurred while trying to
* retrieve data from OMEDS service.
*/
private int getNumberOfRenderingEngines(SecurityContext ctx, long pixelsID)
throws DSOutOfServiceException, DSAccessException
{
int number = 1;
Integer workers =
(Integer) context.lookup(LookupNames.RE_WORKER);
if (workers != null) {
number = workers.intValue();
if (number <= 0) number = 1;
}
if (!gateway.isLargeImage(ctx, pixelsID)) number = 1;
return number;
}
/**
* Imports the specified candidates.
*
* @param ctx The security context.
* @param candidates The file to import.
* @param status The original status.
* @param object The object hosting information about the import.
* @param ioList The containers where to import the files.
* @param list The list of annotations.
* @param userID The identifier of the user.
* @param hcs Value returns by the import containers.
* @param userName The login name of the user to import for.
*/
private Object importCandidates(SecurityContext ctx,
Map<File, StatusLabel> files, StatusLabel status,
ImportableObject object, IObject ioContainer,
List<Annotation> list, long userID, boolean close, boolean hcs,
String userName)
throws DSAccessException, DSOutOfServiceException
{
if (status.isMarkedAsCancel()) {
if (close) gateway.closeImport(ctx, userName);
return Boolean.valueOf(false);
}
Entry<File, StatusLabel> entry;
Iterator<Entry<File, StatusLabel>> jj = files.entrySet().iterator();
StatusLabel label = null;
File file;
boolean toClose = false;
int n = files.size()-1;
int index = 0;
ImportCandidates ic;
List<ImportContainer> icContainers;
ImportContainer importIc;
while (jj.hasNext()) {
entry = jj.next();
file = (File) entry.getKey();
if (hcs && !file.getName().endsWith(ImportableObject.DAT_EXTENSION))
if (ioContainer != null &&
!(ioContainer.getClass().equals(Screen.class) ||
ioContainer.getClass().equals(ScreenI.class)))
ioContainer = null;
label = (StatusLabel) entry.getValue();
if (close) {
toClose = index == n;
index++;
}
if (!label.isMarkedAsCancel()) {
try {
if (ioContainer == null) label.setNoContainer();
ic = gateway.getImportCandidates(ctx, object, file, status);
icContainers = ic.getContainers();
if (icContainers.size() == 0) {
Object o = status.getImportResult();
if (o instanceof ImportException) {
label.setCallback(o);
} else {
label.setCallback(new ImportException(
ImportException.FILE_NOT_VALID_TEXT));
}
} else {
//Check after scanning
if (label.isMarkedAsCancel())
label.setCallback(Boolean.valueOf(false));
else {
importIc = icContainers.get(0);
importIc.setCustomAnnotationList(list);
label.setCallback(gateway.importImageFile(ctx,
object, ioContainer, importIc,
label, toClose,
ImportableObject.isHCSFile(file),
userName));
}
}
} catch (Exception e) {
label.setCallback(e);
}
} else {
label.setCallback(Boolean.valueOf(false));
}
}
if (close) gateway.closeImport(ctx, userName);
return null;
}
/**
* Returns <code>true</code> if the binary data are available,
* <code>false</code> otherwise.
*
* @return See above.
*/
private boolean isBinaryAvailable()
{
Boolean b = (Boolean) context.lookup(LookupNames.BINARY_AVAILABLE);
if (b == null) return true;
return b.booleanValue();
}
/**
* Creates a <code>BufferedImage</code> from the passed array of bytes.
*
* @param values The array of bytes.
* @return See above.
* @throws RenderingServiceException If we cannot create an image.
*/
private BufferedImage createImage(byte[] values)
throws RenderingServiceException
{
try {
return WriterImage.bytesToImage(values);
} catch (Exception e) {
throw new RenderingServiceException("Cannot create buffered image",
e);
}
}
/**
* Creates a <code>BufferedImage</code> from the passed array of bytes.
*
* @param values The array of bytes.
* @return See above.
* @throws RenderingServiceException If we cannot create an image.
*/
private BufferedImage createImage(String path)
throws FSAccessException
{
try {
return ImageIO.read(new File(path));
} catch (Exception e) {
throw new FSAccessException("Cannot create buffered image",
e);
}
}
/**
* Recycles or creates the container.
*
* @param ctx The security context.
* @param dataset The dataset to create or recycle.
* @param container The container to create and link the dataset to.
* @param object The object hosting the import option.
* @param userName The name of the user to create the data for.
* @return See above.
* @throws DSOutOfServiceException If the connection is broken, or logged in
* @throws DSAccessException If an error occurred while trying to
* retrieve data from OMERO service.
*/
private IObject determineContainer(SecurityContext ctx, DatasetData dataset,
DataObject container, ImportableObject object, String userName)
throws DSOutOfServiceException, DSAccessException
{
IObject ioContainer = null;
Map<Object, Object> parameters = new HashMap<Object, Object>();
DataObject createdData;
IObject project = null;
IObject link;
if (dataset != null) { //dataset
if (dataset.getId() <= 0) {
//Check if it has been already been created.
//need to create it first
if (container != null) {
if (container.getId() <= 0) {
//project needs to be created to.
createdData = object.hasObjectBeenCreated(
container, ctx);
if (createdData == null) {
project = gateway.saveAndReturnObject(ctx,
container.asIObject(), parameters,
userName);
//register
object.addNewDataObject(
PojoMapper.asDataObject(
project));
//now create the dataset
ioContainer = gateway.saveAndReturnObject(ctx,
dataset.asIObject(), parameters, userName);
//register
object.registerDataset(
project.getId().getValue(),
(DatasetData)
PojoMapper.asDataObject(
ioContainer));
link = (ProjectDatasetLink)
ModelMapper.linkParentToChild(
(Dataset) ioContainer,
(Project) project);
link = (ProjectDatasetLink)
gateway.saveAndReturnObject(ctx, link,
parameters, userName);
} else {
DatasetData d;
d = object.isDatasetCreated(
createdData.getId(), dataset);
if (d == null) {
ioContainer = gateway.saveAndReturnObject(ctx,
dataset.asIObject(), parameters,
userName);
//register
object.registerDataset(
createdData.getId(),
(DatasetData)
PojoMapper.asDataObject(
ioContainer));
link = (ProjectDatasetLink)
ModelMapper.linkParentToChild(
(Dataset) ioContainer,
(Project) createdData.asProject());
link = (ProjectDatasetLink)
gateway.saveAndReturnObject(ctx, link,
parameters, userName);
} else ioContainer = d.asIObject();
}
} else { //project already exists.
createdData = object.isDatasetCreated(
container.getId(), dataset);
if (createdData == null) {
ioContainer = gateway.saveAndReturnObject(ctx,
dataset.asIObject(), parameters,
userName);
//register
object.registerDataset(
container.getId(),
(DatasetData)
PojoMapper.asDataObject(
ioContainer));
//Check that the project still exists
IObject ho = gateway.findIObject(ctx,
container.asIObject());
if (ho != null) {
link = (ProjectDatasetLink)
ModelMapper.linkParentToChild(
(Dataset) ioContainer,
(Project) container.asProject());
link = (ProjectDatasetLink)
gateway.saveAndReturnObject(ctx, link,
parameters, userName);
}
} else ioContainer = createdData.asIObject();
}
} else { //dataset w/o project.
createdData = object.hasObjectBeenCreated(dataset, ctx);
if (createdData == null) {
ioContainer = gateway.saveAndReturnObject(ctx,
dataset.asIObject(), parameters, userName);
//register
object.addNewDataObject(PojoMapper.asDataObject(
ioContainer));
} else ioContainer = createdData.asIObject();
}
} else ioContainer = dataset.asIObject();
} else { //check on the container.
if (container != null) {
if (container.getId() <= 0) {
//container needs to be created to.
createdData = object.hasObjectBeenCreated(
container, ctx);
if (createdData == null) {
ioContainer = gateway.saveAndReturnObject(ctx,
container.asIObject(), parameters, userName);
//register
object.addNewDataObject(
PojoMapper.asDataObject(
project));
} else {
ioContainer = createdData.asIObject();
}
} else ioContainer = container.asIObject();
}
}
//Check that the container still exist
return gateway.findIObject(ctx, ioContainer);
}
/**
* Creates a new instance.
*
* @param gateway Reference to the OMERO entry point.
* Mustn't be <code>null</code>.
* @param registry Reference to the registry. Mustn't be <code>null</code>.
*/
OmeroImageServiceImpl(OMEROGateway gateway, Registry registry)
{
if (registry == null)
throw new IllegalArgumentException("No registry.");
if (gateway == null)
throw new IllegalArgumentException("No gateway.");
context = registry;
this.gateway = gateway;
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#loadRenderingControl(SecurityContext, long)
*/
public RenderingControl loadRenderingControl(SecurityContext ctx,
long pixelsID)
throws DSOutOfServiceException, DSAccessException, FSAccessException
{
RenderingControl proxy =
PixelsServicesFactory.getRenderingControl(context,
Long.valueOf(pixelsID), true);
if (proxy == null) {
UserCredentials uc =
(UserCredentials) context.lookup(LookupNames.USER_CREDENTIALS);
int compressionLevel;
switch (uc.getSpeedLevel()) {
case UserCredentials.MEDIUM:
compressionLevel = RenderingControl.MEDIUM;
break;
case UserCredentials.LOW:
compressionLevel = RenderingControl.LOW;
break;
default:
compressionLevel = RenderingControl.UNCOMPRESSED;
}
Pixels pixels = gateway.getPixels(ctx, pixelsID);
if (pixels == null) return null;
int number = getNumberOfRenderingEngines(ctx, pixelsID);
ExperimenterData exp = (ExperimenterData) context.lookup(
LookupNames.CURRENT_USER_DETAILS);
List<RenderingEnginePrx> reList =
new ArrayList<RenderingEnginePrx>(number);
for (int i = 0; i < number; i++) {
reList.add(gateway.createRenderingEngine(ctx, pixelsID));
}
List<RndProxyDef> defs = gateway.getRenderingSettingsFor(
ctx, pixelsID, exp.getId());
Collection<Channel> l = pixels.copyChannels();
Iterator<Channel> i = l.iterator();
List<ChannelData> m = new ArrayList<ChannelData>(l.size());
int index = 0;
while (i.hasNext()) {
m.add(new ChannelData(index, i.next()));
index++;
}
proxy = PixelsServicesFactory.createRenderingControl(context, ctx,
reList, pixels, m, compressionLevel, defs);
}
return proxy;
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#renderImage(SecurityContext, long, PlaneDef, boolean, boolean)
*/
public Object renderImage(SecurityContext ctx, long pixelsID, PlaneDef pDef,
boolean asTexture, boolean largeImage)
throws RenderingServiceException
{
try {
return PixelsServicesFactory.render(context, ctx,
Long.valueOf(pixelsID), pDef, asTexture, largeImage);
} catch (Exception e) {
throw new RenderingServiceException("RenderImage", e);
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#isAlive(SecurityContext)
*/
public boolean isAlive(SecurityContext ctx) throws DSOutOfServiceException
{
return null != gateway.getConnector(ctx, true, true);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#shutDown(SecurityContext,long)
*/
public void shutDown(SecurityContext ctx, long pixelsID)
{
try {
if (!PixelsServicesFactory.shutDownRenderingControl(context,
pixelsID))
gateway.removeREService(ctx, pixelsID);
} catch (Exception e) {
context.getLogger().error(this, e.getMessage());
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#getThumbnail(SecurityContext, long, int, int, long)
*/
public BufferedImage getThumbnail(SecurityContext ctx,long pixID, int sizeX,
int sizeY, long userID)
throws RenderingServiceException
{
try {
if (pixID < 0) return null;
if (!isBinaryAvailable()) return null;
return createImage(gateway.getThumbnail(ctx, pixID, sizeX, sizeY,
userID));
} catch (Exception e) {
if (e instanceof DSOutOfServiceException) {
context.getLogger().error(this, e.getMessage());
return null;//getThumbnail(pixID, sizeX, sizeY, userID);
}
throw new RenderingServiceException("Get Thumbnail", e);
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#getThumbnailSet(SecurityContext, List, int)
*/
public Map<Long, BufferedImage> getThumbnailSet(SecurityContext ctx,
Collection<Long> pixelsID, int max)
throws RenderingServiceException
{
Map<Long, BufferedImage> r = new HashMap<Long, BufferedImage>();
List<Long> ids = new ArrayList<Long>();
Iterator i;
try {
if (pixelsID == null || pixelsID.size() == 0) return r;
Iterator j = pixelsID.iterator();
long id;
if (!isBinaryAvailable()) {
while (j.hasNext()) {
id = (Long) j.next();
r.put(id, null);
}
return r;
}
List blocks = new ArrayList();
int index = 0;
List l = null;
while (j.hasNext()) {
if (index == 0) {
l = new ArrayList();
}
l.add(j.next());
index++;
if (index == OMEROGateway.MAX_RETRIEVAL) {
blocks.add(l);
index = 0;
}
}
if (l != null && l.size() > 0)
blocks.add(l);
ids.addAll(pixelsID);
j = blocks.iterator();
Map m = new HashMap();
Map map;
while (j.hasNext()) {
map = gateway.getThumbnailSet(ctx, (List) j.next(), max, false);
m.putAll(map);
}
//m = gateway.getThumbnailSet(pixelsID, max, false);
if (m == null || m.size() == 0) {
i = ids.iterator();
while (i.hasNext())
r.put((Long) i.next(), null);
return r;
}
i = m.keySet().iterator();
byte[] values;
while (i.hasNext()) {
id = (Long) i.next();
values = (byte[]) m.get(id);
ids.remove(id);
if (values == null || values.length == 0)
r.put(id, null);
else {
try {
r.put(id, createImage(values));
} catch (Exception e) {
r.put(id, null);
}
}
}
//could not get a thumbnail for remaining images
if (ids.size() > 0) {
i = ids.iterator();
while (i.hasNext())
r.put((Long) i.next(), null);
}
return r;
} catch (Exception e) {
context.getLogger().error(this, e.getMessage());
if (ids.size() > 0) {
i = ids.iterator();
while (i.hasNext())
r.put((Long) i.next(), null);
}
return r;
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#reloadRenderingService(SecurityContext, long)
*/
public RenderingControl reloadRenderingService(SecurityContext ctx,
long pixelsID)
throws RenderingServiceException
{
RenderingControl proxy =
PixelsServicesFactory.getRenderingControl(context,
Long.valueOf(pixelsID), false);
if (proxy == null) return null;
try {
int number = getNumberOfRenderingEngines(ctx, pixelsID);
List<RenderingEnginePrx>
proxies = new ArrayList<RenderingEnginePrx>(number);
for (int i = 0; i < number; i++) {
proxies.add(gateway.createRenderingEngine(ctx, pixelsID));
}
return PixelsServicesFactory.reloadRenderingControl(context,
pixelsID, proxies);
} catch (Exception e) {
throw new RenderingServiceException("Cannot restart the " +
"rendering engine for : "+pixelsID, e);
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#resetRenderingService(SecurityContext, long)
*/
public RenderingControl resetRenderingService(SecurityContext ctx,
long pixelsID)
throws RenderingServiceException
{
RenderingControl proxy =
PixelsServicesFactory.getRenderingControl(context,
Long.valueOf(pixelsID), false);
if (proxy == null) return null;
try {
int number = getNumberOfRenderingEngines(ctx, pixelsID);
List<RenderingEnginePrx>
proxies = new ArrayList<RenderingEnginePrx>(number);
for (int i = 0; i < number; i++) {
proxies.add(gateway.createRenderingEngine(ctx, pixelsID));
}
ExperimenterData exp = (ExperimenterData) context.lookup(
LookupNames.CURRENT_USER_DETAILS);
RenderingDef def = gateway.getRenderingDef(ctx, pixelsID,
exp.getId());
return PixelsServicesFactory.resetRenderingControl(context,
pixelsID, proxies, def);
} catch (Exception e) {
throw new RenderingServiceException("Cannot restart the " +
"rendering engine for : "+pixelsID, e);
}
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#loadPixels(SecurityContext, long)
*/
public PixelsData loadPixels(SecurityContext ctx, long pixelsID)
throws DSOutOfServiceException, DSAccessException
{
if (pixelsID < 0)
throw new IllegalArgumentException("Pixels' ID not valid.");
return (PixelsData) PojoMapper.asDataObject(
gateway.getPixels(ctx, pixelsID));
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#getPlane(SecurityContext, long, int, int, int)
*/
public byte[] getPlane(SecurityContext ctx, long pixelsID, int z, int t,
int c)
throws DSOutOfServiceException, DSAccessException, FSAccessException
{
if (pixelsID < 0)
throw new IllegalArgumentException("Pixels' ID not valid.");
return gateway.getPlane(ctx, pixelsID, z, t, c);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#pasteRenderingSettings(SecurityContext, long,
* Class, List)
*/
public Map pasteRenderingSettings(SecurityContext ctx, long pixelsID,
Class rootNodeType, List nodesID)
throws DSOutOfServiceException, DSAccessException
{
if (nodesID == null || nodesID.size() == 0)
throw new IllegalArgumentException("No nodes specified.");
return gateway.pasteRenderingSettings(ctx, pixelsID, rootNodeType,
nodesID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#resetRenderingSettings(Class, List)
*/
public Map resetRenderingSettings(SecurityContext ctx, Class rootNodeType,
List nodesID)
throws DSOutOfServiceException, DSAccessException
{
if (nodesID == null || nodesID.size() == 0)
throw new IllegalArgumentException("No nodes specified.");
return gateway.resetRenderingSettings(ctx, rootNodeType, nodesID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#setMinMaxSettings(SecurityContext, Class, List)
*/
public Map setMinMaxSettings(SecurityContext ctx, Class rootNodeType,
List nodesID)
throws DSOutOfServiceException, DSAccessException
{
if (nodesID == null || nodesID.size() == 0)
throw new IllegalArgumentException("No nodes specified.");
return gateway.setMinMaxSettings(ctx, rootNodeType, nodesID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#setOwnerRenderingSettings(SecurityContext, Class,
* List)
*/
public Map setOwnerRenderingSettings(SecurityContext ctx,
Class rootNodeType, List nodesID)
throws DSOutOfServiceException, DSAccessException
{
if (nodesID == null || nodesID.size() == 0)
throw new IllegalArgumentException("No nodes specified.");
return gateway.setOwnerRenderingSettings(ctx, rootNodeType, nodesID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#getRenderingSettings(ctx, long, long)
*/
public Map<DataObject, Collection<RndProxyDef>> getRenderingSettings(
SecurityContext ctx, long pixelsID, long userID)
throws DSOutOfServiceException, DSAccessException
{
return gateway.getRenderingSettings(ctx, pixelsID, userID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#getRenderingSettingsFor(long, long)
*/
public List<RndProxyDef> getRenderingSettingsFor(SecurityContext ctx,
long pixelsID, long userID)
throws DSOutOfServiceException, DSAccessException
{
return gateway.getRenderingSettingsFor(ctx, pixelsID, userID);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#renderProjected(SecurityContext, long, int, int,
* int, int, List)
*/
public BufferedImage renderProjected(SecurityContext ctx, long pixelsID,
int startZ, int endZ, int stepping, int type, List<Integer> channels)
throws RenderingServiceException, DSOutOfServiceException
{
return PixelsServicesFactory.renderProjected(context, pixelsID, startZ,
endZ, type, stepping, channels);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#renderProjectedAsTexture(SecurityContext, long,
* int, int, int, int, List)
*/
public TextureData renderProjectedAsTexture(SecurityContext ctx,
long pixelsID, int startZ, int endZ, int stepping, int type,
List<Integer> channels)
throws RenderingServiceException, DSOutOfServiceException
{
return PixelsServicesFactory.renderProjectedAsTexture(context,
pixelsID, startZ, endZ, type, stepping, channels);
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#projectImage(SecurityContext, ProjectionParam)
*/
public ImageData projectImage(SecurityContext ctx, ProjectionParam ref)
throws DSOutOfServiceException, DSAccessException
{
if (ref == null) return null;
ImageData image = gateway.projectImage(ctx, ref.getPixelsID(),
ref.getStartT(), ref.getEndT(), ref.getStartZ(),
ref.getEndZ(), ref.getStepping(), ref.getProjectionType(),
ref.getChannels(), ref.getName(), ref.getPixelsType());
if (image == null) return null;
Image img = image.asImage();
img.setDescription(omero.rtypes.rstring(ref.getDescription()));
image = (ImageData)
PojoMapper.asDataObject(gateway.updateObject(ctx, img,
new Parameters()));
image = gateway.getImage(ctx, image.getId(), new Parameters());
List<DatasetData> datasets = ref.getDatasets();
if (datasets != null && datasets.size() > 0) {
Iterator<DatasetData> i = datasets.iterator();
//Check if we need to create a dataset.
List<DatasetData> existing = new ArrayList<DatasetData>();
List<DatasetData> toCreate = new ArrayList<DatasetData>();
DatasetData dataset;
while (i.hasNext()) {
dataset = i.next();
if (dataset.getId() > 0) existing.add(dataset);
else toCreate.add(dataset);
}
if (toCreate.size() > 0) {
i = toCreate.iterator();
OmeroDataService svc = context.getDataService();
while (i.hasNext()) {
existing.add((DatasetData)
svc.createDataObject(ctx, i.next(),
ref.getDatasetParent(), null));
}
}
List<IObject> links = new ArrayList<IObject>(datasets.size());
img = image.asImage();
IObject l;
i = existing.iterator();
while (i.hasNext()) {
l = ModelMapper.linkParentToChild(img, i.next().asIObject());
links.add(l);
}
gateway.createObjects(ctx, links);
}
return image;
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#createRenderingSettings(SecurityContext, long,
* RndProxyDef, List)
*/
public Boolean createRenderingSettings(SecurityContext ctx, long pixelsID,
RndProxyDef rndToCopy, List<Integer> indexes)
throws DSOutOfServiceException, DSAccessException, FSAccessException
{
if (rndToCopy == null) {
RenderingDef def = gateway.createRenderingDef(ctx, pixelsID);
return (def != null);
}
RenderingControl rndControl = loadRenderingControl(ctx, pixelsID);
try {
rndControl.copyRenderingSettings(rndToCopy, indexes);
//save them
rndControl.saveCurrentSettings();
//discard it
shutDown(ctx, pixelsID);
} catch (Exception e) {
throw new DSAccessException("Unable to copy the " +
"rendering settings.");
}
return Boolean.TRUE;
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#loadPlaneInfo(SecurityContext, long, int, int, int)
*/
public Collection loadPlaneInfo(SecurityContext ctx, long pixelsID, int z,
int t, int channel)
throws DSOutOfServiceException, DSAccessException
{
return gateway.loadPlaneInfo(ctx, pixelsID, z, t, channel);
}
/**
* Returns <code>true</code> if the containers are <code>HCS</code>
* containers, <code>false</code> otherwise.
*
* @param containers The collection to handle.
* @return See above.
*/
private boolean isHCS(List<ImportContainer> containers)
{
if (containers == null || containers.size() == 0) return false;
int count = 0;
Iterator<ImportContainer> i = containers.iterator();
ImportContainer ic;
while (i.hasNext()) {
ic = i.next();
if (ic.getIsSPW()) count++;
}
return count == containers.size();
}
/**
* Implemented as specified by {@link OmeroImageService}.
* @see OmeroImageService#importFile(ImportableObject,
* ImportableFile, long, long, boolean)
*/
public Object importFile(ImportableObject object,
ImportableFile importable, boolean close)
throws ImportException, DSAccessException, DSOutOfServiceException
{
if (importable == null || importable.getFile() == null)
throw new IllegalArgumentException("No images to import.");
StatusLabel status = importable.getStatus();
SecurityContext ctx = new SecurityContext(importable.getGroup().getId());
//If import as.
ExperimenterData loggedIn = context.getAdminService().getUserDetails();
long userID = loggedIn.getId();
String userName = null;
if (importable.getUser() != null) {
ExperimenterData exp = importable.getUser();
userID = exp.getId();
if (exp.getId() != loggedIn.getId())
userName = exp.getUserName();
}
if (status.isMarkedAsCancel()) {
if (close) gateway.closeImport(ctx, userName);
return Boolean.valueOf(false);
}
Collection<TagAnnotationData> tags = object.getTags();
List<Annotation> customAnnotationList = new ArrayList<Annotation>();
List<IObject> l;
//Tags
Map<Object, Object> parameters = new HashMap<Object, Object>();
if (!CollectionUtils.isEmpty(tags)) {
List<TagAnnotationData> values = new ArrayList<TagAnnotationData>();
Iterator<TagAnnotationData> i = tags.iterator();
TagAnnotationData tag;
l = new ArrayList<IObject>();
while (i.hasNext()) {