Exemple #1
0
/*!
 * Creates an @@Islice structure@ of the given [mode] and allocates a data
 * array appropriate for the size [xsize], [ysize].  Returns a pointer to the
 * slice or the NULL for error
 */
Islice *sliceCreate(int xsize, int ysize, int mode)
{
  Islice *s;
  size_t xysize = (size_t)xsize * (size_t)ysize;

  if (xysize / xsize != ysize) {
    b3dError(stderr, "ERROR: sliceCreate - slice is too large for a 32-bit computer.\n");
    return(NULL);
  }

  s = (Islice *)malloc(sizeof(Islice));
  if (!s)
    return(NULL);
     
  if (dataSizeForMode(mode, &s->dsize, &s->csize)) {
    b3dError(stderr, "ERROR: sliceCreate - Unsupported data mode %d.\n", mode);
    free(s);
    return(NULL);
  }

  s->xsize = xsize;
  s->ysize = ysize;
  s->mode  = mode;
  s->index = -1;
  s->data.b = (unsigned char *)malloc(xysize * s->dsize * s->csize);
  if (!s->data.b) {
    b3dError(stderr, "ERROR: sliceCreate - failed to allocate slice memory.\n");
    free(s);
    return(NULL);
  }

  return(s);
}
Exemple #2
0
int iiMRCLoadPCoord(ImodImageFile *inFile, IloadInfo *li, int nx, int ny,
                    int nz)
{
  int i;
  b3dUInt16 pcoordxy[2];
  b3dInt16 pcoordz;
  int offset=1024;
  int nread = nz;
  MrcHeader *hdr = (MrcHeader *)inFile->header;     
  int iflag = hdr->nreal;
  int nbytes = hdr->nint;
  int nextra = hdr->next;

  if (!iiMRCcheckPCoord(hdr))
    return 0;

  if (iflag & TILT_FLAG)
    offset += 2;

  if (nbytes * nz > nextra) {
    nread = nextra / nbytes;
    b3dError(stderr, "There are piece coordinates for only %d frames in"
            " the extra header\n", nread);
  }

  li->pcoords = (int *)malloc(sizeof(int) * 3 * nz);

  fseek(inFile->fp, offset, SEEK_SET);

  for (i = 0; i < nread; i++) {
    fread(pcoordxy, 2, 2, inFile->fp);
    fread(&pcoordz, 1, 2, inFile->fp);

    /* add swapping 10/2/00 */
    if (hdr->swapped) {
      mrc_swap_shorts((b3dInt16 *)pcoordxy, 2);
      mrc_swap_shorts(&pcoordz, 1);
    }
    if (ferror(inFile->fp)) {
      nread = i;
      b3dError(stderr, "Error reading piece coordinates from extra"
              " header after %d frames\n", i);
      break;
    }

    li->pcoords[(i*3)]   = pcoordxy[0];
    li->pcoords[(i*3)+1] = pcoordxy[1];
    li->pcoords[(i*3)+2] = pcoordz;

    offset = nbytes - 6;
    if (offset > 0)
      fseek(inFile->fp, offset, SEEK_CUR);
  }
  li->plist = nread;
  return(mrc_plist_proc(li, nx, ny, nz));
}
Exemple #3
0
/*!
 * Returns an autodoc index for [inFile].  Set [global] non-zero for accessing or storing 
 * information that applies to all volumes of a multi-volume HDF file or information
 * stored in sections other than in the ZValue collection.  If the file is not an
 * HDF file, it will return the existing value of the {adocIndex} member if it is
 * non-negative or if [openMdocOrNew] is 0; otherwise it will attempt to open a metadata
 * autodoc file by adding ".mdoc" to the filename if [openMdocOrNew] is positive or
 * it will open a new autodoc if [openMdocOrNew] is negative.  Returns -1 if there is 
 * no autodoc, or -2 for more serious errors.
 */
int iiGetAdocIndex(ImodImageFile *inFile, int global, int openMdocOrNew)
{
  char *mdocName;
  if (!inFile)
    return -2;
  if (inFile->file == IIFILE_HDF) {
    if (inFile->stackSetList || inFile->globalAdocIndex < 0 || !global)
      return inFile->adocIndex;
    return inFile->globalAdocIndex;
  }
  if (inFile->adocIndex >= 0 || !openMdocOrNew)
    return inFile->adocIndex;
  if (openMdocOrNew < 0) {
    inFile->adocIndex = AdocNew();
    if (inFile->adocIndex < 0)
      return -2;
  } else {
    mdocName = (char *)malloc(strlen(inFile->filename) + 6);
    if (!mdocName) {
      b3dError(stderr, "ERROR: iiGetAdocIndex - Allocating memory for filename\n");
      return -2;
    }
    sprintf(mdocName, "%s.mdoc", inFile->filename);
    inFile->adocIndex = AdocRead(mdocName);
    free(mdocName);
  }
  return inFile->adocIndex;
}
Exemple #4
0
/*!
 * Opens a new file whose name is in [filename] with the given [mode], which must
 * contain 'w'.  The type of file is specified by [fileKind], which can be either
 * IIFILE_MRC, IIFILE_TIFF, IIFILE_HDF, or IIFILE_DEFAULT for the type defined by calling
 * @@b3dutil.html#b3dOutputFileType@.  Adds the file to the list of opened files and
 * returns the opened file pointer.
 */
