38 std::vector<std::pair<int, int> >
ranges;
52 if (args.
nargs() >= 2) {
66 std::vector<std::pair<int, int> >
ranges;
81 double* out = &args.
outFp;
84 int num = args.
nargs();
86 for (
int k = 2; k < num; k++) val += args.
inFp<1>(k)[0];
88 for (
int k = 0; k < 3; k++) out[k] = val;
99 std::vector<std::pair<int, int> >
ranges;
114 double* out = &args.
outFp;
117 int num = args.
nargs();
119 for (
int k = 1; k < num; k++) val += (args.
inFp<3>(k)[0] + args.
inFp<3>(k)[1] + args.
inFp<3>(k)[2]);
121 for (
int k = 0; k < 3; k++) out[k] = val;
149 void eval(
double* result) { result[0] = val; }
151 void eval(
const char** result) { assert(
false); }
159 void eval(
double* result) {
160 for (
int k = 0; k < 3; k++) result[k] = val[k];
163 void eval(
const char** reuslt) {}
167 mutable std::map<std::string, Var> vars;
168 mutable std::map<std::string, VecVar> vecvars;
173 std::map<std::string, Var>::iterator i = vars.find(name);
174 if (i != vars.end())
return &i->second;
177 std::map<std::string, VecVar>::iterator i = vecvars.find(name);
178 if (i != vecvars.end())
return &i->second;
186 int main(
int argc,
char* argv[]) {
188 std::cerr <<
"Usage: " << argv[0] <<
" <image file> <width> <height> <exprFile>" << std::endl;
197 const char* imageFile = argv[1];
198 const char* exprFile = argv[4];
199 int width = atoi(argv[2]), height = atoi(argv[3]);
200 if (width < 0 || height < 0) {
201 std::cerr <<
"invalid width/height" << std::endl;
205 std::ifstream istream(exprFile);
207 std::cerr <<
"Cannot read file " << exprFile << std::endl;
210 std::string exprStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
211 ImageSynthExpr
expr(exprStr);
214 expr.vars[
"u"] = ImageSynthExpr::Var(0.);
215 expr.vars[
"v"] = ImageSynthExpr::Var(0.);
216 expr.vars[
"w"] = ImageSynthExpr::Var(width);
217 expr.vars[
"h"] = ImageSynthExpr::Var(height);
219 expr.vars[
"faceId"] = ImageSynthExpr::Var(0.);
220 expr.vecvars[
"P"] = ImageSynthExpr::VecVar();
221 expr.vecvars[
"Cs"] = ImageSynthExpr::VecVar();
222 expr.vecvars[
"Ci"] = ImageSynthExpr::VecVar();
225 bool valid =
expr.isValid();
227 std::cerr <<
"Invalid expression " << std::endl;
228 std::cerr <<
expr.parseError() << std::endl;
237 std::cerr <<
"Evaluating expresion...from " << exprFile << std::endl;
238 unsigned char* image =
new unsigned char[width * height * 4];
239 double one_over_width = 1. / width, one_over_height = 1. / height;
240 double& u =
expr.vars[
"u"].val;
241 double& v =
expr.vars[
"v"].val;
243 double& faceId =
expr.vars[
"faceId"].val;
248 unsigned char* pixel = image;
252 for (
int row = 0; row < height; row++) {
253 for (
int col = 0; col < width; col++) {
254 u = one_over_width * (col + .5);
255 v = one_over_height * (row + .5);
257 faceId = floor(u * 5);
268 const double* result =
expr.evalFP();
271 pixel[0] =
clamp(result[0] * 256.);
272 pixel[1] =
clamp(result[1] * 256.);
273 pixel[2] =
clamp(result[2] * 256.);
281 std::cerr <<
"Writing image..." << imageFile << std::endl;
282 FILE* fp = fopen(imageFile,
"wb");
289 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
290 info_ptr = png_create_info_struct(png_ptr);
291 png_init_io(png_ptr, fp);
292 int color_type = PNG_COLOR_TYPE_RGBA;
293 png_set_IHDR(png_ptr,
300 PNG_COMPRESSION_TYPE_DEFAULT,
301 PNG_FILTER_TYPE_DEFAULT);
302 const unsigned char* ptrs[height];
303 for (
int i = 0; i < height; i++) {
304 ptrs[i] = &image[width * i * 4];
306 png_set_rows(png_ptr, info_ptr, (png_byte**)ptrs);
307 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);