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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileNema_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-libs/tina/file/fileNema_io.c,v $
 37  * Date    :  $Date: 2005/12/20 16:28:01 $
 38  * Version :  $Revision: 1.6 $
 39  * CVS Id  :  $Id: fileNema_io.c,v 1.6 2005/12/20 16:28:01 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  *
 45  *  Functions to read/write ACR-NEMA image files
 46  *
 47  *********
 48 */
 49 
 50 #include "fileNema_io.h"
 51 
 52 #if HAVE_CONFIG_H
 53   #include <config.h>
 54 #endif
 55 
 56 #include <stdio.h>
 57 #include <string.h>
 58 #include <math.h>
 59 #include <tina/file/filePro.h>
 60 #include <tina/sys/sysPro.h>
 61 #include <tina/sys/sysDef.h>
 62 #include <tina/image/imgPro.h>
 63 #include <tina/image/imgDef.h>
 64 #include <tina/math/mathPro.h>
 65 #include <tina/math/mathDef.h>
 66 #include <tina/image/imgSeq_logic.h>
 67 
 68 #define VOXELS    450
 69 #define DYNSTIME  451
 70 #define TE_DATA   453
 71 #define PAT_DATA  454
 72 #define TR_DATA   455
 73 #define FLIP_ANGLE_DATA   456
 74 #define S_MIN_FP_DATA     457
 75 #define S_MAX_FP   458
 76 #define N_BITS_STORED  459
 77 
 78 
 79 
 80 void  dynstimes_free(float *times)
 81 {
 82   fvector_free(times, 0);
 83 }
 84 
 85 
 86 /* Create an imrect and read an image file called 'pathname' into it.
 87  * Open file, read data and close file. On failure, give error message
 88  * and return NULL. */
 89 
 90 static int valid_group(short group)
 91 {
 92         if(group == 0x0000 || group == 0x0008 || group == 0x0010
 93         || group == 0x0020 || group == 0x0018 || group == 0x0020
 94         || group == 0x0028 || group == 0x4000 || group == 0x4000
 95         || group == 0x7FE0 )
 96            return(1);
 97         else
 98            return(0);
 99 } 
