ora_converter.cc 3.04 KB
Newer Older
Halla Rempt's avatar
Halla Rempt committed
1 2 3
/*
 *  Copyright (c) 2007 Cyrille Berger <cberger@cberger.net>
 *
4
 *  This library is free software; you can redistribute it and/or modify
Halla Rempt's avatar
Halla Rempt committed
5
 *  it under the terms of the GNU Lesser General Public License as published by
6
 *  the Free Software Foundation; version 2.1 of the License.
Halla Rempt's avatar
Halla Rempt committed
7
 *
8
 *  This library is distributed in the hope that it will be useful,
Halla Rempt's avatar
Halla Rempt committed
9 10 11 12 13 14 15 16 17 18 19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "ora_converter.h"

20
#include <QApplication>
Halla Rempt's avatar
Halla Rempt committed
21

22
#include <QFileInfo>
Halla Rempt's avatar
Halla Rempt committed
23 24

#include <KoStore.h>
Sven Langkamp's avatar
Sven Langkamp committed
25
#include <KoStoreDevice.h>
Halla Rempt's avatar
Halla Rempt committed
26
#include <KoColorSpaceRegistry.h>
27
#include <KisDocument.h>
Halla Rempt's avatar
Halla Rempt committed
28 29 30 31 32
#include <kis_group_layer.h>
#include <kis_image.h>
#include <kis_open_raster_stack_load_visitor.h>
#include <kis_open_raster_stack_save_visitor.h>
#include <kis_paint_layer.h>
Halla Rempt's avatar
Halla Rempt committed
33
#include "kis_png_converter.h"
Halla Rempt's avatar
Halla Rempt committed
34 35 36
#include "ora_load_context.h"
#include "ora_save_context.h"

37
OraConverter::OraConverter(KisDocument *doc)
38 39
    : m_doc(doc)
    , m_stop(false)
Halla Rempt's avatar
Halla Rempt committed
40 41 42 43 44 45 46
{
}

OraConverter::~OraConverter()
{
}

47
KisImageBuilder_Result OraConverter::buildImage(QIODevice *io)
Halla Rempt's avatar
Halla Rempt committed
48
{
49
    KoStore* store = KoStore::createStore(io, KoStore::Read, "image/openraster", KoStore::Zip);
Halla Rempt's avatar
Halla Rempt committed
50
    if (!store) {
51
        delete store;
Halla Rempt's avatar
Halla Rempt committed
52 53
        return KisImageBuilder_RESULT_FAILURE;
    }
54

Halla Rempt's avatar
Halla Rempt committed
55
    OraLoadContext olc(store);
Halla Rempt's avatar
Halla Rempt committed
56
    KisOpenRasterStackLoadVisitor orslv(m_doc->createUndoStore(), &olc);
Halla Rempt's avatar
Halla Rempt committed
57
    orslv.loadImage();
Halla Rempt's avatar
Halla Rempt committed
58
    m_image = orslv.image();
59
    m_activeNodes = orslv.activeNodes();
Halla Rempt's avatar
Halla Rempt committed
60
    delete store;
Halla Rempt's avatar
Halla Rempt committed
61

Halla Rempt's avatar
Halla Rempt committed
62 63 64
    return KisImageBuilder_RESULT_OK;
}

65
KisImageSP OraConverter::image()
Halla Rempt's avatar
Halla Rempt committed
66
{
Halla Rempt's avatar
Halla Rempt committed
67
    return m_image;
Halla Rempt's avatar
Halla Rempt committed
68 69
}

70 71 72 73
vKisNodeSP OraConverter::activeNodes()
{
    return m_activeNodes;
}
Halla Rempt's avatar
Halla Rempt committed
74

75
KisImageBuilder_Result OraConverter::buildFile(QIODevice *io, KisImageSP image, vKisNodeSP activeNodes)
Halla Rempt's avatar
Halla Rempt committed
76 77 78
{

    // Open file for writing
79
    KoStore* store = KoStore::createStore(io, KoStore::Write, "image/openraster", KoStore::Zip);
Halla Rempt's avatar
Halla Rempt committed
80 81 82
    if (!store) {
        return KisImageBuilder_RESULT_FAILURE;
    }
83

Halla Rempt's avatar
Halla Rempt committed
84
    OraSaveContext osc(store);
85
    KisOpenRasterStackSaveVisitor orssv(&osc, activeNodes);
Halla Rempt's avatar
Halla Rempt committed
86 87 88

    image->rootLayer()->accept(orssv);

Sven Langkamp's avatar
Sven Langkamp committed
89 90 91 92
    if (store->open("Thumbnails/thumbnail.png")) {
        QSize previewSize = image->bounds().size();
        previewSize.scale(QSize(256,256), Qt::KeepAspectRatio);

93
        QImage preview = image->convertToQImage(previewSize, 0);
Sven Langkamp's avatar
Sven Langkamp committed
94 95 96

        KoStoreDevice io(store);
        if (io.open(QIODevice::WriteOnly)) {
97 98 99 100 101 102
            preview.save(&io, "PNG");
        }
        io.close();
        store->close();
    }

103 104
    KisPaintDeviceSP dev = image->projection();
    KisPNGConverter::saveDeviceToStore("mergedimage.png", image->bounds(), image->xRes(), image->yRes(), dev, store);
Sven Langkamp's avatar
Sven Langkamp committed
105

Halla Rempt's avatar
Halla Rempt committed
106 107 108 109 110 111 112 113 114 115 116
    delete store;
    return KisImageBuilder_RESULT_OK;
}


void OraConverter::cancel()
{
    m_stop = true;
}