/* * mrc.c * * Read the MRC tomography format * * (c) 2007 Thomas White * dtr - Diffraction Tomography Reconstruction * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "mrc.h" #include "control.h" #include "imagedisplay.h" #include "itrans.h" #include "reflections.h" int mrc_read(ControlContext *ctx) { FILE *fh; MRCHeader mrc; MRCExtHeader ext[1024]; unsigned int i; unsigned int extsize; double sx, sy, sz; unsigned int nx, ny, nz; double pixel_size; int16_t *image_total; unsigned int x, y; int max_x, max_y; int16_t max_val; ImageDisplay *sum_id; fh = fopen(ctx->filename, "rb"); /* Read primary header */ fread(&mrc, sizeof(MRCHeader), 1, fh); printf("%i images in series\n", mrc.nz); if ( mrc.mode != 1 ) { fprintf(stderr, "MR: Unknown MRC image mode\n"); fclose(fh); return -1; } if ( (mrc.nxstart != 0) || (mrc.nystart != 0) ) { fclose(fh); fprintf(stderr, "MR: Image origin must be at zero: found at %i,%i\n", mrc.nxstart, mrc.nystart); return -1; } /* Read all extended headers, one by one */ extsize = 4*mrc.numintegers + 4*mrc.numfloats; if ( extsize > sizeof(MRCExtHeader) ) { fclose(fh); fprintf(stderr, "MR: MRC extended header is too big - tweak mrc.h\n"); return -1; } for ( i=0; i mrc.ny)?mrc.nx:mrc.ny; sx = nx * pixel_size; sy = ny * pixel_size; sz = nz * pixel_size; ctx->reflectionctx = reflection_init(); ctx->fmode = FORMULATION_PIXELSIZE; ctx->first_image = 1; ctx->width = mrc.nx; ctx->height = mrc.ny; printf("Judging centre...\n"); image_total = malloc(mrc.ny * mrc.nx * sizeof(int16_t)); for ( y=0; yheight; y++ ) { for ( x=0; xwidth; x++ ) { if ( image_total[x + ctx->width*y] > max_val ) { max_val = image_total[x + ctx->width*y]; max_x = x; max_y = y; } } } imagedisplay_mark_point(sum_id, max_x, max_y); ctx->x_centre = max_x; ctx->y_centre = max_y; for ( i=0; icamera_length = ext[i].magnification; ctx->lambda = 2.51e-12; /* 200kV. Fudged until Max puts the HT voltage in the MRC headers */ ctx->omega = ext[i].tilt_axis; ctx->pixel_size = ext[i].pixel_size; fseek(fh, mrc.next + sizeof(MRCHeader) + mrc.nx*mrc.ny*2*i, SEEK_SET); fread(image, mrc.nx*mrc.ny*2, 1, fh); for ( y=0; y