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