]> Creatis software - gdcm.git/blob - src/gdcmFile.cxx
Brutal commit. --- Frog
[gdcm.git] / src / gdcmFile.cxx
1 // gdcmFile.cxx
2
3 #include "gdcmFile.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 filename file to be opened for parsing
21  *
22  * @return      
23  */
24  
25 gdcmFile::gdcmFile(string & filename) 
26         :gdcmHeader(filename.c_str())   
27 {
28 }
29
30 gdcmFile::gdcmFile(const char * filename) 
31         :gdcmHeader(filename)   
32 {
33 }
34
35
36 /////////////////////////////////////////////////////////////////
37 /**
38  * \ingroup   gdcmFile
39  * \brief     Renvoie la longueur A ALLOUER pour recevoir les pixels de l'image
40  *              ou DES images dans le cas d'un multiframe
41  *              ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels        
42  *              (dans le cas d'images compressees, elle n'a pas de sens).
43  *
44  * @return      longueur a allouer 
45  */
46
47 size_t gdcmFile::GetImageDataSize(void) {
48         int nb;
49         string str_nb;
50
51         str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
52         if (str_nb == "gdcm::Unfound" ) {
53                 nb = 16;
54         } else {
55                 nb = atoi(str_nb.c_str() );
56       if (nb == 12) nb =16;
57         }
58
59         size_t lgrTotale =  GetXSize() *  GetYSize() *  GetZSize() *(nb/8);
60         return (lgrTotale);
61 }
62
63
64
65 /////////////////////////////////////////////////////////////////
66 /**
67  * \ingroup   gdcmFile
68  * \brief TODO
69  * \warning WARNING
70  *
71  */
72 void * gdcmFile::GetImageData (void) {
73         char * _Pixels;
74         // Longueur en Octets des Pixels a lire
75         size_t taille = GetImageDataSize();// ne faudrait-il pas la stocker?
76         _Pixels = (char *) malloc(taille);
77         GetImageDataIntoVector(_Pixels, taille);
78         
79                 // On l'affecte à un champ du dcmFile   
80         Pixels =    _Pixels;
81         lgrTotale = taille;
82         
83         // ca fait double emploi, il faudra nettoyer ça
84         
85         return(_Pixels);
86 }
87
88
89
90 /////////////////////////////////////////////////////////////////
91 /**
92  * \ingroup   gdcmFile
93  * \brief amene en mémoire dans une zone précisee par l'utilisateur
94  *        les Pixels d'une image NON COMPRESSEE
95  * \Warning Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
96  *
97  * @param destination
98  * @param MaxSize
99  *
100  * @return TODO JPR     
101  */
102
103 int gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
104
105 // Question :
106 //      dans quel cas la MaxSize sert-elle a quelque chose?
107 //      que fait-on si la taille de l'image est + gde    que Maxize?
108 //      que fait-on si la taille de l'image est + petite que Maxize?
109
110         
111         void * Pixels = destination;  // pour garder le code identique avec GetImageData
112
113         int nb, nbu, highBit, signe;
114         string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
115         
116         unsigned short int mask = 0xffff;
117         
118         // Longueur en Octets des Pixels a lire
119         size_t _lgrTotale = GetImageDataSize(); // ne faudrait-il pas la stocker?
120         
121         // si lgrTotale < MaxSize ==> Gros pb 
122         // -> on résoud à la goret
123         
124         if ( _lgrTotale < MaxSize ) MaxSize = _lgrTotale;
125         
126         GetPixels(MaxSize, destination);
127                         
128         // Nombre de Bits Alloues pour le stockage d'un Pixel   
129         str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
130
131         if (str_nb == "gdcm::Unfound" ) {
132                 nb = 16;
133         } else {
134                 nb = atoi(str_nb.c_str() );
135         }
136         
137         // Nombre de Bits Utilises      
138         str_nbu=GetPubElValByNumber(0x0028,0x0101);
139
140         if (str_nbu == "gdcm::Unfound" ) {
141                 nbu = nb;
142         } else {
143                 nbu = atoi(str_nbu.c_str() );
144         }       
145         
146         // Position du Bit de Poids Fort        
147         str_highBit=GetPubElValByNumber(0x0028,0x0102);
148
149         if (str_highBit == "gdcm::Unfound" ) {
150                 highBit = nb - 1;
151         } else {
152                 highBit = atoi(str_highBit.c_str() );
153         }
154                 
155         // Signe des Pixels 
156         str_signe=GetPubElValByNumber(0x0028,0x0103);
157
158         if (str_signe == "gdcm::Unfound" ) {
159                 signe = 1;
160         } else {
161                 signe = atoi(str_signe.c_str() );
162         }
163
164         // On remet les Octets dans le bon ordre si besoin est
165         if (nb != 8) {
166                 int _sw = GetSwapCode();
167
168                 _Swap (destination, _sw, _lgrTotale, nb);
169         }
170         
171         // On remet les Bits des Octets dans le bon ordre si besoin est
172         //
173         // ATTENTION :  Jamais confronté a des pixels stockes sur 32 bits 
174         //                      avec moins de 32 bits utilises
175         //                      et dont le bit de poids fort ne serait pas la ou on l'attend ...
176         //                      --> ne marchera pas dans ce cas 
177         if (nbu!=nb){
178                 mask = mask >> (nb-nbu);
179                 int l=(int)MaxSize/(nb/8);
180                 unsigned short *deb = (unsigned short *)Pixels;
181                 for(int i=0;i<l;i++) {
182                                 *deb = (*deb >> (nbu-highBit-1)) & mask;
183                                 deb ++;
184                 }
185         }
186                         
187         // VOIR s'il ne faudrait pas l'affecter à un champ du dcmHeader
188         
189         return 1; 
190 }
191
192
193 //
194 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
195 //
196
197 static void _Swap(void* im, int swap, int lgr, int nb) {                     
198 guint32 s32;
199 guint16 fort,faible;
200 int i;
201
202 if(nb == 16)
203     
204         switch(swap) {
205                 case 0:
206                 case 12:
207                 case 1234:
208                         break;
209                 
210                 case 21:
211                 case 3412:
212                 case 2143:
213                 case 4321:
214
215                         for(i=0;i<lgr;i++)
216                                 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
217                                                 | ((((unsigned short int*)im)[i])<<8);
218                         break;
219                         
220                 default:
221                         printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
222         } 
223  
224 if( nb == 32 )
225
226         switch (swap) {
227                 case 0:
228                 case 1234:
229                          break;
230
231                 case 4321:
232                          for(i=0;i<lgr;i++) {
233                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 4321 */
234                                 fort  =((unsigned long int*)im)[i]>>16;
235                                 fort=  (fort>>8)   | (fort<<8);
236                                 faible=(faible>>8) | (faible<<8);
237                                 s32=faible;
238                                 ((unsigned long int*)im)[i]=(s32<<16)|fort;
239                         }
240                         break;
241
242                 case 2143:
243                         for(i=0;i<lgr;i++) {
244                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 2143 */
245                                 fort=((unsigned long int*)im)[i]>>16;
246                                 fort=  (fort>>8)   | (fort<<8);
247                                 faible=(faible>>8) | (faible<<8);
248                                 s32=fort; 
249                                 ((unsigned long int*)im)[i]=(s32<<16)|faible;
250                         }
251                         break;
252   
253                 case 3412:
254                         for(i=0;i<lgr;i++) {
255                                 faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 3412 */
256                                 fort=((unsigned long int*)im)[i]>>16;                  
257                                 s32=faible; 
258                                 ((unsigned long int*)im)[i]=(s32<<16)|fort;
259                         }                 
260                         break; 
261                                 
262                 default:
263                         printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
264         } 
265 return;
266 }
267
268 /////////////////////////////////////////////////////////////////
269 /**
270  * \ingroup   gdcmFile
271  * \brief TODO JPR
272  * \warning doit-etre etre publique ?  FIXME JPR
273  *
274  * @param Data TODO JPR
275  * @param ExpectedSize TODO JPR
276  *
277  * @return TODO JPR     
278  */
279 int gdcmFile::SetImageData(void * Data, size_t ExpectedSize) {
280         
281         SetImageDataSize(ExpectedSize);
282         
283         Pixels =    Data;
284         lgrTotale = ExpectedSize;
285         
286         return(1);
287 }
288
289
290 /////////////////////////////////////////////////////////////////
291 /**
292  * \ingroup   gdcmFile
293  * \brief TODO JPR
294  * \
295  * \warning WARNING doit-etre etre publique ? FIXME JPR
296  *
297  * @param ImageDataSize TODO JPR
298  *
299  */
300
301 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
302
303         string content1;
304         string content2;
305         char car[20];
306         
307         // suppose que le ElValue (0x7fe0, 0x0010) existe ...
308         
309         sprintf(car,"%d",ImageDataSize);
310         content2=car;
311         SetPubElValByNumber(content2, 0x7fe0, 0x0010);
312         
313         ImageDataSize+=8;
314         sprintf(car,"%d",ImageDataSize);
315         content1=car;   
316         SetPubElValByNumber(content1, 0x7fe0, 0x0000);
317 }
318
319
320 /////////////////////////////////////////////////////////////////
321 /**
322  * \ingroup   gdcmFile
323  * \brief Ecrit sur disque les pixels d'UNE image
324  *        Aucun test n'est fait sur l'"Endiannerie" du processeur.
325  *        Ca sera à l'utilisateur d'appeler son Reader correctement
326  *        (Equivalent a IdImaWriteRawFile) FIXME JPR
327  *
328  * @param nomFichier TODO JPR
329  *
330  * @return TODO JPR     
331  */
332
333 int gdcmFile::WriteRawData (string nomFichier) {
334
335         FILE * fp1;
336         fp1 = fopen(nomFichier.c_str(),"wb");
337         if (fp1 == NULL) {
338                 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
339                 return (0);
340         } 
341         
342         fwrite (Pixels,lgrTotale, 1, fp1);
343         fclose (fp1);
344         return(1);
345 }
346
347
348
349 /////////////////////////////////////////////////////////////////
350 /**
351  * \ingroup   gdcmFile
352  * \brief Ecrit sur disque UNE image Dicom
353  *        Aucun test n'est fait sur l'"Endiannerie" du processeur.
354  *         Ca fonctionnera correctement (?) sur processeur Intel
355  *         (Equivalent a IdDcmWrite) FIXME JPR 
356  *
357  * @param nomFichier TODO JPR
358  *
359  * @return      TODO JPR
360  */
361
362 int gdcmFile::WriteDcmImplVR (string nomFichier) {
363    return WriteBase(nomFichier, ImplicitVR);
364 }
365
366 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
367    return WriteDcmImplVR (string (nomFichier));
368 }
369         
370 /////////////////////////////////////////////////////////////////
371 /**
372  * \ingroup   gdcmFile
373  *
374  * @param  nomFichier TODO JPR
375  *
376  * @return TODO JPR
377  */
378
379 int gdcmFile::WriteDcmExplVR (string nomFichier) {
380    return WriteBase(nomFichier, ExplicitVR);
381 }
382         
383 /////////////////////////////////////////////////////////////////
384 /**
385  * \ingroup   gdcmFile
386  * \brief  Ecrit sur disque UNE image ACR-NEMA 
387  *        (a l'attention des logiciels cliniques 
388  *        qui ne prennent en entrée QUE des images ACR ...
389  *        si un header DICOM est fourni en entree,
390  *        les groupes < 0x0008 et les groupes impairs sont ignores)
391  *        Aucun test n'est fait sur l'"Endiannerie" du processeur.
392  *        Ca fonctionnera correctement (?) sur processeur Intel
393  *        (Equivalent a IdDcmWrite) 
394  *
395  * @param nomFichier TODO JPR
396  *
397  * @return TODO JPR     
398  */
399
400 int gdcmFile::WriteAcr (string nomFichier) {
401    return WriteBase(nomFichier, ACR);
402 }
403
404 int gdcmFile::WriteBase (string nomFichier, FileType type) {
405
406    FILE * fp1;
407    fp1 = fopen(nomFichier.c_str(),"wb");
408    if (fp1 == NULL) {
409       printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
410       return (0);
411    }
412
413    if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
414       char * filePreamble;
415       // Ecriture Dicom File Preamble
416       filePreamble=(char*)calloc(128,1);
417       fwrite(filePreamble,128,1,fp1);
418       fwrite("DICM",4,1,fp1);
419    }
420
421    gdcmHeader::Write(fp1, type);
422    fwrite(Pixels, lgrTotale, 1, fp1);
423    fclose (fp1);
424    return(1);
425 }