예제 #1
0
int		main(int argc, char *argv[])
{
  int		option,
  		ok = 1,
		usage = 0,
		dim = 2,
		section = 0,
		timer = 0;
  double	yaw = 0.0,
  		pitch = 0.0,
		roll =  0.0,
		dist = 0.0,
		scale = 1.0;
  WlzPixelV	bgdV;
  WlzGreyType	gType;
  WlzDVertex3	up,
  		fixed;
  WlzInterpolationType interp = WLZ_INTERPOLATION_NEAREST;
  WlzThreeDViewMode mode = WLZ_UP_IS_UP_MODE;
  WlzErrorNum	errNum = WLZ_ERR_NONE;
  FILE		*fP = NULL;
  WlzObject	*inObj = NULL,
  		*tlObj = NULL;
  char		*inFileStr,
		*outFileStr,
  		*secFileStr;
  struct timeval times[3];
  const char	*errMsg;
  const size_t	tlSz = 4096;
  static char	optList[] = "hsto:S:",
  		inFileStrDef[] = "-";

  opterr = 0;
  outFileStr = NULL;
  inFileStr = inFileStrDef;
  secFileStr = inFileStrDef;
  gType = WLZ_GREY_UBYTE;
  bgdV.type = WLZ_GREY_UBYTE;
  bgdV.v.ubv = 0;
  up.vtX = up.vtY = 0.0; up.vtZ = -1.0;
  fixed.vtX = fixed.vtY = fixed.vtZ = 0.0;
  while(ok && ((option = getopt(argc, argv, optList)) != -1))
  {
    switch(option)
    {
      case 'o':
        outFileStr = optarg;
	break;
      case 's':
        section = 1;
	break;
      case 'S':
        secFileStr = optarg;
	break;
      case 't':
        timer = 1;
	break;
      case 'h': /* FALLTHROUGH */
      default:
	usage = 1;
	break;
    }
  }
  if((usage == 0) && (optind < argc))
  {
    if((optind + 1) != argc)
    {
      usage = 1;
    }
    else
    {
      inFileStr = *(argv + optind);
    }
  }
  ok = !usage;
  if(ok)
  {
    fP = NULL;
    errNum = WLZ_ERR_READ_EOF;
    if(((fP = (strcmp(inFileStr, "-")? fopen(inFileStr, "r"):
                                       stdin)) == NULL) ||
       ((inObj= WlzAssignObject(WlzReadObj(fP, &errNum), NULL)) == NULL))
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: Failed to read object from file %s (%s).\n",
		     *argv, inFileStr, errMsg);
    }
    if(fP && strcmp(inFileStr, "-"))
    {
      (void )fclose(fP);
    }
  }
  if(ok)
  {
    if(inObj == NULL)
    {
      errNum = WLZ_ERR_OBJECT_NULL;
    }
    else if(inObj->domain.core == NULL)
    {
      errNum = WLZ_ERR_DOMAIN_NULL;
    }
    else
    {
      switch(inObj->type)
      {
        case WLZ_2D_DOMAINOBJ:
	  dim = 2;
	  break;
        case WLZ_3D_DOMAINOBJ:
	  dim = 3;
	  break;
	default:
	  errNum = WLZ_ERR_OBJECT_TYPE;
	  break;
      }
    }
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: invalid object read from file %s (%s).\n",
		     *argv, inFileStr, errMsg);
    }
  }
  if(ok)
  {
    if(timer)
    {
      gettimeofday(times + 0, NULL); 
    }
    tlObj = WlzMakeTiledValuesFromObj(inObj, tlSz, 1, gType, bgdV, &errNum);
    if(timer)
    {
      gettimeofday(times + 1, NULL); 
      timersub(times + 1, times + 0, times + 2);
      (void )fprintf(stderr,
                     "%s: Elapsed time for WlzMakeTiledValuesFromObj() %gs\n",
                     *argv,
		     times[2].tv_sec + (0.000001 * times[2].tv_usec));

    }
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: Failed to create object with tiled values (%s).\n",
		     *argv, errMsg);
    }
  }
  if(ok && (outFileStr != NULL))
  {
    if(((fP = (strcmp(outFileStr, "-")? fopen(outFileStr, "w"):
                                        stdout)) == NULL) ||
       ((errNum = WlzWriteObj(fP, tlObj)) != WLZ_ERR_NONE))
    
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
		     "%s: Failed to write tiled object to file %s (%s).\n",
		     *argv, outFileStr, errMsg);
    }
  }
  if(ok && section && (dim == 3))
  {
    WlzObject 	*secObj = NULL;
    WlzThreeDViewStruct *view = NULL;

    view = WlzMake3DViewStruct(WLZ_3D_VIEW_STRUCT, &errNum);
    if(errNum == WLZ_ERR_NONE)
    {
      view->theta = yaw * WLZ_M_PI / 180.0;
      view->phi = pitch * WLZ_M_PI / 180.0;
      view->zeta = roll * WLZ_M_PI / 180.0;
      view->dist = dist;
      view->fixed = fixed;
      view->up = up;
      view->view_mode = mode;
      view->scale = scale;
      errNum = WlzInit3DViewStruct(view, tlObj);
    }
    if(errNum == WLZ_ERR_NONE)
    {
      if(timer)
      {
	gettimeofday(times + 0, NULL); 
      }
      secObj = WlzGetSubSectionFromObject(tlObj, NULL, view, interp,
      					  NULL, &errNum);
      if(timer)
      {
	gettimeofday(times + 1, NULL); 
	timersub(times + 1, times + 0, times + 2);
	(void )fprintf(stderr,
		     "%s: Elapsed time for WlzGetSubSectionFromObject() %gs\n",
		     *argv,
		     times[2].tv_sec + (0.000001 * times[2].tv_usec));
      }
      fP = NULL;
      errNum = WLZ_ERR_WRITE_EOF;
      if(((fP = (strcmp(secFileStr, "-")? fopen(secFileStr, "w"):
					  stdout)) == NULL) ||
	 ((errNum = WlzWriteObj(fP, secObj)) != WLZ_ERR_NONE))
      {
	ok = 0;
	(void )WlzStringFromErrorNum(errNum, &errMsg);
	(void )fprintf(stderr,
		       "%s: Failed to write section object to file %s (%s).\n",
		       *argv, secFileStr, errMsg);
      }
      if(fP && strcmp(secFileStr, "-"))
      {
	(void )fclose(fP);
      }
    }
    (void )WlzFree3DViewStruct(view);
    (void )WlzFreeObj(secObj);
  }
  (void )WlzFreeObj(tlObj);
  (void )WlzFreeObj(inObj);
  if(usage)
  {
    (void )fprintf(stderr,
    "Usage: %s%s",
    *argv,
    " [-o<output object>] [-h] [-o <file>] [-s] [-S <file>] [-t]\n"
    "                  [<input object>]\n"
    "Copied the input object to an object with tiled values.\n"
    "Options:\n"
    "  -h  Prints this usage information.\n"
    "  -o  Output tiled object.\n"
    "  -s  Cut section from tiled object.\n"
    "  -S  Output file for section object.\n"
    "  -t  Output timing information.\n");
  }
  return(!ok);
}
예제 #2
0
int main(int	argc,
	 char	**argv)
{

  WlzObject	*obj = NULL, *nobj = NULL, *subDomain = NULL;
  FILE		*inFP = NULL, *outFP = NULL, *trFP = NULL, *bibFP = NULL;
  char		*outFile = NULL, *trFile = NULL, *bibFile = NULL;
  char 		optList[] = "ACLNTa:b:d:f:m:o:r:s:t:u:R:h";
  int		option;
  int		i,
  		j,
		iVal,
		timer = 0,
		voxRescale = 0,
  		allFlg = 0;
  double	dist=0.0, pitch=0.0, yaw=0.0, roll=0.0;
  double	scale=1.0;
  WlzDVertex3	fixed={0.0,0.0,0.0};
  WlzDVertex3	up={0.0,0.0,-1.0};
  WlzThreeDViewStruct	*viewStr=NULL;
  WlzThreeDViewMode mode=WLZ_UP_IS_UP_MODE;
  WlzInterpolationType interp = WLZ_INTERPOLATION_NEAREST;
  WlzErrorNum	errNum=WLZ_ERR_NONE;
  BibFileRecord	*bibfileRecord;
  BibFileError	bibFileErr;
  struct timeval times[3];
  char		*errMsg;
    
  /* additional defaults */
  outFile = "-";
  bibFile = NULL;
  trFile = NULL;
  subDomain = NULL;

  /* read the argument list and check for an input file */
  opterr = 0;
  while( (option = getopt(argc, argv, optList)) != EOF ){
    switch( option ){

    case 'A':
      allFlg = 1;
      break;

    case 'C':
      interp = WLZ_INTERPOLATION_CLASSIFY_1;
      break;

    case 'L':
      interp = WLZ_INTERPOLATION_LINEAR;
      break;

    case 'N':
      interp = WLZ_INTERPOLATION_NEAREST;
      break;

    case 'a':
      switch( sscanf(optarg, "%lg,%lg,%lg", &pitch, &yaw, &roll) ){
      default:
	usage(argv[0]);
	return 1;
      case 2:
	break;
      case 3:
	mode = WLZ_ZETA_MODE;
	break;
      }
      break;

    case 'b':
      bibFile = optarg;
      break;

    case 'd':
      if( sscanf(optarg, "%lg", &dist) < 1 ){
	usage(argv[0]);
	return 1;
      }
      break;

    case 'f':
      if( sscanf(optarg, "%lg,%lg,%lg", &(fixed.vtX), &(fixed.vtY),
		 &(fixed.vtZ)) < 3 ){
	usage(argv[0]);
	return 1;
      }
      break;

    case 'm':
      if( sscanf(optarg, "%d", &iVal) < 1 ){
	usage(argv[0]);
	return 1;
      }
      else if( mode != WLZ_ZETA_MODE ){
	switch( iVal ){
	default:
	  usage(argv[0]);
	  return 1;
	case 0:
	  mode = WLZ_UP_IS_UP_MODE;
	  break;
	case 1:
	  mode = WLZ_STATUE_MODE;
	  break;
	case 2:
	  mode = WLZ_ZETA_MODE;
	  break;
	}
      }
      break;

    case 'o':
      outFile = optarg;
      break;

    case 'r':
      if( sscanf(optarg, "%d", &voxRescale) < 1 ){
	usage(argv[0]);
	return 1;
      }
      break;

    case 's':
      if( sscanf(optarg, "%lg", &scale) < 1 ){
	usage(argv[0]);
	return 1;
      }
      break;

    case 'u':
      if( sscanf(optarg, "%lg,%lg,%lg", &(up.vtX), &(up.vtY),
		 &(up.vtZ)) < 3 ){
	usage(argv[0]);
	return 1;
      }
      break;

    case 'R':
      if((inFP = fopen(optarg, "rb"))){
	if((subDomain = WlzReadObj(inFP, &errNum)) == NULL){
	  fprintf(stderr, "%s: can't read sub-domain object %s\n", argv[0], optarg);
	  usage(argv[0]);
	  return 1;
	}
	fclose(inFP);
      }
      else {
	fprintf(stderr, "%s: can't open file %s\n", argv[0], optarg);
	usage(argv[0]);
	return 1;
      }
      break;

    case 'T':
      timer = 1;
      break;
    case 't':
      trFile = optarg;
      break;
    case 'h':
    default:
      usage(argv[0]);
      return 0;

    }
  }

  /* check input file/stream */
  inFP = stdin;
  if( optind < argc ){
    if( (inFP = fopen(*(argv+optind), "rb")) == NULL ){
      fprintf(stderr, "%s: can't open file %s\n", argv[0], *(argv+optind));
      usage(argv[0]);
      return 1;
    }
  }

  /* check output file/stream */
  if( allFlg ){
    if(strcmp(outFile, "-"))
    {
      /* strip any file extension */
      for(i=0, j=strlen(outFile); i < strlen(outFile); i++){
	if( outFile[i] == '.' ){
	  j = i;
	}
      }
      outFile[j] = '\0';
  }
    else {
      outFile = "plane";
    }
  }
  else {
    if(strcmp(outFile, "-"))
    {
      if((outFP = fopen(outFile, "wb")) == NULL)
      {
	errNum = WLZ_ERR_WRITE_EOF;
      }
    }
    else
    {
      outFP = stdout;
    }
  }

  /* create view structure */
  if((viewStr = WlzMake3DViewStruct(WLZ_3D_VIEW_STRUCT, &errNum)) != NULL){
    viewStr->theta = yaw * WLZ_M_PI / 180.0;
    viewStr->phi = pitch * WLZ_M_PI / 180.0;
    viewStr->zeta = roll * WLZ_M_PI / 180.0;
    viewStr->dist = dist;
    viewStr->fixed = fixed;
    viewStr->up = up;
    viewStr->view_mode = mode;
    viewStr->scale = scale;
    viewStr->voxelRescaleFlg = voxRescale;
  }

  if((errNum == WLZ_ERR_NONE) && (bibFile != NULL) && (trFile != NULL)) {
    fprintf(stderr, "%s: both bibfile and transform file specified.\n",
            argv[0]);
  }

  /* check bibfile - select first section parameters in the file */
  if((errNum == WLZ_ERR_NONE) && (bibFile != NULL)){
    if((bibFP = fopen(bibFile, "r")) != NULL){
      bibFileErr = BibFileRecordRead(&bibfileRecord, &errMsg, bibFP);
      while((bibFileErr == BIBFILE_ER_NONE) &&
	    (strncmp(bibfileRecord->name, "Wlz3DSectionViewParams", 22))){
	BibFileRecordFree(&bibfileRecord);
	bibFileErr = BibFileRecordRead(&bibfileRecord, &errMsg, bibFP);
      }
      fclose( bibFP );
      if( bibFileErr != BIBFILE_ER_NONE ){
	fprintf(stderr, "%s: error reading bibfile: %s\n", argv[0], errMsg);
	AlcFree((void *) errMsg);
	return 1;
      }
      WlzEffBibParse3DSectionViewParamsRecord(bibfileRecord, viewStr);
      BibFileRecordFree(&bibfileRecord);
    }
    else {
      fprintf(stderr, "%s: can't open parameter bibfile %s\n", argv[0],
	      bibFile);
      return 1;
    }
  }
  
  if((errNum == WLZ_ERR_NONE) && (trFile != NULL)){
    if((trFP = fopen(trFile, "r")) != NULL){
      WlzObject *trObj = NULL;

      if((trObj = WlzReadObj(trFP, &errNum)) != NULL) {
        if(trObj->type != WLZ_3D_VIEW_STRUCT) {
	  errNum = WLZ_ERR_OBJECT_TYPE;
	}
	else if (trObj->domain.core == NULL) {
	  errNum = WLZ_ERR_DOMAIN_NULL;
	}
	else {
	  WlzThreeDViewStruct *fViewStr;

	  fViewStr = trObj->domain.vs3d;
	  viewStr->theta = fViewStr->theta;
	  viewStr->phi = fViewStr->phi;
	  viewStr->zeta = fViewStr->zeta;
	  viewStr->dist = fViewStr->dist;
	  viewStr->fixed = fViewStr->fixed;
	  viewStr->up = fViewStr->up;
	  viewStr->view_mode = fViewStr->view_mode;
	  viewStr->scale = fViewStr->scale;
	  viewStr->voxelRescaleFlg = fViewStr->voxelRescaleFlg;
	}
	(void )WlzFreeObj(trObj);
      }
    }
    if(errNum != WLZ_ERR_NONE) {
      fprintf(stderr,
              "%s: error reading section view transform from %s.\n",
              argv[0], trFile);
      return 1;
    }
  }

  /* read objects and section if possible */
  while((errNum == WLZ_ERR_NONE) &&
        ((obj = WlzReadObj(inFP, &errNum)) != NULL))
  {
    obj = WlzAssignObject(obj, &errNum);
    switch( obj->type )
    {
      case WLZ_CONTOUR:
      case WLZ_3D_DOMAINOBJ:
	if(voxRescale && obj->domain.core)
	{
	  viewStr->voxelSize[0] = obj->domain.p->voxel_size[0];
	  viewStr->voxelSize[1] = obj->domain.p->voxel_size[1];
	  viewStr->voxelSize[2] = obj->domain.p->voxel_size[2];
	}
	WlzInit3DViewStruct(viewStr, obj);
	if(timer)
	{
	  gettimeofday(times + 0, NULL);
	}
	if( allFlg ){
	  /* loop through all possible planes */
	  for(i=WLZ_NINT(viewStr->minvals.vtZ), j=0;
	      i <= WLZ_NINT(viewStr->maxvals.vtZ); i++, j++){
	    viewStr->dist = i;
	    WlzInit3DViewStruct(viewStr, obj);
	    nobj = WlzGetSubSectionFromObject(obj, subDomain, viewStr, interp,
					      NULL, &errNum);
	    if( nobj != NULL){
	      char	fileBuf[256];
	      sprintf(fileBuf, "%s%06d.wlz", outFile, j);
	      if((outFP = fopen(fileBuf, "w")) != NULL){
		 WlzWriteObj(outFP, nobj);
		 fclose(outFP);
	      }
	    }
	    else {
	      return errNum;
	    }
	    WlzFreeObj(nobj);
	  }
	}
	else {
	  nobj = WlzGetSubSectionFromObject(obj, subDomain, viewStr, interp,
					    NULL, &errNum);
	  if( nobj != NULL){
	    WlzWriteObj(outFP, nobj);
	  }
	  else {
	    return errNum;
	  }
	  WlzFreeObj(nobj);
	}
	if(timer)
	{
	  gettimeofday(times + 1, NULL);
	  ALC_TIMERSUB(times + 1, times + 0, times + 2);
	  (void )fprintf(stderr,
	                 "%s: Elapsed time = %g\n",
			 *argv,
			 times[2].tv_sec + (0.000001 * times[2].tv_usec));
	}
	break;

      default:
	WlzWriteObj(outFP, obj);
	break;
    }

    WlzFreeObj(obj);
  }
  if(errNum == WLZ_ERR_READ_EOF)
  {
    errNum = WLZ_ERR_NONE;
  }

  if( viewStr ){
    WlzFree3DViewStruct(viewStr);
  }
  if( subDomain ){
    WlzFreeObj(subDomain);
  }

  return errNum;
}