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())
31 /////////////////////////////////////////////////////////////////
32 // FIXME JPR: comments in English please !
35 * \brief Renvoie la longueur A ALLOUER pour recevoir les pixels de l'image
36 * ou DES images dans le cas d'un multiframe
37 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
38 * (dans le cas d'images compressees, elle n'a pas de sens).
40 * @return longueur a allouer
43 size_t gdcmFile::GetImageDataSize(void) {
44 int nbLignes, nbCol, nbFrames, nb;
45 string str_nbFrames, str_nb;
47 nbLignes=atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0010).c_str());
49 nbCol =atoi(gdcmHeader::GetPubElValByNumber(0x0028,0x0011).c_str());
52 str_nbFrames=gdcmHeader::GetPubElValByNumber(0x0028,0x0008);
54 if (str_nbFrames == "gdcm::Unfound" ) {
57 nbFrames = atoi(str_nbFrames.c_str() );
60 // Nombre de Bits Alloues pour le stockage d'un Pixel
61 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
63 if (str_nb == "gdcm::Unfound" ) {
66 nb = atoi(str_nb.c_str() );
69 size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
75 /////////////////////////////////////////////////////////////////
82 void * gdcmFile::GetImageData (void) {
84 // Longueur en Octets des Pixels a lire
85 size_t taille = GetImageDataSize();// ne faudrait-il pas la stocker?
86 _Pixels = (char *) malloc(taille);
87 GetImageDataIntoVector(_Pixels, taille);
89 // On l'affecte à un champ du dcmFile
93 // ca fait double emploi, il faudra nettoyer ça
100 /////////////////////////////////////////////////////////////////
103 * \brief amene en mémoire dans une zone précisee par l'utilisateur
104 * les Pixels d'une image NON COMPRESSEE
105 * \Warning Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
113 int gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
116 // dans quel cas la MaxSize sert-elle a quelque chose?
117 // que fait-on si la taille de l'image est + gde que Maxize?
118 // que fait-on si la taille de l'image est + petite que Maxize?
121 void * Pixels = destination; // pour garder le code identique avec GetImageData
123 int nb, nbu, highBit, signe;
124 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
126 unsigned short int mask = 0xffff;
128 // Longueur en Octets des Pixels a lire
129 size_t _lgrTotale = GetImageDataSize(); // ne faudrait-il pas la stocker?
131 // si lgrTotale < MaxSize ==> Gros pb
132 // -> on résoud à la goret
134 if ( _lgrTotale < MaxSize ) MaxSize = _lgrTotale;
136 GetPixels(MaxSize, destination);
138 // Nombre de Bits Alloues pour le stockage d'un Pixel
139 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
141 if (str_nb == "gdcm::Unfound" ) {
144 nb = atoi(str_nb.c_str() );
147 // Nombre de Bits Utilises
148 str_nbu=GetPubElValByNumber(0x0028,0x0101);
150 if (str_nbu == "gdcm::Unfound" ) {
153 nbu = atoi(str_nbu.c_str() );
156 // Position du Bit de Poids Fort
157 str_highBit=GetPubElValByNumber(0x0028,0x0102);
159 if (str_highBit == "gdcm::Unfound" ) {
162 highBit = atoi(str_highBit.c_str() );
166 str_signe=GetPubElValByNumber(0x0028,0x0103);
168 if (str_signe == "gdcm::Unfound" ) {
171 signe = atoi(str_signe.c_str() );
174 // On remet les Octets dans le bon ordre si besoin est
176 int _sw = GetSwapCode();
178 _Swap (destination, _sw, _lgrTotale, nb);
181 // On remet les Bits des Octets dans le bon ordre si besoin est
183 // ATTENTION : Jamais confronté a des pixels stockes sur 32 bits
184 // avec moins de 32 bits utilises
185 // et dont le bit de poids fort ne serait pas la ou on l'attend ...
186 // --> ne marchera pas dans ce cas
188 mask = mask >> (nb-nbu);
189 int l=(int)MaxSize/(nb/8);
190 unsigned short *deb = (unsigned short *)Pixels;
191 for(int i=0;i<l;i++) {
192 *deb = (*deb >> (nbu-highBit-1)) & mask;
197 // VOIR s'il ne faudrait pas l'affecter à un champ du dcmHeader
204 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
207 static void _Swap(void* im, int swap, int lgr, int nb) {
226 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
227 | ((((unsigned short int*)im)[i])<<8);
231 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
243 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
244 fort =((unsigned long int*)im)[i]>>16;
245 fort= (fort>>8) | (fort<<8);
246 faible=(faible>>8) | (faible<<8);
248 ((unsigned long int*)im)[i]=(s32<<16)|fort;
254 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
255 fort=((unsigned long int*)im)[i]>>16;
256 fort= (fort>>8) | (fort<<8);
257 faible=(faible>>8) | (faible<<8);
259 ((unsigned long int*)im)[i]=(s32<<16)|faible;
265 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
266 fort=((unsigned long int*)im)[i]>>16;
268 ((unsigned long int*)im)[i]=(s32<<16)|fort;
273 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
278 /////////////////////////////////////////////////////////////////
282 * \warning doit-etre etre publique ? FIXME JPR
284 * @param Data TODO JPR
285 * @param ExpectedSize TODO JPR
289 int gdcmFile::SetImageData(void * Data, size_t ExpectedSize) {
291 SetImageDataSize(ExpectedSize);
294 lgrTotale = ExpectedSize;
300 /////////////////////////////////////////////////////////////////
305 * \warning WARNING doit-etre etre publique ? FIXME JPR
307 * @param ImageDataSize TODO JPR
311 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
317 sprintf(car,"%d",ImageDataSize);
319 SetPubElValByNumber(content2, 0x7fe0, 0x0010);
322 sprintf(car,"%d",ImageDataSize);
324 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
328 /////////////////////////////////////////////////////////////////
331 * \brief Ecrit sur disque les pixels d'UNE image
332 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
333 * Ca sera à l'utilisateur d'appeler son Reader correctement
334 * (Equivalent a IdImaWriteRawFile) FIXME JPR
336 * @param nomFichier TODO JPR
341 int gdcmFile::WriteRawData (string nomFichier) {
344 fp1 = fopen(nomFichier.c_str(),"wb");
346 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
350 fwrite (Pixels,lgrTotale, 1, fp1);
357 /////////////////////////////////////////////////////////////////
360 * \brief Ecrit sur disque UNE image Dicom
361 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
362 * Ca fonctionnera correctement (?) sur processeur Intel
363 * (Equivalent a IdDcmWrite) FIXME JPR
365 * @param nomFichier TODO JPR
370 int gdcmFile::WriteDcm (string nomFichier) {
372 // ATTENTION : fonction non terminée (commitée a titre de precaution)
376 fp1 = fopen(nomFichier.c_str(),"wb");
378 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
382 // Ecriture Dicom File Preamble
383 filePreamble=(char*)calloc(128,1);
384 fwrite(filePreamble,128,1,fp1);
385 fwrite("DICM",4,1,fp1);
387 // un accesseur de + est obligatoire ???
388 // pourtant le gdcmElValSet contenu dans le gdcmHeader
389 // ne devrait pas être visible par l'utilisateur final (?)
391 GetPubElVals().Write(fp1);
393 fwrite(Pixels, lgrTotale, 1, fp1);
399 /////////////////////////////////////////////////////////////////
402 * \brief Ecrit sur disque UNE image ACR-NEMA
403 * (a l'attention des logiciels cliniques
404 * qui ne prennent en entrée QUE des images ACR ...
405 * si un header DICOM est fourni en entree,
406 * les groupes < 0x0008 et les groupes impairs sont ignores)
407 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
408 * Ca fonctionnera correctement (?) sur processeur Intel
409 * (Equivalent a IdDcmWrite)
411 * @param nomFichier TODO JPR
416 int gdcmFile::WriteAcr (string nomFichier) {
418 // ATTENTION : fonction non terminée (commitée a titre de precaution)
421 fp1 = fopen(nomFichier.c_str(),"wb");
423 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
427 // un accesseur de + est obligatoire ???
428 // pourtant le gdcmElValSet contenu dans le gdcmHeader
429 // ne devrait pas être visible par l'utilisateur final (?)
431 GetPubElVals().WriteAcr(fp1);
433 fwrite(Pixels, lgrTotale, 1, fp1);