filters
kis_openexr_export.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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
00096 KisRgbF16HalfColorSpace *cs = dynamic_cast<KisRgbF16HalfColorSpace *>(layer->paintDevice()->colorSpace());
00097
00098 if (cs == 0) {
00099
00100
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
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
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
00150
00151 return KoFilter::OK;
00152 }
00153
00154 #include "kis_openexr_export.moc"
00155
|