Reduce redundant code in View3DInventorViewer class

This commit is contained in:
wmayer 2013-06-20 16:57:10 +02:00
parent 7e3751224a
commit f05be462bc
5 changed files with 38 additions and 113 deletions

View File

@ -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<std::string, std::string>& cfg = App::Application::Config();
@ -264,10 +263,10 @@ std::string SoFCOffscreenRenderer::createMIBA() const
com << "<MIBA xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://juergen-riegel.net/Miba/Miba2.xsd\" Version=\"2\"> \n" ;
com << " <View>\n";
com << " <Matrix \n";
com << " a11=\"" << _Matrix[0][0] <<"\" a12=\"" << _Matrix[1][0] <<"\" a13=\"" << _Matrix[2][0] <<"\" a14=\"" << _Matrix[3][0] << "\"\n";
com << " a21=\"" << _Matrix[0][1] <<"\" a22=\"" << _Matrix[1][1] <<"\" a23=\"" << _Matrix[2][1] <<"\" a24=\"" << _Matrix[3][1] << "\"\n";
com << " a31=\"" << _Matrix[0][2] <<"\" a32=\"" << _Matrix[1][2] <<"\" a33=\"" << _Matrix[2][2] <<"\" a34=\"" << _Matrix[3][2] << "\"\n";
com << " a41=\"" << _Matrix[0][3] <<"\" a42=\"" << _Matrix[1][3] <<"\" a43=\"" << _Matrix[2][3] <<"\" a44=\"" << _Matrix[3][3] << "\"\n";
com << " a11=\"" << mat[0][0] <<"\" a12=\"" << mat[1][0] <<"\" a13=\"" << mat[2][0] <<"\" a14=\"" << mat[3][0] << "\"\n";
com << " a21=\"" << mat[0][1] <<"\" a22=\"" << mat[1][1] <<"\" a23=\"" << mat[2][1] <<"\" a24=\"" << mat[3][1] << "\"\n";
com << " a31=\"" << mat[0][2] <<"\" a32=\"" << mat[1][2] <<"\" a33=\"" << mat[2][2] <<"\" a34=\"" << mat[3][2] << "\"\n";
com << " a41=\"" << mat[0][3] <<"\" a42=\"" << mat[1][3] <<"\" a43=\"" << mat[2][3] <<"\" a44=\"" << mat[3][3] << "\"\n";
com << " />\n" ;
com << " </View>\n" ;
com << " <Source>\n" ;

View File

@ -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

View File

@ -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

View File

@ -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;
//@}

View File

@ -28,6 +28,8 @@
# include <sstream>
# include <QImage>
# include <QGLFramebufferObject>
# include <Inventor/SbViewVolume.h>
# include <Inventor/nodes/SoCamera.h>
#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(cFileName,w,h,t,cComment);
return Py::None();
_view->getViewer()->savePicture(w, h, t, img);
}
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)