filters

kis_openexr_export.cpp

00001 /*
00002  *  Copyright (c) 2005 Adrian Page <adrian@pagenet.plus.com>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include <qfile.h>
00021 
00022 #include <kmessagebox.h>
00023 
00024 #include <half.h>
00025 #include <ImfRgbaFile.h>
00026 
00027 #include <kgenericfactory.h>
00028 #include <KoDocument.h>
00029 #include <KoFilterChain.h>
00030 
00031 #include "kis_doc.h"
00032 #include "kis_image.h"
00033 #include "kis_layer.h"
00034 #include "kis_paint_layer.h"
00035 #include "kis_annotation.h"
00036 #include "kis_types.h"
00037 #include "kis_iterators_pixel.h"
00038 #include "kis_abstract_colorspace.h"
00039 #include "kis_paint_device.h"
00040 #include "kis_rgb_f32_colorspace.h"
00041 #include "kis_rgb_f16half_colorspace.h"
00042 
00043 #include "kis_openexr_export.h"
00044 
00045 using namespace std;
00046 using namespace Imf;
00047 using namespace Imath;
00048 
00049 typedef KGenericFactory<KisOpenEXRExport, KoFilter> KisOpenEXRExportFactory;
00050 K_EXPORT_COMPONENT_FACTORY(libkrita_openexr_export, KisOpenEXRExportFactory("kofficefilters"))
00051 
00052 KisOpenEXRExport::KisOpenEXRExport(KoFilter *, const char *, const QStringList&) : KoFilter()
00053 {
00054 }
00055 
00056 KisOpenEXRExport::~KisOpenEXRExport()
00057 {
00058 }
00059 
00060 KoFilter::ConversionStatus KisOpenEXRExport::convert(const QCString& from, const QCString& to)
00061 {
00062     if (to != "image/x-exr" || from != "application/x-krita") {
00063         return KoFilter::NotImplemented;
00064     }
00065 
00066     kdDebug(41008) << "Krita exporting to OpenEXR\n";
00067 
00068     // XXX: Add dialog about flattening layers here
00069 
00070     KisDoc *doc = dynamic_cast<KisDoc*>(m_chain -> inputDocument());
00071     QString filename = m_chain -> outputFile();
00072     
00073     if (!doc) {
00074         return KoFilter::CreationError;
00075     }
00076     
00077     if (filename.isEmpty()) {
00078         return KoFilter::FileNotFound;
00079     }
00080 
00081     KisImageSP img = new KisImage(*doc -> currentImage());
00082     Q_CHECK_PTR(img);
00083 
00084     // Don't store this information in the document's undo adapter
00085     bool undo = doc -> undoAdapter() -> undo();
00086     doc -> undoAdapter() -> setUndo(false);
00087 
00088     img -> flatten();
00089 
00090     KisPaintLayerSP layer = dynamic_cast<KisPaintLayer*>(img->activeLayer().data());
00091     Q_ASSERT(layer);
00092     
00093     doc -> undoAdapter() -> setUndo(undo);
00094 
00095     //KisF32RgbColorSpace * cs = static_cast<KisF32RgbColorSpace *>((KisColorSpaceRegistry::instance() -> get(KisID("RGBAF32", ""))));
00096     KisRgbF16HalfColorSpace *cs = dynamic_cast<KisRgbF16HalfColorSpace *>(layer->paintDevice()->colorSpace());
00097 
00098     if (cs == 0) {
00099         // We could convert automatically, but the conversion wants to be done with
00100         // selectable profiles and rendering intent.
00101         KMessageBox::information(0, i18n("The image is using an unsupported color space. "
00102                                          "Please convert to 16-bit floating point RGB/Alpha "
00103                                          "before saving in the OpenEXR format."));
00104 
00105         // Don't show the couldn't save error dialog.
00106         doc -> setErrorMessage("USER_CANCELED");
00107 
00108         return KoFilter::WrongFormat;
00109     }
00110 
00111     Box2i displayWindow(V2i(0, 0), V2i(img -> width() - 1, img -> height() - 1));
00112 
00113     QRect dataExtent = layer -> exactBounds();
00114     int dataWidth = dataExtent.width();
00115     int dataHeight = dataExtent.height();
00116 
00117     Box2i dataWindow(V2i(dataExtent.left(), dataExtent.top()), V2i(dataExtent.right(), dataExtent.bottom()));
00118 
00119     RgbaOutputFile file(QFile::encodeName(filename), displayWindow, dataWindow, WRITE_RGBA);
00120 
00121     QMemArray<Rgba> pixels(dataWidth);
00122 
00123     for (int y = 0; y < dataHeight; ++y) {
00124 
00125         file.setFrameBuffer(pixels.data() - dataWindow.min.x - (dataWindow.min.y + y) * dataWidth, 1, dataWidth);
00126 
00127         KisHLineIterator it = layer->paintDevice()->createHLineIterator(dataWindow.min.x, dataWindow.min.y + y, dataWidth, false);
00128         Rgba *rgba = pixels.data();
00129 
00130         while (!it.isDone()) {
00131 
00132             // XXX: Currently we use unmultiplied data so premult it.
00133             half unmultipliedRed;
00134             half unmultipliedGreen;
00135             half unmultipliedBlue;
00136             half alpha;
00137 
00138             cs -> getPixel(it.rawData(), &unmultipliedRed, &unmultipliedGreen, &unmultipliedBlue, &alpha);
00139             rgba -> r = unmultipliedRed * alpha;
00140             rgba -> g = unmultipliedGreen * alpha;
00141             rgba -> b = unmultipliedBlue * alpha;
00142             rgba -> a = alpha;
00143             ++it;
00144             ++rgba;
00145         }
00146         file.writePixels();
00147     }
00148 
00149     //vKisAnnotationSP_it beginIt = img -> beginAnnotations();
00150     //vKisAnnotationSP_it endIt = img -> endAnnotations();
00151     return KoFilter::OK;
00152 }
00153 
00154 #include "kis_openexr_export.moc"
00155 
KDE Home | KDE Accessibility Home | Description of Access Keys