/*! * 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; }
//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); }
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); }