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)* GetSamplesPerPixel();;
62 /////////////////////////////////////////////////////////////////
65 * \brief Returns the size (in bytes) of required memory to hold
66 * the pixel data represented in this file.
67 * @return The size of pixel data in bytes.
69 size_t gdcmFile::GetImageDataSize(void) {
74 /////////////////////////////////////////////////////////////////
77 * \brief Read pixel data from disk (optionaly decompressing) into the
78 * caller specified memory location.
79 * @param destination where the pixel data should be stored.
82 bool gdcmFile::ReadPixelData(void* destination) {
86 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
92 IsImplicitVRLittleEndianTransferSyntax() ||
93 IsExplicitVRLittleEndianTransferSyntax() ||
94 IsExplicitVRBigEndianTransferSyntax() ||
95 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
97 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
98 if ( ItemRead != 1 ) {
107 // ------------------------------- Position on begining of Jpeg Pixels
110 fseek(fp,4,SEEK_CUR);
114 fseek(fp,ln,SEEK_CUR);
115 fseek(fp,4,SEEK_CUR);
120 // ------------------------------- JPEG LossLess : call to Jpeg Libido
122 if (IsJPEGLossless()) {
124 ClbJpeg* jpg = _IdDcmJpegRead(fp);
131 string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
132 if (str_nb == "gdcm::Unfound" ) {
135 nb = atoi(str_nb.c_str() );
136 if (nb == 12) nb =16;
139 int * dataJpg = jpg->DataImg;
140 int taille = GetXSize() * GetYSize() * GetZSize() * GetSamplesPerPixel();
145 unsigned short *dest = (unsigned short *)destination;
146 for (int i=0; i<taille; i++) {
147 *((unsigned char *)dest+i) = *(dataJpg +i);
154 unsigned short *dest = (unsigned short *)destination;
155 for (int i=0; i<taille; i++) {
156 *((unsigned short *)dest+i) = *(dataJpg +i);
163 unsigned int *dest=(unsigned int *)destination;
164 for (int i=0;i<taille; i++) {
165 *((unsigned int *)dest+i) = *(dataJpg +i);
171 _IdDcmJpegFree (jpg);
175 // ------------------------------- JPEG Lossy : call to IJG 6b
177 // TODO : faire qq chose d'intelligent pour limiter
178 // la duplication du code JPEG <bits=8 / bits=12>
179 // TODO : eplucher icelui pour traiter *egalement* bits=16
182 if ((nBS = GetBitsStored()) != 12) {
183 printf("Sorry, Bits Stored = %d not yet taken into account\n",nBS);
187 bool res = (bool)gdcm_read_JPEG_file (destination);
193 * \brief Allocates necessary memory, copies the pixel data
194 * (image[s]/volume[s]) to newly allocated zone.
195 * @return Pointer to newly allocated pixel data.
197 void * gdcmFile::GetImageData (void) {
198 PixelData = (void *) malloc(lgrTotale);
199 GetImageDataIntoVector(PixelData, lgrTotale);
205 * \brief Copies at most MaxSize bytes of pixel data to caller's
207 * @param destination Address (in caller's memory space) at which the
208 * pixel data should be copied
209 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
210 * is not sufficient to hold the pixel data the copy is not
211 * executed (i.e. no partial copy).
212 * @return On success, the number of bytes actually copied. Zero on
213 * failure e.g. MaxSize is lower than necessary.
216 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
218 int nb, nbu, highBit, signe;
219 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
221 if ( lgrTotale > MaxSize ) {
222 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
223 "than caller's expected MaxSize");
227 (void)ReadPixelData(destination);
229 // Nombre de Bits Alloues pour le stockage d'un Pixel
230 str_nb = GetPubElValByNumber(0x0028,0x0100);
231 if (str_nb == "gdcm::Unfound" ) {
234 nb = atoi(str_nb.c_str() );
237 // Nombre de Bits Utilises
238 str_nbu=GetPubElValByNumber(0x0028,0x0101);
239 if (str_nbu == "gdcm::Unfound" ) {
242 nbu = atoi(str_nbu.c_str() );
245 // Position du Bit de Poids Fort
246 str_highBit=GetPubElValByNumber(0x0028,0x0102);
247 if (str_highBit == "gdcm::Unfound" ) {
250 highBit = atoi(str_highBit.c_str() );
254 str_signe=GetPubElValByNumber(0x0028,0x0103);
255 if (str_signe == "gdcm::Unfound" ) {
258 signe = atoi(str_signe.c_str() );
261 // On remet les Octets dans le bon ordre si besoin est
263 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
265 // On remet les Bits des Octets dans le bon ordre si besoin est
267 int l = (int)lgrTotale / (nb/8);
269 guint16 mask = 0xffff;
270 mask = mask >> (nb-nbu);
271 guint16 *deb = (guint16 *)destination;
272 for(int i = 0; i<l; i++) {
273 *deb = (*deb >> (nbu-highBit-1)) & mask;
276 } else if (nb == 32 ) {
277 guint32 mask = 0xffffffff;
278 mask = mask >> (nb-nbu);
279 guint32 *deb = (guint32 *)destination;
280 for(int i = 0; i<l; i++) {
281 *deb = (*deb >> (nbu-highBit-1)) & mask;
285 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
291 string str_PlanarConfiguration = GetPubElValByNumber(0x0028,0x0006);
292 int PlanarConfiguration;
293 if (str_PlanarConfiguration == "gdcm::Unfound" ) {
294 PlanarConfiguration = 0;
296 PlanarConfiguration = atoi(str_PlanarConfiguration.c_str() );
301 // if (GetPlanarConfiguration() == 1) {
304 if (PlanarConfiguration == 1) { // need to make RGB Pixels
305 int l = lgrTotale/3 ;
307 char * a = (char *)destination;
310 char * newDest = (char*) malloc(lgrTotale);
312 // any trick not to have to allocate temporary buffer is welcome ...
314 for (int j=0;j<l; j++) {
319 a = (char *)destination;
321 for (int i=0;i<lgrTotale; i++) {
331 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
334 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
352 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
353 | ((((unsigned short int*)im)[i])<<8);
357 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;
423 /////////////////////////////////////////////////////////////////
428 * \warning WARNING doit-etre etre publique ? FIXME JPR
429 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
431 * @param ImageDataSize TODO JPR
435 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
440 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
442 sprintf(car,"%d",ImageDataSize);
444 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
445 a->SetLength(ImageDataSize);
448 sprintf(car,"%d",ImageDataSize);
450 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
454 /////////////////////////////////////////////////////////////////
457 * \brief Ecrit sur disque les pixels d'UNE image
458 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
459 * Ca sera à l'utilisateur d'appeler son Reader correctement
460 * (Equivalent a IdImaWriteRawFile) FIXME JPR
462 * @param nomFichier TODO JPR
467 int gdcmFile::WriteRawData (string nomFichier) {
470 fp1 = fopen(nomFichier.c_str(),"wb");
472 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
476 fwrite (PixelData,lgrTotale, 1, fp1);
483 /////////////////////////////////////////////////////////////////
486 * \brief Ecrit sur disque UNE image Dicom
487 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
488 * Ca fonctionnera correctement (?) sur processeur Intel
489 * (Equivalent a IdDcmWrite) FIXME JPR
491 * @param nomFichier TODO JPR
496 int gdcmFile::WriteDcmImplVR (string nomFichier) {
497 return WriteBase(nomFichier, ImplicitVR);
500 /////////////////////////////////////////////////////////////////
504 * @param nomFichier TODO JPR
509 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
510 return WriteDcmImplVR (string (nomFichier));
513 /////////////////////////////////////////////////////////////////
517 * @param nomFichier TODO JPR
522 int gdcmFile::WriteDcmExplVR (string nomFichier) {
523 return WriteBase(nomFichier, ExplicitVR);
526 /////////////////////////////////////////////////////////////////
529 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
530 * (a l'attention des logiciels cliniques
531 * qui ne prennent en entrée QUE des images ACR ...
532 * \warning si un header DICOM est fourni en entree,
533 * les groupes < 0x0008 et les groupes impairs sont ignores)
534 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
535 * Ca fonctionnera correctement (?) sur processeur Intel
536 * (Equivalent a IdDcmWrite)
538 * @param nomFichier TODO JPR
543 int gdcmFile::WriteAcr (string nomFichier) {
544 return WriteBase(nomFichier, ACR);
547 /////////////////////////////////////////////////////////////////
551 * @param FileName TODO JPR
552 * @param type TODO JPR
556 int gdcmFile::WriteBase (string FileName, FileType type) {
559 fp1 = fopen(FileName.c_str(),"wb");
561 printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
565 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
567 // Ecriture Dicom File Preamble
568 filePreamble=(char*)calloc(128,1);
569 fwrite(filePreamble,128,1,fp1);
570 fwrite("DICM",4,1,fp1);
573 gdcmHeader::Write(fp1, type);
574 fwrite(PixelData, lgrTotale, 1, fp1);