/
tiff.cpp
670 lines (557 loc) · 26.2 KB
/
tiff.cpp
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
#ifdef HAVE_TIFF
#include <stddef.h>
#include <tiffio.h>
#include <vector>
#include <string.h>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <assert.h>
#include "AIL.h"
#include "AIL_internal.h"
#include "tiff.h"
namespace AImg
{
AImgFormat getWriteFormatTiff(int32_t inputFormat)
{
// Tiff can write all currently supported formats. Here we are just future-proofing in case we add some more formats later that tiff can't do
if (inputFormat > AImgFormat::INVALID_FORMAT && inputFormat <= AImgFormat::RGBA32F)
return (AImgFormat)inputFormat;
return AImgFormat::INVALID_FORMAT;
}
struct tiffCallbackData
{
ReadCallback mReadCallback = nullptr;
WriteCallback mWriteCallback = nullptr;
TellCallback mTellCallback = nullptr;
SeekCallback mSeekCallback = nullptr;
void *callbackData = nullptr;
int32_t startPos = 0;
int32_t furthestPositionWritten = 0;
};
tsize_t tiffRead(thandle_t st, tdata_t buffer, tsize_t size)
{
tiffCallbackData *callbacks = (tiffCallbackData *)st;
return callbacks->mReadCallback(callbacks->callbackData, (uint8_t *)buffer, size);
}
tsize_t tiff_Write(thandle_t st, tdata_t buffer, tsize_t size)
{
tiffCallbackData *callbacks = (tiffCallbackData *)st;
int32_t start = callbacks->mTellCallback(callbacks->callbackData);
callbacks->mWriteCallback(callbacks->callbackData, (uint8_t *)buffer, size);
int32_t end = callbacks->mTellCallback(callbacks->callbackData);
if (end > callbacks->furthestPositionWritten)
callbacks->furthestPositionWritten = end;
return end - start;
}
// This should never be called
// We don't implement it because AImg is designed not to have the file size
// available to it, we just receive streams. I had a look at the libtiff source,
// and it seems to only be used for a deprecated version of embedded jpeg tiffs,
// and tiff directories. We just cancel decode if we detect an old-style jpeg
// embedded tiff, and never use the directory crap in the first place, so it
// should be safe to just return 0 here.
toff_t tiff_Size(thandle_t st)
{
AIL_UNUSED_PARAM(st);
return 0;
}
toff_t tiff_Seek(thandle_t st, toff_t pos, int whence)
{
tiffCallbackData *callbacks = (tiffCallbackData *)st;
if (pos == 0xFFFFFFFF)
return 0xFFFFFFFF;
toff_t finalPos = pos;
switch (whence)
{
case SEEK_SET:
{
finalPos += callbacks->startPos;
break;
}
case SEEK_CUR:
{
finalPos += callbacks->mTellCallback(callbacks->callbackData);
break;
}
// I checked the libtiff source, this should only be used during image
// writing. In this case, we can keep track of the file size, because we are
// setting the size by writing to the stream, so we can actually implement it,
// unlike tiff_Size above.
case SEEK_END:
{
finalPos = callbacks->furthestPositionWritten + pos;
break;
}
}
callbacks->mSeekCallback(callbacks->callbackData, finalPos);
return callbacks->mTellCallback(callbacks->callbackData);
}
int tiff_Map(thandle_t, tdata_t *, toff_t *)
{
return 0;
}
void tiff_Unmap(thandle_t, tdata_t, toff_t)
{
return;
}
int tiff_Close(thandle_t)
{
return 0;
}
float convertFloat24(const unsigned char *src)
{
int mantissaBits = 16;
int exponentBits = 7;
int maxExponent = pow(2, exponentBits) - 1;
int v = *((int *)src);
int sign = v >> 23;
int exponent = (v >> mantissaBits) & (int)(pow(2, exponentBits) - 1);
int mantissa = v & (int)(pow(2, mantissaBits) - 1);
if (exponent == 0)
{
if (mantissa != 0)
{
while ((mantissa & (int)pow(2, mantissaBits)) == 0)
{
mantissa <<= 1;
exponent--;
}
exponent++;
mantissa &= (int)(pow(2, mantissaBits) - 1);
exponent += 127 - (pow(2, exponentBits - 1) - 1);
}
}
else if (exponent == maxExponent)
{
exponent = 255;
}
else
{
exponent += 127 - (pow(2, exponentBits - 1) - 1);
}
mantissa <<= (23 - mantissaBits);
int value = (sign << 31) | (exponent << 23) | mantissa;
float final = *((float *)&value);
return final;
}
class TiffFile : public AImgBase
{
TIFF *tiff = nullptr;
tiffCallbackData callbacks;
uint16_t bitsPerChannel = 0;
uint16_t channels = 0;
uint32_t width = 0, height = 0;
uint16_t sampleFormat = 0;
uint16_t compression = 0;
uint32_t rowsPerStrip = 0;
uint16_t planarConfig = 0;
uint8_t * compressedProfile = NULL;
uint32_t compressedProfileLen = 0;
public:
virtual ~TiffFile()
{
if (tiff != NULL)
TIFFClose(tiff);
}
int32_t getDecodeFormat()
{
if (channels > 0 && channels <= 4)
{
// handle 24-bit float (lolwtf)
if (bitsPerChannel == 24 && sampleFormat == SAMPLEFORMAT_IEEEFP)
return ((int32_t)AImgFormat::R32F) - 1 + channels;
if (sampleFormat == SAMPLEFORMAT_IEEEFP)
{
if (bitsPerChannel == 16)
return ((int32_t)AImgFormat::R16F) - 1 + channels;
else if (bitsPerChannel == 32)
return ((int32_t)AImgFormat::R32F) - 1 + channels;
}
else if (sampleFormat == SAMPLEFORMAT_UINT || sampleFormat == SAMPLEFORMAT_INT)
{
if (bitsPerChannel == 8)
return ((int32_t)AImgFormat::R8U) - 1 + channels;
else if (bitsPerChannel == 16)
return ((int32_t)AImgFormat::R16U) - 1 + channels;
}
}
return AImgFormat::INVALID_FORMAT;
}
virtual int32_t getImageInfo(int32_t *width, int32_t *height, int32_t *numChannels, int32_t *bytesPerChannel, int32_t *floatOrInt, int32_t *decodedImgFormat, uint32_t *colourProfileLen)
{
*width = this->width;
*height = this->height;
*numChannels = this->channels;
if(colourProfileLen != NULL)
{
*colourProfileLen = this->compressedProfileLen;
}
if (bitsPerChannel % 8 == 0)
*bytesPerChannel = bitsPerChannel / 8;
else
*bytesPerChannel = -1;
if (sampleFormat == SAMPLEFORMAT_IEEEFP)
*floatOrInt = AImgFloatOrIntType::FITYPE_FLOAT;
else if (sampleFormat == SAMPLEFORMAT_UINT || sampleFormat == SAMPLEFORMAT_INT)
*floatOrInt = AImgFloatOrIntType::FITYPE_INT;
else
*floatOrInt = AImgFloatOrIntType::FITYPE_UNKNOWN;
*decodedImgFormat = getDecodeFormat();
return AImgErrorCode::AIMG_SUCCESS;
}
virtual int32_t getColourProfile(char *profileName, uint8_t *colourProfile, uint32_t *colourProfileLen)
{
if (colourProfile != NULL)
{
if(this->compressedProfile != NULL)
{
memcpy(colourProfile, this->compressedProfile, this->compressedProfileLen);
}
*colourProfileLen = this->compressedProfileLen;
}
if (profileName != NULL)
{
std::strcpy(profileName, "");
}
return AImgErrorCode::AIMG_SUCCESS;
}
virtual int32_t decodeImage(void *realDestBuffer, int32_t forceImageFormat)
{
uint8_t *destBuffer = (uint8_t *)realDestBuffer;
int32_t decodeFormat = getDecodeFormat();
std::vector<uint8_t> convertTmpBuffer(0);
if (forceImageFormat != AImgFormat::INVALID_FORMAT && forceImageFormat != decodeFormat)
{
int32_t numChannels, bytesPerChannelF, floatOrInt;
AIGetFormatDetails(decodeFormat, &numChannels, &bytesPerChannelF, &floatOrInt);
convertTmpBuffer.resize(width * height * bytesPerChannelF * numChannels);
destBuffer = &convertTmpBuffer[0];
}
uint32 stripsize = TIFFStripSize(tiff);
int32_t bytesPerChannel = bitsPerChannel / 8;
std::vector<char> stripBuffer(stripsize);
int32_t _;
int32_t decodeFormatBytesPerChannel;
AIGetFormatDetails(decodeFormat, &_, &decodeFormatBytesPerChannel, &_);
if (planarConfig == PLANARCONFIG_CONTIG)
{
unsigned char *bufferPtr = destBuffer;
size_t row = 0;
for (tstrip_t strip = 0; strip < TIFFNumberOfStrips(tiff); strip++)
{
if (TIFFReadEncodedStrip(tiff, strip, &stripBuffer[0], -1) == ((tmsize_t)-1)) // this function returns -1 on failure. As an unsigned int. yaaaaaaaaaaay
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] Tiff read failure, TIFFReadEncodedStrip failed";
return AImgErrorCode::AIMG_LOAD_FAILED_EXTERNAL;
}
char *stripPtr = &stripBuffer[0];
for (size_t rowStrip = 0; rowStrip < rowsPerStrip; rowStrip++)
{
if (row >= height)
break;
for (size_t x = 0; x < width; x++)
{
for (size_t channelIndex = 0; channelIndex < channels; channelIndex++)
{
if (bytesPerChannel == 4)
{
*((float *)bufferPtr) = *(float *)stripPtr;
stripPtr += 4;
bufferPtr += 4;
}
else if (bytesPerChannel == 3)
{
// this will always be 24-bit float, as we return an error in openImage if BITSPERSAMPLE == 3 and SAMPLEFORMAT is not IEEEFP
*((float *)bufferPtr) = convertFloat24((unsigned char *)stripPtr);
stripPtr += 3;
bufferPtr += 4;
}
else if (bytesPerChannel == 2)
{
// doesn't matter if we have 16-bit int or float, we can just copy the data over all the same
*((uint16_t *)bufferPtr) = *((uint16_t *)stripPtr);
stripPtr += 2;
bufferPtr += 2;
}
else if (bytesPerChannel == 1)
{
*bufferPtr = *stripPtr;
stripPtr += 1;
bufferPtr += 1;
}
}
// convert from YCbCr to RGB (jpeg tiffs always have bytesPerChannel == 1 and PLANARCONFIG_CONTIG, and always have three channels)
if (compression == COMPRESSION_JPEG)
{
float Y = bufferPtr[-3];
float Cb = bufferPtr[-2];
float Cr = bufferPtr[-1];
bufferPtr[-3] = (char)std::max(std::min(Y + 1.40200 * (Cr - 127.0), 255.0), 0.0);
bufferPtr[-2] = (char)std::max(std::min(Y - 0.34414 * (Cb - 127.0) - 0.71414 * (Cr - 127.0), 255.0), 0.0);
bufferPtr[-1] = (char)std::max(std::min(Y + 1.77200 * (Cb - 127.0), 255.0), 0.0);
}
}
row++;
}
}
}
// This is just a copy of the above block, fixed up to work for channels being stored sequentially not interleaved.
// for clarity, interleaved for a 2x1 image would be: R1,G1,B1,R2,G2,B2, where as SEPARATE would be R1,R2,G1,G2,B1,B2
// We don't actually support decoding separated channel buffers like that, so we manually interleave the channels to fix it up.
else if (planarConfig == PLANARCONFIG_SEPARATE)
{
int32_t channelIndex = -1;
unsigned char *bufferPtr = NULL;
while (true)
{
size_t row = 0;
for (tstrip_t strip = 0; strip < TIFFNumberOfStrips(tiff); strip++)
{
if (TIFFReadEncodedStrip(tiff, strip, &stripBuffer[0], -1) == ((tmsize_t)-1))
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] Tiff read failure, TIFFReadEncodedStrip failed";
return AImgErrorCode::AIMG_LOAD_FAILED_EXTERNAL;
}
char *stripPtr = (char *)&stripBuffer[0];
for (size_t rowStrip = 0; rowStrip < rowsPerStrip; rowStrip++)
{
if (row % height == 0)
{
channelIndex++;
if (channelIndex >= channels)
goto done;
bufferPtr = destBuffer + channelIndex * decodeFormatBytesPerChannel;
}
for (size_t x = 0; x < width; x++)
{
if (bytesPerChannel == 4)
{
*((float *)bufferPtr) = *(float *)stripPtr;
stripPtr += 4;
bufferPtr += 4 * channels;
}
else if (bytesPerChannel == 3)
{
*((float *)bufferPtr) = convertFloat24((unsigned char *)stripPtr);
stripPtr += 3;
bufferPtr += 4 * channels;
}
else if (bytesPerChannel == 2)
{
*((uint16_t *)bufferPtr) = *((uint16 *)stripPtr);
stripPtr += 2;
bufferPtr += 2 * channels;
}
else if (bytesPerChannel == 1)
{
*bufferPtr = *stripPtr;
stripPtr += 1;
bufferPtr += 1 * channels;
}
}
row++;
}
}
}
done:;
}
if (forceImageFormat != AImgFormat::INVALID_FORMAT && forceImageFormat != decodeFormat)
{
int32_t err = AImgConvertFormat(destBuffer, realDestBuffer, width, height, decodeFormat, forceImageFormat);
if (err != AImgErrorCode::AIMG_SUCCESS)
return err;
}
return AImgErrorCode::AIMG_SUCCESS;
}
virtual int32_t openImage(ReadCallback readCallback, TellCallback tellCallback, SeekCallback seekCallback, void *callbackData)
{
callbacks.mReadCallback = readCallback;
callbacks.mSeekCallback = seekCallback;
callbacks.mTellCallback = tellCallback;
callbacks.callbackData = callbackData;
callbacks.startPos = tellCallback(callbackData);
tiff = TIFFClientOpen("", "r", (thandle_t)&callbacks, tiffRead, tiff_Write, tiff_Seek, tiff_Close, tiff_Size, tiff_Map, tiff_Unmap);
if (tiff == nullptr)
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] Failed to open tiff file for unknown reason";
return AImgErrorCode::AIMG_LOAD_FAILED_EXTERNAL;
}
bool hasSamplesPerPixel = TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &channels);
AImgErrorCode retval = AImgErrorCode::AIMG_SUCCESS;
if(!hasSamplesPerPixel)
channels = 1;
if (channels > 0 && channels <= 4)
{
int16_t bpsNotRead = -999;
std::vector<int16_t> bitsPerSampleValues(channels, bpsNotRead);
uint32_t *stripByteCounts = NULL;
bool hasEssentialTiffTags =
TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitsPerSampleValues[0]) &&
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) &&
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) &&
TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression) &&
TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rowsPerStrip) &&
TIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planarConfig) &&
TIFFGetField(tiff, TIFFTAG_STRIPBYTECOUNTS, &stripByteCounts);
if (hasEssentialTiffTags)
{
bitsPerChannel = bitsPerSampleValues[0];
for (size_t i = 0; i < bitsPerSampleValues.size(); i++)
{
// check if they're all the same, if they have been read at all
// tiff docs are inconsistent on this, http://www.awaresystems.be/imaging/tiff/tifftags/bitspersample.html says it should return
// one value for each channel, http://libtiff.maptools.org/man/TIFFGetField.3tiff.html says just one. (retrieved 10/21/2016)
// I try to work for both (in my experience though, it's just one).
if (bitsPerSampleValues[i] != bitsPerSampleValues[0] && bitsPerSampleValues[i] != bpsNotRead)
{
mErrorDetails = "Bits per sample is not the same for all channels.";
retval = AImgErrorCode::AIMG_LOAD_FAILED_UNSUPPORTED_TIFF;
}
}
}
else
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] Bad tiff file - missing at least one of the essential tifftags "
"(BITSPERSAMPLE, SAMPLESPERPIXEL, IMAGEWIDTH, IMAGELENGTH, COMPRESSION, ROWSPERSTRIP, PLANARCONFIG, STRIPBYTECOUNTS)";
return AImgErrorCode::AIMG_LOAD_FAILED_INTERNAL;
}
}
else
{
mErrorDetails = "Channel count " + std::to_string(channels) + " we only support up to 4";
retval = AImgErrorCode::AIMG_LOAD_FAILED_UNSUPPORTED_TIFF;
}
if (!TIFFGetField(tiff, TIFFTAG_SAMPLEFORMAT, &sampleFormat))
sampleFormat = SAMPLEFORMAT_UINT; // default to uint format if no SAMPLEFORMAT tifftag is present
if(!TIFFGetField(tiff, TIFFTAG_ICCPROFILE, &compressedProfileLen, &compressedProfile))
{
compressedProfile = NULL;
}
if (compression == COMPRESSION_OJPEG)
{
mErrorDetails = "Old-style jpeg tiff detected.";
retval = AImgErrorCode::AIMG_LOAD_FAILED_UNSUPPORTED_TIFF;
}
if (compression == COMPRESSION_JPEG)
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] Jpeg compressed tiff not currently supported. Will be added in a future version.";
return AImgErrorCode::AIMG_LOAD_FAILED_INTERNAL;
}
if (bitsPerChannel % 8 != 0)
{
mErrorDetails = "Bits per channel is not divisible by 8.";
retval = AImgErrorCode::AIMG_LOAD_FAILED_UNSUPPORTED_TIFF;
}
if (getDecodeFormat() == AImgFormat::INVALID_FORMAT)
{
mErrorDetails = "Unsupported combination of tifftags BITSPERSAMPLE and SAMPLEFORMAT.";
retval = AIMG_LOAD_FAILED_UNSUPPORTED_TIFF;
}
if (retval != AImgErrorCode::AIMG_SUCCESS)
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::openImage] " +
mErrorDetails +
" Only a sensible subset of tiffs are supported, this file is "
"outside that subset.";
}
return retval;
}
int32_t writeImage(void *data, int32_t width, int32_t height, int32_t inputFormat, const char *profileName, uint8_t *colourProfile, uint32_t colourProfileLen,
WriteCallback writeCallback, TellCallback tellCallback, SeekCallback seekCallback, void *callbackData, void *encodingOptions)
{
// Suppress unused warning
(void)profileName;
AIL_UNUSED_PARAM(encodingOptions);
tiffCallbackData wCallbacks;
wCallbacks.mWriteCallback = writeCallback;
wCallbacks.mSeekCallback = seekCallback;
wCallbacks.mTellCallback = tellCallback;
wCallbacks.callbackData = callbackData;
wCallbacks.startPos = tellCallback(callbackData);
TIFF *wTiff = TIFFClientOpen("", "w", (thandle_t)&wCallbacks, tiffRead, tiff_Write, tiff_Seek, tiff_Close, tiff_Size, tiff_Map, tiff_Unmap);
int32_t retval = AIMG_SUCCESS;
int32_t wFormat = getWriteFormatTiff(inputFormat);
if (wFormat == AImgFormat::INVALID_FORMAT)
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::writeImage] Cannot write this format to tiff."; // developers: see comment in getWriteFormatTiff
retval = AImgErrorCode::AIMG_WRITE_FAILED_INTERNAL;
}
else
{
int32_t numChannels, bytesPerChannel, floatOrInt;
AIGetFormatDetails(wFormat, &numChannels, &bytesPerChannel, &floatOrInt);
TIFFSetField(wTiff, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(wTiff, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(wTiff, TIFFTAG_SAMPLESPERPIXEL, numChannels);
TIFFSetField(wTiff, TIFFTAG_BITSPERSAMPLE, bytesPerChannel * 8);
TIFFSetField(wTiff, TIFFTAG_SAMPLEFORMAT, floatOrInt == AImgFloatOrIntType::FITYPE_FLOAT ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT);
TIFFSetField(wTiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(wTiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
if (numChannels == 1)
{
TIFFSetField(wTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
}
else
{
TIFFSetField(wTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
}
tsize_t stripRows = TIFFDefaultStripSize(wTiff, 0);
TIFFSetField(wTiff, TIFFTAG_ROWSPERSTRIP, stripRows);
int proflength = colourProfileLen;
const void* profdata = colourProfile;
if (profdata)
TIFFSetField(wTiff, TIFFTAG_ICCPROFILE, proflength, profdata);
for (int32_t y = 0; y < height; y++)
{
if (TIFFWriteScanline(wTiff, &((uint8_t *)data)[numChannels * bytesPerChannel * width * y], y, 0) < 0)
{
mErrorDetails = "[AImg::TIFFImageLoader::TiffFile::writeImage] TIFFWriteScanline failed.";
retval = AImgErrorCode::AIMG_WRITE_FAILED_EXTERNAL;
break;
}
}
}
TIFFClose(wTiff);
// Leave the pointer at the end of the file, because libtiff doesn't... because it's a fantastic piece of software
wCallbacks.mSeekCallback(wCallbacks.callbackData, wCallbacks.furthestPositionWritten);
return retval;
}
};
AImgBase *TIFFImageLoader::getAImg()
{
return new TiffFile();
}
int32_t TIFFImageLoader::initialise()
{
// silence libtiff's crap output.
// also, on windows, it will open message boxes for warnings.
// which block until you click ok.
// just what I wanted libtiff, thanks
TIFFSetWarningHandler(NULL);
return AImgErrorCode::AIMG_SUCCESS;
}
bool TIFFImageLoader::canLoadImage(ReadCallback readCallback, TellCallback tellCallback, SeekCallback seekCallback, void *callbackData)
{
int32_t startingPos = tellCallback(callbackData);
std::vector<uint8_t> header(4);
readCallback(callbackData, &header[0], 4);
seekCallback(callbackData, startingPos);
return (header[0] == 0x49 && header[1] == 0x49 && header[2] == 0x2a && header[3] == 0x00) ||
(header[0] == 0x4d && header[1] == 0x4d && header[2] == 0x00 && header[3] == 0x2a);
}
std::string TIFFImageLoader::getFileExtension()
{
return "tiff";
}
int32_t TIFFImageLoader::getAImgFileFormatValue()
{
return AImgFileFormat::TIFF_IMAGE_FORMAT;
}
AImgFormat TIFFImageLoader::getWhatFormatWillBeWrittenForData(int32_t inputFormat)
{
return getWriteFormatTiff(inputFormat);
}
}
#endif // HAVE_TIFF