Esempio n. 1
0
/*!
* \return	Error code.
* \ingroup	Reconstruct
* \brief	Given a destination directory and file body and a 3D
*		Woolz object, the 3D object is and exploded into a series of 2D
*		section files (in the destination directory) each with a name
*		formed from the given file body and the source plane index. A
*		section list is also created in the directory using the given
*		file body together with the  '.bib' file extension.
* \param	dstDirStr		Destination directory.
* \param	dstBodyStr		Destination file body.
* \param	srcObj			Source 3D woolz object.
* \param	srcFName		Source file name, may be NULL.
* \param	srcFFormat		Source file format, may be
*					WLZEFF_FORMAT_NONE.
* \param	eMsg			Destinagtion pointer for messages.
*/
RecError	RecExplode3DObjToFile(char *dstDirStr, char *dstBodyStr,
				      WlzObject *srcObj, char *srcFName,
				      WlzEffFormat srcFFormat, char **eMsg)
{
  int		objIdx,
		planeIdx,
  		objVecCount,
		fileStrSpace;
  char		*fileStr = NULL,
  		*fileTemplateStr = NULL;
  FILE		*fP;
  WlzObject	**objVec = NULL;
  RecSectionList *secList = NULL;
  RecError	errFlag = REC_ERR_NONE;
  struct stat	statBuf;
  const char 	*errDirAccessStr = "Failed to access directory",
		*errDirCreateStr = "Failed to create directory",
  		*errExplodeStr = "Failed to explode 3D object",
  		*errWriteWoolzStr = "Failed to write 2D section file",
		*errListAppendStr = "Failed to build section list",
		*errWriteListStr = "Failed to write section list to file";
  const char	*errStr = NULL;

  REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_1),
	  ("RecExplode3DObjToFile FE 0x%lx 0x%lx 0x%lx 0x%lx %d 0x%lx\n",
	   (unsigned long )dstDirStr, (unsigned long )dstBodyStr,
	   (unsigned long )srcObj, (unsigned long )srcFName,
	   srcFFormat, (unsigned long )eMsg));
  if((dstDirStr == NULL) || (strlen(dstDirStr) == 0) ||
     (dstBodyStr == NULL) || (strlen(dstBodyStr) == 0) || 
     (srcObj == NULL) || (srcObj->type != WLZ_3D_DOMAINOBJ) ||
     (srcObj->domain.core == NULL))
  {
    errFlag = REC_ERR_FUNC;
  }
  if(errFlag == REC_ERR_NONE)       /* Check for/create destination directory */
  {
    if(stat(dstDirStr, &statBuf) == 0)			 /* Directory exists */
    {
#ifdef LINUX2
      if((statBuf.st_mode & (__S_IFDIR|__S_IREAD|__S_IWRITE|__S_IEXEC)) == 0)
#else /* LINUX2 */
      if((statBuf.st_mode & (S_IFDIR | S_IRWXU)) == 0)
#endif /* LINUX2 */
      {
	errStr = errDirAccessStr;
	errFlag = REC_ERR_WRITE;         /* Can't read/write/search directory */
      }
    }
    else
    {
      if(mkdir(dstDirStr, S_IRWXU))
      {
	errStr = errDirCreateStr;
	errFlag = REC_ERR_WRITE;
      }
      
    }
    REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2),
	    ("RecExplode3DObjToFile 01 %d\n",
	     (int )errFlag));
  }
  if(errFlag == REC_ERR_NONE) 	  	      /* Explode the 3D Woolz object */
  {
    if((errFlag = RecErrorFromWlz(
    		  WlzExplode3D(&objVecCount, &objVec, srcObj))) != REC_ERR_NONE)
    {
      errStr = errExplodeStr;
    }
    REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2),
	    ("RecExplode3DObjToFile 02 %d\n",
	     (int )errFlag));
  }
  if(errFlag == REC_ERR_NONE)                       /* Create a section list */
  {
    if(((secList = RecSecNewSectionList(NULL)) == NULL) ||
       ((secList->list = HGUDlpListCreate(NULL)) == NULL))
    {
      errFlag = REC_ERR_MALLOC;
    }
    else
    {
      secList->reconstruction.fileName = srcFName;
      secList->reconstruction.fileFormat = srcFFormat;
    }
    REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2),
	    ("RecExplode3DObjToFile 03 %d\n",
	     (int )errFlag));
  }
  if(errFlag == REC_ERR_NONE)     /* Write 2D files, build/write section list */
  {
    fP = NULL;
    objIdx = 0;
    planeIdx = srcObj->domain.p->plane1;
    fileStrSpace = (int )strlen(dstDirStr) + (int )strlen(dstBodyStr) + 64;
    if(((fileStr = AlcMalloc(sizeof(char) * fileStrSpace)) == NULL) ||
       ((fileTemplateStr = AlcMalloc(sizeof(char) * fileStrSpace)) == NULL))
    {
      errFlag = REC_ERR_MALLOC;
    }
    else
    {
      (void )sprintf(fileTemplateStr, "%s/%s", dstDirStr, dstBodyStr);
    }
    while((errFlag == REC_ERR_NONE) && (objIdx < objVecCount))
    {
      (void )sprintf(fileStr, "%s_%06d.wlz", fileTemplateStr, planeIdx);
      if((fP = fopen(fileStr, "w")) == NULL)
      {
	errStr = errWriteWoolzStr;
        errFlag = REC_ERR_WRITE;
      }
      if(errFlag == REC_ERR_NONE)
      {
	if((errFlag = RecErrorFromWlz(
		      WlzWriteObj(fP, *(objVec + objIdx)))) != REC_ERR_NONE)
	{
	  errStr = errWriteWoolzStr;
	}
        fclose(fP);
      }
      if(errFlag == REC_ERR_NONE)
      {
        errFlag = RecSecAppendListFromFiles(secList->list, NULL, &fileStr, 1,
					    planeIdx, 1);
        if(errFlag != REC_ERR_NONE)
	{
	  errStr = errListAppendStr;
	}
      }
      if(errFlag == REC_ERR_NONE)
      {
	++objIdx;
	++planeIdx;
      }
    }
    if(objVec && (objVecCount > 0))
    {
      for(objIdx = 0; objIdx < objVecCount; ++objIdx)
      {
	if(*(objVec + objIdx))
	{
	  (void )WlzFreeObj(*(objVec + objIdx));
	}
      }
      free(objVec);
    }
    if(errFlag == REC_ERR_NONE)
    {
      (void )sprintf(fileStr, "%s.bib", fileTemplateStr);
      if((fP = fopen(fileStr, "w")) == NULL)
      {
        errStr = errWriteListStr;
	errFlag = REC_ERR_WRITE;
      }
      else
      {
	errFlag = RecFileSecListWrite(fP, secList, objVecCount, eMsg);
	fclose(fP);
      }
    }
    if(fileStr)
    {
      free(fileStr);
    }
    if(fileTemplateStr)
    {
      free(fileTemplateStr);
    }
    if(secList)
    {
      HGUDlpListDestroy(secList->list);
      AlcFree(secList);
    }
    REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_2),
	    ("RecExplode3DObjToFile 04 %d\n",
	     (int )errFlag));
  }
  if((errFlag != REC_ERR_NONE) && eMsg && (*eMsg == NULL) && (errStr != NULL))
  {
    *eMsg = AlcStrDup(errStr);
  }
  REC_DBG((REC_DBG_3D|REC_DBG_LVL_FN|REC_DBG_LVL_1),
  	  ("RecExplode3DObjToFile FX %d\n",
	   (int )errFlag));
  return(errFlag);
}
int main(int	argc,
	 char	**argv)
{
  char 		optList[] = "hv";
  int		option;
  /* int		verboseFlg=0; */
  WlzErrorNum	errNum=WLZ_ERR_NONE;
  char		*tiffFile=NULL;
  WlzObject	*inObj, *outObj, *obj, **objVec;
  int		i, objVecCount;
  WlzGreyType	gType;

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

    case 'v':
      /* verboseFlg = 1; */
      break;

    case 'h':
    default:
      usage(argv[0]);
      return WLZ_ERR_UNSPECIFIED;
    }
  }

  if( optind < argc ){
    tiffFile = *(argv+optind);
  }
  else {
    usage(argv[0]);
    return WLZ_ERR_UNSPECIFIED;
  }

  /* read the TIFF file */
  if( (inObj = WlzAssignObject(WlzEffReadObj(NULL, tiffFile,
  			                     WLZEFF_FORMAT_TIFF, 0, 0, 0,
					     &errNum), NULL)) == NULL ){
    usage(argv[0]);
    return errNum;
  }

  /* if 2D then that is the shade file, else take max of 3D stack */
  switch( inObj->type ){
  case WLZ_2D_DOMAINOBJ:
    outObj = inObj;
    break;

  case WLZ_3D_DOMAINOBJ:
    if( (errNum = WlzExplode3D(&objVecCount, &objVec, inObj)) != WLZ_ERR_NONE ){
      usage(argv[0]);
      return errNum;
    }
    gType = WlzGreyTypeFromObj(objVec[0], &errNum);
    outObj = WlzAssignObject(objVec[0], NULL);
    for(i=1; i < objVecCount; i++){
      if( gType == WLZ_GREY_RGBA ){
	obj = WlzRGBAImageArithmetic(outObj, objVec[i], WLZ_BO_MAX, 0, &errNum);
      }
      else {
	obj = WlzImageArithmetic(outObj, objVec[i], WLZ_BO_MAX, 0, &errNum);
      }
      if( obj ){
	WlzFreeObj(outObj);
	outObj = WlzAssignObject(obj, &errNum);
      }
      else {
	break;
      }
    }
    
    WlzFreeObj(inObj);
    break;

  default:
    WlzFreeObj(inObj);
    errNum = WLZ_ERR_OBJECT_TYPE;
  }

  /* write shade object */
  if( errNum == WLZ_ERR_NONE ){
    WlzWriteObj(stdout, outObj);
    WlzFreeObj(outObj);
  }
  else {
    usage(argv[0]);
  }
  return errNum;
}
int main(int	argc,
	 char	**argv)
{
  char 		optList[] = "n:O:hv";
  int		option;
  /* int		verboseFlg=0; */
  int		rows=0, cols=0;
  int		overlap=50;
  int		rowShift, colShift;
  WlzErrorNum	errNum=WLZ_ERR_NONE;
  char		*tiffFile=NULL;
  WlzObject	*inObj=NULL, *outObj=NULL, *obj, **objVec;
  WlzCompoundArray	*outCmpnd;
  int		i, j, objVecCount, objIndx;

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

    case 'n':
      switch( sscanf(optarg, "%d,%d", &cols, &rows) ){
      default:
	usage(argv[0]);
	return 1;

      case 1:
	fprintf(stderr,
		"%s: number of rows not set and will be estimated\n",
		argv[0]);
	break;

      case 2:
	break;
      }
      break;

    case 'O':
      switch( sscanf(optarg, "%d", &overlap) ){
      default:
	fprintf(stderr,
		"%s: overlap not read", argv[0]);
	usage(argv[0]);
	return 1;

      case 1:
	break;
      }
      break;

    case 'v':
      /* verboseFlg = 1; */
      break;

    case 'h':
    default:
      usage(argv[0]);
      return WLZ_ERR_UNSPECIFIED;
    }
  }

  if( optind < argc ){
    tiffFile = *(argv+optind);
  }
  else {
    fprintf(stderr, "%s: input TIFF file required\n", argv[0]);
    usage(argv[0]);
    return WLZ_ERR_UNSPECIFIED;
  }

  /* read the TIFF file */
  if( (inObj = WlzAssignObject(
	 WlzEffReadObj(NULL, tiffFile, WLZEFF_FORMAT_TIFF,
	               0, 0, 0, &errNum), NULL)) == NULL ){
    usage(argv[0]);
    return errNum;
  }

  /* if 2D then there is only one patch, simply output as a compound object */
  switch( inObj->type ){
  case WLZ_2D_DOMAINOBJ:
    outCmpnd = WlzMakeCompoundArray(WLZ_COMPOUND_ARR_1, 3, 1, &inObj,
				    inObj->type, &errNum);
      outObj = (WlzObject *) outCmpnd;
    break;

  case WLZ_3D_DOMAINOBJ:
    if( (errNum = WlzExplode3D(&objVecCount, &objVec, inObj)) != WLZ_ERR_NONE ){
      usage(argv[0]);
      return errNum;
    }
    /* check if rows and columns plausible */
    if( cols && rows ){
      if( cols*rows != objVecCount ){
	fprintf(stderr,
		"%s: input columns and rows (%d,%d) does not match"
		"number of patches - %d\n Continuing anyway\n",
		argv[0], cols, rows, objVecCount);
      }
    }
    else if( cols ){
      rows = objVecCount / cols;
      if( cols*rows != objVecCount ){
	fprintf(stderr,
		"%s: input columns and estimated rows (%d,%d) does not match"
		"number of patches - %d\nContinuing anyway\n",
		argv[0], cols, rows, objVecCount);
      }
    }
    else {
      if( guessColsRows(objVecCount, &cols, &rows) ){
	fprintf(stderr,
		"%s: estimated cols and rows - (%d,%d)\nContinuing\n",
		argv[0], cols, rows);
      }
      else {
	fprintf(stderr, 
		"%s: cols,rows (%d, %d) estimated seems crazy,"
		"put values in explicitely\n",
		argv[0], cols, rows);
	usage(argv[0]);
	return WLZ_ERR_UNSPECIFIED;
      }
    }

    /* shift patch objects, assume patches all the same size */
    rowShift = (objVec[0]->domain.i->lastln - objVec[0]->domain.i->line1
		+ 1 - overlap);
    colShift = (objVec[0]->domain.i->lastkl - objVec[0]->domain.i->kol1
		+ 1 - overlap);
    objIndx = 0;
    for(j=0; j < rows; j++){
      for(i=0; i < cols; i++, objIndx++){
	if( objIndx < objVecCount ){
	  obj = WlzShiftObject(objVec[objIndx], 
			       colShift*i, rowShift*j, 0, &errNum);
	  WlzFreeObj(objVec[objIndx]);
	  objVec[objIndx] = WlzAssignObject(obj, &errNum);
	}
      }
    }

    /* make array */
    outCmpnd = WlzMakeCompoundArray(WLZ_COMPOUND_ARR_1, 2,
				    objVecCount, objVec,
				    objVec[0]->type, &errNum);
    outObj = (WlzObject *) outCmpnd;
    break;

  default:
    errNum = WLZ_ERR_OBJECT_TYPE;
  }
  WlzFreeObj(inObj);

  /* write compound object */
  if( errNum == WLZ_ERR_NONE ){
    WlzWriteObj(stdout, outObj);
    WlzFreeObj(outObj);
  }
  else {
    usage(argv[0]);
  }
  return errNum;
}