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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2005-06-17
* Description : A TIFF IO file for DImg framework - save operations
*
* SPDX-FileCopyrightText: 2005 by Renchi Raju <renchi dot raju at gmail dot com>
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
// C ANSI includes
extern "C"
{
#include <tiffvers.h>
}
// C++ includes
#include <cstdio>
// Qt includes
#include <QFile>
#include <QByteArray>
#include <QScopedPointer>
// Local includes
#include "digikam_debug.h"
#include "digikam_config.h"
#include "dimgloaderobserver.h"
#include "dimgtiffloader.h" //krazy:exclude=includes
namespace DigikamTIFFDImgPlugin
{
bool DImgTIFFLoader::save(const QString& filePath, DImgLoaderObserver* const observer)
{
uint32 w = imageWidth();
uint32 h = imageHeight();
uchar* 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
#ifdef Q_OS_WIN
TIFF* const tif = TIFFOpenW((const wchar_t*)filePath.utf16(), "w");
#else
TIFF* const tif = TIFFOpen(filePath.toUtf8().constData(), "w");
#endif
if (!tif)
{
qCWarning(DIGIKAM_DIMG_LOG_TIFF) << "Cannot open target image file.";
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 ?
QVariant compressAttr = imageGetAttribute(QLatin1String("compress"));
bool compress = compressAttr.isValid() ? compressAttr.toBool() : false;
qCDebug(DIGIKAM_DIMG_LOG_TIFF) << "TIFF Compression Enabled:" << compress;
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:
// www.awaresystems.be/imaging/tiff/tifftags/predictor.html
TIFFSetField(tif, TIFFTAG_PREDICTOR, 2);
}
else
{
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
}
uint16 sampleinfo[1];
if (imageHasAlpha())
{
sampleinfo[0] = EXTRASAMPLE_ASSOCALPHA;
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, 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.
QScopedPointer<DMetadata> metaData(new DMetadata(m_image->getMetadata()));
// Standard IPTC tag (available with libtiff 3.6.1)
QByteArray 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 (metaData->hasXmp())
{
#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");
QString soft = metaData->getExifTagString("Exif.Image.Software");
QString libtiffver = QLatin1String(TIFFLIB_VERSION_STR);
libtiffver.replace(QLatin1Char('\n'), QLatin1Char(' '));
soft.append(QString::fromLatin1(" ( %1 )").arg(libtiffver));
TIFFSetField(tif, TIFFTAG_SOFTWARE, (const char*)soft.toLatin1().constData());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
// NOTE: All others Exif tags will be written by Exiv2 (<= 0.18)
// -------------------------------------------------------------------
// Write ICC profile.
QByteArray profile_rawdata = m_image->getIccProfile().data();
if (!profile_rawdata.isEmpty())
{
#if defined(TIFFTAG_ICCPROFILE)
purgeExifWorkingColorSpace();
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(0.1F);
}
uchar* pixel = nullptr;
uint16* pixel16 = nullptr;<--- Variable 'pixel16' can be declared as pointer to const
double alpha_factor = 0;
uint32 x = 0;
uint32 y = 0;
uint8 r8 = 0;
uint8 g8 = 0;
uint8 b8 = 0;
uint8 a8 = 0;
uint16 r16 = 0;
uint16 g16 = 0;
uint16 b16 = 0;
uint16 a16 = 0;
int i = 0;
uint8* buf = (uint8*)_TIFFmalloc(TIFFScanlineSize(tif));<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
uint16* buf16 = nullptr;
if (!buf)
{
qCWarning(DIGIKAM_DIMG_LOG_TIFF) << "Cannot allocate memory buffer for main image.";
TIFFClose(tif);
return false;
}
uint checkpoint = 0;
for (y = 0 ; y < h ; ++y)
{
if (observer && (y == checkpoint))
{
checkpoint += granularity(observer, h, 0.8F);
if (!observer->continueQuery())
{
_TIFFfree(buf);
TIFFClose(tif);
return false;
}
observer->progressInfo(0.1F + (0.8F * (((float)y) / ((float)h))));
}
i = 0;
for (x = 0 ; x < w ; ++x)
{
pixel = &data[((y * w) + x) * imageBytesDepth()];
if (imageSixteenBit()) // 16 bits image.
{
pixel16 = reinterpret_cast<ushort*>(pixel);
b16 = pixel16[0];
g16 = pixel16[1];
r16 = pixel16[2];
if (imageHasAlpha())
{
// TIFF makes you pre-multiply the RGB components by alpha
a16 = pixel16[3];
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
buf16 = reinterpret_cast<ushort*>(buf+i);
*buf16++ = r16;
*buf16++ = g16;
*buf16++ = b16;
i += 6;
if (imageHasAlpha())
{
*buf16++ = a16;
i += 2;
}
}
else // 8 bits image.
{
b8 = (uint8)pixel[0];
g8 = (uint8)pixel[1];
r8 = (uint8)pixel[2];
if (imageHasAlpha())
{
// TIFF makes you pre-multiply 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))
{
qCWarning(DIGIKAM_DIMG_LOG_TIFF) << "Cannot write main image to target file.";
_TIFFfree(buf);
TIFFClose(tif);
return false;
}
}
_TIFFfree(buf);
TIFFWriteDirectory(tif);
// -------------------------------------------------------------------
// Write thumbnail in tiff directory IFD1
QImage thumb = m_image->smoothScale(160, 120, Qt::KeepAspectRatio).copyQImage();
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 = nullptr;
uchar* dataThumb = thumb.bits();
uint8* bufThumb = (uint8*) _TIFFmalloc(TIFFScanlineSize(tif));<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
if (!bufThumb)
{
qCWarning(DIGIKAM_DIMG_LOG_TIFF) << "Cannot allocate memory buffer for thumbnail.";
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))
{
qCWarning(DIGIKAM_DIMG_LOG_TIFF) << "Cannot write thumbnail to target file.";
_TIFFfree(bufThumb);
TIFFClose(tif);
return false;
}
}
_TIFFfree(bufThumb);
TIFFClose(tif);
// -------------------------------------------------------------------
if (observer)
{
observer->progressInfo(1.0F);
}
imageSetAttribute(QLatin1String("savedFormat"), QLatin1String("TIFF"));
// Save metadata
QScopedPointer<DMetadata> metaDataToFile(new DMetadata(filePath));
metaDataToFile->setData(m_image->getMetadata());
// See bug #211758 for these special steps needed when writing a TIFF
metaDataToFile->removeExifThumbnail();
metaDataToFile->removeExifTag("Exif.Image.ProcessingSoftware");
metaDataToFile->applyChanges(true);
return true;
}
void DImgTIFFLoader::tiffSetExifAsciiTag(TIFF* const tif,
ttag_t tiffTag,
const DMetadata& metaData,
const char* const exifTagName)
{
QByteArray tag = metaData.getExifTagData(exifTagName);
if (!tag.isEmpty())
{
QByteArray str(tag.data(), tag.size());
TIFFSetField(tif, tiffTag, (const char*)str.constData());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
}
}
void DImgTIFFLoader::tiffSetExifDataTag(TIFF* const tif,
ttag_t tiffTag,
const DMetadata& metaData,
const char* const exifTagName)
{
QByteArray tag = metaData.getExifTagData(exifTagName);
if (!tag.isEmpty())
{
TIFFSetField(tif, tiffTag, (uint32)tag.size(), (char*)tag.data());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
}
}
} // namespace DigikamTIFFDImgPlugin
|