loadingdocumentimpl.cpp 17.5 KB
Newer Older
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
3
/*
Gwenview: an image viewer
Aurélien Gâteau's avatar
Aurélien Gâteau committed
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Dirk Mueller's avatar
Dirk Mueller committed
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20
21

*/
// Self
David Edmundson's avatar
David Edmundson committed
22
#include "loadingdocumentimpl.h"
23

24
25
26
// STL
#include <memory>

27
28
29
// Exiv2
#include <exiv2/exiv2.hpp>

30
// Qt
31
#include <QBuffer>
32
#include <QByteArray>
33
#include <QFile>
34
35
#include <QFuture>
#include <QFutureWatcher>
36
#include <QImage>
37
#include <QImageReader>
38
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
39
#include <QtConcurrent>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
40
#include <QUrl>
41

42
// KF
Aurélien Gâteau's avatar
Aurélien Gâteau committed
43
#include <KIO/Job>
44
#include <kio/jobclasses.h>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
45
#include <KLocalizedString>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
46
#include <KProtocolInfo>
David Edmundson's avatar
David Edmundson committed
47
48

#ifdef KDCRAW_FOUND
49
#include <kdcraw/kdcraw.h>
David Edmundson's avatar
David Edmundson committed
50
#endif
51
52

// Local
53
#include "gwenview_lib_debug.h"
54
#include "animateddocumentloadedimpl.h"
55
#include "cms/cmsprofile.h"
56
57
#include "document.h"
#include "documentloadedimpl.h"
58
#include "emptydocumentimpl.h"
59
#include "exiv2imageloader.h"
60
#include "gvdebug.h"
61
#include "imageutils.h"
62
#include "jpegcontent.h"
63
#include "jpegdocumentloadedimpl.h"
64
#include "orientation.h"
65
#include "svgdocumentloadedimpl.h"
66
#include "urlutils.h"
67
#include "videodocumentloadedimpl.h"
68
#include "gwenviewconfig.h"
69

70
71
namespace Gwenview
{
72

73
74
#undef ENABLE_LOG
#undef LOG
Aurélien Gâteau's avatar
Aurélien Gâteau committed
75
//#define ENABLE_LOG
76
#ifdef ENABLE_LOG
Laurent Montel's avatar
Laurent Montel committed
77
#define LOG(x) //qCDebug(GWENVIEW_LIB_LOG) << x
78
79
80
81
#else
#define LOG(x) ;
#endif

82
const int HEADER_SIZE = 256;
83

Aurélien Gâteau's avatar
Aurélien Gâteau committed
84
85
struct LoadingDocumentImplPrivate
{
86
    LoadingDocumentImpl* q;
87
88
89
90
91
92
93
94
95
96
97
98
99
    QPointer<KIO::TransferJob> mTransferJob;
    QFuture<bool> mMetaInfoFuture;
    QFutureWatcher<bool> mMetaInfoFutureWatcher;
    QFuture<void> mImageDataFuture;
    QFutureWatcher<void> mImageDataFutureWatcher;

    // If != 0, this means we need to load an image at zoom =
    // 1/mImageDataInvertedZoom
    int mImageDataInvertedZoom;

    bool mMetaInfoLoaded;
    bool mAnimated;
    bool mDownSampledImageLoaded;
100
    QByteArray mFormatHint;
101
102
103
    QByteArray mData;
    QByteArray mFormat;
    QSize mImageSize;
104
    std::unique_ptr<Exiv2::Image> mExiv2Image;
105
    std::unique_ptr<JpegContent> mJpegContent;
106
    QImage mImage;
107
    Cms::Profile::Ptr mCmsProfile;
108
109
110
111
112
113
114
115
116

