-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitAction.cs
247 lines (210 loc) · 11.7 KB
/
SplitAction.cs
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
using System.Buffers;
using TiffLibrary;
namespace SplitBigTiffApp
{
internal class SplitAction
{
protected SplitAction()
{
}
public static async Task<int> Split(string inputFile, CancellationToken cancellationToken)
{
TiffStreamOffset ifdOffset;
await using TiffFileReader reader = await TiffFileReader.OpenAsync(inputFile, cancellationToken);
await using TiffFileContentReader contentReader = await reader.CreateContentReaderAsync(cancellationToken);
await using TiffFieldReader fieldReader = await reader.CreateFieldReaderAsync(cancellationToken);
TiffStreamOffset inputIfdOffset = reader.FirstImageFileDirectoryOffset;
var counter = 1;
while (!inputIfdOffset.IsZero)
{
TiffImageFileDirectory ifd = await reader.ReadImageFileDirectoryAsync(inputIfdOffset, cancellationToken);
var outputFileName = $"{Path.GetFileNameWithoutExtension(inputFile)}_{counter}.tiff";
Console.WriteLine($"Creating: {outputFileName}");
await using TiffFileWriter writer = await TiffFileWriter.OpenAsync(outputFileName, cancellationToken: cancellationToken);
using TiffImageFileDirectoryWriter ifdWriter = writer.CreateImageFileDirectory();
await CopyIfdAsync(contentReader, fieldReader, ifd, ifdWriter, cancellationToken);
ifdOffset = await ifdWriter.FlushAsync(default, cancellationToken);
writer.SetFirstImageFileDirectoryOffset(ifdOffset);
await writer.FlushAsync(cancellationToken);
inputIfdOffset = ifd.NextOffset;
counter++;
}
return 0;
}
private static async Task CopyIfdAsync(TiffFileContentReader contentReader, TiffFieldReader fieldReader, TiffImageFileDirectory ifd, TiffImageFileDirectoryWriter dest, CancellationToken cancellationToken)
{
var tagReader = new TiffTagReader(fieldReader, ifd);
foreach (TiffImageFileDirectoryEntry entry in ifd)
{
// Stripped image data
if (entry.Tag == TiffTag.StripOffsets)
{
await CopyStrippedImageAsync(contentReader, tagReader, dest, cancellationToken);
}
else if (entry.Tag == TiffTag.StripByteCounts)
{
// Ignore this
}
// Tiled image data
else if (entry.Tag == TiffTag.TileOffsets)
{
await CopyTiledImageAsync(contentReader, tagReader, dest, cancellationToken);
}
else if (entry.Tag == TiffTag.TileByteCounts)
{
// Ignore this
}
// Other fields
else
{
await CopyFieldValueAsync(fieldReader, entry, dest, cancellationToken);
}
}
}
private static async Task CopyStrippedImageAsync(TiffFileContentReader contentReader, TiffTagReader tagReader, TiffImageFileDirectoryWriter dest, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
TiffValueCollection<ulong> offsets = await tagReader.ReadStripOffsetsAsync(cancellationToken);
TiffValueCollection<ulong> byteCounts = await tagReader.ReadStripByteCountsAsync(cancellationToken);
if (offsets.Count != byteCounts.Count)
{
throw new InvalidDataException("Failed to copy stripped image data. StripOffsets and StripByteCounts don't have the same amount of elements.");
}
uint[] offsets32 = new uint[offsets.Count];
uint[] byteCounts32 = new uint[offsets.Count];
byte[]? buffer = null;
try
{
for (int i = 0; i < offsets.Count; i++)
{
long offset = checked((long)offsets[i]);
int byteCount = checked((int)byteCounts[i]);
byteCounts32[i] = checked((uint)byteCount);
if (buffer is null || byteCount > buffer.Length)
{
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
buffer = ArrayPool<byte>.Shared.Rent(byteCount);
}
if (await contentReader.ReadAsync(offset, new ArraySegment<byte>(buffer, 0, byteCount), cancellationToken) != byteCount)
{
throw new InvalidDataException("Invalid ByteCount field.");
}
TiffStreamOffset region = await dest.FileWriter.WriteAlignedBytesAsync(buffer, 0, byteCount, cancellationToken);
offsets32[i] = checked((uint)region.Offset);
}
}
finally
{
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
await dest.WriteTagAsync(TiffTag.StripOffsets, TiffValueCollection.UnsafeWrap(offsets32), cancellationToken);
await dest.WriteTagAsync(TiffTag.StripByteCounts, TiffValueCollection.UnsafeWrap(byteCounts32), cancellationToken);
}
private static async Task CopyTiledImageAsync(TiffFileContentReader contentReader, TiffTagReader tagReader, TiffImageFileDirectoryWriter dest, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
TiffValueCollection<ulong> offsets = await tagReader.ReadTileOffsetsAsync(cancellationToken);
TiffValueCollection<ulong> byteCounts = await tagReader.ReadTileByteCountsAsync(cancellationToken);
if (offsets.Count != byteCounts.Count)
{
throw new InvalidDataException("Failed to copy stripped image data. TileOffsets and TileByteCounts don't have the same amount of elements.");
}
uint[] offsets32 = new uint[offsets.Count];
uint[] byteCounts32 = new uint[offsets.Count];
byte[]? buffer = null;
try
{
for (int i = 0; i < offsets.Count; i++)
{
long offset = checked((long)offsets[i]);
int byteCount = checked((int)byteCounts[i]);
byteCounts32[i] = checked((uint)byteCount);
if (buffer is null || byteCount > buffer.Length)
{
if (!(buffer is null))
{
ArrayPool<byte>.Shared.Return(buffer);
}
buffer = ArrayPool<byte>.Shared.Rent(byteCount);
}
if (await contentReader.ReadAsync(offset, new ArraySegment<byte>(buffer, 0, byteCount), cancellationToken) != byteCount)
{
throw new InvalidDataException("Invalid ByteCount field.");
}
TiffStreamOffset region = await dest.FileWriter.WriteAlignedBytesAsync(buffer, 0, byteCount, cancellationToken);
offsets32[i] = checked((uint)region.Offset);
}
}
finally
{
if (!(buffer is null))
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
await dest.WriteTagAsync(TiffTag.TileOffsets, TiffValueCollection.UnsafeWrap(offsets32), cancellationToken);
await dest.WriteTagAsync(TiffTag.TileByteCounts, TiffValueCollection.UnsafeWrap(byteCounts32), cancellationToken);
}
private static async Task CopyFieldValueAsync(TiffFieldReader reader, TiffImageFileDirectoryEntry entry, TiffImageFileDirectoryWriter ifdWriter, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
switch (entry.Type)
{
case TiffFieldType.Byte:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadByteFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.ASCII:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadASCIIFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Short:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadShortFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Long:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadLongFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Rational:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadRationalFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.SByte:
await ifdWriter.WriteTagAsync(entry.Tag, entry.Type, await reader.ReadByteFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Undefined:
await ifdWriter.WriteTagAsync(entry.Tag, entry.Type, await reader.ReadByteFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.SShort:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadSShortFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.SLong:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadSLongFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.SRational:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadSRationalFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Float:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadFloatFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.Double:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadDoubleFieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.IFD:
throw new NotSupportedException("This IFD type is not supported yet.");
case TiffFieldType.Long8:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadLong8FieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.SLong8:
await ifdWriter.WriteTagAsync(entry.Tag, await reader.ReadSLong8FieldAsync(entry, cancellationToken: cancellationToken), cancellationToken);
break;
case TiffFieldType.IFD8:
throw new NotSupportedException("This IFD type is not supported yet.");
default:
throw new NotSupportedException($"Unsupported IFD field type: {entry.Type}.");
}
}
}
}