7 static void _Swap(void* im, int swap, int lgr, int nb);
9 /////////////////////////////////////////////////////////////////
12 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
13 * \file (see SetFileName, SetDcmTag and Write)
14 * \Opens (in read only and when possible) an existing file and checks
15 * \for DICOM compliance. Returns NULL on failure.
16 * \Note: the in-memory representation of all available tags found in
17 * \the DICOM header is post-poned to first header information access.
18 * \This avoid a double parsing of public part of the header when
19 * \one sets an a posteriori shadow dictionary (efficiency can be
20 * \seen as a side effect).
27 gdcmFile::gdcmFile(string & filename)
28 :gdcmHeader(filename.c_str())
30 if (DEBUG) printf("On a echappe a gdcmHeader !\n");
34 /////////////////////////////////////////////////////////////////
37 * \brief Renvoie la longueur A ALLOUER pour recevoir les pixels de l'image
38 * \ ou DES images dans le cas d'un multiframe
39 * \ ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
40 * \ (dans le cas d'images compressees, elle n'a pas de sens).
42 * @param void Rien en entree
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 == "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 == "UNFOUND" ) {
70 nb = atoi(str_nb.c_str() );
73 size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
78 /////////////////////////////////////////////////////////////////
81 * \brief amene en mémoire les Pixels d'une image NON COMPRESSEE
82 * \Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
86 * @return Pointeur sur la zone mémoire contenant les Pixels lus
89 void * gdcmFile::GetImageData (void) {
94 int nbFrames, nb, nbu, highBit, signe;
95 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
97 unsigned short int mask = 0xffff;
100 nbLignes=atoi(GetPubElValByNumber(0x0028,0x0010).c_str());
101 // Nombre de Colonnes
102 nbCol =atoi(GetPubElValByNumber(0x0028,0x0011).c_str());
105 str_nbFrames=GetPubElValByNumber(0x0028,0x0008);
108 if (str_nbFrames == "UNFOUND" ) {
111 nbFrames = atoi(str_nbFrames.c_str() );
114 // Nombre de Bits Alloues
115 str_nb=GetPubElValByNumber(0x0028,0x0100);
117 if (str_nb == "UNFOUND" ) {
120 nb = atoi(str_nb.c_str() );
123 // Nombre de Bits Utilises
124 str_nbu=GetPubElValByNumber(0x0028,0x0101);
126 if (str_nbu == "UNFOUND" ) {
129 nbu = atoi(str_nbu.c_str() );
132 // Position du Bit de Poids Fort
133 str_highBit=GetPubElValByNumber(0x0028,0x0102);
135 if (str_highBit == "UNFOUND" ) {
138 highBit = atoi(str_highBit.c_str() );
141 // Signe des Pixels 0 : Unsigned
142 str_signe=GetPubElValByNumber(0x0028,0x0103);
144 if (str_signe == "UNFOUND" ) {
147 signe = atoi(str_signe.c_str() );
150 // Longueur en Octets des Pixels a lire
151 size_t _lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
153 //Pixels = (char *) g_malloc(_lgrTotale);
154 _Pixels = (char *) malloc(_lgrTotale);
156 GetPixels(lgrTotale, _Pixels);
158 // On remet les Octets dans le bon ordre si besoin est
160 int _sw = GetSwapCode();
162 _Swap (_Pixels, _sw, _lgrTotale, nb);
165 // On remet les Bits des Octets dans le bon ordre si besoin est
167 // ATTENTION : Jamais confronté a des pixels stockes sur 32 bits
168 // avec moins de 32 bits utilises
169 // et dont le bit de poids fort ne serait pas la ou on l'attend ...
170 // --> ne marchera pas dans ce cas
172 mask = mask >> (nb-nbu);
173 int l=(int)_lgrTotale/(nb/8);
174 unsigned short *deb = (unsigned short *)_Pixels;
175 for(int i=0;i<l;i++) {
176 *deb = (*deb >> (nbu-highBit-1)) & mask;
181 printf ("on est sorti\n");
183 // On l'affecte à un champ du dcmFile
186 lgrTotale = _lgrTotale;
192 /////////////////////////////////////////////////////////////////
195 * \brief amene en mémoire dans une zone précisee par l'utilisateur
196 * \les Pixels d'une image NON COMPRESSEE
197 * \Aucun test n'est fait pour le moment sur le caractere compresse ou non de l'image
204 int gdcmFile::PutImageDataHere (void* destination, size_t MaxSize) {
206 void * Pixels = destination; // pour garder le code identique avec GetImageData
209 int nbFrames, nb, nbu, highBit, signe;
210 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
212 unsigned short int mask = 0xffff;
215 nbLignes=atoi(GetPubElValByNumber(0x0028,0x0010).c_str());
216 // Nombre de Colonnes
217 nbCol =atoi(GetPubElValByNumber(0x0028,0x0011).c_str());
220 str_nbFrames=GetPubElValByNumber(0x0028,0x0008);
223 if (str_nbFrames == "UNFOUND" ) {
226 nbFrames = atoi(str_nbFrames.c_str() );
229 // Nombre de Bits Alloues
230 str_nb=GetPubElValByNumber(0x0028,0x0100);
232 if (str_nb == "UNFOUND" ) {
235 nb = atoi(str_nb.c_str() );
238 // Nombre de Bits Utilises
239 str_nbu=GetPubElValByNumber(0x0028,0x0101);
241 if (str_nbu == "UNFOUND" ) {
244 nbu = atoi(str_nbu.c_str() );
247 // Position du Bit de Poids Fort
248 str_highBit=GetPubElValByNumber(0x0028,0x0102);
250 if (str_highBit == "UNFOUND" ) {
253 highBit = atoi(str_highBit.c_str() );
257 str_signe=GetPubElValByNumber(0x0028,0x0103);
259 if (str_signe == "UNFOUND" ) {
262 signe = atoi(str_signe.c_str() );
265 // Longueur en Octets des Pixels a lire
266 size_t lgrTotale = nbFrames*nbLignes*nbCol*(nb/8);
268 // si lgrTotale < MaxSize ==> Gros pb . A VOIR
270 lgrTotale = MaxSize; // pour garder le code identique avec GetImageData
271 //Pixels = (char *) malloc(lgrTotale); // pour garder le code identique avec GetImageData
273 GetPixels(lgrTotale, Pixels);
275 // On remet les Octets dans le bon ordre si besoin est
277 int _sw = GetSwapCode();
279 _Swap (Pixels, _sw, lgrTotale, nb);
282 // On remet les Bits des Octets dans le bon ordre si besoin est
284 // ATTENTION : Jamais confronté a des pixels stockes sur 32 bits
285 // avec moins de 32 bits utilises
286 // et dont le bit de poids fort ne serait pas la ou on l'attend ...
287 // --> ne marchera pas dans ce cas
289 mask = mask >> (nb-nbu);
290 int l=(int)lgrTotale/(nb/8);
291 unsigned short *deb = (unsigned short *)Pixels;
292 for(int i=0;i<l;i++) {
293 *deb = (*deb >> (nbu-highBit-1)) & mask;
298 printf ("on est sorti\n");
300 // VOIR s'il ne faudrait pas l'affecter à un champ du dcmHeader
302 //return (Pixels); // pour garder le code identique avec GetImageData
307 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
310 static void _Swap(void* im, int swap, int lgr, int nb) {
329 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
330 | ((((unsigned short int*)im)[i])<<8);
334 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
346 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
347 fort =((unsigned long int*)im)[i]>>16;
348 fort= (fort>>8) | (fort<<8);
349 faible=(faible>>8) | (faible<<8);
351 ((unsigned long int*)im)[i]=(s32<<16)|fort;
357 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
358 fort=((unsigned long int*)im)[i]>>16;
359 fort= (fort>>8) | (fort<<8);
360 faible=(faible>>8) | (faible<<8);
362 ((unsigned long int*)im)[i]=(s32<<16)|faible;
368 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
369 fort=((unsigned long int*)im)[i]>>16;
371 ((unsigned long int*)im)[i]=(s32<<16)|fort;
376 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
384 /////////////////////////////////////////////////////////////////
387 * \brief Ecrit sur disque les pixels d'UNE image
388 * \Aucun test n'est fait sur l'"Endiannerie" du processeur.
389 * \ C'est à l'utilisateur d'appeler son Reader correctement
390 * \(Equivalent a IdImaWriteRawFile)
397 int gdcmFile::WriteRawData (string nomFichier) {
400 fp1 = fopen(nomFichier.c_str(),"wb");
402 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
406 fwrite (Pixels,lgrTotale, 1, fp1);