44 void eval(
double* result) { result[0] = val; }
46 void eval(
const char** result) { assert(
false); }
49 mutable std::map<std::string, Var> vars;
53 std::map<std::string, Var>::iterator i = vars.find(name);
54 if (i != vars.end())
return &i->second;
62 using namespace SeExpr2;
64 int main(
int argc,
char* argv[]) {
66 std::cerr <<
"Usage: " << argv[0] <<
" <image file> <width> <height> <exprFile>" << std::endl;
71 const char* imageFile = argv[1];
72 const char* exprFile = argv[4];
73 int width = atoi(argv[2]), height = atoi(argv[3]);
74 if (width < 0 || height < 0) {
75 std::cerr <<
"invalid width/height" << std::endl;
79 std::ifstream istream(exprFile);
81 std::cerr <<
"Cannot read file " << exprFile << std::endl;
84 std::string exprStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
85 ImageSynthExpr
expr(exprStr);
88 expr.vars[
"u"] = ImageSynthExpr::Var(0.);
89 expr.vars[
"v"] = ImageSynthExpr::Var(0.);
90 expr.vars[
"w"] = ImageSynthExpr::Var(width);
91 expr.vars[
"h"] = ImageSynthExpr::Var(height);
94 bool valid = expr.isValid();
96 std::cerr <<
"Invalid expression " << std::endl;
97 std::cerr << expr.parseError() << std::endl;
100 if (!expr.returnType().isFP(3)) {
101 std::cerr <<
"Expected color FP[3] got type " << expr.returnType().toString() << std::endl;
106 std::cerr <<
"Evaluating expresion...from " << exprFile << std::endl;
107 unsigned char* image =
new unsigned char[width * height * 4];
111 double one_over_width = 1. / width, one_over_height = 1. / height;
112 double& u = expr.vars[
"u"].val;
113 double& v = expr.vars[
"v"].val;
114 unsigned char* pixel = image;
115 for (
int row = 0; row < height; row++) {
116 for (
int col = 0; col < width; col++) {
117 u = one_over_width * (col + .5);
118 v = one_over_height * (row + .5);
120 const double* result = expr.evalFP();
123 pixel[0] =
clamp(result[0] * 256.);
124 pixel[1] =
clamp(result[1] * 256.);
125 pixel[2] =
clamp(result[2] * 256.);
133 std::cerr <<
"Writing image..." << imageFile << std::endl;
134 FILE* fp = fopen(imageFile,
"wb");
141 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
142 info_ptr = png_create_info_struct(png_ptr);
143 png_init_io(png_ptr, fp);
144 int color_type = PNG_COLOR_TYPE_RGBA;
145 png_set_IHDR(png_ptr,
152 PNG_COMPRESSION_TYPE_DEFAULT,
153 PNG_FILTER_TYPE_DEFAULT);
154 const unsigned char* ptrs[height];
155 for (
int i = 0; i < height; i++) {
156 ptrs[i] = &image[width * i * 4];
158 png_set_rows(png_ptr, info_ptr, (png_byte**)ptrs);
159 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
</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)
double min(double x, double y)
</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)
</pre > Once we have this we need an instance to store our variable and provide a reference to that We make it because resolveVar() is const .One does not need to store a variable reference in a given expression.In fact
int main(int argc, char *argv[])
abstract class for implementing variable references