-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodecTiff.h
More file actions
451 lines (386 loc) · 17.3 KB
/
Copy pathCodecTiff.h
File metadata and controls
451 lines (386 loc) · 17.3 KB
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
#pragma once
#include <Image.h>
#include <tiffio.h>
#include <tiffio.hxx>
#include "TiffClientFunctions.h"
#include "TiffFile.h"
#include <LLUtils/Exception.h>
#include <IImagePlugin.h>
#include <utility>
namespace IMCodec
{
class CodecTiff : public IImagePlugin
{
private:
PluginProperties mPluginProperties;
public:
CodecTiff() : mPluginProperties(
{
// {3AF4C3A1-FA72-4394-8B7C-68412A3433E3}
{ 0x3af4c3a1, 0xfa72, 0x4394, { 0x8b, 0x7c, 0x68, 0x41, 0x2a, 0x34, 0x33, 0xe3 } }
, CodecCapabilities::Decode
, LLUTILS_TEXT("LibTiff image codec")
,
{
{
{ LLUTILS_TEXT("Tag Image File Format")}
,{ LLUTILS_TEXT("tif"),LLUTILS_TEXT("tiff")}
}
}
}
)
{
}
const PluginProperties& GetPluginProperties() override
{
return mPluginProperties;
}
TexelFormat GetTexelFormat(uint16_t sampleFormat, uint16_t bitsPerSample, uint16_t samplesPerPixel, uint16_t photoMetric) const
{
TexelFormat texelFormat = TexelFormat::UNKNOWN;
switch (photoMetric)
{
case PHOTOMETRIC_RGB:
switch (sampleFormat)
{
case SAMPLEFORMAT_IEEEFP:
switch (samplesPerPixel)
{
case 3:
switch (bitsPerSample)
{
case 32:
texelFormat = TexelFormat::F_R32_G32_B32;
break;
}
}
break;
case SAMPLEFORMAT_UINT:
switch (samplesPerPixel)
{
case 3:
switch (bitsPerSample)
{
case 8:
texelFormat = TexelFormat::I_R8_G8_B8;
break;
case 16:
texelFormat = TexelFormat::I_R16_G16_B16;
break;
}
break;
case 4:
switch (bitsPerSample)
{
case 8:
texelFormat = TexelFormat::I_R8_G8_B8_A8;
break;
case 16:
texelFormat = TexelFormat::I_R16_G16_B16_A16;
break;
}
break;
}
}
break;
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
switch (sampleFormat)
{
case SAMPLEFORMAT_IEEEFP:
switch (samplesPerPixel)
{
case 1:
switch (bitsPerSample)
{
case 16:
texelFormat = TexelFormat::F_X16;
break;
case 24:
texelFormat = TexelFormat::F_X24;
break;
case 32:
texelFormat = TexelFormat::F_X32;
break;
case 64:
texelFormat = TexelFormat::F_X64;
break;
default:
break;
}
break;
}
break;
case SAMPLEFORMAT_UINT:
switch (samplesPerPixel)
{
case 1:
switch (bitsPerSample)
{
case 1:
texelFormat = TexelFormat::I_X1;
break;
case 4:
texelFormat = TexelFormat::I_X4;
break;
case 8:
texelFormat = TexelFormat::I_X8;
break;
case 16:
texelFormat = TexelFormat::I_X16;
break;
}
break;
}
break;
case SAMPLEFORMAT_INT:
switch (samplesPerPixel)
{
case 1:
switch (bitsPerSample)
{
case 8:
texelFormat = TexelFormat::S_X8;
break;
case 16:
texelFormat = TexelFormat::S_X16;
break;
default:
break;
}
break;
}
break;
}
}
return texelFormat;
}
std::vector<ImageItemSharedPtr> GetImage(TIFF* tiff)
{
std::vector< ImageItemSharedPtr> imageItems;
imageItems.push_back(std::make_shared<ImageItem>());
auto& imageItem = imageItems.back();
imageItem->itemType = ImageItemType::Image;
uint16_t sampleFormat{};
uint32_t width, height{};
uint16_t bitsPerSample{};
//uint16 compression{};
uint16_t photoMetric{};
uint16_t samplesPerPixel{};
uint16_t orientation{};
uint32_t rowPitch{};
//uint32 rowsPerStrip{};
//uint16_t stripRowCount{};
//uint32 stripoffsets{};
uint16_t planarConfig{};
/*uint16_t minSampleValue{};
uint16_t maxSampleValue{};*/
uint16_t extraSamples{};
uint16_t* sampleTypes{};
//field width specification can be found here: http://www.libtiff.org/man/TIFFGetField.3t.html
TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
//TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression);
TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photoMetric);
TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
TIFFGetFieldDefaulted(tiff, TIFFTAG_ORIENTATION, &orientation);
TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLEFORMAT, &sampleFormat);
//TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rowsPerStrip);
//TIFFGetField(tiff, TIFFTAG_STRIPROWCOUNTS, &stripRowCount);
TIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planarConfig);
/*TIFFGetField(tiff, TIFFTAG_MINSAMPLEVALUE, &minSampleValue);
TIFFGetField(tiff, TIFFTAG_MAXSAMPLEVALUE, &maxSampleValue);*/
TIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extraSamples, &sampleTypes);
if (samplesPerPixel <= extraSamples)
return {};
const uint32_t bytesPerSample = bitsPerSample / CHAR_BIT;
uint32_t numberOfStripts = TIFFNumberOfStrips(tiff);
tmsize_t stripSize = TIFFStripSize(tiff);
rowPitch = static_cast<uint32_t>(TIFFScanlineSize(tiff));
//auto rasterScanLineSize = TIFFRasterScanlineSize(tiff);
const auto mainChannelTexelFormat = GetTexelFormat(sampleFormat, bitsPerSample, samplesPerPixel - extraSamples, photoMetric);
imageItem->descriptor.width = width;
imageItem->descriptor.height = height;
imageItem->descriptor.rowPitchInBytes = rowPitch;
if (mainChannelTexelFormat != TexelFormat::UNKNOWN)
{
imageItem->data.Allocate(numberOfStripts * stripSize);
std::byte* currensPos = imageItem->data.data();
for (uint32_t i = 0; i < numberOfStripts; i++)
{
TIFFReadEncodedStrip(tiff, i, currensPos, stripSize);
currensPos += stripSize;
}
//If there's an alpha channel and it's interleaved in the image data, return as single image with alpha channel
imageItem->descriptor.texelFormatDecompressed = imageItem->descriptor.texelFormatStorage = mainChannelTexelFormat;
auto extractExtraSampleImages = [&]()
{
return ExtactExtraSamples(width, height, rowPitch, samplesPerPixel, extraSamples, planarConfig
, bytesPerSample, imageItem->data, photoMetric, bitsPerSample, sampleFormat);
};
if (planarConfig == PLANARCONFIG_CONTIG && extraSamples > 0)
{
const auto interleavedTexelFormat = GetTexelFormat(sampleFormat, bitsPerSample, samplesPerPixel, photoMetric);
if (interleavedTexelFormat != TexelFormat::UNKNOWN)
{
imageItem->descriptor.texelFormatStorage = imageItem->descriptor.texelFormatDecompressed = interleavedTexelFormat;
}
else
{
auto extraChannelImages = extractExtraSampleImages();
if (extraChannelImages.empty() == false)
imageItems = extraChannelImages;
else
return {};
}
}
else if (extraSamples > 0)
{
auto extraChannelImages = extractExtraSampleImages();
if (extraChannelImages.empty() == false)
imageItems = extraChannelImages;
else
return {};
}
}
else
{
imageItem->descriptor.texelFormatDecompressed = TexelFormat::I_R8_G8_B8_A8;
//override target row pitch - always 32bpp
imageItem->descriptor.rowPitchInBytes = width * 4;
imageItem->data.Allocate(height * imageItem->descriptor.rowPitchInBytes);
TIFFReadRGBAImageOriented(tiff, width, height, reinterpret_cast<uint32_t*>(imageItem->data.data()), orientation);
}
return imageItems;
}
std::vector<ImageItemSharedPtr> ExtactExtraSamples(
uint32_t width, uint32_t height, uint32_t sourceRowPitch,
uint16_t samplesPerPixel, uint16_t extraSmaples
, uint16_t planarConfig, uint16_t bytesPerSample
, const LLUtils::Buffer& sourceBuffer
, uint16_t photometric
, uint16_t bitsPerSample
, uint16_t sampleFormat
)
{
std::vector< ImageItemSharedPtr> subChannels;
auto extractSamples = [&](uint16_t sampleOffset, uint16_t sampleCount)->
ImageItemSharedPtr
{
const auto sampleSize = sampleCount * bytesPerSample;
const auto samplePhotometric = sampleOffset == 0 ? photometric : PHOTOMETRIC_MINISBLACK;
auto imageItem = std::make_shared<ImageItem>();
imageItem->descriptor.texelFormatStorage = GetTexelFormat(sampleFormat, bitsPerSample , sampleCount, samplePhotometric);
if (imageItem->descriptor.texelFormatStorage == TexelFormat::UNKNOWN)
return {};
imageItem->descriptor.texelFormatDecompressed = imageItem->descriptor.texelFormatStorage;
imageItem->descriptor.height = height;
imageItem->descriptor.width = width;
LLUtils::Buffer image;
if (planarConfig == PLANARCONFIG_CONTIG) //interleaved
{
const auto singleSampleRowPitch = sampleSize * width;
image.Allocate(singleSampleRowPitch * height);
size_t sourceOffset = 0;
size_t destOffset = 0;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
const size_t sourceRowIndex = sampleOffset * bytesPerSample + x * samplesPerPixel * bytesPerSample;
const size_t destRowIndex = x * sampleSize;
memcpy(image.data() + destOffset + destRowIndex, sourceBuffer.data() + sourceOffset + sourceRowIndex, sampleSize);
}
sourceOffset += sourceRowPitch;
destOffset += singleSampleRowPitch;
}
imageItem->descriptor.rowPitchInBytes = singleSampleRowPitch;;
}
else if (planarConfig == PLANARCONFIG_SEPARATE)
{
const auto singleSampleRowPitch = sourceRowPitch * sampleCount;
image.Allocate(sourceRowPitch * height);
memcpy(image.data(), sourceBuffer.data() + height * singleSampleRowPitch * sampleOffset, height * singleSampleRowPitch);
imageItem->descriptor.rowPitchInBytes = singleSampleRowPitch;;
}
imageItem->data = std::move(image);
return imageItem;
};
if (extraSmaples > 0)
{
if (bytesPerSample == 0 || bitsPerSample % CHAR_BIT != 0 || samplesPerPixel <= extraSmaples)
return {};
const auto mainImageSampleCount = samplesPerPixel - extraSmaples;
auto mainImage = extractSamples(0, mainImageSampleCount);
if (mainImage == nullptr)
return {};
subChannels.push_back(std::move(mainImage));
for (int i = 0; i < extraSmaples; i++)
{
auto extraImage = extractSamples(mainImageSampleCount + i, 1);
if (extraImage == nullptr)
return {};
subChannels.push_back(std::move(extraImage));
}
}
return subChannels;
}
ImageResult Decode(const std::byte* buffer, std::size_t size,[[maybe_unused]] ImageLoadFlags loadFlags, const Parameters& params ,[[maybe_unused]] ImageSharedPtr& out_image) override
{
using namespace std;
ImageResult result = ImageResult::UnknownError;
TiffFile tifFile(reinterpret_cast<const uint8_t*>(buffer), size);
TIFF* tiff = tifFile.GetTiff();
ImageItemSharedPtr firstImageItem;
std::vector<ImageSharedPtr> subImages;
if (tiff != nullptr)
{
int currentSubImage = 0;
auto firstImageItemChannles = GetImage(tiff);
if (firstImageItemChannles.empty())
return ImageResult::FormatNotSupported;
firstImageItem = firstImageItemChannles.at(0);
if (firstImageItemChannles.size() > 1)
{
for (size_t i = 0; i < firstImageItemChannles.size(); i++)
{
subImages.push_back(std::make_shared<Image>(firstImageItemChannles.at(i), ImageItemType::Pages));
currentSubImage++;
}
}
while (TIFFReadDirectory(tiff) > 0)
{
if (currentSubImage == 0)
{
subImages.push_back(std::make_shared<Image>(firstImageItem, ImageItemType::Pages));
currentSubImage++;
}
auto nextImages = GetImage(tiff);
if (nextImages.empty())
return ImageResult::FormatNotSupported;
for (size_t i = 0; i < nextImages.size(); i++)
{
subImages.push_back(std::make_shared<Image>(nextImages.at(i), ImageItemType::Pages));
currentSubImage++;
}
}
if (currentSubImage > 0)
{
ImageItemSharedPtr containerImageItem = std::make_shared<ImageItem>();
containerImageItem->itemType = ImageItemType::Container;
out_image = std::make_shared<Image>(containerImageItem, ImageItemType::Pages);
out_image->SetNumSubImages(currentSubImage);
for (auto subImage = 0; subImage < currentSubImage; subImage++)
out_image->SetSubImage(subImage, subImages.at(subImage));
}
else
{
out_image = std::make_shared<Image>(firstImageItem, ImageItemType::Image);
}
result = ImageResult::Success;
}
return result;
}
};
}