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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2006-12-09
* Description : a tread-safe libraw Qt interface
*
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2007-2008 by Guillaume Castagnino <casta at xwing dot info>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "drawdecoder_p.h"
namespace Digikam
{
DRawDecoder::DRawDecoder()
: d(new Private(this))
{
}
DRawDecoder::~DRawDecoder()
{
cancel();
delete d;
}
void DRawDecoder::cancel()
{
m_cancel = true;
}
bool DRawDecoder::loadRawPreview(QImage& image, const QString& path)
{
// In first, try to extract the embedded JPEG preview. Very fast.
bool ret = loadEmbeddedPreview(image, path);
if (ret)
{
return true;
}
// In second, decode and half size of RAW picture. More slow.
return (loadHalfPreview(image, path));
}
bool DRawDecoder::loadEmbeddedPreview(QImage& image, const QString& path)
{
QByteArray imgData;
if (loadEmbeddedPreview(imgData, path))
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Preview data size:" << imgData.size();
if (image.loadFromData(imgData))
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Using embedded RAW preview extraction";
return true;
}
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Failed to load embedded RAW preview";
return false;
}
bool DRawDecoder::loadEmbeddedPreview(QByteArray& imgData, const QString& path)
{
QFileInfo fileInfo(path);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: loadEmbeddedPreview from" << path;
LibRaw* const raw = new LibRaw;
#ifdef Q_OS_WIN
int ret = raw->open_file((const wchar_t*)path.utf16());
#else
int ret = raw->open_file(path.toUtf8().constData());
#endif
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run open_file: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
return (Private::loadEmbeddedPreview(imgData, raw));
}
bool DRawDecoder::loadEmbeddedPreview(QByteArray& imgData, const QBuffer& buffer)
{
LibRaw* const raw = new LibRaw;
QByteArray inData = buffer.data();
int ret = raw->open_buffer((void*) inData.data(), (size_t) inData.size());<--- 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 (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run open_buffer: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
return (Private::loadEmbeddedPreview(imgData, raw));
}
bool DRawDecoder::loadHalfPreview(QImage& image, const QString& path, bool rotate)
{
QFileInfo fileInfo(path);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Try to use reduced RAW picture extraction";
LibRaw* const raw = new LibRaw;
raw->imgdata.params.use_auto_wb = 1; // Use automatic white balance.
raw->imgdata.params.use_camera_wb = 1; // Use camera white balance, if possible.
raw->imgdata.params.half_size = 1; // Half-size color image (3x faster than -q).
#ifdef Q_OS_WIN
int ret = raw->open_file((const wchar_t*)path.utf16());
#else
int ret = raw->open_file(path.toUtf8().constData());
#endif
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run open_file: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
if (!Private::loadHalfPreview(image, raw, rotate))
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Failed to get half preview from LibRaw!";
return false;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Using reduced RAW picture extraction for" << path;
return true;
}
bool DRawDecoder::loadHalfPreview(QByteArray& imgData, const QString& path)
{
QFileInfo fileInfo(path);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Try to use reduced RAW picture extraction";
LibRaw* const raw = new LibRaw;
#ifdef Q_OS_WIN
int ret = raw->open_file((const wchar_t*)path.utf16());
#else
int ret = raw->open_file(path.toUtf8().constData());
#endif
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run dcraw_process: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
QImage image;
if (!Private::loadHalfPreview(image, raw))
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "DRawDecoder: failed to get half preview: " << libraw_strerror(ret);
return false;
}
QBuffer buffer(&imgData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG");
return true;
}
bool DRawDecoder::loadHalfPreview(QByteArray& imgData, const QBuffer& inBuffer)
{
LibRaw* const raw = new LibRaw;
QByteArray inData = inBuffer.data();
int ret = raw->open_buffer((void*) inData.data(), (size_t) inData.size());<--- 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 (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run dcraw_make_mem_image: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
QImage image;
if (!Private::loadHalfPreview(image, raw))
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "DRawDecoder: failed to get half preview: " << libraw_strerror(ret);
return false;
}
QBuffer buffer(&imgData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPG");
buffer.close();
return true;
}
bool DRawDecoder::loadFullImage(QImage& image,
const QString& path,
const DRawDecoderSettings& settings)
{
QFileInfo fileInfo(path);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Try to load full RAW picture...";
QPointer<DRawDecoder> decoder(new DRawDecoder);
QByteArray imgData;
int width, height, rgbmax;
DRawDecoderSettings prm = settings;
prm.sixteenBitsImage = false;
bool ret = decoder->decodeRAWImage(path, prm, imgData, width, height, rgbmax);
if (!ret)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Failed to load full RAW picture";
return false;
}
uchar* sptr = (uchar*)imgData.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.
uchar tmp8[2];
// Set RGB color components.
for (int i = 0 ; i < (width * height) ; ++i)
{
// Swap Red and Blue
tmp8[0] = sptr[2];
tmp8[1] = sptr[0];
sptr[0] = tmp8[0];
sptr[2] = tmp8[1];
sptr += 3;
}
image = QImage(width, height, QImage::Format_ARGB32);
uint* dptr = reinterpret_cast<uint*>(image.bits());
sptr = (uchar*)imgData.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.
for (int i = 0 ; i < (width * height) ; ++i)
{
*dptr++ = qRgba(sptr[2], sptr[1], sptr[0], 0xFF);
sptr += 3;
}
qCDebug(DIGIKAM_RAWENGINE_LOG) << "Load full RAW picture done";
return true;
}
bool DRawDecoder::rawFileIdentify(DRawInfo& identify, const QString& path)
{
QFileInfo fileInfo(path);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
identify.isDecodable = false;
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
LibRaw* const raw = new LibRaw;
#ifdef Q_OS_WIN
int ret = raw->open_file((const wchar_t*)path.utf16());
#else
int ret = raw->open_file(path.toUtf8().constData());
#endif
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run open_file: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
ret = raw->adjust_sizes_info_only();
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run adjust_sizes_info_only: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
Private::fillIndentifyInfo(raw, identify);
raw->recycle();
delete raw;
return true;
}
// ----------------------------------------------------------------------------------
bool DRawDecoder::extractRAWData(const QString& filePath,
QByteArray& rawData,
DRawInfo& identify,
unsigned int shotSelect)
{
QFileInfo fileInfo(filePath);
QString rawFilesExt = rawFiles();
QString ext = fileInfo.suffix().toUpper();
identify.isDecodable = false;
if (!fileInfo.exists() || ext.isEmpty() || !rawFilesExt.toUpper().contains(ext))
{
return false;
}
if (m_cancel)
{
return false;
}
d->setProgress(0.1);
LibRaw* const raw = new LibRaw;
// Set progress call back function.
raw->set_progress_handler(s_progressCallbackForLibRaw, d);
raw->set_exifparser_handler(s_exifParserCallbackForLibRaw, d);
#ifdef Q_OS_WIN
int ret = raw->open_file((const wchar_t*)filePath.utf16());
#else
int ret = raw->open_file(filePath.toUtf8().constData());
#endif
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run open_file: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
// cppcheck-suppress knownConditionTrueFalse
if (m_cancel)
{
raw->recycle();
delete raw;
return false;
}
d->setProgress(0.3);
raw->imgdata.params.output_bps = 16;
raw->imgdata.rawparams.shot_select = shotSelect;
raw->imgdata.params.user_flip = -1;
ret = raw->unpack();
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run unpack: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
// cppcheck-suppress knownConditionTrueFalse
if (m_cancel)
{
raw->recycle();
delete raw;
return false;
}
d->setProgress(0.4);
ret = raw->raw2image();
if (ret != LIBRAW_SUCCESS)
{
qCDebug(DIGIKAM_RAWENGINE_LOG) << "LibRaw: failed to run raw2image: " << libraw_strerror(ret);
raw->recycle();
delete raw;
return false;
}
// cppcheck-suppress knownConditionTrueFalse
if (m_cancel)
{
raw->recycle();
delete raw;
return false;
}
d->setProgress(0.6);
Private::fillIndentifyInfo(raw, identify);
// cppcheck-suppress knownConditionTrueFalse
if (m_cancel)
{
raw->recycle();
delete raw;
return false;
}
d->setProgress(0.8);
rawData = QByteArray();
if (raw->imgdata.idata.filters == 0)
{
rawData.resize((int)((int)raw->imgdata.sizes.iwidth * (int)raw->imgdata.sizes.iheight * (int)raw->imgdata.idata.colors * sizeof(unsigned short)));
unsigned short* output = reinterpret_cast<unsigned short*>(rawData.data());
for (unsigned int row = 0 ; row < raw->imgdata.sizes.iheight ; ++row)
{
for (unsigned int col = 0 ; col < raw->imgdata.sizes.iwidth ; ++col)
{
for (int color = 0 ; color < raw->imgdata.idata.colors ; ++color)
{
*output = raw->imgdata.image[raw->imgdata.sizes.iwidth*row + col][color];
output++;
}
}
}
}
else
{
rawData.resize((int)((int)raw->imgdata.sizes.iwidth * (int)raw->imgdata.sizes.iheight * sizeof(unsigned short)));
unsigned short* output = reinterpret_cast<unsigned short*>(rawData.data());
for (unsigned int row = 0 ; row < raw->imgdata.sizes.iheight ; ++row)
{
for (unsigned int col = 0 ; col < raw->imgdata.sizes.iwidth ; ++col)
{
*output = raw->imgdata.image[raw->imgdata.sizes.iwidth*row + col][raw->COLOR(row, col)];
output++;
}
}
}
raw->recycle();
delete raw;
d->setProgress(1.0);
return true;
}
bool DRawDecoder::decodeHalfRAWImage(const QString& filePath,
const DRawDecoderSettings& DRawDecoderSettings,
QByteArray& imageData,
int& width,
int& height,
int& rgbmax)
{
m_decoderSettings = DRawDecoderSettings;
m_decoderSettings.halfSizeColorImage = true;
return (d->loadFromLibraw(filePath, imageData, width, height, rgbmax));
}
bool DRawDecoder::decodeRAWImage(const QString& filePath,
const DRawDecoderSettings& DRawDecoderSettings,
QByteArray& imageData,
int& width,
int& height,
int& rgbmax)
{
m_decoderSettings = DRawDecoderSettings;
return (d->loadFromLibraw(filePath, imageData, width, height, rgbmax));
}
bool DRawDecoder::checkToCancelWaitingData()
{
return m_cancel;
}
void DRawDecoder::setWaitingDataProgress(double)
{
}
QString DRawDecoder::rawFiles()
{
return s_rawFileExtensions();
}
QStringList DRawDecoder::rawFilesList()
{
QString string = rawFiles();
return string.remove(QLatin1String("*.")).split(QLatin1Char(' '));
}
int DRawDecoder::rawFilesVersion()
{
return s_rawFileExtensionsVersion();
}
QStringList DRawDecoder::supportedCamera()
{
QStringList camera;
const char** const list = LibRaw::cameraList();
for (int i = 0 ; i < LibRaw::cameraCount() ; ++i)
{
camera.append(QString::fromUtf8(list[i]));
}
return camera;
}
QString DRawDecoder::librawVersion()
{
QString simplified = QString::fromLatin1(LIBRAW_VERSION_STR)
.section(QLatin1Char('-'), 0, -2);
if (simplified.isEmpty())
{
simplified = QString::fromLatin1(LIBRAW_VERSION_STR);
}
return simplified;
}
int DRawDecoder::librawUseGomp()
{
#ifdef LIBRAW_FORCE_OPENMP
return true;
#else
# ifdef LIBRAW_USE_OPENMP
return true;
# else
return false;
# endif
#endif
}
bool DRawDecoder::isRawFile(const QUrl& url)
{
QString rawFilesExt = rawFiles();
QFileInfo fileInfo(url.toLocalFile());
return (rawFilesExt.toUpper().contains(fileInfo.suffix().toUpper()));
}
} // namespace Digikam
#include "moc_drawdecoder.cpp"
|