ImodImageFile *iiOpenNew(const char *filename, const char *mode, int fileKind)
{
  ImodImageFile *ofile;
  int err = 0;
  if (fileKind == IIFILE_DEFAULT)
    fileKind = b3dOutputFileType();
  /* printf("fk %d\n", fileKind); */
  if (!strstr(mode, "w")) {
    b3dError(stderr, "ERROR: iiOpenNew - File opening mode %s is not appropriate for a "
             "new image file\n");
    return NULL;
  }
      
  if ((ofile = iiNew()) == NULL) 
    return NULL;
  ofile->filename = strdup(filename);
  if (!ofile->filename) {
    err = 1;
  }
   
  if (!err && fileKind == IIFILE_MRC) {
    err = iiMRCopenNew(ofile, mode);
  } else if (!err && fileKind == IIFILE_HDF) {
    err = iiHDFopenNew(ofile, mode);
  } else if (!err && fileKind == IIFILE_TIFF) {
    err = tiffOpenNew(ofile);
    if (err)
      b3dError(stderr, "ERROR: iiOpenNew - Opening new TIFF file\n");
  } else if (!err) {
    b3dError(stderr, "ERROR: iiOpenNew - Cannot open new files with file format %d\n", 
             fileKind);
    err = 1;
  }
  
  if (!err) {
    ofile->file = fileKind;
    ofile->newFile = 1;
    strncpy(ofile->fmode, "rb+", 3);
    ofile->state = IISTATE_READY;
    if (!addToOpenedList(ofile))
      return ofile;
  }

  /* Fall through to here on any kind of error requiring deletion of the file */
  iiDelete(ofile);
  return NULL;
}
Exemple #5
0
/*
 * Functions for adding, removing, and looking up a file in the list
 */
