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