11 #include "iddcmjpeg.h"
14 /////////////////////////////////////////////////////////////////
17 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
18 * file (see SetFileName, SetDcmTag and Write)
19 * Opens (in read only and when possible) an existing file and checks
20 * for DICOM compliance. Returns NULL on failure.
21 * \Note the in-memory representation of all available tags found in
22 * the DICOM header is post-poned to first header information access.
23 * This avoid a double parsing of public part of the header when
24 * one sets an a posteriori shadow dictionary (efficiency can be
25 * seen as a side effect).
27 * @param filename file to be opened for parsing
32 gdcmFile::gdcmFile(string & filename)
33 :gdcmHeader(filename.c_str())
35 SetPixelDataSizeFromHeader();
38 gdcmFile::gdcmFile(const char * filename)
41 SetPixelDataSizeFromHeader();
46 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
48 * ou DES images dans le cas d'un multiframe
49 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
50 * (dans le cas d'images compressees, elle n'a pas de sens).
52 * @return longueur a allouer
54 void gdcmFile::SetPixelDataSizeFromHeader(void) {
58 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
59 if (str_nb == "gdcm::Unfound" ) {
62 nb = atoi(str_nb.c_str() );
65 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8)* GetSamplesPerPixel();;
68 /////////////////////////////////////////////////////////////////
71 * \brief Returns the size (in bytes) of required memory to hold
72 * the pixel data represented in this file.
73 * @return The size of pixel data in bytes.
75 size_t gdcmFile::GetImageDataSize(void) {
80 /////////////////////////////////////////////////////////////////
83 * \brief Read pixel data from disk (optionaly decompressing) into the
84 * caller specified memory location.
85 * @param destination Where the pixel data should be stored.
88 bool gdcmFile::ReadPixelData(void* destination) {
92 printf("GetPixelOffset() %d\n",GetPixelOffset() );
94 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
100 IsImplicitVRLittleEndianTransferSyntax() ||
101 IsExplicitVRLittleEndianTransferSyntax() ||
102 IsExplicitVRBigEndianTransferSyntax() ||
103 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
105 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
106 if ( ItemRead != 1 ) {
115 // ------------------------------- Position on begining of Jpeg Pixels
118 fseek(fp,4,SEEK_CUR);
123 printf ("ln %d\n",ln);
124 fseek(fp,ln,SEEK_CUR);
125 fseek(fp,4,SEEK_CUR);
130 printf ("ln image comprimée %d\n",ln);
133 // ------------------------------- JPEG LossLess : call to Jpeg Libido
135 if (IsJPEGLossless()) {
137 ClbJpeg* jpg = _IdDcmJpegRead(fp);
143 // jpg->DataImg est alloue en int32 systematiquement :
144 // il faut le copier pixel par pixel dans destination ...
145 // ... qui est un void *
146 // --> du CAST sportif a faire ...
148 // memcpy(destination,jpg->DataImg,lgrTotale);
151 // ... y'a p't etre + ruse (?)
154 string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
155 if (str_nb == "gdcm::Unfound" ) {
158 nb = atoi(str_nb.c_str() );
159 if (nb == 12) nb =16;
162 int * dataJpg = jpg->DataImg;
163 int taille = GetXSize() * GetYSize() * GetZSize() * GetSamplesPerPixel();
169 unsigned short *dest = (unsigned short *)destination;
170 for (int i=0; i<taille; i++) {
172 if (*(dataJpg +i) >255) printf("data %d\n",*(dataJpg +i) );
173 *((unsigned char *)dest+i) = *(dataJpg +i);
180 unsigned short *dest = (unsigned short *)destination;
183 // la ligne commentee ci-dessous fait passer l'exec de 0.15 sec a 5.5 sec
184 // pour la meme image 512*512 ...
185 // optimiseur pas mis en oeuvre (?)
186 //for (int i=0; i<GetXSize() * GetYSize() * GetZSize(); i++) {
188 for (int i=0; i<taille; i++) {
189 *((unsigned short *)dest+i) = *(dataJpg +i);
196 unsigned int *dest=(unsigned int *)destination;
197 for (int i=0;i<taille; i++) {
198 *((unsigned int *)dest+i) = *(dataJpg +i);
204 _IdDcmJpegFree (jpg);
208 // ------------------------------- JPEG Lossy : call to IJG 6b
210 // TODO : faire qq chose d'intelligent pour limiter
211 // la duplication du code JPEG <bits=8 / bits=12>
212 // TODO : eplucher icelui pour traiter *egalement* bits=16
214 if ((nBS = GetBitsStored()) != 12) {
215 printf("Sorry, Bits Stored = %d not yet taken into account\n",nBS);
219 int res = gdcm_read_JPEG_file (destination);
221 printf ("res : %d\n",res);
228 * \brief Allocates necessary memory, copies the pixel data
229 * (image[s]/volume[s]) to newly allocated zone.
230 * @return Pointer to newly allocated pixel data.
232 void * gdcmFile::GetImageData (void) {
233 PixelData = (void *) malloc(lgrTotale);
234 GetImageDataIntoVector(PixelData, lgrTotale);
240 * \brief Copies at most MaxSize bytes of pixel data to caller's
242 * @param destination Address (in caller's memory space) at which the
243 * pixel data should be copied
244 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
245 * is not sufficient to hold the pixel data the copy is not
246 * executed (i.e. no partial copy).
247 * @return On success, the number of bytes actually copied. Zero on
248 * failure e.g. MaxSize is lower than necessary.
251 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
253 int nb, nbu, highBit, signe;
254 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
256 if ( lgrTotale > MaxSize ) {
257 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
258 "than caller's expected MaxSize");
262 (void)ReadPixelData(destination);
264 // Nombre de Bits Alloues pour le stockage d'un Pixel
265 str_nb = GetPubElValByNumber(0x0028,0x0100);
266 if (str_nb == "gdcm::Unfound" ) {
269 nb = atoi(str_nb.c_str() );
272 // Nombre de Bits Utilises
273 str_nbu=GetPubElValByNumber(0x0028,0x0101);
274 if (str_nbu == "gdcm::Unfound" ) {
277 nbu = atoi(str_nbu.c_str() );
280 // Position du Bit de Poids Fort
281 str_highBit=GetPubElValByNumber(0x0028,0x0102);
282 if (str_highBit == "gdcm::Unfound" ) {
285 highBit = atoi(str_highBit.c_str() );
289 str_signe=GetPubElValByNumber(0x0028,0x0103);
290 if (str_signe == "gdcm::Unfound" ) {
293 signe = atoi(str_signe.c_str() );
296 // On remet les Octets dans le bon ordre si besoin est
298 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
300 // On remet les Bits des Octets dans le bon ordre si besoin est
302 int l = (int)lgrTotale / (nb/8);
304 guint16 mask = 0xffff;
305 mask = mask >> (nb-nbu);
306 guint16 *deb = (guint16 *)destination;
307 for(int i = 0; i<l; i++) {
308 *deb = (*deb >> (nbu-highBit-1)) & mask;
311 } else if (nb == 32 ) {
312 guint32 mask = 0xffffffff;
313 mask = mask >> (nb-nbu);
314 guint32 *deb = (guint32 *)destination;
315 for(int i = 0; i<l; i++) {
316 *deb = (*deb >> (nbu-highBit-1)) & mask;
320 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
329 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
332 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
351 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
352 | ((((unsigned short int*)im)[i])<<8);
356 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
368 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
369 fort =((unsigned long int*)im)[i]>>16;
370 fort= (fort>>8) | (fort<<8);
371 faible=(faible>>8) | (faible<<8);
373 ((unsigned long int*)im)[i]=(s32<<16)|fort;
379 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
380 fort=((unsigned long int*)im)[i]>>16;
381 fort= (fort>>8) | (fort<<8);
382 faible=(faible>>8) | (faible<<8);
384 ((unsigned long int*)im)[i]=(s32<<16)|faible;
390 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
391 fort=((unsigned long int*)im)[i]>>16;
393 ((unsigned long int*)im)[i]=(s32<<16)|fort;
398 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
403 /////////////////////////////////////////////////////////////////
407 * \warning doit-etre etre publique ? FIXME JPR
408 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
410 * @param inData TODO JPR
411 * @param ExpectedSize TODO JPR
415 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
416 SetImageDataSize(ExpectedSize);
418 lgrTotale = ExpectedSize;
425 /////////////////////////////////////////////////////////////////
430 * \warning WARNING doit-etre etre publique ? FIXME JPR
431 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
433 * @param ImageDataSize TODO JPR
437 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
442 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
444 sprintf(car,"%d",ImageDataSize);
446 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
447 a->SetLength(ImageDataSize);
450 sprintf(car,"%d",ImageDataSize);
452 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
456 /////////////////////////////////////////////////////////////////
459 * \brief Ecrit sur disque les pixels d'UNE image
460 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
461 * Ca sera à l'utilisateur d'appeler son Reader correctement
462 * (Equivalent a IdImaWriteRawFile) FIXME JPR
464 * @param nomFichier TODO JPR
469 int gdcmFile::WriteRawData (string nomFichier) {
472 fp1 = fopen(nomFichier.c_str(),"wb");
474 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
478 fwrite (PixelData,lgrTotale, 1, fp1);
485 /////////////////////////////////////////////////////////////////
488 * \brief Ecrit sur disque UNE image Dicom
489 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
490 * Ca fonctionnera correctement (?) sur processeur Intel
491 * (Equivalent a IdDcmWrite) FIXME JPR
493 * @param nomFichier TODO JPR
498 int gdcmFile::WriteDcmImplVR (string nomFichier) {
499 return WriteBase(nomFichier, ImplicitVR);
502 /////////////////////////////////////////////////////////////////
506 * @param nomFichier TODO JPR
511 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
512 return WriteDcmImplVR (string (nomFichier));
515 /////////////////////////////////////////////////////////////////
519 * @param nomFichier TODO JPR
524 int gdcmFile::WriteDcmExplVR (string nomFichier) {
525 return WriteBase(nomFichier, ExplicitVR);
528 /////////////////////////////////////////////////////////////////
531 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
532 * (a l'attention des logiciels cliniques
533 * qui ne prennent en entrée QUE des images ACR ...
534 * \warning si un header DICOM est fourni en entree,
535 * les groupes < 0x0008 et les groupes impairs sont ignores)
536 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
537 * Ca fonctionnera correctement (?) sur processeur Intel
538 * (Equivalent a IdDcmWrite)
540 * @param nomFichier TODO JPR
545 int gdcmFile::WriteAcr (string nomFichier) {
546 return WriteBase(nomFichier, ACR);
549 /////////////////////////////////////////////////////////////////
553 * @param nomFichier TODO JPR
554 * @param type TODO JPR
558 int gdcmFile::WriteBase (string nomFichier, FileType type) {
561 fp1 = fopen(nomFichier.c_str(),"wb");
563 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
567 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
569 // Ecriture Dicom File Preamble
570 filePreamble=(char*)calloc(128,1);
571 fwrite(filePreamble,128,1,fp1);
572 fwrite("DICM",4,1,fp1);
575 gdcmHeader::Write(fp1, type);
576 fwrite(PixelData, lgrTotale, 1, fp1);