File: m-split.c

package info (click to toggle)
xmedcon 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,928 kB
  • ctags: 5,004
  • sloc: ansic: 47,536; sh: 13,052; makefile: 383
file content (395 lines) | stat: -rw-r--r-- 11,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * filename: m-split.c                                                     *
 *                                                                         *
 * UTIL C-source: Medical Image Conversion Utility                         *
 *                                                                         *
 * purpose      : split file as specified                                  *
 *                                                                         *
 * project      : (X)MedCon by Erik Nolf                                   *
 *                                                                         *
 * Functions    : MdcGetSplitAcqType()    - Get new acquisition type       *
 *                MdcGetNrSplit()         - Get current split index        *
 *                MdcGetSplitBaseName()   - Get basename without prefix    *
 *                MdcUpdateSplitPrefix()  - Update prefix for filename     *
 *                MdcCopySlice()          - Copy specified slice in new FI *
 *                MdcCopyFrame()          - Copy specified frame in new FI *
 *                MdcSplitSlices()        - Write each slice to a file     *
 *                MdcSplitFrames()        - Write each frame to a file     *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* $Id: m-split.c,v 1.32 2013/06/23 21:51:19 enlf Exp $
 */

/*
   Copyright (C) 1997-2013 by Erik Nolf

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any later
   version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
   Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   59 Place - Suite 330, Boston, MA 02111-1307, USA.  */

/****************************************************************************
                              H E A D E R S
****************************************************************************/

#include "m-depend.h"

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#ifndef _WIN32
#include <strings.h>
#endif
#endif

#include "medcon.h"

/****************************************************************************
                              D E F I N E S 
****************************************************************************/

static Uint32 mdc_nrsplit=0;

/****************************************************************************
                            F U N C T I O N S
****************************************************************************/

Int16 MdcGetSplitAcqType(FILEINFO *fi)
{
  Int16 type = MDC_ACQUISITION_TOMO; /* default */

  if (fi->planar && (fi->acquisition_type == MDC_ACQUISITION_STATIC))
    type = MDC_ACQUISITION_STATIC;

  if (fi->planar && (fi->acquisition_type == MDC_ACQUISITION_DYNAMIC))
    type = MDC_ACQUISITION_DYNAMIC;

  return(type);

}

Uint32 MdcGetNrSplit(void)
{
  return(mdc_nrsplit);
}

char *MdcGetSplitBaseName(char *path)
{
  char *p, *bname;

  /* terminate path */
  p = MdcGetLastPathDelim(path);
  if (p != NULL) {
    p[0]='\0'; bname = p + 1;
  }else{
    bname = path;
  }

  /* get pure basename without prefix we need to update */
  if (bname[0]=='m' && bname[4]=='-' && bname[10]=='-' &&
     (bname[5]=='s' || bname[5]=='f')) {
     bname += 11;
  }

  return(bname);

}

void MdcUpdateSplitPrefix(char *dpath, char *spath, char *bname, int nr )
{
  MdcPrefix(nr);
  strcpy(dpath,spath);
  strcat(dpath,MDC_PATH_DELIM_STR);
  strcat(dpath,prefix);
  strcat(dpath,bname);
}

/* copy a single image slice (zero-based number) */
char *MdcCopySlice(FILEINFO *ofi, FILEINFO *ifi, Uint32 slice0)
{
  char *msg;
  IMG_DATA *idin, *idout;
  DYNAMIC_DATA *dd;
  Uint32 i;

  /* copy original FILEINFO struct */
  msg = MdcCopyFI(ofi,ifi,MDC_NO,MDC_NO); if (msg != NULL) return(msg);

  /* preserve dynamic data of single slice */ 
  idin = &ifi->image[slice0];
  if (!MdcGetStructDD(ofi,1)) return("Couldn't malloc DYNAMIC_DATA struct");
  dd = &ofi->dyndata[0]; 
  dd->nr_of_slices = 1;
  dd->time_frame_start    = idin->slice_start;
  dd->time_frame_duration = MdcSingleImageDuration(ifi,idin->frame_number-1);
  /* MARK: frame_delay as alternative for slice_start
  dd->time_frame_delay    = idin->slice_start; 
   */

  /* single slice parameters */
  ofi->dim[0]=3; ofi->pixdim[0]=3.; ofi->dim[3] = 1; ofi->pixdim[3] = 1.;
  for(i=4; i<MDC_MAX_DIMS; i++) { ofi->dim[i]=1; ofi->pixdim[i]=1.; }
  ofi->acquisition_type = MdcGetSplitAcqType(ifi);

  /* get new IMG_DATA struct for slice */
  ofi->image = NULL;
  if (!MdcGetStructID(ofi,1)) 
    return("Couldn't malloc new IMG_DATA struct"); 

  /* copy IMG_DATA struct */
  idin = &ifi->image[slice0];
  idout= &ofi->image[0];

  msg = MdcCopyID(idout,idin,MDC_YES); if (msg != NULL) return(msg);

  idout->frame_number = 1; /* single frame */
 
  /* integrity check of FILEINFO struct */
  if ( (msg=MdcCheckFI(ofi)) != NULL) return(msg);

  return(NULL);

}