static int addToOpenedList(ImodImageFile *iiFile)
{
  if (!sOpenedFiles)
    sOpenedFiles = ilistNew(sizeof(ImodImageFile *), 4);
  if (sOpenedFiles && !ilistAppend(sOpenedFiles, &iiFile))
    return 0;
  b3dError(stderr, "ERROR: iiOpen - Memory error adding new file to master list\n");
  return 1;
}
Exemple #6
0
// Check the file for being a readable QImage and set up parameters
int iiQImageCheck(ImodImageFile *inFile)
{
  QImage *image;
  if (!inFile) 
    return IIERR_BAD_CALL;
  image = new QImage(QString(inFile->filename));
  if (image->isNull()) {
    delete image;
    return IIERR_NOT_FORMAT;
  }

  if (image->depth() < 8) {
    delete image;
    b3dError(NULL, "%s is a recognized file type but data type is not "
             "supported\n", inFile->filename);
    return IIERR_NO_SUPPORT;
  }

  if(inFile->fp)
    fclose(inFile->fp);
  inFile->nx = image->width();
  inFile->ny = image->height();
  inFile->nz = 1;
  inFile->file = IIFILE_QIMAGE;
  inFile->type   = IITYPE_UBYTE;
  inFile->amean  = 128;
  inFile->amax   = 255;
  inFile->smax   = 255;

  // Grayscale images have depth 8 and color tables that are gray
  if (image->depth() == 8 && image->isGrayscale()) {
    inFile->format = IIFORMAT_LUMINANCE;
    inFile->readSectionByte = qimageReadSectionByte;
    inFile->readSectionFloat = qimageReadSectionFloat;
    inFile->mode   = MRC_MODE_BYTE;
  } else {

    // Otherwise images will be read as RGB
    inFile->format = IIFORMAT_RGB;
    inFile->mode   = MRC_MODE_RGB;
    inFile->readSection = qimageReadSection;
  }

  // Imitate TIFF for some of these behaviors - close file, assign image to
  // header but not fp here, assign to fp when reopen
  inFile->headerSize = 8;
  inFile->header = (char *)image;

  inFile->cleanUp = qimageClose;
  inFile->reopen = qimageReopen;
  inFile->close = qimageClose;
  inFile->fillMrcHeader = tiffFillMrcHeader;

  return 0;
}
Exemple #7
0
int iiMRCopenNew(ImodImageFile *inFile, const char *mode) 
{
  errno = 0;
  inFile->fp = fopen(inFile->filename, mode);
  if (!inFile->fp) {
    b3dError(stderr, "ERROR: iiMRCopenNew - Could not open %s%s%s\n" , inFile->filename,
             errno ? " - system message: " : "", errno ? strerror(errno) : "");
    return 1;
  }
  inFile->header = (char *)malloc(sizeof(MrcHeader));
  if (!inFile->header) {
    b3dError(stderr, "ERROR: iiMRCopenNew - Allocating MRC header\n");
    return 1;
  }
  mrc_head_new((MrcHeader *)inFile->header, 1, 1, 1, 0);
  iiMRCsetIOFuncs(inFile, 0);
  ((MrcHeader *)(inFile->header))->fp = inFile->fp;
  inFile->file = IIFILE_MRC;
  return 0;
}
Exemple #8
0
/*!
 * Creates a new image file structure and initialize it to default or
 * null values.  Returns 1 for error.
*/ 
ImodImageFile *iiNew()
{
  ImodImageFile *ofile = (ImodImageFile *)malloc(sizeof(ImodImageFile));
     
  if (!ofile) {
    b3dError(stderr, "ERROR: iiNew -Allocating new ImodImageFile structure\n");
    return NULL;
  }
  memset(ofile, 0, sizeof(ImodImageFile));
  ofile->xscale = ofile->yscale = ofile->zscale = 1.0f;
  ofile->slope  = 1.0f;
  ofile->smax   = 255;
  ofile->axis   = 3;
  ofile->mirrorFFT = 0;
  ofile->anyTiffPixSize = 0;
  ofile->format = IIFILE_UNKNOWN;
  ofile->fp     = NULL;
  ofile->readSection     = NULL;
  ofile->readSectionByte = NULL;
  ofile->readSectionUShort = NULL;
  ofile->readSectionFloat  = NULL;
  ofile->writeSection      = NULL;
  ofile->writeSectionFloat = NULL;
  ofile->fillMrcHeader = NULL;
  ofile->syncFromMrcHeader = NULL;
  ofile->writeHeader     = NULL;
  ofile->cleanUp         = NULL;
  ofile->reopen          = NULL;
  ofile->close           = NULL;
  ofile->writeSection    = NULL;
  ofile->colormap        = NULL;

  /* DNM 2/26/03: set upper right to -1 for later replacement */
  ofile->llx =  0;
  ofile->lly =  0;
  ofile->llz =  0;
  ofile->urx = -1;
  ofile->ury = -1;
  ofile->urz = -1;
  ofile->padLeft = 0;
  ofile->padRight = 0;
  ofile->nx = 0;
  ofile->ny = 0;
  ofile->nz = 0;
  ofile->lastWrittenZ = -1;
  ofile->adocIndex = -1;
  ofile->globalAdocIndex = -1;
  ofile->stackSetList = NULL;
  ofile->zToDataSetMap = NULL;
  ofile->datasetName = NULL;
  ofile->iiVolumes = NULL;
  ofile->numVolumes = 0;
  return(ofile);
}
Exemple #9
0
/*!
 * Opens the volume at index [volIndex] in a multi-volume HDF file, where the index is 
 * numbered from 0 and must be at least 1; i.e., this should not be called on the initial
 * volume accessed by iiFOpen.
 */
FILE *iiFOpenVolume(ImodImageFile *inFile, int volIndex)
{
  if (!inFile)
    return NULL;
  if (inFile->file != IIFILE_HDF || inFile->numVolumes < 2) {
    b3dError(stderr, "ERROR: iiFOpenVolume - Attempting to open a secondary volume for "
             "a non-HDF file or an HDF file with only a stack or one volume");
    return NULL;
  }
  if (volIndex < 1 || volIndex >= inFile->numVolumes) {
    b3dError(stderr, "ERROR: iiFOpenVolume - Requested volume index %d out of range\n",
             volIndex);
    return NULL;
  }
  if (iiReopen(inFile->iiVolumes[volIndex])) {
    b3dError(stderr, "ERROR: iiFOpenVolume - Error calling iiReopen on volume at "
             "index %d\n", volIndex); 
    return NULL;
  }
  return inFile->iiVolumes[volIndex]->fp;
}
Exemple #10
0
/*!
 * Initialize elements in the @@Islice structure@ [s] with the given size
 * [xsize], [ysize] and mode [mode], and sets the data member to [data]. 
 * Returns -1 for an undefined mode.
 */
int sliceInit(Islice *s, int xsize, int ysize, int mode, void *data)
{
  s->xsize = xsize;
  s->ysize = ysize;
  s->mode  = mode;
  s->data.b = data;
  if (dataSizeForMode(mode, &s->dsize, &s->csize)) {
    b3dError(stderr, "ERROR: sliceInit - Unsupported data mode %d.\n", mode);
    return(-1);
  }
  return(0);
}
Exemple #11
0
/*!
 * Gets the value of pixel at [x], [y] in slice [s] and puts it into the
 * value array [val].  Returns -1 and puts the slice mean into the first
 * element of [val] for a point out of bounds, and returns -1 if the 
 * slice mode is undefined.
 */
