8 /////////////////////////////////////////////////////////////////
11 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
12 * file (see SetFileName, SetDcmTag and Write)
13 * Opens (in read only and when possible) an existing file and checks
14 * for DICOM compliance. Returns NULL on failure.
15 * \Note the in-memory representation of all available tags found in
16 * the DICOM header is post-poned to first header information access.
17 * This avoid a double parsing of public part of the header when
18 * one sets an a posteriori shadow dictionary (efficiency can be
19 * seen as a side effect).
21 * @param filename file to be opened for parsing
26 gdcmFile::gdcmFile(string & filename)
27 :gdcmHeader(filename.c_str())
29 SetPixelDataSizeFromHeader();
32 gdcmFile::gdcmFile(const char * filename)
35 SetPixelDataSizeFromHeader();
40 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
42 * ou DES images dans le cas d'un multiframe
43 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
44 * (dans le cas d'images compressees, elle n'a pas de sens).
46 * @return longueur a allouer
48 void gdcmFile::SetPixelDataSizeFromHeader(void) {
52 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
53 if (str_nb == "gdcm::Unfound" ) {
56 nb = atoi(str_nb.c_str() );
59 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8);
64 * \brief Returns the size (in bytes) of required memory to hold
65 * the pixel data represented in this file.
66 * @return The size of pixel data in bytes.
68 size_t gdcmFile::GetImageDataSize(void) {
74 * \brief Read pixel data from disk (optionaly decompressing) into the
75 * caller specified memory location.
76 * @param destination Where the pixel data should be stored.
79 bool gdcmFile::ReadPixelData(void* destination) {
83 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
89 IsImplicitVRLittleEndianTransferSyntax() ||
90 IsExplicitVRLittleEndianTransferSyntax() ||
91 IsExplicitVRBigEndianTransferSyntax() ||
92 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
94 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
95 if ( ItemRead != 1 ) {
104 if (IsJPEGLossless()) {
106 fseek(fp,4,SEEK_CUR);
111 printf ("ln %d\n",ln);
112 fseek(fp,ln,SEEK_CUR);
113 fseek(fp,4,SEEK_CUR);
118 printf ("ln image comprimée %d\n",ln);
120 ClbJpeg* jpg = _IdDcmJpegRead(fp);
125 memcpy(destination,jpg->DataImg,lgrTotale);
126 _IdDcmJpegFree (jpg);
131 printf ("Sorry, TransfertSyntax not yet taken into account ...\n");
139 * \brief Allocates necessary memory, copies the pixel data
140 * (image[s]/volume[s]) to newly allocated zone.
141 * @return Pointer to newly allocated pixel data.
143 void * gdcmFile::GetImageData (void) {
144 PixelData = (void *) malloc(lgrTotale);
145 GetImageDataIntoVector(PixelData, lgrTotale);
151 * \brief Copies at most MaxSize bytes of pixel data to caller's
153 * @param destination Address (in caller's memory space) at which the
154 * pixel data should be copied
155 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
156 * is not sufficient to hold the pixel data the copy is not
157 * executed (i.e. no partial copy).
158 * @return On success, the number of bytes actually copied. Zero on
159 * failure e.g. MaxSize is lower than necessary.
162 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
164 int nb, nbu, highBit, signe;
165 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
167 if ( lgrTotale > MaxSize ) {
168 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
169 "than caller's expected MaxSize");
173 (void)ReadPixelData(destination);
175 // Nombre de Bits Alloues pour le stockage d'un Pixel
176 str_nb = GetPubElValByNumber(0x0028,0x0100);
177 if (str_nb == "gdcm::Unfound" ) {
180 nb = atoi(str_nb.c_str() );
183 // Nombre de Bits Utilises
184 str_nbu=GetPubElValByNumber(0x0028,0x0101);
185 if (str_nbu == "gdcm::Unfound" ) {
188 nbu = atoi(str_nbu.c_str() );
191 // Position du Bit de Poids Fort
192 str_highBit=GetPubElValByNumber(0x0028,0x0102);
193 if (str_highBit == "gdcm::Unfound" ) {
196 highBit = atoi(str_highBit.c_str() );
200 str_signe=GetPubElValByNumber(0x0028,0x0103);
201 if (str_signe == "gdcm::Unfound" ) {
204 signe = atoi(str_signe.c_str() );
207 // On remet les Octets dans le bon ordre si besoin est
209 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
211 // On remet les Bits des Octets dans le bon ordre si besoin est
213 int l = (int)lgrTotale / (nb/8);
215 guint16 mask = 0xffff;
216 mask = mask >> (nb-nbu);
217 guint16 *deb = (guint16 *)destination;
218 for(int i = 0; i<l; i++) {
219 *deb = (*deb >> (nbu-highBit-1)) & mask;
222 } else if (nb == 32 ) {
223 guint32 mask = 0xffffffff;
224 mask = mask >> (nb-nbu);
225 guint32 *deb = (guint32 *)destination;
226 for(int i = 0; i<l; i++) {
227 *deb = (*deb >> (nbu-highBit-1)) & mask;
231 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
240 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
243 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
262 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
263 | ((((unsigned short int*)im)[i])<<8);
267 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
279 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
280 fort =((unsigned long int*)im)[i]>>16;
281 fort= (fort>>8) | (fort<<8);
282 faible=(faible>>8) | (faible<<8);
284 ((unsigned long int*)im)[i]=(s32<<16)|fort;
290 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
291 fort=((unsigned long int*)im)[i]>>16;
292 fort= (fort>>8) | (fort<<8);
293 faible=(faible>>8) | (faible<<8);
295 ((unsigned long int*)im)[i]=(s32<<16)|faible;
301 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
302 fort=((unsigned long int*)im)[i]>>16;
304 ((unsigned long int*)im)[i]=(s32<<16)|fort;
309 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
314 /////////////////////////////////////////////////////////////////
318 * \warning doit-etre etre publique ? FIXME JPR
319 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
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;
336 /////////////////////////////////////////////////////////////////
341 * \warning WARNING doit-etre etre publique ? FIXME JPR
342 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
344 * @param ImageDataSize TODO JPR
348 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
353 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
355 sprintf(car,"%d",ImageDataSize);
357 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
358 a->SetLength(ImageDataSize);
361 sprintf(car,"%d",ImageDataSize);
363 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
367 /////////////////////////////////////////////////////////////////
370 * \brief Ecrit sur disque les pixels d'UNE image
371 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
372 * Ca sera à l'utilisateur d'appeler son Reader correctement
373 * (Equivalent a IdImaWriteRawFile) FIXME JPR
375 * @param nomFichier TODO JPR
380 int gdcmFile::WriteRawData (string nomFichier) {
383 fp1 = fopen(nomFichier.c_str(),"wb");
385 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
389 fwrite (PixelData,lgrTotale, 1, fp1);
396 /////////////////////////////////////////////////////////////////
399 * \brief Ecrit sur disque UNE image Dicom
400 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
401 * Ca fonctionnera correctement (?) sur processeur Intel
402 * (Equivalent a IdDcmWrite) FIXME JPR
404 * @param nomFichier TODO JPR
409 int gdcmFile::WriteDcmImplVR (string nomFichier) {
410 return WriteBase(nomFichier, ImplicitVR);
413 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
414 return WriteDcmImplVR (string (nomFichier));
417 /////////////////////////////////////////////////////////////////
421 * @param nomFichier TODO JPR
426 int gdcmFile::WriteDcmExplVR (string nomFichier) {
427 return WriteBase(nomFichier, ExplicitVR);
430 /////////////////////////////////////////////////////////////////
433 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
434 * (a l'attention des logiciels cliniques
435 * qui ne prennent en entrée QUE des images ACR ...
436 * \warning si un header DICOM est fourni en entree,
437 * les groupes < 0x0008 et les groupes impairs sont ignores)
438 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
439 * Ca fonctionnera correctement (?) sur processeur Intel
440 * (Equivalent a IdDcmWrite)
442 * @param nomFichier TODO JPR
447 int gdcmFile::WriteAcr (string nomFichier) {
448 return WriteBase(nomFichier, ACR);
451 int gdcmFile::WriteBase (string nomFichier, FileType type) {
454 fp1 = fopen(nomFichier.c_str(),"wb");
456 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
460 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
462 // Ecriture Dicom File Preamble
463 filePreamble=(char*)calloc(128,1);
464 fwrite(filePreamble,128,1,fp1);
465 fwrite("DICM",4,1,fp1);
468 gdcmHeader::Write(fp1, type);
469 fwrite(PixelData, lgrTotale, 1, fp1);