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
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-06-17
* Description : A TIFF IO file for DImg framework
*
* Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
* Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* Specifications & references:
* - TIFF 6.0 : http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
* - TIFF/EP : http://www.map.tu.chiba-u.ac.jp/IEC/100/TA2/recdoc/N4378.pdf
* - TIFF/Tags : http://www.awaresystems.be/imaging/tiff/tifftags.html
* - DNG : http://www.adobe.com/products/dng/pdfs/dng_spec.pdf
*
* Others Linux Tiff Loader implementation using libtiff:
* - http://websvn.kde.org/trunk/koffice/filters/krita/tiff/kis_tiff_converter.cc
* - http://artis.inrialpes.fr/Software/TiffIO/
* - http://cvs.graphicsmagick.org/cgi-bin/cvsweb.cgi/GraphicsMagick/coders/tiff.c
* - http://freeimage.cvs.sourceforge.net/freeimage/FreeImage/Source/FreeImage/PluginTIFF.cpp
* - http://freeimage.cvs.sourceforge.net/freeimage/FreeImage/Source/Metadata/XTIFF.cpp
* - https://subversion.imagemagick.org/subversion/ImageMagick/trunk/coders/tiff.c
*
* Test images repository:
* - http://www.remotesensing.org/libtiff/images.html
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
// This line must be commented to prevent any latency time
// when we use threaded image loader interface for each image
// files io. Uncomment this line only for debugging.
//#define ENABLE_DEBUG_MESSAGES
// C ANSI includes.
extern "C"
{
#include <tiffvers.h>
}
// C++ includes.
#include <cstdio>
// TQt includes.
#include <tqfile.h>
// Local includes.
#include "ddebug.h"
#include "dimg.h"
#include "dimgloaderobserver.h"
#include "dmetadata.h"
#include "tiffloader.h"
namespace Digikam
{
// To manage Errors/Warnings handling provide by libtiff
void TIFFLoader::dimg_tiff_warning(const char* module, const char* format, va_list warnings)
{
#ifdef ENABLE_DEBUG_MESSAGES
char message[4096];
vsnprintf(message, 4096, format, warnings);
DDebug() << module << "::" << message << endl;
#else
Q_UNUSED(module);
Q_UNUSED(format);
Q_UNUSED(warnings);
#endif
}
void TIFFLoader::dimg_tiff_error(const char* module, const char* format, va_list errors)
{
#ifdef ENABLE_DEBUG_MESSAGES
char message[4096];
vsnprintf(message, 4096, format, errors);
DDebug() << module << "::" << message << endl;
#else
Q_UNUSED(module);
Q_UNUSED(format);
Q_UNUSED(errors);
#endif
}
TIFFLoader::TIFFLoader(DImg* image)
: DImgLoader(image)
{
m_hasAlpha = false;
m_sixteenBit = false;
}
bool TIFFLoader::load(const TQString& filePath, DImgLoaderObserver *observer)
{
readMetadata(filePath, DImg::TIFF);
// -------------------------------------------------------------------
// TIFF error handling. If an errors/warnings occurs during reading,
// libtiff will call these methods
TIFFSetWarningHandler(dimg_tiff_warning);
TIFFSetErrorHandler(dimg_tiff_error);
// -------------------------------------------------------------------
// Open the file
TIFF* tif = TIFFOpen(TQFile::encodeName(filePath), "r");
if (!tif)
{
DDebug() << k_funcinfo << "Cannot open image file." << endl;
return false;
}
#ifdef ENABLE_DEBUG_MESSAGES
TIFFPrintDirectory(tif, stdout, 0);
#endif
// -------------------------------------------------------------------
// Get image information.
uint32 w, h;
uint16 bits_per_sample;
uint16 samples_per_pixel;
uint16 photometric;
uint32 rows_per_strip;
tsize_t strip_size;
tstrip_t num_of_strips;
TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
if (TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rows_per_strip) == 0 ||
rows_per_strip == 0 || rows_per_strip == (unsigned int)-1)
{
DWarning() << "TIFF loader: Cannot handle non-stripped images. Loading file " << filePath << endl;
TIFFClose(tif);
return false;
}
if (bits_per_sample == 0 || samples_per_pixel == 0 ||
rows_per_strip == 0 || rows_per_strip > h)
{
DWarning() << "TIFF loader: Encountered invalid value 0 in image."
<< " bits_per_sample " << bits_per_sample
<< " samples_per_pixel " << samples_per_pixel
<< " rows_per_strip " << rows_per_strip
<< " Loading file " << filePath << endl;
TIFFClose(tif);
return false;
}
// TODO: check others TIFF color-spaces here. Actually, only RGB and MINISBLACK
// have been tested.
// Complete description of TIFFTAG_PHOTOMETRIC tag can be found at this url:
// http://www.awaresystems.be/imaging/tiff/tifftags/photometricinterpretation.html
TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric);
if (photometric != PHOTOMETRIC_RGB &&
photometric != PHOTOMETRIC_MINISBLACK)
{
DWarning() << "Can't handle image without RGB color-space: "
<< photometric << endl;
TIFFClose(tif);
return false;
}
if (samples_per_pixel == 4)
m_hasAlpha = true;
else
m_hasAlpha = false;
if (bits_per_sample == 16)
m_sixteenBit = true;
else
m_sixteenBit = false;
// -------------------------------------------------------------------
// Read image ICC profile
TQMap<int, TQByteArray>& metaData = imageMetaData();
uchar *profile_data=0;
uint32 profile_size;
if (TIFFGetField (tif, TIFFTAG_ICCPROFILE, &profile_size, &profile_data))
{
TQByteArray profile_rawdata(profile_size);
memcpy(profile_rawdata.data(), profile_data, profile_size);
metaData.insert(DImg::ICC, profile_rawdata);
}
else
{
// If ICC profile is null, check Exif metadata.
checkExifWorkingColorSpace();
}
// -------------------------------------------------------------------
// Get image data.
if (observer)
observer->progressInfo(m_image, 0.1);
uchar* data = 0;
strip_size = TIFFStripSize(tif);
num_of_strips = TIFFNumberOfStrips(tif);
if (bits_per_sample == 16) // 16 bits image.
{
data = new uchar[w*h*8];
uchar* strip = new uchar[strip_size];
long offset = 0;
long bytesRead = 0;
uint checkpoint = 0;
for (tstrip_t st=0; st < num_of_strips; st++)
{
if (observer && st == checkpoint)
{
checkpoint += granularity(observer, num_of_strips, 0.8);
if (!observer->continueQuery(m_image))
{
delete [] data;
delete [] strip;
TIFFClose(tif);
return false;
}
observer->progressInfo(m_image, 0.1 + (0.8 * ( ((float)st)/((float)num_of_strips) )));
}
bytesRead = TIFFReadEncodedStrip(tif, st, strip, strip_size);
if (bytesRead == -1)
{
DDebug() << k_funcinfo << "Failed to read strip" << endl;
delete [] data;
TIFFClose(tif);
return false;
}
ushort *stripPtr = (ushort*)(strip);
ushort *dataPtr = (ushort*)(data + offset);
ushort *p;
// tiff data is read as BGR or ABGR or Greyscale
if (samples_per_pixel == 3)
{
for (int i=0; i < bytesRead/6; i++)
{
p = dataPtr;
// See B.K.O #148037 : take a care about byte order with Motorola computers.
if (TQImage::systemByteOrder() == TQImage::BigEndian) // PPC
{
p[3] = *stripPtr++;
p[0] = *stripPtr++;
p[1] = *stripPtr++;
p[2] = 0xFFFF;
}
else
{
p[2] = *stripPtr++;
p[1] = *stripPtr++;
p[0] = *stripPtr++;
p[3] = 0xFFFF;
}
dataPtr += 4;
}
offset += bytesRead/6 * 8;
}
else if (samples_per_pixel == 1) // See B.K.O #148400: Greyscale pictures only have _one_ sample per pixel
{
for (int i=0; i < bytesRead/2; i++)
{
// We have to read two bytes for one pixel
p = dataPtr;
// See B.K.O #148037 : take a care about byte order with Motorola computers.
if (TQImage::systemByteOrder() == TQImage::BigEndian) // PPC
{
p[3] = 0xFFFF;
p[0] = *stripPtr;
p[1] = *stripPtr;
p[2] = *stripPtr++;
}
else
{
p[0] = *stripPtr; // RGB have to be set to the _same_ value
p[1] = *stripPtr;
p[2] = *stripPtr++;
p[3] = 0xFFFF; // set alpha to 100%
}
dataPtr += 4;
}
offset += bytesRead*4; // The _byte_offset in the data array is, of course, four times bytesRead
}
else // ABGR
{
for (int i=0; i < bytesRead/8; i++)
{
p = dataPtr;
// See B.K.O #148037 : take a care about byte order with Motorola computers.
if (TQImage::systemByteOrder() == TQImage::BigEndian) // PPC
{
p[3] = *stripPtr++;
p[0] = *stripPtr++;
p[1] = *stripPtr++;
p[2] = *stripPtr++;
}
else
{
p[2] = *stripPtr++;
p[1] = *stripPtr++;
p[0] = *stripPtr++;
p[3] = *stripPtr++;
}
dataPtr += 4;
}
offset += bytesRead;
}
}
delete [] strip;
}
else // Non 16 bits images ==> get it on BGRA 8 bits.
{
data = new uchar[w*h*4];
uchar* strip = new uchar[w*rows_per_strip*4];
long offset = 0;
long pixelsRead = 0;
// this is inspired by TIFFReadRGBAStrip, tif_getimage.c
char emsg[1024] = "";
TIFFRGBAImage img;
uint32 rows_to_read;
uint checkpoint = 0;
// test whether libtiff can read format and initiate reading
if (!TIFFRGBAImageOK(tif, emsg) || !TIFFRGBAImageBegin(&img, tif, 0, emsg))
{
DDebug() << k_funcinfo << "Failed to set up RGBA reading of image, filename "
<< TIFFFileName(tif) << " error message from Libtiff: " << emsg << endl;
delete [] data;
delete [] strip;
TIFFClose(tif);
return false;
}
img.req_orientation = ORIENTATION_TOPLEFT;
// read strips from image: read rows_per_strip, so always start at beginning of a strip
for (uint row = 0; row < h; row += rows_per_strip)
{
if (observer && row >= checkpoint)
{
checkpoint += granularity(observer, h, 0.8);
if (!observer->continueQuery(m_image))
{
delete [] data;
delete [] strip;
TIFFClose(tif);
return false;
}
observer->progressInfo(m_image, 0.1 + (0.8 * ( ((float)row)/((float)h) )));
}
img.row_offset = row;
img.col_offset = 0;
if( row + rows_per_strip > img.height )
rows_to_read = img.height - row;
else
rows_to_read = rows_per_strip;
// Read data
if (TIFFRGBAImageGet(&img, (uint32*)strip, img.width, rows_to_read ) == -1)
{
DDebug() << k_funcinfo << "Failed to read image data" << endl;
delete [] data;
delete [] strip;
TIFFClose(tif);
return false;
}
pixelsRead = rows_to_read * img.width;
uchar *stripPtr = (uchar*)(strip);
uchar *dataPtr = (uchar*)(data + offset);
uchar *p;
// Reverse red and blue
for (int i=0; i < pixelsRead; i++)
{
p = dataPtr;
// See B.K.O #148037 : take a care about byte order with Motorola computers.
if (TQImage::systemByteOrder() == TQImage::BigEndian) // PPC
{
p[3] = *stripPtr++;
p[0] = *stripPtr++;
p[1] = *stripPtr++;
p[2] = *stripPtr++;
}
else
{
p[2] = *stripPtr++;
p[1] = *stripPtr++;
p[0] = *stripPtr++;
p[3] = *stripPtr++;
}
dataPtr += 4;
}
offset += pixelsRead * 4;
}
TIFFRGBAImageEnd(&img);
delete [] strip;
}
// -------------------------------------------------------------------
TIFFClose(tif);
if (observer)
observer->progressInfo(m_image, 1.0);
imageWidth() = w;
imageHeight() = h;
imageData() = data;
imageSetAttribute("format", "TIFF");
return true;
}
bool TIFFLoader::save(const TQString& filePath, DImgLoaderObserver *observer)
{
TIFF *tif;
uchar *data;
uint32 w, h;
w = imageWidth();
h = imageHeight();
data = imageData();
// -------------------------------------------------------------------
// TIFF error handling. If an errors/warnings occurs during reading,
// libtiff will call these methods
TIFFSetWarningHandler(dimg_tiff_warning);
TIFFSetErrorHandler(dimg_tiff_error);
// -------------------------------------------------------------------
// Open the file
tif = TIFFOpen(TQFile::encodeName(filePath), "w");
if (!tif)
{
DDebug() << k_funcinfo << "Cannot open target image file." << endl;
return false;
}
// -------------------------------------------------------------------
// Set image properties
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
// Image must be compressed using deflate algorithm ?
TQVariant compressAttr = imageGetAttribute("compress");
bool compress = compressAttr.isValid() ? compressAttr.toBool() : false;
if (compress)
{
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
TIFFSetField(tif, TIFFTAG_ZIPQUALITY, 9);
// NOTE : this tag values aren't defined in libtiff 3.6.1. '2' is PREDICTOR_HORIZONTAL.
// Use horizontal differencing for images which are
// likely to be continuous tone. The TIFF spec says that this
// usually leads to better compression.
// See this url for more details:
// http://www.awaresystems.be/imaging/tiff/tifftags/predictor.html
TIFFSetField(tif, TIFFTAG_PREDICTOR, 2);
}
else
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
// Image has an alpha channel ?
if (imageHasAlpha())
{
uint16 sampleinfo[1] = { EXTRASAMPLE_UNASSALPHA };
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, EXTRASAMPLE_ASSOCALPHA, sampleinfo);
}
else
{
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
}
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16)imageBitsDepth());
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
// -------------------------------------------------------------------
// Write meta-data Tags contents.
DMetadata metaData;
metaData.setExif(m_image->getExif());
metaData.setIptc(m_image->getIptc());
// Standard IPTC tag (available with libtiff 3.6.1)
TQByteArray ba = metaData.getIptc(true);
if (!ba.isEmpty())
{
#if defined(TIFFTAG_PHOTOSHOP)
TIFFSetField (tif, TIFFTAG_PHOTOSHOP, (uint32)ba.size(), (uchar *)ba.data());
#endif
}
// Standard XMP tag (available with libtiff 3.6.1)
#if defined(TIFFTAG_XMLPACKET)
tiffSetExifDataTag(tif, TIFFTAG_XMLPACKET, &metaData, "Exif.Image.XMLPacket");
#endif
// Standard Exif Ascii tags (available with libtiff 3.6.1)
tiffSetExifAsciiTag(tif, TIFFTAG_DOCUMENTNAME, &metaData, "Exif.Image.DocumentName");
tiffSetExifAsciiTag(tif, TIFFTAG_IMAGEDESCRIPTION, &metaData, "Exif.Image.ImageDescription");
tiffSetExifAsciiTag(tif, TIFFTAG_MAKE, &metaData, "Exif.Image.Make");
tiffSetExifAsciiTag(tif, TIFFTAG_MODEL, &metaData, "Exif.Image.Model");
tiffSetExifAsciiTag(tif, TIFFTAG_DATETIME, &metaData, "Exif.Image.DateTime");
tiffSetExifAsciiTag(tif, TIFFTAG_ARTIST, &metaData, "Exif.Image.Artist");
tiffSetExifAsciiTag(tif, TIFFTAG_COPYRIGHT, &metaData, "Exif.Image.Copyright");
TQString soft = metaData.getExifTagString("Exif.Image.Software");
TQString libtiffver(TIFFLIB_VERSION_STR);
libtiffver.replace('\n', ' ');
soft.append(TQString(" ( %1 )").arg(libtiffver));
TIFFSetField(tif, TIFFTAG_SOFTWARE, (const char*)soft.ascii());
// NOTE: All others Exif tags will be written by Exiv2 (<= 0.18)
// -------------------------------------------------------------------
// Write ICC profil.
TQByteArray profile_rawdata = m_image->getICCProfil();
if (!profile_rawdata.isEmpty())
{
#if defined(TIFFTAG_ICCPROFILE)
TIFFSetField(tif, TIFFTAG_ICCPROFILE, (uint32)profile_rawdata.size(), (uchar *)profile_rawdata.data());
#endif
}
// -------------------------------------------------------------------
// Write full image data in tiff directory IFD0
if (observer)
observer->progressInfo(m_image, 0.1);
uint8 *buf=0;
uchar *pixel;
double alpha_factor;
uint32 x, y;
uint8 r8, g8, b8, a8=0;
uint16 r16, g16, b16, a16=0;
int i=0;
buf = (uint8 *) _TIFFmalloc(TIFFScanlineSize(tif));
if (!buf)
{
DDebug() << k_funcinfo << "Cannot allocate memory buffer for main image." << endl;
TIFFClose(tif);
return false;
}
uint checkpoint = 0;
for (y = 0; y < h; y++)
{
if (observer && y == checkpoint)
{
checkpoint += granularity(observer, h, 0.8);
if (!observer->continueQuery(m_image))
{
_TIFFfree(buf);
TIFFClose(tif);
return false;
}
observer->progressInfo(m_image, 0.1 + (0.8 * ( ((float)y)/((float)h) )));
}
i = 0;
for (x = 0; x < w; x++)
{
pixel = &data[((y * w) + x) * imageBytesDepth()];
if ( imageSixteenBit() ) // 16 bits image.
{
b16 = (uint16)(pixel[0]+256*pixel[1]);
g16 = (uint16)(pixel[2]+256*pixel[3]);
r16 = (uint16)(pixel[4]+256*pixel[5]);
if (imageHasAlpha())
{
// TIFF makes you pre-mutiply the rgb components by alpha
a16 = (uint16)(pixel[6]+256*pixel[7]);
alpha_factor = ((double)a16 / 65535.0);
r16 = (uint16)(r16*alpha_factor);
g16 = (uint16)(g16*alpha_factor);
b16 = (uint16)(b16*alpha_factor);
}
// This might be endian dependent
buf[i++] = (uint8)(r16);
buf[i++] = (uint8)(r16 >> 8);
buf[i++] = (uint8)(g16);
buf[i++] = (uint8)(g16 >> 8);
buf[i++] = (uint8)(b16);
buf[i++] = (uint8)(b16 >> 8);
if (imageHasAlpha())
{
buf[i++] = (uint8)(a16) ;
buf[i++] = (uint8)(a16 >> 8) ;
}
}
else // 8 bits image.
{
b8 = (uint8)pixel[0];
g8 = (uint8)pixel[1];
r8 = (uint8)pixel[2];
if (imageHasAlpha())
{
// TIFF makes you pre-mutiply the rgb components by alpha
a8 = (uint8)(pixel[3]);
alpha_factor = ((double)a8 / 255.0);
r8 = (uint8)(r8*alpha_factor);
g8 = (uint8)(g8*alpha_factor);
b8 = (uint8)(b8*alpha_factor);
}
// This might be endian dependent
buf[i++] = r8;
buf[i++] = g8;
buf[i++] = b8;
if (imageHasAlpha())
buf[i++] = a8;
}
}
if (!TIFFWriteScanline(tif, buf, y, 0))
{
DDebug() << k_funcinfo << "Cannot write main image to target file." << endl;
_TIFFfree(buf);
TIFFClose(tif);
return false;
}
}
_TIFFfree(buf);
TIFFWriteDirectory(tif);
// -------------------------------------------------------------------
// Write thumbnail in tiff directory IFD1
TQImage thumb = m_image->smoothScale(160, 120, TQSize::ScaleMin).copyTQImage();
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)thumb.width());
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)thumb.height());
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
uchar *pixelThumb;
uchar *dataThumb = thumb.bits();
uint8 *bufThumb = (uint8 *) _TIFFmalloc(TIFFScanlineSize(tif));
if (!bufThumb)
{
DDebug() << k_funcinfo << "Cannot allocate memory buffer for thumbnail." << endl;
TIFFClose(tif);
return false;
}
for (y = 0 ; y < uint32(thumb.height()) ; y++)
{
i = 0;
for (x = 0 ; x < uint32(thumb.width()) ; x++)
{
pixelThumb = &dataThumb[((y * thumb.width()) + x) * 4];
// This might be endian dependent
bufThumb[i++] = (uint8)pixelThumb[2];
bufThumb[i++] = (uint8)pixelThumb[1];
bufThumb[i++] = (uint8)pixelThumb[0];
}
if (!TIFFWriteScanline(tif, bufThumb, y, 0))
{
DDebug() << k_funcinfo << "Cannot write thumbnail to target file." << endl;
_TIFFfree(bufThumb);
TIFFClose(tif);
return false;
}
}
_TIFFfree(bufThumb);
TIFFClose(tif);
// -------------------------------------------------------------------
if (observer)
observer->progressInfo(m_image, 1.0);
imageSetAttribute("savedformat", "TIFF");
saveMetadata(filePath);
return true;
}
bool TIFFLoader::hasAlpha() const
{
return m_hasAlpha;
}
bool TIFFLoader::sixteenBit() const
{
return m_sixteenBit;
}
void TIFFLoader::tiffSetExifAsciiTag(TIFF* tif, ttag_t tiffTag,
const DMetadata *metaData, const char* exifTagName)
{
TQByteArray tag = metaData->getExifTagData(exifTagName);
if (!tag.isEmpty())
{
TQCString str(tag.data(), tag.size());
TIFFSetField(tif, tiffTag, (const char*)str);
}
}
void TIFFLoader::tiffSetExifDataTag(TIFF* tif, ttag_t tiffTag,
const DMetadata *metaData, const char* exifTagName)
{
TQByteArray tag = metaData->getExifTagData(exifTagName);
if (!tag.isEmpty())
{
TIFFSetField (tif, tiffTag, (uint32)tag.size(), (char *)tag.data());
}
}
} // NameSpace Digikam
|