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
321 * @param inData TODO JPR
322 * @param ExpectedSize TODO JPR
326 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
327 SetImageDataSize(ExpectedSize);
329 lgrTotale = ExpectedSize;
334 /////////////////////////////////////////////////////////////////
339 * \warning WARNING doit-etre etre publique ? FIXME JPR
341 * @param ImageDataSize TODO JPR
345 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
351 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
353 sprintf(car,"%d",ImageDataSize);
355 SetPubElValByNumber(content2, 0x7fe0, 0x0010);
358 sprintf(car,"%d",ImageDataSize);
360 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
364 /////////////////////////////////////////////////////////////////
367 * \brief Ecrit sur disque les pixels d'UNE image
368 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
369 * Ca sera à l'utilisateur d'appeler son Reader correctement
370 * (Equivalent a IdImaWriteRawFile) FIXME JPR
372 * @param nomFichier TODO JPR
377 int gdcmFile::WriteRawData (string nomFichier) {
380 fp1 = fopen(nomFichier.c_str(),"wb");
382 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
386 fwrite (PixelData,lgrTotale, 1, fp1);
393 /////////////////////////////////////////////////////////////////
396 * \brief Ecrit sur disque UNE image Dicom
397 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
398 * Ca fonctionnera correctement (?) sur processeur Intel
399 * (Equivalent a IdDcmWrite) FIXME JPR
401 * @param nomFichier TODO JPR
406 int gdcmFile::WriteDcmImplVR (string nomFichier) {
407 return WriteBase(nomFichier, ImplicitVR);
410 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
411 return WriteDcmImplVR (string (nomFichier));
414 /////////////////////////////////////////////////////////////////
418 * @param nomFichier TODO JPR
423 int gdcmFile::WriteDcmExplVR (string nomFichier) {
424 return WriteBase(nomFichier, ExplicitVR);
427 /////////////////////////////////////////////////////////////////
430 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
431 * (a l'attention des logiciels cliniques
432 * qui ne prennent en entrée QUE des images ACR ...
433 * \warning si un header DICOM est fourni en entree,
434 * les groupes < 0x0008 et les groupes impairs sont ignores)
435 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
436 * Ca fonctionnera correctement (?) sur processeur Intel
437 * (Equivalent a IdDcmWrite)
439 * @param nomFichier TODO JPR
444 int gdcmFile::WriteAcr (string nomFichier) {
445 return WriteBase(nomFichier, ACR);
448 int gdcmFile::WriteBase (string nomFichier, FileType type) {
451 fp1 = fopen(nomFichier.c_str(),"wb");
453 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
457 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
459 // Ecriture Dicom File Preamble
460 filePreamble=(char*)calloc(128,1);
461 fwrite(filePreamble,128,1,fp1);
462 fwrite("DICM",4,1,fp1);
465 gdcmHeader::Write(fp1, type);
466 fwrite(PixelData, lgrTotale, 1, fp1);