]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.cxx
9d30a55d4bdeeedad3fde43672b22f05fba8325b
[gdcm.git] / src / gdcmFileHelper.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/25 15:21:20 $
7   Version:   $Revision: 1.6 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmFileHelper.h"
20 #include "gdcmGlobal.h"
21 #include "gdcmTS.h"
22 #include "gdcmDocument.h"
23 #include "gdcmDebug.h"
24 #include "gdcmUtil.h"
25 #include "gdcmBinEntry.h"
26 #include "gdcmValEntry.h"
27 #include "gdcmContentEntry.h"
28 #include "gdcmFile.h"
29 #include "gdcmPixelReadConvert.h"
30 #include "gdcmPixelWriteConvert.h"
31 #include "gdcmDocEntryArchive.h"
32
33 #include <fstream>
34
35 namespace gdcm 
36 {
37 typedef std::pair<TagDocEntryHT::iterator,TagDocEntryHT::iterator> IterHT;
38
39 //-------------------------------------------------------------------------
40 // Constructor / Destructor
41 /**
42  * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
43  *        file (gdcm::File only deals with the ... header)
44  *        Opens (in read only and when possible) an existing file and checks
45  *        for DICOM compliance. Returns NULL on failure.
46  *        It will be up to the user to load the pixels into memory
47  * \note  the in-memory representation of all available tags found in
48  *        the DICOM header is post-poned to first header information access.
49  *        This avoid a double parsing of public part of the header when
50  *        one sets an a posteriori shadow dictionary (efficiency can be
51  *        seen as a side effect).   
52  */
53 FileHelper::FileHelper( )
54 {
55    FileInternal = new File( );
56    SelfHeader = true;
57    Initialize();
58 }
59
60 /**
61  * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
62  *        file (File only deals with the ... header)
63  *        Opens (in read only and when possible) an existing file and checks
64  *        for DICOM compliance. Returns NULL on failure.
65  *        It will be up to the user to load the pixels into memory
66  * \note  the in-memory representation of all available tags found in
67  *        the DICOM header is post-poned to first header information access.
68  *        This avoid a double parsing of public part of the header when
69  *        user sets an a posteriori shadow dictionary (efficiency can be
70  *        seen as a side effect).   
71  * @param header already built Header
72  */
73 FileHelper::FileHelper(File *header)
74 {
75    FileInternal = header;
76    SelfHeader = false;
77    Initialize();
78 }
79
80 /**
81  * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
82  *        file (gdcm::File only deals with the ... header)
83  *        Opens (in read only and when possible) an existing file and checks
84  *        for DICOM compliance. Returns NULL on failure.
85  *        It will be up to the user to load the pixels into memory
86  * \note  the in-memory representation of all available tags found in
87  *        the DICOM header is post-poned to first header information access.
88  *        This avoid a double parsing of public part of the header when
89  *        one sets an a posteriori shadow dictionary (efficiency can be
90  *        seen as a side effect).   
91  * @param filename file to be opened for parsing
92  */
93 FileHelper::FileHelper(std::string const & filename )
94 {
95    FileInternal = new File( filename );
96    SelfHeader = true;
97    Initialize();
98 }
99
100 /**
101  * \brief canonical destructor
102  * \note  If the header (gdcm::File) was created by the FileHelper constructor,
103  *        it is destroyed by the FileHelper
104  */
105 FileHelper::~FileHelper()
106
107    if( PixelReadConverter )
108    {
109       delete PixelReadConverter;
110    }
111    if( PixelWriteConverter )
112    {
113       delete PixelWriteConverter;
114    }
115    if( Archive )
116    {
117       delete Archive;
118    }
119
120    if( SelfHeader )
121    {
122       delete FileInternal;
123    }
124    FileInternal = 0;
125 }
126
127 //-----------------------------------------------------------------------------
128 // Print
129 void FileHelper::Print(std::ostream &os, std::string const &)
130 {
131    FileInternal->SetPrintLevel(PrintLevel);
132    FileInternal->Print(os);
133
134    PixelReadConverter->SetPrintLevel(PrintLevel);
135    PixelReadConverter->Print(os);
136 }
137
138 //-----------------------------------------------------------------------------
139 // Public
140 /**
141  * \brief   Get the size of the image data
142  *          If the image can be RGB (with a lut or by default), the size 
143  *          corresponds to the RGB image
144  *         (use GetImageDataRawSize if you want to be sure to get *only*
145  *          the size of the pixels)
146  * @return  The image size
147  */
148 size_t FileHelper::GetImageDataSize()
149 {
150    if ( PixelWriteConverter->GetUserData() )
151    {
152       return PixelWriteConverter->GetUserDataSize();
153    }
154
155    return PixelReadConverter->GetRGBSize();
156 }
157
158 /**
159  * \brief   Get the size of the image data
160  *          If the image could be converted to RGB using a LUT, 
161  *          this transformation is not taken into account by GetImageDataRawSize
162  *          (use GetImageDataSize if you wish)
163  * @return  The raw image size
164  */
165 size_t FileHelper::GetImageDataRawSize()
166 {
167    if ( PixelWriteConverter->GetUserData() )
168    {
169       return PixelWriteConverter->GetUserDataSize();
170    }
171
172    return PixelReadConverter->GetRawSize();
173 }
174
175 /**
176  * \brief   - Allocates necessary memory, 
177  *          - Reads the pixels from disk (uncompress if necessary),
178  *          - Transforms YBR pixels, if any, into RGB pixels
179  *          - Transforms 3 planes R, G, B, if any, into a single RGB Plane
180  *          - Transforms single Grey plane + 3 Palettes into a RGB Plane
181  *          - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
182  * @return  Pointer to newly allocated pixel data.
183  *          NULL if alloc fails 
184  */
185 uint8_t *FileHelper::GetImageData()
186 {
187    if ( PixelWriteConverter->GetUserData() )
188    {
189       return PixelWriteConverter->GetUserData();
190    }
191
192    if ( ! GetRaw() )
193    {
194       // If the decompression failed nothing can be done.
195       return 0;
196    }
197
198    if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
199    {
200       return PixelReadConverter->GetRGB();
201    }
202    else
203    {
204       // When no LUT or LUT conversion fails, return the Raw
205       return PixelReadConverter->GetRaw();
206    }
207 }
208
209 /**
210  * \brief   Allocates necessary memory, 
211  *          Transforms YBR pixels (if any) into RGB pixels
212  *          Transforms 3 planes R, G, B  (if any) into a single RGB Plane
213  *          Copies the pixel data (image[s]/volume[s]) to newly allocated zone. 
214  *          DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
215  * @return  Pointer to newly allocated pixel data.
216  * \        NULL if alloc fails 
217  */
218 uint8_t *FileHelper::GetImageDataRaw ()
219 {
220    return GetRaw();
221 }
222
223 /**
224  * \brief
225  *          Read the pixels from disk (uncompress if necessary),
226  *          Transforms YBR pixels, if any, into RGB pixels
227  *          Transforms 3 planes R, G, B, if any, into a single RGB Plane
228  *          Transforms single Grey plane + 3 Palettes into a RGB Plane   
229  *          Copies at most MaxSize bytes of pixel data to caller allocated
230  *          memory space.
231  * \warning This function allows people that want to build a volume
232  *          from an image stack *not to* have, first to get the image pixels, 
233  *          and then move them to the volume area.
234  *          It's absolutely useless for any VTK user since vtk chooses 
235  *          to invert the lines of an image, that is the last line comes first
236  *          (for some axis related reasons?). Hence he will have 
237  *          to load the image line by line, starting from the end.
238  *          VTK users have to call GetImageData
239  *     
240  * @param   destination Address (in caller's memory space) at which the
241  *          pixel data should be copied
242  * @param   maxSize Maximum number of bytes to be copied. When MaxSize
243  *          is not sufficient to hold the pixel data the copy is not
244  *          executed (i.e. no partial copy).
245  * @return  On success, the number of bytes actually copied. Zero on
246  *          failure e.g. MaxSize is lower than necessary.
247  */
248 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
249 {
250    if ( ! GetRaw() )
251    {
252       // If the decompression failed nothing can be done.
253       return 0;
254    }
255
256    if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
257    {
258       if ( PixelReadConverter->GetRGBSize() > maxSize )
259       {
260          gdcmVerboseMacro( "Pixel data bigger than caller's expected MaxSize");
261          return 0;
262       }
263       memcpy( destination,
264               (void*)PixelReadConverter->GetRGB(),
265               PixelReadConverter->GetRGBSize() );
266       return PixelReadConverter->GetRGBSize();
267    }
268
269    // Either no LUT conversion necessary or LUT conversion failed
270    if ( PixelReadConverter->GetRawSize() > maxSize )
271    {
272       gdcmVerboseMacro( "Pixel data bigger than caller's expected MaxSize");
273       return 0;
274    }
275    memcpy( destination,
276            (void*)PixelReadConverter->GetRaw(),
277            PixelReadConverter->GetRawSize() );
278    return PixelReadConverter->GetRawSize();
279 }
280
281 /**
282  * \brief   Points the internal pointer to the callers inData
283  *          image representation, BUT WITHOUT COPYING THE DATA.
284  *          'image' Pixels are presented as C-like 2D arrays : line per line.
285  *          'volume'Pixels are presented as C-like 3D arrays : plane per plane 
286  * \warning Since the pixels are not copied, it is the caller's responsability
287  *          not to deallocate its data before gdcm uses them (e.g. with
288  *          the Write() method.
289  * @param inData user supplied pixel area
290  * @param expectedSize total image size, in Bytes
291  *
292  * @return boolean
293  */
294 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
295 {
296    SetUserData(inData,expectedSize);
297 }
298
299 /**
300  * \brief   Set the image data defined by the user
301  * \warning When writting the file, this data are get as default data to write
302  */
303 void FileHelper::SetUserData(uint8_t *data, size_t expectedSize)
304 {
305    PixelWriteConverter->SetUserData(data,expectedSize);
306 }
307
308 /**
309  * \brief   Get the image data defined by the user
310  * \warning When writting the file, this data are get as default data to write
311  */
312 uint8_t *FileHelper::GetUserData()
313 {
314    return PixelWriteConverter->GetUserData();
315 }
316
317 /**
318  * \brief   Get the image data size defined by the user
319  * \warning When writting the file, this data are get as default data to write
320  */
321 size_t FileHelper::GetUserDataSize()
322 {
323    return PixelWriteConverter->GetUserDataSize();
324 }
325
326 /**
327  * \brief   Get the image data from the file.
328  *          If a LUT is found, the data are expanded to be RGB
329  */
330 uint8_t *FileHelper::GetRGBData()
331 {
332    return PixelReadConverter->GetRGB();
333 }
334
335 /**
336  * \brief   Get the image data size from the file.
337  *          If a LUT is found, the data are expanded to be RGB
338  */
339 size_t FileHelper::GetRGBDataSize()
340 {
341    return PixelReadConverter->GetRGBSize();
342 }
343
344 /**
345  * \brief   Get the image data from the file.
346  *          If a LUT is found, the data are not expanded !
347  */
348 uint8_t *FileHelper::GetRawData()
349 {
350    return PixelReadConverter->GetRaw();
351 }
352
353 /**
354  * \brief   Get the image data size from the file.
355  *          If a LUT is found, the data are not expanded !
356  */
357 size_t FileHelper::GetRawDataSize()
358 {
359    return PixelReadConverter->GetRawSize();
360 }
361
362 /**
363  * \brief Writes on disk A SINGLE Dicom file
364  *        NO test is performed on  processor "Endiannity".
365  *        It's up to the user to call his Reader properly
366  * @param fileName name of the file to be created
367  *                 (any already existing file is over written)
368  * @return false if write fails
369  */
370
371 bool FileHelper::WriteRawData(std::string const &fileName)
372 {
373   std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
374    if (!fp1)
375    {
376       gdcmVerboseMacro( "Fail to open (write) file:" << fileName.c_str());
377       return false;
378    }
379
380    if( PixelWriteConverter->GetUserData() )
381    {
382       fp1.write( (char*)PixelWriteConverter->GetUserData(), 
383                  PixelWriteConverter->GetUserDataSize() );
384    }
385    else if ( PixelReadConverter->GetRGB() )
386    {
387       fp1.write( (char*)PixelReadConverter->GetRGB(), 
388                  PixelReadConverter->GetRGBSize());
389    }
390    else if ( PixelReadConverter->GetRaw() )
391    {
392       fp1.write( (char*)PixelReadConverter->GetRaw(), 
393                  PixelReadConverter->GetRawSize());
394    }
395    else
396    {
397       gdcmErrorMacro( "Nothing written." );
398    }
399
400    fp1.close();
401
402    return true;
403 }
404
405 /**
406  * \brief Writes on disk A SINGLE Dicom file, 
407  *        using the Implicit Value Representation convention
408  *        NO test is performed on  processor "Endiannity".
409  * @param fileName name of the file to be created
410  *                 (any already existing file is overwritten)
411  * @return false if write fails
412  */
413
414 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
415 {
416    SetWriteTypeToDcmImplVR();
417    return Write(fileName);
418 }
419
420 /**
421 * \brief Writes on disk A SINGLE Dicom file, 
422  *        using the Explicit Value Representation convention
423  *        NO test is performed on  processor "Endiannity". 
424  * @param fileName name of the file to be created
425  *                 (any already existing file is overwritten)
426  * @return false if write fails
427  */
428
429 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
430 {
431    SetWriteTypeToDcmExplVR();
432    return Write(fileName);
433 }
434
435 /**
436  * \brief Writes on disk A SINGLE Dicom file, 
437  *        using the ACR-NEMA convention
438  *        NO test is performed on  processor "Endiannity".
439  *        (a l'attention des logiciels cliniques 
440  *        qui ne prennent en entrée QUE des images ACR ...
441  * \warning if a DICOM_V3 header is supplied,
442  *         groups < 0x0008 and shadow groups are ignored
443  * \warning NO TEST is performed on processor "Endiannity".
444  * @param fileName name of the file to be created
445  *                 (any already existing file is overwritten)
446  * @return false if write fails
447  */
448
449 bool FileHelper::WriteAcr (std::string const &fileName)
450 {
451    SetWriteTypeToAcr();
452    return Write(fileName);
453 }
454
455 /**
456  * \brief Writes on disk A SINGLE Dicom file, 
457  * @param fileName name of the file to be created
458  *                 (any already existing file is overwritten)
459  * @return false if write fails
460  */
461 bool FileHelper::Write(std::string const &fileName)
462 {
463    switch(WriteType)
464    {
465       case ImplicitVR:
466          SetWriteFileTypeToImplicitVR();
467          break;
468       case ExplicitVR:
469          SetWriteFileTypeToExplicitVR();
470          break;
471       case ACR:
472       case ACR_LIBIDO:
473          SetWriteFileTypeToACR();
474          break;
475       default:
476          SetWriteFileTypeToExplicitVR();
477    }
478
479    // --------------------------------------------------------------
480    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
481    //
482    // if recognition code tells us we dealt with a LibIDO image
483    // we reproduce on disk the switch between lineNumber and columnNumber
484    // just before writting ...
485    /// \todo the best trick would be *change* the recognition code
486    ///       but pb expected if user deals with, e.g. COMPLEX images
487    if( WriteType == ACR_LIBIDO )
488    {
489       SetWriteToLibido();
490    }
491    else
492    {
493       SetWriteToNoLibido();
494    }
495    // ----------------- End of Special Patch ----------------
496   
497    switch(WriteMode)
498    {
499       case WMODE_RAW :
500          SetWriteToRaw();
501          break;
502       case WMODE_RGB :
503          SetWriteToRGB();
504          break;
505    }
506
507    bool check = CheckWriteIntegrity();
508    if(check)
509    {
510       check = FileInternal->Write(fileName,WriteType);
511    }
512
513    RestoreWrite();
514    RestoreWriteFileType();
515
516    // --------------------------------------------------------------
517    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
518    // 
519    // ...and we restore the header to be Dicom Compliant again 
520    // just after writting
521    RestoreWriteOfLibido();
522    // ----------------- End of Special Patch ----------------
523
524    return check;
525 }
526
527 /**
528  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
529  *          through it's (group, element) and modifies it's content with
530  *          the given value.
531  * @param   content new value (string) to substitute with
532  * @param   group  group number of the Dicom Element to modify
533  * @param   elem element number of the Dicom Element to modify
534  */
535 bool FileHelper::SetEntryValue(std::string const &content,
536                     uint16_t group, uint16_t elem)
537
538    return FileInternal->SetEntryValue(content,group,elem);
539 }
540
541
542 /**
543  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
544  *          through it's (group, element) and modifies it's content with
545  *          the given value.
546  * @param   content new value (void*  -> uint8_t*) to substitute with
547  * @param   lgth new value length
548  * @param   group  group number of the Dicom Element to modify
549  * @param   elem element number of the Dicom Element to modify
550  */
551 bool FileHelper::SetEntryBinArea(uint8_t *content, int lgth,
552                                  uint16_t group, uint16_t elem)
553 {
554    return FileInternal->SetEntryBinArea(content,lgth,group,elem);
555 }
556
557 /**
558  * \brief   Modifies the value of a given DocEntry (Dicom entry)
559  *          when it exists. Create it with the given value when unexistant.
560  * @param   content (string) Value to be set
561  * @param   group   Group number of the Entry 
562  * @param   elem  Element number of the Entry
563  * \return  pointer to the modified/created Dicom entry (NULL when creation
564  *          failed).
565  */ 
566 bool FileHelper::Insert(std::string const &content,
567                         uint16_t group, uint16_t elem)
568 {
569    return FileInternal->Insert(content,group,elem) != NULL;
570 }
571
572 /*
573  * \brief   Modifies the value of a given DocEntry (Dicom entry)
574  *          when it exists. Create it with the given value when unexistant.
575  *          A copy of the binArea is made to be kept in the Document.
576  * @param   binArea (binary) value to be set
577  * @param   group   Group number of the Entry 
578  * @param   elem  Element number of the Entry
579  * \return  pointer to the modified/created Dicom entry (NULL when creation
580  *          failed).
581  */
582 bool FileHelper::Insert(uint8_t *binArea, int lgth,
583                         uint16_t group, uint16_t elem)
584 {
585    return FileInternal->Insert(binArea,lgth,group,elem) != NULL;
586 }
587
588 /**
589  * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
590  */
591 uint8_t* FileHelper::GetLutRGBA()
592 {
593    return PixelReadConverter->GetLutRGBA();
594 }
595
596 //-----------------------------------------------------------------------------
597 // Protected
598
599 /**
600  * \brief Check the write integrity
601  *
602  * The tests made are :
603  *  - verify the size of the image to write with the possible write
604  *    when the user set an image data
605  * @return true if check is successfull
606  */
607 bool FileHelper::CheckWriteIntegrity()
608 {
609    if(PixelWriteConverter->GetUserData())
610    {
611       int numberBitsAllocated = FileInternal->GetBitsAllocated();
612       if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
613       {
614          numberBitsAllocated = 16;
615       }
616
617       size_t decSize = FileInternal->GetXSize()
618                     * FileInternal->GetYSize() 
619                     * FileInternal->GetZSize()
620                     * ( numberBitsAllocated / 8 )
621                     * FileInternal->GetSamplesPerPixel();
622       size_t rgbSize = decSize;
623       if( FileInternal->HasLUT() )
624          rgbSize = decSize * 3;
625
626       switch(WriteMode)
627       {
628          case WMODE_RAW :
629             if( decSize!=PixelWriteConverter->GetUserDataSize() )
630             {
631                gdcmVerboseMacro( "Data size (Raw) is incorrect. Should be " 
632                            << decSize << " / Found :" 
633                            << PixelWriteConverter->GetUserDataSize() );
634                return false;
635             }
636             break;
637          case WMODE_RGB :
638             if( rgbSize!=PixelWriteConverter->GetUserDataSize() )
639             {
640                gdcmVerboseMacro( "Data size (RGB) is incorrect. Should be " 
641                           << decSize << " / Found " 
642                           << PixelWriteConverter->GetUserDataSize() );
643                return false;
644             }
645             break;
646       }
647    }
648    
649    return true;
650 }
651
652 /**
653  * \brief   
654  */ 
655 void FileHelper::SetWriteToRaw()
656 {
657    if( FileInternal->GetNumberOfScalarComponents() == 3 
658     && !FileInternal->HasLUT())
659    {
660       SetWriteToRGB();
661    } 
662    else
663    {
664       ValEntry *photInt = CopyValEntry(0x0028,0x0004);
665       if(FileInternal->HasLUT())
666       {
667          photInt->SetValue("PALETTE COLOR ");
668       }
669       else
670       {
671          photInt->SetValue("MONOCHROME1 ");
672       }
673
674       PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
675                                        PixelReadConverter->GetRawSize());
676
677       BinEntry *pixel = 
678          CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
679       pixel->SetValue(GDCM_BINLOADED);
680       pixel->SetBinArea(PixelWriteConverter->GetData(),false);
681       pixel->SetLength(PixelWriteConverter->GetDataSize());
682
683       Archive->Push(photInt);
684       Archive->Push(pixel);
685    }
686 }
687
688 /**
689  * \brief   
690  */ 
691 void FileHelper::SetWriteToRGB()
692 {
693    if(FileInternal->GetNumberOfScalarComponents()==3)
694    {
695       PixelReadConverter->BuildRGBImage();
696       
697       ValEntry *spp = CopyValEntry(0x0028,0x0002);
698       spp->SetValue("3 ");
699
700       ValEntry *planConfig = CopyValEntry(0x0028,0x0006);
701       planConfig->SetValue("0 ");
702
703       ValEntry *photInt = CopyValEntry(0x0028,0x0004);
704       photInt->SetValue("RGB ");
705
706       if(PixelReadConverter->GetRGB())
707       {
708          PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
709                                           PixelReadConverter->GetRGBSize());
710       }
711       else // Raw data
712       {
713          PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
714                                           PixelReadConverter->GetRawSize());
715       }
716
717       BinEntry *pixel = 
718          CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
719       pixel->SetValue(GDCM_BINLOADED);
720       pixel->SetBinArea(PixelWriteConverter->GetData(),false);
721       pixel->SetLength(PixelWriteConverter->GetDataSize());
722
723       Archive->Push(spp);
724       Archive->Push(planConfig);
725       Archive->Push(photInt);
726       Archive->Push(pixel);
727
728       // Remove any LUT
729       Archive->Push(0x0028,0x1101);
730       Archive->Push(0x0028,0x1102);
731       Archive->Push(0x0028,0x1103);
732       Archive->Push(0x0028,0x1201);
733       Archive->Push(0x0028,0x1202);
734       Archive->Push(0x0028,0x1203);
735
736       // For old ACR-NEMA
737       // Thus, we have a RGB image and the bits allocated = 24 and 
738       // samples per pixels = 1 (in the read file)
739       if(FileInternal->GetBitsAllocated()==24) 
740       {
741          ValEntry *bitsAlloc = CopyValEntry(0x0028,0x0100);
742          bitsAlloc->SetValue("8 ");
743
744          ValEntry *bitsStored = CopyValEntry(0x0028,0x0101);
745          bitsStored->SetValue("8 ");
746
747          ValEntry *highBit = CopyValEntry(0x0028,0x0102);
748          highBit->SetValue("7 ");
749
750          Archive->Push(bitsAlloc);
751          Archive->Push(bitsStored);
752          Archive->Push(highBit);
753       }
754    }
755    else
756    {
757       SetWriteToRaw();
758    }
759 }
760
761 /**
762  * \brief   
763  */ 
764 void FileHelper::RestoreWrite()
765 {
766    Archive->Restore(0x0028,0x0002);
767    Archive->Restore(0x0028,0x0004);
768    Archive->Restore(0x0028,0x0006);
769    Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
770
771    // For old ACR-NEMA (24 bits problem)
772    Archive->Restore(0x0028,0x0100);
773    Archive->Restore(0x0028,0x0101);
774    Archive->Restore(0x0028,0x0102);
775
776    // For the LUT
777    Archive->Restore(0x0028,0x1101);
778    Archive->Restore(0x0028,0x1102);
779    Archive->Restore(0x0028,0x1103);
780    Archive->Restore(0x0028,0x1201);
781    Archive->Restore(0x0028,0x1202);
782    Archive->Restore(0x0028,0x1203);
783 }
784
785 /**
786  * \brief   
787  */ 
788 void FileHelper::SetWriteFileTypeToACR()
789 {
790    Archive->Push(0x0002,0x0010);
791 }
792
793 /**
794  * \brief   
795  */ 
796 void FileHelper::SetWriteFileTypeToExplicitVR()
797 {
798    std::string ts = Util::DicomString( 
799       Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
800
801    ValEntry *tss = CopyValEntry(0x0002,0x0010);
802    tss->SetValue(ts);
803
804    Archive->Push(tss);
805 }
806
807 /**
808  * \brief   
809  */ 
810 void FileHelper::SetWriteFileTypeToImplicitVR()
811 {
812    std::string ts = Util::DicomString(
813       Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
814
815    ValEntry *tss = CopyValEntry(0x0002,0x0010);
816    tss->SetValue(ts);
817
818    Archive->Push(tss);
819 }
820
821
822 /**
823  * \brief   
824  */ 
825 void FileHelper::RestoreWriteFileType()
826 {
827    Archive->Restore(0x0002,0x0010);
828 }
829
830 void FileHelper::SetWriteToLibido()
831 {
832    ValEntry *oldRow = dynamic_cast<ValEntry *>
833                 (FileInternal->GetDocEntry(0x0028, 0x0010));
834    ValEntry *oldCol = dynamic_cast<ValEntry *>
835                 (FileInternal->GetDocEntry(0x0028, 0x0011));
836    
837    if( oldRow && oldCol )
838    {
839       std::string rows, columns; 
840
841       ValEntry *newRow=new ValEntry(oldRow->GetDictEntry());
842       ValEntry *newCol=new ValEntry(oldCol->GetDictEntry());
843
844       newRow->Copy(oldCol);
845       newCol->Copy(oldRow);
846
847       newRow->SetValue(oldCol->GetValue());
848       newCol->SetValue(oldRow->GetValue());
849
850       Archive->Push(newRow);
851       Archive->Push(newCol);
852    }
853
854    ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
855    libidoCode->SetValue("ACRNEMA_LIBIDO_1.1");
856    Archive->Push(libidoCode);
857 }
858
859 /**
860  * \brief   
861  */ 
862 void FileHelper::SetWriteToNoLibido()
863 {
864    ValEntry *recCode = dynamic_cast<ValEntry *>
865                 (FileInternal->GetDocEntry(0x0008,0x0010));
866    if( recCode )
867    {
868       if( recCode->GetValue() == "ACRNEMA_LIBIDO_1.1" )
869       {
870          ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
871          libidoCode->SetValue("");
872          Archive->Push(libidoCode);
873       }
874    }
875 }
876
877 /**
878  * \brief   
879  */ 
880 void FileHelper::RestoreWriteOfLibido()
881 {
882    Archive->Restore(0x0028,0x0010);
883    Archive->Restore(0x0028,0x0011);
884    Archive->Restore(0x0008,0x0010);
885 }
886
887 ValEntry *FileHelper::CopyValEntry(uint16_t group,uint16_t elem)
888 {
889    DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
890    ValEntry *newE;
891
892    if(oldE)
893    {
894       newE = new ValEntry(oldE->GetDictEntry());
895       newE->Copy(oldE);
896    }
897    else
898    {
899       newE = GetFile()->NewValEntry(group,elem);
900    }
901
902    return newE;
903 }
904
905 /**
906  * \brief   Modifies the value of a given Bin Entry (Dicom Element)
907  *          when it exists. Create it with the given value when unexistant.
908  * @param   group   Group number of the Entry 
909  * @param   elem  Element number of the Entry
910  * \return  pointer to the modified/created Bin Entry (NULL when creation
911  *          failed).
912  */ 
913 BinEntry *FileHelper::CopyBinEntry(uint16_t group,uint16_t elem)
914 {
915    DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
916    BinEntry *newE;
917
918    if(oldE)
919    {
920       newE = new BinEntry(oldE->GetDictEntry());
921       newE->Copy(oldE);
922    }
923    else
924    {
925       newE = GetFile()->NewBinEntry(group,elem);
926    }
927
928    return newE;
929 }
930
931 //-----------------------------------------------------------------------------
932 // Protected
933 /**
934  * \brief Factorization for various forms of constructors.
935  */
936 void FileHelper::Initialize()
937 {
938    WriteMode = WMODE_RAW;
939    WriteType = ExplicitVR;
940
941    PixelReadConverter = new PixelReadConvert;
942    PixelWriteConverter = new PixelWriteConvert;
943    Archive = new DocEntryArchive( FileInternal );
944
945    if ( FileInternal->IsReadable() )
946    {
947       PixelReadConverter->GrabInformationsFromHeader( FileInternal );
948    }
949 }
950
951 /**
952  * \brief   
953  */ 
954 uint8_t *FileHelper::GetRaw()
955 {
956    uint8_t *raw = PixelReadConverter->GetRaw();
957    if ( ! raw )
958    {
959       // The Raw image migth not be loaded yet:
960       std::ifstream *fp = FileInternal->OpenFile();
961       PixelReadConverter->ReadAndDecompressPixelData( fp );
962       if(fp) 
963          FileInternal->CloseFile();
964
965       raw = PixelReadConverter->GetRaw();
966       if ( ! raw )
967       {
968          gdcmVerboseMacro( "Read/decompress of pixel data apparently went wrong.");
969          return 0;
970       }
971    }
972
973    return raw;
974 }
975
976 //-----------------------------------------------------------------------------
977 // Private
978
979 //-----------------------------------------------------------------------------
980 } // end namespace gdcm
981