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

Linux Cross Reference
Tina6/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 <math.h>
 56 #include <tina/file/fileDef.h>
 57 #include <tina/file/filePro.h>
 58 #include <tina/image/imgDef.h>
 59 #include <tina/image/imgPro.h>
 60 #include <tinatool/tlbase/tlbase_SeqDef.h>
 61 #include <tinatool/tlbase/tlbase_SeqPro.h>
 62 
 63 /*static char   *filename = NULL; should be in appropriate functions? */
 64 /* function to take sequence and file type and works out correct sequence 
 65    filenames, and calls the particular file_type i/o routines */
 66 /* no longer returns iscale structure as these are assigned in the lower
 67    level input functions. perhaps wrongly */
 68 /* NB all the set_frame_no stuff is probably totally pointless */
 69 
 70 /* I have updated this function to allow averaging across slices when the sequence is loaded
 71 with stride ne 1.  I have tested this for image loading with AIFF, ANLZ, RAD, PGM and DICOM
 72 (NEMA appears to be obsolete anyway).  However, this functionality could have several 
 73 undesirable effects:
 74 
 75 - it necessarily results in a 0.5 voxel offset to the coordinate system 
 76 - I have not attempted to modify the parameters extracted from the DICOM header and 
 77   entered into the props list; they will be the parameters from the first image 
 78   that contributes to each averaging process.
 79 - Odd things might happen with quantised images (e.g. PGM, which stores each pixel value as 
 80   an int), in terms of the statistics of the grey-level histogram.
 81 
 82 I have no way to test much of the old functionality (e.g. the flow tool) against these 
 83 changes, and so the averaging across slices must be switched on by the user via a choice
 84 list in the top-level sequence tool, at the user's risk.  PAB 8/12/2011 */
 85 
 86 static void seq_read_images(Sequence *seq, int end, int file_type, int stride_av_flag)
 87 {
 88     Imrect *im=NULL, *im1=NULL, *im2=NULL, *im3=NULL;
 89     int     i, j, k, extra = 0, scale_factor_flag = 0, offset, stride, downsample, filename_length;
 90     char   *pfname[1], filename[512], temp[512];
 91     char    digit_len[10]; /* assuming no-one will enter a 9 digit number */
 92     void  **im_vector=NULL;
 93     double stride_fac;
 94 
 95     /* These are in the wrong place, it is entirely possible to change the filename 
 96        length, particularly in DICOM if you have more than single digit figures. */
 97     /* filename_length = strlen(seq->filename);
 98        temp = cvector_alloc(0, filename_length+1); */
 99 
100     offset = seq->offset;
101     stride = seq->stride;
102     downsample = seq->downsample;
103     stride_fac = 1.0/((double) stride);
104 
105     switch (file_type)
106     {
107         case SEQTL_AIFF_FILE:
108         if(stride>1 && stride_av_flag==1)
109         {
110             im_vector = pvector_alloc(0, stride);
111 
112             for(i = offset; i <= end; i=i+stride)
113             {
114                 for(j=0; j<stride; j++)
115                 {
116                     extra = sprintf(digit_len, "%d", (i+j))-1;
117                     /* NB the hash used in the filename counts as one digit */
118                     filename_length = strlen(seq->filename) + extra;
119 
120                     parse_fname(seq->filename,temp,(i+j));
121                     sprintf(filename, "%s%s", temp, ".aiff");
122                     *pfname = filename;
123 
124                     if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
125                     {
126                         error("seq_input: no such file or filename", warning);
127                         return; 
128                     }
129                     if ((im_vector[j] = (void *)aiff_read_image(*pfname)) == NULL)
130                     {
131                         error("seq_input: no such file or filename", warning);
132                         for(k=0; k<stride; k++)
133                         {
134                             im = (Imrect *)im_vector[k];
135                             if(im!=NULL) im_free(im);
136                         }
137                         pvector_free(im_vector, 0);
138 
139                         return;
140                     }
141                 }
142 
143                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
144 
145                 for(j=1; j<stride; j++)
146                 {
147                     im1 = (Imrect *)im_vector[0];
148                     im2 = (Imrect *)im_vector[j];
149                     im3 = im_sum(im1, im2);
150                     im_free(im1);
151                     im_free(im2);
152                     im_vector[0] = im3;
153                 }
154                 im1 = (Imrect *)im_vector[0];
155                 im = im_times(stride_fac, im1);
156                 im_free(im1);
157 
158                 if(downsample!=1)
159                 {
160                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
161                     im_free(im);
162                 }
163                 else
164                 {
165                     seq_frame_insert(im, seq);
166                 }
167             }
168 
169             pvector_free(im_vector, 0);
170         }
171         else
172         {
173             for(i = offset; i <= end; i = i+stride)
174             {
175                 extra = sprintf(digit_len, "%d", i)-1;
176                 /* NB the hash used in the filename counts as one digit */
177                 filename_length = strlen(seq->filename) + extra;
178 
179                 parse_fname(seq->filename,temp,i);
180                 sprintf(filename, "%s%s", temp, ".aiff");
181                 *pfname = filename;
182 
183                 if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
184                 {
185                     error("seq_input: no such file or filename", warning);
186                     return;
187                 }
188 
189                 if ((im = (Imrect *)aiff_read_image(*pfname)) != NULL)
190                 {
191                     if(downsample!=1)
192                     {
193                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
194                         im_free(im);
195                     }
196                     else
197                     {
198                         seq_frame_insert(im, seq);
199                     }
200                 }
201             }
202         }
203         break;
204 
205         case SEQTL_ANLZ_FILE:
206         if(stride>1 && stride_av_flag==1)
207         {
208             im_vector = pvector_alloc(0, stride);
209 
210             for(i = offset; i <= end; i=i+stride)
211             {
212                 for(j=0; j<stride; j++)
213                 {
214                     if ((im_vector[j] = (void *)ani_read_image(seq->filename,(i+j))) == NULL)
215                     {
216                         error("seq_input: no such file or filename", warning);
217                         for(k=0; k<stride; k++)
218                         {
219                             im = (Imrect *)im_vector[k];
220                             if(im!=NULL) im_free(im);
221                         }
222                         pvector_free(im_vector, 0);
223 
224                         return;
225                     }
226                 }
227 
228                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
229 
230                 for(j=1; j<stride; j++)
231                 {
232                     im1 = (Imrect *)im_vector[0];
233                     im2 = (Imrect *)im_vector[j];
234                     im3 = im_sum(im1, im2);
235                     im_free(im1);
236                     im_free(im2);
237                     im_vector[0] = im3;
238                 }
239                 im1 = (Imrect *)im_vector[0];
240                 im = im_times(stride_fac, im1);
241                 im_free(im1);
242 
243                 if(downsample!=1)
244                 {
245                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
246                     im_free(im);
247                 }
248                 else
249                 {
250                     seq_frame_insert(im, seq);
251                 }
252             }
253 
254             pvector_free(im_vector, 0);
255         }
256         else
257         {
258             for(i = offset; i <= end; i = i+stride)
259             {
260                 if ((im = (Imrect *)ani_read_image(seq->filename,i)) != NULL)
261                 {
262                     if(downsample!=1)
263                     {
264                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
265                         im_free(im);
266                     }
267                     else
268                     {
269                         seq_frame_insert(im, seq);
270                     }
271                 }
272             }
273         }
274         break;
275 
276         case SEQTL_RAD_FILE:
277         if(stride>1 && stride_av_flag==1)
278         {
279             im_vector = pvector_alloc(0, stride);
280 
281             for(i = offset; i <= end; i=i+stride)
282             {
283                 for(j=0; j<stride; j++)
284                 {
285                     extra = sprintf(digit_len, "%d", (i+j))-1;
286                     filename_length = strlen(seq->filename) + extra;
287 
288                     parse_fname(seq->filename,temp,(i+j));
289                     sprintf(filename, "%s%s", temp, ".rad");
290                     *pfname = filename;
291 
292                     if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
293                     {
294                         error("seq_input: no such file or filename", warning);
295                         return; 
296                     }
297                     if ((im_vector[j] = (void *)rad_read_image(*pfname)) == NULL)
298                     {
299                         error("seq_input: no such file or filename", warning);
300                         for(k=0; k<stride; k++)
301                         {
302                             im = (Imrect *)im_vector[k];
303                             if(im!=NULL) im_free(im);
304                         }
305                         pvector_free(im_vector, 0);
306 
307                         return;
308                     }
309                 }
310 
311                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
312 
313                 for(j=1; j<stride; j++)
314                 {
315                     im1 = (Imrect *)im_vector[0];
316                     im2 = (Imrect *)im_vector[j];
317                     im3 = im_sum(im1, im2);
318                     im_free(im1);
319                     im_free(im2);
320                     im_vector[0] = im3;
321                 }
322                 im1 = (Imrect *)im_vector[0];
323                 im = im_times(stride_fac, im1);
324                 im_free(im1);
325 
326                 if(downsample!=1)
327                 {
328                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
329                     im_free(im);
330                 }
331                 else
332                 {
333                     seq_frame_insert(im, seq);
334                 }
335             }
336 
337             pvector_free(im_vector, 0);
338         }
339         else
340         {
341             for(i = offset; i <= end; i = i+stride)
342             {
343                 extra = sprintf(digit_len, "%d", i)-1;
344                 filename_length = strlen(seq->filename) + extra;
345 
346                 parse_fname(seq->filename,temp,i);
347                 sprintf(filename, "%s%s", temp, ".rad");
348                 *pfname = filename;
349 
350                 if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
351                 {
352                     error("seq_input: no such file or filename", warning);
353                     return;
354                 }
355 
356                 if ((im = (Imrect *)rad_read_image(*pfname)) != NULL)
357                 {
358                     if(downsample!=1)
359                     {
360                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
361                         im_free(im);
362                     }
363                     else
364                     {
365                         seq_frame_insert(im, seq);
366                     }
367                 }
368             }
369         }
370         break;
371 
372         case SEQTL_NEMA_FILE:
373         if(stride>1 && stride_av_flag==1)
374         {
375             im_vector = pvector_alloc(0, stride);
376 
377             for(i = offset; i <= end; i=i+stride)
378             {
379                 for(j=0; j<stride; j++)
380                 {
381                     extra = sprintf(digit_len, "%d", (i+j))-1;
382                     filename_length = strlen(seq->filename) + extra;
383 
384                     parse_fname(seq->filename,temp,(i+j));
385                     sprintf(filename, "%s%s", temp, ".ani");
386                     *pfname = filename;
387 
388                     if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
389                     {
390                         error("seq_input: no such file or filename", warning);
391                         return; 
392                     }
393                     if ((im_vector[j] = (void *)nema_read_image(*pfname)) == NULL)
394                     {
395                         error("seq_input: no such file or filename", warning);
396                         for(k=0; k<stride; k++)
397                         {
398                             im = (Imrect *)im_vector[k];
399                             if(im!=NULL) im_free(im);
400                         }
401                         pvector_free(im_vector, 0);
402 
403                         return;
404                     }
405                 }
406 
407                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
408 
409                 for(j=1; j<stride; j++)
410                 {
411                     im1 = (Imrect *)im_vector[0];
412                     im2 = (Imrect *)im_vector[j];
413                     im3 = im_sum(im1, im2);
414                     im_free(im1);
415                     im_free(im2);
416                     im_vector[0] = im3;
417                 }
418                 im1 = (Imrect *)im_vector[0];
419                 im = im_times(stride_fac, im1);
420                 im_free(im1);
421 
422                 if(downsample!=1)
423                 {
424                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
425                     im_free(im);
426                 }
427                 else
428                 {
429                     seq_frame_insert(im, seq);
430                 }
431             }
432 
433             pvector_free(im_vector, 0);
434         }
435         else
436         {
437             for(i = offset; i <= end; i = i+stride)
438             {
439                 extra = sprintf(digit_len, "%d", i)-1;
440                 filename_length = strlen(seq->filename) + extra;
441 
442                 parse_fname(seq->filename,temp,i);
443                 sprintf(filename, "%s%s", temp, ".ani");
444                 *pfname = filename;
445 
446                 if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
447                 {
448                     error("seq_input: no such file or filename", warning);
449                     return; 
450                 }
451 
452                 if ((im = (Imrect *)nema_read_image(*pfname)) != NULL)
453                 {
454                     if(downsample!=1)
455                     {
456                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
457                         im_free(im);
458                     }
459                     else
460                     {
461                         seq_frame_insert(im, seq);
462                     }
463                 }
464             }
465         }
466         break;
467 
468         case SEQTL_PGM_FILE:
469         if(stride>1 && stride_av_flag==1)
470         {
471             im_vector = pvector_alloc(0, stride);
472 
473             for(i = offset; i <= end; i=i+stride)
474             {
475                 for(j=0; j<stride; j++)
476                 {
477                     extra = sprintf(digit_len, "%d", (i+j))-1;
478                     filename_length = strlen(seq->filename) + extra;
479 
480                     parse_fname(seq->filename,temp,(i+j));
481                     sprintf(filename, "%s%s", temp, ".pgm");
482                     *pfname = filename;
483 
484                     if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
485                     {
486                         error("seq_input: no such file or filename", warning);
487                         return; 
488                     }
489                     if ((im_vector[j] = (void *)pgm_read_image(*pfname)) == NULL)
490                     {
491                         error("seq_input: no such file or filename", warning);
492                         for(k=0; k<stride; k++)
493                         {
494                             im = (Imrect *)im_vector[k];
495                             if(im!=NULL) im_free(im);
496                         }
497                         pvector_free(im_vector, 0);
498 
499                         return;
500                     }
501                 }
502 
503                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
504 
505                 for(j=1; j<stride; j++)
506                 {
507                     im1 = (Imrect *)im_vector[0];
508                     im2 = (Imrect *)im_vector[j];
509                     im3 = im_sum(im1, im2);
510                     im_free(im1);
511                     im_free(im2);
512                     im_vector[0] = im3;
513                 }
514                 im1 = (Imrect *)im_vector[0];
515                 im = im_times(stride_fac, im1);
516                 im_free(im1);
517 
518                 if(downsample!=1)
519                 {
520                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
521                     im_free(im);
522                 }
523                 else
524                 {
525                     seq_frame_insert(im, seq);
526                 }
527             }
528 
529             pvector_free(im_vector, 0);
530         }
531         else
532         {
533             for(i = offset; i <= end; i = i+stride)
534             {
535                 extra = sprintf(digit_len, "%d", i)-1;
536                 filename_length = strlen(seq->filename) + extra;
537 
538                 parse_fname(seq->filename,temp,i);
539                 sprintf(filename, "%s%s", temp, ".pgm");
540                 *pfname = filename;
541 
542                 if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
543                 {
544                     error("seq_input: no such file or filename", warning);
545                     return;
546                 }
547 
548                 if ((im = (Imrect *)pgm_read_image(*pfname)) != NULL)
549                 {
550                     if(downsample!=1)
551                     {
552                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
553                         im_free(im);
554                     }
555                     else
556                     {
557                         seq_frame_insert(im, seq);
558                     }
559                 }
560             }
561         }
562         break;
563 
564         case SEQTL_DICOM_FILE:
565         if(stride>1 && stride_av_flag==1)
566         {
567             im_vector = pvector_alloc(0, stride);
568 
569             for(i = offset; i <= end; i=i+stride)
570             {
571                 for(j=0; j<stride; j++)
572                 {
573                     scale_factor_flag = get_seq_scale_factor_flag();
574                     extra = sprintf(digit_len, "%d", (i+j))-1;
575                     filename_length = strlen(seq->filename) + extra;
576                     parse_fname(seq->filename,temp,(i+j));
577                     sprintf(filename, "%s%s", temp, "");
578                     *pfname = filename;
579 
580                     if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
581                     {
582                         error("seq_input: no such file or filename", warning);
583                         return; 
584                     }
585                     if ((im_vector[j] = (void *)dicom_read_image(*pfname, scale_factor_flag)) == NULL)
586                     {
587                         error("seq_input: no such file or filename", warning);
588                         for(k=0; k<stride; k++)
589                         {
590                             im = (Imrect *)im_vector[k];
591                             if(im!=NULL) im_free(im);
592                         }
593                         pvector_free(im_vector, 0);
594 
595                         return;
596                     }
597                 }
598 
599                 /* In order to reach this point, the im_vector must be full and there must be at least two images*/
600 
601                 for(j=1; j<stride; j++)
602                 {
603                     im1 = (Imrect *)im_vector[0];
604                     im2 = (Imrect *)im_vector[j];
605                     im3 = im_sum(im1, im2);
606                     im_free(im1);
607                     im_free(im2);
608                     im_vector[0] = im3;
609                 }
610                 im1 = (Imrect *)im_vector[0];
611                 im = im_times(stride_fac, im1);
612                 im_free(im1);
613 
614                 if(downsample!=1)
615                 {
616                     seq_frame_insert(imf_downsample_average(downsample, im), seq);
617                     im_free(im);
618                 }
619                 else
620                 {
621                     seq_frame_insert(im, seq);
622                 }
623             }
624 
625             pvector_free(im_vector, 0);
626         }
627         else
628         {
629             for(i = offset; i <= end; i = i+stride)
630             {
631                 scale_factor_flag = get_seq_scale_factor_flag();
632                 extra = sprintf(digit_len, "%d", i)-1;
633                 filename_length = strlen(seq->filename) + extra;
634                 parse_fname(seq->filename,temp,i);
635                 sprintf(filename, "%s%s", temp, "");
636                 *pfname = filename;
637 
638                 if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
639                 {
640                     error("seq_input: no such file or filename", warning);
641                     return; 
642                 }
643                 /*mjs 25/11/05 adjusted dicom read image to take scale factor flag*/
644 
645                 if ((im = (Imrect *)dicom_read_image(*pfname, scale_factor_flag)) != NULL)
646                 {
647                     if(downsample!=1)
648                     {
649                         seq_frame_insert(imf_downsample_average(downsample, im), seq);
650                         im_free(im);
651                     }
652                     else
653                     {
654                         seq_frame_insert(im, seq);
655                     }
656                 }
657             }
658         }
659 
660         break;
661     }
662 }
663 
664 
665 /* seq load allocates a Sequence and loads the sequence structure */
666 
667 Sequence *seq_load(char *filename, int offset, int stride, int downsample, int end, int filetype, int stride_av_flag)
668 {
669     /* NB!!!! you create a new instance of sequence every time you load one in, without ever    */
670     /* deleting the one which is there. Maybe I should write the sequence handling stuff too... */
671     /* or maybe not.                                                                            */
672     /*                                                                                          */
673     /* PAB 15/3/2011: this is just an invitation for a massive memory leak, so I have added the */
674     /* code to delete the current sequence (the existing seq_load_proc makes sure that the Tv   */
675     /* in the sequence tool is kept updated).  If we ever write the code to handle multiple     */
676     /* sequences, just remove the seq_rm line.                                                  */
677 
678     Sequence * seq = NULL;
679     int filename_length;
680     Tv     *seq_tv_get(void);
681     
682     seq = seq_get_current();
683     if(seq!=NULL) seq_rm(seq);
684 
685     if ((seq = seq_alloc()) == NULL)
686     {
687         error("can't allocate sequence \n", non_fatal);
688         return(NULL);
689     }
690 
691     seq_set_current(seq);
692 
693     if (seq->filename != NULL) cvector_free(seq->filename, 0); /* this condition never reached */
694 
695     filename_length = strlen(filename);
696 
697     seq->filename = cvector_copy(filename, 0, filename_length+1);
698     seq->offset = offset;
699 
700     if (stride < 1)
701     {
702         error("stride is < 1, resetting to 1\n", warning);
703         stride = 1;
704     }
705 
706     if (downsample < 1)
707     {
708         error("downsample is < 1, resetting to 1\n", warning);
709         downsample = 1;
710     }
711 
712     seq->stride = stride;
713     seq->downsample = downsample;
714     seq_read_images(seq, end, filetype, stride_av_flag);
715 
716     /* If reading the sequence hasn't worked want to get rid of sequence structure */
717     /* should really be using a return value from seq_read_images :( */
718     if (get_seq_start_el(seq) == NULL)
719     {
720         seq_rm(seq);
721         return(NULL);
722     }
723     else
724     {
725         set_seq_current_el(seq, get_seq_start_el(seq));
726         seq_get_dyndata(seq, end, filetype);
727 
728         /* Now sort out the sequence dimensions */
729 
730         return(seq);
731     }
732 }
733 
734 
735 /* Function to output sequences, again calls file type specific functions */
736 
737 /* Not fully tested - the problem here (NEED TO DISCUSS) is that you're going 
738    to output with the same filename, as it is fixed in the sequence 
739    structure. Therefore if you want to output the same sequence as a 
740    different file type you're in trouble */
741 /* otherwise seems to work */
742 
743 static void  seq_output_images(Sequence *seq, char *name, int offset, int stride, List *start, int file_type)
744 {
745   List   *store = NULL;
746   int    i;
747   int    end = get_end_frame(seq);
748   char   filename[512];
749   char   temp[512];
750   int    filename_length;
751   int    flag[2];
752   float  min,max;
753   FILE   *stream;
754   struct key_info dimensions;
755 
756   store = start;
757 
758   filename_length = strlen(name);
759 
760   switch (file_type)
761   {
762     case SEQTL_AIFF_FILE:
763 
764       if (store == NULL)
765       {
766         sprintf(temp, " nothing to save in %s", name);
767         error(temp, warning);
768         return;
769       }
770 
771       for(i = offset; i <= end; i = i+stride)
772       {
773         parse_fname(name,temp,i);
774         sprintf(filename, "%s%s", temp, ".aiff");
775 
776         if ((store == NULL) || (store->to == NULL))
777         {
778           sprintf(temp, " no data for %s, aborting", filename);
779           error(temp, warning);
780           return;
781         }
782 
783         aiff_write_image(store->to, filename);
784         store = get_stride_store(store, stride);
785       }
786 
787     break;
788 
789     case SEQTL_RAD_FILE:
790 
791       if (store == NULL)
792       {
793         sprintf(temp, " nothing to save in %s", name);
794         error(temp, warning);
795         return;
796       }
797 
798       for(i = offset; i <= end; i = i+stride)
799       {
800         parse_fname(name,temp,i);
801         sprintf(filename, "%s%s", temp, ".rad");
802 
803         if ((store == NULL) || (store->to == NULL))
804         {
805           sprintf(temp, " no data for %s, aborting", filename);
806           error(temp, warning);
807           return;
808         }
809 
810         rad_write_image(store->to, filename);
811         store = get_stride_store(store, stride);
812       }
813 
814     break;
815 
816     case SEQTL_ANLZ_FILE:
817 
818       sprintf(filename, "%s%s", name, ".img");
819       sprintf(temp, "Tina generated processed file, some data fields may be missing");
820 
821       if (store == NULL)
822       {
823         sprintf(temp, " nothing to save in %s", filename);
824         error(temp, warning);
825         return;
826       }
827 
828       stream = ani_write_openfile(filename, &dimensions, store->to, seq);
829 
830       flag[0] = INT_MIN;
831       flag[1] = INT_MAX;
832       for(i = offset; i <= end; i = i+stride)
833       {
834         if ((store == NULL) || (store->to == NULL))
835         {
836           sprintf(temp, " no data for %s, aborting", filename);
837           error(temp, warning);
838           break;
839         }
840 
841         imf_minmax(store->to, &min, &max);
842         if (min<flag[0]) flag[0] = min;
843         if (min>flag[1]) flag[1] = max;
844 
845         ani_write_imagedata(store->to, stream, filename);
846         store = get_stride_store(store, stride);
847       }
848 
849       ani_write_closefile(filename, stream);
850       dimensions.z_dim = end-offset;
851       write_ani_header(name, &dimensions, temp, flag);
852 
853     break;
854 
855 /*
856  *   Commented out to improve lib / tool separation - GAB 19 Feb 2003
857  *   Replacing with new version to bring get_stride_store call to this level.
858  *
859     case SEQTL_ANLZ_FILE:
860       ani_write_image(seq, name, offset, stride, start); 
861       break;
862  *
863  *  End of commenting out of SEQTL_ANLZ_FILE code - GAB 19 Feb 2003
864  */
865 
866     case SEQTL_PGM_FILE:
867 
868       if (store == NULL)
869       {
870         sprintf(temp, " nothing to save in %s", name);
871         error(temp, warning);
872         return;
873       }
874 
875       for(i = offset; i <= end; i = i+stride)
876       {
877         parse_fname(name,temp,i);
878         sprintf(filename, "%s%s", temp, ".pgm");
879 
880         if ((store == NULL) || (store->to == NULL))
881         {
882           sprintf(temp, " no data for %s, aborting", filename);
883           error(temp, warning);
884           return;
885         }
886         pgm_write_image(store->to, filename);
887         store = get_stride_store(store, stride);
888       }
889 
890     break;
891 
892     case SEQTL_DICOM_FILE:
893     case SEQTL_NEMA_FILE:
894     default:
895       error("Cannot output NEMA or DICOM file types.\n", warning);
896 
897     break;
898   }
899 
900   return;
901 }       
902 
903 
904 /* seq save is a wrapper around the output function, which determines which 
905    parameters should be used to output the sequence
906    If the values in the Sequence structure are to be used, then all
907    except seq and file_type should be set to NULL
908    The option is there however to use other values */
909 
910 void seq_save(Sequence *seq, char *name, int *offset_ptr, int *stride_ptr, List *start, List *end, int file_type)
911 {
912   int offset, stride;
913   int filename_length;
914 
915   if (seq == NULL)
916     {
917       error(" no sequence to save \n", non_fatal);
918       return;
919     }
920   /* already checked for seq being null, but leave here, as may be called outside button */
921   
922   if (name == NULL)
923     {
924       filename_length = strlen(seq->filename);
925       name = cvector_alloc(0, filename_length+1);
926       if ((name = strcpy(name, seq->filename)) == NULL)
927         {
928           error("filename is NULL \n", non_fatal);
929           return;
930         }
931     }  
932   if (start == NULL)
933     {
934       if ((start = seq->start) == NULL)
935         {
936           error("start is NULL \n", non_fatal);
937           return;
938         }
939     }
940   if (end == NULL)
941     {
942       if ((end = seq->end) == NULL)
943         {
944           error("end is NULL \n", non_fatal);
945           return;
946         }
947     }
948   if (offset_ptr == NULL)
949     {
950       offset = seq->offset;
951     }
952   else
953     offset = *offset_ptr;
954   if (stride_ptr == NULL)
955     {
956       stride = seq->stride;
957       if (stride < 1) /*should never happen... */
958         {
959           error("stride < 1, resetting to 1\n", warning);
960           stride = 1;
961         }
962     }
963   else
964     stride = *stride_ptr;
965   seq_output_images(seq, name, offset, stride, start, file_type);
966 }
967 

~ [ 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.