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())
28 SetPixelDataSizeFromHeader();
31 gdcmFile::gdcmFile(const char * filename)
34 SetPixelDataSizeFromHeader();
38 /////////////////////////////////////////////////////////////////
41 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
43 * ou DES images dans le cas d'un multiframe
44 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
45 * (dans le cas d'images compressees, elle n'a pas de sens).
47 * @return longueur a allouer
50 void gdcmFile::SetPixelDataSizeFromHeader(void) {
54 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
55 if (str_nb == "gdcm::Unfound" ) {
58 nb = atoi(str_nb.c_str() );
61 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8);
68 size_t gdcmFile::GetImageDataSize(void) {
75 * \brief Read pixel data from disk (optionaly decompressing) into the
76 * caller specified memory location.
77 * @param destination Where the pixel data should be stored.
80 bool gdcmFile::ReadPixelData(void* destination) {
84 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
90 IsImplicitVRLittleEndianTransferSyntax() ||
91 IsExplicitVRLittleEndianTransferSyntax() ||
92 IsExplicitVRBigEndianTransferSyntax() ||
93 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
95 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
96 if ( ItemRead != 1 ) {
105 if (IsJPEGLossless()) {
107 fseek(fp,4,SEEK_CUR);
112 printf ("ln %d\n",ln);
113 fseek(fp,ln,SEEK_CUR);
114 fseek(fp,4,SEEK_CUR);
119 printf ("ln image comprimée %d\n",ln);
121 ClbJpeg* jpg = _IdDcmJpegRead(fp);
126 memcpy(destination,jpg->DataImg,lgrTotale);
127 _IdDcmJpegFree (jpg);
132 printf ("Sorry, TransfertSyntax not yet taken into account ...\n");
138 /////////////////////////////////////////////////////////////////
141 * \brief Allocates necessary memory, copies the pixel data
142 * (image[s]/volume[s]) to newly allocated zone and return a
145 void * gdcmFile::GetImageData (void) {
146 PixelData = (void *) malloc(lgrTotale);
147 GetImageDataIntoVector(PixelData, lgrTotale);
151 /////////////////////////////////////////////////////////////////
154 * \brief amene en mémoire dans une zone précisee par l'utilisateur
155 * les Pixels d'une image
160 * @return The number of bytes actually copied.
163 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
165 int nb, nbu, highBit, signe;
166 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
168 if ( lgrTotale > MaxSize ) {
169 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
170 "than caller's expected MaxSize");
174 (void)ReadPixelData(destination);
176 // Nombre de Bits Alloues pour le stockage d'un Pixel
177 str_nb = GetPubElValByNumber(0x0028,0x0100);
178 if (str_nb == "gdcm::Unfound" ) {
181 nb = atoi(str_nb.c_str() );
184 // Nombre de Bits Utilises
185 str_nbu=GetPubElValByNumber(0x0028,0x0101);
186 if (str_nbu == "gdcm::Unfound" ) {
189 nbu = atoi(str_nbu.c_str() );
192 // Position du Bit de Poids Fort
193 str_highBit=GetPubElValByNumber(0x0028,0x0102);
194 if (str_highBit == "gdcm::Unfound" ) {
197 highBit = atoi(str_highBit.c_str() );
201 str_signe=GetPubElValByNumber(0x0028,0x0103);
202 if (str_signe == "gdcm::Unfound" ) {
205 signe = atoi(str_signe.c_str() );
208 // On remet les Octets dans le bon ordre si besoin est
210 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
212 // On remet les Bits des Octets dans le bon ordre si besoin est
214 int l = (int)lgrTotale / (nb/8);
216 guint16 mask = 0xffff;
217 mask = mask >> (nb-nbu);
218 guint16 *deb = (guint16 *)destination;
219 for(int i = 0; i<l; i++) {
220 *deb = (*deb >> (nbu-highBit-1)) & mask;
223 } else if (nb == 32 ) {
224 guint32 mask = 0xffffffff;
225 mask = mask >> (nb-nbu);
226 guint32 *deb = (guint32 *)destination;
227 for(int i = 0; i<l; i++) {
228 *deb = (*deb >> (nbu-highBit-1)) & mask;
232 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
241 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
244 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
263 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
264 | ((((unsigned short int*)im)[i])<<8);
268 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
280 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
281 fort =((unsigned long int*)im)[i]>>16;
282 fort= (fort>>8) | (fort<<8);
283 faible=(faible>>8) | (faible<<8);
285 ((unsigned long int*)im)[i]=(s32<<16)|fort;
291 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
292 fort=((unsigned long int*)im)[i]>>16;
293 fort= (fort>>8) | (fort<<8);
294 faible=(faible>>8) | (faible<<8);
296 ((unsigned long int*)im)[i]=(s32<<16)|faible;
302 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
303 fort=((unsigned long int*)im)[i]>>16;
305 ((unsigned long int*)im)[i]=(s32<<16)|fort;
310 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
315 /////////////////////////////////////////////////////////////////
319 * \warning doit-etre etre publique ? FIXME JPR
320 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
322 * @param inData TODO JPR
323 * @param ExpectedSize TODO JPR
327 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
328 SetImageDataSize(ExpectedSize);
330 lgrTotale = ExpectedSize;
337 /////////////////////////////////////////////////////////////////
342 * \warning WARNING doit-etre etre publique ? FIXME JPR
343 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
345 * @param ImageDataSize TODO JPR
349 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
354 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
356 sprintf(car,"%d",ImageDataSize);
358 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
359 a->SetLength(ImageDataSize);
362 sprintf(car,"%d",ImageDataSize);
364 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
368 /////////////////////////////////////////////////////////////////
371 * \brief Ecrit sur disque les pixels d'UNE image
372 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
373 * Ca sera à l'utilisateur d'appeler son Reader correctement
374 * (Equivalent a IdImaWriteRawFile) FIXME JPR
376 * @param nomFichier TODO JPR
381 int gdcmFile::WriteRawData (string nomFichier) {
384 fp1 = fopen(nomFichier.c_str(),"wb");
386 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
390 fwrite (PixelData,lgrTotale, 1, fp1);
397 /////////////////////////////////////////////////////////////////
400 * \brief Ecrit sur disque UNE image Dicom
401 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
402 * Ca fonctionnera correctement (?) sur processeur Intel
403 * (Equivalent a IdDcmWrite) FIXME JPR
405 * @param nomFichier TODO JPR
410 int gdcmFile::WriteDcmImplVR (string nomFichier) {
411 return WriteBase(nomFichier, ImplicitVR);
414 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
415 return WriteDcmImplVR (string (nomFichier));
418 /////////////////////////////////////////////////////////////////
422 * @param nomFichier TODO JPR
427 int gdcmFile::WriteDcmExplVR (string nomFichier) {
428 return WriteBase(nomFichier, ExplicitVR);
431 /////////////////////////////////////////////////////////////////
434 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
435 * (a l'attention des logiciels cliniques
436 * qui ne prennent en entrée QUE des images ACR ...
437 * \warning si un header DICOM est fourni en entree,
438 * les groupes < 0x0008 et les groupes impairs sont ignores)
439 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
440 * Ca fonctionnera correctement (?) sur processeur Intel
441 * (Equivalent a IdDcmWrite)
443 * @param nomFichier TODO JPR
448 int gdcmFile::WriteAcr (string nomFichier) {
449 return WriteBase(nomFichier, ACR);
452 int gdcmFile::WriteBase (string nomFichier, FileType type) {
455 fp1 = fopen(nomFichier.c_str(),"wb");
457 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
461 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
463 // Ecriture Dicom File Preamble
464 filePreamble=(char*)calloc(128,1);
465 fwrite(filePreamble,128,1,fp1);
466 fwrite("DICM",4,1,fp1);
469 gdcmHeader::Write(fp1, type);
470 fwrite(PixelData, lgrTotale, 1, fp1);