int sliceGetVal(Islice *s, int x, int y, Ival val)
{
  size_t index = x + ((size_t)y * (size_t)s->xsize);
     
  if ( (x < 0) || (y < 0) || (x >= s->xsize) || (y >= s->ysize)){
    val[0] = s->mean;
    return(-1);
  }

  val[1] = 0;
  switch (s->mode)
    {
    case MRC_MODE_BYTE:
      val[0] = (float)s->data.b[index];
      break;
    case MRC_MODE_SHORT:
      val[0] = (float)s->data.s[index];
      break;
    case MRC_MODE_USHORT:
      val[0] = (float)s->data.us[index];
      break;
    case MRC_MODE_FLOAT:
      val[0] = s->data.f[index];
      break;
    case MRC_MODE_COMPLEX_SHORT:
      index *= 2;
      val[0] = (float)s->data.s[index];
      val[1] = (float)s->data.s[index + 1];
      break;
    case MRC_MODE_COMPLEX_FLOAT:
      index *= 2;
      val[0] = s->data.f[index];
      val[1] = s->data.f[index + 1];
      break;
    case MRC_MODE_RGB:
      index *= 3;
      val[0] = (float)s->data.b[index++];
      val[1] = (float)s->data.b[index++];;
      val[2] = (float)s->data.b[index];
      break;
    case SLICE_MODE_MAX:
      index *= 3;
      val[0] = s->data.f[index++];
      val[1] = s->data.f[index++];
      val[2] = s->data.f[index];
      break;
    default:
      b3dError(stderr, "sliceGetVal: unknown mode.\n");
      return(-1);
    }
  return(0);
}
Exemple #12
0
/*!
 * For the HDF file or volume dataset in [inFile], sets the tile (chunk) sizes in X and Y
 * to [xSize] and [ySize], and the chunk size in Z to [zSize].  Pass 0 for [xSize] or 
 * [ySize] to avoid tiling in those dimensions.  [zSize] must be 1 or greater.  This 
 * function must be called before the volume or its header is written.  Returns 1 for 
 * error. 
 */
int iiSetChunkSizes(ImodImageFile *inFile, int xSize, int ySize, int zSize)
{
  if (!inFile || inFile->file != IIFILE_HDF || inFile->stackSetList) {
    b3dError(stderr, "ERROR: iiSetChunkSizes - Attempting to set chunk sizes "
             "for a non-HDF file or an HDF file with a stack in it\n");
    return 1;
  }
  if (inFile->datasetName) {
    b3dError(stderr, "ERROR: iiSetChunkSizes - The volume dataset properties have "
             "already been set and cannot be changed\n");
    return 1;
  }
  if (xSize < 0 || ySize < 0 || zSize <= 0) {
    b3dError(stderr, "ERROR: iiSetChunkSizes - X and Y chunk sizes must be non-negative "
             "and Z size must be positive\n");
    return 1;
  }
  inFile->tileSizeX = xSize;
  inFile->tileSizeY = ySize;
  inFile->zChunkSize = zSize;
  return 0;
}
Exemple #13
0
/*!
 * Puts the value(s) in [val] into the pixel at [x], [y] in slice [s].
 * Returns -1 if the point is out of bounds or the slice mode is undefined.
 */
int slicePutVal(Islice *s, int x, int y, Ival val)
{
  size_t index = x + ((size_t)y * (size_t)s->xsize);

  if ( (x < 0) || (y < 0) || (x >= s->xsize) || (y >= s->ysize))
    return(-1);

  switch (s->mode)
    {
    case MRC_MODE_BYTE:
      s->data.b[index] = (unsigned char)val[0];
      break;
    case MRC_MODE_SHORT:
      s->data.s[index] = (b3dInt16)val[0];
      break;
    case MRC_MODE_USHORT:
      s->data.us[index] = (b3dUInt16)val[0];
      break;
    case MRC_MODE_FLOAT:
      s->data.f[index] = val[0];
      break;
    case MRC_MODE_COMPLEX_SHORT:
      index *= 2;
      s->data.s[index]     = (b3dInt16)val[0];
      s->data.s[index + 1] = (b3dInt16)val[1];
      break;
    case MRC_MODE_COMPLEX_FLOAT:
      index *= 2;
      s->data.f[index]     = val[0];
      s->data.f[index + 1] = val[1];
      break;
    case MRC_MODE_RGB:
      index *= 3;
      s->data.b[index++] = val[0];
      s->data.b[index++] = val[1];
      s->data.b[index]   = val[2];
      break;
    case SLICE_MODE_MAX:
      index *= 3;
      s->data.f[index++] = val[0];
      s->data.f[index++] = val[1];
      s->data.f[index]   = val[2];
      break;
    default:
      b3dError(stderr, "slicePutVal: unknown mode.\n");
      return(-1);
    }
  return(0);
}
Exemple #14
0
/*!
 * Returns a slice with one Z plane of data at Z value [secno] from the file 
 * described by the @@mrcfiles.html#MrcHeader structure@ [hin].  
 * The file pointer in [hin] is used.  Bytes are swapped if necessary.
 * Returns NULL for errors.
 */
