00001 #include <qfiledialog.h> 00002 #include <qimage.h> 00003 #include <typeinfo> 00004 #include <math.h> 00005 #include "runtime.h" 00006 #include "image-dialog.h" 00007 #include "text.h" 00008 #include "line.h" 00009 #include "box.h" 00010 #include "status.h" 00011 00012 /* 00013 Value * Runtime::distributionxz(ImageValue * image) 00014 { 00015 int as=image->width(); 00016 int bs=256; 00017 int cs=image->channelcount(); 00018 ImageValue * O = new ImageValue(as,bs,cs); 00019 int xs = image->width(); 00020 int ys = image->height(); 00021 O->clear(0); 00022 double zmax = image->maxz(); 00023 double zmin = image->minz(); 00024 double ztot = zmax-zmin; 00025 for(int c = 0; c < cs ; c ++) 00026 for(int x = 0; x < xs ; x ++) 00027 for(int y = 0 ; y < ys ; y++) 00028 { 00029 double z = image->get(x,y,c); 00030 z-=zmin; 00031 z/=ztot; 00032 z*=255.0; 00033 O->inc(x,(int)z,c); 00034 } 00035 return O; 00036 } 00037 00038 Value * Runtime::distributionyz(ImageValue * image) 00039 { 00040 int as=256; 00041 int bs=image->height(); 00042 int cs=image->channelcount(); 00043 ImageValue * O = new ImageValue(as,bs,cs); 00044 int xs = image->width(); 00045 int ys = image->height(); 00046 O->clear(0); 00047 double zmax = image->maxz(); 00048 double zmin = image->minz(); 00049 double ztot = zmax-zmin; 00050 for(int c = 0; c < cs ; c ++) 00051 for(int x = 0; x < xs ; x ++) 00052 for(int y = 0 ; y < ys ; y++) 00053 { 00054 double z = image->get(x,y,c); 00055 z-=zmin; 00056 z/=ztot; 00057 z*=255.0; 00058 O->inc((int)z,y,c); 00059 } 00060 return O; 00061 } 00062 00063 Value * Runtime::distribution(ImageValue * image) 00064 { 00065 if (image->width() > image->height()) 00066 return distributionxz(image); 00067 return distributionyz(image); 00068 } 00069 00070 Value * Runtime::distribution(vector<Value*> v) 00071 { 00072 if (v.size()==0) return NULL; 00073 Value * v0 = v[0]; 00074 if (v0==NULL) return NULL; 00075 if (typeid(*v0)!=typeid(ImageValue)) return NULL; 00076 ImageValue *I=(ImageValue*)v0; 00077 if (v.size()==1) return distribution(I); 00078 return NULL; 00079 } 00080 00081 00082 Value * Runtime::projectxz(ImageValue * image) 00083 { 00084 int as=image->width(); 00085 int bs=256; 00086 int cs=image->channelcount(); 00087 ImageValue * O = new ImageValue(as,bs,cs); 00088 int xs = image->width(); 00089 int ys = image->height(); 00090 O->clear(1); 00091 double zmax = image->maxz(); 00092 double zmin = image->minz(); 00093 double ztot = zmax-zmin; 00094 for(int c = 0; c < cs ; c ++) 00095 for(int x = 0; x < xs ; x ++) 00096 { 00097 double z = 0; 00098 for(int y = 0 ; y < ys ; y++) 00099 z+=image->get(x,y,c); 00100 z/=ys; 00101 z-=zmin; 00102 z/=ztot; 00103 z*=255; 00104 O->set(x,(int)z,c,0); 00105 } 00106 return O; 00107 } 00108 00109 Value * Runtime::projectyz(ImageValue * image) 00110 { 00111 int as=256; 00112 int bs=image->height(); 00113 int cs=image->channelcount(); 00114 ImageValue * O = new ImageValue(as,bs,cs); 00115 int xs = image->width(); 00116 int ys = image->height(); 00117 O->clear(1); 00118 double zmax = image->maxz(); 00119 double zmin = image->minz(); 00120 double ztot = zmax-zmin; 00121 for(int c = 0; c < cs ; c ++) 00122 for(int y = 0 ; y < ys ; y++) 00123 { 00124 double z = 0; 00125 for(int x = 0; x < xs ; x ++) 00126 z += image->get(x,y,c); 00127 z/=xs; 00128 z-=zmin; 00129 z/=ztot; 00130 z*=255.0; 00131 O->set((int)z,y,c,0); 00132 } 00133 return O; 00134 } 00135 00136 Value * Runtime::project(ImageValue * image) 00137 { 00138 if (image->width() > image->height()) 00139 return projectxz(image); 00140 return projectyz(image); 00141 } 00142 00143 Value * Runtime::project(vector<Value*> v) 00144 { 00145 if (v.size()==0) return NULL; 00146 Value * v0 = v[0]; 00147 if (v0==NULL) return NULL; 00148 if (typeid(*v0)!=typeid(ImageValue)) return NULL; 00149 ImageValue *I=(ImageValue*)v0; 00150 if (v.size()==1) return project(I); 00151 return NULL; 00152 } 00153 00154 */ 00155 00156 Value * Runtime::apply(QString op, vector<Value*> evaluated) 00157 { 00158 if (op=="+") return add(evaluated); 00159 if (op=="-") return sub(evaluated); 00160 if (op=="/") return div(evaluated); 00161 if (op=="*") return mul(evaluated); 00162 if (op=="image") return image(evaluated); 00163 // selection 00164 if (op=="select") return select(evaluated); 00165 if (op=="line") return line(evaluated); 00166 if (op=="box") return box(evaluated); 00167 // translation, rotation and affine transformation 00168 if (op=="center") return center(evaluated); 00169 if (op=="zoom") return zoom(evaluated); 00170 if (op=="zoomx") return zoomx(evaluated); 00171 if (op=="zoomy") return zoomy(evaluated); 00172 if (op=="straighten") return straighten(evaluated); 00173 if (op=="crop") return crop(evaluated); 00174 // projection like operations 00175 // if (op=="distribution") return distribution(evaluated); 00176 // if (op=="project") return project(evaluated); 00177 #ifdef PSYTRACK 00178 if (op=="morph") return morph(evaluated); 00179 #endif 00180 throw new Error("Unknown operator "+op); 00181 } 00182 00183 bool Runtime::requires_user_input(Value * val) 00184 { 00185 if (!val) return false; 00186 if (typeid(*val)!=typeid(Expression)) 00187 return false; 00188 Expression * app = (Expression*)val; 00189 for(int i = 0 ; i < app->argc() ; i++) 00190 if (requires_user_input(app->argv(i))) return true; 00191 QString op = app->op(); 00192 if (op=="image") return true; 00193 if (op=="select") return true; 00194 if (op=="line") return true; 00195 if (op=="box") return true; 00196 return false; 00197 } 00198 00199 Runtime runtime;