/
FormatReader.java
1897 lines (1676 loc) · 58.1 KB
/
FormatReader.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
/*
* #%L
* Top-level reader and writer APIs
* %%
* Copyright (C) 2005 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package loci.formats;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.Arrays;
import loci.common.DataTools;
import loci.common.Location;
import loci.common.RandomAccessInputStream;
import loci.common.services.DependencyException;
import loci.common.services.ServiceException;
import loci.common.services.ServiceFactory;
import loci.formats.in.DynamicMetadataOptions;
import loci.formats.in.MetadataLevel;
import loci.formats.in.MetadataOptions;
import loci.formats.meta.DummyMetadata;
import loci.formats.meta.FilterMetadata;
import loci.formats.meta.IMetadata;
import loci.formats.meta.MetadataRetrieve;
import loci.formats.meta.MetadataStore;
import loci.formats.ome.OMEXMLMetadata;
import loci.formats.services.OMEXMLService;
import ome.xml.model.AffineTransform;
import ome.xml.model.enums.AcquisitionMode;
import ome.xml.model.enums.ArcType;
import ome.xml.model.enums.Binning;
import ome.xml.model.enums.Compression;
import ome.xml.model.enums.ContrastMethod;
import ome.xml.model.enums.Correction;
import ome.xml.model.enums.DetectorType;
import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.EnumerationException;
import ome.xml.model.enums.ExperimentType;
import ome.xml.model.enums.FilamentType;
import ome.xml.model.enums.FillRule;
import ome.xml.model.enums.FilterType;
import ome.xml.model.enums.FontFamily;
import ome.xml.model.enums.FontStyle;
import ome.xml.model.enums.IlluminationType;
import ome.xml.model.enums.Immersion;
import ome.xml.model.enums.LaserMedium;
import ome.xml.model.enums.LaserType;
import ome.xml.model.enums.Marker;
import ome.xml.model.enums.Medium;
import ome.xml.model.enums.MicrobeamManipulationType;
import ome.xml.model.enums.MicroscopeType;
import ome.xml.model.enums.NamingConvention;
import ome.xml.model.enums.PixelType;
import ome.xml.model.enums.Pulse;
import ome.xml.model.enums.handlers.AcquisitionModeEnumHandler;
import ome.xml.model.enums.handlers.ArcTypeEnumHandler;
import ome.xml.model.enums.handlers.BinningEnumHandler;
import ome.xml.model.enums.handlers.CompressionEnumHandler;
import ome.xml.model.enums.handlers.ContrastMethodEnumHandler;
import ome.xml.model.enums.handlers.CorrectionEnumHandler;
import ome.xml.model.enums.handlers.DetectorTypeEnumHandler;
import ome.xml.model.enums.handlers.DimensionOrderEnumHandler;
import ome.xml.model.enums.handlers.ExperimentTypeEnumHandler;
import ome.xml.model.enums.handlers.FilamentTypeEnumHandler;
import ome.xml.model.enums.handlers.FillRuleEnumHandler;
import ome.xml.model.enums.handlers.FilterTypeEnumHandler;
import ome.xml.model.enums.handlers.FontFamilyEnumHandler;
import ome.xml.model.enums.handlers.FontStyleEnumHandler;
import ome.xml.model.enums.handlers.IlluminationTypeEnumHandler;
import ome.xml.model.enums.handlers.ImmersionEnumHandler;
import ome.xml.model.enums.handlers.LaserMediumEnumHandler;
import ome.xml.model.enums.handlers.LaserTypeEnumHandler;
import ome.xml.model.enums.handlers.MarkerEnumHandler;
import ome.xml.model.enums.handlers.MediumEnumHandler;
import ome.xml.model.enums.handlers.MicrobeamManipulationTypeEnumHandler;
import ome.xml.model.enums.handlers.MicroscopeTypeEnumHandler;
import ome.xml.model.enums.handlers.NamingConventionEnumHandler;
import ome.xml.model.enums.handlers.PixelTypeEnumHandler;
import ome.xml.model.enums.handlers.PulseEnumHandler;
/**
* Abstract superclass of all biological file format readers.
*/
public abstract class FormatReader extends FormatHandler
implements IFormatReader
{
// -- Constants --
/** Default thumbnail width and height. */
protected static final int THUMBNAIL_DIMENSION = 128;
// -- Fields --
/** Current file. */
protected transient RandomAccessInputStream in;
/** Hashtable containing metadata key/value pairs. */
protected Hashtable<String, Object> metadata;
/** The number of the current series. */
protected int coreIndex = 0;
/** The number of the current series (non flat). */
protected int series = 0;
/** Core metadata values. */
protected List<CoreMetadata> core;
/** The number of the current resolution. */
protected int resolution = 0;
/** Whether or not resolutions are flattened. */
protected boolean flattenedResolutions = true;
/**
* Whether the file extension matching one of the reader's suffixes
* is necessary to identify the file as an instance of this format.
*/
protected boolean suffixNecessary = true;
/**
* Whether the file extension matching one of the reader's suffixes
* is sufficient to identify the file as an instance of this format.
*/
protected boolean suffixSufficient = true;
/** Whether this format supports multi-file datasets. */
protected boolean hasCompanionFiles = false;
/** Short description of the structure of the dataset. */
protected String datasetDescription = "Single file";
/** Whether or not to normalize float data. */
protected boolean normalizeData;
/** Whether or not to filter out invalid metadata. */
protected boolean filterMetadata;
/** Whether or not to save proprietary metadata in the MetadataStore. */
protected boolean saveOriginalMetadata = false;
/** Whether or not MetadataStore sets C = 3 for indexed color images. */
protected boolean indexedAsRGB = false;
/** Whether or not to group multi-file formats. */
protected boolean group = true;
/** Fill value for undefined pixels. */
protected Byte fillColor = null;
/** List of domains in which this format is used. */
protected String[] domains = new String[0];
/**
* Current metadata store. Should never be accessed directly as the
* semantics of {@link #getMetadataStore()} prevent "null" access.
*/
protected MetadataStore metadataStore = new DummyMetadata();
private ServiceFactory factory;
private OMEXMLService service;
// -- Constructors --
/** Constructs a format reader with the given name and default suffix. */
public FormatReader(String format, String suffix) { super(format, suffix); }
/** Constructs a format reader with the given name and default suffixes. */
public FormatReader(String format, String[] suffixes) {
super(format, suffixes);
}
// -- Internal FormatReader API methods --
/* @see IFormatReader#reopenFile() */
@Override
public void reopenFile() throws IOException {
if (in != null) {
in.close();
}
in = new RandomAccessInputStream(currentId);
in.order(isLittleEndian());
}
/**
* Initializes the given file (parsing header information, etc.).
* Most subclasses should override this method to perform
* initialization operations such as parsing metadata.
*
* @throws FormatException if a parsing error occurs processing the file.
* @throws IOException if an I/O error occurs processing the file
*/
protected void initFile(String id) throws FormatException, IOException {
LOGGER.debug("{}.initFile({})", this.getClass().getName(), id);
if (currentId != null) {
String[] s = getUsedFiles();
for (int i=0; i<s.length; i++) {
if (id.equals(s[i])) return;
}
}
coreIndex = 0;
series = 0;
close();
currentId = id;
metadata = new Hashtable<String, Object>();
core = new ArrayList<CoreMetadata>();
CoreMetadata core0 = new CoreMetadata();
core.add(core0);
core0.orderCertain = true;
// reinitialize the MetadataStore
// NB: critical for metadata conversion to work properly!
getMetadataStore().createRoot();
String optionsFile = DynamicMetadataOptions.getMetadataOptionsFile(id);
if (optionsFile != null) {
MetadataOptions options = getMetadataOptions();
if (options != null && options instanceof DynamicMetadataOptions) {
((DynamicMetadataOptions) options).loadOptions(optionsFile, getAvailableOptions());
}
}
}
/** Returns the list of available metadata options. */
protected ArrayList<String> getAvailableOptions() {
ArrayList<String> optionsList = new ArrayList<String>();
optionsList.add(DynamicMetadataOptions.METADATA_LEVEL_KEY);
optionsList.add(DynamicMetadataOptions.READER_VALIDATE_KEY);
return optionsList;
}
/** Returns true if the given file name is in the used files list. */
protected boolean isUsedFile(String file) {
String[] usedFiles = getUsedFiles();
for (String used : usedFiles) {
if (used.equals(file)) return true;
String path = new Location(file).getAbsolutePath();
if (used.equals(path)) return true;
}
return false;
}
/** Adds an entry to the specified Hashtable. */
protected void addMeta(String key, Object value,
Hashtable<String, Object> meta)
{
if (key == null || value == null ||
getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM)
{
return;
}
key = key.trim();
boolean string = value instanceof String || value instanceof Character;
// string value, if passed in value is a string
String val = string ? String.valueOf(value) : null;
if (filterMetadata ||
(saveOriginalMetadata && (getMetadataStore() instanceof OMEXMLMetadata)))
{
// filter out complex data types
boolean simple = string ||
value instanceof Number ||
value instanceof Boolean;
if (!simple) return;
// verify key & value are reasonable length
int maxLen = 8192;
if (key.length() > maxLen) return;
if (string && val.length() > maxLen) return;
// remove all non-printable characters
key = DataTools.sanitize(key);
if (string) val = DataTools.sanitize(val);
// verify key contains at least one alphabetic character
if (!key.matches(".*[a-zA-Z].*")) return;
// remove <, > and & to prevent XML parsing errors
String[] invalidSequences = new String[] {
"<", ">", "&", "<", ">", "&"
};
for (int i=0; i<invalidSequences.length; i++) {
if (key.indexOf(invalidSequences[i]) >= 0) {
key = key.replaceAll(invalidSequences[i], "");
}
if (string && val.indexOf(invalidSequences[i]) >= 0) {
val = val.replaceAll(invalidSequences[i], "");
}
}
// verify key & value are not empty
if (key.length() == 0) return;
if (string && val.trim().length() == 0) return;
if (string) value = val;
}
meta.put(key, val == null ? value : val);
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, Object value) {
addMeta(key, value, metadata);
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, boolean value) {
addGlobalMeta(key, Boolean.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, byte value) {
addGlobalMeta(key, Byte.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, short value) {
addGlobalMeta(key, Short.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, int value) {
addGlobalMeta(key, Integer.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, long value) {
addGlobalMeta(key, Long.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, float value) {
addGlobalMeta(key, Float.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, double value) {
addGlobalMeta(key, Double.valueOf(value));
}
/** Adds an entry to the global metadata table. */
protected void addGlobalMeta(String key, char value) {
addGlobalMeta(key, Character.valueOf(value));
}
/** Gets a value from the global metadata table. */
protected Object getGlobalMeta(String key) {
return metadata.get(key);
}
/**
* Add the given key/value pair to the given hashtable.
* If the key already exists, a list will be stored in the hashtable
* and the value will be appended to the list.
* @param key the key to store in the hashtable
* @param value the value to store in the hashtable or list
* @param meta the hashtable in which to store the key/value
*/
protected void addMetaList(String key, Object value,
Hashtable<String, Object> meta)
{
Object v = meta.remove(key);
Vector list = null;
if (v != null && v instanceof Vector) {
list = (Vector) v;
}
else if (v != null) {
list = new Vector();
list.add(v);
}
addMeta(key, value, meta);
Object newValue = meta.remove(key);
if (newValue != null) {
if (list == null) {
list = new Vector();
}
list.add(newValue);
meta.put(key, list);
}
else if (list != null) {
meta.put(key, list);
}
}
/**
* Add the given key/value pair to the global metadata hashtable.
* If the key already exists, a list will be stored in the hashtable
* and the value will be appended to the list.
* @param key the key to store in the hashtable
* @param value the value to store in the hashtable or list
*/
protected void addGlobalMetaList(String key, Object value) {
addMetaList(key, value, metadata);
}
/**
* Add the given key/value pair to the current series metadata hashtable.
* If the key already exists, a list will be stored in the hashtable
* and the value will be appended to the list.
*/
protected void addSeriesMetaList(String key, Object value) {
addMetaList(key, value, getCurrentCore().seriesMetadata);
}
/**
* Call {@link #updateMetadataLists(Hashtable)} on
* all metadata hashtables.
*/
protected void flattenHashtables() {
updateMetadataLists(metadata);
for (int s=0; s<core.size(); s++) {
if (core.get(s).seriesMetadata.size() > 0) {
updateMetadataLists(core.get(s).seriesMetadata);
}
}
}
/**
* For the given metadata hashtable, replace any value that is
* a list with one key/value pair per list entry. The new keys
* will be the original key with the list index appended.
* @param meta the hashtable from which to remove lists
*/
protected void updateMetadataLists(Hashtable<String, Object> meta) {
String[] keys = meta.keySet().toArray(new String[meta.size()]);
for (String key : keys) {
Object v = meta.get(key);
if (v instanceof Vector) {
Vector list = (Vector) v;
if (list.size() == 1) {
meta.put(key, list.get(0));
}
else {
int digits = String.valueOf(list.size()).length();
for (int i=0; i<list.size(); i++) {
String index = String.valueOf(i + 1);
while (index.length() < digits) {
index = "0" + index;
}
meta.put(key + " #" + index, list.get(i));
}
meta.remove(key);
}
}
}
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, Object value) {
addMeta(key, value, getCurrentCore().seriesMetadata);
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, boolean value) {
addSeriesMeta(key, Boolean.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, byte value) {
addSeriesMeta(key, Byte.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, short value) {
addSeriesMeta(key, Short.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, int value) {
addSeriesMeta(key, Integer.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, long value) {
addSeriesMeta(key, Long.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, float value) {
addSeriesMeta(key, Float.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, double value) {
addSeriesMeta(key, Double.valueOf(value));
}
/** Adds an entry to the metadata table for the current series. */
protected void addSeriesMeta(String key, char value) {
addSeriesMeta(key, Character.valueOf(value));
}
/** Gets an entry from the metadata table for the current series. */
protected Object getSeriesMeta(String key) {
return getCurrentCore().seriesMetadata.get(key);
}
/** Reads a raw plane from disk. */
protected byte[] readPlane(RandomAccessInputStream s, int x, int y,
int w, int h, byte[] buf) throws IOException
{
return readPlane(s, x, y, w, h, 0, buf);
}
/** Reads a raw plane from disk. */
protected byte[] readPlane(RandomAccessInputStream s, int x, int y,
int w, int h, int scanlinePad, byte[] buf) throws IOException
{
return readPlane(s, x, y, w, h, scanlinePad, getSizeX(), getSizeY(), buf);
}
protected byte[] readPlane(RandomAccessInputStream s, int x, int y,
int w, int h, int scanlinePad, int imageWidth, int imageHeight, byte[] buf)
throws IOException
{
int c = getRGBChannelCount();
int bpp = FormatTools.getBytesPerPixel(getPixelType());
if (x == 0 && y == 0 && w == imageWidth && h == imageHeight &&
scanlinePad == 0)
{
s.read(buf);
}
else if (x == 0 && w == imageWidth && scanlinePad == 0) {
if (isInterleaved()) {
s.skipBytes((long) y * w * bpp * c);
s.read(buf, 0, h * w * bpp * c);
}
else {
int rowLen = w * bpp;
for (int channel=0; channel<c; channel++) {
s.skipBytes((long) y * rowLen);
s.read(buf, channel * h * rowLen, h * rowLen);
if (channel < c - 1) {
// no need to skip bytes after reading final channel
s.skipBytes((long) (imageHeight - y - h) * rowLen);
}
}
}
}
else {
long scanlineWidth = imageWidth + scanlinePad;
if (isInterleaved()) {
s.skipBytes(y * scanlineWidth * bpp * c);
for (int row=0; row<h; row++) {
s.skipBytes(x * bpp * c);
s.read(buf, row * w * bpp * c, w * bpp * c);
if (row < h - 1) {
// no need to skip bytes after reading final row
s.skipBytes(bpp * c * (scanlineWidth - w - x));
}
}
}
else {
for (int channel=0; channel<c; channel++) {
s.skipBytes(y * scanlineWidth * bpp);
for (int row=0; row<h; row++) {
s.skipBytes(x * bpp);
s.read(buf, channel * w * h * bpp + row * w * bpp, w * bpp);
if (row < h - 1 || channel < c - 1) {
// no need to skip bytes after reading final row of final channel
s.skipBytes(bpp * (scanlineWidth - w - x));
}
}
if (channel < c - 1) {
// no need to skip bytes after reading final channel
s.skipBytes(scanlineWidth * bpp * (imageHeight - y - h));
}
}
}
}
return buf;
}
/** Return a properly configured loci.formats.meta.FilterMetadata. */
protected MetadataStore makeFilterMetadata() {
return new FilterMetadata(getMetadataStore(), isMetadataFiltered());
}
// -- IFormatReader API methods --
/**
* Checks if a file matches the type of this format reader.
* Checks filename suffixes against those known for this format.
* If the suffix check is inconclusive and the open parameter is true,
* the file is opened and tested with
* {@link #isThisType(RandomAccessInputStream)}.
*
* @param open If true, and the file extension is insufficient to determine
* the file type, the (existing) file is opened for further analysis.
*/
@Override
public boolean isThisType(String name, boolean open) {
// if file extension ID is insufficient and we can't open the file, give up
if (!suffixSufficient && !open) return false;
if (suffixNecessary || suffixSufficient) {
// it's worth checking the file extension
boolean suffixMatch = super.isThisType(name);
// if suffix match is required but it doesn't match, failure
if (suffixNecessary && !suffixMatch) return false;
// if suffix matches and that's all we need, green light it
if (suffixMatch && suffixSufficient) return true;
}
// suffix matching was inconclusive; we need to analyze the file contents
if (!open) return false; // not allowed to open any files
try (RandomAccessInputStream stream = new RandomAccessInputStream(name)) {
return isThisType(stream);
}
catch (IOException exc) {
LOGGER.debug("", exc);
return false;
}
}
/* @see IFormatReader#isThisType(byte[]) */
@Override
public boolean isThisType(byte[] block) {
try (RandomAccessInputStream stream = new RandomAccessInputStream(block)) {
return isThisType(stream);
}
catch (IOException e) {
LOGGER.debug("", e);
}
return false;
}
/* @see IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
return false;
}
/* @see IFormatReader#getImageCount() */
@Override
public int getImageCount() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().imageCount;
}
/* @see IFormatReader#isRGB() */
@Override
public boolean isRGB() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().rgb;
}
/* @see IFormatReader#getSizeX() */
@Override
public int getSizeX() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().sizeX;
}
/* @see IFormatReader#getSizeY() */
@Override
public int getSizeY() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().sizeY;
}
/* @see IFormatReader#getSizeZ() */
@Override
public int getSizeZ() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().sizeZ;
}
/* @see IFormatReader#getSizeC() */
@Override
public int getSizeC() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().sizeC;
}
/* @see IFormatReader#getSizeT() */
@Override
public int getSizeT() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().sizeT;
}
/* @see IFormatReader#getPixelType() */
@Override
public int getPixelType() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().pixelType;
}
/* @see IFormatReader#getBitsPerPixel() */
@Override
public int getBitsPerPixel() {
FormatTools.assertId(currentId, true, 1);
if (getCurrentCore().bitsPerPixel == 0) {
getCurrentCore().bitsPerPixel =
FormatTools.getBytesPerPixel(getPixelType()) * 8;
}
return getCurrentCore().bitsPerPixel;
}
/* @see IFormatReader#getEffectiveSizeC() */
@Override
public int getEffectiveSizeC() {
// NB: by definition, imageCount == effectiveSizeC * sizeZ * sizeT
int sizeZT = getSizeZ() * getSizeT();
if (sizeZT == 0) return 0;
return getImageCount() / sizeZT;
}
/* @see IFormatReader#getRGBChannelCount() */
@Override
public int getRGBChannelCount() {
int effSizeC = getEffectiveSizeC();
if (effSizeC == 0) return 0;
return getSizeC() / effSizeC;
}
/* @see IFormatReader#isIndexed() */
@Override
public boolean isIndexed() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().indexed;
}
/* @see IFormatReader#isFalseColor() */
@Override
public boolean isFalseColor() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().falseColor;
}
/* @see IFormatReader#get8BitLookupTable() */
@Override
public byte[][] get8BitLookupTable() throws FormatException, IOException {
return null;
}
/* @see IFormatReader#get16BitLookupTable() */
@Override
public short[][] get16BitLookupTable() throws FormatException, IOException {
return null;
}
/* @see IFormatReader#getModuloZ() */
@Override
public Modulo getModuloZ() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().moduloZ;
}
/* @see IFormatReader#getModuloC() */
@Override
public Modulo getModuloC() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().moduloC;
}
/* @see IFormatReader#getModuloT() */
@Override
public Modulo getModuloT() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().moduloT;
}
/* @see IFormatReader#getThumbSizeX() */
@Override
public int getThumbSizeX() {
FormatTools.assertId(currentId, true, 1);
if (getCurrentCore().thumbSizeX == 0) {
int sx = getSizeX();
int sy = getSizeY();
int thumbSizeX = 0;
if (sx < THUMBNAIL_DIMENSION && sy < THUMBNAIL_DIMENSION)
thumbSizeX = sx;
else if (sx > sy) thumbSizeX = THUMBNAIL_DIMENSION;
else if (sy > 0) thumbSizeX = sx * THUMBNAIL_DIMENSION / sy;
if (thumbSizeX == 0) thumbSizeX = 1;
return thumbSizeX;
}
return getCurrentCore().thumbSizeX;
}
/* @see IFormatReader#getThumbSizeY() */
@Override
public int getThumbSizeY() {
FormatTools.assertId(currentId, true, 1);
if (getCurrentCore().thumbSizeY == 0) {
int sx = getSizeX();
int sy = getSizeY();
int thumbSizeY = 1;
if (sx < THUMBNAIL_DIMENSION && sy < THUMBNAIL_DIMENSION)
thumbSizeY = sy;
else if (sy > sx) thumbSizeY = THUMBNAIL_DIMENSION;
else if (sx > 0) thumbSizeY = sy * THUMBNAIL_DIMENSION / sx;
if (thumbSizeY == 0) thumbSizeY = 1;
return thumbSizeY;
}
return getCurrentCore().thumbSizeY;
}
/* @see IFormatReader.isLittleEndian() */
@Override
public boolean isLittleEndian() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().littleEndian;
}
/* @see IFormatReader#getDimensionOrder() */
@Override
public String getDimensionOrder() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().dimensionOrder;
}
/* @see IFormatReader#isOrderCertain() */
@Override
public boolean isOrderCertain() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().orderCertain;
}
/* @see IFormatReader#isThumbnailSeries() */
@Override
public boolean isThumbnailSeries() {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().thumbnail;
}
/* @see IFormatReader#isInterleaved() */
@Override
public boolean isInterleaved() {
return isInterleaved(0);
}
/* @see IFormatReader#isInterleaved(int) */
@Override
public boolean isInterleaved(int subC) {
FormatTools.assertId(currentId, true, 1);
return getCurrentCore().interleaved;
}
/* @see IFormatReader#openBytes(int) */
@Override
public byte[] openBytes(int no) throws FormatException, IOException {
return openBytes(no, 0, 0, getSizeX(), getSizeY());
}
/* @see IFormatReader#openBytes(int, byte[]) */
@Override
public byte[] openBytes(int no, byte[] buf)
throws FormatException, IOException
{
return openBytes(no, buf, 0, 0, getSizeX(), getSizeY());
}
/* @see IFormatReader#openBytes(int, int, int, int, int) */
@Override
public byte[] openBytes(int no, int x, int y, int w, int h)
throws FormatException, IOException
{
int ch = getRGBChannelCount();
int bpp = FormatTools.getBytesPerPixel(getPixelType());
byte[] newBuffer;
try {
newBuffer = DataTools.allocate(w, h, ch, bpp);
}
catch (IllegalArgumentException e) {
throw new FormatException("Image plane too large. Only 2GB of data can " +
"be extracted at one time. You can work around the problem by opening " +
"the plane in tiles; for further details, see: " +
"https://docs.openmicroscopy.org/bio-formats/" + FormatTools.VERSION +
"/about/bug-reporting.html#common-issues-to-check", e);
}
return openBytes(no, newBuffer, x, y, w, h);
}
/* @see IFormatReader#openBytes(int, byte[], int, int, int, int) */
@Override
public abstract byte[] openBytes(int no, byte[] buf, int x, int y,
int w, int h) throws FormatException, IOException;
/* @see IFormatReader#openPlane(int, int, int, int, int int) */
@Override
public Object openPlane(int no, int x, int y, int w, int h)
throws FormatException, IOException
{
// NB: Readers use byte arrays by default as the native type.
return openBytes(no, x, y, w, h);
}
/* @see IFormatReader#openThumbBytes(int) */
@Override
public byte[] openThumbBytes(int no) throws FormatException, IOException {
FormatTools.assertId(currentId, true, 1);
return FormatTools.openThumbBytes(this, no);
}
/* @see IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
if (in != null) in.close();
if (!fileOnly) {
in = null;
currentId = null;
resolution = 0;
core = null;
}
}
/* @see IFormatReader#getSeriesCount() */
@Override
public int getSeriesCount() {
FormatTools.assertId(currentId, true, 1);
if (hasFlattenedResolutions()) {
return core.size();
}
return coreIndexToSeries(core.size() - 1) + 1;
}
/* @see IFormatReader#setSeries(int) */
@Override
public void setSeries(int no) {
coreIndex = seriesToCoreIndex(no);
series = no;
resolution = 0;
}
/* @see IFormatReader#getSeries() */
@Override
public int getSeries() {
return series;
}
/* @see IFormatReader#setGroupFiles(boolean) */
@Override
public void setGroupFiles(boolean groupFiles) {
FormatTools.assertId(currentId, false, 1);
group = groupFiles;
}
/* @see IFormatReader#isGroupFiles() */
@Override
public boolean isGroupFiles() {
return group;
}
/* @see IFormatReader#fileGroupOption(String) */
@Override
public int fileGroupOption(String id)
throws FormatException, IOException
{