Islice *sliceReadFloat(MrcHeader *hin, int secno)
{
  Islice *slice;
  if (sliceModeIfReal(hin->mode) < 0) {
    b3dError(stderr, "ERROR: sliceReadFloat - file mode must be real");
    return NULL;
  }
  slice = sliceCreate(hin->nx, hin->ny, MRC_MODE_FLOAT);
  if (!slice)
    return NULL;
  if (mrcReadFloatSlice(slice->data.f, hin, secno)) {
    sliceFree(slice);
    return NULL;
  }
  return slice;
}
Exemple #15
0
/*!
 * Creates a new volume in an HDF file open on [inFile] that was not opened read-only and 
 * has volume datasets rather than a stack of single-image datasets.  Sets the 
 * {zChunkSize} member of [inFile] to 1 to ensure that a volume dataset will be created 
 * later, but you can still change chunk properties with @iiSetChunkSizes.  Returns the 
 * {fp} member of the new ImodImageFile structure, or NULL for error.
 */
FILE *iiFOpenNewVolume(ImodImageFile *inFile)
{
  ImodImageFile *iiFile;
  if (!inFile)
    return NULL;
  if (inFile->file != IIFILE_HDF || inFile->stackSetList) {
    b3dError(stderr, "ERROR: iiFOpenNewVolume - Attempting to create an additional "
             "volume for a non-HDF file or an HDF file with a stack in it\n");
    return NULL;
  }
  if (iiHDFopenNew(inFile, "wb+"))
    return NULL;
  iiFile = inFile->iiVolumes[inFile->numVolumes - 1];
  if (addToOpenedList(iiFile))
    return NULL;
  return iiFile->fp;
}
Exemple #16
0
/* The routine that does the work */
static int readWriteSection(ImodImageFile *inFile, char *buf, int inSection, 
                            iiSectionFunc func, const char *mess)
{
  int err;
  if (!func) {
    b3dError(stderr, "ERROR: iiRead/WriteSection - There is no function for %s this "
             "type of file\n", mess);
    return -1;
  }
  if (!inFile->fp){
    if (iiReopen(inFile))
      return -1;
  }
  iiChangeCallCount(1);
  err = func(inFile, buf, inSection);
  iiChangeCallCount(-1);
  return(err);
}
Exemple #17
0
/*!
 * Calculates the min, max, and mean of slice [s] and fills in the structure
 * members.  Returns -1 for an empty slice.
 */
int sliceMMM(Islice *s)
{
  int i, j;
  Ival val;
  double tsum, sum;
  float temp;

  /* DNM 3/29/01: need to take magnitude for complex, and set mean to val */
  sliceGetVal(s, 0, 0, val);
  if (s->mode == MRC_MODE_COMPLEX_FLOAT){
    temp = (val[0] * val[0]) + (val[1] * val[1]);
    val[0] = (float)sqrt(temp);
  }
  s->max = s->min = val[0];

  /* 5/23/05: Huh?  sum needs to be 0 */
  sum = 0.;

  if ((!s->xsize) || (!s->ysize)){
    b3dError(stderr, "sliceMMM: Warning, empty slice.\n");
    return(-1);
  }

  for (j = 0; j < s->ysize; j++) {
    tsum = 0.;
    for (i = 0; i < s->xsize; i++){
      sliceGetVal(s, i, j, val);
           
      if (s->mode == MRC_MODE_COMPLEX_FLOAT){
        temp = (val[0] * val[0]) + (val[1] * val[1]);
        val[0] = (float)sqrt(temp);
      }

      if (s->min > val[0])
        s->min = val[0];
      if (s->max < val[0])
        s->max = val[0];
      tsum += val[0];
    }
    sum += tsum;
  }
  s->mean = sum / (float)(s->xsize * s->ysize);
  return(0);
}
Exemple #18
0
/*!
 * Filters the FFT data in slice [sin] with a bandpass filter specified by
 * [low] and [high], in cycles/pixel (range 0 to 0.5).  The attenuation at
 * frequency {rad} is the product of 1/(1+(rad/low)**3) if [low] > 0 and 
 * 1/1+(high/rad)**3) if high > 0.  Returns -1 for mode not complex float.
 */
