168
175
bool fromEmbeddedPreview = false;
170
178
// -- Get the image preview --------------------------------
174
182
// First the QImage-dependent loading methods
184
// check embedded previews
185
#if KEXIV2_VERSION >= 0x010000
186
KExiv2Iface::KExiv2Previews previews(m_loadingDescription.filePath);
187
// Only check the first and largest preview
188
if (!previews.isEmpty() && continueQuery())
190
// require at least half preview size
191
if (qMax(previews.width(), previews.height()) >= size / 2)
193
qimage = previews.image();
194
if (!qimage.isNull())
195
fromEmbeddedPreview = true;
175
199
// Trying to load with dcraw: RAW files.
176
if (KDcrawIface::KDcraw::loadEmbeddedPreview(qimage, m_loadingDescription.filePath))
200
if (continueQuery() && KDcrawIface::KDcraw::loadEmbeddedPreview(qimage, m_loadingDescription.filePath))
178
202
// Discard if too small
179
203
if (qMax(qimage.width(), qimage.height()) < size / 2)
182
206
fromEmbeddedPreview = true;
210
if (qimage.isNull() && continueQuery())
187
212
//TODO: Use DImg based loader instead?
188
213
KDcrawIface::KDcraw::loadHalfPreview(qimage, m_loadingDescription.filePath);
191
216
// Try to extract Exif/IPTC preview.
217
if (qimage.isNull() && continueQuery())
194
219
loadImagePreview(qimage, m_loadingDescription.filePath);
197
if (!qimage.isNull())
222
if (!qimage.isNull() && continueQuery())
199
224
// convert from QImage
200
225
m_img = DImg(qimage);
227
DImg::FORMAT format = DImg::fileFormat(m_loadingDescription.filePath);
228
m_img.setAttribute("detectedFileFormat", format);
229
m_img.setAttribute("originalFilePath", m_loadingDescription.filePath);
231
DMetadata metadata(m_loadingDescription.filePath);
232
#if KEXIV2_VERSION >= 0x010100
233
m_img.setAttribute("originalSize", metadata.getPixelSize());
201
235
// mark as embedded preview (for Exif rotation)
202
236
if (fromEmbeddedPreview)
215
248
// DImg-dependent loading methods
249
if (m_img.isNull() && continueQuery())
218
251
// Set a hint to try to load a JPEG or PGF with the fast scale-before-decoding method
219
252
m_img.setAttribute("scaledLoadingSize", size);
220
253
m_img.load(m_loadingDescription.filePath, this, m_loadingDescription.rawDecodingSettings);
256
if (m_img.isNull() && continueQuery())
225
258
kWarning() << "Cannot extract preview for " << m_loadingDescription.filePath;
230
//TODO: Use Exiv2's PreviewManager
231
263
KDcrawIface::DcrawInfoContainer dcrawIdentify;
232
if (KDcrawIface::KDcraw::rawFileIdentify(dcrawIdentify, m_loadingDescription.filePath)
233
&& dcrawIdentify.isDecodable)
264
if (KDcrawIface::KDcraw::rawFileIdentify(dcrawIdentify, m_loadingDescription.filePath))
266
#if KEXIV2_VERSION >= 0x010000
267
// Check if full-size preview is available
269
KExiv2Iface::KExiv2Previews previews(m_loadingDescription.filePath);
270
// Only check the first and largest preview
271
if (!previews.isEmpty() && continueQuery())
273
// discard if smaller than half preview
274
int acceptableWidth = lround(dcrawIdentify.imageSize.width() * 0.5);
275
int acceptableHeight = lround(dcrawIdentify.imageSize.height() * 0.5);
276
if (previews.width() >= acceptableWidth && previews.height() >= acceptableHeight)
278
qimage = previews.image();
279
if (!qimage.isNull())
280
fromEmbeddedPreview = true;
235
285
// Trying to load with dcraw: RAW files.
236
if (KDcrawIface::KDcraw::loadEmbeddedPreview(qimage, m_loadingDescription.filePath))
286
if (qimage.isNull() && continueQuery() && dcrawIdentify.isDecodable &&
287
KDcrawIface::KDcraw::loadEmbeddedPreview(qimage, m_loadingDescription.filePath))
238
289
// discard if smaller than half preview
239
290
if (qMax(qimage.width(), qimage.height()) < dcrawIdentify.imageSize.width() / 2)
242
293
fromEmbeddedPreview = true;
297
if (qimage.isNull() && continueQuery())
247
299
KDcrawIface::KDcraw::loadHalfPreview(qimage, m_loadingDescription.filePath);
251
if (!qimage.isNull())
303
if (!qimage.isNull() && continueQuery())
253
305
// convert from QImage
254
306
m_img = DImg(qimage);
308
DImg::FORMAT format = DImg::fileFormat(m_loadingDescription.filePath);
309
m_img.setAttribute("detectedFileFormat", format);
310
m_img.setAttribute("originalFilePath", m_loadingDescription.filePath);
312
DMetadata metadata(m_loadingDescription.filePath);
313
#if KEXIV2_VERSION >= 0x010100
314
m_img.setAttribute("originalSize", metadata.getPixelSize());
255
316
// mark as embedded preview (for Exif rotation)
256
317
if (fromEmbeddedPreview)
269
329
// DImg-dependent loading methods
330
if (m_img.isNull() && continueQuery())
272
332
m_img.load(m_loadingDescription.filePath, this, m_loadingDescription.rawDecodingSettings);
334
// Now that we did a full load of the image, consider putting it in the cache
335
// but not for RAWs, there are so many cases to consider
336
if (!m_img.isNull() && m_img.detectedFormat() != DImg::RAW)
338
LoadingCache::CacheLock lock(cache);
339
LoadingDescription fullDescription(m_loadingDescription.filePath);
340
cache->putImage(fullDescription.cacheKey(), new DImg(m_img.copy()), m_loadingDescription.filePath);
344
if (m_img.isNull() && continueQuery())
277
346
kWarning() << "Cannot extract preview for " << m_loadingDescription.filePath;
281
m_img.convertToEightBit();
283
// Reduce size of image:
284
// - only scale down if size is considerably larger
285
// - only scale down, do not scale up
286
QSize scaledSize = m_img.size();
287
if (needToScale(scaledSize, size))
289
scaledSize.scale(size, size, Qt::KeepAspectRatio);
290
m_img = m_img.smoothScale(scaledSize.width(), scaledSize.height());
293
// Scale if hinted, Store previews rotated in the cache (?)
294
if (m_loadingDescription.previewParameters.exifRotate)
295
LoadSaveThread::exifRotate(m_img, m_loadingDescription.filePath);
297
// For previews, we put the image post processed in the cache
352
m_img.convertToEightBit();
354
// Reduce size of image:
355
// - only scale down if size is considerably larger
356
// - only scale down, do not scale up
357
QSize scaledSize = m_img.size();
358
if (needToScale(scaledSize, size))
360
scaledSize.scale(size, size, Qt::KeepAspectRatio);
361
m_img = m_img.smoothScale(scaledSize.width(), scaledSize.height());
364
// Scale if hinted, Store previews rotated in the cache (?)
365
if (m_loadingDescription.previewParameters.exifRotate)
366
LoadSaveThread::exifRotate(m_img, m_loadingDescription.filePath);
368
// For previews, we put the image post processed in the cache
301
377
LoadingCache::CacheLock lock(cache);