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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-05-29
* Description : static helper methods for PGF image format.
*
* SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "pgfutils.h"
// C Ansi includes
extern "C"
{
#ifndef Q_CC_MSVC
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
}
// Qt includes
#include <QFile>
#include <qplatformdefs.h>
// Windows includes
#ifdef Q_OS_WIN
# include <windows.h>
#endif
// LibPGF includes
#if defined(Q_CC_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wkeyword-macro"
#endif
#include "PGFimage.h"
#if defined(Q_CC_CLANG)
# pragma clang diagnostic pop
#endif
// Local includes
#include "digikam_debug.h"
namespace Digikam
{
namespace PGFUtils
{
// Private method
bool writePGFImageDataToStream(const QImage& image,
CPGFStream& stream,
int quality,
UINT32& nWrittenBytes,
bool verbose);
bool readPGFImageData(const QByteArray& data,
QImage& img,
bool verbose)
{
try
{
if (data.isEmpty())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF image data to decode : size is null";
return false;
}
CPGFMemoryStream stream((UINT8*)data.data(), (size_t)data.size());
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: image data stream size is : " << stream.GetSize();
}
CPGFImage pgfImg;
// NOTE: see bug #273765 : Loading PGF thumbs with OpenMP support through a separated thread do not work properly with libppgf 6.11.24
pgfImg.ConfigureDecoder(false);
pgfImg.Open(&stream);
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF image is open";
}
if (pgfImg.Channels() != 4)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF channels not supported";
return false;
}
img = QImage(pgfImg.Width(), pgfImg.Height(), QImage::Format_ARGB32);
pgfImg.Read();
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF image is read";
}
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
{
int map[] = {3, 2, 1, 0};
pgfImg.GetBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
else
{
int map[] = {0, 1, 2, 3};
pgfImg.GetBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF image is decoded";
}
}
catch (IOException& e)
{
int err = e.error;
if (err >= AppError)
{
err -= AppError;
}
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error running libpgf (" << err << ")!";
return false;
}
return true;
}
bool writePGFImageFile(const QImage& image,
const QString& filePath,
int quality,
bool verbose)
{
#ifdef Q_OS_WIN
HANDLE fd = CreateFile((LPCWSTR)filePath.utf16(), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (fd == INVALID_HANDLE_VALUE)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error: Could not open destination file.";
return false;
}
#else
int fd = QT_OPEN(filePath.toUtf8().constData(),
O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error: Could not open destination file.";
return false;
}
#endif
CPGFFileStream stream(fd);
UINT32 nWrittenBytes = 0;
bool ret = writePGFImageDataToStream(image, stream, quality, nWrittenBytes, verbose);
if (!nWrittenBytes)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Written PGF file : data size is null";
ret = false;
}
else
{
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: file size written : " << nWrittenBytes;
}
}
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
return ret;
}
bool writePGFImageData(const QImage& image, QByteArray& data, int quality, bool verbose)
{
try
{
// We will use uncompressed image bytes size to allocate PGF stream in memory. In all case, due to PGF compression ratio,
// PGF data will be so far lesser than image raw size.
int rawSize = image.sizeInBytes();
CPGFMemoryStream stream(rawSize);
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF stream memory allocation in bytes: " << rawSize;
}
UINT32 nWrittenBytes = 0;
bool ret = writePGFImageDataToStream(image, stream, quality, nWrittenBytes, verbose);
int pgfsize =
#ifdef PGFCodecVersionID
# if PGFCodecVersionID == 0x061224
// Wrap around libpgf 6.12.24 about CPGFMemoryStream bytes size generated to make PGF file data.
// It miss 16 bytes at end. This solution fix the problem. Problem have been fixed in 6.12.27.
nWrittenBytes + 16;
# else
nWrittenBytes;
# endif
#else
nWrittenBytes;
#endif
data = QByteArray((const char*)stream.GetBuffer(), pgfsize);
if (!pgfsize)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Encoded PGF image : data size is null";
ret = false;
}
else
{
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: data size written : " << pgfsize;
}
}
return ret;
}
catch (IOException& e)
{
int err = e.error;
if (err >= AppError)
{
err -= AppError;
}
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error running libpgf (" << err << ")!";
return false;
}
}
bool writePGFImageDataToStream(const QImage& image,
CPGFStream& stream,
int quality,
UINT32& nWrittenBytes,
bool verbose)
{
try
{
if (image.isNull())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Thumb image is null";
return false;
}
QImage img;
// Convert image with Alpha channel.
if (image.format() != QImage::Format_ARGB32)
{
img = image.convertToFormat(QImage::Format_ARGB32);
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: RGB => ARGB";
}
}
else
{
img = image;
}
CPGFImage pgfImg;
PGFHeader header;
header.width = img.width();
header.height = img.height();
header.nLevels = 0; // Auto.
header.quality = quality;
header.bpp = img.depth();
header.channels = 4;
header.mode = ImageModeRGBA;
header.usedBitsPerChannel = 0; // Auto
#ifdef PGFCodecVersionID
# if PGFCodecVersionID < 0x061142
header.background.rgbtBlue = 0;
header.background.rgbtGreen = 0;
header.background.rgbtRed = 0;
# endif
#endif
pgfImg.SetHeader(header);
// NOTE: see bug #273765 : Loading PGF thumbs with OpenMP support through a separated thread do not work properly with libppgf 6.11.24
pgfImg.ConfigureEncoder(false);
if (verbose)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF image settings:";
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: width: " << header.width;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: height: " << header.height;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: nLevels: " << header.nLevels;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: quality: " << header.quality;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: bpp: " << header.bpp;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: channels: " << header.channels;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: mode: " << header.mode;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: usedBitsPerChannel: " << header.usedBitsPerChannel;
}
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
{
int map[] = {3, 2, 1, 0};
pgfImg.ImportBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
else
{
int map[] = {0, 1, 2, 3};
pgfImg.ImportBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
nWrittenBytes = 0;
#ifdef PGFCodecVersionID
# if PGFCodecVersionID >= 0x061124
pgfImg.Write(&stream, &nWrittenBytes);
# else
pgfImg.Write(&stream, 0, 0, &nWrittenBytes);
# endif
#else
pgfImg.Write(&stream, 0, 0, &nWrittenBytes);
#endif
}
catch (IOException& e)
{
int err = e.error;
if (err >= AppError)
{
err -= AppError;
}
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error running libpgf (" << err << ")!";
return false;
}
return true;
}
bool loadPGFScaled(QImage& img, const QString& path, int maximumSize)
{
#ifdef Q_OS_WIN
FILE* const file = _wfopen((const wchar_t*)path.utf16(), L"rb");
#else
FILE* const file = fopen(path.toUtf8().constData(), "rb");
#endif
if (!file)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error: Could not open source file.";
return false;
}
unsigned char header[3];
if (fread(&header, 3, 1, file) != 1)
{
fclose(file);
return false;
}
unsigned char pgfID[3] = { 0x50, 0x47, 0x46 };<--- Variable 'pgfID' can be declared as const array
if (memcmp(&header[0], &pgfID, 3) != 0)
{
// not a PGF file
fclose(file);
return false;
}
fclose(file);
// -------------------------------------------------------------------
// Initialize PGF API.
#ifdef Q_OS_WIN
HANDLE fd = CreateFile((LPCWSTR)path.utf16(), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (fd == INVALID_HANDLE_VALUE)
{
return false;
}
#else
int fd = QT_OPEN(path.toUtf8().constData(), O_RDONLY);
if (fd == -1)
{
return false;
}
#endif
try
{
CPGFFileStream stream(fd);
CPGFImage pgf;
pgf.Open(&stream);
// Try to find the right PGF level to get reduced image accordingly
// with preview size wanted.
int i = 0;
if (pgf.Levels() > 0)
{
for (i = pgf.Levels()-1 ; i >= 0 ; --i)
{
if (qMin((int)pgf.Width(i), (int)pgf.Height(i)) >= maximumSize)
{
break;
}
}
}
if (i < 0)
{
i = 0;
}
pgf.Read(i); // Read PGF image at reduced level i.
img = QImage(pgf.Width(i), pgf.Height(i), QImage::Format_RGB32);
/*
const PGFHeader* header = pgf.GetHeader();
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF width = " << header->width;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF height = " << header->height;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF bbp = " << header->bpp;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF channels = " << header->channels;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF quality = " << header->quality;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF mode = " << header->mode;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: PGF levels = " << header->nLevels;
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Level (w x h)= " << i << "(" << pgf.Width(i)
<< " x " << pgf.Height(i) << ")";
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: QImage depth = " << img.depth();
*/
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
{
int map[] = {3, 2, 1, 0};
pgf.GetBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
else
{
int map[] = {0, 1, 2, 3};
pgf.GetBitmap(img.bytesPerLine(), (UINT8*)img.bits(), img.depth(), map);
}
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
}
catch (IOException& e)
{
int err = e.error;
if (err >= AppError)
{
err -= AppError;
}
qCDebug(DIGIKAM_GENERAL_LOG) << "PGFUtils: Error running libpgf (" << err << ")!";
#ifdef Q_OS_WIN
CloseHandle(fd);
#else
close(fd);
#endif
return false;
}
return true;
}
QString libPGFVersion()
{
return (QLatin1String(PGFCodecVersion));
}
} // namespace PGFUtils
} // namespace Digikam
|