int mrc_bandpass_filter(struct MRCslice *sin, double low, double high)
{
  int i, j;
  double dist, mval, dx, dy, xscale, xadd, power = 3;
  Ival val;
     
  if (sin->mode != MRC_MODE_COMPLEX_FLOAT){
    b3dError(stderr, " mrc_band_filter: Only complex float mode.\n");
    return(-1);
  }

  /* Set up X coordinate scaling for odd or even (mirrored) FFTs */
  if (sin->xsize % 2) {
    xscale = 0.5 / (sin->xsize - 1.);
    xadd = 0.;
  } else {
    xscale = 1. / sin->xsize;
    xadd = -0.5;
  }

  for (j = 0; j < sin->ysize; j++)
    for(i = 0; i < sin->xsize; i++){
      dx = xscale * i + xadd;

      dy = (float)j / sin->ysize - 0.5;
      dist = sqrt(dx *dx + dy * dy);
      if (low > 0.) {
        if (dist < 0.00001)
          mval = 0;
        else
          mval = 1 / (1 + pow(low / dist, power));
      } else 
        mval = 1.0;
           
      if (high > 0.0)
        mval *= 1 / (1 + pow(dist / high, power));

      sliceGetVal(sin, i, j, val);
      val[0] *= mval;
      val[1] *= mval;
      slicePutVal(sin, i, j, val);
    }
  return(0);
}
Exemple #19
0
static int writeSection(ImodImageFile *inFile, char *buf, int inSection, int asFloat) 
{
  int err;
  IloadInfo li;
  MrcHeader *h = (MrcHeader *)inFile->header;

  iiMRCsetLoadInfo(inFile, &li);
  h->fp = inFile->fp;
  if (inFile->axis != 3) {
    b3dError(stderr, "ERROR: iiMRCwriteSection - attempting to write Y slices\n");
    return 1;
  }
  iiChangeCallCount(1);
  if (asFloat)
    err = mrcWriteZFloat(h, &li, (b3dFloat *)buf, inSection);
  else
    err = mrcWriteZ(h, &li, (unsigned char *)buf, inSection);
  iiChangeCallCount(-1);
  return err;
}
Exemple #20
0
int iiMRCCheck(ImodImageFile *iif)
{
  FILE *fp;
  MrcHeader *hdr;
  int err;

  if (!iif) 
    return IIERR_BAD_CALL;
  fp = iif->fp;
  if (!fp) 
    return IIERR_BAD_CALL;

  hdr = (MrcHeader *)malloc(sizeof(MrcHeader));
  if (!hdr) {
    b3dError(stderr, "ERROR: iiMRCCheck - getting memory for header\n");
    return IIERR_MEMORY_ERR;
  }

  if ((err = mrc_head_read(fp, hdr))){
    free(hdr);
    if (err < 0)
      return IIERR_IO_ERROR;
    return IIERR_NOT_FORMAT;
  }

  iif->header = (char *)hdr;
  iif->file = IIFILE_MRC;
  iiMRCmodeToFormatType(iif, hdr->mode, hdr->bytesSigned);
  iiSyncFromMrcHeader(iif, hdr);
  iif->smin  = iif->amin;
  iif->smax  = iif->amax;

  iif->hasPieceCoords = iiMRCcheckPCoord(hdr);

  iiMRCsetIOFuncs(iif, 0);
  return(0);
}
Exemple #21
0
int iiMRCLoadPCoord(ImodImageFile *inFile, struct LoadInfo *li, int nx, int ny,
                    int nz)
{
  int i;
  b3dUInt16 pcoordxy[2];
  b3dInt16 pcoordz;
  int extra_bytes[32];
  int extratot = 0;
  int offset=1024;
  int nread = nz;
  MrcHeader *hdr = (MrcHeader *)inFile->header;     
  int iflag = hdr->nreal;
  int nbytes = hdr->nint;
  int nextra = hdr->next;
  int flag_count;
  if(!nextra || !(iflag & MONTAGE_FLAG))
    return 0;

   b3dHeaderItemBytes(&flag_count, &extra_bytes[0]);

  /* DNM 12/10/01: as partial protection against mistaking other entries
     for montage information, at least make sure that the total bytes
     implied by the bits in the flag equals the nint entry.  Also
     reject deltavision files */
  /* DNM 2/3/02: make sure nreal also does not have bits beyond the flags */
  for (i = 0; i < flag_count; i++)
    if (iflag & (1 << i))
      extratot += extra_bytes[i];

  if (extratot != nbytes || hdr->creatid == -16224 || 
      iflag >= (1 << flag_count))
    return 0;

  if (iflag & TILT_FLAG)
    offset += 2;

  if (nbytes * nz > nextra) {
    nread = nextra / nbytes;
    b3dError(stderr, "There are piece coordinates for only %d frames in"
            " the extra header\n", nread);
  }

  li->pcoords = (int *)malloc(sizeof(int) * 3 * nz);

  fseek(inFile->fp, offset, SEEK_SET);

  for (i = 0; i < nread; i++) {
    fread(pcoordxy, 2, 2, inFile->fp);
    fread(&pcoordz, 1, 2, inFile->fp);

    /* add swapping 10/2/00 */
    if (hdr->swapped) {
      mrc_swap_shorts((b3dInt16 *)pcoordxy, 2);
      mrc_swap_shorts(&pcoordz, 1);
    }
    if (ferror(inFile->fp)) {
      nread = i;
      b3dError(stderr, "Error reading piece coordinates from extra"
              " header after %d frames\n", i);
      break;
    }

    li->pcoords[(i*3)]   = pcoordxy[0];
    li->pcoords[(i*3)+1] = pcoordxy[1];
    li->pcoords[(i*3)+2] = pcoordz;

    offset = nbytes - 6;
    if (offset > 0)
      fseek(inFile->fp, offset, SEEK_CUR);
  }
  li->plist = nread;
  return(mrc_plist_proc(li, nx, ny, nz));
}
Exemple #22
0
/*!
 * Reads a color map specification from the file specified by [filename] and 
 * converts it to a complete color table in [table].  The file starts with the
 * number of lines of color data.  If there are 256 lines they must be 
 * red, green, blue triplets (range 0-255); otherwise each line has a red,
 * green, and blue followed by a relative position in the color table.
 * Returns 1 for error opening a file, 2 for errors reading the file, 3 for
 * an invalid number of lines, or 4 for a memory allocation error.
 */