    /**
     * Determine kind of document and switch to an implementation if it is not
     * necessary to download more data.
     * @return true if switched to another implementation.
     */
    bool determineKind()
    {
        QString mimeType;
David Edmundson's avatar
David Edmundson committed
117
        const QUrl &url = q->document()->url();
118
        QMimeDatabase db;
119
120
121
122

        auto mime = db.mimeTypeForData(mData);
        if (mime.isDefault() && KProtocolInfo::determineMimetypeFromExtension(url.scheme())) {
            mime = db.mimeTypeForFileNameAndData(url.fileName(), mData);
123
        }
124
125
126

        mimeType = mime.name();

127
128
129
        MimeTypeUtils::Kind kind = MimeTypeUtils::mimeTypeKind(mimeType);
        LOG("mimeType:" << mimeType);
        LOG("kind:" << kind);
130
        q->setDocumentKind(kind);
131
132
133
134
135
136
137

        switch (kind) {
        case MimeTypeUtils::KIND_RASTER_IMAGE:
        case MimeTypeUtils::KIND_SVG_IMAGE:
            return false;

        case MimeTypeUtils::KIND_VIDEO:
138
            q->switchToImpl(new VideoDocumentLoadedImpl(q->document()));
139
140
141
            return true;

        default:
142
            q->setDocumentErrorString(
143
144
                i18nc("@info", "Gwenview cannot display documents of type %1.", mimeType)
            );
145
146
            emit q->loadingFailed();
            q->switchToImpl(new EmptyDocumentImpl(q->document()));
147
148
149
150
151
152
153
154
            return true;
        }
    }

    void startLoading()
    {
        Q_ASSERT(!mMetaInfoLoaded);

155
        switch (q->document()->kind()) {
156
        case MimeTypeUtils::KIND_RASTER_IMAGE:
157
158
159
160
161
162
163
164
            // The hint is used to:
            // - Speed up loadMetaInfo(): QImageReader will try to decode the
            //   image using plugins matching this format first.
            // - Avoid breakage: Because of a bug in Qt TGA image plugin, some
            //   PNG were incorrectly identified as PCX! See:
            //   https://bugs.kde.org/show_bug.cgi?id=289819
            //
            mFormatHint = q->document()->url().fileName()
165
                .section(QLatin1Char('.'), -1).toLocal8Bit().toLower();
166
167
168
169
170
            mMetaInfoFuture = QtConcurrent::run(this, &LoadingDocumentImplPrivate::loadMetaInfo);
            mMetaInfoFutureWatcher.setFuture(mMetaInfoFuture);
            break;

        case MimeTypeUtils::KIND_SVG_IMAGE:
171
            q->switchToImpl(new SvgDocumentLoadedImpl(q->document(), mData));
172
173
174
175
176
177
            break;

        case MimeTypeUtils::KIND_VIDEO:
            break;

        default:
Laurent Montel's avatar
Laurent Montel committed
178
            qCWarning(GWENVIEW_LIB_LOG) << "We should not reach this point!";
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
            break;
        }
    }

    void startImageDataLoading()
    {
        LOG("");
        Q_ASSERT(mMetaInfoLoaded);
        Q_ASSERT(mImageDataInvertedZoom != 0);
        Q_ASSERT(!mImageDataFuture.isRunning());
        mImageDataFuture = QtConcurrent::run(this, &LoadingDocumentImplPrivate::loadImageData);
        mImageDataFutureWatcher.setFuture(mImageDataFuture);
    }

