~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/threadimageio/previewtask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-06-18 21:58:46 UTC
  • mfrom: (1.2.29 upstream) (3.2.13 sid)
  • Revision ID: james.westby@ubuntu.com-20100618215846-c95bk5aoysmu786d
Tags: 2:1.3.0-0ubuntu1
* Merge with Debian unstable, remaining changes:
  - Export .pot name and copy to plugins in debian/rules
  - Enable Nepomuk support in Digikam
    - Add shared-desktop-ontologies to build depends
* New upstream release:
  - Remove digikam-1.2.0-kde232628.patch fixed by upstream
  - Add mysql-server-5.1 to buil-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#include <kdebug.h>
41
41
 
 
42
// libkexiv2 includes
 
43
 
 
44
#include <libkexiv2/version.h>
 
45
#if KEXIV2_VERSION >= 0x010000
 
46
#include <libkexiv2/kexiv2previews.h>
 
47
#endif
 
48
 
42
49
// LibKDcraw includes
43
50
 
44
51
#include <libkdcraw/kdcraw.h>
166
173
 
167
174
    QImage qimage;
168
175
    bool fromEmbeddedPreview = false;
 
176
    QSize originalSize;
169
177
 
170
178
    // -- Get the image preview --------------------------------
171
179
 
172
180
    if (size)
173
181
    {
174
182
        // First the QImage-dependent loading methods
 
183
 
 
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())
 
189
        {
 
190
            // require at least half preview size
 
191
            if (qMax(previews.width(), previews.height()) >= size / 2)
 
192
            {
 
193
                qimage = previews.image();
 
194
                if (!qimage.isNull())
 
195
                    fromEmbeddedPreview = true;
 
196
            }
 
197
        }
 
198
        #else
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))
177
201
        {
178
202
            // Discard if too small
179
203
            if (qMax(qimage.width(), qimage.height()) < size / 2)
181
205
            else
182
206
                fromEmbeddedPreview = true;
183
207
        }
 
208
        #endif
184
209
 
185
 
        if (qimage.isNull())
 
210
        if (qimage.isNull() && continueQuery())
186
211
        {
187
212
            //TODO: Use DImg based loader instead?
188
213
            KDcrawIface::KDcraw::loadHalfPreview(qimage, m_loadingDescription.filePath);
189
214
        }
190
215
 
191
216
        // Try to extract Exif/IPTC preview.
192
 
        if (qimage.isNull())
 
217
        if (qimage.isNull() && continueQuery())
193
218
        {
194
219
            loadImagePreview(qimage, m_loadingDescription.filePath);
195
220
        }
196
221
 
197
 
        if (!qimage.isNull())
 
222
        if (!qimage.isNull() && continueQuery())
198
223
        {
199
224
            // convert from QImage
200
225
            m_img = DImg(qimage);
 
226
 
 
227
            DImg::FORMAT format = DImg::fileFormat(m_loadingDescription.filePath);
 
228
            m_img.setAttribute("detectedFileFormat", format);
 
229
            m_img.setAttribute("originalFilePath", m_loadingDescription.filePath);
 
230
 
 
231
            DMetadata metadata(m_loadingDescription.filePath);
 
232
            #if KEXIV2_VERSION >= 0x010100
 
233
            m_img.setAttribute("originalSize", metadata.getPixelSize());
 
234
            #endif
201
235
            // mark as embedded preview (for Exif rotation)
202
236
            if (fromEmbeddedPreview)
203
237
            {
205
239
 
206
240
                // If we loaded the embedded preview, the Exif of the RAW indicates
207
241
                // the color space of the preview (see bug 195950 for NEF files)
208
 
                DMetadata metadata(m_loadingDescription.filePath);
209
242
                m_img.setIccProfile(metadata.getIccProfile());
210
243
            }
211
244
            // free memory
213
246
        }
214
247
 
215
248
        // DImg-dependent loading methods
216
 
        if (m_img.isNull())
 
249
        if (m_img.isNull() && continueQuery())
217
250
        {
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);
221
254
        }
222
255
 
223
 
        if (m_img.isNull())
 
256
        if (m_img.isNull() && continueQuery())
224
257
        {
225
258
            kWarning() << "Cannot extract preview for " << m_loadingDescription.filePath;
226
259
        }
227
260
    }
228
261
    else
229
262
    {
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))
234
265
        {
 
266
            #if KEXIV2_VERSION >= 0x010000
 
267
            // Check if full-size preview is available
 
268
            {
 
269
                KExiv2Iface::KExiv2Previews previews(m_loadingDescription.filePath);
 
270
                // Only check the first and largest preview
 
271
                if (!previews.isEmpty() && continueQuery())
 
272
                {
 
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)
 
277
                    {
 
278
                        qimage = previews.image();
 
279
                        if (!qimage.isNull())
 
280
                            fromEmbeddedPreview = true;
 
281
                    }
 
282
                }
 
283
            }
 
