Пример #1
0
main( int argc, char *argv[] )
{
     FILE   *fin, *fout;
     struct MRCheader hdata;
     float first, inc;
     int datasize = 0;

     if (argc != 4){
	  fprintf(stderr, 
		  "%s version 1.0 Copyright (C)1994 Boulder Laboratory for\n", 
		  argv[0]);
	  fprintf(stderr,
		  "3-Dimensional Fine Structure, University of Colorado.\n"); 
	  fprintf(stderr, "%s: Modify a mrc header to contain tilt information.\n", argv[0]) ;
	  fprintf(stderr, "Usage: %s [image file] [first tilt] [tilt increment]\n",  argv[0]) ;
	   exit(3) ;
     }
     

     fin = fopen(argv[1], "rb");
     if (fin == NULL)
	  {
	       fprintf(stderr, "Error opening %s.\n", argv[1]);
	       exit(3);
	  }
     
     fout = fopen(argv[1], "rb+");
     if (fin == NULL)
	  {
	       fprintf(stderr, "Error opening %s.\n", argv[2]);
	       exit(3);
	  }
     
     sscanf(argv[2], "%f", &first);
     sscanf(argv[3], "%f", &inc);


     if (mrc_head_read(fin, &hdata))
	  {
	       fprintf(stderr, "Can't Read Input File Header.\n");
	       exit(3);
	  }

     printf("First tilt = %g, Inc = %g\n", first, inc);

     hdata.idtype = 1;
     hdata.vd1 = first * 100.0;
     hdata.vd2 = inc * 100.0;

     mrc_head_write(fout, &hdata);	  
     fclose(fin);
     fclose(fout);
}
Пример #2
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);
}
Пример #3
0
//init SliceCache and clear its old contents
//It needs to be called whenever the tomogram it manages changes.
void SliceCache::initCache(const char *fnStack, int dim,
                           int hyper, int tSize, int &nx, int &ny, int &nz)
{
  int dsize, csize;
  mDataOffset = 0.;
  if (mFpStack)
    iiFClose(mFpStack);
  if ((mFpStack = iiFOpen(fnStack, "rb")) == 0)
    exitError("could not open input file %s", fnStack);

  /* read mHeader */
  if (mrc_head_read(mFpStack, &mHeader))
    exitError("could not read header of input file %s",  fnStack);
  mSliceMode = sliceModeIfReal(mHeader.mode);
  if (mSliceMode < 0)
    exitError("File mode is %d; only byte, short, integer allowed\n",
              mHeader.mode);
  mrc_getdcsize(mHeader.mode, &dsize, &csize);
  nx = mHeader.nx;
  ny = mHeader.ny;
  nz = mHeader.nz;
  mPsDim = (dim + 1) * hyper + 1;
  if (mFreqCount)
    free(mFreqCount);
  mFreqCount = (int *)malloc(mPsDim * sizeof(int));
  if (mSliceData)
    free(mSliceData);
  mSliceData = (float *)malloc(nx * ny * dsize);
  if (mPsTmp)
    free(mPsTmp);
  mPsTmp = (double *)malloc(mPsDim * sizeof(double));
  if (!mSliceData || !mFreqCount || !mPsTmp)
    exitError("Allocating memory for image slice, frequency counts, or mPsTmp");
  mCurSlice = -1;
  clearAndSetSize(dim, hyper, tSize);
}
Пример #4
0
/*
 * Main entry
 */