100 
101 Imrect *im_nema_conv(Imrect *im)
102 {
103     Imrect *im2;
104     Imregion roi;
105     unsigned char *row1;
106     unsigned char *row2;
107     int i,j,k;
108     int lx, ux, ly, uy;
109 
110     if (im == NULL)
111       return(NULL);
112 
113     roi = *(im->region);
114     roi.ux = roi.ux*4.0/3;
115     im2 = im_alloc(im->height,roi.ux,&roi,short_v);
116     lx = roi.lx;
117     ux = roi.ux;
118     ly = roi.ly;
119     uy = roi.uy;
120 
121     for (i = ly; i < uy; ++i)
122     {
123         row1 = IM_ROW(im,i);
124         row2 = IM_ROW(im2,i);
125         for (k = 2*lx, j = 2*lx; k < 2*ux; j+=3)
126         {
127             row2[k++] = row1[j];
128             row2[k++] = row1[j+1]%16;
129             row2[k++] = row1[j+1]/16 + (row1[j+2]%16)*16;
130             row2[k++] = row1[j+2]/16;
131         }
132     }
133 
134     return(im2);
135 }
136 
137 void im_endian_inplace(Imrect *imrect)
138 {
139     unsigned int size = var_size(imrect->vtype);
140     Imregion roi;
141     int y;
142     int lx, ux, ly, uy;
143 
144     roi = *(imrect->region);
145     lx = roi.lx;
146     ux = roi.ux;
147     ly = roi.ly;
148     uy = roi.uy;
149 
150 
151     for(y = ly; y < uy; y++)
152     {
153         void*  row = NULL;
154         char   temp[40];
155         int    x;
156         unsigned int i;
157 
158         row = IM_ROW(imrect,y);
159 
160         for(x = lx; x < ux; x++)
161         {
162             for(i = 0; i < size; i++)
163                 temp[i] = *(((char *)row) + (x*size+i));
164             for(i = 1; i <= size; i++)
165                 *(((char *)row) + (x*size + size-i)) = temp[i-1];
166         }
167     }
168 }
169 
170 /* mjs 11/11/05 modified to insert TE as an array on sequence proplist */
171 Bool   nema_hdr_TE_extract()
172 {
173   Bool                swapit = false;
174   Bool                success = false;
175   float              *TE_arr=NULL, te=0.0;
176   int                 i, k;
177   char                temp[64];
178   unsigned char       junk;
179   unsigned short      group, element;
180   unsigned short      dblock;
181   unsigned short      dblock1;
182   
183   FILE    *fp = NULL;
184   char    *pfname[1];
185   char     filename[512];
186   char     filename_temp[512];
187   Sequence *seq = (Sequence *)seq_get_current();
188   int end = get_end_frame(seq); 
189 
190   strcpy(filename, "");
191   strcpy(filename_temp, "");
192 
193   TE_arr = fvector_alloc(seq->offset, end+1);
194 
195   for (i = seq->offset; i <= end; i = i+seq->stride)
196     {
197       success=false;
198       parse_fname(seq->filename, filename_temp, i);
199       sprintf(filename, "%s%s", filename_temp, ".ani");
200       *pfname = filename;
201       if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
202         {
203           error("nema_hdrinfo_extract: no such file or filename", warning);
204           return(false);
205         }
206   
207       if ((fp = fopen_2(*pfname, "rb")) == NULL)
208         {
209           error("file pointer equals NULL", warning);
210           return(false);
211         }
212       
213       set_swapping_ts(0);
214       /* TE = (float *)ralloc(sizeof(float));*/
215       while (fp && fread(&group, sizeof(short), 1, fp) && !success)
216         { 
217           if(!swapit)
218             {
219               if(!valid_group(group))
220                 {
221                   set_swapping_ts(1);
222                   short_swap((char *)&group);
223                   if (valid_group(group)) 
224                     swapit = true;
225                   else    
226                     {
227                       short_swap((char *)&group);
228                       set_swapping_ts(0);
229                     }
230                 }
231             }
232           else
233             short_swap((char *)&group);
234           
235           fread(&element, sizeof(short), 1, fp);
236           short_swap((char *)&element);
237           fread(&dblock,  sizeof(short), 1, fp);
238           short_swap((char *)&dblock);
239           fread(&dblock1, sizeof(short), 1, fp);
240           short_swap((char *)&dblock1);
241           
242           if (dblock1 != 0)
243             {
244               format("error: non-standard block size\n");
245               return(false);
246             }
247           else if (group == 0x0018 && element == 0x0081)
248             {
249               fread(temp, sizeof(char), dblock, fp);
250               temp[dblock] = '\0';
251               sscanf(temp, "%f", &te);
252               te = te/100.0;
253               format("TE: %f", te);
254               TE_arr[i]=te;
255               success = true;
256             }
257           else 
258             for (k = 0; k < dblock; k++) 
259               fread(&junk, sizeof(char), 1, fp);
260           
261           if (success) break;
262         }
263       
264       
265       fclose_2(fp, filename);
266     }
267   if (success)
268     {
269       seq->props = proplist_rm(seq->props, TE_DATA);
270       seq->props = proplist_addifnp(seq->props, (void *) TE_arr, TE_DATA, rfree, false);
271     }
272   
273   return (success);
274   
275 }
276 
277 /* added GJMP 6/3/2001 */
278 /* mjs 11/11/05 modified to insert TR as an array on sequence proplist */
279 Bool   nema_hdr_TR_extract()
280 {
281   Bool                swapit = false;
282   Bool                success = false;
283   float              *TR_arr=NULL, tr=0.0;
284   int                 i, k;
285   char                temp[64];
286   unsigned char       junk;
287   unsigned short      group, element;
288   unsigned short      dblock;
289   unsigned short      dblock1;
290   
291   FILE    *fp = NULL;
292   char    *pfname[1];
293   char     filename[512];
294   char     filename_temp[512];
295   Sequence *seq = (Sequence *)seq_get_current();
296   int end = get_end_frame(seq);
297 
298   strcpy(filename, "");
299   strcpy(filename_temp, "");
300 
301   TR_arr = fvector_alloc(seq->offset, end+1);
302 
303   for (i = seq->offset; i <= end; i = i+seq->stride)
304     {
305       success=false;
306       parse_fname(seq->filename, filename_temp, i);
307       sprintf(filename, "%s%s", filename_temp, ".ani");
308       *pfname = filename;
309       if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
310         {
311           error("nema_hdrinfo_extract: no such file or filename", warning);
312           return(false);
313         }
314   
315       if ((fp = fopen_2(*pfname, "rb")) == NULL)
316         {
317           error("file pointer equals NULL", warning);
318           return(false);
319         }
320       
321 
322       set_swapping_ts(0);
323   /*TR = (float *)ralloc(sizeof(float));*/
324       while (fp && fread(&group, sizeof(short), 1, fp) && !success)
325         {
326           if(!swapit)
327             {
328               if(!valid_group(group))
329                 {
330                   set_swapping_ts(1);
331                   short_swap((char *)&group);
332                   if (valid_group(group))
333                     swapit = true;
334                   else
335                     {
336                       short_swap((char *)&group);
337                       set_swapping_ts(0);
338                     }
339                 }
340             }
341           else
342             short_swap((char *)&group);
343           
344           fread(&element, sizeof(short), 1, fp);
345           short_swap((char *)&element);
346           fread(&dblock,  sizeof(short), 1, fp);
347           short_swap((char *)&dblock);
348           fread(&dblock1, sizeof(short), 1, fp);
349           short_swap((char *)&dblock1);
350           
351           if (dblock1 != 0)
352             {
353               format("error: non-standard block size\n");
354               return(false);
355             }
356           else if (group == 0x0018 && element == 0x0080)
357             {
358               fread(temp, sizeof(char), dblock, fp);
359               temp[dblock] = '\0';
360               sscanf(temp, "%f", &tr);
361               TR_arr[i]=tr;
362               format("TR: %f\n", tr);
363               success = true;
364             }
365           else
366             for (k = 0; k < dblock; k++)
367               fread(&junk, sizeof(char), 1, fp);
368           
369           if (success) break;
370         }
371        fclose_2(fp, filename);
372     }
373 
374   if (success)
375     {
376       seq->props = proplist_rm(seq->props, TR_DATA);
377       seq->props = proplist_addifnp(seq->props, (void *)TR_arr, TR_DATA, rfree, false);
378     }
379   
380   
381   return (success);
382 }
383 
384 /* added GJMP 6/3/2001 */
385 /* mjs 11/11/05 modified to insert Flip Angle as an array on sequence proplist */
386 Bool   nema_hdr_flip_angle_extract()
387 {
388   Bool                swapit = false;
389   Bool                success = false;
390   float              *FA_arr=NULL, fa=0.0;
391   int                 i, k;
392   char                temp[64];
393   unsigned char       junk;
394   unsigned short      group, element;
395   unsigned short      dblock;
396   unsigned short      dblock1;
397 
398   FILE    *fp = NULL;
399   char    *pfname[1];
400   char     filename[512];
401   char     filename_temp[512];
402   Sequence *seq = (Sequence *)seq_get_current();
403   int end = get_end_frame(seq);
404 
405   strcpy(filename, "");
406   strcpy(filename_temp, "");
407 
408   FA_arr = fvector_alloc(seq->offset, end+1);
409 
410   for (i = seq->offset; i <= end; i = i+seq->stride)
411     {
412       success=false;
413       parse_fname(seq->filename, filename_temp, i);
414       sprintf(filename, "%s%s", filename_temp, ".ani");
415       *pfname = filename;
416       if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
417         {
418           error("nema_hdrinfo_extract: no such file or filename", warning);
419           return(false);
420         }
421   
422       if ((fp = fopen_2(*pfname, "rb")) == NULL)
423         {
424           error("file pointer equals NULL", warning);
425           return(false);
426         }
427       
428       set_swapping_ts(0);
429       /*flip_angle = (float *)ralloc(sizeof(float));*/
430       while (fp && fread(&group, sizeof(short), 1, fp) && !success)
431         {
432           if(!swapit)
433             {
434               if(!valid_group(group))
435                 {
436                   set_swapping_ts(1);
437                   short_swap((char *)&group);
438                   if (valid_group(group))
439                     swapit = true;
440                   else
441                     {
442                       short_swap((char *)&group);
443                       set_swapping_ts(0);
444                     }
445                 }
446             }
447           else
448             short_swap((char *)&group);
449           
450           fread(&element, sizeof(short), 1, fp);
451           short_swap((char *)&element);
452           fread(&dblock,  sizeof(short), 1, fp);
453           short_swap((char *)&dblock);
454           fread(&dblock1, sizeof(short), 1, fp);
455           short_swap((char *)&dblock1);
456           
457           if (dblock1 != 0)
458             {
459               format("error: non-standard block size\n");
460               return(false);
461             }
462           else if (group == 0x0019 && element == 0x101A)
463             {
464               fread(temp, sizeof(char), dblock, fp);
465               temp[dblock] = '\0';
466               sscanf(temp, "%f", &fa);
467               success = true;
468               format("FA: %f", fa);
469               FA_arr[i]=fa;
470             }
471           else
472             for (k = 0; k < dblock; k++)
473               fread(&junk, sizeof(char), 1, fp);
474           
475           if (success) break;
476         }
477       
478       fclose_2(fp, filename);
479     }
480 
481 
482   if (success)
483     {
484       seq->props = proplist_rm(seq->props, FLIP_ANGLE_DATA);
485       seq->props = proplist_addifnp(seq->props, (void *)FA_arr, FLIP_ANGLE_DATA, rfree, false);
486     }
487   
488       
489   return (success);
490 }
491 
492 Bool   nema_hdr_dynstimes_extract()
493 {/*here only need to read in one image worth */
494   Bool                swapit = false;
495   Bool                success = false;
496   float              *times=NULL;
497   int                 i, j, k, stime;
498   int                 dynstudy;
499   char               *buffer = NULL;
500   char                temp[64];
501   unsigned char       junk;
502   unsigned short      group, element;
503   unsigned short      dblock;
504   unsigned short      dblock1;
505   Sequence * seq = (Sequence *)seq_get_current();
506 
507   FILE    *fp = NULL;
508   char    *pfname[1];
509   char     filename[512];
510   char     filename_temp[512];
511 
512   strcpy(filename, "");
513   strcpy(filename_temp, "");
514 
515   parse_fname(seq->filename, filename_temp, seq->offset);
516   sprintf(filename, "%s%s", filename_temp, ".ani");
517   *pfname = filename;
518   if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
519     {
520       error("nema_hdrinfo_extract: no such file or filename", warning);
521       return(false);
522     }
523   
524   if ((fp = fopen_2(*pfname, "rb")) == NULL)
525     {         
526         error("file pointer equals NULL", warning);
527         return(false);
528     }
529   
530   set_swapping_ts(0);
531   while (fp && fread(&group, sizeof(short), 1, fp) && !success)
532     { 
533       if(!swapit)
534         {
535           if(!valid_group(group))
536             {
537               set_swapping_ts(1);
538               short_swap((char *)&group);
539               if (valid_group(group)) 
540                 swapit = true;
541               else    
542                 {
543                   short_swap((char *)&group);
544                   set_swapping_ts(0);
545                 }
546             }
547         }
548       else
549         short_swap((char *)&group);
550       
551       fread(&element, sizeof(short), 1, fp);
552       short_swap((char *)&element);
553       fread(&dblock,  sizeof(short), 1, fp);
554       short_swap((char *)&dblock);
555       fread(&dblock1, sizeof(short), 1, fp);
556       short_swap((char *)&dblock1);
557       
558       if (dblock1 != 0)
559         {
560           format("error: non-standard block size\n");
561           return(false);
562         }
563       else if (group == 0x0019 && element == 0x1015)
564         {
565           fread(temp, sizeof(char), dblock, fp);
566           temp[dblock] = '\0';
567           sscanf(temp, "%d", &dynstudy);
568           if (!dynstudy)
569             {
570               return(false);
571             }
572         }
573       else if (group == 0x0019 && element == 0x101B)
574         {
575           fread(temp, sizeof(char), dblock, fp);
576           temp[dblock] = '\0';
577           sscanf(temp, "%d", &stime);
578         } 
579       else if (group == 0x0019 && element == 0x1021)
580         {
581           buffer = (char *)ralloc(dblock*sizeof(char));
582           times = fvector_alloc(0, stime);
583           fread(buffer, sizeof(char), dblock, fp);
584           
585           j = k = 0;
586           for (i = 0; i < stime; i++)
587             {
588               while (buffer[j] != '\\')
589                 temp[k++] = buffer[j++];
590               temp[k] = '\0';
591               sscanf(temp, "%f", &(times[i]));
592               k = 0; j++;
593             }
594           rfree(buffer);
595           success = true;
596           break;
597         } 
598       else 
599         for (i = 0; i < dblock; i++) 
600           fread(&junk, sizeof(char), 1, fp);
601     }
602   fclose_2(fp, filename);
603   if (success)
604     {
605       seq->dim[3] = times[1];
606       format("Time: %f\n", times[1]);
607      }
608     
609   return (success);
610 }
611 
612 
613 Bool   nema_hdr_heartrate_extract()
614 {
615   Bool                swapit = false;
616   Bool                success = false;
617   float              *times, btime, phases, heartrate;
618   float               beattime;
619   int                 i;
620   int                 psuccess = 0;
621   char                temp[64];
622   unsigned char       junk;
623   unsigned short      group, element;
624   unsigned short      dblock;
625   unsigned short      dblock1;
626   Sequence * seq = (Sequence *)seq_get_current();
627   FILE    *fp = NULL;
628   char    *pfname[1];
629   char     filename[512];
630   char     filename_temp[512];
631 
632   strcpy(filename, "");
633   strcpy(filename_temp, "");
634  
635   parse_fname(seq->filename, filename_temp, seq->offset);
636   sprintf(filename, "%s%s", filename_temp, ".ani");
637   *pfname = filename;
638   if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
639     {
640       error("nema_hdrinfo_extract: no such file or filename", warning);
641       return(false);
642     }
643   
644   if ((fp = fopen_2(*pfname, "rb")) == NULL)
645     {   
646       error("file pointer equals NULL", warning);
647       return(false);
648     }
649   
650   set_swapping_ts(0);
651   while (fp && fread(&group, sizeof(short), 1, fp) && !success)
652     { 
653       if(!swapit)
654         {
655           if(!valid_group(group))
656             {
657               set_swapping_ts(1);
658               short_swap((char *)&group);
659               if (valid_group(group)) 
660                 swapit = true;
661               else    
662                 {
663                   short_swap((char *)&group);
664                   set_swapping_ts(0);
665                 }
666             }
667         }
668       else
669         short_swap((char *)&group);
670       
671       fread(&element, sizeof(short), 1, fp);
672       short_swap((char *)&element);
673       fread(&dblock,  sizeof(short), 1, fp);
674       short_swap((char *)&dblock);
675       fread(&dblock1, sizeof(short), 1, fp);
676       short_swap((char *)&dblock1);
677       
678       if (dblock1 != 0)
679         {
680           format("error: non-standard block size\n");
681           return(false);
682         }
683       else if (group == 0x0019 && element == 0x1022)
684         {
685           fread(temp, sizeof(char), dblock, fp);
686           temp[dblock] = '\0';
687           sscanf(temp, "%f", &btime);
688           if (psuccess == 2) success = true;
689           else psuccess++;
690         }
691       else if (group == 0x0019 && element == 0x1069)
692         {
693           fread(temp, sizeof(char), dblock, fp);
694           temp[dblock] = '\0';
695           sscanf(temp, "%f", &phases);
696           if (psuccess == 2) success = true;
697           else psuccess++;
698         }
699       else if (group == 0x0019 && element == 0x106A)
700         {
701           fread(temp, sizeof(char), dblock, fp);
702           temp[dblock] = '\0';
703           sscanf(temp, "%f", &heartrate);
704           if (psuccess == 2) success = true;
705           else psuccess++;
706         }
707       else 
708         for (i = 0; i < dblock; i++) 
709           fread(&junk, sizeof(char), 1, fp);
710     }
711   
712   if (phases == 1) success = false;
713   fclose_2(fp, filename);
714   if (success)
715     {
716       times = fvector_alloc(0, phases);
717       beattime = 1.0/(heartrate/60);
718       for (i = 0; i < phases; i++)
719         times[i] = btime + (float)i*(beattime/(phases-1));
720       /*
721         seq->props = proplist_rm(seq->props, DYNSTIME);
722         seq->props = proplist_addifnp(seq->props, (void *) times, DYNSTIME, dynstimes_free, false);
723       */
724       seq->dim[3] = times[1];
725     }
726   
727   return (success);
728 }
729 
730 
731 Bool  nema_hdr_patientdetails_extract()
732 {
733   Bool                swapit = false;
734   Bool                psuccess = false;
735   int                 i;
736   char               *details = NULL;
737   unsigned char       junk;
738   unsigned short      group, element;
739   unsigned short      dblock;
740   unsigned short      dblock1;
741   FILE    *fp = NULL;
742   char    *pfname[1];
743   char     filename[512];
744   char     filename_temp[512];
745   Sequence *seq = (Sequence *)seq_get_current();
746 
747   strcpy(filename, "");
748   strcpy(filename_temp, "");
749  
750   parse_fname(seq->filename, filename_temp, seq->offset);
751   sprintf(filename, "%s%s", filename_temp, ".ani");
752   *pfname = filename;
753   if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
754     {
755       error("nema_hdrinfo_extract: no such file or filename", warning);
756       return(false);
757     }
758   if ((fp = fopen_2(*pfname, "rb")) == NULL)
759     {
760       error("file pointer equals NULL", warning);
761       return(false);
762     }
763 
764   details = (char *)cvector_alloc(0, 256);
765   set_swapping_ts(0);
766   while (fp && fread(&group, sizeof(short), 1, fp) && !psuccess)
767     { 
768       if(!swapit)
769         {
770           if(!valid_group(group))
771             {
772               set_swapping_ts(1);
773               short_swap((char *)&group);
774               if (valid_group(group)) 
775                 swapit = true;
776               else    
777                 {
778                   short_swap((char *)&group);
779                   set_swapping_ts(0);
780                 }
781             }
782         }
783       else
784         short_swap((char *)&group);
785  
786       fread(&element, sizeof(short), 1, fp);
787       short_swap((char *)&element);
788       fread(&dblock,  sizeof(short), 1, fp);
789       short_swap((char *)&dblock);
790       fread(&dblock1, sizeof(short), 1, fp);
791       short_swap((char *)&dblock1);
792 
793       if (dblock1 != 0)
794         {
795           format("error: non-standard block size\n");
796           cvector_free(details, 0);
797           return(false);
798         }
799       else if (group == 0x0008 && element == 0x0020)
800         {
801           fread(details, sizeof(char), dblock, fp);
802           details[dblock] = '\0';
803           psuccess = true;
804         }
805       else 
806         for (i = 0; i < dblock; i++) 
807           fread(&junk, sizeof(char), 1, fp);
808     }
809 
810   if (psuccess != true)
811                 cvector_free(details, 0);
812   else
813     {
814       seq->props = proplist_rm(seq->props, PAT_DATA);
815       seq->props = proplist_addifnp(seq->props, (void *) details, PAT_DATA, rfree, false);
816     }
817   fclose_2(fp, filename);
818     
819   return (psuccess);
820 }
821 
822 
823 Bool  nema_hdr_voxelscale_extract()
824 {
825   Bool                swapit = false;
826   float               xsize,ysize,zsize;
827   float               zgap;
828   int                 psuccess = 0;
829   int                 i;
830   char                dimension[64];
831   unsigned char       junk;
832   unsigned short      group, element;
833   unsigned short      dblock;
834   unsigned short      dblock1;
835   FILE    *fp = NULL;
836   char    *pfname[1];
837   char     filename[512];
838   char     filename_temp[512];
839   Sequence * seq = (Sequence *)seq_get_current();
840 
841   strcpy(filename, "");
842   strcpy(filename_temp, "");
843  
844   parse_fname(seq->filename, filename_temp, seq->offset);
845   sprintf(filename, "%s%s", filename_temp, ".ani");
846   *pfname = filename;
847   if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
848     {
849       error("nema_hdrinfo_extract: no such file or filename", warning);
850       return(false);
851     }
852       
853   if ((fp = fopen_2(*pfname, "rb")) == NULL)
854     {  
855       error("file pointer equals NULL", warning);
856       return(false);
857     }
858   
859   set_swapping_ts(0);
860   while (fp && fread(&group, sizeof(short), 1, fp) && psuccess < 3)
861     { 
862       if(!swapit)
863         {
864           if(!valid_group(group))
865             {
866               set_swapping_ts(1);
867               short_swap((char *)&group);
868               if (valid_group(group)) 
869                 swapit = true;
870               else    
871                 {
872                   short_swap((char *)&group);
873                   set_swapping_ts(0);
874                 }
875             }
876         }
877       else
878         short_swap((char *)&group);
879  
880       fread(&element, sizeof(short), 1, fp);
881       short_swap((char *)&element);
882       fread(&dblock,  sizeof(short), 1, fp);
883       short_swap((char *)&dblock);
884       fread(&dblock1, sizeof(short), 1, fp);
885       short_swap((char *)&dblock1);
886 
887       if (dblock1 != 0)
888         {
889           format("error: non-standard block size\n");
890           return(false);
891         }
892       else if (group == 0x0018 && element == 0x0050)
893         {
894           fread(dimension, sizeof(char),dblock,fp);
895           dimension[dblock] = '\0';
896           sscanf(dimension,"%f",&zsize);
897                                         psuccess++;
898         }
899       else if (group == 0x0021 && element == 0x1221)
900         {
901           fread(dimension, sizeof(char),dblock,fp);
902           dimension[dblock] = '\0';
903           sscanf(dimension,"%f",&zgap);
904                                         psuccess++;
905         }
906                         else if(group == 0x0028 && element == 0x0030)
907                                 {
908                                         fread(dimension, sizeof(char),dblock,fp);
909                                         dimension[dblock] = '\0';
910                                         sscanf(dimension,"%f\\%f",&xsize,&ysize);
911                                         psuccess++;
912                                 }
913  
914       else 
915         for (i = 0; i < dblock; i++) 
916           fread(&junk, sizeof(char), 1, fp);
917     }
918 
919   if (psuccess == 3)
920     {
921       /*
922         iscale = vec3_alloc();
923         iscale->el[0] = xsize;
924         iscale->el[1] = ysize;
925         iscale->el[2] = zsize + zgap;
926         
927         seq->props = proplist_rm(seq->props, VOXELS);
928         seq->props = proplist_addifnp(seq->props, iscale, VOXELS, vec3_free, false);
929       */
930 
931       seq->dim[0] = xsize;
932       seq->dim[1] = ysize;
933       seq->dim[2] = zsize;
934 
935     }
936   fclose_2(fp, filename);
937     
938   return ((Bool)(psuccess == 3));
939 }
940 
941 
942 Imrect *nema_read_image(char *pathname)
943 {
944   Imrect *imrect = NULL, *imrect2 = NULL, *fim = NULL;
945   Imregion imregion;
946   FILE                    *fp_img=fopen_2(pathname, "rb");
947   Vartype                new_vtype;
948   float        fpmin, fpmax, range;
949   float        sslope=0.0, sinter=0.0,rslope=0.0, rinter=0;
950   float                   val, pix;
951   int                      i, j, k;
952   short                   endian=0;
953   unsigned char               junk;
954   char               dimension[64];
955   unsigned short              abits;
956   unsigned short              sbits;
957   unsigned short              sign;
958   unsigned short              rows;
959   unsigned short              cols;
960   unsigned short             group;
961   unsigned short           element;
962   unsigned short            dblock;
963   unsigned short           dblock1;
964   float          xsize,ysize,zsize;
965   float                       zgap;
966   Sequence * seq = (Sequence *)seq_get_current();
967 
968   set_swapping_ts(0);
969   while (fp_img&&fread(&group, sizeof(short),1,fp_img))
970     {
971       if(!endian)
972         {
973           if(valid_group(group)!=1)
974             {
975               set_swapping_ts(1);
976               short_swap((char *)&group);
977               if (valid_group(group) == 1) endian = 1;
978               else    
979                 {
980                   short_swap((char *)&group);
981                   set_swapping_ts(0);
982                 }
983             }
984         }
985       else
986         {
987           short_swap((char *)&group);
988         } 
989       fread(&element, sizeof(short),1,fp_img);
990       short_swap((char *)&element);
991       fread(&dblock,  sizeof(short),1,fp_img);
992       short_swap((char *)&dblock);
993       fread(&dblock1, sizeof(short),1,fp_img);
994       short_swap((char *)&dblock1);
995 
996       if (group == 0x7FE0 && element == 0x0010)
997         break; /* found the image data */
998       if (dblock1!=0)
999         {
1000           format("error: non-standard block size\n");
1001           fclose_2(fp_img,pathname); 
1002           return(NULL);
1003         }
1004       else if (group == 0x0018 && element == 0x0050)
1005         {
1006           fread(dimension, sizeof(char),dblock,fp_img);
1007           dimension[dblock] = '\0';
1008           sscanf(dimension,"%f",&zsize);
1009         }
1010       else if (group == 0x0021 && element == 0x1221)
1011         {
1012           fread(dimension, sizeof(char),dblock,fp_img);
1013           dimension[dblock] = '\0';
1014           sscanf(dimension,"%f",&zgap);
1015         }
1016 /* old discredited scale facotrs for previous versions of NEMA NAT 20/1/2002 */
1017       else if (group == 0x0029 && element == 0x1010)
1018         {
1019           fread(dimension, sizeof(char), dblock, fp_img);
1020           dimension[dblock] = '\0';
1021           sscanf(dimension, "%f", &fpmin);
1022         }
1023       else if (group == 0x0029 && element == 0x1011)
1024         {
1025           fread(dimension, sizeof(char), dblock, fp_img);
1026           dimension[dblock] = '\0';
1027           sscanf(dimension, "%f", &fpmax);
1028         }
1029 /* new shiny scale factors until Phillips change things again */
1030 /* mjs 17.11.05 sadly for the calculations that tina does we picked the wrong ones! grr */
1031       /*else if (group == 0x0029 && element == 0x1053)*/
1032       else if (group == 0x0028 && element == 0x1053)
1033         {
1034           fread(dimension, sizeof(char), dblock, fp_img);
1035           dimension[dblock] = '\0';
1036           sscanf(dimension, "%f", &rslope);
1037 /* arbitrary rescaling to put the data into some sensible range for 16 bit storage*/
1038           /*mjs well. if the right ones were used wouldn't need this....
1039             slope *= 1000; */
1040         }
1041        else if (group == 0x0029 && element == 0x1053)
1042         {
1043           fread(dimension, sizeof(char), dblock, fp_img);
1044           dimension[dblock] = '\0';
1045           sscanf(dimension, "%f", &sslope);
1046         }
1047 
1048       /*else if (group == 0x0029 && element == 0x1052)*/
1049       else if (group == 0x0028 && element == 0x1052)
1050         {
1051           fread(dimension, sizeof(char), dblock, fp_img);
1052           dimension[dblock] = '\0';
1053           sscanf(dimension, "%f", &rinter);
1054         }
1055       else if (group == 0x0029 && element == 0x1052)
1056         {
1057           fread(dimension, sizeof(char), dblock, fp_img);
1058           dimension[dblock] = '\0';
1059           sscanf(dimension, "%f", &sinter);
1060         }
1061 
1062       else if (group == 0x0028)
1063         {
1064           if(element == 0x0010)
1065             {
1066               fread(&rows, sizeof(short),1,fp_img);
1067               short_swap((char *)&rows);
1068             }
1069           else if(element == 0x0011)
1070             {
1071               fread(&cols, sizeof(short),1,fp_img);
1072               short_swap((char *)&cols);
1073             }
1074           else if(element == 0x0103)
1075             {
1076               fread(&sign, sizeof(short),1,fp_img);
1077               short_swap((char *)&sign);
1078             }
1079           else if(element == 0x0101)
1080             {
1081               fread(&sbits, sizeof(short),1,fp_img);
1082               short_swap((char *)&sbits);
1083             }
1084           else if(element == 0x0100)
1085             {
1086               fread(&abits, sizeof(short),1,fp_img);
1087               short_swap((char *)&abits);
1088             }
1089           else if(element == 0x0030)
1090             {
1091               fread(dimension, sizeof(char),dblock,fp_img);
1092               dimension[dblock] = '\0';
1093               sscanf(dimension,"%f\\%f",&xsize,&ysize);
1094             }
1095           else for (i=0;i<dblock;i++) 
1096             fread(&junk, sizeof(char),1,fp_img);
1097         }   
1098       else 
1099         for (i=0;i<dblock;i++) fread(&junk, sizeof(char),1,fp_img);
1100     }
1101 
1102     
1103   if (fp_img)
1104     {
1105       imregion.lx = 0;
1106       imregion.ux = cols;
1107       imregion.ly = 0;
1108       imregion.uy = rows;
1109         
1110       format("x_dim=%i ",cols);
1111       format("y_dim=%i\n",rows);
1112       if (sign == 0)
1113         {
1114           if (abits == 16 || abits == 12) 
1115             {
1116               new_vtype = ushort_v;
1117             }
1118           else new_vtype = uchar_v;
1119         }
1120       else
1121         { 
1122           if (abits == 16 || abits == 12) 
1123             {
1124               new_vtype = short_v;
1125             }
1126           else new_vtype = char_v;
1127         }
1128       if(abits == 12) imregion.ux = 3.0*imregion.ux/4;
1129       cols = imregion.ux;
1130 
1131       imrect = im_alloc(rows, cols, &imregion, (Vartype)new_vtype);
1132 
1133       if (!fread_imrect_data(imrect, fp_img, pathname))
1134         {
1135           im_free(imrect);
1136           imrect = NULL;
1137           fclose_2(fp_img, pathname);
1138           return(NULL);
1139         }
1140       fclose_2(fp_img, pathname);
1141       if (abits == 12)
1142         {
1143           imrect2 = im_nema_conv(imrect);
1144           im_free(imrect);
1145           imrect = imrect2;
1146         }
1147       if (imrect!=NULL&&endian)
1148         {
1149           im_endian_inplace(imrect);
1150         }
1151 
1152 /*
1153       format("scale min %.2f  scale max %.2f\n", fpmin, fpmax);
1154 */
1155       format("slope %.2f  intercept %.2f\n", sslope, sinter);
1156       range = pow(2.0, sbits) - 1;
1157       fim = im_alloc(imrect->height, imrect->width, imrect->region, 
1158                      (Vartype)float_v);
1159       for(j = imrect->region->ly; j < imrect->region->uy; j++)
1160         for(k = imrect->region->lx; k < imrect->region->ux; k++)
1161           {
1162             if ((pix = im_get_pixf(imrect, j, k)) != 0.0)
1163             {
1164                if(sslope==0.0)
1165                   val = ((fpmax-fpmin)*pix)/(float)range + fpmin;
1166                else
1167                   val = (pix - sinter)/sslope;
1168             }
1169             else
1170                val = 0.0; 
1171             im_put_pixf(val, fim, j, k);    
1172           }   
1173       /*
1174         iscale = vec3_alloc();
1175         iscale->el[0] = xsize;
1176         iscale->el[1] = ysize;
1177         iscale->el[2] = zsize + zgap;
1178         
1179         if (seq != NULL)
1180         {
1181         seq->props = proplist_rm(seq->props, VOXELS);
1182         seq->props = proplist_addifnp(seq->props, iscale, VOXELS, vec3_free, false);
1183         }
1184       */
1185 
1186       seq->dim[0] = xsize;
1187       seq->dim[1] = ysize;
1188       seq->dim[2] = zsize;
1189 
1190     }
1191   
1192   im_free(imrect);
1193   return fim;
1194 }
1195 
1196 
1197 /*
1198   recover information from nema headers ref'd in seq using hdrextract_func
1199   which should store info in image propslist.  hdrextract_func should be
1200   passed fileptr and imrect and should return Bool success. 
1201 */
1202 /*
1203 Bool    nema_hdrinfo_extract(Sequence *seq, void (*hdrextract_func))
1204 {
1205   List  *store = (List *)get_seq_start_el(seq);
1206   Bool     status = false, success = true ;
1207   FILE    *fp = NULL;
1208   char    *pfname[1];
1209   char     filename[512];
1210   char     temp[512];
1211   int      i;
1212   int end = get_end_frame(seq);
1213 
1214   strcpy(filename, "");
1215   strcpy(temp, "");
1216  
1217   for(i = (seq->offset); i <= end; i= i+seq->stride)
1218     {
1219       parse_fname(seq->filename, temp, i);
1220       sprintf(filename, "%s%s", temp, ".ani");
1221       *pfname = filename;
1222       if (strchr(filename, '*') && !fname_resolve(NULL, filename, pfname))
1223       {
1224         error("nema_hdrinfo_extract: no such file or filename", warning);
1225         return(false);
1226       }
1227       
1228       if ((store == NULL) || (store->to == NULL))
1229         continue;
1230       if ((fp = fopen_2(*pfname, "rb")) == NULL)
1231         continue;
1232       status = ((Bool (*) ())hdrextract_func)(fp, (Imrect *)(store->to));
1233       fclose_2(fp, filename);
1234       if (!status)
1235         success = status;
1236       store = store->next;
1237     }
1238 
1239   return(success);
1240 }
1241 */
1242 
1243 
1244 
1245 
1246 
1247 
1248 
1249 
1250 
1251 
1252 
1253 

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