]> Creatis software - gdcm.git/blob - src/gdcmFile.cxx
Ajout de quelques Accesseurs pour la Transfert Syntax.
[gdcm.git] / src / gdcmFile.cxx
1 // gdcmFile.cxx
2
3 #include "gdcm.h"
4
5 static void _Swap(void* im, int swap, int lgr, int nb);
6
7 /////////////////////////////////////////////////////////////////
8 /**
9  * \ingroup   gdcmFile
10  * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
11  * \file (see SetFileName, SetDcmTag and Write)
12  * \Opens (in read only and when possible) an existing file and checks
13  * \for DICOM compliance. Returns NULL on failure.
14  * \Note: the in-memory representation of all available tags found in
15  * \the DICOM header is post-poned to first header information access.
16  * \This avoid a double parsing of public part of the header when
17  * \one sets an a posteriori shadow dictionary (efficiency can be
18  * \seen as a side effect).   
19  *
20  * @param 
21  *
22  * @return      
23  */
24
25 /////////////////////////////////////////////////////////////////
26 /**
27  * \ingroup   gdcmFile
28  * \brief    constructeur 
29  *
30  * @param 
31  *
32  * @return
33  */
34  
35  
36 gdcmFile::gdcmFile(string & filename)
37         :gdcmHeader(filename.c_str())
38 {
39 }
40
41
42 /////////////////////////////////////////////////////////////////
43 /**
44  * \ingroup   gdcmFile
45  * \brief     Renvoie la longueur A ALLOUER pour recevoir les pixels de l'image
46  * \            ou DES images dans le cas d'un multiframe
47  * \            ATTENTION : il ne s'agit PAS de la lgr du groupe des Pixels     
48  * \            (dans le cas d'images compressees, elle n'a pas de sens).
49  *
50  * @param void  Rien en entree
51  *
52  * @return      longueur a allouer 
53  */
54
55 size_t gdcmFile::GetImageDataSize(void) {
56         int nbLignes, nbCol, nbFrames, nb;
57         string str_nbFrames, str_nb;
58         // Nombre de Lignes     
59         nbLignes=atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0010).c_str());
60         // Nombre de Colonnes   
61         nbCol   =atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0011).c_str());
62
63         // Nombre de Frames     
64         str_nbFrames=gdcmHeader::GetPubElValByNumber(0x0028,0x0008);
65         
66         if (str_nbFrames == "UNFOUND" ) {
67                 nbFrames = 1;
68         } else {
69                 nbFrames = atoi(str_nbFrames.c_str() );
70         }
71         
72         // Nombre de Bits Alloues pour le stockage d'un Pixel   
73         str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
74
75         if (str_nb == "UNFOUND" ) {
76                 nb = 16;
77         } else {
78                 nb = atoi(str_nb.c_str() );
79         }
80
81         size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
82         return (lgrTotale);
83
84 }
85
86 /////////////////////////////////////////////////////////////////
87 /**
88  * \ingroup   gdcmFile
89  * \brief amene en mémoire les Pixels d'une image NON COMPRESSEE
90  * \Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
91  *
92  * @param rien
93  *
94  * @return      Pointeur sur la zone mémoire contenant les Pixels lus
95  */
96
97 void * gdcmFile::GetImageData (void) {
98         
99         char* Pixels;
100         int nbLignes, nbCol;
101
102         int nbFrames, nb, nbu, highBit, signe;
103         string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
104         
105         unsigned short int mask = 0xffff;
106                 
107         // Nombre de Lignes     
108         nbLignes=atoi(GetPubElValByNumber(0x0028,0x0010).c_str());
109         // Nombre de Colonnes   
110         nbCol   =atoi(GetPubElValByNumber(0x0028,0x0011).c_str());
111
112         // Nombre de Frames     
113         str_nbFrames=GetPubElValByNumber(0x0028,0x0008);
114
115         
116         if (str_nbFrames == "UNFOUND" ) {
117                 nbFrames = 1;
118         } else {
119                 nbFrames = atoi(str_nbFrames.c_str() );
120         }
121         
122         // Nombre de Bits Alloues       
123         str_nb=GetPubElValByNumber(0x0028,0x0100);
124
125         if (str_nb == "UNFOUND" ) {
126                 nb = 16;
127         } else {
128                 nb = atoi(str_nb.c_str() );
129         }
130         
131         // Nombre de Bits Utilises      
132         str_nbu=GetPubElValByNumber(0x0028,0x0101);
133
134         if (str_nbu == "UNFOUND" ) {
135                 nbu = nb;
136         } else {
137                 nbu = atoi(str_nbu.c_str() );
138         }       
139         
140         // Position du Bit de Poids Fort        
141         str_highBit=GetPubElValByNumber(0x0028,0x0102);
142
143         if (str_highBit == "UNFOUND" ) {
144                 highBit = nb - 1;
145         } else {
146                 highBit = atoi(str_highBit.c_str() );
147         }
148                 
149         // Signe des Pixels 
150         str_signe=GetPubElValByNumber(0x0028,0x0103);
151
152         if (str_signe == "UNFOUND" ) {
153                 signe = 1;
154         } else {
155                 signe = atoi(str_signe.c_str() );
156         }
157         
158         // Longueur en Octets des Pixels a lire
159         size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
160         
161         //Pixels = (char *) g_malloc(lgrTotale);
162         Pixels = (char *) malloc(lgrTotale);
163         
164         GetPixels(lgrTotale, Pixels);
165
166         // On remet les Octets dans le bon ordre si besoin est
167         if (nb != 8) {
168                 //int sw = GetSwapCode();
169
170         //      _Swap (Pixels, sw, lgrTotale, nb);                                               // A REMETTRE
171         }
172         
173         // On remet les Bits des Octets dans le bon ordre si besoin est
174         //
175         // ATTENTION :  Jamais confronté a des pixels stockes sur 32 bits 
176         //                      avec moins de 32 bits utilises
177         //                      et dont le bit de poids fort ne serait pas la ou on l'attend ...
178         //                      --> ne marchera pas dans ce cas 
179         if (nbu!=nb){
180                 mask = mask >> (nb-nbu);
181                 int l=(int)lgrTotale/(nb/8);
182                 unsigned short *deb = (unsigned short *)Pixels;
183                 for(int i=0;i<l;i++) {
184                                 *deb = (*deb >> (nbu-highBit-1)) & mask;
185                                 deb ++;
186                 }
187         }
188                 
189         printf ("on est sorti\n");
190         
191         // VOIR s'il ne faudrait pas l'affecter à un champ du dcmHeader
192         
193         return (Pixels);                
194 }
195
196
197 //
198 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
199 //
200
201 static void _Swap(void* im, int swap, int lgr, int nb) {                     
202 guint32 s32;
203 guint16 fort,faible;
204 int i;
205
206 if(nb == 16)
207     
208         switch(swap) {
209                 case 0:
210                 case 12:
211                 case 1234:
212                         break;
213                 
214                 case 21:
215                 case 3412:
216                 case 2143:
217                 case 4321:
218
219                         for(i=0;i<lgr;i++)
220                                 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
221                                                 | ((((unsigned short int*)im)[i])<<8);
222                         break;
223                         
224                 default:
225                         printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
226         } 
227  
228 if( nb == 32 )
229
230         switch (swap) {
231                 case 0:
232                 case 1234:
233                          break;
234
235                 case 4321:
236                          for(i=0;i<lgr;i++) {
237                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 4321 */
238                                 fort  =((unsigned long int*)im)[i]>>16;
239                                 fort=  (fort>>8)   | (fort<<8);
240                                 faible=(faible>>8) | (faible<<8);
241                                 s32=faible;
242                                 ((unsigned long int*)im)[i]=(s32<<16)|fort;
243                         }
244                         break;
245
246                 case 2143:
247                         for(i=0;i<lgr;i++) {
248                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 2143 */
249                                 fort=((unsigned long int*)im)[i]>>16;
250                                 fort=  (fort>>8)   | (fort<<8);
251                                 faible=(faible>>8) | (faible<<8);
252                                 s32=fort; 
253                                 ((unsigned long int*)im)[i]=(s32<<16)|faible;
254                         }
255                         break;
256   
257                 case 3412:
258                         for(i=0;i<lgr;i++) {
259                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 3412 */
260                                 fort=((unsigned long int*)im)[i]>>16;                  
261                                 s32=faible; 
262                                 ((unsigned long int*)im)[i]=(s32<<16)|fort;
263                         }                 
264                         break; 
265                                 
266                 default:
267                         printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
268         } 
269 return;
270 }
271
272
273