⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segfile.c

📁 蒙特卡罗模拟光子成像C语言版,代码简洁专业
💻 C
字号:
/*********************************************************
        OPEN AND READ THE SEGMENTATION FILE

  Returns non-zero on error
    1 -> File error (can't open file, unexpected EOF, etc.)
    2 -> Invalid input parameter
    3 -> System error (out of memory, etc.)

*********************************************************/

/*
 * This file is part of tMCimg.
 * 
 * tMCimg is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "tMCimg.h"
#include "mccore.h"

#if (HAVE_ASSERT_H)
#include <assert.h>
#endif

int loadsegments(const struct Config *p, TISSUE ****tt)
{
  TISSUE ***tissueType, t;
  FILE *fp;
  int i, j, k;

#if (HAVE_ASSERT)
  assert(p != NULL);
  assert(tt != NULL);
#endif

  /* Initialize to NULL */
  *tt = NULL;

  if ((fp = fopen(p->segFile, "rb")) == NULL) 
    {
      fprintf(stderr, "ERROR: THe binary image file %s was not found!\n", 
              p->segFile);
      return 1;
    }

  rewind(fp);			  /* Check actual vs expected file sizes */
  fseek(fp, 0, SEEK_END);

  if (ftell(fp) != (long)(p->nxstep * p->nystep * p->nzstep * sizeof(TISSUE)))
    {
      fprintf(stderr, 
              "Size of segmentation file != nxstep*nystep*nzstep elements\n");
      return 1;
    } 
  else
    rewind(fp);

  if ((tissueType = 
       (TISSUE ***)alloc_3d_array(p->nxstep, p->nystep, 
                                  p->nzstep, sizeof(TISSUE))) == NULL) 
    {
      fprintf(stderr, "malloc() failed, can't allocate %d bytes\n",
              p->nxstep * p->nystep * p->nzstep * sizeof(TISSUE));

      return 3;
    }

  for (k = 0; k < p->nzstep; k++)
    {
      if (p->flags.matlab_order != 0)
        {
          /* Matlab stores data column [Y,X,Z], I use data row
           * [X,Y,Z] first.  Flip the data around as I read it in.
           */

          for (i = 0; i < p->nxstep; i++) 
            for (j = 0; j < p->nystep; j++) 
              {
                if (fread(&t, sizeof(TISSUE), 1, fp) != 1) 
                  { 
                    fprintf(stderr, "Unexpected end of segmentation file %s", 
                            p->segFile);
                    return 1;
                  }  
                else 
                  tissueType[i][j][k] = t;
              }
        }
      else                        /* Normal C ordering */
        {
          for (j = 0; j < p->nystep; j++) 
            for (i = 0; i < p->nxstep; i++) 
              if (fread(&t, sizeof(TISSUE), 1, fp) != 1) 
                { 
                  fprintf(stderr, "Unexpected end of segmentation file %s", 
                          p->segFile);
                  return 1;
                }  
              else 
                tissueType[i][j][k] = t;
        }

      /* Print "." as every plane is read to provide some feedback */

      printf(".");
      fflush(stdout);
    }

  printf("\n");

  fclose(fp);

  *tt = tissueType;

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -