5 static void _Swap(void* im, int swap, int lgr, int nb);
7 /////////////////////////////////////////////////////////////////
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).
20 * @param filename file to be opened for parsing
25 gdcmFile::gdcmFile(string & filename)
26 :gdcmHeader(filename.c_str())
30 gdcmFile::gdcmFile(const char * filename)
36 /////////////////////////////////////////////////////////////////
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).
44 * @return longueur a allouer
47 size_t gdcmFile::GetImageDataSize(void) {
48 int nbLignes, nbCol, nbFrames, nb;
49 string str_nbFrames, str_nb;
51 nbLignes=atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0010).c_str());
53 nbCol =atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0011).c_str());
56 str_nbFrames=gdcmHeader::GetPubElValByNumber(0x0028,0x0008);
58 if (str_nbFrames == "gdcm::Unfound" ) {
61 nbFrames = atoi(str_nbFrames.c_str() );
64 // Nombre de Bits Alloues pour le stockage d'un Pixel
65 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
67 if (str_nb == "gdcm::Unfound" ) {
70 nb = atoi(str_nb.c_str() );
73 size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
79 /////////////////////////////////////////////////////////////////
86 void * gdcmFile::GetImageData (void) {
88 // Longueur en Octets des Pixels a lire
89 size_t taille = GetImageDataSize();// ne faudrait-il pas la stocker?
90 _Pixels = (char *) malloc(taille);
91 GetImageDataIntoVector(_Pixels, taille);
93 // On l'affecte à un champ du dcmFile
97 // ca fait double emploi, il faudra nettoyer ça
104 /////////////////////////////////////////////////////////////////
107 * \brief amene en mémoire dans une zone précisee par l'utilisateur
108 * les Pixels d'une image NON COMPRESSEE
109 * \Warning Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
117 int gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
120 // dans quel cas la MaxSize sert-elle a quelque chose?
121 // que fait-on si la taille de l'image est + gde que Maxize?
122 // que fait-on si la taille de l'image est + petite que Maxize?
125 void * Pixels = destination; // pour garder le code identique avec GetImageData
127 int nb, nbu, highBit, signe;
128 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
130 unsigned short int mask = 0xffff;
132 // Longueur en Octets des Pixels a lire
133 size_t _lgrTotale = GetImageDataSize(); // ne faudrait-il pas la stocker?
135 // si lgrTotale < MaxSize ==> Gros pb
136 // -> on résoud à la goret
138 if ( _lgrTotale < MaxSize ) MaxSize = _lgrTotale;
140 GetPixels(MaxSize, destination);
142 // Nombre de Bits Alloues pour le stockage d'un Pixel
143 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
145 if (str_nb == "gdcm::Unfound" ) {
148 nb = atoi(str_nb.c_str() );
151 // Nombre de Bits Utilises
152 str_nbu=GetPubElValByNumber(0x0028,0x0101);
154 if (str_nbu == "gdcm::Unfound" ) {
157 nbu = atoi(str_nbu.c_str() );
160 // Position du Bit de Poids Fort
161 str_highBit=GetPubElValByNumber(0x0028,0x0102);
163 if (str_highBit == "gdcm::Unfound" ) {
166 highBit = atoi(str_highBit.c_str() );
170 str_signe=GetPubElValByNumber(0x0028,0x0103);
172 if (str_signe == "gdcm::Unfound" ) {
175 signe = atoi(str_signe.c_str() );
178 // On remet les Octets dans le bon ordre si besoin est
180 int _sw = GetSwapCode();
182 _Swap (destination, _sw, _lgrTotale, nb);
185 // On remet les Bits des Octets dans le bon ordre si besoin est
187 // ATTENTION : Jamais confronté a des pixels stockes sur 32 bits
188 // avec moins de 32 bits utilises
189 // et dont le bit de poids fort ne serait pas la ou on l'attend ...
190 // --> ne marchera pas dans ce cas
192 mask = mask >> (nb-nbu);
193 int l=(int)MaxSize/(nb/8);
194 unsigned short *deb = (unsigned short *)Pixels;
195 for(int i=0;i<l;i++) {
196 *deb = (*deb >> (nbu-highBit-1)) & mask;
201 // VOIR s'il ne faudrait pas l'affecter à un champ du dcmHeader
208 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
211 static void _Swap(void* im, int swap, int lgr, int nb) {
230 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
231 | ((((unsigned short int*)im)[i])<<8);
235 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
247 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
248 fort =((unsigned long int*)im)[i]>>16;
249 fort= (fort>>8) | (fort<<8);
250 faible=(faible>>8) | (faible<<8);
252 ((unsigned long int*)im)[i]=(s32<<16)|fort;
258 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
259 fort=((unsigned long int*)im)[i]>>16;
260 fort= (fort>>8) | (fort<<8);
261 faible=(faible>>8) | (faible<<8);
263 ((unsigned long int*)im)[i]=(s32<<16)|faible;
269 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
270 fort=((unsigned long int*)im)[i]>>16;
272 ((unsigned long int*)im)[i]=(s32<<16)|fort;
277 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
282 /////////////////////////////////////////////////////////////////
286 * \warning doit-etre etre publique ? FIXME JPR
288 * @param Data TODO JPR
289 * @param ExpectedSize TODO JPR
293 int gdcmFile::SetImageData(void * Data, size_t ExpectedSize) {
295 SetImageDataSize(ExpectedSize);
298 lgrTotale = ExpectedSize;
304 /////////////////////////////////////////////////////////////////
309 * \warning WARNING doit-etre etre publique ? FIXME JPR
311 * @param ImageDataSize TODO JPR
315 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
321 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
323 sprintf(car,"%d",ImageDataSize);
325 SetPubElValByNumber(content2, 0x7fe0, 0x0010);
328 sprintf(car,"%d",ImageDataSize);
330 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
334 /////////////////////////////////////////////////////////////////
337 * \brief Ecrit sur disque les pixels d'UNE image
338 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
339 * Ca sera à l'utilisateur d'appeler son Reader correctement
340 * (Equivalent a IdImaWriteRawFile) FIXME JPR
342 * @param nomFichier TODO JPR
347 int gdcmFile::WriteRawData (string nomFichier) {
350 fp1 = fopen(nomFichier.c_str(),"wb");
352 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
356 fwrite (Pixels,lgrTotale, 1, fp1);
363 /////////////////////////////////////////////////////////////////
366 * \brief Ecrit sur disque UNE image Dicom
367 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
368 * Ca fonctionnera correctement (?) sur processeur Intel
369 * (Equivalent a IdDcmWrite) FIXME JPR
371 * @param nomFichier TODO JPR
376 int gdcmFile::WriteDcm (string nomFichier) {
378 // ATTENTION : fonction non terminée (commitée a titre de precaution)
382 fp1 = fopen(nomFichier.c_str(),"wb");
384 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
388 // Ecriture Dicom File Preamble
389 filePreamble=(char*)calloc(128,1);
390 fwrite(filePreamble,128,1,fp1);
391 fwrite("DICM",4,1,fp1);
393 // un accesseur de + est obligatoire ???
394 // pourtant le gdcmElValSet contenu dans le gdcmHeader
395 // ne devrait pas être visible par l'utilisateur final (?)
397 GetPubElValSet().Write(fp1);
399 fwrite(Pixels, lgrTotale, 1, fp1);
405 int gdcmFile::WriteDcm (const char* nomFichier) {
406 WriteDcm (string (nomFichier));
410 /////////////////////////////////////////////////////////////////
414 * @param nomFichier TODO JPR
419 int gdcmFile::WriteDcmExplVR (string nomFichier) {
421 // ATTENTION : fonction non terminée (commitée a titre de precaution)
425 fp1 = fopen(nomFichier.c_str(),"wb");
427 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
431 // Ecriture Dicom File Preamble
432 filePreamble=(char*)calloc(128,1);
433 fwrite(filePreamble,128,1,fp1);
434 fwrite("DICM",4,1,fp1);
436 // un accesseur de + est obligatoire ???
437 // pourtant le gdcmElValSet contenu dans le gdcmHeader
438 // ne devrait pas être visible par l'utilisateur final (?)
440 GetPubElValSet().WriteExplVR(fp1);
442 fwrite(Pixels, lgrTotale, 1, fp1);
448 /////////////////////////////////////////////////////////////////
451 * \brief Ecrit sur disque UNE image ACR-NEMA
452 * (a l'attention des logiciels cliniques
453 * qui ne prennent en entrée QUE des images ACR ...
454 * si un header DICOM est fourni en entree,
455 * les groupes < 0x0008 et les groupes impairs sont ignores)
456 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
457 * Ca fonctionnera correctement (?) sur processeur Intel
458 * (Equivalent a IdDcmWrite)
460 * @param nomFichier TODO JPR
465 int gdcmFile::WriteAcr (string nomFichier) {
467 // ATTENTION : fonction non terminée (commitée a titre de precaution)
470 fp1 = fopen(nomFichier.c_str(),"wb");
472 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
476 // un accesseur de + est obligatoire ???
477 // pourtant le gdcmElValSet contenu dans le gdcmHeader
478 // ne devrait pas être visible par l'utilisateur final (?)
480 GetPubElValSet().WriteAcr(fp1);
482 fwrite(Pixels, lgrTotale, 1, fp1);