~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-tools/tinatool/tlbase/tlbaseSeq_io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/tlbase/tlbaseSeq_io.c,v $
 37  * Date    :  $Date: 2005/12/20 16:34:48 $
 38  * Version :  $Revision: 1.10 $
 39  * CVS Id  :  $Id: tlbaseSeq_io.c,v 1.10 2005/12/20 16:34:48 matts Exp $
 40  *
 41  * Notes : Functions pertaining to loading and saving 
 42  *
 43  *********
 44 */
 45 
 46 #include "tlbaseSeq_io.h"
 47 
 48 #if HAVE_CONFIG_H
 49 #   include <config.h>
 50 #endif
 51 
 52 #include <stdio.h>
 53 #include <string.h>
 54 #include <limits.h>
 55 #include <tina/file/fileDef.h>
 56 #include <tina/file/filePro.h>
 57 #include <tina/image/imgDef.h>
 58 #include <tina/image/imgPro.h>
 59 #include <tinatool/tlbase/tlbase_SeqDef.h>
 60 #include <tinatool/tlbase/tlbase_SeqPro.h>
 61 
 62 /*static char   *filename = NULL; should be in appropriate functions? */
 63 /* function to take sequence and file type and works out correct sequence 
 64    filenames, and calls the particular file_type i/o routines */
 65 /* no longer returns iscale structure as these are assigned inthe lower
 66    level input functions. perhaps wrongly */
 67 /* NB all the set_frame_no stuff is probably totally pointless */
 68 
 69 
 70 static void seq_read_images(Sequence *seq, int end, int file_type)
 71 {
 72 
 73   Imrect  *im;
 74   int      i;
 75   char    *pfname[1];
 76   /*char **pfname;*/
 77   Vec3    *iscale=NULL;
 78   int offset, stride, filename_length;
 79   char filename[512];
 80   char temp[512];
 81   offset = seq->offset;
 82   stride = seq->stride;
 83 
 84   /* These are in the wrong place, it is entirely possible to change the filename 
 85      length, particularly in DICOM if you have more than single digit figures. */
 86   /*filename_length = strlen(seq->filename);
 87     temp = cvector_alloc(0, filename_length+1); */
 88 
 89   switch (file_type)
 90     {
 91     case SEQTL_AIFF_FILE:
 92       
 93            
 94       for(i = offset; i <= end; i = i+stride)
 95         {
 96           int extra = 0;
 97           char digit_len[10]; /* assuming no-one will enter a 9 digit number */
 98           
 99           extra = sprintf(digit_len, "%d", i)-1;
100           /* NB the hash used in the filename counts as one digit */
101           filename_length = strlen(seq->filename) + extra;
102           
103           parse_fname(seq->filename,temp,i);
104           sprintf(filename, "%s%s", temp, ".aiff");
105           *pfname = filename;
106           if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
107             {
108               error("seq_input: no such file or filename", warning);
109               return; 
110             }
111           
112           if ((im = (Imrect *)aiff_read_image(*pfname)) != NULL)
113             {
114               seq_frame_insert(im, seq);
115             }
116         }
117      
118       break;
119       
120     case SEQTL_ANLZ_FILE:
121       
122       for(i = offset; i <= end; i = i+stride)
123         {
124           
125           if ((im = (Imrect *)ani_read_image(seq->filename,i)) != NULL)
126             {
127               seq_frame_insert(im, seq);
128               
129             }
130         }
131       break;
132       
133     case SEQTL_RAD_FILE:
134       
135            
136       for(i = offset; i <= end; i = i+stride)
137         {
138 
139           int extra = 0;
140           char digit_len[10]; /* assuming no-one will enter a 9 digit number */
141 
142           extra = sprintf(digit_len, "%d", i)-1;
143           filename_length = strlen(seq->filename) + extra;
144           
145           parse_fname(seq->filename,temp,i);
146           sprintf(filename, "%s%s", temp, ".rad");
147           *pfname = filename;
148           if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
149             {
150               error("seq_input: no such file or filename", warning);
151               return;
152             }
153           
154           if ((im = (Imrect *)rad_read_image(*pfname)) != NULL)
155             { 
156               seq_frame_insert(im, seq);
157             }
158         }
159       
160       break;
161       
162     case SEQTL_NEMA_FILE:
163       
164            
165       for(i = offset; i <= end; i = i+stride)
166         {
167           int extra = 0;
168           char digit_len[10]; /* assuming no-one will enter a 9 digit number */
169           
170           extra = sprintf(digit_len, "%d", i)-1;
171           filename_length = strlen(seq->filename) + extra;
172 
173           parse_fname(seq->filename,temp,i);
174           sprintf(filename, "%s%s", temp, ".ani");
175           *pfname = filename;
176           if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
177             {
178               error("seq_input: no such file or filename", warning);
179               return; 
180             }
181           
182           if ((im = (Imrect *)nema_read_image(*pfname)) != NULL)
183             {
184               seq_frame_insert(im, seq);
185             }
186         }
187 
188       break;
189       
190     case SEQTL_PGM_FILE:
191 
192           
193       for(i = offset; i <= end; i = i+stride)
194         {
195           int extra = 0;
196           char digit_len[10]; /* assuming no-one will enter a 9 digit number */
197 
198           extra = sprintf(digit_len, "%d", i)-1;
199           filename_length = strlen(seq->filename) + extra;
200 
201           parse_fname(seq->filename,temp,i);
202           sprintf(filename, "%s%s", temp, ".pgm");
203           *pfname = filename;
204           if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
205             {
206               error("seq_input: no such file or filename", warning);
207               return; 
208             }
209           
210           if ((im = (Imrect *)pgm_read_image(*pfname)) != NULL)
211             {
212               seq_frame_insert(im, seq);
213             }
214         }
215      
216       break;
217       
218     case SEQTL_DICOM_FILE:
219 
220      
221       for(i = offset; i <= end; i = i+stride)
222         {
223                  
224           int extra = 0;
225           char digit_len[10]; /* assuming no-one will enter a 9 digit number */
226           int scale_factor_flag = 0;
227 
228           scale_factor_flag = get_seq_scale_factor_flag();
229           extra = sprintf(digit_len, "%d", i)-1;
230           filename_length = strlen(seq->filename) + extra;
231           parse_fname(seq->filename,temp,i);
232           sprintf(filename, "%s%s", temp, "");
233           *pfname = filename;
234           if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
235             {
236               error("seq_input: no such file or filename", warning);
237               return; 
238             }
239           /*mjs 25/11/05 adjusted dicom read image to take scale factor flag*/
240           if ((im = (Imrect *)dicom_read_image(*pfname, scale_factor_flag)) != NULL)
241             {
242               seq_frame_insert(im, seq);
243             }
244         }
245       
246       break;
247     }
248   return;
249   
250 }
251 
252 /* seq load allocates a Sequence and loads the sequence structure */
253 
254 Sequence *seq_load(char *filename, int offset, int stride, int end, int filetype)
255 {
256 
257   /*NB!!!! you create a new instance of sequence every time you load one in, 
258     without ever deleting the one which is there. 
259     Maybe I should write the sequence handling stuff too...
260     or maybe not*/
261 
262   Sequence * seq = NULL;
263   int filename_length;
264 
265 
266   Tv     *seq_tv_get(void);
267    
268   if ((seq = seq_alloc()) == NULL)
269     {
270       error("can't allocate sequence \n", non_fatal);
271       return(NULL);
272     }
273 
274   seq_set_current(seq);
275 
276   if (seq->filename != NULL)
277     cvector_free(seq->filename, 0); /* this condition never reached */
278 
279   filename_length = strlen(filename);
280   
281 
282   seq->filename = cvector_copy(filename, 0, filename_length+1);
283   seq->offset = offset;
284 
285   if (stride < 1)
286     {
287       error("stride is < 1, resetting to 1\n", warning);
288       stride = 1;
289     }
290 
291   seq->stride = stride;
292   seq_read_images(seq, end, filetype);
293 
294   /* If reading the sequence hasnt worked want to get rid of sequence structure*/
295   /* shoudl really be using a return value from seq_read_images :( */
296   if (get_seq_start_el(seq) == NULL)
297     {
298       seq_rm(seq);
299       return(NULL);
300     }
301   else
302     {
303       set_seq_current_el(seq, get_seq_start_el(seq));
304       seq_get_dyndata(seq, end, filetype);
305       
306       return(seq);
307     }
308 }
309 
310 /* Function to output sequences, again calls file type specific functions */
311 
312 /* Not fully tested - the problem here (NEED TO DISCUSS) is that you're going 
313    to output with the same filename, as it is fixed in the sequence 
314    structure. Therefore if you want to output the same sequence as a 
315    different file type you're in trouble */
316 /* otherwise seems to work */
317 
318 static void  seq_output_images(Sequence *seq, char *name, int offset, int stride, List *start, int file_type)
319 {
320   List  *store = NULL;
321   int    i;
322   int    end = get_end_frame(seq);
323   char   filename[512];
324   char   temp[512];
325   int    filename_length;
326   int    flag[2];
327   float  min,max;
328   FILE  *stream;
329   struct key_info dimensions;
330 
331 
332   store = start;
333   
334   filename_length = strlen(name);
335   switch (file_type)
336     {
337     case SEQTL_AIFF_FILE:
338 
339       if (store == NULL)
340         {
341           sprintf(temp, " nothing to save in %s", name);
342           error(temp, warning);
343           return;
344         }
345 
346       for(i = offset; i <= end; i = i+stride)
347         {
348           parse_fname(name,temp,i);
349           sprintf(filename, "%s%s", temp, ".aiff");
350 
351           if ((store == NULL) || (store->to == NULL))
352             {
353               sprintf(temp, " no data for %s, aborting", filename);
354               error(temp, warning);
355               return;
356             }
357                 
358           aiff_write_image(store->to, filename);
359           store = get_stride_store(store, stride);
360         }
361       break;
362       
363     case SEQTL_RAD_FILE:
364 
365       if (store == NULL)
366         {
367           sprintf(temp, " nothing to save in %s", name);
368           error(temp, warning);
369           return;
370         }
371 
372 
373       for(i = offset; i <= end; i = i+stride)
374         {
375           parse_fname(name,temp,i);
376           sprintf(filename, "%s%s", temp, ".rad");
377 
378           if ((store == NULL) || (store->to == NULL))
379             {
380               sprintf(temp, " no data for %s, aborting", filename);
381               error(temp, warning);
382               return;
383             }
384 
385           rad_write_image(store->to, filename);
386           store = get_stride_store(store, stride);
387         }
388       break;
389       
390     case SEQTL_ANLZ_FILE:
391 
392       sprintf(filename, "%s%s", name, ".img");
393       sprintf(temp, "Tina generated processed file, some data fields may be missing");
394 
395       if (store == NULL)
396         {
397           sprintf(temp, " nothing to save in %s", filename);
398           error(temp, warning);
399           return;
400         }
401 
402       stream = ani_write_openfile(filename, &dimensions, store->to, seq);
403 
404       flag[0] = INT_MIN;
405       flag[1] = INT_MAX;
406       for(i = offset; i <= end; i = i+stride)
407         {
408           if ((store == NULL) || (store->to == NULL))
409             {
410               sprintf(temp, " no data for %s, aborting", filename);
411               error(temp, warning);
412               break;
413             }
414 
415           imf_minmax(store->to, &min, &max);
416           if (min<flag[0]) flag[0] = min;
417           if (min>flag[1]) flag[1] = max;
418 
419           ani_write_imagedata(store->to, stream, filename);
420           store = get_stride_store(store, stride);
421         }
422       
423       
424       ani_write_closefile(filename, stream);
425       dimensions.z_dim = end-offset;
426       write_ani_header(name, &dimensions, temp, flag);
427 
428       break;
429       
430 /*
431  *   Commented out to improve lib / tool separation - GAB 19 Feb 2003
432  *   Replacing with new version to bring get_stride_store call to this level.
433  *
434     case SEQTL_ANLZ_FILE:
435       ani_write_image(seq, name, offset, stride, start); 
436       break;
437  *
438  *  End of commenting out of SEQTL_ANLZ_FILE code - GAB 19 Feb 2003
439  */
440 
441     case SEQTL_PGM_FILE:
442 
443       if (store == NULL)
444         {
445           sprintf(temp, " nothing to save in %s", name);
446           error(temp, warning);
447           return;
448         }
449 
450       for(i = offset; i <= end; i = i+stride)
451         {
452           parse_fname(name,temp,i);
453           sprintf(filename, "%s%s", temp, ".pgm");
454 
455           if ((store == NULL) || (store->to == NULL))
456             {
457               sprintf(temp, " no data for %s, aborting", filename);
458               error(temp, warning);
459               return;
460             }
461           pgm_write_image(store->to, filename);
462           store = get_stride_store(store, stride);
463         }
464         break;
465     }
466 
467   return;
468 }       
469 
470 
471 /* seq save is a wrapper around the output function, which determines which 
472    parameters should be used to output the sequence
473    If the values in the Sequence structure are to be used, then all
474    except seq and file_type should be set to NULL
475    The option is there however to use other values */
476 
477 void seq_save(Sequence *seq, char *name, int *offset_ptr, int *stride_ptr, List *start, List *end, int file_type)
478 {
479   int offset, stride;
480   int filename_length;
481 
482   if (seq == NULL)
483     {
484       error(" no sequence to save \n", non_fatal);
485       return;
486     }
487   /* already checked for seq being null, but leave here, as may be called outside button */
488   
489   if (name == NULL)
490     {
491       filename_length = strlen(seq->filename);
492       name = cvector_alloc(0, filename_length+1);
493       if ((name = strcpy(name, seq->filename)) == NULL)
494         {
495           error("filename is NULL \n", non_fatal);
496           return;
497         }
498     }  
499   if (start == NULL)
500     {
501       if ((start = seq->start) == NULL)
502         {
503           error("start is NULL \n", non_fatal);
504           return;
505         }
506     }
507   if (end == NULL)
508     {
509       if ((end = seq->end) == NULL)
510         {
511           error("end is NULL \n", non_fatal);
512           return;
513         }
514     }
515   if (offset_ptr == NULL)
516     {
517       offset = seq->offset;
518     }
519   else
520     offset = *offset_ptr;
521   if (stride_ptr == NULL)
522     {
523       stride = seq->stride;
524       if (stride < 1) /*should never happen... */
525         {
526           error("stride < 1, resetting to 1\n", warning);
527           stride = 1;
528         }
529     }
530   else
531     stride = *stride_ptr;
532   seq_output_images(seq, name, offset, stride, start, file_type);
533 }
534 
535 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.