File: kritacreator.cpp

package info (click to toggle)
kio-extras 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,364 kB
  • sloc: cpp: 25,948; ansic: 4,443; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (108 lines) | stat: -rw-r--r-- 3,215 bytes parent folder | download
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
/*  This file is part of the Calligra project.
    Copyright 2015 Friedrich W. H. Kossebau <kossebau@kde.org>

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

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kritacreator.h"

#include <kzip.h>

// Qt
#include <QPainter>
#include <QImage>
#include <QIODevice>
#include <QFile>

extern "C"
{
    Q_DECL_EXPORT ThumbCreator *new_creator()
    {
        return new KritaCreator;
    }
}

KritaCreator::KritaCreator()
{
}

KritaCreator::~KritaCreator()
{
}

bool KritaCreator::create(const QString &path, int width, int height, QImage &image)
{
    // for now just rely on the rendered data inside the file,
    // do not load Krita code for rendering ourselves, as that currently (2.9)
    // means loading all plugins, resources etc.
    KZip zip(path);
    if (!zip.open(QIODevice::ReadOnly)) {
        return false;
    }

    QImage thumbnail;
    bool biggerSizeNeeded = true;

    // first check if normal thumbnail is good enough
    // ORA thumbnail?
    const KArchiveEntry *entry = zip.directory()->entry(QLatin1String("Thumbnails/thumbnail.png"));
    // KRA thumbnail
    if (!entry || !entry->isFile()) {
        entry = zip.directory()->entry(QLatin1String("preview.png"));
    }

    if (!entry || !entry->isFile()) {
        return false;
    }

    const KZipFileEntry* fileZipEntry = static_cast<const KZipFileEntry*>(entry);
    bool thumbLoaded = thumbnail.loadFromData(fileZipEntry->data(), "PNG");
    if (thumbLoaded) {
        biggerSizeNeeded = (thumbnail.width() < width || thumbnail.height() < height);
    }

    if (biggerSizeNeeded || !thumbLoaded) {
        entry = zip.directory()->entry(QLatin1String("mergedimage.png"));
    }

    if (entry && entry->isFile()) {
        fileZipEntry = static_cast<const KZipFileEntry*>(entry);
        thumbLoaded = thumbnail.loadFromData(fileZipEntry->data(), "PNG");
        if (thumbLoaded) {
            thumbnail = thumbnail.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        } else {
            return false;
        }
    }

    if (!thumbnail.isNull()) {
        // transparent areas put a white background behind the thumbnail
        // (or better checkerboard?)
        image = QImage(thumbnail.size(), QImage::Format_RGB32);
        image.fill(QColor(Qt::white).rgb());
        QPainter p(&image);
        p.drawImage(QPoint(0, 0), thumbnail);

        return true;
    }

    return false;
}

ThumbCreator::Flags KritaCreator::flags() const
{
    return None;
}