00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #include <iostream>
00016 #include <string>
00017 #include <png.h>
00018
00019 #include <QtGui/QApplication>
00020 #include <QtGui/QDialog>
00021 #include <QtGui/QVBoxLayout>
00022 #include <QtGui/QScrollArea>
00023 #include <QtGui/QLabel>
00024 #include <QtGui/QImage>
00025 #include <QtGui/QPushButton>
00026 #include <QtGui/QMessageBox>
00027
00028 #include <SeExprEdControlCollection.h>
00029 #include <SeExprEditor.h>
00030 #include <SeExprEdBrowser.h>
00031 #include <SeExpression.h>
00032
00033 #include "ImageEditorDialog.h"
00034
00035
00036
00037 double clamp(double x){return std::max(0.,std::min(255.,x));}
00038
00039
00040 class ImageSynthExpression:public SeExpression
00041 {
00042 public:
00043
00044 ImageSynthExpression(const std::string& expr)
00045 :SeExpression(expr)
00046 {}
00047
00048
00049 struct Var:public SeExprScalarVarRef
00050 {
00051 Var(const double val):val(val){}
00052 Var(){}
00053 double val;
00054 void eval(const SeExprVarNode* ,SeVec3d& result)
00055 {result[0]=val;}
00056 };
00057
00058 mutable std::map<std::string,Var> vars;
00059
00060
00061 SeExprVarRef* resolveVar(const std::string& name) const
00062 {
00063 std::map<std::string,Var>::iterator i=vars.find(name);
00064 if(i != vars.end()) return &i->second;
00065 return 0;
00066 }
00067 };
00068
00069 class ImageSynthesizer
00070 {
00071 public:
00072 ImageSynthesizer();
00073 unsigned char *evaluateExpression(const std::string &exprStr);
00074 private:
00075 int _width;
00076 int _height;
00077 };
00078
00079 ImageSynthesizer::ImageSynthesizer()
00080 {
00081 _width = 256;
00082 _height = 256;
00083 }
00084
00085 unsigned char *ImageSynthesizer::evaluateExpression(const std::string &exprStr)
00086 {
00087 ImageSynthExpression expr(exprStr);
00088
00089
00090 expr.vars["u"]=ImageSynthExpression::Var(0.);
00091 expr.vars["v"]=ImageSynthExpression::Var(0.);
00092 expr.vars["w"]=ImageSynthExpression::Var(_width);
00093 expr.vars["h"]=ImageSynthExpression::Var(_height);
00094
00095
00096 bool valid=expr.isValid();
00097 if(!valid){
00098 std::cerr<<"Invalid expression "<<std::endl;
00099 std::cerr<<expr.parseError()<<std::endl;
00100 return NULL;
00101 }
00102
00103
00104 std::cerr<<"Evaluating expression..."<<std::endl;
00105 unsigned char* image=new unsigned char[_width*_height*4];
00106 double one_over_width=1./_width,one_over_height=1./_height;
00107 double& u=expr.vars["u"].val;
00108 double& v=expr.vars["v"].val;
00109 unsigned char* pixel=image;
00110 for(int row=0;row<_height;row++){
00111 for(int col=0;col<_width;col++){
00112 u=one_over_width*(col+.5);
00113 v=one_over_height*(row+.5);
00114 SeVec3d result=expr.evaluate();
00115 pixel[0]=clamp(result[2]*256.);
00116 pixel[1]=clamp(result[1]*256.);
00117 pixel[2]=clamp(result[0]*256.);
00118 pixel[3]=255;
00119 pixel+=4;
00120 }
00121 }
00122
00123 return image;
00124 }
00125
00126
00127
00128 ImageEditorDialog::ImageEditorDialog(QWidget *parent)
00129 :QDialog(parent)
00130 {
00131 _imageSynthesizer = new ImageSynthesizer();
00132
00133 this->setWindowTitle("Image Synthesis Editor");
00134
00135
00136 _imageLabel = new QLabel();
00137 _imageLabel->setFixedSize(256,256);
00138 _imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
00139 QImage image("./src/doc/html/seexprlogo.png");
00140 QPixmap imagePixmap = QPixmap::fromImage(image);
00141 imagePixmap = imagePixmap.scaled(256, 256, Qt::KeepAspectRatio);
00142 _imageLabel->setPixmap(imagePixmap);
00143
00144
00145 SeExprEdControlCollection *controls = new SeExprEdControlCollection();
00146 QScrollArea* scrollArea=new QScrollArea();
00147 scrollArea->setMinimumHeight(100);
00148 scrollArea->setFixedWidth(450);
00149 scrollArea->setWidgetResizable(true);
00150 scrollArea->setWidget(controls);
00151
00152
00153 _editor = new SeExprEditor(this, controls);
00154
00155
00156 SeExprEdBrowser *browser = new SeExprEdBrowser(0, _editor);
00157
00158
00159 browser->setSearchPath("imageEditor", "./src/demos/imageEditor");
00160 browser->getExpressionDirs();
00161 browser->update();
00162
00163
00164 QPushButton *applyButton=new QPushButton("Apply");
00165 connect(applyButton, SIGNAL(clicked()), (ImageEditorDialog*)this, SLOT(applyExpression()));
00166
00167
00168
00169
00170 QVBoxLayout *rootLayout = new QVBoxLayout();
00171 this->setLayout(rootLayout);
00172
00173 QWidget* topWidget=new QWidget();
00174 QHBoxLayout* topLayout=new QHBoxLayout();
00175 topLayout->setContentsMargins(0,0,0,0);
00176 topWidget->setLayout(topLayout);
00177
00178 QWidget *leftWidget=new QWidget();
00179 QVBoxLayout *leftLayout=new QVBoxLayout();
00180 leftLayout->setContentsMargins(0,0,0,0);
00181 leftWidget->setLayout(leftLayout);
00182 leftLayout->addWidget(_imageLabel);
00183 leftLayout->addWidget(scrollArea,1);
00184
00185 QWidget *bottomWidget=new QWidget();
00186 QVBoxLayout *bottomLayout=new QVBoxLayout();
00187 bottomLayout->setContentsMargins(0,0,0,0);
00188 bottomWidget->setLayout(bottomLayout);
00189
00190 QWidget *buttonWidget=new QWidget();
00191 QHBoxLayout *buttonLayout = new QHBoxLayout(0);
00192 buttonWidget->setLayout(buttonLayout);
00193 buttonLayout->addWidget(applyButton);
00194
00195 topLayout->addWidget(leftWidget);
00196 topLayout->addWidget(browser,1);
00197
00198 bottomLayout->addWidget(_editor);
00199 bottomLayout->addWidget(buttonWidget);
00200
00201 rootLayout->addWidget(topWidget);
00202 rootLayout->addWidget(bottomWidget);
00203 }
00204
00205
00206 void ImageEditorDialog::applyExpression()
00207 {
00208 std::string exprStr = _editor->getExpr();
00209 if( exprStr.empty() )
00210 {
00211 QMessageBox msgBox;
00212 msgBox.setText("No expression entered in the editor.");
00213 msgBox.exec();
00214 } else {
00215 QImage image(_imageSynthesizer->evaluateExpression(exprStr),
00216 256,
00217 256,
00218 QImage::Format_RGB32);
00219 if( image.isNull() )
00220 {
00221 QMessageBox msgBox;
00222 msgBox.setText("Error evaluating expression to create preview image.");
00223 msgBox.exec();
00224 } else {
00225 QPixmap imagePixmap = QPixmap::fromImage(image);
00226 _imageLabel->setPixmap(imagePixmap);
00227 }
00228 }
00229 }
00230
00231
00232
00233 int main(int argc, char *argv[]){
00234 QApplication app(argc, argv);
00235 ImageEditorDialog *dialog = new ImageEditorDialog(0);
00236 dialog->show();
00237 app.exec();
00238 return 0;
00239 }
00240