Newer
Older
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
package azgracompress.io;
import azgracompress.cli.InputFileInfo;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
import azgracompress.utilities.TypeConverter;
import java.io.*;
import java.util.Arrays;
public class RawDataLoader implements IPlaneLoader {
private final InputFileInfo inputFileInfo;
public RawDataLoader(final InputFileInfo inputFileInfo) {
this.inputFileInfo = inputFileInfo;
}
@Override
public ImageU16 loadPlaneU16(int plane) throws IOException {
byte[] buffer;
final V3i rawDataDimension = inputFileInfo.getDimensions();
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath())) {
final long planeSize = (long) rawDataDimension.getX() * (long) rawDataDimension.getY() * 2;
final long expectedFileSize = planeSize * rawDataDimension.getZ();
final long fileSize = fileStream.getChannel().size();
if (expectedFileSize != fileSize) {
throw new IOException(
"File specified by `rawFile` doesn't contains raw data for image of dimensions " +
"`rawDataDimension`");
}
final long planeOffset = plane * planeSize;
buffer = new byte[(int) planeSize];
if (fileStream.skip(planeOffset) != planeOffset) {
throw new IOException("Failed to skip.");
}
if (fileStream.read(buffer, 0, (int) planeSize) != planeSize) {
throw new IOException("Read wrong number of bytes.");
}
}
return new ImageU16(rawDataDimension.getX(),
rawDataDimension.getY(),
TypeConverter.unsignedShortBytesToIntArray(buffer));
}
@Override
public int[] loadPlanesU16Data(int[] planes) throws IOException {
if (planes.length < 1)
return new int[0];
final int planeValueCount = inputFileInfo.getDimensions().getX() * inputFileInfo.getDimensions().getY();
final long planeDataSize = 2 * (long) planeValueCount;
final long totalValueCount = (long) planeValueCount * planes.length;
int[] values = new int[(int) totalValueCount];
if (totalValueCount > (long) Integer.MAX_VALUE) {
throw new IOException("Integer count is too big.");
}
Arrays.sort(planes);
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
DataInputStream dis = new DataInputStream(new BufferedInputStream(fileStream, 8192))) {
int lastIndex = 0;
int valIndex = 0;
for (final int planeIndex : planes) {
// Skip specific number of bytes to get to the next plane.
final int requestedSkip = (planeIndex == 0) ? 0 : ((planeIndex - lastIndex) - 1) * (int) planeDataSize;
lastIndex = planeIndex;
final int actualSkip = dis.skipBytes(requestedSkip);
if (requestedSkip != actualSkip) {
throw new IOException("Skip operation failed.");
}
for (int i = 0; i < planeValueCount; i++) {
values[valIndex++] = dis.readUnsignedShort();
}
}
}
return values;
}
@Override
public int[] loadAllPlanesU16Data() throws IOException {
final V3i imageDims = inputFileInfo.getDimensions();
final long dataSize = (long) imageDims.getX() * (long) imageDims.getY() * (long) imageDims.getZ();
int[] values = new int[(int) dataSize];
if (dataSize > (long) Integer.MAX_VALUE) {
throw new IOException("RawFile size is too big.");
}
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
DataInputStream dis = new DataInputStream(new BufferedInputStream(fileStream, 8192))) {
for (int i = 0; i < (int) dataSize; i++) {
values[i] = dis.readUnsignedShort();
}
}
return values;
}
}