1 /*
2 * Copyright (c) 2007 Cyrille Berger <cberger@cberger.net>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "ora_converter.h"
20
21 #include <QApplication>
22
23 #include <QFileInfo>
24
25 #include <KoStore.h>
26 #include <KoStoreDevice.h>
27 #include <KoColorSpaceRegistry.h>
28 #include <KisDocument.h>
29 #include <kis_group_layer.h>
30 #include <kis_image.h>
31 #include <kis_open_raster_stack_load_visitor.h>
32 #include <kis_open_raster_stack_save_visitor.h>
33 #include <kis_paint_layer.h>
34 #include "kis_png_converter.h"
35 #include "kis_open_raster_load_context.h"
36 #include "kis_open_raster_save_context.h"
37
OraConverter(KisDocument * doc)38 OraConverter::OraConverter(KisDocument *doc)
39 : m_doc(doc)
40 , m_stop(false)
41 {
42 }
43
~OraConverter()44 OraConverter::~OraConverter()
45 {
46 }
47
buildImage(QIODevice * io)48 KisImportExportErrorCode OraConverter::buildImage(QIODevice *io)
49 {
50 KoStore* store = KoStore::createStore(io, KoStore::Read, "image/openraster", KoStore::Zip);
51 if (!store) {
52 delete store;
53 return ImportExportCodes::FileFormatIncorrect;
54 }
55
56 KisOpenRasterLoadContext olc(store);
57 KisOpenRasterStackLoadVisitor orslv(m_doc->createUndoStore(), &olc);
58 orslv.loadImage();
59 m_image = orslv.image();
60
61 qDebug() << "m_image" << m_image;
62
63 if (!m_image) {
64 delete store;
65 return ImportExportCodes::ErrorWhileReading;
66 }
67
68 m_activeNodes = orslv.activeNodes();
69 delete store;
70
71 return ImportExportCodes::OK;
72 }
73
image()74 KisImageSP OraConverter::image()
75 {
76 return m_image;
77 }
78
activeNodes()79 vKisNodeSP OraConverter::activeNodes()
80 {
81 return m_activeNodes;
82 }
83
buildFile(QIODevice * io,KisImageSP image,vKisNodeSP activeNodes)84 KisImportExportErrorCode OraConverter::buildFile(QIODevice *io, KisImageSP image, vKisNodeSP activeNodes)
85 {
86
87 // Open file for writing
88 KoStore* store = KoStore::createStore(io, KoStore::Write, "image/openraster", KoStore::Zip);
89 if (!store) {
90 delete store;
91 return ImportExportCodes::Failure;
92 }
93
94 KisOpenRasterSaveContext osc(store);
95 KisOpenRasterStackSaveVisitor orssv(&osc, activeNodes);
96
97 image->rootLayer()->accept(orssv);
98
99 if (store->open("Thumbnails/thumbnail.png")) {
100 QSize previewSize = image->bounds().size();
101 previewSize.scale(QSize(256,256), Qt::KeepAspectRatio);
102
103 QImage preview = image->convertToQImage(previewSize, 0);
104
105 KoStoreDevice io(store);
106 if (io.open(QIODevice::WriteOnly)) {
107 preview.save(&io, "PNG");
108 }
109 io.close();
110 store->close();
111 }
112
113 KisPaintDeviceSP dev = image->projection();
114 KisPNGConverter::saveDeviceToStore("mergedimage.png", image->bounds(), image->xRes(), image->yRes(), dev, store);
115
116 delete store;
117 return ImportExportCodes::OK;
118 }
119
120
cancel()121 void OraConverter::cancel()
122 {
123 m_stop = true;
124 }
125
126
127