3
// general-purpose munging util for voxbo
4
// Copyright (c) 2003-2009 by The VoxBo Development Team
6
// VoxBo is free software: you can redistribute it and/or modify it
7
// under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
11
// VoxBo is distributed in the hope that it will be useful, but
12
// WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
// General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with VoxBo. If not, see <http://www.gnu.org/licenses/>.
19
// For general information on VoxBo, including the latest complete
20
// source code and binary distributions, manual, and associated files,
21
// see the VoxBo home page at: http://www.voxbo.org/
23
// original version written by Dan Kimberg
26
#include <boost/format.hpp>
29
#include "imageutils.h"
30
#include "vbversion.h"
31
#include "vbimagemunge.hlp.h"
33
void vbimagemunge_help();
34
void vbimagemunge_version();
36
int cube_combine(Cube &cb,tokenlist &args);
37
void selectcubes(list<Cube> &clist,size_t n);
38
bool comparedims(list<Cube> cubelist);
39
void cube_random01(Cube &cb);
40
void cube_printregioninfo(Cube &cb);
41
void reallyload(Cube &cube);
44
// ugly globals -- code needs some reorg
53
// an operator is combining if either initfn or finishfn is non-null
54
vbreturn (*initfn)(list<Cube> &cubelist,tokenlist &args);
55
vbreturn (*procfn)(Cube &cube,tokenlist &args);
56
vbreturn (*finishfn)(list<Cube> &cubelist,tokenlist &args);
57
// storage for combining operators
58
// sole constructor requires name and nargs
60
imageop(string xname,int xnargs,vbreturn (*xprocfn)(Cube &,tokenlist &));
61
void init(string xname,int xnargs,vbreturn (*xprocfn)(Cube &,tokenlist &));
63
bool iscombining() {return (initfn!=NULL || finishfn!=NULL);}
71
imageop::imageop(string xname,int xnargs,vbreturn (*xprocfn)(Cube &,tokenlist &))
73
init(xname,xnargs,xprocfn);
77
void imageop::init(string xname,int xnargs,vbreturn (*xprocfn)(Cube &,tokenlist &))
87
// three operator lists that are handled differently
88
list<imageop> phase1; // do the cubes one at a time. if last operator is combining, feed.
89
list<imageop> phase2; // do each operator for all cubes
90
typedef list<imageop>::iterator OPI;
91
typedef list<Cube>::iterator CUBI;
93
map<string,imageop> oplist;
96
main(int argc,char *argv[])
104
list<string> filelist;
106
args.Transfer(argc-1,argv+1);
109
for (int i=0; i<args.size(); i++) {
111
if (oplist.count(args[i])==0) {
112
printf("[E] vbimagemunge: unknown operator %s\n",args(i));
115
imageop myop=oplist[args[i]];
116
if (i+myop.nargs>=args.size()) {
117
printf("[E] vbimagemunge: operator %s requires %d arguments\n",
118
myop.name.c_str(),myop.nargs);
121
myop.args.Add(args[i]); // first arg is the operator name
122
for (int j=0; j<myop.nargs; j++)
123
myop.args.Add(args[++i]);
124
// now decide which list gets the op
126
phase2.push_back(myop);
127
// if we're in phase 1 and it's parallel, just put it on
128
else if (!myop.iscombining())
129
phase1.push_back(myop);
130
// if we're in phase 1 and it's combining, see if we have any parallels first
133
phase1.push_back(myop);
135
phase2.push_back(myop);
143
else if (args[i]=="-h") {
147
else if (args[i]=="-v") {
148
vbimagemunge_version();
152
filelist.push_back(args[i]);
159
// pre-load all the files. we should figure out how not to load all
160
// the image data from 4D files, maybe.
162
for (list<string>::iterator ff=filelist.begin(); ff!=filelist.end(); ff++) {
163
printf("[I] vbimagemunge: reading file %s\n",ff->c_str());
166
if (cb.ReadHeader(*ff)==0) {
167
cubelist.push_back(cb);
170
if (ts.ReadFile(*ff)) {
171
printf("[E] vbimagemunge: couldn't read file %s, continuing anyway\n",ff->c_str());
174
for (int i=0; i<ts.dimt; i++) {
176
cubelist.push_back(tmpc);
177
ts.getCube(i,cubelist.back());
180
if (cubelist.size()==0) {
181
printf("[E] vbimagemunge: no valid input cubes found\n");
188
if (phase1.back().iscombining() && phase1.back().initfn)
189
phase1.back().initfn(cubelist,phase1.back().args);
190
for (CUBI cc=cubelist.begin(); cc!=cubelist.end(); cc++) {
191
for (OPI oo=phase1.begin(); oo!=phase1.end(); oo++) {
193
oo->procfn(*cc,oo->args);
197
if (phase1.back().iscombining() && phase1.back().finishfn)
198
phase1.back().finishfn(cubelist,phase1.back().args);
200
// if we have a phase2 block, now do that
201
for (OPI oo=phase2.begin(); oo!=phase2.end(); oo++) {
203
oo->initfn(cubelist,oo->args);
205
for (CUBI cc=cubelist.begin(); cc!=cubelist.end(); cc++) {
207
oo->procfn(*cc,oo->args);
211
oo->finishfn(cubelist,oo->args);
217
// for "combine" the arguments are:
218
// combine fixed/moving x y z op value
222
cube_combine_fixed(Cube &cb,tokenlist &args)
224
int xx=strtol(args[2]);
225
int yy=strtol(args[3]);
226
int zz=strtol(args[4]);
227
string method=args[5];
228
double value=strtod(args[6]);
230
if (method=="sum" || method=="average" || method=="sumthresh" || method=="averagethresh")
233
if (xx<1 || yy<1 || zz<1) {
234
printf("[E] vbimagemunge: invalid subcube dimensions\n");
239
// initial subcube range 0:xx-1, etc.
241
int xn=xx-1,yn=yy-1,zn=zz-1;
249
if (x1>cb.dimx-1 || y1>cb.dimy-1 || z1>cb.dimz-1)
251
if (xn>cb.dimx-1) xn=cb.dimx-1;
252
if (yn>cb.dimy-1) yn=cb.dimy-1;
253
if (zn>cb.dimz-1) zn=cb.dimz-1;
254
for (i=x1; i<=xn; i++) {
255
for (j=y1; j<=yn; j++) {
256
for (k=z1; k<=zn; k++) {
258
val=cb.getValue<T>(i,j,k);
263
else if (cb.testValue(i,j,k))
268
// calculate the replacement value -- sum, count, average,
269
// pct, any sumthresh, averagethresh
271
int rsize=(xn-x1+1)*(yn-y1+1)*(zn-z1+1);
274
else if (method=="sum")
276
else if (method=="pct")
277
newvalue=(count*100)/rsize;
278
else if (method=="average")
279
newvalue=total/rsize;
280
else if (method=="any") {
281
if (count) newvalue=1;
283
else if (method=="sumthresh") {
284
if (total>value) newvalue=1;
286
else if (method=="averagethresh") {
287
if ((total/rsize)>value) newvalue=1;
291
// all voxels in the subcube get the identical new value
292
for (i=x1; i<=xn; i++) {
293
for (j=y1; j<=yn; j++) {
294
for (k=z1; k<=zn; k++) {
295
cb2.setValue<T>(i,j,k,newvalue);
304
else if (yn<cb.dimy-1) {
310
else if (zn<cb.dimz-1) {
326
cube_combine_moving(Cube &cb,tokenlist &args)
328
int xx=strtol(args[2]);
329
int yy=strtol(args[3]);
330
int zz=strtol(args[4]);
331
string method=args[5];
332
double value=strtod(args[6]);
334
if (method=="sum" || method=="average" || method=="sumthresh" || method=="averagethresh")
337
if (xx<1 || yy<1 || zz<1) {
338
printf("[E] vbimagemunge: invalid subcube dimensions\n");
343
// initial subcube range 0:xx-1, etc.
345
int xn=xx-1,yn=yy-1,zn=zz-1;
351
for (int xxx=0; xxx<=cb.dimx; xxx++) {
352
for (int yyy=0; yyy<=cb.dimy; yyy++) {
353
for (int zzz=0; zzz<=cb.dimz; zzz++) {
356
// build a neighborhood around the voxel
363
// clip it to cube boundaries
367
if (xn>cb.dimx-1) xn=cb.dimx-1;
368
if (yn>cb.dimy-1) yn=cb.dimy-1;
369
if (zn>cb.dimz-1) zn=cb.dimz-1;
370
for (i=x1; i<=xn; i++) {
371
for (j=y1; j<=yn; j++) {
372
for (k=z1; k<=zn; k++) {
374
val=cb.getValue<T>(i,j,k);
379
else if (cb.testValue(i,j,k))
384
// calculate the replacement value -- sum, count, average,
385
// pct, any sumthresh, averagethresh
387
int rsize=(xn-x1+1)*(yn-y1+1)*(zn-z1+1);
390
else if (method=="sum")
392
else if (method=="pct")
393
newvalue=(count*100)/rsize;
394
else if (method=="average")
395
newvalue=total/rsize;
396
else if (method=="any") {
397
if (count) newvalue=1;
399
else if (method=="sumthresh") {
400
if (total>value) newvalue=1;
402
else if (method=="averagethresh") {
403
if ((total/rsize)>value) newvalue=1;
407
// set just the original voxel
408
cb2.setValue<T>(xxx,yyy,zzz,newvalue);
417
cube_combine(Cube &cb,tokenlist &args)
419
if (args[1]=="fixed" || args[1]=="f") {
420
switch(cb.datatype) {
422
return cube_combine_fixed<char>(cb,args);
425
return cube_combine_fixed<int16>(cb,args);
428
return cube_combine_fixed<int32>(cb,args);
431
return cube_combine_fixed<float>(cb,args);
434
return cube_combine_fixed<double>(cb,args);
438
else if (args[1]=="moving" || args[1]=="m") {
439
switch(cb.datatype) {
441
return cube_combine_moving<char>(cb,args);
444
return cube_combine_moving<int16>(cb,args);
447
return cube_combine_moving<int32>(cb,args);
450
return cube_combine_moving<float>(cb,args);
453
return cube_combine_moving<double>(cb,args);
458
return 101; // shouldn't happen
462
cube_printregioninfo(Cube &cb)
464
vector<VBRegion> rlist=findregions(cb,vb_ne,0.0);
466
for (size_t i=0; i<rlist.size(); i++)
467
totalvoxels+=rlist[i].size();
468
printf("[I] vbimagemunge: %d voxels in %d regions found in volume %s\n",
469
totalvoxels,(int)(rlist.size()),cb.GetFileName().c_str());
470
for (size_t i=0; i<rlist.size(); i++) {
471
// calculate centers of mass
472
double x1=0,y1=0,z1=0,x2=0,y2=0,z2=0,totalmass=0;
473
for (VI myvox=rlist[i].begin(); myvox!=rlist[i].end(); myvox++) {
477
x2+=myvox->second.x*myvox->second.val;
478
y2+=myvox->second.y*myvox->second.val;
479
z2+=myvox->second.z*myvox->second.val;
480
totalmass+=myvox->second.val;
489
printf("[I] vbimagemunge: region %d, %d voxels,",(int)i,(int)(rlist[i].size()));
490
printf(" center of mass: %g,%g,%g (weighted: %g,%g,%g)\n",x1,y1,z1,x2,y2,z2);
491
// totalvoxels+=rlist[i].size();
493
// printf("[I] vbimagemunge: %d total voxels in volume %s\n",totalvoxels,cb.GetFileName().c_str());
497
cube_random01(Cube &cb)
502
for (int i=0; i<cb.dimx; i++) {
503
for (int j=0; j<cb.dimy; j++) {
504
for (int k=0; k<cb.dimz; k++) {
510
cb.SetValue(i,j,k,1.0);
518
selectcubes(list<Cube> &clist,size_t n)
520
if (n>=clist.size() || n<1)
522
map<uint32,bool> randlist;
523
while (randlist.size()<n)
524
randlist[VBRandom()]=1;
525
while (randlist.size()<clist.size())
526
randlist[VBRandom()]=0;
527
map<uint32,bool>::iterator mm=randlist.begin();
528
list<Cube>::iterator cc=clist.begin();
529
int oldn=clist.size();
530
for (int i=0; i<oldn; i++) {
540
comparedims(list<Cube> cubelist)
542
if (cubelist.size()<2)
544
list<Cube>::iterator cc=cubelist.begin();
549
while (cc!=cubelist.end()) {
550
if (cc->dimx!=dimx || cc->dimy!=dimy || cc->dimz!=dimz)
559
vbreturn op_shortzeros(list<Cube> &cubelist,tokenlist &)
561
if (cubelist.size()==0)
563
mycube.SetVolume(cubelist.front().dimx,cubelist.front().dimy,
564
cubelist.front().dimz,vb_short);
568
vbreturn op_allones(list<Cube> &cubelist,tokenlist &)
570
if (cubelist.size()==0)
572
reallyload(cubelist.front());
573
mycube=cubelist.front();
574
mycube.quantize(1.0);
578
vbreturn op_allzeros(list<Cube> &cubelist,tokenlist &)
580
if (cubelist.size()==0)
582
reallyload(cubelist.front());
583
mycube=cubelist.front();
588
vbreturn op_copy(list<Cube> &cubelist,tokenlist &)
590
if (cubelist.size()==0)
592
reallyload(cubelist.front());
593
mycube=cubelist.front();
599
vbreturn op_smoothvox(Cube &cube,tokenlist &args)
602
smoothCube(cube,strtod(args[1]),strtod(args[2]),strtod(args[3]));
606
vbreturn op_smoothmm(Cube &cube,tokenlist &)
609
printf("smoothmm not implemented\n");
614
vbreturn op_thresh(Cube &cube,tokenlist &args)
617
cube.thresh(strtod(args[1]));
621
vbreturn op_rotate(Cube &cube,tokenlist &args)
624
rotatecube(cube,strtod(args[1]),strtod(args[2]),strtod(args[3]));
628
vbreturn op_regionat(Cube &cube,tokenlist &args)
632
mask.SetVolume(cube.dimx,cube.dimy,cube.dimz,vb_short);
635
rr=growregion(strtol(args[1]),strtol(args[2]),strtol(args[3]),cube,mask,vb_agt,FLT_MIN);
637
for (VI myvox=rr.begin(); myvox!=rr.end(); myvox++) {
638
mask.setValue(myvox->second.x,myvox->second.y,myvox->second.z,(int32)1);
644
vbreturn op_splitregions(Cube &cube,tokenlist &args)
647
vector<VBRegion> regions;
648
regions=findregions(cube,vb_gt,0.0);
650
mask.SetVolume(cube.dimx,cube.dimy,cube.dimz,vb_byte);
653
vbforeach(VBRegion &rr,regions) {
655
for (VI myvox=rr.begin(); myvox!=rr.end(); myvox++)
656
mask.setValue<char>(myvox->second.x,myvox->second.y,myvox->second.z,1);
658
string num=(format("%05d")%index).str();
659
replace_string(fname,"XXX",num);
660
mask.WriteFile(fname);
667
vbreturn op_threshabs(Cube &cube,tokenlist &args)
670
cube.threshabs(strtod(args[1]));
674
vbreturn op_cutoff(Cube &cube,tokenlist &args)
677
cube.cutoff(strtod(args[1]));
681
vbreturn op_orient(Cube &cube,tokenlist &)
684
printf("orient not implemented\n");
689
vbreturn op_quantize(Cube &cube,tokenlist &args)
692
cube.quantize(strtod(args[1]));
696
vbreturn op_invert(Cube &cube,tokenlist &)
703
vbreturn op_flipx(Cube &cube,tokenlist &)
710
vbreturn op_flipy(Cube &cube,tokenlist &)
717
vbreturn op_flipz(Cube &cube,tokenlist &)
724
vbreturn op_zeroleft(Cube &cube,tokenlist &)
731
vbreturn op_zeroright(Cube &cube,tokenlist &)
738
vbreturn op_bigendian(Cube &cube,tokenlist &)
741
cube.filebyteorder=ENDIAN_BIG;
745
vbreturn op_littleendian(Cube &cube,tokenlist &)
748
cube.filebyteorder=ENDIAN_LITTLE;
752
vbreturn op_byteswap(Cube &cube,tokenlist &)
759
vbreturn op_combine(Cube &cube,tokenlist &args)
762
if (cube_combine(cube,args)) {
763
printf("[E] vbimagemunge: error downsampling\n");
769
vbreturn op_convert(Cube &cube,tokenlist &args)
773
string newtype=vb_tolower(args[1]);
776
err=cube.convert_type(vb_byte,VBSETALT);
777
if (newtype=="int16")
778
err=cube.convert_type(vb_short,VBSETALT);
779
if (newtype=="int32")
780
err=cube.convert_type(vb_long,VBSETALT);
781
if (newtype=="float")
782
err=cube.convert_type(vb_float,VBSETALT|VBNOSCALE);
783
if (newtype=="double")
784
err=cube.convert_type(vb_double,VBSETALT|VBNOSCALE);
787
printf("[E] vbimagemunge: error converting datatype\n");
794
vbreturn op_removesmallregions(Cube &cube,tokenlist &)
800
vbreturn op_add(Cube &cube,tokenlist &args)
803
if (vb_fileexists(args[1])) {
805
if (tmp.ReadFile(args[1])) {
806
printf("[E] vbimagemunge: couldn't read file %s\n",args(1));
812
cube+=strtod(args[1]);
816
vbreturn op_sub(Cube &cube,tokenlist &args)
819
if (vb_fileexists(args[1])) {
821
if (tmp.ReadFile(args[1])) {
822
printf("[E] vbimagemunge: couldn't read file %s\n",args(1));
828
cube-=strtod(args[1]);
832
vbreturn op_mult(Cube &cube,tokenlist &args)
835
if (vb_fileexists(args[1])) {
837
if (tmp.ReadFile(args[1])) {
838
printf("[E] vbimagemunge: couldn't read file %s\n",args(1));
844
cube*=strtod(args[1]);
848
vbreturn op_div(Cube &cube,tokenlist &args)
851
if (vb_fileexists(args[1])) {
853
if (tmp.ReadFile(args[1])) {
854
printf("[E] vbimagemunge: couldn't read file %s\n",args(1));
860
cube/=strtod(args[1]);
864
vbreturn op_nminus(Cube &cube,tokenlist &args)
867
if (vb_fileexists(args[1])) {
869
if (tmp.ReadFile(args[1])) {
870
printf("[E] vbimagemunge: couldn't read file %s\n",args(1));
876
cube-=strtod(args[1]);
882
vbreturn op_random01(Cube &cube,tokenlist &)
889
vbreturn op_write(Cube &cube,tokenlist &args)
892
if (cube.WriteFile(args[1]))
893
printf("[E] vbimagemunge: error writing file %s\n",args(1));
895
printf("[I] vbimagemunge: wrote file %s\n",args(1));
899
vbreturn op_writeprefixed(Cube &cube,tokenlist &args)
902
string xd=xdirname(cube.GetFileName());
903
string xf=xfilename(cube.GetFileName());
904
string fname=xd+"/"+args[1]+xf;
907
if (cube.WriteFile(fname))
908
printf("[E] vbimagemunge: error writing file %s\n",fname.c_str());
910
printf("[I] vbimagemunge: wrote file %s\n",fname.c_str());
914
vbreturn op_regioninfo(Cube &cube,tokenlist &)
917
cube_printregioninfo(cube);
921
vbreturn op_remap(Cube &cube,tokenlist &args)
925
fs.open(args(1),ios::in);
927
printf("[E] vbimagemunge: couldn't read map file %s\n",args(1));
930
const short bufsz=1024;
932
map<int,float> mymap;
937
fs.getline(buf,bufsz);
938
if (fs.gcount()==0) break;
939
mapargs.ParseLine(buf);
940
if (mapargs.size()!=2) continue;
941
fromval=strtol(mapargs[0]);
942
toval=strtod(mapargs[1]);
943
if (mymap.count(fromval)) {
944
printf("[E] vbimagemunge: duplicate value %d found in map %s\n",
949
mymap[fromval]=toval;
953
// now build the new cube
955
newcube.SetVolume(cube.dimx,cube.dimy,cube.dimz,vb_float);
956
int mappedcnt=0,unmappedcnt=0;
957
for (int i=0; i<cube.dimx; i++) {
958
for (int j=0; j<cube.dimy; j++) {
959
for (int k=0; k<cube.dimz; k++) {
960
int key=cube.getValue<int32>(i,j,k);
961
if (mymap.count(key)) {
962
newcube.SetValue(i,j,k,mymap[key]);
966
newcube.SetValue(i,j,k,key);
973
printf("[I] vbimagemunge: applied map file %s (%d voxels mapped, %d unmapped)\n",
974
args(1),mappedcnt,unmappedcnt);
978
vbreturn op_info(Cube &cube,tokenlist &)
981
printf("[I] cube %s: min=%f max=%f infs/nans=%d\n",
982
cube.GetFileName().c_str(),
983
cube.get_minimum(),cube.get_maximum(),(int)cube.get_nonfinites());
987
// procfns for combining operations
989
vbreturn op_sum(Cube &cube,tokenlist &)
996
vbreturn op_product(Cube &cube,tokenlist &)
1003
vbreturn op_multi(Cube &cube,tokenlist &)
1016
vbreturn op_union(Cube &cube,tokenlist &)
1020
mycube.unionmask(cube);
1024
vbreturn op_intersect(Cube &cube,tokenlist &)
1027
mycube.intersect(cube);
1031
vbreturn op_count(Cube &cube,tokenlist &)
1039
// finishfns for combining operations
1041
vbreturn op_overlapmask(list <Cube> &cubelist,tokenlist &)
1043
if (cubelist.size()!=2 && cubelist.size()!=3) {
1044
printf("[E] vbimagemunge: overlapmask can only work with 2 or 3 input masks\n");
1048
cc1=cubelist.begin();
1053
cc1->filename=xsetextension(xfilename(cc1->filename),"");
1054
cc2->filename=xsetextension(xfilename(cc2->filename),"");
1055
if (cubelist.size()==3) {
1057
cc3->filename=xsetextension(xfilename(cc3->filename),"");
1064
for (CUBI cc=++(cubelist.begin()); cc!=cubelist.end(); cc++) {
1065
// for (size_t j=1; j<cubelist.size(); j++) {
1066
if (cc->dimx!=dimx || cc->dimy!=dimy || cc->dimz!=dimz) {
1067
printf("[E] vbimagemunge: overlapmask requires masks of matching dimensions\n");
1073
outmask.SetVolume(dimx,dimy,dimz,vb_byte);
1075
for (int i=0; i<dimx*dimy*dimz; i++) {
1077
if (cc1->testValue(i)) val+=1;
1078
if (cc2->testValue(i)) val+=2;
1079
if (cubelist.size()==3)
1080
if (cc3->testValue(i)) val+=4;
1081
outmask.setValue<char>(i,val);
1083
outmask.AddHeader((string)"vb_maskspec: 1 255 0 0 "+cc1->filename);
1084
outmask.AddHeader((string)"vb_maskspec: 2 0 0 255 "+cc2->filename);
1085
outmask.AddHeader((string)"vb_maskspec: 3 170 0 170 \""+cc1->filename+"/"+cc2->filename+"\"");
1086
if (cubelist.size()==3) {
1087
outmask.AddHeader((string)"vb_maskspec: 4 220 220 0 "+cc3->filename);
1088
outmask.AddHeader((string)"vb_maskspec: 5 255 140 0 \""+cc1->filename+"/"+cc3->filename+"\"");
1089
outmask.AddHeader((string)"vb_maskspec: 6 0 255 0 \""+cc2->filename+"/"+cc3->filename+"\"");
1090
outmask.AddHeader((string)"vb_maskspec: 7 140 140 80\""+cc1->filename+"/"+cc2->filename+"/"+cc3->filename+"\"");
1093
cubelist.push_back(outmask);
1097
vbreturn op_null(list <Cube> &cubelist,tokenlist &)
1100
cubelist.push_back(mycube);
1104
vbreturn op_scale(list<Cube> &cubelist,tokenlist &)
1106
if (cubelist.size()==0)
1108
mycube/=cubelist.size();
1110
cubelist.push_back(mycube);
1114
vbreturn op_select(list<Cube> &cubelist,tokenlist &args)
1116
selectcubes(cubelist,strtol(args[1]));
1120
vbreturn op_include(list<Cube> &cubelist,tokenlist &args)
1122
vector<int> nlist=numberlist(args[1]);
1123
bool mymap[cubelist.size()];
1124
memset(mymap,0,cubelist.size()*sizeof(bool));
1125
for (size_t i=0; i<nlist.size(); i++)
1126
if (nlist[i]<(int)cubelist.size())
1128
list<Cube>::iterator cc=cubelist.begin();
1129
int osize=cubelist.size();
1130
for (int i=0; i<osize; i++) {
1132
cc=cubelist.erase(cc);
1139
vbreturn op_exclude(list<Cube> &cubelist,tokenlist &args)
1141
vector<int> nlist=numberlist(args[1]);
1142
bool mymap[cubelist.size()];
1143
memset(mymap,0,cubelist.size()*sizeof(bool));
1144
for (size_t i=0; i<nlist.size(); i++)
1145
if (nlist[i]<(int)cubelist.size())
1147
list<Cube>::iterator cc=cubelist.begin();
1148
int osize=cubelist.size();
1149
for (int i=0; i<osize; i++) {
1151
cc=cubelist.erase(cc);
1158
vbreturn op_write4d(list<Cube> &cubelist,tokenlist &args)
1160
if (cubelist.size()==0)
1163
Cube *cb=&(cubelist.front());
1164
ts.SetVolume(cb->dimx,cb->dimy,cb->dimz,cubelist.size(),cb->datatype);
1166
for (CUBI cc=cubelist.begin(); cc!=cubelist.end(); cc++) {
1168
ts.SetCube(ind++,*cc);
1171
if (ts.WriteFile(args[1]))
1172
printf("[E] vbimagemunge: couldn't write file %s\n",args[1].c_str());
1174
printf("[I] vbimagemunge: wrote file %s\n",args[1].c_str());
1181
// non-combining ones are easy
1182
oplist["smoothvox"]=imageop("smoothvox",3,op_smoothvox);
1183
oplist["smoothmm"]=imageop("smoothmm",3,op_smoothmm);
1184
oplist["thresh"]=imageop("thresh",1,op_thresh);
1185
oplist["rotate"]=imageop("rotate",3,op_rotate);
1186
oplist["regionat"]=imageop("regionat",3,op_regionat);
1187
oplist["threshabs"]=imageop("threshabs",1,op_threshabs);
1188
oplist["cutoff"]=imageop("cutoff",1,op_cutoff);
1189
oplist["invert"]=imageop("invert",0,op_invert);
1190
oplist["splitregions"]=imageop("splitregions",1,op_splitregions);
1192
oplist["flipx"]=imageop("flipx",0,op_flipx);
1193
oplist["flipy"]=imageop("flipy",0,op_flipy);
1194
oplist["flipz"]=imageop("flipz",0,op_flipz);
1195
oplist["zeroleft"]=imageop("zeroleft",0,op_zeroleft);
1196
oplist["zeroright"]=imageop("zeroright",0,op_zeroright);
1197
oplist["bigendian"]=imageop("bigendian",0,op_bigendian);
1198
oplist["littleendian"]=imageop("zeroright",0,op_littleendian);
1199
oplist["byteswap"]=imageop("zeroright",0,op_byteswap);
1201
oplist["orient"]=imageop("",99,op_orient);
1202
oplist["quantize"]=imageop("quantize",1,op_quantize);
1203
oplist["remap"]=imageop("remap",1,op_remap);
1204
oplist["combine"]=imageop("combine",6,op_combine);
1205
oplist["convert"]=imageop("convert",1,op_convert);
1206
oplist["removesmallregions"]=imageop("removesmallregions",1,op_removesmallregions);
1207
oplist["add"]=imageop("add",1,op_add);
1208
oplist["sub"]=imageop("sub",1,op_sub);
1209
oplist["mult"]=imageop("mult",1,op_mult);
1210
oplist["div"]=imageop("div",1,op_div);
1211
oplist["nminus"]=imageop("nminus",1,op_nminus);
1212
oplist["random01"]=imageop("random01",0,op_random01);
1213
oplist["write"]=imageop("write",1,op_write);
1214
oplist["writeprefixed"]=imageop("writeprefixed",1,op_writeprefixed);
1215
oplist["regioninfo"]=imageop("regioninfo",0,op_regioninfo);
1216
oplist["info"]=imageop("info",0,op_info);
1218
// combining operators take a little more work
1221
// by doing it this way, all the cubes are in core before we select.
1222
// if we create an initfn that stores the inclusion map and a procfn
1223
// that invalidates the data for the ones we don't want, we can be
1224
// tighter about memory
1226
tmp=imageop("overlapmask",0,NULL);
1227
tmp.finishfn=op_overlapmask;
1228
oplist[tmp.name]=tmp;
1230
tmp=imageop("select",1,NULL);
1231
tmp.finishfn=op_select;
1232
oplist[tmp.name]=tmp;
1234
tmp=imageop("include",1,NULL);
1235
tmp.finishfn=op_include;
1236
oplist[tmp.name]=tmp;
1238
tmp=imageop("exclude",1,NULL);
1239
tmp.finishfn=op_exclude;
1240
oplist[tmp.name]=tmp;
1242
tmp=imageop("sum",0,NULL);
1243
tmp.initfn=op_allzeros;
1245
tmp.finishfn=op_null;
1246
oplist[tmp.name]=tmp;
1248
tmp=imageop("average",0,NULL);
1249
tmp.initfn=op_allzeros;
1251
tmp.finishfn=op_scale;
1252
oplist[tmp.name]=tmp;
1254
tmp=imageop("multi",0,NULL);
1255
tmp.initfn=op_shortzeros;
1256
tmp.procfn=op_multi;
1257
tmp.finishfn=op_null;
1258
oplist[tmp.name]=tmp;
1260
tmp=imageop("union",0,NULL);
1261
tmp.initfn=op_shortzeros;
1262
tmp.procfn=op_union;
1263
tmp.finishfn=op_null;
1264
oplist[tmp.name]=tmp;
1266
tmp=imageop("intersect",0,NULL);
1267
tmp.initfn=op_allones;
1268
tmp.procfn=op_intersect;
1269
tmp.finishfn=op_null;
1270
oplist[tmp.name]=tmp;
1272
tmp=imageop("count",0,NULL);
1273
tmp.initfn=op_shortzeros;
1274
tmp.procfn=op_count;
1275
tmp.finishfn=op_null;
1276
oplist[tmp.name]=tmp;
1278
tmp=imageop("product",0,NULL);
1279
tmp.initfn=op_allones;
1280
tmp.procfn=op_product;
1281
tmp.finishfn=op_null;
1282
oplist[tmp.name]=tmp;
1284
tmp=imageop("write4d",1,NULL);
1285
tmp.finishfn=op_write4d;
1286
oplist[tmp.name]=tmp;
1290
void reallyload(Cube &cube)
1292
if (cube.data) return;
1293
if (cube.ReadData(cube.GetFileName()))
1294
printf("[E] vbimagemunge: error reading file %s\n",cube.GetFileName().c_str());
1300
cout << boost::format(myhelp) % vbversion;
1304
vbimagemunge_version()
1306
printf("VoxBo vbimagemunge (v%s)\n",vbversion.c_str());