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