Esempio n. 1
0
void
Extractor::Go()
{
  double val;

  for (int i=0; i<newcube.dimx; i++) {
    for (int j=0; j<newcube.dimy; j++) {
      for (int k=0; k<newcube.dimz; k++) {
	val=regioncode(i,j,k);
	if (xvflag==0 || val==xval)
	  newcube.SetValue(i,j,k,val);
      }
    }
  }
  newcube.WriteFile();

  return;
}
Esempio n. 2
0
int receive_3d (char *fifo, char *path, int dimx, int dimy, int dimz, int datatype)
{
	char fifo_from_idl [PATH_MAX];
	char fifo_to_idl [PATH_MAX];
	char line[200];
	int i;

	strcpy (fifo_from_idl, fifo );
	strcpy (fifo_to_idl, fifo );
	strcat (fifo_from_idl, "-");

	readfifopath = fifo_from_idl;
	return_path = fifo_to_idl;

	FILE *f = fopen (fifo_from_idl, "rb");
	if (!f) 
		complain(BADPATH);

	read_fifo = f;

#ifdef DEBUG
	fprintf (stderr, "Entered receive_3d\n");
	fflush (stderr);
#endif

	//----------------------------------------------
	// Check params
	//
	if (dimx <= 0 || dimy <= 0 || dimz <= 0)
		complain(ERROR_BADDIMS);
	if (datatype < 0)
		complain(ERROR_BADTYPE);

	//----------------------------------------------
	// Get data type & size.
	//
	VB_datatype dt2;
	int datasize=0;
	switch (datatype) {
	case 1:	dt2 = vb_byte; datasize=1; break;
	case 2:	dt2 = vb_short; datasize=2; break;
	case 3:	dt2 = vb_long; datasize=4; break;
	case 4:	dt2 = vb_float; datasize=sizeof(float); break;
	case 5:	dt2 = vb_double; datasize=sizeof(double); break;
	default:
                return ERROR_BADTYPE;
	}

	//----------------------------------------------
	// Get the user-header info.
	//
	vector<string> user_header;
	if (0 > readline(f, line, 199))
		complain(ERROR_EOF);
	int total_header_items = atoi(line);
	if (total_header_items < 0)
		complain(ERROR_BADHEADER);
	for (i=0; i < total_header_items; i++) {
		if (0 > readline(f, line, 199))
			complain(ERROR_EOF);
		user_header.push_back (string(line));
	}

	//----------------------------------------------
	// Create the Cube object & put the data in it.
	//
	Cube *c = new Cube(dimx,dimy,dimz,dt2);
	if (!c)
		complain(ERROR_NOMEM);

	//----------------------------------------------
	// Store the user header into the Cube object.
	//
	for (i=0; i < total_header_items; i++) {
		c->header.push_back (user_header[i]);
	}

	//----------------------------------------------
	// Now we process specific user header data
	// that we know have equivalents in the Tes object.
	//
	for (i=0; i < total_header_items; i++) {
		const char *s = user_header[i].c_str();
		char *s2 = s ? strchr(s, ':') : NULL;
		if (s2 && '*' == *s2)
			continue;
#ifdef DEBUG
		fprintf(stderr, "putdata got user header line: %s\n", s);
#endif
		if (!s2) s2 = s ? strchr (s, '=') : NULL;
		if (s2) {
			*s2++ = 0;
			if (!strcasecmp (s, "VoxSizes(xyz)")) {
				double x,y,z;
				if (3 == sscanf (s2, "%lg %lg %lg", &x,&y,&z)) {
					c->voxsize[0] = x;
					c->voxsize[1] = y;
					c->voxsize[2] = z;
				} else {
					WARNING("invalid voxsizes from user header.");
				}
			}
			else
			if (!strcasecmp (s, "Origin(xyz)")) {
				int x,y,z;
				if (3 == sscanf (s2, "%d %d %d", &x,&y,&z)) {
					c->origin[0] = x;
					c->origin[1] = y;
					c->origin[2] = z;
				} else {
					WARNING("invalid origin from user header.");
				}
			}
			else
			if (!strcasecmp (s, "Orientation")) {
				c->orient = s2;
			} 
			else
			if (!strcasecmp (s, "Scale")) {
				// float f = atof(s2);
				// c->scalefactor = f ? f : 1.0;
			} 
			else
			if (!strcasecmp (s, "ByteOrder")) {
				c->filebyteorder = atoi(s2)? ENDIAN_BIG : ENDIAN_LITTLE;
			}
		} 
	}

	//----------------------------------------------
	// Get the data
	//
	ulong chunk_size = dimx * dimy * dimz * datasize;
	unsigned char *chunk = (unsigned char*) malloc (chunk_size);
	if (!chunk) 
		complain(ERROR_NOMEM);
	if (chunk_size != fread (chunk,1,chunk_size,f))
		complain(ERROR_READFAIL);

	fclose (f);
	read_completed = true;

	c->data = chunk;

	//----------------------------------------------
	// Write the Cube to a file.
	//
	c->SetFileName (path);
#ifdef DEBUG
	fprintf(stderr, "Calling write cube to file %s\n", path);
#endif
	c->WriteFile ();

	//----------------------------------------------
	// Return the success code
	//
	f = fopen (fifo_to_idl, "wb");
	if (!f)
		return 0;
	fprintf (f, "0\n");
	fflush(f);
	fclose(f);

	delete c;

	return 0;
}
Esempio n. 3
0
int
main(int argc,char *argv[])
{
  if (argc==1) {
    vbconv_help();
    exit(0);
  }
  tokenlist args;
  string outfile;
  int floatflag=0,nanflag=0,extractflag=0;
  set<int> includeset,excludeset;
  list<string> filelist;
  VBFF nullff;

  args.Transfer(argc-1,argv+1);
  for (size_t i=0; i<args.size(); i++) {
    if (args[i]=="-f")
      floatflag=1;
    else if (args[i]=="-n")
      nanflag=1;
    else if (args[i]=="-i" && i<args.size()-1) {
      includeset=numberset(args[++i]);
      if (includeset.empty()) {
        cout << "[E] vbconv: invalid inclusion range specified with -i\n";
        exit(111);
      }
    }
    else if (args[i]=="-e" && i<args.size()-1) {
      excludeset=numberset(args[++i]);
      if (excludeset.empty()) {
        cout << "[E] vbconv: invalid exclusion range specified with -e\n";
        exit(111);
      }
    }
    else if (args[i]=="-x" && i<args.size()-1)
      extractflag=1;
    else if (args[i]=="-v") {
      vbconv_version();
      exit(0);
    }
    else if (args[i]=="-h") {
      vbconv_help();
      exit(0);
    }
    else if (args[i]=="-o" && i<args.size()-1)
      outfile=args[++i];
    else {
      filelist.push_back(args[i]);
    }
  }

  if (filelist.size()<1) {
    printf("[E] vbconv: requires at least one input file\n");
    exit(10);
  }
  // if there's no -o flag and exactly two files specified, convert
  // the first to the second
  if (filelist.size()==2 && outfile.size()==0) {
    outfile=filelist.back();
    filelist.pop_back();
  }
  if (outfile.size()==0) {
    printf("[E] vbconv: requires an output filename be provided\n");
    exit(11);
  }

  // multiple files, must be 3D/4D combination
  if (filelist.size()>1) {
    Tes newtes;
    ConvertMultiple(filelist,nanflag,floatflag,newtes);
    if (WriteTes(newtes,outfile,extractflag,floatflag,nanflag,includeset,excludeset))
      exit(223);
    exit(0);  // just in case
  }
  int err;
  // just one file, see what kind
  Cube cb;
  if (cb.ReadFile(filelist.front())==0) {
    cb.fileformat=nullff;
    if (floatflag && cb.datatype!=vb_float)
      cb.convert_type(vb_float);
    if (nanflag)
      cb.removenans();
    if ((err=cb.WriteFile(outfile))) {
      printf("[E] vbconv: error %d writing %s\n",err,outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote cube %s\n",outfile.c_str());
      exit(0);
    }
  }
  Tes ts;
  if (ts.ReadFile(filelist.front())==0) {
    ts.fileformat=nullff;
    if ((err=WriteTes(ts,outfile,extractflag,floatflag,nanflag,includeset,excludeset))) {
      printf("[E] vbconv: error %d writing %s\n",err,outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote 4D volume %s\n",outfile.c_str());
      exit(0);
    }
  }
  VB_Vector vv;
  if (vv.ReadFile(*filelist.begin())==0) {
    // vv.fileformat=nullff; // FIXME -- can we set 1d fileformat?
    if (vv.WriteFile(outfile)) {
      printf("[E] vbconv: error writing %s\n",outfile.c_str());
      exit(4);
    }
    else {
      printf("[I] vbconv: wrote vector %s\n",outfile.c_str());
      exit(0);
    }
  }
  printf("[E] vbconv: couldn't make sense of file %s\n",filelist.front().c_str());
  exit(100);
}
Esempio n. 4
0
int
main(int argc,char *argv[])
{
  tokenlist args;
  Tes *tes;
  Cube *cub;
  int ind=0;
  stringstream tmps;

  args.Transfer(argc-1,argv+1);

  if (args.size() == 0) {
    tes2cub_help();
    exit(0);
  }

  if (args.size() != 2 && args.size() != 3) {
    tes2cub_help();
    exit(5);
  }

  if (args.size() == 3)
    ind = strtol(args[2]);
  tes = new Tes();
  if (tes->ReadFile(args[0])) {
    tmps.str("");
    tmps << "tes2cub: couldn't read tes file " << args[0];
    printErrorMsg(VB_ERROR,tmps.str());
    exit(5);
  }
  if (!tes->data_valid) {
    tmps.str("");
    tmps << "tes2cub: tes file " << args[0] << "isn't valid.";
    printErrorMsg(VB_ERROR,tmps.str());
    exit(5);
  }
  if (ind > tes->dimt) {
    tmps.str("");
    tmps << "tes2cub: index (" << ind << ") is beyond the last image (" << tes->dimt << ").";
    printErrorMsg(VB_ERROR,tmps.str());
    exit(5);
  } 
  cub = new Cube((*tes)[ind]);
  if (!cub->data_valid) {
    tmps.str("");
    tmps << "tes2cub: error extracting the cube from the 4D file (shouldn't happen!).";
    printErrorMsg(VB_ERROR,tmps.str());
    exit(5);
  } 
  if (!cub->WriteFile(args[1])) {
    tmps.str("");
    tmps << "tes2cub: wrote cube " << ind << " to file " << args[1] << ".";
    printErrorMsg(VB_INFO,tmps.str());
    exit(0);
  }
  else {
    tmps.str("");
    tmps << "tes2cub: failed to write extracted cube to file " << args[1] << ".";
    printErrorMsg(VB_INFO,tmps.str());
    exit(5);
  }
  exit(0);
}