filters

kis_png_export.cpp
1/*
2 * Copyright (c) 2005-2006 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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 General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kis_png_export.h"
21
22#include <tqcheckbox.h>
23#include <tqslider.h>
24
25#include <tdeapplication.h>
26#include <kdialogbase.h>
27#include <kgenericfactory.h>
28
29#include <KoFilterChain.h>
30
31#include <kis_colorspace.h>
32#include <kis_doc.h>
33#include <kis_image.h>
34#include <kis_iterators_pixel.h>
35#include <kis_paint_layer.h>
36#include <kis_progress_display_interface.h>
37
38#include "kis_png_converter.h"
39#include "kis_wdg_options_png.h"
40
41typedef KGenericFactory<KisPNGExport, KoFilter> KisPNGExportFactory;
42K_EXPORT_COMPONENT_FACTORY(libchalkpngexport, KisPNGExportFactory("kofficefilters"))
43
44KisPNGExport::KisPNGExport(KoFilter *, const char *, const TQStringList&) : KoFilter()
45{
46}
47
48KisPNGExport::~KisPNGExport()
49{
50}
51
52KoFilter::ConversionStatus KisPNGExport::convert(const TQCString& from, const TQCString& to)
53{
54 kdDebug(41008) << "Png export! From: " << from << ", To: " << to << "\n";
55
56 KisDoc *output = dynamic_cast<KisDoc*>(m_chain->inputDocument());
57 TQString filename = m_chain->outputFile();
58
59 if (!output)
60 return KoFilter::CreationError;
61
62
63 if (filename.isEmpty()) return KoFilter::FileNotFound;
64
65 if (from != "application/x-chalk")
66 return KoFilter::NotImplemented;
67
68
69 KDialogBase* kdb = new KDialogBase(0, "", false, i18n("PNG Export Options"), KDialogBase::Ok | KDialogBase::Cancel);
70
71 KisImageSP img = output->currentImage();
72 KisPaintDeviceSP pd = new KisPaintDevice(*img->projection());
73 KisPaintLayerSP l = new KisPaintLayer(img, "projection", OPACITY_OPAQUE, pd);
74
75 KisRectIteratorPixel it = l->paintDevice()->createRectIterator(0,0, img->width(), img->height(), false);
76 KisColorSpace* cs = l->paintDevice()->colorSpace();
77 bool isThereAlpha = false;
78 while( !it.isDone() )
79 {
80 if(cs->getAlpha( it.rawData() ) != 255)
81 {
82 isThereAlpha = true;
83 break;
84 }
85 ++it;
86 }
87
88 KisWdgOptionsPNG* wdg = new KisWdgOptionsPNG(kdb);
89 wdg->alpha->setChecked(isThereAlpha);
90 wdg->alpha->setEnabled(isThereAlpha);
91 kdb->setMainWidget(wdg);
92 kapp->restoreOverrideCursor();
93 if(kdb->exec() == TQDialog::Rejected)
94 {
95 return KoFilter::OK; // FIXME Cancel doesn't exist :(
96 }
97
98 bool alpha = wdg->alpha->isChecked();
99 bool interlace = wdg->interlacing->isChecked();
100 int compression = wdg->compressionLevel->value();
101
102 delete kdb;
103
104
105 KURL url;
106 url.setPath(filename);
107
108 KisPNGConverter kpc(output, output->undoAdapter());
109
110 vKisAnnotationSP_it beginIt = img->beginAnnotations();
111 vKisAnnotationSP_it endIt = img->endAnnotations();
112 KisImageBuilder_Result res;
113
114
115 if ( (res = kpc.buildFile(url, l, beginIt, endIt, compression, interlace, alpha)) == KisImageBuilder_RESULT_OK) {
116 kdDebug(41008) << "success !" << endl;
117 return KoFilter::OK;
118 }
119 kdDebug(41008) << " Result = " << res << endl;
120 return KoFilter::InternalError;
121}
122
123#include <kis_png_export.moc>
124