/* copy a (time) frame of images (zero-based number) */
char *MdcCopyFrame(FILEINFO *ofi, FILEINFO *ifi, Uint32 frame0)
{
  char *msg;
  IMG_DATA *idin, *idout;
  DYNAMIC_DATA *dd;
  Uint32 i, begin, slices;

  /* copy FILEINFO struct */
  msg = MdcCopyFI(ofi,ifi,MDC_NO,MDC_NO); if (msg != NULL) return(msg);

  /* preserve corresponding dynamic data */
  if ((ifi->dynnr > 0) && (ifi->dyndata != NULL)) {
    if (frame0 < ifi->dynnr) {
      if (!MdcGetStructDD(ofi,1))
        return("Couldn't malloc DYNAMIC_DATA struct");
      MdcCopyDD(&ofi->dyndata[0],&ifi->dyndata[frame0]);
    }
  }

  /* get begin and total slices of frame */
  if (ifi->planar && (ifi->acquisition_type == MDC_ACQUISITION_DYNAMIC)) {
    dd = &ifi->dyndata[frame0];
    slices = (frame0<ifi->dynnr) ? dd->nr_of_slices : ifi->dim[3];
    for (begin=0, i=0; i<frame0; i++) begin += ifi->dyndata[i].nr_of_slices;
  }else{
    slices = (Uint32)ifi->dim[3];
    begin  = slices * frame0;
  }

  /* set single frame parameters */
  ofi->dim[0] = 3; ofi->pixdim[0]=3.;
  ofi->dim[3] = (Int16)slices;
  for(i=4; i<MDC_MAX_DIMS; i++) {
     ofi->dim[i]=1;
     ofi->pixdim[i]=1.;
  }

  MdcDebugPrint("output slices = %d",slices);
  
  ofi->acquisition_type = MdcGetSplitAcqType(ifi);

  /* disable ACQ_DATA structs */
  /* ofi->acqnr = 0; ofi->acqdata = NULL; */

  /* get new IMG_DATA structs for slices */
  ofi->image = NULL;
  if (!MdcGetStructID(ofi,slices)) 
    return("Couldn't malloc new IMG_DATA structs"); 

  /* copy IMG_DATA information */
  for (i=0; i < slices; i++) {

    /* copy IMG_DATA struct */
    idin = &ifi->image[begin+i];
    idout= &ofi->image[i];
    msg = MdcCopyID(idout,idin,MDC_YES); if (msg != NULL) return(msg);
    idout->frame_number = 1; /* single frame */
  }

  /* integrity check of FILEINFO struct */
  if ( (msg=MdcCheckFI(ofi)) != NULL) return(msg);

  return(NULL);

}

