]> Creatis software - gdcm.git/blob - src/gdcmFile.cxx
-->File gdcmParse.cxx added
[gdcm.git] / src / gdcmFile.cxx
1 // gdcmFile.cxx
2
3 //This is needed when compiling in debug mode
4 #ifdef _MSC_VER
5 // 'type' : forcing value to bool 'true' or 'false' (performance warning)
6 //#pragma warning ( disable : 4800 )
7 // 'identifier' : class 'type' needs to have dll-interface to be used by
8 // clients of class 'type2'
9 #pragma warning ( disable : 4251 )
10 // 'identifier' : identifier was truncated to 'number' characters in the
11 // debug information
12 #pragma warning ( disable : 4786 )
13 #endif //_MSC_VER
14
15 #include "gdcmFile.h"
16 #include "gdcmUtil.h"
17 #include "iddcmjpeg.h" // for the 'LibIDO' Jpeg LossLess
18
19 /////////////////////////////////////////////////////////////////
20 /**
21  * \ingroup   gdcmFile
22  * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
23  *        file (see SetFileName, SetDcmTag and Write)
24  *        Opens (in read only and when possible) an existing file and checks
25  *        for DICOM compliance. Returns NULL on failure.
26  * \Note  the in-memory representation of all available tags found in
27  *        the DICOM header is post-poned to first header information access.
28  *        This avoid a double parsing of public part of the header when
29  *        one sets an a posteriori shadow dictionary (efficiency can be
30  *        seen as a side effect).   
31  *
32  * @param filename file to be opened for parsing
33  *
34  * @return      
35  */
36  
37 gdcmFile::gdcmFile(std::string & filename) 
38         :gdcmHeader(filename.c_str())   
39 {
40    SetPixelDataSizeFromHeader();
41 }
42
43 gdcmFile::gdcmFile(const char * filename) 
44         :gdcmHeader(filename)   
45 {
46    SetPixelDataSizeFromHeader();
47 }
48
49 /**
50  * \ingroup   gdcmFile
51  * \brief     calcule la longueur (in bytes) A ALLOUER pour recevoir les
52  *              pixels de l'image
53  *              ou DES images dans le cas d'un multiframe
54  *              ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
55  *              (dans le cas d'images compressees, elle n'a pas de sens).
56  *
57  * @return      longueur a allouer 
58  */
59 void gdcmFile::SetPixelDataSizeFromHeader(void) {
60    int nb;
61    std::string str_nb;
62
63    str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
64    if (str_nb == GDCM_UNFOUND ) {
65       nb = 16;
66    } else {
67       nb = atoi(str_nb.c_str() );
68       if (nb == 12) nb =16;
69    }
70    lgrTotale =  GetXSize() *  GetYSize() *  GetZSize() * (nb/8)* GetSamplesPerPixel();
71    
72    std::string str_PhotometricInterpretation = gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
73    if (   str_PhotometricInterpretation == "PALETTE COLOR " 
74        || str_PhotometricInterpretation == "YBR_FULL") {   // --> some more to be added !!
75       lgrTotale*=3;
76    }
77 }
78    // see PS 3.3-2003 : C.7.6.3.2.1  
79    // 
80    //   MONOCHROME1
81    //   MONOCHROME2
82    //   PALETTE COLOR
83    //   RGB
84    //   HSV  (Retired)
85    //   ARGB (Retired)
86    //   CMYK (Retired)
87    //   YBR_FULL
88    //   YBR_FULL_422 (no LUT, no Palette)
89    //   YBR_PARTIAL_422
90    //   YBR_ICT
91    //   YBR_RCT
92
93   // LUT's
94   // ex : gdcm-US-ALOKA-16.dcm
95   // 0028|1221 [OW]   [Segmented Red Palette Color Lookup Table Data]
96   // 0028|1222 [OW]   [Segmented Green Palette Color Lookup Table Data]  
97   // 0028|1223 [OW]   [Segmented Blue Palette Color Lookup Table Data]
98
99   // ex : US-PAL-8-10x-echo.dcm, 8BitsRunLengthColor.dcm
100   // 0028|1201 [OW]   [Red Palette Color Lookup Table Data]
101   // 0028|1202 [OW]   [Green Palette Color Lookup Table Data]
102   // 0028|1203 [OW]   [Blue Palette Color Lookup Table Data]
103
104   // ex  : OT-PAL-8-face.dcm
105   // 0028|1201 [US]   [Red Palette Color Lookup Table Data]
106   // 0028|1202 [US]   [Green Palette Color Lookup Table Data]
107   // 0028|1203 [US]   [Blue Palette Color Lookup Table Data]
108
109
110
111 /////////////////////////////////////////////////////////////////
112 /**
113  * \ingroup   gdcmFile
114  * \brief     Returns the size (in bytes) of required memory to hold
115  *            the pixel data represented in this file.
116  * @return    The size of pixel data in bytes.
117  */
118
119 size_t gdcmFile::GetImageDataSize(void) {
120    return (lgrTotale);
121 }
122
123
124 /////////////////////////////////////////////////////////////////
125 /**
126  * \ingroup gdcmFile
127  * \brief   Read pixel data from disk (optionaly decompressing) into the
128  *          caller specified memory location.
129  * @param   destination where the pixel data should be stored.
130  *
131  */
132 bool gdcmFile::ReadPixelData(void* destination) {
133
134    if ( !OpenFile())
135       return false;
136       
137     if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
138       CloseFile();
139       return false;
140    }     
141
142 // -------------------------  Uncompressed File
143     
144    if ( !IsDicomV3()                             ||
145         IsImplicitVRLittleEndianTransferSyntax() ||
146         IsExplicitVRLittleEndianTransferSyntax() ||
147         IsExplicitVRBigEndianTransferSyntax()    ||
148         IsDeflatedExplicitVRLittleEndianTransferSyntax() ) { 
149                     
150       size_t ItemRead = fread(destination, lgrTotale, 1, fp);
151       if ( ItemRead != 1 ) {
152          CloseFile();
153          return false;
154       } else {
155          CloseFile();
156          return true;
157       }
158    } 
159     
160  // ------------------------  Compressed File .
161        
162       int nb;
163       std::string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
164       if (str_nb == GDCM_UNFOUND ) {
165          nb = 16;
166       } else {
167          nb = atoi(str_nb.c_str() );
168          if (nb == 12) nb =16;
169       }
170       int nBytes= nb/8;
171       
172       int taille = GetXSize() *  GetYSize()  * GetSamplesPerPixel(); 
173           
174                 
175   // ------------------------------- JPEG LossLess : call to Jpeg Libido
176    
177    if (IsJPEGLossless() && GetZSize() == 1) {
178    
179       int ln; //  Position on begining of Jpeg Pixels
180       fseek(fp,4,SEEK_CUR);  // skipping (fffe,e000) : Basic Offset Table Item
181       fread(&ln,4,1,fp); 
182       if(GetSwapCode()) 
183          ln=SwapLong(ln);    // Item length
184       fseek(fp,ln,SEEK_CUR); // skipping Basic Offset Table ('ln' bytes) 
185       fseek(fp,4,SEEK_CUR);  // skipping (fffe,e000) : First fragment Item Tag
186       fread(&ln,4,1,fp);     // First fragment length (just to know)
187       if(GetSwapCode()) 
188          ln=SwapLong(ln);      
189  
190       ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
191                                          // (We use the LibIDO one :-(
192       if(jpg == NULL) {
193          CloseFile();
194          return false;
195       }      
196       int * dataJpg = jpg->DataImg;
197       
198       switch (nBytes) {   
199          case 1:
200          {
201             unsigned short *dest = (unsigned short *)destination;
202             for (int i=0; i<taille; i++) {
203                *((unsigned char *)dest+i) = *(dataJpg +i);   
204             }
205          }
206          break;        
207          
208          case 2:
209          {
210             unsigned short *dest = (unsigned short *)destination;
211             for (int i=0; i<taille; i++) {           
212                *((unsigned short *)dest+i) = *(dataJpg +i);    
213             }
214          }
215          break;       
216      }
217       _IdDcmJpegFree (jpg);
218       return true;
219    } 
220  
221   // ------------------------------- RLE
222
223       if (gdcmHeader::IsRLELossLessTransferSyntax()) {
224             int res = (bool)gdcm_read_RLE_file (destination);
225             return res; 
226       }
227
228   // ------------------------------- JPEG Lossy : call to IJG 6b
229     
230       long fragmentBegining; // for ftell, fseek
231       bool b = gdcmHeader::IsJPEG2000();
232        
233       bool res;
234       guint16 ItemTagGr,ItemTagEl;
235       int ln;  //  Position on begining of Jpeg Pixels
236       
237       fread(&ItemTagGr,2,1,fp);  // Reading (fffe) : Item Tag Gr
238       fread(&ItemTagEl,2,1,fp);  // Reading (e000) : Item Tag El
239       if(GetSwapCode()) {
240          ItemTagGr=SwapShort(ItemTagGr); 
241          ItemTagEl=SwapShort(ItemTagEl);            
242       }
243       fread(&ln,4,1,fp); 
244       if(GetSwapCode()) 
245          ln=SwapLong(ln);    // Basic Offset Table Item length
246          
247       if (ln != 0) {
248          // What is it used for ?!?
249          char *BasicOffsetTableItemValue = (char *)malloc(ln+1);        
250          fread(BasicOffsetTableItemValue,ln,1,fp); 
251       }
252       
253       // first Fragment initialisation
254       fread(&ItemTagGr,2,1,fp);  // Reading (fffe) : Item Tag Gr
255       fread(&ItemTagEl,2,1,fp);  // Reading (e000) : Item Tag El
256       if(GetSwapCode()) {
257          ItemTagGr=SwapShort(ItemTagGr); 
258          ItemTagEl=SwapShort(ItemTagEl);            
259       }
260               
261       // parsing fragments until Sequence Delim. Tag found
262        //unsigned short *dest = (unsigned short *)destination;
263          
264       while (  ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
265          fread(&ln,4,1,fp); 
266          if(GetSwapCode()) 
267             ln=SwapLong(ln);    // Fragment Item length
268       
269          // FIXME : multi fragments 
270          fragmentBegining=ftell(fp);
271                        
272  
273          if (b)
274             res = (bool)gdcm_read_JPEG2000_file (destination);  // Reading Fragment pixels 
275             
276          else if (IsJPEGLossless()) {  
277                   // ------------- call to LibIDO Jpeg for each Frame/fragment
278                   
279                   // Warning : Works only if there is one fragment per frame
280                   //           (Or a single fragment for the multiframe file)
281             ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
282                                                // (We use the LibIDO one :-(
283             if(jpg == NULL) {
284                CloseFile();
285                return false;
286             }      
287             int * dataJpg = jpg->DataImg;
288             unsigned short *dest = (unsigned short *)destination;
289             switch (nBytes) {   
290                case 1:
291                {
292                   for (int i=0; i<taille; i++) {
293                      *((unsigned char *)dest+i) = *(dataJpg +i);   
294                   }
295                break;
296                }        
297          
298                case 2:
299                {
300                   for (int i=0; i<taille; i++) {        
301                      *((unsigned short *)dest+i) = *(dataJpg +i);    
302                   }
303                break;
304                }       
305            } 
306            _IdDcmJpegFree (jpg);
307      
308          } // ------------------------------------- endif (IsJPEGLossless())
309                   
310          else
311             if  (GetBitsStored() == 8) {
312                 res = (bool)gdcm_read_JPEG_file (destination);  // Reading Fragment pixels         
313             } else {
314                 res = (bool)gdcm_read_JPEG_file12 (destination);// Reading Fragment pixels  
315             }       
316             
317          if (!res) break;
318          
319          // FIXME : will work only when each fragment corresponds to a Frame :-(
320          
321          destination = (char *)destination + taille * nBytes; // location in user's memory 
322                                                   // for next fragment (if any) 
323          // TODO : find a suitable file (multifragment/single Frame Jpeg file) to check
324          
325          fseek(fp,fragmentBegining,SEEK_SET); // To be sure we start 
326          fseek(fp,ln,SEEK_CUR);               // at the begining of next fragment
327          
328          ItemTagGr = ItemTagEl =0;
329          fread(&ItemTagGr,2,1,fp);  // Reading (fffe) : Item Tag Gr
330          fread(&ItemTagEl,2,1,fp);  // Reading (e000) : Item Tag El
331          if(GetSwapCode()) {
332             ItemTagGr=SwapShort(ItemTagGr); 
333             ItemTagEl=SwapShort(ItemTagEl);            
334          } 
335          
336       //(char *) destination += taille * nBytes;
337       //std::cout << "destination" << destination << std::endl;
338       }
339                 
340       return res;
341 }   
342
343 /**
344  * \ingroup gdcmFile
345  * \brief   Allocates necessary memory, copies the pixel data
346  *          (image[s]/volume[s]) to newly allocated zone.
347  * @return  Pointer to newly allocated pixel data.
348  * \        NULL if alloc fails 
349  */
350 void * gdcmFile::GetImageData (void) {
351    PixelData = (void *) malloc(lgrTotale);
352    if (PixelData)
353       GetImageDataIntoVector(PixelData, lgrTotale);
354    return(PixelData);
355 }
356
357 /**
358  * \ingroup gdcmFile
359  * \brief   Copies at most MaxSize bytes of pixel data to caller's
360  *          memory space.
361  * @param   destination Address (in caller's memory space) at which the
362  *          pixel data should be copied
363  * @param   MaxSize Maximum number of bytes to be copied. When MaxSize
364  *          is not sufficient to hold the pixel data the copy is not
365  *          executed (i.e. no partial copy).
366  * @return  On success, the number of bytes actually copied. Zero on
367  *          failure e.g. MaxSize is lower than necessary.
368  */
369
370 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
371
372    int nb, nbu, highBit, signe;
373    std::string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
374  
375    if ( lgrTotale > MaxSize ) {
376       dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
377                      "than caller's expected MaxSize");
378       return (size_t)0; 
379    }
380         
381    (void)ReadPixelData(destination);
382                         
383         // Nombre de Bits Alloues pour le stockage d'un Pixel
384    str_nb = GetPubElValByNumber(0x0028,0x0100);
385    if (str_nb == GDCM_UNFOUND ) {
386       nb = 16;
387    } else {
388       nb = atoi(str_nb.c_str() );
389    }
390         
391         // Nombre de Bits Utilises
392    str_nbu=GetPubElValByNumber(0x0028,0x0101);
393    if (str_nbu == GDCM_UNFOUND ) {
394       nbu = nb;
395    } else {
396       nbu = atoi(str_nbu.c_str() );
397    }    
398         
399         // Position du Bit de Poids Fort
400    str_highBit=GetPubElValByNumber(0x0028,0x0102);
401    if (str_highBit == GDCM_UNFOUND ) {
402       highBit = nb - 1;
403    } else {
404       highBit = atoi(str_highBit.c_str() );
405    }
406                 
407         // Signe des Pixels 
408    str_signe=GetPubElValByNumber(0x0028,0x0103);
409    if (str_signe == GDCM_UNFOUND ) {
410       signe = 1;
411    } else {
412       signe = atoi(str_signe.c_str() );
413    }
414
415    // re arange bytes inside the integer
416    if (nb != 8)
417      SwapZone(destination, GetSwapCode(), lgrTotale, nb);
418  
419    // re arange bits inside the bytes
420    if (nbu != nb){
421       int l = (int)lgrTotale / (nb/8);
422       if (nb == 16) {
423          guint16 mask = 0xffff;
424          mask = mask >> (nb-nbu);
425          guint16 *deb = (guint16 *)destination;
426          for(int i = 0; i<l; i++) {
427             *deb = (*deb >> (nbu-highBit-1)) & mask;
428             deb ++;
429          }
430       } else if (nb == 32 ) {
431          guint32 mask = 0xffffffff;
432          mask = mask >> (nb-nbu);
433          guint32 *deb = (guint32 *)destination;
434          for(int i = 0; i<l; i++) {
435             *deb = (*deb >> (nbu-highBit-1)) & mask;
436             deb ++;
437          }
438       } else {
439          dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
440          return (size_t)0; 
441       }
442    }  
443    
444    // *Try* to deal with the color
445    // --------------------------
446      
447    std::string str_PhotometricInterpretation = gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
448    
449    if ( (str_PhotometricInterpretation == "MONOCHROME1 ") 
450      || (str_PhotometricInterpretation == "MONOCHROME2 ") 
451      || (str_PhotometricInterpretation == "RGB")) {
452       return lgrTotale; 
453    }
454    int planConf=GetPlanarConfiguration();
455
456    if( str_PhotometricInterpretation!="PALETTE COLOR" && planConf == 0) 
457       planConf=2;  // Sorry, this is an heuristic
458      
459    switch ( planConf) {
460    case 0:                              
461       //       Pixels are already RGB
462       break;
463     
464    case 1:
465       //       need to make RGB Pixels from Planes R,G,B
466       {
467          int l = lgrTotale/3 ;
468
469          char * a = (char *)destination;
470          char * b = a + l;
471          char * c = b + l;
472          char * newDest = (char*) malloc(lgrTotale);
473          // TODO :
474          // any trick not to have to allocate temporary buffer is welcome ...
475          char *x = newDest;
476          for (int j=0;j<l; j++) {
477             *(x++) = *(a++);
478             *(x++) = *(b++);
479             *(x++) = *(c++);  
480          }
481          memmove(destination,newDest,lgrTotale);
482          free(newDest);
483          // now, it's an RGB image
484          std::string spp = "3";
485          gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
486          std::string rgb="RGB";
487          gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
488          break;
489       }
490       
491     case 2:                      
492       //       from Lut R + Lut G + Lut B
493        
494       // we no longer use gdcmHeader::GetLUTRGB
495       // since a lot of images have wrong info 
496       // in the Lookup Table Descriptors (0028,1101),...
497       {
498          unsigned char *lutR =(unsigned char *)GetPubElValVoidAreaByNumber(0x0028,0x1201);
499          unsigned char *lutG =(unsigned char *)GetPubElValVoidAreaByNumber(0x0028,0x1202);
500          unsigned char *lutB =(unsigned char *)GetPubElValVoidAreaByNumber(0x0028,0x1203);
501       
502          if (lutR && lutG && lutB ) { // need to make RGB Pixels 
503                                       // from grey Pixels 
504                                       // and Lut R,Lut G,Lut B
505             unsigned char * newDest = (unsigned char*) malloc(lgrTotale);
506             int l = lgrTotale/3;
507             memmove(newDest, destination, l);// move Gray pixels to temp area
508
509             unsigned char * x = newDest;
510             unsigned char * a = (unsigned char *)destination;
511             int j;        
512             for (int i=0;i<l; i++) {
513                j=newDest[i]*2;  // Who can explain *why* we have to skip bytes
514                *a++ = lutR[j]; 
515                *a++ = lutG[j];
516                *a++ = lutB[j];
517             }
518             free(newDest);
519          
520             // now, it's an RGB image      
521            std::string spp = "3";
522             gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002); 
523            std::string rgb="RGB";
524            gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
525             
526          } else { // need to make RGB Pixels (?)
527                // from grey Pixels (?!)
528                // and Gray Lut  (!?!) 
529             unsigned char *lutGray =(unsigned char *)GetPubElValVoidAreaByNumber(0x0028,0x1200); 
530                  // Well . I'll wait till I find such an image 
531          }
532          break;
533       }
534    } 
535     
536    return lgrTotale; 
537 }
538
539 //
540 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
541 //
542
543 /**
544  * \ingroup gdcmFile
545  * \brief   Swap the bytes, according to swap code.
546  * \warning not end user intended
547  * @param   im area to deal with
548  * @param   swap swap code
549  * @param   lgr Area Length
550  * @param   nb Pixels Bit number 
551  */
552
553 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
554 guint32 s32;
555 guint16 fort,faible;
556 int i;
557
558 if(nb == 16)  
559    switch(swap) {
560       case 0:
561       case 12:
562       case 1234:
563          break;
564                 
565       case 21:
566       case 3412:
567       case 2143:
568       case 4321:
569
570          for(i=0;i<lgr;i++)
571             ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
572                                         | ((((unsigned short int*)im)[i])<<8);
573          break;
574                         
575       default:
576          printf("valeur de SWAP (16 bits) not allowed : %d\n", swap);
577    } 
578  
579 if( nb == 32 )
580    switch (swap) {
581       case 0:
582       case 1234:
583          break;
584
585       case 4321:
586          for(i=0;i<lgr;i++) {
587             faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 4321 */
588             fort  =((unsigned long int*)im)[i]>>16;
589             fort=  (fort>>8)   | (fort<<8);
590             faible=(faible>>8) | (faible<<8);
591             s32=faible;
592             ((unsigned long int*)im)[i]=(s32<<16)|fort;
593          }
594          break;
595
596       case 2143:
597          for(i=0;i<lgr;i++) {
598             faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 2143 */
599             fort=((unsigned long int*)im)[i]>>16;
600             fort=  (fort>>8)   | (fort<<8);
601             faible=(faible>>8) | (faible<<8);
602             s32=fort; 
603             ((unsigned long int*)im)[i]=(s32<<16)|faible;
604          }
605          break;
606   
607       case 3412:
608          for(i=0;i<lgr;i++) {
609             faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 3412 */
610             fort=((unsigned long int*)im)[i]>>16;                  
611             s32=faible; 
612             ((unsigned long int*)im)[i]=(s32<<16)|fort;
613          }                 
614          break; 
615                                 
616       default:
617          printf(" SWAP value (32 bits) not allowed : %d\n", swap);
618    } 
619 return;
620 }
621
622 /////////////////////////////////////////////////////////////////
623 /**
624  * \ingroup   gdcmFile
625  * \brief TODO JPR
626  * \warning doit-etre etre publique ?  
627  * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
628  *
629  * @param inData 
630  * @param ExpectedSize 
631  *
632  * @return integer acts as a boolean    
633  */
634 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
635    SetImageDataSize(ExpectedSize);
636    PixelData = inData;
637    lgrTotale = ExpectedSize;
638    return(1);
639 }
640
641
642 /////////////////////////////////////////////////////////////////
643 /**
644  * \ingroup   gdcmFile
645  * \brief Sets the Pixel Area size in the Header
646  *        --> not-for-rats function
647  * 
648  * \warning WARNING doit-etre etre publique ? 
649  * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
650  *
651  * @param ImageDataSize new Pixel Area Size
652  *        warning : nothing else is checked
653  */
654
655 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
656    std::string content1;
657    char car[20];        
658    // Assumes ElValue (0x7fe0, 0x0010) exists ...       
659    sprintf(car,"%d",ImageDataSize);
660  
661    gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
662    a->SetLength(ImageDataSize);
663                 
664    ImageDataSize+=8;
665    sprintf(car,"%d",ImageDataSize);
666    content1=car;        
667    SetPubElValByNumber(content1, 0x7fe0, 0x0000);
668 }
669
670
671 /////////////////////////////////////////////////////////////////
672 /**
673  * \ingroup   gdcmFile
674  * \brief Ecrit sur disque les pixels d'UNE image
675  *        Aucun test n'est fait sur l'"Endiannerie" du processeur.
676  *        Ca sera à l'utilisateur d'appeler son Reader correctement
677  *        (Equivalent a IdImaWriteRawFile) 
678  *
679  * @param fileName 
680  * @return      
681  */
682
683 int gdcmFile::WriteRawData (std::string fileName) {
684    FILE * fp1;
685    fp1 = fopen(fileName.c_str(),"wb");
686    if (fp1 == NULL) {
687       printf("Echec ouverture (ecriture) Fichier [%s] \n",fileName.c_str());
688       return (0);
689    }    
690    fwrite (PixelData,lgrTotale, 1, fp1);
691    fclose (fp1);
692    return(1);
693 }
694
695 /////////////////////////////////////////////////////////////////
696 /**
697  * \ingroup   gdcmFile
698  * \brief Ecrit sur disque UNE image Dicom
699  *        Aucun test n'est fait sur l'"Endiannerie" du processeur.
700  *         Ca fonctionnera correctement (?) sur processeur Intel
701  *         (Equivalent a IdDcmWrite) 
702  *
703  * @param fileName 
704  * @return int acts as a boolean
705  */
706
707 int gdcmFile::WriteDcmImplVR (std::string fileName) {
708    return WriteBase(fileName, ImplicitVR);
709 }
710
711 /////////////////////////////////////////////////////////////////
712 /**
713  * \ingroup   gdcmFile
714  * \brief  
715  * @param  fileName 
716  * @return int acts as a boolean
717  */
718  
719 int gdcmFile::WriteDcmImplVR (const char* fileName) {
720    return WriteDcmImplVR (std::string (fileName));
721 }
722         
723 /////////////////////////////////////////////////////////////////
724 /**
725  * \ingroup   gdcmFile
726  * \brief  
727  * @param  fileName
728  * @return int acts as a boolean
729  */
730
731 int gdcmFile::WriteDcmExplVR (std::string fileName) {
732    return WriteBase(fileName, ExplicitVR);
733 }
734         
735 /////////////////////////////////////////////////////////////////
736 /**
737  * \ingroup   gdcmFile
738  * \brief  Ecrit au format ACR-NEMA sur disque l'entete et les pixels
739  *        (a l'attention des logiciels cliniques 
740  *        qui ne prennent en entrée QUE des images ACR ...
741  * \warning si un header DICOM est fourni en entree,
742  *        les groupes < 0x0008 et les groupes impairs sont ignores)
743  * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
744  *        Ca fonctionnera correctement (?) sur processeur Intel
745  *        (Equivalent a IdDcmWrite) 
746  *
747  * @param fileName
748  * @return int acts as a boolean        
749  */
750
751 int gdcmFile::WriteAcr (std::string fileName) {
752    return WriteBase(fileName, ACR);
753 }
754
755 /////////////////////////////////////////////////////////////////
756 /**
757  * \ingroup   gdcmFile
758  *
759  * @param  FileName
760  * @param  type 
761  *
762  * @return int acts as a boolean
763  */
764 int gdcmFile::WriteBase (std::string FileName, FileType type) {
765
766    FILE * fp1;
767    fp1 = fopen(FileName.c_str(),"wb");
768    if (fp1 == NULL) {
769       printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
770       return (0);
771    }
772
773    if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
774       char * filePreamble;
775       // writing Dicom File Preamble
776       filePreamble=(char*)calloc(128,1);
777       fwrite(filePreamble,128,1,fp1);
778       fwrite("DICM",4,1,fp1);
779    }
780
781    // --------------------------------------------------------------
782    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
783    //
784    // if recognition code tells us we dealt with a LibIDO image
785    // we reproduce on disk the switch between lineNumber and columnNumber
786    // just before writting ...
787
788    std::string rows, columns; 
789    if ( filetype == ACR_LIBIDO){
790          rows    = GetPubElValByNumber(0x0028, 0x0010);
791          columns = GetPubElValByNumber(0x0028, 0x0011);
792          SetPubElValByNumber(columns,  0x0028, 0x0010);
793          SetPubElValByNumber(rows   ,  0x0028, 0x0011);
794    }    
795    // ----------------- End of Special Patch ----------------
796
797    gdcmHeader::Write(fp1, type);
798
799    // --------------------------------------------------------------
800    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
801    // 
802    // ...and we restore the Header to be Dicom Compliant again 
803    // just after writting
804
805    if (filetype == ACR_LIBIDO){
806          SetPubElValByNumber(rows   , 0x0028, 0x0010);
807          SetPubElValByNumber(columns, 0x0028, 0x0011);
808    }    
809    // ----------------- End of Special Patch ----------------
810
811    fwrite(PixelData, lgrTotale, 1, fp1);
812    fclose (fp1);
813    return(1);
814 }