#include "medcon.h"
-----------------------------------------------------------------------
Important Global Variables
-----------------------------------------------------------------------
char prefix[MDC_MAX_PREFIX+1]; /* prefix for new filenames */
/* command-line input data */
char *mdc_arg_files[]; /* pointers to input files */
int mdc_arg_convs[]; /* conversions selected */
int mdc_arg_total[]; /* total files & conversions*/
/* options set at command-line */
char MDC_INSTITUTION[]; /* name of institution */
Int8 MDC_COLOR_MAP; /* gray color palette selected */
Int8 MDC_PADDING_MODE; /* resized image padding mode */
Int8 MDC_ANLZ_SPM; /* Analyze/SPM scaling factor */
Int8 MDC_ANLZ_OPTIONS; /* Analyze/SPM request params */
Int8 MDC_DICOM_MOSAIC_ENABLED; /* DICOM: mosaic support enabled */
Int8 MDC_DICOM_MOSAIC_FORCED; /* DICOM: mosaic preset forced */
Int8 MDC_DICOM_MOSAIC_DO_INTERL; /* DICOM: mosaic forced interlaced */
Int8 MDC_DICOM_MOSAIC_FIX_VOXEL; /* DICOM: mosaic fix voxel sizes */
Int8 MDC_DICOM_WRITE_IMPLICIT; /* DICOM: write little implicit */
Int8 MDC_DICOM_WRITE_NOMETA; /* DICOM: write without meta header */
Int8 MDC_DICOM_WRITE_ENCAP_UNCOMP; /* DICOM: write encap. uncompressed */
Int8 MDC_FORCE_RESCALE; /* user specified slope/intercept */
Int8 MDC_FORCE_CONTRAST; /* user specified centre/width */
Int8 MDC_INFO; /* default print header info */
Int8 MDC_INTERACTIVE; /* interactive read of raw file */
Int8 MDC_CONVERT; /* image conversion requested */
Int8 MDC_EXTRACT; /* extract images */
Int8 MDC_RENAME; /* rename base filename */
Int8 MDC_ECHO_ALIAS; /* echo alias name based on ID's */
Int8 MDC_EDIT_FI; /* edit FILEINFO struct */
Int8 MDC_PIXELS; /* print specified pix values */
Int8 MDC_PIXELS_PRINT_ALL; /* print all pix values */
Int8 MDC_NEGATIVE; /* allow negative pixel values */
Int8 MDC_QUANTIFY; /* quantitation with one factor */
Int8 MDC_CALIBRATE; /* quantitation with two factors */
Int8 MDC_CONTRAST_REMAP; /* apply contrast remapping */
Int8 MDC_DEBUG; /* give debug info */
Int8 MDC_VERBOSE; /* run in verbose mode */
Int8 MDC_GIF_OPTIONS; /* request for extra GIF options */
Int8 MDC_MAKE_GRAY; /* forced remap color to gray scale */
Int8 MDC_DITHER_COLOR; /* apply dither on color reduction */
Int8 MDC_NORM_OVER_FRAMES; /* normalize over images in a frame */
/* instead of all images */
Int8 MDC_SKIP_PREVIEW; /* skip the first (preview) slice */
Int8 MDC_IGNORE_PATH; /* ignore path in INTF data fname */
Int8 MDC_SINGLE_FILE; /* write INTF as single file */
Int8 MDC_FORCE_INT; /* force integer pixels */
Int8 MDC_INT16_BITS_USED; /* bits to use for Int16 type */
Int8 MDC_TRUE_GAP; /* spacing = true gap/overlap */
Int8 MDC_ALIAS_NAME; /* use alias name based on ID's */
Int8 MDC_PREFIX_DISABLED; /* prevent the prefix in names */
Int8 MDC_PREFIX_ACQ; /* use acquisition number as prefix */
Int8 MDC_PREFIX_SER; /* use series number as prefix */
Int8 MDC_PATIENT_ANON; /* make patient anonymous */
Int8 MDC_PATIENT_IDENT; /* give patient identification */
Int8 MDC_FILE_OVERWRITE; /* allow file overwriting */
Int8 MDC_FILE_STDIN; /* input from stdin stream */
Int8 MDC_FILE_STDOUT; /* output to stdout stream */
Int8 MDC_FILE_SPLIT; /* split up file in parts */
Int8 MDC_FILE_STACK; /* stack up files */
Int8 MDC_FLIP_HORIZONTAL; /* flip horizontal (x) */
Int8 MDC_FLIP_VERTICAL; /* flip vertical (y) */
Int8 MDC_SORT_REVERSE; /* reverse sorting */
Int8 MDC_SORT_CINE_APPLY; /* cine apply sorting */
Int8 MDC_SORT_CINE_UNDO; /* cine undo sorting */
Int8 MDC_MAKE_SQUARE; /* make square image */
Int8 MDC_CROP_IMAGES; /* crop image dimensions */
Int8 MDC_RESLICE; /* reslice images (tra, sag, cor) */
Int8 MDC_FRMT_INPUT; /* format used for stdin */
Int8 MDC_ECAT6_SORT; /* ECAT sort order */
Int8 MDC_FALLBACK_FRMT; /* fallback read format */
-----------------------------------------------------------------------
Important Defines
-----------------------------------------------------------------------
/* representation of supported formats */
#define MDC_FRMT_NONE 0 /* unsupported format */
#define MDC_FRMT_RAW 1 /* Read Interactive */
/* Write RAW Binary */
#define MDC_FRMT_ASCII 2 /* Write RAW Ascii */
#define MDC_FRMT_GIF 3 /* GIF89a or GIF87a */
#define MDC_FRMT_ACR 4 /* Acr/Nema 2.0 (Papyrus) */
#define MDC_FRMT_INW 5 /* INW (RUG) */
#define MDC_FRMT_ECAT6 6 /* Siemens/CTI ECAT 6.4 */
#define MDC_FRMT_ECAT7 7 /* Siemens/CTI ECAT 7.2 */
#define MDC_FRMT_INTF 8 /* Interfile v3.3 */
#define MDC_FRMT_ANLZ 9 /* Analyze (SPM) */
#define MDC_FRMT_DICM 10 /* DICOM 3.0 */
#define MDC_FRMT_PNG 11 /* PNG */
#define MDC_FRMT_CONC 12 /* Concorde uPet */
#define MDC_FRMT_NIFTI 13 /* NIfTI-1 */
#define MDC_MAX_FMTS 14 /* total + 1 */
/* supported color maps */
#define MDC_MAP_PRESENT 0 /* 256 RGB colormap */
#define MDC_MAP_GRAY 1 /* grayscale colormap */
#define MDC_MAP_INVERTED 2 /* inverted colormap */
#define MDC_MAP_RAINBOW 3 /* rainbow colormap */
#define MDC_MAP_COMBINED 4 /* combined colormap */
#define MDC_MAP_HOTMETAL 5 /* hotmetal colormap */
#define MDC_MAP_LOADED 6 /* extern LUT loaded */
-----------------------------------------------------------------------
Important Structures
-----------------------------------------------------------------------
/* static related data */
typedef struct Static_Data_t {
char label[MDC_MAXSTR]; /* label name of image */ /* Ant/Post */
float total_counts; /* total counts in image */
float image_duration; /* duration of image (ms) */
Int16 start_time_hour; /* start time hour */
Int16 start_time_minute; /* start time minute */
Int16 start_time_second; /* start time second */
} STATIC_DATA;
/* gated SPECT related data */
typedef struct Gated_Data_t {
Int8 gspect_nesting; /* gated spect nesting */
float nr_projections; /* number of projections */
float extent_rotation; /* extent of rotation */
float study_duration; /* study duration (ms) */
float image_duration; /* image duration (ms) */
float time_per_proj; /* time per proj (ms) */
float window_low; /* lower limit (ms) */
float window_high; /* higher limit (ms) */
float cycles_observed; /* cardiac cycles observed*/
float cycles_acquired; /* cardiac cycles acquired*/
} GATED_DATA;
/* acquisition data */
typedef struct Acquisition_Data_t {
Int16 rotation_direction; /* direction of rotation */
Int16 detector_motion; /* type detector motion */
float rotation_offset; /* centre rotation offset */
float radial_position; /* radial position */
float angle_start; /* start angle (interfile)*/ /* 180 - dicom */
float angle_step; /* angular step */
float scan_arc; /* angular range */
} ACQ_DATA;
/* dynamic data */
typedef struct Dynamic_Data_t {
Uint32 nr_of_slices; /* images in time frame */
float time_frame_start; /* start time frame (ms) */
float time_frame_delay; /* delay this frame (ms) */
float time_frame_duration; /* duration frame (ms) */
float delay_slices; /* delay each slice (ms) */
} DYNAMIC_DATA;
/* bed data */
typedef struct Bed_Data_t {
float hoffset; /* horizon. position (mm) */
float voffset; /* vertical position (mm) */
} BED_DATA;
/* images related data */
typedef struct Image_Data_t {
/* ** general data ** */
Uint32 width,height; /* image dimension */
Int16 bits,type; /* bits/pixel & datatype */
Uint16 flags; /* extra flag */
double min, max; /* min/max pixelvalue */
double qmin, qmax; /* quantified min/max */
double fmin, fmax; /* min/max in whole frame */
double qfmin, qfmax; /* in whole frame (quant) */
float rescale_slope; /* rescale slope */ /* auto filled */
float rescale_intercept; /* rescale intercept */ /* auto filled */
Uint32 frame_number; /* part of frame (1-based)*/ /* auto filled */
float slice_start; /* start of slice (ms) */ /* auto filled */
Uint8 *buf; /* pointer to raw image */
off_t load_location; /* load start in file */
/* ** internal items ** */
Int8 rescaled; /* rescaled image? */
double rescaled_min; /* new rescaled max */
double rescaled_max; /* new rescaled min */
double rescaled_fctr; /* new rescaled fctr */
double rescaled_slope; /* new rescaled slope */
double rescaled_intercept; /* new rescaled intercept */
/* ** ecat64 items ** */
Int16 quant_units; /* quantification units */
Int16 calibr_units; /* calibration units */
float quant_scale; /* quantification scale */
float calibr_fctr; /* calibration factor */
float intercept; /* scale intercept */
float pixel_xsize; /* pixel size X (mm) */
float pixel_ysize; /* pixel size Y (mm) */
float slice_width; /* slice width (mm) */
float recon_scale; /* recon magnification */
/* ** Acr/Nema items ** */
float image_pos_dev[3]; /* image posit dev (mm) */
float image_orient_dev[6]; /* image orient dev (mm) */
float image_pos_pat[3]; /* image posit pat (mm) */
float image_orient_pat[6]; /* image orient pat (mm) */
float slice_spacing; /* space btw centres (mm) */
float ct_zoom_fctr; /* CT image zoom factor */
/* ** Miscellaneous ** */
STATIC_DATA *sdata; /* extra static entries */ /* just one */
unsigned char *plugb; /* like to attach here? */
} IMG_DATA;
/* the file information struct */
typedef struct File_Info_t {
FILE *ifp; /* pointer to input file */
FILE *ifp_raw; /* pointer to raw input */
FILE *ofp; /* pointer to output file */
FILE *ofp_raw; /* pointer to raw output */
char ipath[MDC_MAX_PATH+1]; /* path to input file */
char opath[MDC_MAX_PATH+1]; /* path to output file */
char *idir; /* dir to input file */
char *odir; /* dir to output file */
char *ifname; /* name of input file */
char *ofname; /* name of output file */
int iformat; /* format of input file */
int oformat; /* format of output file */
int modality; /* modality */
Int8 rawconv; /* FRMT_RAW | FRMT_ASCII */
Int8 endian; /* endian of file */
Int8 compression; /* file compression */
Int8 truncated; /* truncated file? */
Int8 diff_type; /* images with diff type? */
Int8 diff_size; /* images with diff size? */
Int8 diff_scale; /* images with diff scale?*/
Uint32 number; /* total number of images */ /* private */
Uint32 mwidth,mheight; /* global max dimensions */
Int16 bits, type; /* global bits & datatype */
Int16 dim[MDC_MAX_DIMS]; /* [0] = # of dimensions */
/* [1] = X-dim (pixels) */
/* [2] = Y-dim (pixels) */
/* [3] = Z-dim (planes) */
/* [4] = (frames) */
/* [5] = (gates) */
/* [6] = (beds) */
/* ... */
/* values must be 1-based */
float pixdim[MDC_MAX_DIMS]; /* [0] = # of dimensions */
/* [1] = X-dim (mm) */
/* [2] = Y-dim (mm) */
/* [3] = Z-dim (mm) */
/* [4] = time (ms) */
/* ... */
double glmin, glmax; /* global min/max value */
double qglmin, qglmax; /* quantified min/max */
Int8 contrast_remapped; /* contrast remap applied */
float window_centre; /* contrast window centre */
float window_width; /* contrast window width */
Int8 slice_projection; /* projection of images */
Int8 pat_slice_orient; /* combined flag */
char pat_pos[MDC_MAXSTR]; /* patient position */
char pat_orient[MDC_MAXSTR]; /* patient orientation */
char patient_sex[MDC_MAXSTR]; /* sex of patient */
char patient_name[MDC_MAXSTR];/* name of patient */
char patient_id[MDC_MAXSTR]; /* id of patient */
char patient_dob[MDC_MAXSTR]; /* birth of patient */ /* YYYYMMDD */
float patient_weight; /* weight of patient (kg) */
float patient_height; /* height of patient (m) */
char operator_name[MDC_MAXSTR];/* name of scan operator */
char study_descr[MDC_MAXSTR]; /* study description */
char study_id[MDC_MAXSTR]; /* study id */
Int16 study_date_day; /* day of study (1-31) */
Int16 study_date_month; /* month of study (1-12) */
Int16 study_date_year; /* year of study */
Int16 study_time_hour; /* hour of study */
Int16 study_time_minute; /* minute of study */
Int16 study_time_second; /* second of study */
Int16 dose_time_hour; /* hour of dose start */
Int16 dose_time_minute; /* minute of dose start */
Int16 dose_time_second; /* second of dose start */
Int32 nr_series; /* series number */
Int32 nr_acquisition; /* acquisition number */
Int32 nr_instance; /* instance number */
Int16 acquisition_type; /* acquisition type */
Int16 planar; /* planar or tomo ? */
Int16 decay_corrected; /* decay corrected ? */
Int16 flood_corrected; /* flood corrected ? */
Int16 reconstructed; /* reconstructed ? */
char recon_method[MDC_MAXSTR]; /* reconstruction method */
char institution[MDC_MAXSTR]; /* name of institution */
char manufacturer[MDC_MAXSTR]; /* name of manufacturer */
char series_descr[MDC_MAXSTR]; /* series description */
char radiopharma[MDC_MAXSTR]; /* radiopharmaceutical */
char filter_type[MDC_MAXSTR]; /* filter type */
char organ_code[MDC_MAXSTR]; /* organ */
char isotope_code[MDC_MAXSTR]; /* isotope */
float isotope_halflife; /* isotope halflife (sec) */
float injected_dose; /* amount injected (MBq) */
float gantry_tilt; /* gantry tilt */
Uint8 map; /* indexed 256 colormap */
Uint8 palette[768]; /* global palette */
char *comment; /* whatever comment */
Uint32 comm_length; /* length of comment */
Uint32 gatednr; /* number of gated entries*/ /* now 0 or 1 */
GATED_DATA *gdata; /* array of GATED_DATA */
Uint32 acqnr; /* number acq. entries */
ACQ_DATA *acqdata; /* array ACQ_DATA entries */
Uint32 dynnr; /* number of time frames */
DYNAMIC_DATA *dyndata; /* array of DYNAMIC_DATA */
Uint32 bednr; /* number bed positions */
BED_DATA * beddata; /* array of BED_DATA */
IMG_DATA *image; /* array IMG_DATA images */
MOD_INFO *mod; /* modality related info */
unsigned char *pluga; /* want to attach stuff? */
} FILEINFO;
------------------------------------------------------------------------
Important Functions
------------------------------------------------------------------------
void MdcInit (void);
void MdcFinish (void);
int MdcHandleArgs ( FILEINFO *fi, int argc, char *argv[], int MAXFILES );
void MdcPrintUsage ( char *pgrname );
int MdcOpenFile ( FILEINFO *fi, char *path );
int MdcReadFile ( FILEINFO *fi, int filenr, char *(*ReadFunc)(FILEINFO *fi) );
int MdcWriteFile ( FILEINFO *fi, int format, int prefixnr, char *(*WriteFunc)(FILEINFO *fi) );
void MdcInitFI ( FILEINFO *fi, char *path );
void MdcFreeIDs ( FILEINFO *fi );
void MdcCleanUpFI ( FILEINFO *fi );
void MdcResetIDs ( FILEINFO *fi );
void MdcPrintFI ( FILEINFO *fi );
void MdcCloseFile ( FILE *fp );
void MdcSplitPath ( char path[], char **dir, char **fname );
int MdcGetFrmt ( FILEINFO *fi );
void MdcGetColorMap ( int map, Uint8 palette[] );
char *MdcImagesPixelFiddle( FILEINFO *fi);
void MdcPrntMesg ( char *fmt, ... );
void MdcPrntWarn ( char *fmt, ... );
void MdcPrntErr ( int code, char *fmt, ... );
char *MdcReadACR ( FILEINFO *fi );
char *MdcReadANLZ ( FILEINFO *fi );
char *MdcReadCONC ( FILEINFO *fi );
char *MdcReadDICM ( FILEINFO *fi );
char *MdcReadECAT6 ( FILEINFO *fi );
char *MdcReadECAT7 ( FILEINFO *fi );
char *MdcReadGIF ( FILEINFO *fi );
char *MdcReadINTF ( FILEINFO *fi );
char *MdcReadINW ( FILEINFO *fi );
char *MdcReadNIFTI ( FILEINFO *fi );
char *MdcReadPNG ( FILEINFO *fi );
char *MdcReadRAW ( FILEINFO *fi );
char *MdcWriteACR ( FILEINFO *fi );
char *MdcWriteANLZ ( FILEINFO *fi );
char *MdcWriteCONC ( FILEINFO *fi );
char *MdcWriteDICM ( FILEINFO *fi );
char *MdcWriteECAT6 ( FILEINFO *fi );
char *MdcWriteECAT7 ( FILEINFO *fi );
char *MdcWriteGIF ( FILEINFO *fi );
char *MdcWriteINTF ( FILEINFO *fi );
char *MdcWriteINW ( FILEINFO *fi );
char *MdcWriteNIFTI ( FILEINFO *fi );
char *MdcWritePNG ( FILEINFO *fi );
char *MdcWriteRAW ( FILEINFO *fi );