char *MdcSplitSlices(FILEINFO *fi, int format, int prefixnr)
{
  FILEINFO *ofi;
  Uint32 nr_of_slices;
  Int32 instance=0, series=0;
  char *msg, *tpath=NULL, *bname=NULL;

  /* alloc temp struct, path */
  ofi = (FILEINFO *)malloc(sizeof(FILEINFO));
  if (ofi == NULL) return("Couldn't malloc output struct");

  tpath = (char *)malloc(MDC_MAX_PATH);
  if (tpath == NULL) return("Couldn't malloc tpath");
  if (XMDC_GUI == MDC_NO) {
    MdcGetSafeString(tpath,fi->ifname,strlen(fi->ifname),MDC_MAX_PATH);
  }else{
    /* terminate path and get basename */
    MdcGetSafeString(tpath,fi->ofname,strlen(fi->ofname),MDC_MAX_PATH);
    bname = MdcGetSplitBaseName(tpath);
  }

  /* preserve & initialize series number */
  series = fi->nr_series; fi->nr_series = (Int32)prefixnr + 1;

  /* preserve & initialize instance number */
  instance = fi->nr_instance; fi->nr_instance = 0;

  /* split up all slices */
  nr_of_slices = fi->number;
  for (mdc_nrsplit=0; mdc_nrsplit < nr_of_slices; mdc_nrsplit++) {

     /* increment instance for each slice */
     fi->nr_instance = (Int32)mdc_nrsplit + 1;

     msg = MdcCopySlice(ofi,fi,mdc_nrsplit);
     if (msg != NULL) {
       fi->nr_instance = instance;
       MdcCleanUpFI(ofi); MdcFree(ofi);
       MdcFree(tpath);
       return("Failure to copy slice");
     }

     /* prepare filename */
     if (XMDC_GUI == MDC_NO) {
       strcpy(ofi->ipath,tpath); ofi->ifname = ofi->ipath;
     }else{
       MdcUpdateSplitPrefix(ofi->opath,tpath,bname,prefixnr);
       ofi->ofname = ofi->opath;
     }

     if (MdcWriteFile(ofi, format, prefixnr, NULL) != MDC_OK) {
       fi->nr_instance = instance;
       MdcCleanUpFI(ofi); MdcFree(ofi);
       MdcFree(tpath);
       return("Failure to write split slice");
     } 

     MdcCleanUpFI(ofi);
  }

  /* free mallocs */
  MdcFree(ofi);
  MdcFree(tpath);

  /* restore series */
  fi->nr_series = series;

  /* restore instance */
  fi->nr_instance = instance;

  return(NULL);

}

char *MdcSplitFrames(FILEINFO *fi, int format, int prefixnr)
{
  FILEINFO *ofi;
  Int32 instance=0, series=0;
  Uint32 i, nr_of_frames=1;
  char *msg, *tpath=NULL, *bname=NULL, *p=NULL;

  /* alloc temp struct, path */
  ofi = (FILEINFO *)malloc(sizeof(FILEINFO));
  if (ofi == NULL) return("Couldn't malloc output struct");

  tpath = (char *)malloc(MDC_MAX_PATH);
  if (tpath == NULL) return("Couldn't malloc tpath");
  if (XMDC_GUI == MDC_NO) {
    MdcGetSafeString(tpath,fi->ifname,strlen(fi->ifname),MDC_MAX_PATH);
  }else{
    MdcGetSafeString(tpath,fi->ofname,strlen(fi->ofname),MDC_MAX_PATH);
    p = MdcGetLastPathDelim(tpath);
    if (p != NULL) {
      p[0]='\0'; bname = p + 1;
    }else{
      bname = tpath;
    }
    /* get pure basename without prefix we need to update */
    if (bname[0]=='m' && bname[4]=='-' && bname[10]=='-' &&
       (bname[5]=='s' || bname[5]=='f')) {
       bname += 11;
    }
  }

  /* preserve & initialize series number */
  series = fi->nr_series; fi->nr_series = (Int32)prefixnr + 1;

  /* preserve & initialize instance number */
  instance = fi->nr_instance; fi->nr_instance = 0;

  if (fi->planar && (fi->acquisition_type == MDC_ACQUISITION_DYNAMIC)) {
    nr_of_frames = fi->dynnr;
  }else{
    for (i=4; i<MDC_MAX_DIMS; i++)  nr_of_frames *= (Uint32)fi->dim[i];
  }

  /* split up all frames */
  for (mdc_nrsplit=0; mdc_nrsplit < nr_of_frames; mdc_nrsplit++) {
 
     /* increment instance for each frame */
     fi->nr_instance = (Int32)mdc_nrsplit + 1;

     msg = MdcCopyFrame(ofi,fi,mdc_nrsplit);
     if (msg != NULL) {
       fi->nr_instance = instance;
       MdcCleanUpFI(ofi); MdcFree(ofi);
       MdcFree(tpath);
       return("Failure to copy frame");
     }

     /* prepare filename */
     if (XMDC_GUI == MDC_NO) {
       strcpy(ofi->ipath,tpath); ofi->ifname = ofi->ipath;
     }else{
       MdcUpdateSplitPrefix(ofi->opath,tpath,bname,prefixnr);
       ofi->ofname = ofi->opath;
     }

     if (MdcWriteFile(ofi, format, prefixnr, NULL) != MDC_OK) {
       fi->nr_instance = instance;
       MdcCleanUpFI(ofi); MdcFree(ofi);
       MdcFree(tpath);
       return("Failure to write split frame");
     } 

     MdcCleanUpFI(ofi);
  }

  /* free mallocs */
  MdcFree(ofi);
  MdcFree(tpath);

  /* restore series */
  fi->nr_series = series;

  /* restore instance */
  fi->nr_instance = instance;

  return(NULL);

}