From f05be462bc7f06955ab558bb8d75e90f5bece85c Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 20 Jun 2013 16:57:10 +0200 Subject: [PATCH] Reduce redundant code in View3DInventorViewer class --- src/Gui/SoFCOffscreenRenderer.cpp | 27 ++++++----- src/Gui/SoFCOffscreenRenderer.h | 11 +++-- src/Gui/View3DInventorViewer.cpp | 75 ------------------------------- src/Gui/View3DInventorViewer.h | 8 +--- src/Gui/View3DPy.cpp | 30 +++++++------ 5 files changed, 38 insertions(+), 113 deletions(-) diff --git a/src/Gui/SoFCOffscreenRenderer.cpp b/src/Gui/SoFCOffscreenRenderer.cpp index 8af4a831b..025ac69d7 100644 --- a/src/Gui/SoFCOffscreenRenderer.cpp +++ b/src/Gui/SoFCOffscreenRenderer.cpp @@ -85,19 +85,16 @@ void SoFCOffscreenRenderer::writeToImage (QImage& img) const BitmapFactory().convert(image, img); } -void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* comment) const +void SoFCOffscreenRenderer::writeToImageFile(const char* filename, const char* comment, const SbMatrix& mat, const QImage& image) { Base::FileInfo file(filename); if (file.hasExtension("JPG") || file.hasExtension("JPEG")) { - QImage img; - writeToImage(img); - // writing comment in case of jpeg (Qt ignores setText() in case of jpeg) std::string com; if (strcmp(comment,"")==0) com = "Screenshot created by FreeCAD"; else if (strcmp(comment,"$MIBA")==0) - com = createMIBA(); + com = createMIBA(mat); else com = comment; @@ -105,7 +102,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); - img.save(&buffer, "JPG"); + image.save(&buffer, "JPG"); writeJPEGComment(com, ba); QFile file(QString::fromUtf8(filename)); @@ -134,8 +131,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* // Supported by Qt if (supported) { - QImage img; - writeToImage(img); + QImage img = image; // set keywords for PNG format if (file.hasExtension("PNG")) { img.setText(QLatin1String("Title"), QString::fromUtf8(filename)); @@ -143,7 +139,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* if (strcmp(comment,"")==0) img.setText(QLatin1String("Description"), QLatin1String("Screenshot created by FreeCAD")); else if (strcmp(comment,"$MIBA")==0) - img.setText(QLatin1String("Description"), QLatin1String(createMIBA().c_str())); + img.setText(QLatin1String("Description"), QLatin1String(createMIBA(mat).c_str())); else img.setText(QLatin1String("Description"), QString::fromUtf8(comment)); img.setText(QLatin1String("Creation Time"), QDateTime::currentDateTime().toString()); @@ -169,6 +165,9 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* throw Base::Exception(str.str()); } } + // + // Use internal buffer instead of QImage + // else if (isWriteSupported(file.extension().c_str())) { // Any format which is supported by Coin only if (!writeToFile(filename, file.extension().c_str())) @@ -249,7 +248,7 @@ QStringList SoFCOffscreenRenderer::getWriteImageFiletypeInfo() return formats; } -std::string SoFCOffscreenRenderer::createMIBA() const +std::string SoFCOffscreenRenderer::createMIBA(const SbMatrix& mat) const { std::stringstream com; const std::map& cfg = App::Application::Config(); @@ -264,10 +263,10 @@ std::string SoFCOffscreenRenderer::createMIBA() const com << " \n" ; com << " \n"; com << " \n" ; com << " \n" ; com << " \n" ; diff --git a/src/Gui/SoFCOffscreenRenderer.h b/src/Gui/SoFCOffscreenRenderer.h index a5f87b1a4..7a8cc71f4 100644 --- a/src/Gui/SoFCOffscreenRenderer.h +++ b/src/Gui/SoFCOffscreenRenderer.h @@ -79,19 +79,22 @@ public: * Note that you must still specify the full filename for the first argument, i.e. the second argument will * not automatically be attached to the filename -- it is only used to decide the filetype. * + * If \a comment is set to '$MIBA' information regarding the MIBA standard is + * embedded to the picture, otherwise the \a comment is embedded as is. + * The appropriate file format must support embedding meta information which + * is provided by JPEG or PNG. + * * This does basically the same as writeToFile() unless that all QImage file formats are supported if not * directly supported by Coin3D. */ - void writeToImageFile (const char *filename, const char* comment) const; + void writeToImageFile(const char* filename, const char* comment, const SbMatrix& mat, const QImage& img); /** * This method returns all image file formats supported by Coin3D (see getWriteFiletypeInfo()) with all QImage file formats that are * not directly supported by Coin3D, if so. */ QStringList getWriteImageFiletypeInfo(); - std::string createMIBA() const; - - SbMatrix _Matrix; + std::string createMIBA(const SbMatrix& mat) const; }; } // namespace Gui diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index 5f19b4b94..80796f003 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -516,81 +516,6 @@ void View3DInventorViewer::setSceneGraph (SoNode *root) } } -void View3DInventorViewer::savePicture(const char* filename, int w, int h, - int eBackgroundType, const char* comment) const -{ - // if no valid color use the current background - bool useBackground = false; - SbViewportRegion vp(getViewportRegion()); - if (w>0 && h>0) - vp.setWindowSize( (short)w, (short)h ); - - //NOTE: To support pixels per inch we must use SbViewportRegion::setPixelsPerInch( ppi ); - //The default value is 72.0. - //If we need to support grayscale images with must either use SoOffscreenRenderer::LUMINANCE or - //SoOffscreenRenderer::LUMINANCE_TRANSPARENCY. - SoFCOffscreenRenderer& renderer = SoFCOffscreenRenderer::instance(); - renderer.setViewportRegion(vp); - SoCallback* cb = 0; - - // if we use transparency then we must not set a background color - switch(eBackgroundType){ - case Current: - if (backgroundroot->findChild(pcBackGround) == -1) { - renderer.setBackgroundColor(this->getBackgroundColor()); - } - else { - useBackground = true; - cb = new SoCallback; - cb->setCallback(clearBuffer); - } - break; - case White: - renderer.setBackgroundColor( SbColor(1.0, 1.0, 1.0) ); - break; - case Black: - renderer.setBackgroundColor( SbColor(0.0, 0.0, 0.0) ); - break; - case Transparent: - renderer.setComponents(SoFCOffscreenRenderer::RGB_TRANSPARENCY ); - break; - default: - break; - } - - SoSeparator* root = new SoSeparator; - root->ref(); - - SoCamera* camera = getCamera(); - if (useBackground) { - root->addChild(backgroundroot); - root->addChild(cb); - } - root->addChild(getHeadlight()); - root->addChild(camera); - SoCallback* gl = new SoCallback; - gl->setCallback(setGLWidget,this->getGLWidget()); - root->addChild(gl); - root->addChild(pcViewProviderRoot); - if (useBackground) - root->addChild(cb); - root->addChild(foregroundroot); - - try { - // render the scene - if (!renderer.render(root)) - throw Base::Exception("Offscreen rendering failed"); - // set matrix for miba - renderer._Matrix = camera->getViewVolume().getMatrix(); - renderer.writeToImageFile(filename, comment); - root->unref(); - } - catch (...) { - root->unref(); - throw; // re-throw exception - } -} - void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const { // if no valid color use the current background diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h index 0a589a270..c8e2384ac 100644 --- a/src/Gui/View3DInventorViewer.h +++ b/src/Gui/View3DInventorViewer.h @@ -162,14 +162,8 @@ public: //@{ /** * Creates an image with width \a w and height \a h of the current scene graph - * and exports the rendered scenegraph directly to file \a filename. - * If \a comment is set to '$MIBA' information regarding the MIBA standard is - * embedded to the picture, otherwise the \a comment is embedded as is. - * The appropriate file format must support embedding meta information which - * is provided by JPEG or PNG. + * and exports the rendered scenegraph to an image. */ - void savePicture(const char* filename, int w, int h, int eBackgroundType, - const char* comment) const; void savePicture(int w, int h, int eBackgroundType, QImage&) const; void saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const; //@} diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp index a0570a16c..9f008bacd 100644 --- a/src/Gui/View3DPy.cpp +++ b/src/Gui/View3DPy.cpp @@ -28,6 +28,8 @@ # include # include # include +# include +# include #endif @@ -39,6 +41,7 @@ #include "NavigationStyle.h" #include "SoFCSelection.h" #include "SoFCSelectionAction.h" +#include "SoFCOffscreenRenderer.h" #include "SoFCVectorizeSVGAction.h" #include "SoFCVectorizeU3DAction.h" #include "SoFCDB.h" @@ -620,24 +623,25 @@ Py::Object View3DInventorPy::saveImage(const Py::Tuple& args) else throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)"); #endif + QImage img; if (App::GetApplication().GetParameterGroupByPath ("User parameter:BaseApp/Preferences/Document")->GetBool("DisablePBuffers",false)) { - QImage img; createImageFromFramebuffer(t, w, h, img); - img.save(QString::fromUtf8(cFileName)); - return Py::None(); + } + else { + try { + _view->getViewer()->savePicture(w, h, t, img); + } + catch (const Base::Exception&) { + createImageFromFramebuffer(t, w, h, img); + } } - try { - _view->getViewer()->savePicture(cFileName,w,h,t,cComment); - return Py::None(); - } - catch (const Base::Exception&) { - QImage img; - createImageFromFramebuffer(t, w, h, img); - img.save(QString::fromUtf8(cFileName)); - return Py::None(); - } + SoFCOffscreenRenderer& renderer = SoFCOffscreenRenderer::instance(); + SoCamera* cam = _view->getViewer()->getCamera(); + renderer.writeToImageFile(cFileName, cComment, cam->getViewVolume().getMatrix(), img); + + return Py::None(); } Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)