int main( int argc, char *argv[])
{
    Imod *imod;
    Iobj *obj;
    Ipoint point;
    Istore store;
    FILE *infp;
    char line[1024];
    int open = 0, zsort = 0, scat = 0, numPerCont = 0, fromZero = 0, zFromZero = 0;
    int err, nvals, nread, ob, co, lineNum, after, needcont, i, numOffset, linelen;
    int numPts = 0, numConts = 0, numObjs = 0;
    int sphere = 0, circle = 0;
    float tst1, tst2, xx, yy, zz, value;
    float zOffset = 0.;
    int numColors = 0;
    int numNames = 0;
    int hasValues = 0;
    int contValues = 0;
    int *red, *green, *blue;
    int directScale = 0;
    float xscale = 1., yscale = 1., zscale = 1., xtrans = 0., ytrans = 0., ztrans = 0.;
    char **names = NULL;

    char *progname = imodProgName(argv[0]);
    char *filename, *imagename;
    MrcHeader hdata;
    IrefImage *ref;
    FILE *fpimage = NULL;
    char *errString;
    int numOptArgs, numNonOptArgs;

    /* Fallbacks from    ../manpages/autodoc2man 2 1 point2model  */
    int numOptions = 16;
    const char *options[] = {
        "input:InputFile:FN:", "output:OutputFile:FN:", "open:OpenContours:B:",
        "scat:ScatteredPoints:B:", "number:PointsPerContour:I:", "planar:PlanarContours:B:",
        "zero:NumberedFromZero:B:", "zcoord:ZCoordinatesFromZero:B:",
        "values:ValuesInLastColumn:I:", "circle:CircleSize:I:", "sphere:SphereRadius:I:",
        "color:ColorOfObject:ITM:", "name:NameOfObject:CHM:",
        "image:ImageForCoordinates:FN:", "pixel:PixelSpacingOfImage:FT:",
        "origin:OriginOfImage:FT:"
    };

    /* Startup with fallback */
    PipReadOrParseOptions(argc, argv, options, numOptions, progname,
                          2, 1, 1, &numOptArgs, &numNonOptArgs, imodUsageHeader);

    /* Get input and output files */
    if (PipGetInOutFile("InputFile", 0, &filename))
        exitError("No input file specified");
    infp = fopen(filename, "r");
    if (!infp)
        exitError("Error opening input file %s", filename);
    free(filename);
    if (PipGetInOutFile("OutputFile", 1, &filename))
        exitError("No output file specified");

    if (!PipGetString("ImageForCoordinates", &imagename)) {
        fpimage = fopen(imagename, "rb");
        if (!fpimage)
            exitError("Could not open image file for coordinates: %s", imagename);
        if (mrc_head_read(fpimage, &hdata))
            exitError("Reading header from %s", imagename);
        free(imagename);
    }

    directScale = 2 - PipGetThreeFloats("PixelSpacingOfImage", &xscale, &yscale, &zscale)
                  - PipGetThreeFloats("OriginOfImage", &xtrans, &ytrans, &ztrans);
    if (directScale && fpimage)
        exitError("You cannot use -image together with -pixel or -origin");

    err = PipGetInteger("PointsPerContour", &numPerCont);
    err = PipGetInteger("SphereRadius", &sphere);
    err = PipGetInteger("CircleSize", &circle);
    err = PipGetBoolean("OpenContours", &open);
    err = PipGetBoolean("ScatteredPoints", &scat);
    err = PipGetBoolean("PlanarContours", &zsort);
    err = PipGetInteger("ValuesInLastColumn", &hasValues);
    err = PipGetBoolean("ZCoordinatesFromZero", &zFromZero);
    if (zFromZero)
        zOffset = 0.5;
    B3DCLAMP(hasValues, -1, 1);
    if (hasValues < 0) {
        hasValues = 1;
        contValues = 1;
    }
    err = PipGetBoolean("NumberedFromZero", &fromZero);
    numOffset = 1 - fromZero;
    if (numPerCont < 0)
        exitError("Number of points per contour must be positive or zero");
    if (open + scat > 1)
        exitError("Only one of -open or -scat may be entered");

    // Get colors
    err = PipNumberOfEntries("ColorOfObject", &numColors);
    if (numColors) {
        red = (int *)malloc(numColors * sizeof(int));
        green = (int *)malloc(numColors * sizeof(int));
        blue = (int *)malloc(numColors * sizeof(int));
        if (!red || !green || !blue)
            exitError("Allocating memory for colors");
        for (co = 0; co < numColors; co++)
            err = PipGetThreeIntegers("ColorOfObject", &red[co], &green[co],
                                      &blue[co]);
    }

    // Get names
    err = PipNumberOfEntries("NameOfObject", &numNames);
    if (numNames) {
        names = (char **)malloc(numNames * sizeof(char *));
        if (!names)
            exitError("Allocating memory for names");
        for (co = 0; co < numNames; co++)
            err = PipGetString("NameOfObject", &names[co]);
    }

    PipDone();

    // Read first line of file, figure out how many values
    if (fgetline(infp, line, 1024) <= 0)
        exitError("Reading beginning of file");

    nvals = sscanf(line, "%f %f %f %f %f %f", &tst1, &tst2, &xx, &yy, &zz, &value);
    nvals -= hasValues;
    if (nvals < 3)
        exitError("There must be at least %d values per line", 3 + hasValues);
    nvals = B3DMIN(nvals, 5);
    if (numPerCont && nvals > 3) {
        exitError("The point file has contour numbers and the -number option "
                  "cannot be used");
    }
    if (zsort && nvals > 3)
        exitError("The point file has contour numbers and the -planar option "
                  "cannot be used");

    rewind(infp);

    imod = imodNew();
    if (!imod)
        exitError("Failed to get model structure");

    // Set the image reference scaling
    if (fpimage) {
        imodSetRefImage(imod, &hdata);
        fclose(fpimage);
    } else if (directScale) {
        imod->refImage = (IrefImage *)malloc(sizeof(IrefImage));
        if (!imod->refImage)
            exitError("Allocating IrefImage structure");
        ref = imod->refImage;
        ref->ctrans.x = xtrans;
        ref->ctrans.y = ytrans;
        ref->ctrans.z = ztrans;
        ref->cscale.x = xscale;
        ref->cscale.y = yscale;
        ref->cscale.z = zscale;
        ref->oscale.x = ref->oscale.y = ref->oscale.z = 1.;
        ref->orot.x = ref->orot.y = ref->orot.z = 0.;
        ref->crot.x = ref->crot.y = ref->crot.z = 0.;
        ref->otrans.x = ref->otrans.y = ref->otrans.z = 0.;
    }

    ob = 0;
    co = 0;
    lineNum = 0;
    imod->xmax = 0.;
    imod->ymax = 0.;
    imod->zmax = 0.;
    store.type = GEN_STORE_VALUE1;
    store.flags = GEN_STORE_FLOAT << 2;

    // To do: error check contour and object #'s, and they are numbered from 1.
    while (1) {

        // get line, done on EOF, skip blank line
        linelen = fgetline(infp, line, 1024);
        if (linelen < 0)
            break;
        if (linelen == 0)
            continue;

        if (nvals == 3) {
            nread = sscanf(line, "%f %f %f %f", &xx, &yy, &zz, &value);
        } else if (nvals == 4) {
            nread = sscanf(line, "%d %f %f %f %f", &co, &xx, &yy, &zz, &value);
            co -= numOffset;
        } else {
            nread = sscanf(line, "%d %d %f %f %f %f", &ob, &co, &xx, &yy, &zz, &value);
            co -= numOffset;
            ob -= numOffset;
        }
        zz -= zOffset;
        lineNum++;

        // Skip line with no values
        if (nread <= 0)
            continue;
        if (B3DMIN(5 + hasValues, nread) != nvals + hasValues &&
                !(contValues && nread == nvals))
            exitError("Every line should have %d entries; line %d has %d",
                      nvals + hasValues, lineNum, nread);

        if (ob < 0 || co < 0)
            exitError("Illegal object or contour number (object %d, contour %d at line %d",
                      ob + numOffset, co + numOffset, lineNum);

        // Add objects if needed to get to the current object
        if (ob >= imod->objsize) {
            for (i = imod->objsize; i <= ob; i++) {
                if (imodNewObject(imod))
                    exitError("Failed to add object to model");
                if (open)
                    imod->obj[i].flags |= IMOD_OBJFLAG_OPEN;
                if (scat)
                    imod->obj[i].flags |= IMOD_OBJFLAG_SCAT | IMOD_OBJFLAG_OPEN;
                numObjs++;
                imod->obj[i].pdrawsize = B3DMAX(0, sphere);
                if (circle > 0) {
                    imod->obj[i].symsize = circle;
                    imod->obj[i].symbol = IOBJ_SYM_CIRCLE;
                }
                if (i < numColors) {
                    imod->obj[i].red = red[i] / 255.;
                    imod->obj[i].green = green[i] / 255.;
                    imod->obj[i].blue = blue[i] / 255.;
                }
                if (i < numNames) {
                    strncpy(imod->obj[i].name, names[i], IOBJ_STRSIZE - 1);
                    imod->obj[i].name[IOBJ_STRSIZE - 1] = 0x00;
                }
            }
        }

        // Determine if a contour is needed: either the contour number is too high
        // or the number limit is reached or there is a change in Z
        needcont = 0;
        obj = &imod->obj[ob];
        if (co >= obj->contsize)
            needcont = 1;
        else if ((numPerCont && obj->cont[co].psize >= numPerCont) ||
                 (zsort && B3DNINT(obj->cont[co].pts[0].z) != B3DNINT(zz))) {
            co++;
            needcont = 1;
        }

        if (needcont) {
            imodSetIndex(imod, ob, -1, -1);
            for (i = obj->contsize; i<= co; i++) {
                if (imodNewContour(imod))
                    exitError("Failed to add contour to model");
                numConts++;
            }
        }

        point.x = xx;
        point.y = yy;
        point.z = zz;
        imod->xmax = B3DMAX(imod->xmax, B3DNINT(xx + 10.));
        imod->ymax = B3DMAX(imod->ymax, B3DNINT(yy + 10.));
        imod->zmax = B3DMAX(imod->zmax, B3DNINT(zz + 1.));
        if (!imodPointAppend(&obj->cont[co], &point))
            exitError("Failed to add point to contour");
        numPts++;

        // take care of value for contour or point, only add one per contour
        if (hasValues) {
            store.value.f = value;
            err = 0;
            if (contValues && istoreLookup(obj->store, co, &after) < 0) {
                if (nread < nvals + 1)
                    exitError("The first point for a contour must have a value entry; line %d "
                              "has only %d entries", lineNum, nread);
                store.index.i = co;
                err = istoreInsert(&obj->store, &store);
            } else {
                store.index.i = obj->cont[co].psize - 1;
                err = istoreInsert(&obj->cont[co].store, &store);
            }
            if (err)
                exitError("Failed to add general value");
        }
    }

    // Get the object min/max values set up
    if (hasValues) {
        for (ob = 0; ob < imod->objsize; ob++) {
            if (imod->obj[ob].contsize && istoreFindAddMinMax1(&imod->obj[ob]))
                exitError("Adding min/max values to object");
        }
    }

    // Attach image file's size as the max values
    if (fpimage) {
        imod->xmax = hdata.nx;
        imod->ymax = hdata.ny;
        imod->zmax = hdata.nz;
    }

    fclose(infp);
    if (imodBackupFile(filename))
        printf("Warning: %s - Failed to make old version of %s be a backup file\n",
               progname, filename);

    imod->file = fopen(filename, "wb");
    if (!imod->file)
        exitError("Opening new model file %s", filename);
    if (imodWriteFile(imod))
        exitError("Writing model file %s", filename);
    free(filename);
    printf("Model created with %d objects, %d contours, %d points\n", numObjs,
           numConts, numPts);
    exit(0);
}
Пример #5
0
int main(int argc, char *argv[])
{
  Imod   model;
  FILE   *fin, *fout;
  char   *filename = NULL;
  int    i;
  int    mode = 0;
  int    useZscale = 0;
  int    doflip = 1;
  int    transopt = 0;
  int    rotScaleopt = 0;
  int    toImage = 0, fromImage = 0;
  int    oneLine = -1;
  int    toggleFlip = 0;
  int    flipModel = 0;
  float  zscale = 1.;
  float transScale = 1.;
  float  rx = 0., ry = 0., rz = 0.;
  float transx = 0., transy = 0., transz = 0.;
  int multiTrans = 0;
  int newNx = 0, newNy = 0, newNz = 0;
  char *progname = imodProgName(argv[0]);
  Ipoint newCen, tmpPt;
  Imat *mat = imodMatNew(3);
  Imat *normMat = imodMatNew(3);
  IrefImage useRef, *modRefp;
  MrcHeader hdata, hdataFirst;
  Ipoint unitPt = {1., 1., 1.};
  char prefix[100];
  sprintf(prefix, "ERROR: %s - ", progname);
  setExitPrefix(prefix);

  if (argc < 3){
    usage(progname);
  }
     
  for (i = 1; i < argc; i++) {
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {

      case '2':
        filename = argv[++i];
        mode += 2;
        break;
            
      case '3':
        filename = argv[++i];
        mode += 3;
        break;

      case 'S':
        sscanf(argv[++i], "%f", &transScale);
        break;

      case 'i':
        toImage = 1;
        if (NULL == (fin = fopen(argv[++i], "rb")))
          exitError("Couldn't open %s", argv[i]);

        if (mrc_head_read(fin, &hdata)) 
          exitError("Reading header from %s", argv[i]);
        fclose(fin);
        break;

      case 'I':
        fromImage = 1;
        if (NULL == (fin = fopen(argv[++i], "rb")))
          exitError("Couldn't open %s", argv[i]);

        if (mrc_head_read(fin, &hdataFirst)) 
          exitError("Reading header from %s", argv[i]);
        fclose(fin);
        break;

      case 'z':
        useZscale = 1;
        break;

      case 'f':
        doflip = 0;
        break;

      case 'Y':
        flipModel = 1;
        break;

      case 'T':
        toggleFlip = 1;
        break;

      case 'n':
        sscanf(argv[++i], "%d%*c%d%*c%d", &newNx, &newNy, &newNz);
        break;
            
      case 'l':
        oneLine = atoi(argv[++i]);
        break;

      case 't':  /* Translations */
        tmpPt.x = tmpPt.y = tmpPt.z = 0.;
        transopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-tx%f", &tmpPt.x);
          else
            sscanf(argv[++i], "%f", &tmpPt.x);
          if (transx)
            multiTrans = 1;
          transx = tmpPt.x;
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-ty%f", &tmpPt.y);
          else
            sscanf(argv[++i], "%f", &tmpPt.y);
          if (transy)
            multiTrans = 1;
          transy = tmpPt.y;
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-tz%f", &tmpPt.z);
          else
            sscanf(argv[++i], "%f", &tmpPt.z);
          if (transz)
            multiTrans = 1;
          transz = tmpPt.z;
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        imodMatTrans(mat, &tmpPt);
        break;

      case 's':  /* Scaling */
        tmpPt.x = tmpPt.y = tmpPt.z = 1.;
        transopt = 1;
        rotScaleopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sx%f", &tmpPt.x);
          else
            sscanf(argv[++i], "%f", &tmpPt.x);
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sy%f", &tmpPt.y);
          else
            sscanf(argv[++i], "%f", &tmpPt.y);
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sz%f", &tmpPt.z);
          else
            sscanf(argv[++i], "%f", &tmpPt.z);
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        imodMatScale(mat, &tmpPt);
        tmpPt.x = 1. / tmpPt.x;
        tmpPt.y = 1. / tmpPt.y;
        tmpPt.z = 1. / tmpPt.z;
        imodMatScale(normMat, &tmpPt);
        break;

      case 'r':  /* Rotations */
        transopt = 1;
        rotScaleopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-rx%f", &rx);
          else
            sscanf(argv[++i], "%f", &rx);
          imodMatRot(mat, (double)rx, b3dX);
          imodMatRot(normMat, (double)rx, b3dX);
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-ry%f", &ry);
          else
            sscanf(argv[++i], "%f", &ry);
          imodMatRot(mat, (double)ry, b3dY);
          imodMatRot(normMat, (double)ry, b3dY);
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-rz%f", &rz);
          else
            sscanf(argv[++i], "%f", &rz);
          imodMatRot(mat, (double)rz, b3dZ);
          imodMatRot(normMat, (double)rz, b3dZ);
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        break;

      default:
        exitError("Invalid option %s",  argv[i]);
      }

    }else{
      break;
    }
  }

  if (mode && mode / 2 != 1) 
    exitError("You cannot enter both -2 and -3");
  
  if (mode && rotScaleopt)  
    exitError("You cannot enter -r or -s options with -2 and -3");
  if (transopt && mode == 2 && oneLine < 1 && transz)
    exitError("You cannot enter -tz option with -2 unless transforming with one line");
  if (mode && multiTrans)
    exitError("You cannot enter -t options more than once with -2 and -3");
  
  if (i != argc - 2) {
    printf("ERROR: %s - Command line should end with two non-option "
            "arguments\n", progname);
    usage(progname);
  }

  fin = fopen(argv[i], "rb");
  if (!fin)
    exitError("Opening input file %s", argv[i]);
      
  imodDefault(&model);
  model.file = fin;
  if (imodReadFile(&model))
    exitError("Reading imod model (%s)", argv[i]);
  fclose(fin);

  if (imodBackupFile(argv[i + 1])) 
    exitError("Couldn't create backup file");

  fout = fopen(argv[i + 1], "wb");
  if (!fout)
    exitError("Opening output file %s", argv[i + 1]);

  /* Change image reference information */
  if (fromImage && imodSetRefImage(&model, &hdataFirst))
    exitError("Allocating a IrefImage structure");

  /* Do flipping operations first */
  if (flipModel)
    imodFlipYZ(&model);
  if (toggleFlip) {
    if (model.flags & IMODF_FLIPYZ)
      model.flags &= ~IMODF_FLIPYZ;
    else
      model.flags |= IMODF_FLIPYZ;
  }

  /* Do scaling to image reference next */
  if (toImage) {

    modRefp = model.refImage;
    if (modRefp && !(model.flags & IMODF_OTRANS_ORIGIN)) 
      exitError("Model has no image origin information; -i option invalid");

    /* get the target transformation */
    useRef.ctrans.x = hdata.xorg;
    useRef.ctrans.y = hdata.yorg;
    useRef.ctrans.z = hdata.zorg;
    useRef.crot.x = hdata.tiltangles[3];
    useRef.crot.y = hdata.tiltangles[4];
    useRef.crot.z = hdata.tiltangles[5];
    useRef.cscale = unitPt;
    if (hdata.xlen && hdata.mx)
      useRef.cscale.x = hdata.xlen/(float)hdata.mx;
    if (hdata.ylen && hdata.my)
      useRef.cscale.y = hdata.ylen/(float)hdata.my;
    if (hdata.zlen && hdata.mz)
      useRef.cscale.z = hdata.zlen/(float)hdata.mz;
    
    if (modRefp) {

      /* If there is a refImage, scale the data as when reading into 3dmod */
      useRef.otrans = modRefp->ctrans;
      useRef.orot = modRefp->crot;
      useRef.oscale = modRefp->cscale;
      imodTransFromRefImage(&model, &useRef, unitPt);
    } else {

      /* If there is no refImage, do not scale data but assign all the information
         just as if reading into 3dmod */
      model.refImage = (IrefImage *)malloc (sizeof(IrefImage));
      modRefp = model.refImage;
      if (!modRefp) 
        exitError("Allocating a IrefImage structure");
      model.flags |= IMODF_OTRANS_ORIGIN;
    }
    *modRefp = useRef;
    modRefp->otrans = useRef.ctrans;

    /* Adjust the maxes for this change */
    model.xmax = hdata.nx;
    model.ymax = hdata.ny;
    model.zmax = hdata.nz;
  }

  /* Use zscale if user indicated it */
  if (useZscale && model.zscale > 0.)
    zscale = model.zscale;

  /* Do flipping if model flipped and user did not turn it off */
  if (!(model.flags & IMODF_FLIPYZ))
    doflip = 0;

  /* Warning every time is too noxious!
  if (!newNx)
    printf("Assuming transformed images have same size as original "
            "images\n (use -n option to specify size of transformed "
            "images)\n");
  */
  newCen.x = (newNx ? newNx : model.xmax) * 0.5f;
  newCen.y = (newNy ? newNy : model.ymax) * 0.5f;
  newCen.z = (newNz ? newNz : model.zmax) * 0.5f;

  if (filename){
    if (filetrans(filename, &model, mode, oneLine, newCen, zscale, doflip, transScale,
                  transx, transy, transz)) 
      exitError("Transforming model.");

  } else if (transopt) {
    imodTransModel3D(&model, mat, normMat, newCen, zscale, doflip);
  }

  model.xmax = newNx ? newNx : model.xmax;
  model.ymax = newNy ? newNy : model.ymax;
  model.zmax = newNz ? newNz : model.zmax;
  imodWrite(&model, fout);
  exit(0);
}
Пример #6
0
main( int argc, char *argv[] )
{
     FILE   *fin, *fout;
     struct MRCheader hdata;
     unsigned char bdata;
     short sdata;
     float fpixel, fadd;
     float min, max, total;
     double dpixel;
     int pixsize;
     int i;

     int datasize = 0;


     if (argc < 3){
	  fprintf(stderr, "Usage: %s [infile] [outfile]\n",  argv[0]) ;
	   exit(3) ;
     }
     

     fin = fopen(argv[1], "rb");
     if (fin == NULL)
	  {
	       fprintf(stderr, "Error opening %s.\n", argv[1]);
	       exit(3);
	  }
     
     fout = fopen(argv[2], "wb");
     if (fin == NULL)
	  {
	       fprintf(stderr, "Error opening %s.\n", argv[2]);
	       exit(3);
	  }
     



     if (mrc_head_read(fin, &hdata))
	  {
	       fprintf(stderr, "Can't Read Input File Header.\n");
	       exit(3);
	  }
     
     datasize = hdata.nx * hdata.ny * hdata.nz;


     if (hdata.mode == MRC_MODE_COMPLEX_SHORT)
	  datasize *= 2;

     if (hdata.mode == MRC_MODE_COMPLEX_FLOAT)
	  datasize *= 2;

     mrc_head_label(&hdata, "mrclog: Took log base 10 of data");
     mrc_head_write(fout, &hdata);
     

     switch(hdata.mode){
	  
	case MRC_MODE_BYTE:
	  pixsize = sizeof(unsigned char);
	  for(i = 0; i < datasize; i++){
	       fread(&bdata, pixsize, 1, fin);
	       fpixel = bdata;
	       fpixel = flog(fpixel);
	       bdata = fpixel;
	       fwrite(&bdata,  pixsize, 1, fout);
	       
	  }
	  break;

	case MRC_MODE_SHORT:
	case MRC_MODE_COMPLEX_SHORT:
	  pixsize = sizeof(short);
	  for(i = 0; i < datasize; i++){
	       fread(&sdata, pixsize, 1, fin);
	       fpixel = sdata;
	       fpixel = flog(fpixel);
	       sdata = fpixel;
	       fwrite(&sdata,  pixsize, 1, fout);
	  }
	  break;

	case MRC_MODE_FLOAT:
	case MRC_MODE_COMPLEX_FLOAT:
	  fread(&min, pixsize, 1, fin);
	  for ( i = 1; i < datasize; i++){
	       fread(&fpixel, pixsize, 1, fin);
	       if (fpixel < min)
		    min = fpixel;
	  }
	  fseek(fin, 1024, SEEK_SET);
	  
	  pixsize = sizeof(float);
	  if (min < 1.0)
	       fadd = 1.0 - min;
	  else
	       fadd = 0.0;
	  min = 999999;
	  max = 0;
	  for(i = 0; i < datasize; i++){
	       fread(&fpixel, pixsize, 1, fin);
	       fpixel += fadd;
	       dpixel = fpixel;
	       if (dpixel < 1.0)
		    dpixel = 1.0;
	       fpixel = (float)log(dpixel);
	       if (fpixel < min)
		    min = fpixel;
	       if (max < fpixel)
		    max = fpixel;
	       total += fpixel;
	       fwrite(&fpixel,  pixsize, 1, fout);
	  }
	  total = total / datasize;
	  hdata.amin = min;
	  hdata.amax = max;
	  hdata.amean = total;
	  mrc_head_write(fout, &hdata);	  
	  break;

	default:
	  fprintf(stderr, "%s: data type %d unsupported.\n", argv[0], 
		  hdata.mode);
	  break;

     }

     fclose(fin);
     fclose(fout);


}
Пример #7
0
int main( int argc, char *argv[] )
{

  int    i = 0;
  FILE   *fin, *fout;
  struct MRCheader hdata, hout;
  struct MRCheader *hptr;
  unsigned char *buf;
  int bsize, csize, dsize;
  int inside = 0;
  int ntaper = DEFAULT_TAPER;
  int taperEntered = 0;
  Islice slice;
  int zmin = -1, zmax = -1;
  int secofs;
  char *progname = imodProgName(argv[0]);
  setStandardExitPrefix(progname);

  if (argc < 2){
    fprintf(stderr, 
            "%s version %s\n", progname, VERSION_NAME);
    imodCopyright();
    mrctaper_help(progname);
    exit(3);
  }

  for (i = 1; i < argc; i++) {
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {
          
      case 'i':
        inside = 1;
        break;

      case 't':
        taperEntered = 1;
        if (argv[i][2] != 0x00)
          sscanf(argv[i], "-t%d", &ntaper);
        else
          sscanf(argv[++i], "%d", &ntaper);
        break;

      case 'z':
        if (argv[i][2] != 0x00)
          sscanf(argv[i], "-z%d%*c%d", &zmin, &zmax);
        else
          sscanf(argv[++i], "%d%*c%d", &zmin, &zmax);
        break;

      default:
        printf("ERROR: %s - illegal option\n", progname);
        mrctaper_help(progname);
        exit(1);
        break;
      }
    } else
      break;
  }

  if (i < (argc - 2) || i == argc){
    mrctaper_help(progname);
    exit(3);      
  }

  if (ntaper < 1 || ntaper > 127)
    exitError("Taper must be between 1 and 127.");

  if (i < argc - 1)
    fin = iiFOpen(argv[i++], "rb");
  else
    fin = iiFOpen(argv[i++], "rb+");

  if (fin == NULL)
    exitError("Opening %s.", argv[i - 1]);
  if (mrc_head_read(fin, &hdata))
    exitError("Can't Read Input File Header.");

  if (sliceModeIfReal(hdata.mode) < 0)
    exitError("Can operate only on byte, integer and real data.");

  if (!taperEntered) {
    ntaper = (hdata.nx + hdata.ny) / 200;
    ntaper = B3DMIN(127, B3DMAX(DEFAULT_TAPER, ntaper));
    printf("Tapering over %d pixels\n", ntaper);
  }
     
  if (zmin == -1 && zmax == -1) {
    zmin = 0;
    zmax = hdata.nz - 1;
  } else {
    if (zmin < 0)
      zmin = 0;
    if (zmax >= hdata.nz)
      zmax = hdata.nz - 1;
  }
     
  if (i < argc) {
    fout = iiFOpen(argv[i], "wb");
    if (fout == NULL)
      exitError("Opening %s.", argv[i]);
    hout = hdata;
    hout.fp = fout;

    /* DNM: eliminate extra header info in the output, and mark it as not swapped  */
    mrcInitOutputHeader(&hout);
    hptr = &hout;
    hout.nz = zmax + 1 - zmin;
    hout.mz = hout.nz;
    hout.zlen = hout.nz;
    secofs = zmin;
  } else {
    if (b3dOutputFileType() == IIFILE_TIFF)
      exitError("Cannot write to an existing TIFF file.");
      
    hptr = &hdata;
    fout = fin;
    secofs = 0;
  }
     
  mrc_getdcsize(hdata.mode, &dsize, &csize);

  bsize = hdata.nx * hdata.ny;
  buf = (unsigned char *)malloc(dsize * csize * bsize);
     
  if (!buf)
    exitError("Couldn't get memory for slice.");
  sliceInit(&slice, hdata.nx, hdata.ny, hdata.mode, buf);

  for (i = zmin; i <= zmax; i++) {
    printf("\rDoing section #%4d", i);
    fflush(stdout);
    if (mrc_read_slice(buf, fin, &hdata, i, 'Z'))
      exitError("Reading section %d.", i);
      
    if (sliceTaperAtFill(&slice, ntaper, inside))
      exitError("Can't get memory for taper operation.");
          
    if (mrc_write_slice(buf, fout, hptr, i - secofs, 'Z'))
      exitError("Writing section %d.", i);
  }
  puts("\nDone!");

  mrc_head_label(hptr, "mrctaper: Image tapered down to fill value at edges");

  mrc_head_write(fout, hptr);
  iiFClose(fout);

  return(0);
}
Пример #8
0
main( int argc, char *argv[] )
{
     int    i = 0;
     int    error;
     FILE   *fin;
     struct MRCheader hdata;
     struct MRCheader *data;
     float  vd1, vd2, nd1, nd2;

     fprintf(stderr, 
	     "%s version 1.0 Copyright (C)1994 Boulder Laboratory for\n", 
	     argv[0]);
     fprintf(stderr,
	     "3-Dimensional Fine Structure, University of Colorado.\n");

     if (argc < 2){
	  fprintf(stderr,"Usage: %s <image file>\n",argv[0]) ;
	  exit(3) ;
     }

     data = &hdata;          
     fin = fopen(argv[1], "rb");
     
     if (fin == NULL){
	  fprintf(stderr, "Error opening %s.\n", argv[1]);
	  exit(3);
     }
     
     error = mrc_head_read(fin, &hdata);
     if (error < 0){
	  fprintf(stderr, "Can't Read Input File Header.\n");
	  exit(3);
     }
     if (error > 0)
	  fprintf(stderr, "Warning: File is not in MRC format.\n\n");

     /* Output info */
     printf("MRCinfo: Info on image file %s\n", argv[1]);
     switch(data->mode)
	  {
	     case MRC_MODE_BYTE :
		  printf("mode = Byte\n") ;
		  break ;
		case MRC_MODE_SHORT :
		     printf("mode = Short\n") ;
		  break ;
		case MRC_MODE_FLOAT :
		     printf("mode = Float\n") ;
		  break ;
		case MRC_MODE_COMPLEX_SHORT :
		     printf("mode = Complex Short\n") ;
		  break ;
		case MRC_MODE_COMPLEX_FLOAT :
		     printf("mode = Complex Float\n") ;
		  break ;
		case MRC_MODE_RGB:
		  printf("mode = rgb byte\n");
		  break;
		default:
		  printf("mode is unknown.\n");
		  break;
	     }

     printf("Image size    =  ( %d, %d, %d)\n", 
	    data->nx, data->ny, data->nz);

     printf("minimum value = %g\n",data->amin) ;
     printf("maximum value = %g\n",data->amax) ;
     printf("mean value    = %g\n",data->amean) ;

     printf("Start reading image at ( %d, %d, %d).\n",
	    data->nxstart,
	    data->nystart,
	    data->nzstart) ;
    
     printf("Read length   =  ( %d, %d, %d).\n",
	    data->mx, data->my, data->mz);

     printf("Size of voxel is ( %g x %g x %g ) um.\n",
	    data->xlen, data->ylen, data->zlen) ;

     printf("Rotation      =  ( %g, %g, %g).\n", 
	    data->alpha, data->beta, data->gamma) ;

     printf("Col, rows, sections  = axis (%d , %d, %d)\n",
	    data->mapc, data->mapr, data->maps) ;

     printf("Angles (%g, %g, %g) --> (%g, %g, %g)\n",
	    data->tiltangles[0], data->tiltangles[1], data->tiltangles[2],
	    data->tiltangles[3], data->tiltangles[4], data->tiltangles[5]);
     
     if (data->ispg)
	  printf("ispg =\t\t%d\n",data->ispg) ;
     if (data->idtype){
	  printf("idtype =\t%d\n", data->idtype);

	  nd1 = data->nd1;
	  nd2 = data->nd2;
	  vd1 = data->vd1;
	  vd2 = data->vd2;
	  
	  
	  printf("nd1 =\t\t%g\n", (nd1));
	  printf("nd2 =\t\t%g\n", (nd2));
	  printf("vd1 =\t\t%g\n", (vd1 / 100.0));
	  printf("vd2 =\t\t%g\n", (vd2 / 100.0));
     }

     printf("orgin = ( %g, %g, %g)\n",
	    data->xorg, data->yorg, data->zorg);

     if (data->nlabl > MRC_NLABELS){
	  printf("There are to many labels.\n\n");
	  exit(0);
     }

     printf("Thare are %d labels.\n\n",data->nlabl) ;

     for (i = 0; i < data->nlabl; i++)
	  printf("%s",data->labels[i]) ;
     
     exit(0);
}
Пример #9
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);
}