filters

kis_openexr_import.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 <qstring.h>
00021 #include <qfile.h>
00022 
00023 #include <kgenericfactory.h>
00024 #include <KoDocument.h>
00025 #include <KoFilterChain.h>
00026 
00027 #include <half.h>
00028 #include <ImfRgbaFile.h>
00029 //#include <ImfStringAttribute.h>
00030 //#include <ImfMatrixAttribute.h>
00031 //#include <ImfArray.h>
00032 //#include <drawImage.h>
00033 
00034 #include <iostream>
00035 
00036 #include "kis_types.h"
00037 #include "kis_openexr_import.h"
00038 #include "kis_doc.h"
00039 #include "kis_image.h"
00040 #include "kis_meta_registry.h"
00041 #include "kis_layer.h"
00042 #include "kis_paint_layer.h"
00043 #include "kis_annotation.h"
00044 #include "kis_colorspace_factory_registry.h"
00045 #include "kis_iterators_pixel.h"
00046 #include "kis_abstract_colorspace.h"
00047 #include "kis_rgb_f32_colorspace.h"
00048 #include "kis_rgb_f16half_colorspace.h"
00049 
00050 using namespace std;
00051 using namespace Imf;
00052 using namespace Imath;
00053 
00054 typedef KGenericFactory<KisOpenEXRImport, KoFilter> KisOpenEXRImportFactory;
00055 K_EXPORT_COMPONENT_FACTORY(libkrita_openexr_import, KisOpenEXRImportFactory("kofficefilters"))
00056 
00057 KisOpenEXRImport::KisOpenEXRImport(KoFilter *, const char *, const QStringList&) : KoFilter()
00058 {
00059 }
00060 
00061 KisOpenEXRImport::~KisOpenEXRImport()
00062 {
00063 }
00064 
00065 KoFilter::ConversionStatus KisOpenEXRImport::convert(const QCString& from, const QCString& to)
00066 {
00067     if (from != "image/x-exr" || to != "application/x-krita") {
00068         return KoFilter::NotImplemented;
00069     }
00070 
00071     kdDebug(41008) << "\n\n\nKrita importing from OpenEXR\n";
00072 
00073     KisDoc * doc = dynamic_cast<KisDoc*>(m_chain -> outputDocument());
00074     if (!doc) {
00075         return KoFilter::CreationError;
00076     }
00077 
00078     doc -> prepareForImport();
00079 
00080     QString filename = m_chain -> inputFile();
00081 
00082     if (filename.isEmpty()) {
00083         return KoFilter::FileNotFound;
00084     }
00085 
00086     RgbaInputFile file(QFile::encodeName(filename));
00087     Box2i dataWindow = file.dataWindow();
00088     Box2i displayWindow = file.displayWindow();
00089 
00090     kdDebug(41008) << "Data window: " << QRect(dataWindow.min.x, dataWindow.min.y, dataWindow.max.x - dataWindow.min.x + 1, dataWindow.max.y - dataWindow.min.y + 1) << endl;
00091     kdDebug(41008) << "Display window: " << QRect(displayWindow.min.x, displayWindow.min.y, displayWindow.max.x - displayWindow.min.x + 1, displayWindow.max.y - displayWindow.min.y + 1) << endl;
00092 
00093     int imageWidth = displayWindow.max.x - displayWindow.min.x + 1;
00094     int imageHeight = displayWindow.max.y - displayWindow.min.y + 1;
00095 
00096     QString imageName = "Imported from OpenEXR";
00097 
00098     int dataWidth  = dataWindow.max.x - dataWindow.min.x + 1;
00099     int dataHeight = dataWindow.max.y - dataWindow.min.y + 1;
00100 
00101     KisRgbF16HalfColorSpace *cs = static_cast<KisRgbF16HalfColorSpace *>((KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("RGBAF16HALF", ""),"")));
00102 
00103     if (cs == 0) {
00104         return KoFilter::InternalError;
00105     }
00106 
00107     doc -> undoAdapter() -> setUndo(false);
00108 
00109     KisImageSP image = new KisImage(doc->undoAdapter(), imageWidth, imageHeight, cs, imageName);
00110 
00111     if (image == 0) {
00112         return KoFilter::CreationError;
00113     }
00114 
00115     KisPaintLayerSP layer = dynamic_cast<KisPaintLayer*>(image->newLayer(image -> nextLayerName(), OPACITY_OPAQUE).data());
00116 
00117     if (layer == 0) {
00118         return KoFilter::CreationError;
00119     }
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         file.readPixels(dataWindow.min.y + y);
00127 
00128         KisHLineIterator it = layer->paintDevice()->createHLineIterator(dataWindow.min.x, dataWindow.min.y + y, dataWidth, true);
00129         Rgba *rgba = pixels.data();
00130 
00131         while (!it.isDone()) {
00132 
00133             // XXX: For now unmultiply the alpha, though compositing will be faster if we
00134             // keep it premultiplied.
00135             half unmultipliedRed = rgba -> r;
00136             half unmultipliedGreen = rgba -> g;
00137             half unmultipliedBlue = rgba -> b;
00138 
00139             if (rgba -> a >= HALF_EPSILON) {
00140                 unmultipliedRed /= rgba -> a;
00141                 unmultipliedGreen /= rgba -> a;
00142                 unmultipliedBlue /= rgba -> a;
00143             }
00144 
00145             cs -> setPixel(it.rawData(), unmultipliedRed, unmultipliedGreen, unmultipliedBlue, rgba -> a);
00146             ++it;
00147             ++rgba;
00148         }
00149     }
00150 
00151     layer->setDirty();
00152     doc -> setCurrentImage(image);
00153     doc -> undoAdapter() -> setUndo(true);
00154     doc -> setModified(false);
00155 
00156     return KoFilter::OK;
00157 }
00158 
00159 #include "kis_openexr_import.moc"
00160 
KDE Home | KDE Accessibility Home | Description of Access Keys