Debian Codegraph @

conversion/dicomserver/siemens_dicom_to_minc.c

raw symbols SCIP
1 /* ----------------------------- MNI Header -----------------------------------
2 @NAME : siemens_dicom_to_minc.c
3 @DESCRIPTION: Code to convert a list of Siemens dicom files to minc
4 format.
5 @METHOD :
6 @GLOBALS :
7 @CALLS :
8 @CREATED : January 28, 1997 (Peter Neelin)
9 @MODIFIED :
10 * $Log: siemens_dicom_to_minc.c,v $
11 * Revision 6.1 1999-10-29 17:51:59 neelin
12 * Fixed Log keyword
13 *
14 * Revision 6.0 1997/09/12 13:24:27 neelin
15 * Release of minc version 0.6
16 *
17 * Revision 5.0 1997/08/21 13:25:26 neelin
18 * Release of minc version 0.5
19 *
20 * Revision 4.0 1997/05/07 20:06:20 neelin
21 * Release of minc version 0.4
22 *
23 * Revision 1.1 1997/03/04 20:56:47 neelin
24 * Initial revision
25 *
26 @COPYRIGHT :
27 Copyright 1997 Peter Neelin, McConnell Brain Imaging Centre,
28 Montreal Neurological Institute, McGill University.
29 Permission to use, copy, modify, and distribute this
30 software and its documentation for any purpose and without
31 fee is hereby granted, provided that the above copyright
32 notice appear in all copies. The author and McGill University
33 make no representations about the suitability of this
34 software for any purpose. It is provided "as is" without
35 express or implied warranty.
36 ---------------------------------------------------------------------------- */
37
38 #include <dicomserver.h>
39
40 extern int Do_logging;
41
42 /* ----------------------------- MNI Header -----------------------------------
43 @NAME : siemens_dicom_to_minc
44 @INPUT : num_files - number of image files
45 file_list - list of file names
46 minc_file - name of output minc file (NULL means make one
47 up)
48 clobber - if TRUE, then open the output with NC_CLOBBER
49 file_prefix - string providing any directory or prefix
50 for internally generated filename (if it is a directory,
51 then it must contain the last "/")
52 @OUTPUT : output_file_name - returns a pointer to an internal area
53 containing the file name of the created file if minc_file
54 is NULL, or simply a pointer to minc_file. If NULL, then
55 nothing is returned.
56 @RETURNS : EXIT_SUCCESS if no error, EXIT_FAILURE on error.
57 @DESCRIPTION: Routine to convert a list of Siemens dicom files to minc
58 format.
59 @METHOD :
60 @GLOBALS : Do_logging
61 @CALLS :
62 @CREATED : November 25, 1993 (Peter Neelin)
63 @MODIFIED :
64 ---------------------------------------------------------------------------- */
65 public int siemens_dicom_to_minc(int num_files, char *file_list[],
66 char *minc_file, int clobber,
67 char *file_prefix, char **output_file_name)
68 {
69 Acr_Group group_list;
70 int max_group;
71 File_Info *file_info;
72 General_Info general_info;
73 Image_Data image;
74 int icvid;
75 int ifile;
76 Mri_Index imri;
77 char *out_file_name;
78 int isep;
79
80 /* Allocate space for the file information */
81 file_info = MALLOC(num_files * sizeof(*file_info));
82
83 /* Last group needed for first pass */
84 max_group = ACR_ACTUAL_IMAGE_GID - 1;
85
86 /* Add all control characters as numeric array separators to handle
87 odd behaviour with Siemens dicom files */
88 for (isep=0; isep < 31; isep++) {
89 (void) acr_element_numeric_array_separator(isep);
90 }
91
92 /* Initialize some values for general info */
93 general_info.initialized = FALSE;
94 general_info.group_list = NULL;
95 for (imri=0; imri < MRI_NDIMS; imri++) {
96 general_info.indices[imri] = NULL;
97 general_info.coordinates[imri] = NULL;
98 }
99
100 /* Loop through file list getting information */
101 for (ifile=0; ifile < num_files; ifile++) {
102
103 /* Read the file */
104 group_list = read_siemens_dicom(file_list[ifile], max_group);
105
106 /* Get file-specific information */
107 get_file_info(group_list, &file_info[ifile], &general_info);
108
109 /* Delete the group list */
110 acr_delete_group_list(group_list);
111
112 /* Print log message if not using file */
113 if (!file_info[ifile].valid) {
114 if (Do_logging >= LOW_LOGGING) {
115 (void) fprintf(stderr, "Not using file %s\n",
116 file_list[ifile]);
117 }
118
119 }
120
121 }
122
123 /* Sort the dimensions */
124 sort_dimensions(&general_info);
125
126 /* Create the output file */
127 if (general_info.initialized) {
128 icvid = create_minc_file(minc_file, clobber, &general_info,
129 file_prefix, &out_file_name);
130 }
131 if (output_file_name != NULL)
132 *output_file_name = out_file_name;
133
134 /* Check that we found the general info and that the minc file was
135 created okay */
136 if ((!general_info.initialized) || (icvid == MI_ERROR)) {
137 if (general_info.initialized) {
138 (void) fprintf(stderr, "Error creating minc file %s.\n",
139 out_file_name);
140 }
141 free_info(&general_info, file_info, num_files);
142 FREE(file_info);
143 return EXIT_FAILURE;
144 }
145
146 /* We now read all groups */
147 max_group = 0;
148
149 /* Loop through the files again and put images into the minc file */
150 for (ifile=0; ifile < num_files; ifile++) {
151
152 /* Check that we have a valid file */
153 if (!file_info[ifile].valid) {
154 continue;
155 }
156
157 /* Read the file */
158 group_list = read_siemens_dicom(file_list[ifile], max_group);
159
160 /* Get image */
161 get_siemens_dicom_image(group_list, &image);
162
163 /* Save the image and any other information */
164 save_minc_image(icvid, &general_info, &file_info[ifile], &image);
165
166 /* Delete the group list */
167 acr_delete_group_list(group_list);
168
169 /* Free the image data */
170 if ((image.data != NULL) && (image.free)) FREE(image.data);
171
172 }
173
174 /* Close the output file */
175 close_minc_file(icvid);
176
177 /* Free the general_info and file_info stuff */
178 free_info(&general_info, file_info, num_files);
179 FREE(file_info);
180
181 return EXIT_SUCCESS;
182 }
183
184 /* ----------------------------- MNI Header -----------------------------------
185 @NAME : read_siemens_dicom
186 @INPUT : filename - name of siemens dicom file to read
187 max_group - maximum group number to read
188 @OUTPUT : (none)
189 @RETURNS : group list read in from file
190 @DESCRIPTION: Routine to read in a group list from a file.
191 @METHOD :
192 @GLOBALS :
193 @CALLS :
194 @CREATED : November 25, 1993 (Peter Neelin)
195 @MODIFIED :
196 ---------------------------------------------------------------------------- */
197 public Acr_Group read_siemens_dicom(char *filename, int max_group)
198 {
199 FILE *fp;
200 Acr_File *afp;
201 Acr_Group group_list;
202
203 /* Open the file */
204 fp = fopen(filename, "r");
205 if (fp == NULL) return NULL;
206
207 /* Connect to input stream */
208 afp=acr_file_initialize(fp, 0, acr_stdio_read);
209
210 /* Read in group list */
211 (void) acr_input_group_list(afp, &group_list, max_group);
212
213 /* Close the file */
214 acr_file_free(afp);
215 (void) fclose(fp);
216
217 return group_list;
218
219 }
220
221 /* ----------------------------- MNI Header -----------------------------------
222 @NAME : free_info
223 @INPUT : general_info
224 file_info
225 num_files
226 @OUTPUT : (none)
227 @RETURNS : (nothing)
228 @DESCRIPTION: Routine to free contents of general and file info structures.
229 @METHOD :
230 @GLOBALS :
231 @CALLS :
232 @CREATED : November 26, 1993 (Peter Neelin)
233 @MODIFIED :
234 ---------------------------------------------------------------------------- */
235 /* ARGSUSED */
236 public void free_info(General_Info *general_info, File_Info *file_info,
237 int num_files)
238 {
239 Mri_Index imri;
240
241 /* Free the general info pointers */
242 for (imri=0; imri < MRI_NDIMS; imri++) {
243 if (general_info->indices[imri] != NULL) {
244 FREE(general_info->indices[imri]);
245 }
246 if (general_info->coordinates[imri] != NULL) {
247 FREE(general_info->coordinates[imri]);
248 }
249 }
250
251 /* Free the group list */
252 if (general_info->group_list != NULL) {
253 acr_delete_group_list(general_info->group_list);
254 }
255
256 return;
257
258 }
259
260 /* ----------------------------- MNI Header -----------------------------------
261 @NAME : search_list
262 @INPUT : value
263 list
264 list_length
265 starting_point - point from which search should start
266 @OUTPUT : (none)
267 @RETURNS : Index in list where value is found, or -1 is value not found.
268 @DESCRIPTION: Routine to search a list for a value, returning the index
269 into the list. If the value is not found, then -1 is returned.
270 @METHOD :
271 @GLOBALS :
272 @CALLS :
273 @CREATED : February 28, 1997 (Peter Neelin)
274 @MODIFIED :
275 ---------------------------------------------------------------------------- */
276 public int search_list(int value, int list[], int list_length,
277 int starting_point)
278 {
279 int index;
280
281 /* Check list length and starting point */
282 if (list_length <= 0) return -1;
283 if ((starting_point >= list_length) || (starting_point < 0)) {
284 starting_point = 0;
285 }
286
287 /* Loop over indices, wrapping at the end of the list */
288 index = starting_point;
289 do {
290 if (list[index] == value) return index;
291 index++;
292 if (index >= list_length) index = 0;
293 } while (index != starting_point);
294
295 /* If we get to here, we didn't find the value */
296 return -1;
297 }
298
299 /* ----------------------------- MNI Header -----------------------------------
300 @NAME : sort_dimensions
301 @INPUT : general_info
302 @OUTPUT : general_info
303 @RETURNS : (nothing)
304 @DESCRIPTION: Routine to sort the MRI dimensions according to their
305 coordinates. It also fills in the step and start values for
306 the SLICE dimension.
307 @METHOD :
308 @GLOBALS :
309 @CALLS :
310 @CREATED : February 28, 1997 (Peter Neelin)
311 @MODIFIED :
312 ---------------------------------------------------------------------------- */
313 public void sort_dimensions(General_Info *general_info)
314 {
315 Mri_Index imri;
316 Sort_Element *sort_array;
317 int nvalues, ival, jval;
318 int reverse_array;
319
320 /* Sort the dimensions, if needed */
321 for (imri = 0; imri < MRI_NDIMS; imri++) {
322 if (general_info->size[imri] > 1) {
323
324 /* Set up the array for sorting */
325 nvalues = general_info->size[imri];
326 sort_array = MALLOC(nvalues * sizeof(*sort_array));
327 for (ival=0; ival < nvalues; ival++) {
328 sort_array[ival].identifier = general_info->indices[imri][ival];
329 sort_array[ival].original_index = ival;
330 sort_array[ival].value = general_info->coordinates[imri][ival];
331 }
332
333 /* Sort the array */
334 qsort((void *) sort_array, (size_t) nvalues, sizeof(*sort_array),
335 dimension_sort_function);
336
337 /* Figure out if we should reverse the array to keep something
338 similar to the original ordering */
339 reverse_array = (sort_array[0].original_index >
340 sort_array[nvalues-1].original_index);
341
342 /* Copy the information back into the appropriate arrays */
343 for (ival=0; ival < nvalues; ival++) {
344 jval = (reverse_array ? nvalues - ival - 1 : ival);
345 general_info->indices[imri][ival] = sort_array[jval].identifier;
346 general_info->coordinates[imri][ival] = sort_array[jval].value;
347 }
348
349 /* Free the array */
350 FREE(sort_array);
351
352 /* Update slice step and start */
353 if (imri == SLICE) {
354 if (general_info->coordinates[imri][0] !=
355 general_info->coordinates[imri][nvalues-1]) {
356 general_info->step[general_info->slice_world] =
357 (general_info->coordinates[imri][nvalues-1] -
358 general_info->coordinates[imri][0]) /
359 ((double) general_info->size[imri] - 1.0);
360 }
361 general_info->start[general_info->slice_world] =
362 general_info->coordinates[imri][0];
363 }
364
365 } /* If size > 1 */
366 } /* Loop over dimensions */
367
368 }
369
370 /* ----------------------------- MNI Header -----------------------------------
371 @NAME : dimension_sort_function
372 @INPUT : v1, v2 - values to compare
373 @OUTPUT : (none)
374 @RETURNS : -1, 0 or 1 if v1 < v2, v1 == v2 or v1 > v2
375 @DESCRIPTION: Function to compare to array elements for sorting. Elements are
376 compared first on value, then on their original array index
377 (this tries to preserve the original sequence).
378 @METHOD :
379 @GLOBALS :
380 @CALLS :
381 @CREATED : February 28, 1997 (Peter Neelin)
382 @MODIFIED :
383 ---------------------------------------------------------------------------- */
384 public int dimension_sort_function(const void *v1, const void *v2)
385 {
386 Sort_Element *value1, *value2;
387
388 value1 = (Sort_Element *) v1;
389 value2 = (Sort_Element *) v2;
390
391 if (value1->value < value2->value)
392 return -1;
393 else if (value1->value > value2->value)
394 return 1;
395 else if (value1->original_index < value2->original_index)
396 return -1;
397 else if (value1->original_index > value2->original_index)
398 return 1;
399 else
400 return 0;
401 }
402