65int main(
int argc,
char* argv[]) {
67 std::cerr <<
"Usage: " << argv[0] <<
" <image file> <width> <height> <exprFile>" << std::endl;
72 const char* imageFile = argv[1];
73 const char* exprFile = argv[4];
74 int width = atoi(argv[2]), height = atoi(argv[3]);
75 if (width < 0 || height < 0) {
76 std::cerr <<
"invalid width/height" << std::endl;
80 std::ifstream istream(exprFile);
82 std::cerr <<
"Cannot read file " << exprFile << std::endl;
85 std::string exprStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
86 ImageSynthExpr
expr(exprStr);
89 expr.vars[
"u"] = ImageSynthExpr::Var(0.);
90 expr.vars[
"v"] = ImageSynthExpr::Var(0.);
91 expr.vars[
"w"] = ImageSynthExpr::Var(width);
92 expr.vars[
"h"] = ImageSynthExpr::Var(height);
95 bool valid =
expr.isValid();
97 std::cerr <<
"Invalid expression " << std::endl;
98 std::cerr <<
expr.parseError() << std::endl;
101 if (!
expr.returnType().isFP(3)) {
102 std::cerr <<
"Expected color FP[3] got type " <<
expr.returnType().toString() << std::endl;
107 std::cerr <<
"Evaluating expresion...from " << exprFile << std::endl;
108 unsigned char* image =
new unsigned char[width * height * 4];
112 double one_over_width = 1. / width, one_over_height = 1. / height;
113 double& u =
expr.vars[
"u"].val;
114 double& v =
expr.vars[
"v"].val;
115 unsigned char* pixel = image;
116 for (
int row = 0; row < height; row++) {
117 for (
int col = 0; col < width; col++) {
118 u = one_over_width * (col + .5);
119 v = one_over_height * (row + .5);
121 const double* result =
expr.evalFP();
124 pixel[0] =
clamp(result[0] * 256.);
125 pixel[1] =
clamp(result[1] * 256.);
126 pixel[2] =
clamp(result[2] * 256.);
134 std::cerr <<
"Writing image..." << imageFile << std::endl;
135 FILE* fp = fopen(imageFile,
"wb");
142 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
143 info_ptr = png_create_info_struct(png_ptr);
144 png_init_io(png_ptr, fp);
145 int color_type = PNG_COLOR_TYPE_RGBA;
146 png_set_IHDR(png_ptr,
153 PNG_COMPRESSION_TYPE_DEFAULT,
154 PNG_FILTER_TYPE_DEFAULT);
155 const unsigned char* ptrs[height];
156 for (
int i = 0; i < height; i++) {
157 ptrs[i] = &image[width * i * 4];
159 png_set_rows(png_ptr, info_ptr, (png_byte**)ptrs);
160 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);