]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.cxx
Initialize UserFunction pointer to 0, even for 'deprecated' form of constructor.
[gdcm.git] / src / gdcmFileHelper.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.cxx,v $
5   Language:  C++
6
7   Date:      $Date: 2005/08/19 13:12:15 $
8   Version:   $Revision: 1.52 $
9                                                                                 
10   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
11   l'Image). All rights reserved. See Doc/License.txt or
12   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13                                                                                 
14      This software is distributed WITHOUT ANY WARRANTY; without even
15      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16      PURPOSE.  See the above copyright notices for more information.
17                                                                                 
18 =========================================================================*/
19
20 #include "gdcmFileHelper.h"
21 #include "gdcmGlobal.h"
22 #include "gdcmTS.h"
23 #include "gdcmDocument.h"
24 #include "gdcmDebug.h"
25 #include "gdcmUtil.h"
26 #include "gdcmBinEntry.h"
27 #include "gdcmValEntry.h"
28 #include "gdcmSeqEntry.h"
29 #include "gdcmSQItem.h"
30 #include "gdcmContentEntry.h"
31 #include "gdcmFile.h"
32 #include "gdcmPixelReadConvert.h"
33 #include "gdcmPixelWriteConvert.h"
34 #include "gdcmDocEntryArchive.h"
35 #include "gdcmDictSet.h"
36
37 #include <fstream>
38
39 /*
40 // ----------------------------- WARNING -------------------------
41
42 These lines will be moved to the document-to-be 'User's Guide'
43
44 // To read an image, user needs a gdcm::File
45 gdcm::File *f1 = new gdcm::File(fileName);
46 // user can now check some values
47 std::string v = f1->GetEntryValue(groupNb,ElementNb);
48 // to get the pixels, user needs a gdcm::FileHelper
49 gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
50 // user may ask not to convert Palette to RGB
51 uint8_t *pixels = fh1->GetImageDataRaw();
52 int imageLength = fh1->GetImageDataRawSize();
53 // He can now use the pixels, create a new image, ...
54 uint8_t *userPixels = ...
55
56 To re-write the image, user re-uses the gdcm::FileHelper
57
58 fh1->SetImageData( userPixels, userPixelsLength);
59 fh1->SetTypeToRaw(); // Even if it was possible to convert Palette to RGB
60                      // (WriteMode is set)
61  
62 fh1->SetWriteTypeToDcmExpl(); // he wants Explicit Value Representation
63                               // Little Endian is the default
64                               // no other value is allowed
65                                 (-->SetWriteType(ExplicitVR);)
66                                    -->WriteType = ExplicitVR;
67 fh1->Write(newFileName);      // overwrites the file, if any
68
69 // or :
70 fh1->WriteDcmExplVR(newFileName);
71
72
73 // ----------------------------- WARNING -------------------------
74
75 These lines will be moved to the document-to-be 'Developer's Guide'
76
77 WriteMode : WMODE_RAW / WMODE_RGB
78 WriteType : ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO
79
80 fh1->Write(newFileName);
81    SetWriteFileTypeToImplicitVR() / SetWriteFileTypeToExplicitVR();
82    (modifies TransferSyntax)
83    SetWriteToRaw(); / SetWriteToRGB();
84       (modifies, when necessary : photochromatic interpretation, 
85          samples per pixel, Planar configuration, 
86          bits allocated, bits stored, high bit -ACR 24 bits-
87          Pixels element VR, pushes out the LUT )
88    CheckWriteIntegrity();
89       (checks user given pixels length)
90    FileInternal->Write(fileName,WriteType)
91    fp = opens file(fileName);
92    ComputeGroup0002Length(writetype);
93    BitsAllocated 12->16
94       RemoveEntryNoDestroy(palettes, etc)
95       Document::WriteContent(fp, writetype);
96    RestoreWrite();
97       (moves back to the File all the archived elements)
98    RestoreWriteFileType();
99       (pushes back group 0002, with TransferSyntax)
100 */
101
102
103
104
105 namespace gdcm 
106 {
107 //-------------------------------------------------------------------------
108 // Constructor / Destructor
109 /**
110  * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
111  *        file (gdcm::File only deals with the ... header)
112  *        Opens (in read only and when possible) an existing file and checks
113  *        for DICOM compliance. Returns NULL on failure.
114  *        It will be up to the user to load the pixels into memory
115  * \note  the in-memory representation of all available tags found in
116  *        the DICOM header is post-poned to first header information access.
117  *        This avoid a double parsing of public part of the header when
118  *        one sets an a posteriori shadow dictionary (efficiency can be
119  *        seen as a side effect).   
120  */
121 FileHelper::FileHelper( )
122 {
123    FileInternal = new File( );
124    SelfHeader = true;
125    Initialize();
126 }
127
128 /**
129  * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
130  *        file (File only deals with the ... header)
131  *        Opens (in read only and when possible) an existing file and checks
132  *        for DICOM compliance. Returns NULL on failure.
133  *        It will be up to the user to load the pixels into memory
134  * \note  the in-memory representation of all available tags found in
135  *        the DICOM header is post-poned to first header information access.
136  *        This avoid a double parsing of public part of the header when
137  *        user sets an a posteriori shadow dictionary (efficiency can be
138  *        seen as a side effect).   
139  * @param header already built Header
140  */
141 FileHelper::FileHelper(File *header)
142 {
143    FileInternal = header;
144    SelfHeader = false;
145    Initialize();
146 }
147
148
149 /**
150  * \brief canonical destructor
151  * \note  If the header (gdcm::File) was created by the FileHelper constructor,
152  *        it is destroyed by the FileHelper
153  */
154 FileHelper::~FileHelper()
155
156    if ( PixelReadConverter )
157    {
158       delete PixelReadConverter;
159    }
160    if ( PixelWriteConverter )
161    {
162       delete PixelWriteConverter;
163    }
164    if ( Archive )
165    {
166       delete Archive;
167    }
168
169    if ( SelfHeader )
170    {
171       delete FileInternal;
172    }
173    FileInternal = 0;
174 }
175
176 //-----------------------------------------------------------------------------
177 // Public
178
179 /**
180  * \brief Sets the LoadMode of the internal gdcm::File as a boolean string. 
181  *        NO_SEQ, NO_SHADOW, NO_SHADOWSEQ
182  *... (nothing more, right now)
183  *        WARNING : before using NO_SHADOW, be sure *all* your files
184  *        contain accurate values in the 0x0000 element (if any) 
185  *        of *each* Shadow Group. The parser will fail if the size is wrong !
186  * @param   loadMode Load mode to be used    
187  */
188 void FileHelper::SetLoadMode(int loadMode) 
189
190    GetFile()->SetLoadMode( loadMode ); 
191 }
192 /**
193  * \brief Sets the LoadMode of the internal gdcm::File
194  * @param  fileName name of the file to be open  
195  */
196 void FileHelper::SetFileName(std::string const &fileName)
197 {
198    FileInternal->SetFileName( fileName );
199 }
200
201 /**
202  * \brief   Loader  
203  * @return false if file cannot be open or no swap info was found,
204  *         or no tag was found.
205  */
206 bool FileHelper::Load()
207
208    return FileInternal->Load();
209 }
210
211 /**
212  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
213  *          through it's (group, element) and modifies it's content with
214  *          the given value.
215  * @param   content new value (string) to substitute with
216  * @param   group  group number of the Dicom Element to modify
217  * @param   elem element number of the Dicom Element to modify
218  * \return  false if DocEntry not found
219  */
220 bool FileHelper::SetValEntry(std::string const &content,
221                              uint16_t group, uint16_t elem)
222
223    return FileInternal->SetValEntry(content, group, elem);
224 }
225
226
227 /**
228  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
229  *          through it's (group, element) and modifies it's content with
230  *          the given value.
231  * @param   content new value (void*  -> uint8_t*) to substitute with
232  * @param   lgth new value length
233  * @param   group  group number of the Dicom Element to modify
234  * @param   elem element number of the Dicom Element to modify
235  * \return  false if DocEntry not found
236  */
237 bool FileHelper::SetBinEntry(uint8_t *content, int lgth,
238                              uint16_t group, uint16_t elem)
239 {
240    return FileInternal->SetBinEntry(content, lgth, group, elem);
241 }
242
243 /**
244  * \brief   Modifies the value of a given DocEntry (Dicom entry)
245  *          when it exists. Creates it with the given value when unexistant.
246  * @param   content (string)value to be set
247  * @param   group   Group number of the Entry 
248  * @param   elem  Element number of the Entry
249  * \return  pointer to the modified/created Dicom entry (NULL when creation
250  *          failed).
251  */ 
252 ValEntry *FileHelper::InsertValEntry(std::string const &content,
253                                      uint16_t group, uint16_t elem)
254 {
255    return FileInternal->InsertValEntry(content, group, elem);
256 }
257
258 /**
259  * \brief   Modifies the value of a given DocEntry (Dicom entry)
260  *          when it exists. Creates it with the given value when unexistant.
261  *          A copy of the binArea is made to be kept in the Document.
262  * @param   binArea (binary)value to be set
263  * @param   lgth new value length
264  * @param   group   Group number of the Entry 
265  * @param   elem  Element number of the Entry
266  * \return  pointer to the modified/created Dicom entry (NULL when creation
267  *          failed).
268  */
269 BinEntry *FileHelper::InsertBinEntry(uint8_t *binArea, int lgth,
270                                      uint16_t group, uint16_t elem)
271 {
272    return FileInternal->InsertBinEntry(binArea, lgth, group, elem);
273 }
274
275 /**
276  * \brief   Modifies the value of a given DocEntry (Dicom entry)
277  *          when it exists. Creates it, empty (?!) when unexistant.
278  * @param   group   Group number of the Entry 
279  * @param   elem  Element number of the Entry
280  * \return  pointer to the modified/created Dicom entry (NULL when creation
281  *          failed).
282  */
283 SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
284 {
285    return FileInternal->InsertSeqEntry(group, elem);
286 }
287
288 /**
289  * \brief   Get the size of the image data
290  *          If the image can be RGB (with a lut or by default), the size 
291  *          corresponds to the RGB image
292  *         (use GetImageDataRawSize if you want to be sure to get *only*
293  *          the size of the pixels)
294  * @return  The image size
295  */
296 size_t FileHelper::GetImageDataSize()
297 {
298    if ( PixelWriteConverter->GetUserData() )
299    {
300       return PixelWriteConverter->GetUserDataSize();
301    }
302    return PixelReadConverter->GetRGBSize();
303 }
304
305 /**
306  * \brief   Get the size of the image data
307  *          If the image could be converted to RGB using a LUT, 
308  *          this transformation is not taken into account by GetImageDataRawSize
309  *          (use GetImageDataSize if you wish)
310  * @return  The raw image size
311  */
312 size_t FileHelper::GetImageDataRawSize()
313 {
314    if ( PixelWriteConverter->GetUserData() )
315    {
316       return PixelWriteConverter->GetUserDataSize();
317    }
318    return PixelReadConverter->GetRawSize();
319 }
320
321 /**
322  * \brief   - Allocates necessary memory,
323  *          - Reads the pixels from disk (uncompress if necessary),
324  *          - Transforms YBR pixels, if any, into RGB pixels,
325  *          - Transforms 3 planes R, G, B, if any, into a single RGB Plane
326  *          - Transforms single Grey plane + 3 Palettes into a RGB Plane
327  *          - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
328  * @return  Pointer to newly allocated pixel data.
329  *          NULL if alloc fails 
330  */
331 uint8_t *FileHelper::GetImageData()
332 {
333    if ( PixelWriteConverter->GetUserData() )
334    {
335       return PixelWriteConverter->GetUserData();
336    }
337
338    if ( ! GetRaw() )
339    {
340       // If the decompression failed nothing can be done.
341       return 0;
342    }
343
344    if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
345    {
346       return PixelReadConverter->GetRGB();
347    }
348    else
349    {
350       // When no LUT or LUT conversion fails, return the Raw
351       return PixelReadConverter->GetRaw();
352    }
353 }
354
355 /**
356  * \brief   Allocates necessary memory, 
357  *          Transforms YBR pixels (if any) into RGB pixels
358  *          Transforms 3 planes R, G, B  (if any) into a single RGB Plane
359  *          Copies the pixel data (image[s]/volume[s]) to newly allocated zone. 
360  *          DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
361  * @return  Pointer to newly allocated pixel data.
362  *          NULL if alloc fails 
363  */
364 uint8_t *FileHelper::GetImageDataRaw ()
365 {
366    return GetRaw();
367 }
368
369 #ifndef GDCM_LEGACY_REMOVE
370 /**
371  * \brief   Useless function, since PixelReadConverter forces us 
372  *          copy the Pixels anyway.  
373  *          Reads the pixels from disk (uncompress if necessary),
374  *          Transforms YBR pixels, if any, into RGB pixels
375  *          Transforms 3 planes R, G, B, if any, into a single RGB Plane
376  *          Transforms single Grey plane + 3 Palettes into a RGB Plane   
377  *          Copies at most MaxSize bytes of pixel data to caller allocated
378  *          memory space.
379  * \warning This function allows people that want to build a volume
380  *          from an image stack *not to* have, first to get the image pixels, 
381  *          and then move them to the volume area.
382  *          It's absolutely useless for any VTK user since vtk chooses 
383  *          to invert the lines of an image, that is the last line comes first
384  *          (for some axis related reasons?). Hence he will have 
385  *          to load the image line by line, starting from the end.
386  *          VTK users have to call GetImageData
387  *     
388  * @param   destination Address (in caller's memory space) at which the
389  *          pixel data should be copied
390  * @param   maxSize Maximum number of bytes to be copied. When MaxSize
391  *          is not sufficient to hold the pixel data the copy is not
392  *          executed (i.e. no partial copy).
393  * @return  On success, the number of bytes actually copied. Zero on
394  *          failure e.g. MaxSize is lower than necessary.
395  */
396 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
397 {
398    if ( ! GetRaw() )
399    {
400       // If the decompression failed nothing can be done.
401       return 0;
402    }
403
404    if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
405    {
406       if ( PixelReadConverter->GetRGBSize() > maxSize )
407       {
408          gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
409          return 0;
410       }
411       memcpy( destination,
412               (void*)PixelReadConverter->GetRGB(),
413               PixelReadConverter->GetRGBSize() );
414       return PixelReadConverter->GetRGBSize();
415    }
416
417    // Either no LUT conversion necessary or LUT conversion failed
418    if ( PixelReadConverter->GetRawSize() > maxSize )
419    {
420       gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
421       return 0;
422    }
423    memcpy( destination,
424            (void *)PixelReadConverter->GetRaw(),
425            PixelReadConverter->GetRawSize() );
426    return PixelReadConverter->GetRawSize();
427 }
428 #endif
429
430 /**
431  * \brief   Points the internal pointer to the callers inData
432  *          image representation, BUT WITHOUT COPYING THE DATA.
433  *          'image' Pixels are presented as C-like 2D arrays : line per line.
434  *          'volume'Pixels are presented as C-like 3D arrays : plane per plane 
435  * \warning Since the pixels are not copied, it is the caller's responsability
436  *          not to deallocate its data before gdcm uses them (e.g. with
437  *          the Write() method )
438  * @param inData user supplied pixel area (uint8_t* is just for the compiler.
439  *               user is allowed to pass any kind of pixelsn since the size is
440  *               given in bytes) 
441  * @param expectedSize total image size, *in Bytes*
442  */
443 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
444 {
445    SetUserData(inData, expectedSize);
446 }
447
448 /**
449  * \brief   Set the image data defined by the user
450  * \warning When writting the file, this data are get as default data to write
451  * @param inData user supplied pixel area (uint8_t* is just for the compiler.
452  *               user is allowed to pass any kind of pixels since the size is
453  *               given in bytes) 
454  * @param expectedSize total image size, *in Bytes* 
455  */
456 void FileHelper::SetUserData(uint8_t *inData, size_t expectedSize)
457 {
458    PixelWriteConverter->SetUserData(inData, expectedSize);
459 }
460
461 /**
462  * \brief   Get the image data defined by the user
463  * \warning When writting the file, this data are get as default data to write
464  */
465 uint8_t *FileHelper::GetUserData()
466 {
467    return PixelWriteConverter->GetUserData();
468 }
469
470 /**
471  * \brief   Get the image data size defined by the user
472  * \warning When writting the file, this data are get as default data to write
473  */
474 size_t FileHelper::GetUserDataSize()
475 {
476    return PixelWriteConverter->GetUserDataSize();
477 }
478
479 /**
480  * \brief   Get the image data from the file.
481  *          If a LUT is found, the data are expanded to be RGB
482  */
483 uint8_t *FileHelper::GetRGBData()
484 {
485    return PixelReadConverter->GetRGB();
486 }
487
488 /**
489  * \brief   Get the image data size from the file.
490  *          If a LUT is found, the data are expanded to be RGB
491  */
492 size_t FileHelper::GetRGBDataSize()
493 {
494    return PixelReadConverter->GetRGBSize();
495 }
496
497 /**
498  * \brief   Get the image data from the file.
499  *          Even when a LUT is found, the data are not expanded to RGB!
500  */
501 uint8_t *FileHelper::GetRawData()
502 {
503    return PixelReadConverter->GetRaw();
504 }
505
506 /**
507  * \brief   Get the image data size from the file.
508  *          Even when a LUT is found, the data are not expanded to RGB!
509  */
510 size_t FileHelper::GetRawDataSize()
511 {
512    return PixelReadConverter->GetRawSize();
513 }
514
515 /**
516  * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
517  */
518 uint8_t* FileHelper::GetLutRGBA()
519 {
520    if ( PixelReadConverter->GetLutRGBA() ==0 )
521       PixelReadConverter->BuildLUTRGBA();
522    return PixelReadConverter->GetLutRGBA();
523 }
524
525 /**
526  * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Number
527  */
528 int FileHelper::GetLutItemNumber()
529 {
530    return PixelReadConverter->GetLutItemNumber();
531 }
532
533 /**
534  * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Size
535  */
536 int FileHelper::GetLutItemSize()
537 {
538    return PixelReadConverter->GetLutItemSize();
539 }
540
541 /**
542  * \brief Writes on disk A SINGLE Dicom file
543  *        NO test is performed on  processor "Endiannity".
544  *        It's up to the user to call his Reader properly
545  * @param fileName name of the file to be created
546  *                 (any already existing file is over written)
547  * @return false if write fails
548  */
549 bool FileHelper::WriteRawData(std::string const &fileName)
550 {
551    std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
552    if (!fp1)
553    {
554       gdcmWarningMacro( "Fail to open (write) file:" << fileName.c_str());
555       return false;
556    }
557
558    if ( PixelWriteConverter->GetUserData() )
559    {
560       fp1.write( (char *)PixelWriteConverter->GetUserData(), 
561                  PixelWriteConverter->GetUserDataSize() );
562    }
563    else if ( PixelReadConverter->GetRGB() )
564    {
565       fp1.write( (char *)PixelReadConverter->GetRGB(), 
566                  PixelReadConverter->GetRGBSize());
567    }
568    else if ( PixelReadConverter->GetRaw() )
569    {
570       fp1.write( (char *)PixelReadConverter->GetRaw(), 
571                  PixelReadConverter->GetRawSize());
572    }
573    else
574    {
575       gdcmErrorMacro( "Nothing written." );
576    }
577
578    fp1.close();
579
580    return true;
581 }
582
583 /**
584  * \brief Writes on disk A SINGLE Dicom file, 
585  *        using the Implicit Value Representation convention
586  *        NO test is performed on  processor "Endianity".
587  * @param fileName name of the file to be created
588  *                 (any already existing file is overwritten)
589  * @return false if write fails
590  */
591
592 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
593 {
594    SetWriteTypeToDcmImplVR();
595    return Write(fileName);
596 }
597
598 /**
599 * \brief Writes on disk A SINGLE Dicom file, 
600  *        using the Explicit Value Representation convention
601  *        NO test is performed on  processor "Endiannity". 
602  * @param fileName name of the file to be created
603  *                 (any already existing file is overwritten)
604  * @return false if write fails
605  */
606
607 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
608 {
609    SetWriteTypeToDcmExplVR();
610    return Write(fileName);
611 }
612
613 /**
614  * \brief Writes on disk A SINGLE Dicom file, 
615  *        using the ACR-NEMA convention
616  *        NO test is performed on  processor "Endiannity".
617  *        (a l'attention des logiciels cliniques 
618  *        qui ne prennent en entrée QUE des images ACR ...
619  * \warning if a DICOM_V3 header is supplied,
620  *         groups < 0x0008 and shadow groups are ignored
621  * \warning NO TEST is performed on processor "Endiannity".
622  * @param fileName name of the file to be created
623  *                 (any already existing file is overwritten)
624  * @return false if write fails
625  */
626
627 bool FileHelper::WriteAcr (std::string const &fileName)
628 {
629    SetWriteTypeToAcr();
630    return Write(fileName);
631 }
632
633 /**
634  * \brief Writes on disk A SINGLE Dicom file, 
635  * @param fileName name of the file to be created
636  *                 (any already existing file is overwritten)
637  * @return false if write fails
638  */
639 bool FileHelper::Write(std::string const &fileName)
640 {
641    switch(WriteType)
642    {
643       case ImplicitVR:
644          SetWriteFileTypeToImplicitVR();
645          break;
646       case Unknown:  // should never happen; ExplicitVR is the default value
647       case ExplicitVR:
648          SetWriteFileTypeToExplicitVR();
649          break;
650       case ACR:
651       case ACR_LIBIDO:
652       // NOTHING is done here just for LibIDO.
653       // Just to avoid further trouble if user creates a file ex-nihilo,
654       // wants to write it as an ACR-NEMA file,
655       // and forgets to create any Entry belonging to group 0008
656       // (shame on him !)
657       // We add Recognition Code (RET)
658         if ( ! FileInternal->GetValEntry(0x0008, 0x0010) )
659             FileInternal->InsertValEntry("", 0x0008, 0x0010);
660          SetWriteFileTypeToACR();
661         // SetWriteFileTypeToImplicitVR(); // ACR IS implicit VR !
662          break;
663    }
664    CheckMandatoryElements();
665
666    // --------------------------------------------------------------
667    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
668    //
669    // if recognition code tells us we dealt with a LibIDO image
670    // we reproduce on disk the switch between lineNumber and columnNumber
671    // just before writting ...
672    /// \todo the best trick would be *change* the recognition code
673    ///       but pb expected if user deals with, e.g. COMPLEX images
674    
675    if ( WriteType == ACR_LIBIDO )
676    {
677       SetWriteToLibido();
678    }
679    else
680    {
681       SetWriteToNoLibido();
682    }
683    // ----------------- End of Special Patch ----------------
684   
685    switch(WriteMode)
686    {
687       case WMODE_RAW :
688          SetWriteToRaw(); // modifies and pushes to the archive, when necessary
689          break;
690       case WMODE_RGB :
691          SetWriteToRGB(); // modifies and pushes to the archive, when necessary
692          break;
693    }
694
695    bool check = CheckWriteIntegrity(); // verifies length
696    if (check)
697    {
698       check = FileInternal->Write(fileName,WriteType);
699    }
700
701    RestoreWrite();
702    RestoreWriteFileType();
703    RestoreWriteMandatory();
704
705    // --------------------------------------------------------------
706    // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
707    // 
708    // ...and we restore the header to be Dicom Compliant again 
709    // just after writting
710    RestoreWriteOfLibido();
711    // ----------------- End of Special Patch ----------------
712
713    return check;
714 }
715
716 //-----------------------------------------------------------------------------
717 // Protected
718 /**
719  * \brief Checks the write integrity
720  *
721  * The tests made are :
722  *  - verify the size of the image to write with the possible write
723  *    when the user set an image data
724  * @return true if check is successfull
725  */
726 bool FileHelper::CheckWriteIntegrity()
727 {
728    if ( PixelWriteConverter->GetUserData() )
729    {
730       int numberBitsAllocated = FileInternal->GetBitsAllocated();
731       if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
732       {
733          gdcmWarningMacro( "numberBitsAllocated changed from " 
734                           << numberBitsAllocated << " to 16 " 
735                           << " for consistency purpose" );
736          numberBitsAllocated = 16;
737       }
738
739       size_t decSize = FileInternal->GetXSize()
740                      * FileInternal->GetYSize() 
741                      * FileInternal->GetZSize()
742                      * FileInternal->GetSamplesPerPixel()
743                      * ( numberBitsAllocated / 8 );
744       size_t rgbSize = decSize;
745       if ( FileInternal->HasLUT() )
746          rgbSize = decSize * 3;
747
748       switch(WriteMode)
749       {
750          case WMODE_RAW :
751             if ( decSize!=PixelWriteConverter->GetUserDataSize() )
752             {
753                gdcmWarningMacro( "Data size (Raw) is incorrect. Should be " 
754                            << decSize << " / Found :" 
755                            << PixelWriteConverter->GetUserDataSize() );
756                return false;
757             }
758             break;
759          case WMODE_RGB :
760             if ( rgbSize!=PixelWriteConverter->GetUserDataSize() )
761             {
762                gdcmWarningMacro( "Data size (RGB) is incorrect. Should be " 
763                           << decSize << " / Found " 
764                           << PixelWriteConverter->GetUserDataSize() );
765                return false;
766             }
767             break;
768       }
769    }   
770    return true;
771 }
772
773 /**
774  * \brief Updates the File to write RAW data (as opposed to RGB data)
775  *       (modifies, when necessary, photochromatic interpretation, 
776  *       bits allocated, Pixels element VR)
777  */ 
778 void FileHelper::SetWriteToRaw()
779 {
780    if ( FileInternal->GetNumberOfScalarComponents() == 3 
781     && !FileInternal->HasLUT() )
782    {
783       SetWriteToRGB();
784    } 
785    else
786    {
787       ValEntry *photInt = CopyValEntry(0x0028,0x0004);
788       if (FileInternal->HasLUT() )
789       {
790          photInt->SetValue("PALETTE COLOR ");
791       }
792       else
793       {
794          photInt->SetValue("MONOCHROME2 ");
795       }
796
797       PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
798                                        PixelReadConverter->GetRawSize());
799
800       std::string vr = "OB";
801       if ( FileInternal->GetBitsAllocated()>8 )
802          vr = "OW";
803       if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files 
804          vr = "OB";
805       BinEntry *pixel = 
806          CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
807       pixel->SetValue(GDCM_BINLOADED);
808       pixel->SetBinArea(PixelWriteConverter->GetData(),false);
809       pixel->SetLength(PixelWriteConverter->GetDataSize());
810
811       Archive->Push(photInt);
812       Archive->Push(pixel);
813    }
814 }
815
816 /**
817  * \brief Updates the File to write RGB data (as opposed to RAW data)
818  *       (modifies, when necessary, photochromatic interpretation, 
819  *       samples per pixel, Planar configuration, 
820  *       bits allocated, bits stored, high bit -ACR 24 bits-
821  *       Pixels element VR, pushes out the LUT, )
822  */ 
823 void FileHelper::SetWriteToRGB()
824 {
825    if ( FileInternal->GetNumberOfScalarComponents()==3 )
826    {
827       PixelReadConverter->BuildRGBImage();
828       
829       ValEntry *spp = CopyValEntry(0x0028,0x0002);
830       spp->SetValue("3 ");
831
832       ValEntry *planConfig = CopyValEntry(0x0028,0x0006);
833       planConfig->SetValue("0 ");
834
835       ValEntry *photInt = CopyValEntry(0x0028,0x0004);
836       photInt->SetValue("RGB ");
837
838       if ( PixelReadConverter->GetRGB() )
839       {
840          PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
841                                           PixelReadConverter->GetRGBSize());
842       }
843       else // Raw data
844       {
845          PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
846                                           PixelReadConverter->GetRawSize());
847       }
848
849       std::string vr = "OB";
850       if ( FileInternal->GetBitsAllocated()>8 )
851          vr = "OW";
852       if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files 
853          vr = "OB";
854       BinEntry *pixel = 
855          CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
856       pixel->SetValue(GDCM_BINLOADED);
857       pixel->SetBinArea(PixelWriteConverter->GetData(),false);
858       pixel->SetLength(PixelWriteConverter->GetDataSize());
859
860       Archive->Push(spp);
861       Archive->Push(planConfig);
862       Archive->Push(photInt);
863       Archive->Push(pixel);
864
865       // Remove any LUT
866       Archive->Push(0x0028,0x1101);
867       Archive->Push(0x0028,0x1102);
868       Archive->Push(0x0028,0x1103);
869       Archive->Push(0x0028,0x1201);
870       Archive->Push(0x0028,0x1202);
871       Archive->Push(0x0028,0x1203);
872
873       // push out Palette Color Lookup Table UID, if any
874       Archive->Push(0x0028,0x1199);
875
876       // For old '24 Bits' ACR-NEMA
877       // Thus, we have a RGB image and the bits allocated = 24 and 
878       // samples per pixels = 1 (in the read file)
879       if ( FileInternal->GetBitsAllocated()==24 ) 
880       {
881          ValEntry *bitsAlloc = CopyValEntry(0x0028,0x0100);
882          bitsAlloc->SetValue("8 ");
883
884          ValEntry *bitsStored = CopyValEntry(0x0028,0x0101);
885          bitsStored->SetValue("8 ");
886
887          ValEntry *highBit = CopyValEntry(0x0028,0x0102);
888          highBit->SetValue("7 ");
889
890          Archive->Push(bitsAlloc);
891          Archive->Push(bitsStored);
892          Archive->Push(highBit);
893       }
894    }
895    else
896    {
897       SetWriteToRaw();
898    }
899 }
900
901 /**
902  * \brief Restore the File write mode  
903  */ 
904 void FileHelper::RestoreWrite()
905 {
906    Archive->Restore(0x0028,0x0002);
907    Archive->Restore(0x0028,0x0004);
908    Archive->Restore(0x0028,0x0006);
909    Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
910
911    // For old ACR-NEMA (24 bits problem)
912    Archive->Restore(0x0028,0x0100);
913    Archive->Restore(0x0028,0x0101);
914    Archive->Restore(0x0028,0x0102);
915
916    // For the LUT
917    Archive->Restore(0x0028,0x1101);
918    Archive->Restore(0x0028,0x1102);
919    Archive->Restore(0x0028,0x1103);
920    Archive->Restore(0x0028,0x1201);
921    Archive->Restore(0x0028,0x1202);
922    Archive->Restore(0x0028,0x1203);
923
924    // For the Palette Color Lookup Table UID
925    Archive->Restore(0x0028,0x1203); 
926
927
928    // group 0002 may be pushed out for ACR-NEMA writting purposes 
929    Archive->Restore(0x0002,0x0000);
930    Archive->Restore(0x0002,0x0001);
931    Archive->Restore(0x0002,0x0002);
932    Archive->Restore(0x0002,0x0003);
933    Archive->Restore(0x0002,0x0010);
934    Archive->Restore(0x0002,0x0012);
935    Archive->Restore(0x0002,0x0013);
936    Archive->Restore(0x0002,0x0016);
937    Archive->Restore(0x0002,0x0100);
938    Archive->Restore(0x0002,0x0102);
939 }
940
941 /**
942  * \brief Pushes out the whole group 0002
943  *        FIXME : better, set a flag to tell the writer not to write it ...
944  *        FIXME : method should probably have an other name !
945  *                SetWriteFileTypeToACR is NOT opposed to 
946  *                SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
947  */ 
948 void FileHelper::SetWriteFileTypeToACR()
949 {
950    Archive->Push(0x0002,0x0000);
951    Archive->Push(0x0002,0x0001);
952    Archive->Push(0x0002,0x0002);
953    Archive->Push(0x0002,0x0003);
954    Archive->Push(0x0002,0x0010);
955    Archive->Push(0x0002,0x0012);
956    Archive->Push(0x0002,0x0013);
957    Archive->Push(0x0002,0x0016);
958    Archive->Push(0x0002,0x0100);
959    Archive->Push(0x0002,0x0102);
960 }
961
962 /**
963  * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"   
964  */ 
965 void FileHelper::SetWriteFileTypeToExplicitVR()
966 {
967    std::string ts = Util::DicomString( 
968       Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
969
970    ValEntry *tss = CopyValEntry(0x0002,0x0010);
971    tss->SetValue(ts);
972
973    Archive->Push(tss);
974 }
975
976 /**
977  * \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"   
978  */ 
979 void FileHelper::SetWriteFileTypeToImplicitVR()
980 {
981    std::string ts = Util::DicomString(
982       Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
983
984    ValEntry *tss = CopyValEntry(0x0002,0x0010);
985    tss->SetValue(ts);
986
987    Archive->Push(tss);
988 }
989
990
991 /**
992  * \brief Restore in the File the initial group 0002
993  */ 
994 void FileHelper::RestoreWriteFileType()
995 {
996 }
997
998 /**
999  * \brief Set the Write not to Libido format
1000  */ 
1001 void FileHelper::SetWriteToLibido()
1002 {
1003    ValEntry *oldRow = dynamic_cast<ValEntry *>
1004                 (FileInternal->GetDocEntry(0x0028, 0x0010));
1005    ValEntry *oldCol = dynamic_cast<ValEntry *>
1006                 (FileInternal->GetDocEntry(0x0028, 0x0011));
1007    
1008    if ( oldRow && oldCol )
1009    {
1010       std::string rows, columns; 
1011
1012       ValEntry *newRow=new ValEntry(oldRow->GetDictEntry());
1013       ValEntry *newCol=new ValEntry(oldCol->GetDictEntry());
1014
1015       newRow->Copy(oldCol);
1016       newCol->Copy(oldRow);
1017
1018       newRow->SetValue(oldCol->GetValue());
1019       newCol->SetValue(oldRow->GetValue());
1020
1021       Archive->Push(newRow);
1022       Archive->Push(newCol);
1023    }
1024
1025    ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
1026    libidoCode->SetValue("ACRNEMA_LIBIDO_1.1");
1027    Archive->Push(libidoCode);
1028 }
1029
1030 /**
1031  * \brief Set the Write not to No Libido format
1032  */ 
1033 void FileHelper::SetWriteToNoLibido()
1034 {
1035    ValEntry *recCode = dynamic_cast<ValEntry *>
1036                 (FileInternal->GetDocEntry(0x0008,0x0010));
1037    if ( recCode )
1038    {
1039       if ( recCode->GetValue() == "ACRNEMA_LIBIDO_1.1" )
1040       {
1041          ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
1042          libidoCode->SetValue("");
1043          Archive->Push(libidoCode);
1044       }
1045    }
1046 }
1047
1048 /**
1049  * \brief Restore the Write format
1050  */ 
1051 void FileHelper::RestoreWriteOfLibido()
1052 {
1053    Archive->Restore(0x0028,0x0010);
1054    Archive->Restore(0x0028,0x0011);
1055    Archive->Restore(0x0008,0x0010);
1056
1057    // Restore 'LibIDO-special' entries, if any
1058    Archive->Restore(0x0028,0x0015);
1059    Archive->Restore(0x0028,0x0016);
1060    Archive->Restore(0x0028,0x0017);
1061    Archive->Restore(0x0028,0x00199);
1062 }
1063
1064 /**
1065  * \brief Duplicates a ValEntry or creates it.
1066  * @param   group   Group number of the Entry 
1067  * @param   elem  Element number of the Entry
1068  * \return  pointer to the new Val Entry (NULL when creation failed).          
1069  */ 
1070 ValEntry *FileHelper::CopyValEntry(uint16_t group, uint16_t elem)
1071 {
1072    DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1073    ValEntry *newE;
1074
1075    if ( oldE )
1076    {
1077       newE = new ValEntry(oldE->GetDictEntry());
1078       newE->Copy(oldE);
1079    }
1080    else
1081    {
1082       newE = GetFile()->NewValEntry(group, elem);
1083    }
1084
1085    return newE;
1086 }
1087
1088 /**
1089  * \brief   Duplicates a BinEntry or creates it.
1090  * @param   group   Group number of the Entry 
1091  * @param   elem  Element number of the Entry
1092  * @param   vr  Value Representation of the Entry
1093  *          FIXME : what is it used for?
1094  * \return  pointer to the new Bin Entry (NULL when creation failed).
1095  */ 
1096 BinEntry *FileHelper::CopyBinEntry(uint16_t group, uint16_t elem,
1097                                    const std::string &vr)
1098 {
1099    DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1100    BinEntry *newE;
1101
1102    if ( oldE ) 
1103       if ( oldE->GetVR()!=vr )
1104          oldE = NULL;
1105
1106    if ( oldE )
1107    {
1108       newE = new BinEntry(oldE->GetDictEntry());
1109       newE->Copy(oldE);
1110    }
1111    else
1112    {
1113       newE = GetFile()->NewBinEntry(group, elem, vr);
1114    }
1115
1116    return newE;
1117 }
1118
1119 /**
1120  * \brief   This method is called automatically, just before writting
1121  *         in order to produce a 'True Dicom V3' image
1122  *         We cannot know *how* the user made the File (reading an old ACR-NEMA
1123  *         file or a not very clean DICOM file ...) 
1124  *          
1125  *          Just before writting :
1126  *             - we check the Entries
1127  *             - we create the mandatory entries if they are missing
1128  *             - we modify the values if necessary
1129  *             - we push the sensitive entries to the Archive
1130  *          The writing process will restore the entries as they where before 
1131  *          entering FileHelper::CheckMandatoryElements, so the user will always
1132  *          see the entries just as he left them.
1133  * 
1134  * \todo : - warn the user if we had to add some entries :
1135  *         even if a mandatory entry is missing, we add it, with a default value
1136  *         (we don't want to give up the writting process if user forgot to
1137  *         specify Lena's Patient ID, for instance ...)
1138  *         - read the whole PS 3.3 Part of DICOM  (890 pages)
1139  *         and write a *full* checker (probably one method per Modality ...)
1140  *         Any contribution is welcome. 
1141  *         - write a user callable full checker, to allow post reading
1142  *         and/or pre writting image consistency check.           
1143  */ 
1144  
1145 void FileHelper::CheckMandatoryElements()
1146 {
1147    // just to remember : 'official' 0002 group
1148    if ( WriteType != ACR && WriteType != ACR_LIBIDO )
1149    {
1150      // Group 000002 (Meta Elements) already pushed out
1151   
1152    //0002 0000 UL 1 Meta Group Length
1153    //0002 0001 OB 1 File Meta Information Version
1154    //0002 0002 UI 1 Media Stored SOP Class UID
1155    //0002 0003 UI 1 Media Stored SOP Instance UID
1156    //0002 0010 UI 1 Transfer Syntax UID
1157    //0002 0012 UI 1 Implementation Class UID
1158    //0002 0013 SH 1 Implementation Version Name
1159    //0002 0016 AE 1 Source Application Entity Title
1160    //0002 0100 UI 1 Private Information Creator
1161    //0002 0102 OB 1 Private Information
1162   
1163    // Create them if not found
1164    // Always modify the value
1165    // Push the entries to the archive.
1166       ValEntry *e_0002_0000 = CopyValEntry(0x0002,0x0000);
1167       e_0002_0000->SetValue("0"); // for the moment
1168       Archive->Push(e_0002_0000);
1169   
1170       BinEntry *e_0002_0001 = CopyBinEntry(0x0002,0x0001, "OB");
1171       e_0002_0001->SetBinArea((uint8_t*)Util::GetFileMetaInformationVersion(),
1172                                false);
1173       e_0002_0001->SetLength(2);
1174       Archive->Push(e_0002_0001);
1175
1176    // 'Media Stored SOP Class UID' 
1177       ValEntry *e_0002_0002 = CopyValEntry(0x0002,0x0002);
1178       // [Secondary Capture Image Storage]
1179       e_0002_0002->SetValue("1.2.840.10008.5.1.4.1.1.7"); 
1180       Archive->Push(e_0002_0002);
1181  
1182    // 'Media Stored SOP Instance UID'   
1183       ValEntry *e_0002_0003 = CopyValEntry(0x0002,0x0003);
1184       e_0002_0003->SetValue(Util::CreateUniqueUID());
1185       Archive->Push(e_0002_0003); 
1186
1187    // 'Implementation Class UID'
1188       ValEntry *e_0002_0012 = CopyValEntry(0x0002,0x0012);
1189       e_0002_0012->SetValue(Util::CreateUniqueUID());
1190       Archive->Push(e_0002_0012); 
1191
1192    // 'Implementation Version Name'
1193       ValEntry *e_0002_0013 = CopyValEntry(0x0002,0x0013);
1194       e_0002_0013->SetValue("GDCM 1.1");
1195       Archive->Push(e_0002_0013);
1196
1197    //'Source Application Entity Title' Not Mandatory
1198    //ValEntry *e_0002_0016 = CopyValEntry(0x0002,0x0016);
1199    //   e_0002_0016->SetValue("1.2.840.10008.5.1.4.1.1.7");
1200    //   Archive->Push(e_0002_0016);
1201    }
1202
1203    // Push out 'LibIDO-special' entries, if any
1204    Archive->Push(0x0028,0x0015);
1205    Archive->Push(0x0028,0x0016);
1206    Archive->Push(0x0028,0x0017);
1207    Archive->Push(0x0028,0x00199);
1208
1209    // Deal with the pb of (Bits Stored = 12)
1210    // - we're gonna write the image as Bits Stored = 16
1211    if ( FileInternal->GetEntryValue(0x0028,0x0100) ==  "12")
1212    {
1213       ValEntry *e_0028_0100 = CopyValEntry(0x0028,0x0100);
1214       e_0028_0100->SetValue("16");
1215       Archive->Push(e_0028_0100);
1216    }
1217
1218    // Check if user wasn't drunk ;-)
1219
1220    std::ostringstream s;
1221    // check 'Bits Allocated' vs decent values
1222    int nbBitsAllocated = FileInternal->GetBitsAllocated();
1223    if ( nbBitsAllocated == 0 || nbBitsAllocated > 32)
1224    {
1225       ValEntry *e_0028_0100 = CopyValEntry(0x0028,0x0100);
1226       e_0028_0100->SetValue("16");
1227       Archive->Push(e_0028_0100); 
1228       gdcmWarningMacro("(0028,0100) changed from "
1229          << nbBitsAllocated << " to 16 for consistency purpose");
1230       nbBitsAllocated = 16; 
1231    }
1232    // check 'Bits Stored' vs 'Bits Allocated'   
1233    int nbBitsStored = FileInternal->GetBitsStored();
1234    if ( nbBitsStored == 0 || nbBitsStored > nbBitsAllocated )
1235    {
1236       s << nbBitsAllocated;
1237       ValEntry *e_0028_0101 = CopyValEntry(0x0028,0x0101);
1238       e_0028_0101->SetValue( s.str() );
1239       Archive->Push(e_0028_0101);
1240       gdcmWarningMacro("(0028,0101) changed from "
1241                        << nbBitsStored << " to " << nbBitsAllocated
1242                        << " for consistency purpose" );
1243       nbBitsStored = nbBitsAllocated; 
1244     }
1245    // check 'Hight Bit Position' vs 'Bits Allocated' and 'Bits Stored'
1246    int highBitPosition = FileInternal->GetHighBitPosition();
1247    if ( highBitPosition == 0 || 
1248         highBitPosition > nbBitsAllocated-1 ||
1249         highBitPosition < nbBitsStored-1  )
1250    {
1251       ValEntry *e_0028_0102 = CopyValEntry(0x0028,0x0102);
1252
1253       s << nbBitsStored - 1; 
1254       e_0028_0102->SetValue( s.str() );
1255       Archive->Push(e_0028_0102);
1256       gdcmWarningMacro("(0028,0102) changed from "
1257                        << highBitPosition << " to " << nbBitsAllocated-1
1258                        << " for consistency purpose");
1259    }
1260   // --- Check UID-related Entries ---
1261
1262    // If 'SOP Class UID' exists ('true DICOM' image)
1263    // we create the 'Source Image Sequence' SeqEntry
1264    // to hold informations about the Source Image
1265
1266    ValEntry *e_0008_0016 = FileInternal->GetValEntry(0x0008, 0x0016);
1267    if ( e_0008_0016 != 0 )
1268    {
1269       // Create 'Source Image Sequence' SeqEntry
1270       SeqEntry *sis = new SeqEntry (
1271             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x2112) );
1272       SQItem *sqi = new SQItem(1);
1273       // (we assume 'SOP Instance UID' exists too) 
1274       // create 'Referenced SOP Class UID'
1275       ValEntry *e_0008_1150 = new ValEntry(
1276             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1150) );
1277       e_0008_1150->SetValue( e_0008_0016->GetValue());
1278       sqi->AddEntry(e_0008_1150);
1279       
1280       // create 'Referenced SOP Instance UID'
1281       ValEntry *e_0008_0018 = FileInternal->GetValEntry(0x0008, 0x0018);
1282       ValEntry *e_0008_1155 = new ValEntry(
1283             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1155) );
1284       e_0008_1155->SetValue( e_0008_0018->GetValue());
1285       sqi->AddEntry(e_0008_1155);
1286
1287       sis->AddSQItem(sqi,1); 
1288       // temporarily replaces any previous 'Source Image Sequence' 
1289       Archive->Push(sis);
1290  
1291       // 'Image Type' (The written image is no longer an 'ORIGINAL' one)
1292       ValEntry *e_0008_0008 = CopyValEntry(0x0008,0x0008);
1293       e_0008_0008->SetValue("DERIVED\\PRIMARY");
1294       Archive->Push(e_0008_0008);
1295    } 
1296    else
1297    {
1298       // There was no 'SOP Class UID'.
1299       // the source image was NOT a true Dicom one.
1300       // We consider the image is a 'Secondary Capture' one
1301       // SOP Class UID
1302       e_0008_0016  =  new ValEntry( 
1303             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0016) );
1304       // [Secondary Capture Image Storage]
1305       e_0008_0016 ->SetValue("1.2.840.10008.5.1.4.1.1.7"); 
1306       Archive->Push(e_0008_0016); 
1307    }
1308
1309 // ---- The user will never have to take any action on the following ----.
1310
1311    // new value for 'SOP Instance UID'
1312    ValEntry *e_0008_0018 = new ValEntry(
1313          Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0018) );
1314    e_0008_0018->SetValue( Util::CreateUniqueUID() );
1315    Archive->Push(e_0008_0018);
1316
1317    // Instance Creation Date
1318    ValEntry *e_0008_0012 = CopyValEntry(0x0008,0x0012);
1319    std::string date = Util::GetCurrentDate();
1320    e_0008_0012->SetValue(date.c_str());
1321    Archive->Push(e_0008_0012);
1322  
1323    // Instance Creation Time
1324    ValEntry *e_0008_0013 = CopyValEntry(0x0008,0x0013);
1325    std::string time = Util::GetCurrentTime();
1326    e_0008_0013->SetValue(time.c_str());
1327    Archive->Push(e_0008_0013);
1328
1329 // ----- Add Mandatory Entries if missing ---
1330
1331 // Entries whose type is 1 are mandatory, with a mandatory value
1332 // Entries whose type is 1c are mandatory-inside-a-Sequence
1333 // Entries whose type is 2 are mandatory, with a optional value
1334 // Entries whose type is 2c are mandatory-inside-a-Sequence
1335 // Entries whose type is 3 are optional
1336
1337    // 'Serie Instance UID'
1338    // Keep the value if exists
1339    // The user is allowed to create his own Series, 
1340    // keeping the same 'Serie Instance UID' for various images
1341    // The user shouldn't add any image to a 'Manufacturer Serie'
1342    // but there is no way no to allowed him to do that 
1343    ValEntry *e_0020_000e = FileInternal->GetValEntry(0x0020, 0x000e);
1344    if ( !e_0020_000e )
1345    {
1346       e_0020_000e = new ValEntry(
1347            Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000e) );
1348       e_0020_000e->SetValue(Util::CreateUniqueUID() );
1349       Archive->Push(e_0020_000e);
1350    } 
1351
1352    // 'Study Instance UID'
1353    // Keep the value if exists
1354    // The user is allowed to create his own Study, 
1355    //          keeping the same 'Study Instance UID' for various images
1356    // The user may add images to a 'Manufacturer Study',
1357    //          adding new series to an already existing Study 
1358    ValEntry *e_0020_000d = FileInternal->GetValEntry(0x0020, 0x000d);
1359    if ( !e_0020_000d )
1360    {
1361       e_0020_000d = new ValEntry(
1362             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000d) );
1363       e_0020_000d->SetValue(Util::CreateUniqueUID() );
1364       Archive->Push(e_0020_000d);
1365    }
1366
1367    // Modality : if missing we set it to 'OTher'
1368    ValEntry *e_0008_0060 = FileInternal->GetValEntry(0x0008, 0x0060);
1369    if ( !e_0008_0060 )
1370    {
1371       e_0008_0060 = new ValEntry(
1372             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0060) );
1373       e_0008_0060->SetValue("OT");
1374       Archive->Push(e_0008_0060);
1375    } 
1376
1377    // Manufacturer : if missing we set it to 'GDCM Factory'
1378    ValEntry *e_0008_0070 = FileInternal->GetValEntry(0x0008, 0x0070);
1379    if ( !e_0008_0070 )
1380    {
1381       e_0008_0070 = new ValEntry(
1382             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0070) );
1383       e_0008_0070->SetValue("GDCM Factory");
1384       Archive->Push(e_0008_0070);
1385    } 
1386
1387    // Institution Name : if missing we set it to 'GDCM Hospital'
1388    ValEntry *e_0008_0080 = FileInternal->GetValEntry(0x0008, 0x0080);
1389    if ( !e_0008_0080 )
1390    {
1391       e_0008_0080 = new ValEntry(
1392             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0080) );
1393       e_0008_0080->SetValue("GDCM Hospital");
1394       Archive->Push(e_0008_0080);
1395    } 
1396
1397    // Patient's Name : if missing, we set it to 'GDCM^Patient'
1398    ValEntry *e_0010_0010 = FileInternal->GetValEntry(0x0010, 0x0010);
1399    if ( !e_0010_0010 )
1400    {
1401       e_0010_0010 = new ValEntry(
1402             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0010) );
1403       e_0010_0010->SetValue("GDCM^Patient");
1404       Archive->Push(e_0010_0010);
1405    } 
1406
1407    // Patient's Birth Date : 'type 2' entry -> must exist, value not mandatory
1408    ValEntry *e_0010_0030 = FileInternal->GetValEntry(0x0010, 0x0030);
1409    if ( !e_0010_0030 )
1410    {
1411       e_0010_0030 = new ValEntry(
1412             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0030) );
1413       e_0010_0030->SetValue("");
1414       Archive->Push(e_0010_0030);
1415    }
1416
1417    // Patient's Sex :'type 2' entry -> must exist, value not mandatory
1418    ValEntry *e_0010_0040 = FileInternal->GetValEntry(0x0010, 0x0040);
1419    if ( !e_0010_0040 )
1420    {
1421       e_0010_0040 = new ValEntry(
1422             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0040) );
1423       e_0010_0040->SetValue("");
1424       Archive->Push(e_0010_0040);
1425    }
1426
1427    // Referring Physician's Name :'type 2' entry -> must exist, value not mandatory
1428    ValEntry *e_0008_0090 = FileInternal->GetValEntry(0x0008, 0x0090);
1429    if ( !e_0008_0090 )
1430    {
1431       e_0008_0090 = new ValEntry(
1432             Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0090) );
1433       e_0008_0090->SetValue("");
1434       Archive->Push(e_0008_0090);
1435    }
1436  
1437    // Remove some inconstencies (probably some more will be added)
1438
1439    // if (0028 0008)Number of Frames exists
1440    //    Push out (0020 0052),Frame of Reference UID
1441    //    (only meaningfull within a Serie)
1442    ValEntry *e_0028_0008 = FileInternal->GetValEntry(0x0028, 0x0008);
1443    if ( !e_0028_0008 )
1444    {
1445       Archive->Push(0x0020, 0X0052);
1446    }
1447
1448  
1449 /**
1450  * \brief Restore in the File the initial group 0002
1451  */
1452 void FileHelper::RestoreWriteMandatory()
1453 {
1454    // group 0002 may be pushed out for ACR-NEMA writting purposes 
1455    Archive->Restore(0x0002,0x0000);
1456    Archive->Restore(0x0002,0x0001);
1457    Archive->Restore(0x0002,0x0002);
1458    Archive->Restore(0x0002,0x0003);
1459    Archive->Restore(0x0002,0x0010);
1460    Archive->Restore(0x0002,0x0012);
1461    Archive->Restore(0x0002,0x0013);
1462    Archive->Restore(0x0002,0x0016);
1463    Archive->Restore(0x0002,0x0100);
1464    Archive->Restore(0x0002,0x0102);
1465
1466    Archive->Restore(0x0008,0x0012);
1467    Archive->Restore(0x0008,0x0013);
1468    Archive->Restore(0x0008,0x0016);
1469    Archive->Restore(0x0008,0x0018);
1470    Archive->Restore(0x0008,0x0060);
1471    Archive->Restore(0x0008,0x0070);
1472    Archive->Restore(0x0008,0x0080);
1473    Archive->Restore(0x0008,0x0090);
1474    Archive->Restore(0x0008,0x2112);
1475
1476    Archive->Restore(0x0010,0x0010);
1477    Archive->Restore(0x0010,0x0030);
1478    Archive->Restore(0x0010,0x0040);
1479
1480    Archive->Restore(0x0020,0x000d);
1481    Archive->Restore(0x0020,0x000e);
1482
1483 }
1484
1485 //-----------------------------------------------------------------------------
1486 // Private
1487 /**
1488  * \brief Factorization for various forms of constructors.
1489  */
1490 void FileHelper::Initialize()
1491 {
1492    UserFunction = 0;
1493
1494    WriteMode = WMODE_RAW;
1495    WriteType = ExplicitVR;
1496
1497    PixelReadConverter  = new PixelReadConvert;
1498    PixelWriteConverter = new PixelWriteConvert;
1499    Archive = new DocEntryArchive( FileInternal );
1500
1501    if ( FileInternal->IsReadable() )
1502    {
1503       PixelReadConverter->GrabInformationsFromFile( FileInternal );
1504    }
1505 }
1506
1507 /**
1508  * \brief Reads/[decompresses] the pixels, 
1509  *        *without* making RGB from Palette Colors 
1510  * @return the pixels area, whatever its type 
1511  *         (uint8_t is just for prototyping : feel free to Cast it) 
1512  */ 
1513 uint8_t *FileHelper::GetRaw()
1514 {
1515    PixelReadConverter->SetUserFunction( UserFunction );
1516
1517    uint8_t *raw = PixelReadConverter->GetRaw();
1518    if ( ! raw )
1519    {
1520       // The Raw image migth not be loaded yet:
1521       std::ifstream *fp = FileInternal->OpenFile();
1522       PixelReadConverter->ReadAndDecompressPixelData( fp );
1523       if ( fp ) 
1524          FileInternal->CloseFile();
1525
1526       raw = PixelReadConverter->GetRaw();
1527       if ( ! raw )
1528       {
1529          gdcmWarningMacro( "Read/decompress of pixel data apparently went wrong.");
1530          return 0;
1531       }
1532    }
1533    return raw;
1534 }
1535
1536 //-----------------------------------------------------------------------------
1537 /**
1538  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
1539  * @param   os ostream we want to print in
1540  * @param indent (unused)
1541  */
1542 void FileHelper::Print(std::ostream &os, std::string const &)
1543 {
1544    FileInternal->SetPrintLevel(PrintLevel);
1545    FileInternal->Print(os);
1546
1547    PixelReadConverter->SetPrintLevel(PrintLevel);
1548    PixelReadConverter->Print(os);
1549 }
1550
1551 #ifndef GDCM_LEGACY_REMOVE
1552 /**
1553  * \brief DEPRECATED : use SetFilename() + Load() methods
1554  *        Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
1555  *        file (gdcm::File only deals with the ... header)
1556  *        Opens (in read only and when possible) an existing file and checks
1557  *        for DICOM compliance. Returns NULL on failure.
1558  *        It will be up to the user to load the pixels into memory
1559  * \note  the in-memory representation of all available tags found in
1560  *        the DICOM header is post-poned to first header information access.
1561  *        This avoid a double parsing of public part of the header when
1562  *        one sets an a posteriori shadow dictionary (efficiency can be
1563  *        seen as a side effect).   
1564  * @param filename file to be opened for parsing
1565  * @deprecated  use SetFilename() + Load() methods
1566  */
1567 FileHelper::FileHelper(std::string const &filename )
1568 {
1569    FileInternal = new File( );
1570    FileInternal->SetFileName( filename );
1571    FileInternal->Load();
1572    SelfHeader = true;
1573    Initialize();
1574 }
1575 #endif
1576 //-----------------------------------------------------------------------------
1577 } // end namespace gdcm