Skip to content

Commit

Permalink
+ support of arbitrary background colors in snapshot function
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 8, 2014
1 parent e753af2 commit 99caf6d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 120 deletions.
5 changes: 0 additions & 5 deletions src/Gui/DlgSettingsImage.ui
Expand Up @@ -61,11 +61,6 @@
<string>Black</string>
</property>
</item>
<item>
<property name="text" >
<string>Transparent</string>
</property>
</item>
</widget>
</item>
</layout>
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Thumbnail.cpp
Expand Up @@ -92,7 +92,7 @@ void Thumbnail::SaveDocFile (Base::Writer &writer) const
}
else {
try {
this->viewer->savePicture(this->size, this->size, View3DInventorViewer::Current, img);
this->viewer->savePicture(this->size, this->size, QColor(), img);
}
catch (...) {
this->createThumbnailFromFramebuffer(img);
Expand Down
5 changes: 3 additions & 2 deletions src/Gui/View3DInventor.cpp
Expand Up @@ -518,7 +518,8 @@ void View3DInventor::print(QPrinter* printer)
ps = SoVectorizeAction::A4;
break;
}
_viewer->saveGraphic(ps,View3DInventorViewer::White,&action);
QColor c = Qt::white;
_viewer->saveGraphic(ps,c,&action);
out->closeFile();
QSvgRenderer svg;
if (svg.load(QString::fromUtf8(tmp.c_str()))) {
Expand All @@ -537,7 +538,7 @@ void View3DInventor::print(QPrinter* printer)
}
else {
try {
_viewer->savePicture(rect.width(), rect.height(), View3DInventorViewer::White, img);
_viewer->savePicture(rect.width(), rect.height(), QColor(Qt::white), img);
}
catch (...) {
previewFromFramebuffer(rect, img);
Expand Down
72 changes: 20 additions & 52 deletions src/Gui/View3DInventorViewer.cpp
Expand Up @@ -796,7 +796,7 @@ void View3DInventorViewer::setSceneGraph(SoNode* root)
}
}

void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const
void View3DInventorViewer::savePicture(int w, int h, const QColor& bg, QImage& img) const
{
// if no valid color use the current background
bool useBackground = false;
Expand All @@ -813,9 +813,8 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
renderer.setViewportRegion(vp);
SoCallback* cb = 0;

// if we use transparency then we must not set a background color
switch (eBackgroundType) {
case Current:
// for an invalid color use the viewer's current background color
if (!bg.isValid()) {
if (backgroundroot->findChild(pcBackGround) == -1) {
const QColor col = this->backgroundColor();
renderer.setBackgroundColor(SbColor(col.redF(), col.greenF(), col.blueF()));
Expand All @@ -825,22 +824,9 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
cb = new SoCallback;
cb->setCallback(clearBufferCB);
}
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;
}
else {
renderer.setBackgroundColor(SbColor(bg.redF(), bg.greenF(), bg.blueF()));
}

SoSeparator* root = new SoSeparator;
Expand Down Expand Up @@ -888,29 +874,10 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
}
}

void View3DInventorViewer::saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const
void View3DInventorViewer::saveGraphic(int pagesize, const QColor& bgcolor, SoVectorizeAction* va) const
{
const QColor col = this->backgroundColor();

switch(eBackgroundType) {
case Current:
va->setBackgroundColor(true, SbColor(col.redF(), col.greenF(), col.blueF()));
break;

case White:
va->setBackgroundColor(true, SbColor(1.0, 1.0, 1.0));
break;

case Black:
va->setBackgroundColor(true, SbColor(0.0, 0.0, 0.0));
break;

case Transparent:
break; // not supported

default:
break;
}
if (bgcolor.isValid())
va->setBackgroundColor(true, SbColor(bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF()));

float border = 10.0f;
SbVec2s vpsize = this->getSoRenderManager()->getViewportRegion().getViewportSizePixels();
Expand Down Expand Up @@ -1069,9 +1036,9 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
Base::FileInfo fi(filename);

// Write VRML V2.0
if(fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) {
if (fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) {
// If 'wrz' is set then force compression
if(fi.hasExtension("wrz"))
if (fi.hasExtension("wrz"))
binary = true;

SoToVRML2Action tovrml2;
Expand All @@ -1081,7 +1048,7 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
std::string buffer = SoFCDB::writeNodesToString(vrmlRoot);
vrmlRoot->unref(); // release the memory as soon as possible

if(binary) {
if (binary) {
// We want to write compressed VRML but Coin 2.4.3 doesn't do it even though
// SoOutput::getAvailableCompressionMethods() delivers a string list that
// contains 'GZIP'. setCompression() was called directly after opening the file,
Expand All @@ -1091,7 +1058,7 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
Base::ofstream str(fi, std::ios::out | std::ios::binary);
zipios::GZIPOutputStream gzip(str);

if(gzip) {
if (gzip) {
gzip << buffer;
gzip.close();
ret = true;
Expand All @@ -1107,11 +1074,12 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
}
}
}
else if(fi.hasExtension("idtf") || fi.hasExtension("svg")) {
int ps=4, t=2;
else if (fi.hasExtension("idtf") || fi.hasExtension("svg")) {
int ps=4;
QColor c = Qt::white;
std::auto_ptr<SoVectorizeAction> vo;

if(fi.hasExtension("svg")) {
if (fi.hasExtension("svg")) {
vo = std::auto_ptr<SoVectorizeAction>(new SoFCVectorizeSVGAction());
}
else if(fi.hasExtension("idtf")) {
Expand All @@ -1123,21 +1091,21 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const

SoVectorOutput* out = vo->getOutput();

if(!out || !out->openFile(filename)) {
if (!out || !out->openFile(filename)) {
std::ostringstream a_out;
a_out << "Cannot open file '" << filename << "'";
throw Base::Exception(a_out.str());
}

saveGraphic(ps,t,vo.get());
saveGraphic(ps,c,vo.get());
out->closeFile();
}
else {
// Write Inventor in ASCII
std::string buffer = SoFCDB::writeNodesToString(pcViewProviderRoot);
Base::ofstream str(Base::FileInfo(filename), std::ios::out);

if(str) {
if (str) {
str << buffer;
str.close();
ret = true;
Expand Down
11 changes: 2 additions & 9 deletions src/Gui/View3DInventorViewer.h
Expand Up @@ -76,13 +76,6 @@ class GuiExport View3DInventorViewer : public Quarter::SoQTQuarterAdaptor, publi
typedef Quarter::SoQTQuarterAdaptor inherited;

public:
/// Background modes for the savePicture() method
enum eBackgroundType {
Current = 0, /**< Use the current viewer Background */
Black = 1, /**< Black background */
White = 2, /**< White background */
Transparent = 3, /**< Transparent background */
};
/// Pick modes for picking points in the scene
enum SelectionMode {
Lasso = 0, /**< Select objects using a lasso. */
Expand Down Expand Up @@ -204,8 +197,8 @@ class GuiExport View3DInventorViewer : public Quarter::SoQTQuarterAdaptor, publi
* Creates an image with width \a w and height \a h of the current scene graph
* and exports the rendered scenegraph to an image.
*/
void savePicture(int w, int h, int eBackgroundType, QImage&) const;
void saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const;
void savePicture(int w, int h, const QColor&, QImage&) const;
void saveGraphic(int pagesize, const QColor&, SoVectorizeAction* va) const;
//@}
/**
* Writes the current scenegraph to an Inventor file, either in ascii or binary.
Expand Down
79 changes: 29 additions & 50 deletions src/Gui/View3DPy.cpp
Expand Up @@ -26,6 +26,7 @@
#ifndef __InventorAll__
# include "InventorAll.h"
# include <sstream>
# include <QColor>
# include <QImage>
# include <QGLFramebufferObject>
# include <Inventor/SbViewVolume.h>
Expand Down Expand Up @@ -655,29 +656,15 @@ Py::Object View3DInventorPy::isAnimationEnabled(const Py::Tuple& args)
return Py::Boolean(ok ? true : false);
}

void View3DInventorPy::createImageFromFramebuffer(int backgroundType, int width, int height, QImage& img)
void View3DInventorPy::createImageFromFramebuffer(int width, int height, const QColor& bgcolor, QImage& img)
{
QGLFramebufferObject fbo(width, height, QGLFramebufferObject::Depth);
const QColor col = _view->getViewer()->backgroundColor();
bool on = _view->getViewer()->hasGradientBackground();

switch(backgroundType){
case 0: // Current
break;
case 1: // Black
_view->getViewer()->setBackgroundColor(QColor(0,0,0));
_view->getViewer()->setGradientBackground(false);
break;
case 2: // White
_view->getViewer()->setBackgroundColor(QColor(255,255,255));
_view->getViewer()->setGradientBackground(false);
break;
case 3: // Transparent
_view->getViewer()->setBackgroundColor(QColor(255,255,255));
_view->getViewer()->setGradientBackground(false);
break;
default:
break;
if (bgcolor.isValid()) {
_view->getViewer()->setBackgroundColor(bgcolor);
_view->getViewer()->setGradientBackground(false);
}

_view->getViewer()->renderToFramebuffer(&fbo);
Expand All @@ -688,46 +675,30 @@ void View3DInventorPy::createImageFromFramebuffer(int backgroundType, int width,

Py::Object View3DInventorPy::saveImage(const Py::Tuple& args)
{
char *cFileName,*cImageType="Current",*cComment="$MIBA";
int w=-1,h=-1,t;
char *cFileName,*cColor="Current",*cComment="$MIBA";
int w=-1,h=-1;

if (!PyArg_ParseTuple(args.ptr(), "s|iiss",&cFileName,&w,&h,&cImageType,&cComment))
if (!PyArg_ParseTuple(args.ptr(), "s|iiss",&cFileName,&w,&h,&cColor,&cComment))
throw Py::Exception();

#ifdef __GNUC__
if (strcasecmp(cImageType,"Current")==0)
t=0;
else if(strcasecmp(cImageType,"Black")==0)
t=1;
else if(strcasecmp(cImageType,"White")==0)
t=2;
else if(strcasecmp(cImageType,"Transparent")==0)
t=3;
else
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
#else
if (_stricmp(cImageType,"Current")==0)
t=0;
else if(_stricmp(cImageType,"Black")==0)
t=1;
else if(_stricmp(cImageType,"White")==0)
t=2;
else if(_stricmp(cImageType,"Transparent")==0)
t=3;
else
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
#endif
QColor bg;
QString colname = QString::fromLatin1(cColor);
if (colname.compare(QLatin1String("Current"), Qt::CaseInsensitive))
bg = QColor(); // assign an invalid color here
else
bg.setNamedColor(colname);

QImage img;
if (App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Document")->GetBool("DisablePBuffers",false)) {
createImageFromFramebuffer(t, w, h, img);
createImageFromFramebuffer(w, h, bg, img);
}
else {
try {
_view->getViewer()->savePicture(w, h, t, img);
_view->getViewer()->savePicture(w, h, bg, img);
}
catch (const Base::Exception&) {
createImageFromFramebuffer(t, w, h, img);
createImageFromFramebuffer(w, h, bg, img);
}
}

Expand All @@ -741,9 +712,10 @@ Py::Object View3DInventorPy::saveImage(const Py::Tuple& args)
Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
{
char* filename;
int ps=4, t=2;
int ps=4;
char* name="white";

if (!PyArg_ParseTuple(args.ptr(), "s|ii",&filename,&ps,&t))
if (!PyArg_ParseTuple(args.ptr(), "s|is",&filename,&ps,&name))
throw Py::Exception();

std::auto_ptr<SoVectorizeAction> vo;
Expand All @@ -769,7 +741,14 @@ Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
throw Py::Exception(a_out.str());
}

_view->getViewer()->saveGraphic(ps,t,vo.get());
QColor bg;
QString colname = QString::fromLatin1(name);
if (colname.compare(QLatin1String("Current"), Qt::CaseInsensitive))
bg = _view->getViewer()->backgroundColor();
else
bg.setNamedColor(colname);

_view->getViewer()->saveGraphic(ps,bg,vo.get());
out->closeFile();
return Py::None();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/View3DPy.h
Expand Up @@ -113,7 +113,7 @@ class View3DInventorPy : public Py::PythonExtension<View3DInventorPy>
typedef PyObject* (*method_varargs_handler)(PyObject *_self, PyObject *_args);
static method_varargs_handler pycxx_handler;
static PyObject *method_varargs_ext_handler(PyObject *_self, PyObject *_args);
void createImageFromFramebuffer(int backgroundType, int width, int height, QImage&);
void createImageFromFramebuffer(int width, int height, const QColor&, QImage&);

private:
std::list<PyObject*> callbacks;
Expand Down

0 comments on commit 99caf6d

Please sign in to comment.