int cmapReadConvert(char *filename, unsigned char table[3][256])
{
  char line[128];
  int err = 0;
  int ind, i, nlines, red, green, blue;
  int *rampData = NULL;
  FILE *fin = fopen(filename, "r");
  
  if (!fin) {
    b3dError(stderr, "cmapReadConvert: error opening file %s\n", filename);
    return 1;
  }
  
  /* Get line with number of entries */
  if (fgetline(fin, line, 128) <= 0) {
    err = 2;
  } else {
    
    nlines = atoi(line);
    if (nlines <=0 || nlines > 256) {
      b3dError(stderr, "cmapReadConvert: invalid number of lines (%d) in %s\n",
               nlines, filename);
      err = 3;
    }
  }
  
  /* If < 256, get array for ramp */
  if (!err && nlines < 256) {
    rampData = (int *)malloc((1 + nlines * 4) * sizeof(int));
    if (!rampData) {
      err = 4;
    } else {

      ind = 1;
      rampData[0] = nlines;
    }
  }

  /* Read the lines and do the appropriate thing with them */
  for (i = 0; !err && i < nlines; i++) {
    if (fgetline(fin, line, 128) <= 0) {
      err = 2;
    } else if (nlines < 256) {
      sscanf(line, "%d%*c%d%*c%d%*c%d", &rampData[ind], &rampData[ind + 1], 
             &rampData[ind + 2], &rampData[ind + 3]);
      ind += 4;
    } else {
      sscanf(line, "%d%*c%d%*c%d", &red, &green, &blue);
      table[0][i] = (unsigned char)red;
      table[1][i] = (unsigned char)green;
      table[2][i] = (unsigned char)blue;
    }
  }

  /* Convert the ramp */
  if (!err && nlines < 256) {
    err = cmapConvertRamp(rampData, table);
    if (err)
      err = 4;
  }
  
  fclose(fin);
  if (rampData)
    free(rampData);
  if (err == 2)
    b3dError(stderr, "cmapReadConvert: error reading file %s\n", filename);
  else if (err == 4)
    b3dError(stderr, "cmapReadConvert: memory allocation error");
  return err;
}
Exemple #23
0
/*!
 * Tries to open an image file with name [filename] and with the fopen mode 
 * [mode] (e.g. "rb"), using the file format check functions on the list.
 * If [filename] is NULL or an empty string, then it assigns stdin to the file
 * pointer.  Returns NULL for error; it and all checking routines should call 
 * b3dError with their error strings.
 */
