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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-06-03
* Description : A PGF IO file for DImg framework - load operations
*
* SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "digikam_config.h"
#include "dimgpgfloader.h" // krazy:exclude=includes
// C Ansi includes
extern "C"
{
#ifndef Q_CC_MSVC
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
}
// C++ includes
#include <iostream>
#include <cmath>
#include <cstdio>
// Qt includes
#include <QFile>
#include <QVariant>
#include <qplatformdefs.h>
// Windows includes
#ifdef Q_OS_WIN
# include <windows.h>
#endif
// Libpgf includes
// Pragma directives to reduce warnings from Libpgf header files.
#if defined(Q_CC_GNU)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#if defined(Q_CC_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wkeyword-macro"
# pragma clang diagnostic ignored "-Wreorder-ctor"
#endif
#include "PGFimage.h"
// Restore warnings
#if defined(Q_CC_CLANG)
# pragma clang diagnostic pop
#endif
#if defined(Q_CC_GNU)
# pragma GCC diagnostic pop
#endif
// Local includes
#include "digikam_debug.h"
#include "dimg.h"
#include "dimgloaderobserver.h"
#include "pgfutils.h"
#include "metaengine.h"
namespace Digikam
{
bool DImgPGFLoader::load(const QString& filePath, DImgLoaderObserver* const observer)
{
m_observer = observer;
readMetadata(filePath);
#ifdef Q_OS_WIN
FILE* const file = _wfopen((const wchar_t*)filePath.utf16(), L"rb");
#else
FILE* const file = fopen(filePath.toUtf8().constData(), "rb");
#endif
if (!file)
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Error: Could not open source file.";
loadingFailed();
return false;
}
unsigned char header[3];
if (fread(&header, 3, 1, file) != 1)
{
loadingFailed();
fclose(file);
return false;
}
const unsigned char pgfID[3] = { 0x50, 0x47, 0x46 };
if (memcmp(&header[0], &pgfID, 3) != 0)
{
// not a PGF file
loadingFailed();
fclose(file);
return false;
}
fclose(file);
// -------------------------------------------------------------------
// Initialize PGF API.
#ifdef Q_OS_WIN
HANDLE fd = CreateFileW((LPCWSTR)filePath.utf16(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
if (fd == INVALID_HANDLE_VALUE)
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Error: Could not open source file.";
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Last error code:" << GetLastError();
loadingFailed();
return false;
}
#else
int fd = QT_OPEN(filePath.toUtf8().constData(), O_RDONLY);
if (fd == -1)
{
loadingFailed();
return false;
}
#endif
CPGFFileStream stream(fd);
CPGFImage pgf;
int colorModel = DImg::COLORMODELUNKNOWN;
try
{
// open pgf image
pgf.Open(&stream);
switch (pgf.Mode())
{
case ImageModeRGBColor:
case ImageModeRGB48:
{
m_hasAlpha = false;
colorModel = DImg::RGB;
break;
}
case ImageModeRGBA:
{
m_hasAlpha = true;
colorModel = DImg::RGB;
break;
}
default:
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Cannot load PGF image: color mode not supported ("
<< pgf.Mode() << ")";
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
loadingFailed();
return false;
}
}
switch (pgf.Channels())
{
case 3:
case 4:
{
break;
}
default:
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Cannot load PGF image: color channels number not supported ("
<< pgf.Channels() << ")";
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
loadingFailed();
return false;
}
}
int bitDepth = pgf.BPP();
switch (bitDepth)
{
case 24: // RGB 8 bits.
case 32: // RGBA 8 bits.
{
m_sixteenBit = false;
break;
}
case 48: // RGB 16 bits.
case 64: // RGBA 16 bits.
{
m_sixteenBit = true;
break;
}
default:
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Cannot load PGF image: color bits depth not supported ("
<< bitDepth << ")";
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
loadingFailed();
return false;
}
}
if (DIGIKAM_DIMG_LOG_PGF().isDebugEnabled())
{
const PGFHeader* const pgfHeader = pgf.GetHeader();
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF width = " << pgfHeader->width;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF height = " << pgfHeader->height;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF bbp = " << pgfHeader->bpp;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF channels = " << pgfHeader->channels;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF quality = " << pgfHeader->quality;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "PGF mode = " << pgfHeader->mode;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "Has Alpha = " << m_hasAlpha;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "Is 16 bits = " << m_sixteenBit;
}
// NOTE: see bug #273765 : Loading PGF thumbs with OpenMP support through a separated thread do not work properly with libppgf 6.11.24
pgf.ConfigureDecoder(false);
int width = pgf.Width();
int height = pgf.Height();
uchar* data = nullptr;
QSize originalSize(width, height);
if (m_loadFlags & LoadImageData)
{
// Find out if we do the fast-track loading with reduced size. PGF specific.
int level = 0;
QVariant attribute = imageGetAttribute(QLatin1String("scaledLoadingSize"));
if (attribute.isValid() && pgf.Levels() > 0)
{
int scaledLoadingSize = attribute.toInt();
int i, w, h;
for (i = pgf.Levels() - 1 ; i >= 0 ; --i)
{
w = pgf.Width(i);
h = pgf.Height(i);
if (qMin(w, h) >= scaledLoadingSize)
{
break;
}
}
if (i >= 0)
{
width = w;
height = h;
level = i;
qCDebug(DIGIKAM_DIMG_LOG_PGF) << "Loading PGF scaled version at level " << i
<< " (" << w << " x " << h << ") for size "
<< scaledLoadingSize;
}
}
if (m_sixteenBit)
{
data = new_failureTolerant(width, height, 8); // 16 bits/color/pixel
}
else
{
data = new_failureTolerant(width, height, 4); // 8 bits/color/pixel
}
// Fill all with 255 including alpha channel.
memset(data, 0xFF, width * height * (m_sixteenBit ? 8 : 4));
pgf.Read(level, DImgPGFLoader::CallbackForLibPGF, this);
pgf.GetBitmap(m_sixteenBit ? width * 8 : width * 4,
(UINT8*)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.
m_sixteenBit ? 64 : 32,
nullptr,
CallbackForLibPGF, this);
if (observer)
{
observer->progressInfo(1.0F);
}
}
// -------------------------------------------------------------------
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
// Get ICC color profile.
if (m_loadFlags & LoadICCData)
{
// TODO: Implement proper storage in PGF for color profiles
checkExifWorkingColorSpace();
}
imageWidth() = width;
imageHeight() = height;
imageData() = data;
imageSetAttribute(QLatin1String("format"), QLatin1String("PGF"));
imageSetAttribute(QLatin1String("originalColorModel"), colorModel);
imageSetAttribute(QLatin1String("originalBitDepth"), bitDepth);
imageSetAttribute(QLatin1String("originalSize"), originalSize);
return true;
}
catch (IOException& e)
{
int err = e.error;
if (err >= AppError)
{
err -= AppError;
}
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Error: Opening and reading PGF image failed (" << err << ")!";
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
loadingFailed();
return false;
}
catch (std::bad_alloc& e)
{
qCWarning(DIGIKAM_DIMG_LOG_PGF) << "Failed to allocate memory for loading" << filePath << e.what();
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
loadingFailed();
return false;
}
return true;
}
} // namespace Digikam
|