26 #include <QtGui/QApplication>
27 #include <QtGui/QDialog>
28 #include <QtGui/QVBoxLayout>
29 #include <QtGui/QScrollArea>
30 #include <QtGui/QLabel>
31 #include <QtGui/QImage>
32 #include <QtGui/QPushButton>
33 #include <QtGui/QMessageBox>
35 #include <SeExpr2/UI/ExprControlCollection.h>
36 #include <SeExpr2/UI/ExprEditor.h>
37 #include <SeExpr2/UI/ExprBrowser.h>
38 #include <SeExpr2/Expression.h>
51 ImageSynthExpression(
const std::string&
expr)
59 : SeExpr2::
ExprVarRef(SeExpr2::ExprType().FP(1).Varying()), val(val) {}
61 : SeExpr2::
ExprVarRef(SeExpr2::ExprType().FP(1).Varying()), val(0.0) {}
63 void eval(
double* result){result[0]=val;}
64 void eval(
const char** result){assert(
false);}
67 mutable std::map<std::string,Var> vars;
72 std::map<std::string,Var>::iterator i=vars.find(name);
73 if(i != vars.end())
return &i->second;
78 class ImageSynthesizer
82 unsigned char *evaluateExpression(
const std::string &exprStr);
88 ImageSynthesizer::ImageSynthesizer()
94 unsigned char *ImageSynthesizer::evaluateExpression(
const std::string &exprStr)
96 ImageSynthExpression
expr(exprStr);
99 expr.vars[
"u"]=ImageSynthExpression::Var(0.);
100 expr.vars[
"v"]=ImageSynthExpression::Var(0.);
101 expr.vars[
"w"]=ImageSynthExpression::Var(_width);
102 expr.vars[
"h"]=ImageSynthExpression::Var(_height);
105 bool valid=
expr.isValid();
107 std::cerr<<
"Invalid expression "<<std::endl;
108 std::cerr<<
expr.parseError()<<std::endl;
113 std::cerr<<
"Evaluating expression..."<<std::endl;
114 unsigned char* image=
new unsigned char[_width*_height*4];
115 double one_over_width=1./_width,one_over_height=1./_height;
116 double& u=
expr.vars[
"u"].val;
117 double& v=
expr.vars[
"v"].val;
118 unsigned char* pixel=image;
119 for(
int row=0;row<_height;row++){
120 for(
int col=0;col<_width;col++){
121 u=one_over_width*(col+.5);
122 v=one_over_height*(row+.5);
124 pixel[0]=
clamp(result[2]*256.);
125 pixel[1]=
clamp(result[1]*256.);
126 pixel[2]=
clamp(result[0]*256.);
137 ImageEditorDialog::ImageEditorDialog(QWidget *parent)
140 _imageSynthesizer =
new ImageSynthesizer();
142 this->setWindowTitle(
"Image Synthesis Editor");
145 _imageLabel =
new QLabel();
146 _imageLabel->setFixedSize(256,256);
147 _imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
150 QString imageFile = QCoreApplication::applicationDirPath() +
"/../share/doc/SeExpr2/seexprlogo.png";
151 QImage image(imageFile);
153 QPixmap imagePixmap = QPixmap::fromImage(image);
154 imagePixmap = imagePixmap.scaled(256, 256, Qt::KeepAspectRatio);
155 _imageLabel->setPixmap(imagePixmap);
156 QWidget* imagePreviewWidget=
new QWidget();
157 QHBoxLayout* imagePreviewLayout=
new QHBoxLayout(imagePreviewWidget);
158 imagePreviewLayout->addStretch();
159 imagePreviewLayout->addWidget(_imageLabel);
160 imagePreviewLayout->addStretch();
164 QScrollArea* scrollArea=
new QScrollArea();
165 scrollArea->setMinimumHeight(100);
166 scrollArea->setFixedWidth(450);
167 scrollArea->setWidgetResizable(
true);
168 scrollArea->setWidget(controls);
178 #ifdef IMAGE_EDITOR_ROOT
179 std::string exPathStr = IMAGE_EDITOR_ROOT;
180 exPathStr +=
"/share/SeExpr2/expressions";
181 browser->
addPath(
"Examples", exPathStr);
183 browser->
addPath(
"Examples",
"./src/demos/imageEditor");
188 QPushButton *applyButton=
new QPushButton(
"Apply");
189 connect(applyButton, SIGNAL(clicked()), (ImageEditorDialog*)
this, SLOT(applyExpression()));
194 QVBoxLayout *rootLayout =
new QVBoxLayout();
195 this->setLayout(rootLayout);
197 QWidget* topWidget=
new QWidget();
198 QHBoxLayout* topLayout=
new QHBoxLayout();
199 topLayout->setContentsMargins(0,0,0,0);
200 topWidget->setLayout(topLayout);
202 QWidget *leftWidget=
new QWidget();
203 QVBoxLayout *leftLayout=
new QVBoxLayout();
204 leftLayout->setContentsMargins(0,0,0,0);
205 leftWidget->setLayout(leftLayout);
206 leftLayout->addWidget(imagePreviewWidget);
207 leftLayout->addWidget(scrollArea,1);
209 QWidget *bottomWidget=
new QWidget();
210 QVBoxLayout *bottomLayout=
new QVBoxLayout();
211 bottomLayout->setContentsMargins(0,0,0,0);
212 bottomWidget->setLayout(bottomLayout);
214 QWidget *buttonWidget=
new QWidget();
215 QHBoxLayout *buttonLayout =
new QHBoxLayout(0);
216 buttonWidget->setLayout(buttonLayout);
217 buttonLayout->addWidget(applyButton);
219 topLayout->addWidget(leftWidget);
220 topLayout->addWidget(browser,1);
222 bottomLayout->addWidget(_editor);
223 bottomLayout->addWidget(buttonWidget);
225 rootLayout->addWidget(topWidget);
226 rootLayout->addWidget(bottomWidget);
230 void ImageEditorDialog::applyExpression()
232 std::string exprStr = _editor->getExpr();
233 if( exprStr.empty() )
236 msgBox.setText(
"No expression entered in the editor.");
239 QImage image(_imageSynthesizer->evaluateExpression(exprStr),
242 QImage::Format_RGB32);
246 msgBox.setText(
"Error evaluating expression to create preview image.");
249 QPixmap imagePixmap = QPixmap::fromImage(image);
250 _imageLabel->setPixmap(imagePixmap);
257 int main(
int argc,
char *argv[]){
258 QApplication app(argc, argv);
259 ImageEditorDialog *dialog =
new ImageEditorDialog(0);
void addPath(const std::string &name, const std::string &path)
virtual ExprVarRef * resolveVar(const std::string &name) const
</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
virtual void eval(ArgHandle args)
double max(double x, double y)
Vec< const double, 3, true > Vec3dConstRef
double min(double x, double y)
void addUserExpressionPath(const std::string &context)
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x
double clamp(double x, double lo, double hi)
int main(int argc, char *argv[])
ExprVarRef(const ExprType &type)
abstract class for implementing variable references
Expression(EvaluationStrategy be=Expression::defaultEvaluationStrategy)