ImodImageFile *iiOpen(const char *filename, const char *mode)
{
  ImodImageFile *ofile;
  IIFileCheckFunction *checkFunc;
  int i, err = 0;

  /* If the mode contains w for a new file, call the open routine for a default file */
  if (strstr(mode, "w"))
    return iiOpenNew(filename, mode, IIFILE_DEFAULT);

  if ((ofile = iiNew()) == NULL) 
    return NULL;
  if (filename && filename[0])
    ofile->fp = fopen(filename, mode);
  else
    ofile->fp = stdin;

  if (ofile->fp == NULL || initCheckList()) {
    b3dError(stderr, "ERROR: iiOpen - Opening file %s\n", filename);
    iiDelete(ofile);
    return(NULL);
  }
  ofile->format = IIFILE_UNKNOWN;
  ofile->filename = strdup(filename);
  strncpy(ofile->fmode, mode, 3);
    
  /* Try to open the file with each of the check functions in turn 
   * until one succeeds
   */
  for (i = 0; i < ilistSize(sCheckList); i++) {
    checkFunc = (IIFileCheckFunction *)ilistItem(sCheckList, i);

    /* If file was closed and couldn't reopen, bail out */
    if (ofile->fp == NULL) {
      b3dError(stderr, "ERROR: iiOpen - %s could not be reopened\n", filename);
      break;
    }

    if (!(err = (*checkFunc)(ofile))) {
      if (ofile->numVolumes > 1 && !sAllowMultiVolume) {
        b3dError(stderr, "ERROR: iiOpen - %s is an HDF file with multiple volumes and "
                 "cannot be opened by this program or with current options to the "
                 "program\n", filename);
      } else {
        ofile->state = IISTATE_READY;
        if (!addToOpenedList(ofile))
          return ofile;
      }
      iiDelete(ofile);
      return NULL;
    }
    
    if (err != IIERR_NOT_FORMAT)
      break;
  }
  
  if (err == IIERR_NOT_FORMAT)
    b3dError(stderr, "ERROR: iiOpen - %s has unknown format.\n", filename);
  iiDelete(ofile);
  return NULL;
}
Exemple #24
0
int iiMRCCheck(ImodImageFile *iif)
{
  FILE *fp;
  MrcHeader *hdr;
  int err;

  if (!iif) 
    return IIERR_BAD_CALL;
  fp = iif->fp;
  if (!fp) 
    return IIERR_BAD_CALL;

  hdr = (MrcHeader *)malloc(sizeof(MrcHeader));
  if (!hdr) {
    b3dError(stderr, "ERROR: iiMRCCheck - getting memory for header\n");
    return IIERR_MEMORY_ERR;
  }

  if ((err = mrc_head_read(fp, hdr))){
    free(hdr);
    if (err < 0)
      return IIERR_IO_ERROR;
    return IIERR_NOT_FORMAT;
  }

  iif->nx   = hdr->nx;
  iif->ny   = hdr->ny;
  iif->nz   = hdr->nz;
  iif->file = IIFILE_MRC;

  switch(hdr->mode){
  case MRC_MODE_BYTE:
    iif->format = IIFORMAT_LUMINANCE;
    iif->type   = IITYPE_UBYTE;
    break;
  case MRC_MODE_SHORT:
    iif->format = IIFORMAT_LUMINANCE;
    iif->type   = IITYPE_SHORT;
    break;
  case MRC_MODE_USHORT:
    iif->format = IIFORMAT_LUMINANCE;
    iif->type   = IITYPE_USHORT;
    break;
  case MRC_MODE_FLOAT:
    iif->format = IIFORMAT_LUMINANCE;
    iif->type   = IITYPE_FLOAT;
    break;
  case MRC_MODE_COMPLEX_SHORT:
    iif->format = IIFORMAT_COMPLEX;
    iif->type   = IITYPE_SHORT;
    break;
  case MRC_MODE_COMPLEX_FLOAT:
    iif->format = IIFORMAT_COMPLEX;
    iif->type   = IITYPE_FLOAT;
    break;
  case MRC_MODE_RGB:
    iif->format = IIFORMAT_RGB;
    iif->type   = IITYPE_UBYTE;
    break;
  }
  iif->mode  = hdr->mode;
  iif->amin  = hdr->amin;
  iif->amax  = hdr->amax;
  iif->smin  = iif->amin;
  iif->smax  = iif->amax;
  iif->amean = hdr->amean;
  iif->xscale = iif->yscale = iif->zscale = 1.;

  /* DNM 11/5/98: inverted these expressions to give proper usage */
  /* DNM 9/13/02: needed to divide by mx, ny, nz, not nx, ny, nz */
  if (hdr->xlen && hdr->mx)
    iif->xscale = hdr->xlen/(float)hdr->mx;
  if (hdr->ylen && hdr->my)
    iif->yscale = hdr->ylen/(float)hdr->my;
  if (hdr->xlen && hdr->mz)
    iif->zscale = hdr->zlen/(float)hdr->mz;
  iif->xtrans = hdr->xorg;
  iif->ytrans = hdr->yorg;
  iif->ztrans = hdr->zorg;
  iif->xrot = hdr->tiltangles[3];
  iif->yrot = hdr->tiltangles[4];
  iif->zrot = hdr->tiltangles[5];

  iif->headerSize = 1024;
  iif->sectionSkip = 0;
  iif->header = (char *)hdr;

  iif->readSection = iiMRCreadSection;
  iif->readSectionByte = iiMRCreadSectionByte;
  iif->cleanUp = iiMRCdelete;

  return(0);
}