284
            #else
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))
237
288
            {
238
289
                // discard if smaller than half preview
239
290
                if (qMax(qimage.width(), qimage.height()) < dcrawIdentify.imageSize.width() / 2)
241
292
                else
242
293
                    fromEmbeddedPreview = true;
243
294
            }
 
295
            #endif
244
296
 
245
 
            if (qimage.isNull())
 
297
            if (qimage.isNull() && continueQuery())
246
298
            {
247
299
                KDcrawIface::KDcraw::loadHalfPreview(qimage, m_loadingDescription.filePath);
248
300
            }
249
301
        }
250
302
 
251
 
        if (!qimage.isNull())
 
303
        if (!qimage.isNull() && continueQuery())
252
304
        {
253
305
            // convert from QImage
254
306
            m_img = DImg(qimage);
 
307
 
 
308
            DImg::FORMAT format = DImg::fileFormat(m_loadingDescription.filePath);
 
309
            m_img.setAttribute("detectedFileFormat", format);
 
310
            m_img.setAttribute("originalFilePath", m_loadingDescription.filePath);
 
311
 
 
312
            DMetadata metadata(m_loadingDescription.filePath);
 
313
            #if KEXIV2_VERSION >= 0x010100
 
314
            m_img.setAttribute("originalSize", metadata.getPixelSize());
 
315
            #endif
255
316
            // mark as embedded preview (for Exif rotation)
256
317
            if (fromEmbeddedPreview)
257
318
            {
259
320
 
260
321
                // If we loaded the embedded preview, the Exif of the RAW indicates
261
322
                // the color space of the preview (see bug 195950 for NEF files)
262
 
                DMetadata metadata(m_loadingDescription.filePath);
263
323
                m_img.setIccProfile(metadata.getIccProfile());
264
324
            }
265
325
            // free memory
267
327
        }
268
328
 
269
329
        // DImg-dependent loading methods
270
 
        if (m_img.isNull())
 
330
        if (m_img.isNull() && continueQuery())
271
331
        {
272
332
            m_img.load(m_loadingDescription.filePath, this, m_loadingDescription.rawDecodingSettings);
 
333
 
 
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)
 
337
            {
 
338
                LoadingCache::CacheLock lock(cache);
 
339
                LoadingDescription fullDescription(m_loadingDescription.filePath);
 
340
                cache->putImage(fullDescription.cacheKey(), new DImg(m_img.copy()), m_loadingDescription.filePath);
 
341
            }
273
342
        }
274
343
 
275
 
        if (m_img.isNull())
 
344
        if (m_img.isNull() && continueQuery())
276
345
        {
277
346
            kWarning() << "Cannot extract preview for " << m_loadingDescription.filePath;
278
347
        }
279
348
    }
280
349
 
281
 
    m_img.convertToEightBit();
282
 
 
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))
288
 
    {
289
 
        scaledSize.scale(size, size, Qt::KeepAspectRatio);
290
 
        m_img = m_img.smoothScale(scaledSize.width(), scaledSize.height());
291
 
    }
292
 
 
293
 
    // Scale if hinted, Store previews rotated in the cache (?)
294
 
    if (m_loadingDescription.previewParameters.exifRotate)
295
 
        LoadSaveThread::exifRotate(m_img, m_loadingDescription.filePath);
296
 
 
297
 
    // For previews, we put the image post processed in the cache
298
 
    postProcess();
 
350
    if (continueQuery())
 
351
    {
 
352
        m_img.convertToEightBit();
 
353
 
 
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))
 
359
        {
 
360
            scaledSize.scale(size, size, Qt::KeepAspectRatio);
 
361
            m_img = m_img.smoothScale(scaledSize.width(), scaledSize.height());
 
362
        }
 
363
 
 
364
        // Scale if hinted, Store previews rotated in the cache (?)
 
365
        if (m_loadingDescription.previewParameters.exifRotate)
 
366
            LoadSaveThread::exifRotate(m_img, m_loadingDescription.filePath);
 
367
 
 
368
        // For previews, we put the image post processed in the cache
 
369
        postProcess();
 
370
    }
 
371
    else
 
372
    {
 
373
        m_img = DImg();
 
374
    }
299
375
 
300
376
    {
301
377
        LoadingCache::CacheLock lock(cache);
365
441
    DMetadata metadata(path);
366
442
    if (metadata.getImagePreview(image))
367
443
    {
368
 
        kDebug() << "Use Exif/IPTC preview extraction. Size of image: "
 
444
        kDebug(50003) << "Use Exif/IPTC preview extraction. Size of image: "
369
445
                      << image.width() << "x" << image.height();
370
446
        return true;
371
447
    }