/*! * Writes the header for unit [iunit] to file with the minimum, maximum and mean * density values in [dmin], [dmax], and [dmean]. A single title can be supplied in * an 80-byte array, [label], and will be treated according to the value of [labFlag]: ^ * 0: [label] becomes the only title ^ * 1: [label] is added at the end of existing titles, replacing the last one if there * are already 10 titles ^ * 2: [label] is added as the first title and others are shifted up ^ * -1 or anything else: do not add a title ^ * Returns -1 for various internal errors or 1 for an error writing the header. * Fortran wrappers iiuWriteHeader and iwrhdr (a void). */ int iiuWriteHeader(int iunit, int *label, int labFlag, float dmin, float dmax, float dmean) { int doExit = iiuGetExitOnError(); int i; MrcHeader *hdr = iiuMrcHeader(iunit, "iiuWriteHeader", doExit, 2); if (!hdr) return -1; if (labFlag == 0) { iiuAltLabels(iunit, label, 1); } else if (labFlag == 1) { hdr->nlabl = B3DMIN(hdr->nlabl + 1, MRC_NLABELS); memcpy(hdr->labels[hdr->nlabl - 1], label, MRC_LABEL_SIZE); fixTitlePadding(hdr->labels[hdr->nlabl - 1]); } else if (labFlag == 2) { hdr->nlabl = B3DMIN(hdr->nlabl + 1, MRC_NLABELS); for (i = hdr->nlabl - 1; i > 0; i--) memcpy(hdr->labels[i], hdr->labels[i - 1], MRC_LABEL_SIZE); memcpy(hdr->labels[0], label, MRC_LABEL_SIZE); fixTitlePadding(hdr->labels[0]); } hdr->amin = dmin; hdr->amax = dmax; hdr->amean = dmean; if (mrc_head_write(hdr->fp, hdr)) { if (doExit) exit(1); return 1; } return 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); }
/* * Sets up output size and file header based on output options * Returns first z section to write, or negative for error. */ int set_output_options(ClipOptions *opt, MrcHeader *hout) { int z = 0; /* create a new file */ if (!opt->add2file){ mrc_head_new(hout, opt->ox, opt->oy, opt->oz, opt->mode); if (mrc_head_write(hout->fp, hout)) return -1; } else { if ((opt->ox != hout->nx) || (opt->oy != hout->ny)){ opt->ox = hout->nx; opt->oy = hout->ny; show_warning("clip - Appended file can't change size."); } if (opt->mode != hout->mode){ opt->mode = hout->mode; show_warning("clip - Appended file can't change mode."); } if (IP_APPEND_ADD == opt->add2file){ /* If appending, scale mean down so complete mean can be computed by adding slice mean / new nz */ z = hout->nz; hout->nz += opt->oz; hout->amean = z * hout->amean / hout->nz; } else{ z = opt->isec; hout->nz = opt->oz + opt->isec; } if (mrc_head_write(hout->fp, hout)) return -1; /* DNM 1/15/05: removed the calculation of data size and file seek */ } return(z); }
/* DNM 1/5/05: changed to return result of writing header */ int set_mrc_coords(ClipOptions *opt) { float tx, ty, tz, sx, sy, sz; MrcHeader *hin = opt->hin; MrcHeader *hout = opt->hout; int ixScale = (opt->process == IP_UNPACK) ? 2 : 1; mrc_coord_cp(hout, hin); mrc_get_scale(hout, &sx, &sy, &sz); /* 1/30/07: In response to a request to preserve the n[xyz]start */ hout->nxstart = hin->nxstart; hout->nystart = hin->nystart; hout->nzstart = hin->nzstart; /* DNM 9/21/01: subtract rather than add tx, ty, tz, because of weird definition of origin 4/15/12: Argh! It needs to multiply by the scale */ tx = (int)(opt->cx * ixScale) - (opt->ix * ixScale) / 2; tx += (opt->ix * ixScale - opt->ox)/2; if (sx) tx *= sx; hout->xorg -= tx; ty = (int)opt->cy - opt->iy / 2; ty += (opt->iy - opt->oy)/2; if (sy) ty *= sy; hout->yorg -= ty; /* DNM 1/5/05: with -2d, -iz is treated like a range list, not a size */ if (opt->dim == 3) { tz = (int)floor(opt->cz - opt->iz / 2.); tz += (opt->iz - opt->oz)/2; if (sz) tz *= sz; hout->zorg -= tz; } return (mrc_head_write(hout->fp, hout)); }
/*! * Writes the data from [slice] into a new MRC file whose name is given in * [filename]. Returns -1 for error opening the file, -2 for error writing * header, or an error from ferror if there is an error writing the data. * Unused as of 2/3/07, but seems usable. */ int sliceWriteMRCfile(char *filename, Islice *slice) { MrcHeader hout; FILE *fp = fopen(filename, "wb"); int error; if (!fp) return -1; mrc_head_new(&hout, slice->xsize, slice->ysize, 1, slice->mode); sliceMMM(slice); hout.amin = slice->min; hout.amax = slice->max; hout.amean = slice->mean; if (mrc_head_write(fp, &hout)) { fclose(fp); return -2; } b3dFwrite(slice->data.b, slice->dsize, slice->csize * slice->xsize * slice->ysize, fp); error = ferror(fp); fclose(fp); return(error); }
/* Write the data volume with potential padding */ int grap_volume_write(Istack *v, MrcHeader *hout, ClipOptions *opt) { FILE *fp = hout->fp; int zs, ks, z; int k; float min, max, mean, zscale; Islice *s, *ps = NULL; int labels; if (opt->mode == IP_DEFAULT) opt->mode = v->vol[0]->mode; if (opt->ox == IP_DEFAULT) opt->ox = v->vol[0]->xsize; if (opt->oy == IP_DEFAULT) opt->oy = v->vol[0]->ysize; if (opt->oz == IP_DEFAULT) opt->oz = v->zsize; if (v->vol[0]->mode != opt->mode) for(k = 0; k < v->zsize; k++){ sliceNewMode(v->vol[k], opt->mode); } /* calculate min, max, mean of volume. */ sliceMMM(v->vol[0]); min = v->vol[0]->min; max = v->vol[0]->max; mean = v->vol[0]->mean; for(k = 1; k < v->zsize; k++){ sliceMMM(v->vol[k]); min = B3DMIN(min, v->vol[k]->min); max = B3DMAX(max, v->vol[k]->max); mean += v->vol[k]->mean; } mean /= (float)v->zsize; /* DNM 6/26/02: do not make a new header if appending or overwriting! */ switch(opt->add2file){ case IP_APPEND_OVERWRITE: ks = opt->isec; hout->nz = ks + opt->oz; opt->ox = hout->nx; opt->oy = hout->ny; if (hout->mode != v->vol[0]->mode){ fprintf(stderr, "overwriting requires data modes to be the same.\n"); return(-1); } /* DNM 6/26/02: fix min and max, also fix mz and zlen */ /* DNM 9/13/02: oops, had xlen instead of zlen */ /* todo: recalc mmm. This is a start but not much */ hout->amin = B3DMIN(min, hout->amin); hout->amax = B3DMAX(max, hout->amax); zscale = hout->zlen / hout->mz; hout->mz = hout->nz; hout->zlen = hout->mz * zscale; break; case IP_APPEND_ADD: ks = hout->nz; hout->nz = hout->nz + opt->oz; opt->ox = hout->nx; opt->oy = hout->ny; if (hout->mode != v->vol[0]->mode){ fprintf(stderr, "inserting requires data modes to be the same.\n"); return(-1); } hout->amin = B3DMIN(min, hout->amin); hout->amax = B3DMAX(max, hout->amax); hout->amean = ((hout->amean * ks) + (mean * opt->oz)) / hout->nz; zscale = hout->zlen / hout->mz; hout->mz = hout->nz; hout->zlen = hout->mz * zscale; break; case IP_APPEND_FALSE: /* New file: reinitialize header but preserve the labels, set min/max/mean */ labels = hout->nlabl; mrc_head_new(hout, opt->ox, opt->oy, opt->oz, v->vol[0]->mode); hout->nlabl = labels; hout->amin = min; hout->amax = max; hout->amean = mean; ks = 0; } if (opt->pad == IP_DEFAULT) opt->pad = hout->amean; if (mrc_head_write(fp, hout)) return -1; zs = (v->zsize - opt->oz) / 2; for (k = ks, z = zs; k < hout->nz; k++, z++) { /* printf("%d %d\n", k, z);*/ /* Write blank slice outside z range */ if ((z < 0) || (z >= v->zsize)) { if (!ps) { ps = clipBlankSlice(hout, opt); if (!ps) return -1; } if (mrc_write_slice((void *)ps->data.b, fp, hout, k, 'z')) return (-1); } else { /* Resize slice only if necessary */ s = v->vol[z]; if (opt->ox != v->vol[0]->xsize || opt->oy != v->vol[0]->ysize) { v->vol[z]->mean = opt->pad; s = mrc_slice_resize(v->vol[z], opt->ox, opt->oy); if (!s){ fprintf(stderr, "volume_write: error resizing slice.\n"); return(-1); } } if (mrc_write_slice((void *)s->data.b, fp, hout, k, 'z')) return (-1); if (opt->ox != v->vol[0]->xsize || opt->oy != v->vol[0]->ysize) sliceFree(s); } } if (ps) sliceFree(ps); return(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); }
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); }