    bool loadMetaInfo()
    {
Aurélien Gâteau's avatar
Aurélien Gâteau committed
195
        LOG("mFormatHint" << mFormatHint);
196
197
198
        QBuffer buffer;
        buffer.setBuffer(&mData);
        buffer.open(QIODevice::ReadOnly);
Aurélien Gâteau's avatar
Aurélien Gâteau committed
199

200
201
202
203
204
        Exiv2ImageLoader loader;
        if (loader.load(mData)) {
            mExiv2Image = loader.popImage();
        }

David Edmundson's avatar
David Edmundson committed
205
#ifdef KDCRAW_FOUND
206
        if (KDcrawIface::KDcraw::rawFilesList().contains(QString::fromLatin1(mFormatHint))) {
Aurélien Gâteau's avatar
Aurélien Gâteau committed
207
208
209
210
211
212
213
214
215
            QByteArray previewData;

            // if the image is in format supported by dcraw, fetch its embedded preview
            mJpegContent.reset(new JpegContent());

            // use KDcraw for getting the embedded preview
            // KDcraw functionality cloned locally (temp. solution)
            bool ret = KDcrawIface::KDcraw::loadEmbeddedPreview(previewData, buffer);

216
217
218
            if (!ret) {
                // if the embedded preview loading failed, load half preview instead.
                // That's slower but it works even for images containing
Aurélien Gâteau's avatar
Aurélien Gâteau committed
219
220
                // small (160x120px) or none embedded preview.
                if (!KDcrawIface::KDcraw::loadHalfPreview(previewData, buffer)) {
Laurent Montel's avatar
Laurent Montel committed
221
                    qCWarning(GWENVIEW_LIB_LOG) << "unable to get half preview for " << q->document()->url().fileName();
Aurélien Gâteau's avatar
Aurélien Gâteau committed
222
223
                    return false;
                }
224
            }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
225
226
227
228
229
230
231
232
233

            buffer.close();

            // now it's safe to replace mData with the jpeg data
            mData = previewData;

            // need to fill mFormat so gwenview can tell the type when trying to save
            mFormat = mFormatHint;
        } else {
David Edmundson's avatar
David Edmundson committed
234
235
236
#else
{
#endif
Aurélien Gâteau's avatar
Aurélien Gâteau committed
237
238
239
            QImageReader reader(&buffer, mFormatHint);
            mImageSize = reader.size();

240
            if (!reader.canRead()) {
Laurent Montel's avatar
Laurent Montel committed
241
                qCWarning(GWENVIEW_LIB_LOG) << "QImageReader::read() using format hint" << mFormatHint << "failed:" << reader.errorString();
Aurélien Gâteau's avatar
Aurélien Gâteau committed
242
                if (buffer.pos() != 0) {
Laurent Montel's avatar
Laurent Montel committed
243
                    qCWarning(GWENVIEW_LIB_LOG) << "A bad Qt image decoder moved the buffer to" << buffer.pos() << "in a call to canRead()! Rewinding.";
Aurélien Gâteau's avatar
Aurélien Gâteau committed
244
245
246
247
248
249
                    buffer.seek(0);
                }
                reader.setFormat(QByteArray());
                // Set buffer again, otherwise QImageReader won't restart from scratch
                reader.setDevice(&buffer);
                if (!reader.canRead()) {
Laurent Montel's avatar
Laurent Montel committed
250
                    qCWarning(GWENVIEW_LIB_LOG) << "QImageReader::read() without format hint failed:" << reader.errorString();
Aurélien Gâteau's avatar
Aurélien Gâteau committed
251
252
                    return false;
                }
Laurent Montel's avatar
Laurent Montel committed
253
                qCWarning(GWENVIEW_LIB_LOG) << "Image format is actually" << reader.format() << "not" << mFormatHint;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
254
255
256
257
258
259
260
261
            }

            mFormat = reader.format();

            if (mFormat == "jpg") {
                // if mFormatHint was "jpg", then mFormat is "jpg", but the rest of
                // Gwenview code assumes JPEG images have "jpeg" format.
                mFormat = "jpeg";
262
            }
263
        }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
264

Aurélien Gâteau's avatar
Aurélien Gâteau committed
265
        LOG("mFormat" << mFormat);
266
        GV_RETURN_VALUE_IF_FAIL(!mFormat.isEmpty(), false);
267
268
269

        if (mFormat == "jpeg" && mExiv2Image.get()) {
            mJpegContent.reset(new JpegContent());
Aurélien Gâteau's avatar
Aurélien Gâteau committed
270
271
272
273
274
        }

        if (mJpegContent.get()) {
            if (!mJpegContent->loadFromData(mData, mExiv2Image.get()) &&
                !mJpegContent->loadFromData(mData)) {
Laurent Montel's avatar
Laurent Montel committed
275
                qCWarning(GWENVIEW_LIB_LOG) << "Unable to use preview of " << q->document()->url().fileName();
276
277
278
279
280
                return false;
            }
            // Use the size from JpegContent, as its correctly transposed if the
            // image has been rotated
            mImageSize = mJpegContent->size();
281
282

            mCmsProfile = Cms::Profile::loadFromExiv2Image(mExiv2Image.get());
Aurélien Gâteau's avatar
Aurélien Gâteau committed
283

284
        }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
285

286
287
        LOG("mImageSize" << mImageSize);

288
289
290
291
        if (!mCmsProfile) {
            mCmsProfile = Cms::Profile::loadFromImageData(mData, mFormat);
        }

292
293
294
295
296
297
298
299
        return true;
    }

    void loadImageData()
    {
        QBuffer buffer;
        buffer.setBuffer(&mData);
        buffer.open(QIODevice::ReadOnly);
300
        QImageReader reader(&buffer, mFormat);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

        LOG("mImageDataInvertedZoom=" << mImageDataInvertedZoom);
        if (mImageSize.isValid()
                && mImageDataInvertedZoom != 1
                && reader.supportsOption(QImageIOHandler::ScaledSize)
           ) {
            // Do not use mImageSize here: QImageReader needs a non-transposed
            // image size
            QSize size = reader.size() / mImageDataInvertedZoom;
            if (!size.isEmpty()) {
                LOG("Setting scaled size to" << size);
                reader.setScaledSize(size);
            } else {
                LOG("Not setting scaled size as it is empty" << size);
            }
        }

318
319
320
321
        if (GwenviewConfig::applyExifOrientation()) {
            reader.setAutoTransform(true);
        }

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
        bool ok = reader.read(&mImage);
        if (!ok) {
            LOG("QImageReader::read() failed");
            return;
        }

        if (reader.supportsAnimation()
                && reader.nextImageDelay() > 0 // Assume delay == 0 <=> only one frame
           ) {
            /*
             * QImageReader is not really helpful to detect animated gif:
             * - QImageReader::imageCount() returns 0
             * - QImageReader::nextImageDelay() may return something > 0 if the
             *   image consists of only one frame but includes a "Graphic
             *   Control Extension" (usually only present if we have an
             *   animation) (Bug #185523)
             *
             * Decoding the next frame is the only reliable way I found to
             * detect an animated gif
             */
            LOG("May be an animated image. delay:" << reader.nextImageDelay());
            QImage nextImage;
            if (reader.read(&nextImage)) {
                LOG("Really an animated image (more than one frame)");
                mAnimated = true;
            } else {
Laurent Montel's avatar
Laurent Montel committed
348
                qCWarning(GWENVIEW_LIB_LOG) << q->document()->url() << "is not really an animated image (only one frame)";
349
350
351
            }
        }
    }
352
353
};

354
LoadingDocumentImpl::LoadingDocumentImpl(Document* document)
355
: AbstractDocumentImpl(document)
356
357
, d(new LoadingDocumentImplPrivate)
{
358
    d->q = this;
359
360
361
362
363
    d->mMetaInfoLoaded = false;
    d->mAnimated = false;
    d->mDownSampledImageLoaded = false;
    d->mImageDataInvertedZoom = 0;

Laurent Montel's avatar
Laurent Montel committed
364
365
    connect(&d->mMetaInfoFutureWatcher, &QFutureWatcherBase::finished,
            this, &LoadingDocumentImpl::slotMetaInfoLoaded);
366

Laurent Montel's avatar
Laurent Montel committed
367
368
    connect(&d->mImageDataFutureWatcher, &QFutureWatcherBase::finished,
            this, &LoadingDocumentImpl::slotImageLoaded);
369
370
}

371
372
373
374
375
376
LoadingDocumentImpl::~LoadingDocumentImpl()
{
    LOG("");
    // Disconnect watchers to make sure they do not trigger further work
    d->mMetaInfoFutureWatcher.disconnect();
    d->mImageDataFutureWatcher.disconnect();
377

378
379
    d->mMetaInfoFutureWatcher.waitForFinished();
    d->mImageDataFutureWatcher.waitForFinished();
380

381
382
383
384
    if (d->mTransferJob) {
        d->mTransferJob->kill();
    }
    delete d;
385
386
}

387
388
void LoadingDocumentImpl::init()
{
David Edmundson's avatar
David Edmundson committed
389
    QUrl url = document()->url();
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

    if (UrlUtils::urlIsFastLocalFile(url)) {
        // Load file content directly
        QFile file(url.toLocalFile());
        if (!file.open(QIODevice::ReadOnly)) {
            setDocumentErrorString(i18nc("@info", "Could not open file %1", url.toLocalFile()));
            emit loadingFailed();
            switchToImpl(new EmptyDocumentImpl(document()));
            return;
        }
        d->mData = file.read(HEADER_SIZE);
        if (d->determineKind()) {
            return;
        }
        d->mData += file.readAll();
        d->startLoading();
    } else {
        // Transfer file via KIO
408
        d->mTransferJob = KIO::get(document()->url(), KIO::NoReload, KIO::HideProgressInfo);
Laurent Montel's avatar
Laurent Montel committed
409
410
411
412
        connect(d->mTransferJob.data(), &KIO::TransferJob::data,
                this, &LoadingDocumentImpl::slotDataReceived);
        connect(d->mTransferJob.data(), &KJob::result,
                this, &LoadingDocumentImpl::slotTransferFinished);
413
414
        d->mTransferJob->start();
    }
415
416
}

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
void LoadingDocumentImpl::loadImage(int invertedZoom)
{
    if (d->mImageDataInvertedZoom == invertedZoom) {
        LOG("Already loading an image at invertedZoom=" << invertedZoom);
        return;
    }
    if (d->mImageDataInvertedZoom == 1) {
        LOG("Ignoring request: we are loading a full image");
        return;
    }
    d->mImageDataFutureWatcher.waitForFinished();
    d->mImageDataInvertedZoom = invertedZoom;

    if (d->mMetaInfoLoaded) {
        // Do not test on mMetaInfoFuture.isRunning() here: it might not have
        // started if we are downloading the image from a remote url
        d->startImageDataLoading();
    }
435
436
}

437
438
439
440
441
442
443
444
445
void LoadingDocumentImpl::slotDataReceived(KIO::Job* job, const QByteArray& chunk)
{
    d->mData.append(chunk);
    if (document()->kind() == MimeTypeUtils::KIND_UNKNOWN && d->mData.length() >= HEADER_SIZE) {
        if (d->determineKind()) {
            job->kill();
            return;
        }
    }
446
447
}

448
449
450
451
452
453
454
void LoadingDocumentImpl::slotTransferFinished(KJob* job)
{
    if (job->error()) {
        setDocumentErrorString(job->errorString());
        emit loadingFailed();
        switchToImpl(new EmptyDocumentImpl(document()));
        return;
455
456
457
458
459
460
    } else if (document()->kind() == MimeTypeUtils::KIND_UNKNOWN) {
        // Transfer finished. If the mime type is still unknown (e.g. for files < HEADER_SIZE)
        // determine the kind again.
        if (d->determineKind()) {
            return;
        }
461
462
    }
    d->startLoading();
463
464
}

465
466
467
bool LoadingDocumentImpl::isEditable() const
{
    return d->mDownSampledImageLoaded;
468
469
}

470
471
472
473
474
475
476
477
478
479
480
Document::LoadingState LoadingDocumentImpl::loadingState() const
{
    if (!document()->image().isNull()) {
        return Document::Loaded;
    } else if (d->mMetaInfoLoaded) {
        return Document::MetaInfoLoaded;
    } else if (document()->kind() != MimeTypeUtils::KIND_UNKNOWN) {
        return Document::KindDetermined;
    } else {
        return Document::Loading;
    }
481
482
}

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
void LoadingDocumentImpl::slotMetaInfoLoaded()
{
    LOG("");
    Q_ASSERT(!d->mMetaInfoFuture.isRunning());
    if (!d->mMetaInfoFuture.result()) {
        setDocumentErrorString(
            i18nc("@info", "Loading meta information failed.")
        );
        emit loadingFailed();
        switchToImpl(new EmptyDocumentImpl(document()));
        return;
    }

    setDocumentFormat(d->mFormat);
    setDocumentImageSize(d->mImageSize);
498
    setDocumentExiv2Image(std::move(d->mExiv2Image));
499
    setDocumentCmsProfile(d->mCmsProfile);
500
501
502
503
504
505
506
507
508
509

    d->mMetaInfoLoaded = true;
    emit metaInfoLoaded();

    // Start image loading if necessary
    // We test if mImageDataFuture is not already running because code connected to
    // metaInfoLoaded() signal could have called loadImage()
    if (!d->mImageDataFuture.isRunning() && d->mImageDataInvertedZoom != 0) {
        d->startImageDataLoading();
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
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
void LoadingDocumentImpl::slotImageLoaded()
{
    LOG("");
    if (d->mImage.isNull()) {
        setDocumentErrorString(
            i18nc("@info", "Loading image failed.")
        );
        emit loadingFailed();
        switchToImpl(new EmptyDocumentImpl(document()));
        return;
    }

    if (d->mAnimated) {
        if (d->mImage.size() == d->mImageSize) {
            // We already decoded the first frame at the right size, let's show
            // it
            setDocumentImage(d->mImage);
        }

        switchToImpl(new AnimatedDocumentLoadedImpl(
                         document(),
                         d->mData));

        return;
    }

    if (d->mImageDataInvertedZoom != 1 && d->mImage.size() != d->mImageSize) {
        LOG("Loaded a down sampled image");
        d->mDownSampledImageLoaded = true;
        // We loaded a down sampled image
        setDocumentDownSampledImage(d->mImage, d->mImageDataInvertedZoom);
        return;
    }

    LOG("Loaded a full image");
    setDocumentImage(d->mImage);
    DocumentLoadedImpl* impl;
    if (d->mJpegContent.get()) {
        impl = new JpegDocumentLoadedImpl(
            document(),
            d->mJpegContent.release());
    } else {
        impl = new DocumentLoadedImpl(
            document(),
            d->mData);
    }
    switchToImpl(impl);
559
560
561
}

} // namespace