1 /*=========================================================================
4 Module: $RCSfile: gdcmFileHelper.cxx,v $
7 Date: $Date: 2006/03/29 16:09:48 $
8 Version: $Revision: 1.98 $
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.
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.
18 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmDocument.h"
24 #include "gdcmDebug.h"
26 #include "gdcmSeqEntry.h"
27 #include "gdcmSQItem.h"
28 #include "gdcmDataEntry.h"
29 #include "gdcmDocEntry.h"
31 #include "gdcmPixelReadConvert.h"
32 #include "gdcmPixelWriteConvert.h"
33 #include "gdcmDocEntryArchive.h"
34 #include "gdcmDictSet.h"
35 #include "gdcmOrientation.h"
37 #if defined(__BORLANDC__)
38 #include <mem.h> // for memset
44 // ----------------------------- WARNING -------------------------
46 These lines will be moved to the document-to-be 'User's Guide'
48 // To read an image, user needs a gdcm::File
49 gdcm::File *f = new gdcm::File(fileName);
51 // user may also decide he doesn't want to load some parts of the header
52 gdcm::File *f = new gdcm::File();
53 f->SetFileName(fileName);
54 f->SetLoadMode(LD_NOSEQ); // or
55 f->SetLoadMode(LD_NOSHADOW); // or
56 f->SetLoadMode(LD_NOSEQ | LD_NOSHADOW); // or
57 f->SetLoadMode(LD_NOSHADOWSEQ);
60 // user can now check some values
61 std::string v = f->GetEntryValue(groupNb,ElementNb);
63 // to get the pixels, user needs a gdcm::FileHelper
64 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
65 // user may ask not to convert Palette to RGB
66 uint8_t *pixels = fh->GetImageDataRaw();
67 int imageLength = fh->GetImageDataRawSize();
68 // He can now use the pixels, create a new image, ...
69 uint8_t *userPixels = ...
71 To re-write the image, user re-uses the gdcm::FileHelper
73 fh->SetImageData( userPixels, userPixelsLength);
74 fh->SetTypeToRaw(); // Even if it was possible to convert Palette to RGB
77 fh->SetWriteTypeToDcmExpl(); // he wants Explicit Value Representation
78 // Little Endian is the default
79 // no other value is allowed
80 (-->SetWriteType(ExplicitVR);)
81 -->WriteType = ExplicitVR;
82 fh->Write(newFileName); // overwrites the file, if any
85 fh->WriteDcmExplVR(newFileName);
88 // ----------------------------- WARNING -------------------------
90 These lines will be moved to the document-to-be 'Developer's Guide'
92 WriteMode : WMODE_RAW / WMODE_RGB
93 WriteType : ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO
95 fh1->Write(newFileName);
96 SetWriteFileTypeToImplicitVR() / SetWriteFileTypeToExplicitVR();
97 (modifies TransferSyntax)
98 SetWriteToRaw(); / SetWriteToRGB();
99 (modifies, when necessary : photochromatic interpretation,
100 samples per pixel, Planar configuration,
101 bits allocated, bits stored, high bit -ACR 24 bits-
102 Pixels element VR, pushes out the LUT )
103 CheckWriteIntegrity();
104 (checks user given pixels length)
105 FileInternal->Write(fileName,WriteType)
106 fp = opens file(fileName);
107 ComputeGroup0002Length( );
109 RemoveEntry(palettes, etc)
110 Document::WriteContent(fp, writetype);
112 (moves back to the File all the archived elements)
113 RestoreWriteFileType();
114 (pushes back group 0002, with TransferSyntax)
122 typedef std::map<uint16_t, int> GroupHT; // Hash Table
123 //-------------------------------------------------------------------------
124 // Constructor / Destructor
126 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
127 * file (gdcm::File only deals with the ... header)
128 * Opens (in read only and when possible) an existing file and checks
129 * for DICOM compliance. Returns NULL on failure.
130 * It will be up to the user to load the pixels into memory
131 * ( GetImageDataSize() + GetImageData() methods)
132 * \note the in-memory representation of all available tags found in
133 * the DICOM header is post-poned to first header information access.
134 * This avoid a double parsing of public part of the header when
135 * one sets an a posteriori shadow dictionary (efficiency can be
136 * seen as a side effect).
138 FileHelper::FileHelper( )
140 FileInternal = File::New( );
145 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
146 * file (File only deals with the ... header)
147 * Opens (in read only and when possible) an existing file and checks
148 * for DICOM compliance. Returns NULL on failure.
149 * It will be up to the user to load the pixels into memory
150 * ( GetImageDataSize() + GetImageData() methods)
151 * \note the in-memory representation of all available tags found in
152 * the DICOM header is post-poned to first header information access.
153 * This avoid a double parsing of public part of the header when
154 * user sets an a posteriori shadow dictionary (efficiency can be
155 * seen as a side effect).
156 * @param header already built Header
158 FileHelper::FileHelper(File *header)
160 gdcmAssertMacro(header);
162 FileInternal = header;
163 FileInternal->Register();
165 if ( FileInternal->IsReadable() )
167 PixelReadConverter->GrabInformationsFromFile( FileInternal, this );
172 * \brief canonical destructor
173 * \note If the header (gdcm::File) was created by the FileHelper constructor,
174 * it is destroyed by the FileHelper
176 FileHelper::~FileHelper()
178 if ( PixelReadConverter )
180 delete PixelReadConverter;
182 if ( PixelWriteConverter )
184 delete PixelWriteConverter;
191 FileInternal->Unregister();
194 //-----------------------------------------------------------------------------
198 * \brief Sets the LoadMode of the internal gdcm::File as a boolean string.
199 * NO_SEQ, NO_SHADOW, NO_SHADOWSEQ ... (nothing more, right now)
200 * WARNING : before using NO_SHADOW, be sure *all* your files
201 * contain accurate values in the 0x0000 element (if any)
202 * of *each* Shadow Group. The parser will fail if the size is wrong !
203 * @param loadMode Load mode to be used
205 void FileHelper::SetLoadMode(int loadMode)
207 GetFile()->SetLoadMode( loadMode );
210 * \brief Sets the LoadMode of the internal gdcm::File
211 * @param fileName name of the file to be open
213 void FileHelper::SetFileName(std::string const &fileName)
215 FileInternal->SetFileName( fileName );
220 * @return false if file cannot be open or no swap info was found,
221 * or no tag was found.
223 bool FileHelper::Load()
225 if ( !FileInternal->Load() )
228 PixelReadConverter->GrabInformationsFromFile( FileInternal, this );
233 * \brief Accesses an existing DataEntry through it's (group, element)
234 * and modifies it's content with the given value.
235 * @param content new value (string) to substitute with
236 * @param group group number of the Dicom Element to modify
237 * @param elem element number of the Dicom Element to modify
238 * \return false if DataEntry not found
240 bool FileHelper::SetEntryString(std::string const &content,
241 uint16_t group, uint16_t elem)
243 return FileInternal->SetEntryString(content, group, elem);
248 * \brief Accesses an existing DataEntry through it's (group, element)
249 * and modifies it's content with the given value.
250 * @param content new value (void* -> uint8_t*) to substitute with
251 * @param lgth new value length
252 * @param group group number of the Dicom Element to modify
253 * @param elem element number of the Dicom Element to modify
254 * \return false if DataEntry not found
256 bool FileHelper::SetEntryBinArea(uint8_t *content, int lgth,
257 uint16_t group, uint16_t elem)
259 return FileInternal->SetEntryBinArea(content, lgth, group, elem);
263 * \brief Modifies the value of a given DataEntry when it exists.
264 * Creates it with the given value when unexistant.
265 * @param content (string) value to be set
266 * @param group Group number of the Entry
267 * @param elem Element number of the Entry
268 * \return pointer to the modified/created DataEntry (NULL when creation
271 DataEntry *FileHelper::InsertEntryString(std::string const &content,
272 uint16_t group, uint16_t elem)
274 return FileInternal->InsertEntryString(content, group, elem);
278 * \brief Modifies the value of a given DataEntry when it exists.
279 * Creates it with the given value when unexistant.
280 * A copy of the binArea is made to be kept in the Document.
281 * @param binArea (binary)value to be set
282 * @param lgth new value length
283 * @param group Group number of the Entry
284 * @param elem Element number of the Entry
285 * \return pointer to the modified/created DataEntry (NULL when creation
288 DataEntry *FileHelper::InsertEntryBinArea(uint8_t *binArea, int lgth,
289 uint16_t group, uint16_t elem)
291 return FileInternal->InsertEntryBinArea(binArea, lgth, group, elem);
295 * \brief Adds an empty SeqEntry
296 * (remove any existing entry with same group,elem)
297 * @param group Group number of the Entry
298 * @param elem Element number of the Entry
299 * \return pointer to the created SeqEntry (NULL when creation
302 SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
304 return FileInternal->InsertSeqEntry(group, elem);
308 * \brief Get the size of the image data
309 * If the image can be RGB (with a lut or by default), the size
310 * corresponds to the RGB image
311 * (use GetImageDataRawSize if you want to be sure to get *only*
312 * the size of the pixels)
313 * @return The image size
315 size_t FileHelper::GetImageDataSize()
317 if ( PixelWriteConverter->GetUserData() )
319 return PixelWriteConverter->GetUserDataSize();
321 return PixelReadConverter->GetRGBSize();
325 * \brief Get the size of the image data.
326 * If the image could be converted to RGB using a LUT,
327 * this transformation is not taken into account by GetImageDataRawSize
328 * (use GetImageDataSize if you wish)
329 * @return The raw image size
331 size_t FileHelper::GetImageDataRawSize()
333 if ( PixelWriteConverter->GetUserData() )
335 return PixelWriteConverter->GetUserDataSize();
337 return PixelReadConverter->GetRawSize();
341 * \brief brings pixels into memory :
342 * - Allocates necessary memory,
343 * - Reads the pixels from disk (uncompress if necessary),
344 * - Transforms YBR pixels, if any, into RGB pixels,
345 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
346 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
347 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
348 * @return Pointer to newly allocated pixel data.
349 * (uint8_t is just for prototyping. feel free to cast)
350 * NULL if alloc fails
352 uint8_t *FileHelper::GetImageData()
354 if ( PixelWriteConverter->GetUserData() )
356 return PixelWriteConverter->GetUserData();
361 // If the decompression failed nothing can be done.
365 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
367 return PixelReadConverter->GetRGB();
371 // When no LUT or LUT conversion fails, return the Raw
372 return PixelReadConverter->GetRaw();
377 * \brief brings pixels into memory :
378 * - Allocates necessary memory,
379 * - Transforms YBR pixels (if any) into RGB pixels
380 * - Transforms 3 planes R, G, B (if any) into a single RGB Plane
381 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
382 * - DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
383 * @return Pointer to newly allocated pixel data.
384 * (uint8_t is just for prototyping. feel free to cast)
385 * NULL if alloc fails
387 uint8_t *FileHelper::GetImageDataRaw ()
392 #ifndef GDCM_LEGACY_REMOVE
394 * \brief Useless function, since PixelReadConverter forces us
395 * copy the Pixels anyway.
396 * Reads the pixels from disk (uncompress if necessary),
397 * Transforms YBR pixels, if any, into RGB pixels
398 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
399 * Transforms single Grey plane + 3 Palettes into a RGB Plane
400 * Copies at most MaxSize bytes of pixel data to caller allocated
402 * \warning This function allows people that want to build a volume
403 * from an image stack *not to* have, first to get the image pixels,
404 * and then move them to the volume area.
405 * It's absolutely useless for any VTK user since vtk chooses
406 * to invert the lines of an image, that is the last line comes first
407 * (for some axis related reasons?). Hence he will have
408 * to load the image line by line, starting from the end.
409 * VTK users have to call GetImageData
411 * @param destination Address (in caller's memory space) at which the
412 * pixel data should be copied
413 * @param maxSize Maximum number of bytes to be copied. When MaxSize
414 * is not sufficient to hold the pixel data the copy is not
415 * executed (i.e. no partial copy).
416 * @return On success, the number of bytes actually copied. Zero on
417 * failure e.g. MaxSize is lower than necessary.
419 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
423 // If the decompression failed nothing can be done.
427 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
429 if ( PixelReadConverter->GetRGBSize() > maxSize )
431 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
435 (void*)PixelReadConverter->GetRGB(),
436 PixelReadConverter->GetRGBSize() );
437 return PixelReadConverter->GetRGBSize();
440 // Either no LUT conversion necessary or LUT conversion failed
441 if ( PixelReadConverter->GetRawSize() > maxSize )
443 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
447 (void *)PixelReadConverter->GetRaw(),
448 PixelReadConverter->GetRawSize() );
449 return PixelReadConverter->GetRawSize();
454 * \brief Points the internal pointer to the callers inData
455 * image representation, BUT WITHOUT COPYING THE DATA.
456 * 'image' Pixels are presented as C-like 2D arrays : line per line.
457 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
458 * \warning Since the pixels are not copied, it is the caller's responsability
459 * not to deallocate its data before gdcm uses them (e.g. with
460 * the Write() method )
461 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
462 * user is allowed to pass any kind of pixelsn since the size is
464 * @param expectedSize total image size, *in Bytes*
466 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
468 SetUserData(inData, expectedSize);
472 * \brief Set the image data defined by the user
473 * \warning When writting the file, this data are get as default data to write
474 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
475 * user is allowed to pass any kind of pixels since the size is
477 * @param expectedSize total image size, *in Bytes*
479 void FileHelper::SetUserData(uint8_t *inData, size_t expectedSize)
481 PixelWriteConverter->SetUserData(inData, expectedSize);
485 * \brief Get the image data defined by the user
486 * \warning When writting the file, this data are get as default data to write
488 uint8_t *FileHelper::GetUserData()
490 return PixelWriteConverter->GetUserData();
494 * \brief Get the image data size defined by the user
495 * \warning When writting the file, this data are get as default data to write
497 size_t FileHelper::GetUserDataSize()
499 return PixelWriteConverter->GetUserDataSize();
503 * \brief Get the image data from the file.
504 * If a LUT is found, the data are expanded to be RGB
506 uint8_t *FileHelper::GetRGBData()
508 return PixelReadConverter->GetRGB();
512 * \brief Get the image data size from the file.
513 * If a LUT is found, the data are expanded to be RGB
515 size_t FileHelper::GetRGBDataSize()
517 return PixelReadConverter->GetRGBSize();
521 * \brief Get the image data from the file.
522 * Even when a LUT is found, the data are not expanded to RGB!
524 uint8_t *FileHelper::GetRawData()
526 return PixelReadConverter->GetRaw();
530 * \brief Get the image data size from the file.
531 * Even when a LUT is found, the data are not expanded to RGB!
533 size_t FileHelper::GetRawDataSize()
535 return PixelReadConverter->GetRawSize();
539 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
541 uint8_t* FileHelper::GetLutRGBA()
543 if ( PixelReadConverter->GetLutRGBA() ==0 )
544 PixelReadConverter->BuildLUTRGBA();
545 return PixelReadConverter->GetLutRGBA();
549 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Number
551 int FileHelper::GetLutItemNumber()
553 return PixelReadConverter->GetLutItemNumber();
557 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Size
559 int FileHelper::GetLutItemSize()
561 return PixelReadConverter->GetLutItemSize();
565 * \brief Writes on disk A SINGLE Dicom file
566 * NO test is performed on processor "Endiannity".
567 * It's up to the user to call his Reader properly
568 * @param fileName name of the file to be created
569 * (any already existing file is over written)
570 * @return false if write fails
572 bool FileHelper::WriteRawData(std::string const &fileName)
574 std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
577 gdcmWarningMacro( "Fail to open (write) file:" << fileName.c_str());
581 if ( PixelWriteConverter->GetUserData() )
583 fp1.write( (char *)PixelWriteConverter->GetUserData(),
584 PixelWriteConverter->GetUserDataSize() );
586 else if ( PixelReadConverter->GetRGB() )
588 fp1.write( (char *)PixelReadConverter->GetRGB(),
589 PixelReadConverter->GetRGBSize());
591 else if ( PixelReadConverter->GetRaw() )
593 fp1.write( (char *)PixelReadConverter->GetRaw(),
594 PixelReadConverter->GetRawSize());
598 gdcmErrorMacro( "Nothing written." );
607 * \brief Writes on disk A SINGLE Dicom file,
608 * using the Implicit Value Representation convention
609 * NO test is performed on processor "Endianity".
610 * @param fileName name of the file to be created
611 * (any already existing file is overwritten)
612 * @return false if write fails
615 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
617 SetWriteTypeToDcmImplVR();
618 return Write(fileName);
622 * \brief Writes on disk A SINGLE Dicom file,
623 * using the Explicit Value Representation convention
624 * NO test is performed on processor "Endiannity".
625 * @param fileName name of the file to be created
626 * (any already existing file is overwritten)
627 * @return false if write fails
630 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
632 SetWriteTypeToDcmExplVR();
633 return Write(fileName);
637 * \brief Writes on disk A SINGLE Dicom file,
638 * using the ACR-NEMA convention
639 * NO test is performed on processor "Endiannity".
640 * (a l'attention des logiciels cliniques
641 * qui ne prennent en entrée QUE des images ACR ...
642 * \warning if a DICOM_V3 header is supplied,
643 * groups < 0x0008 and shadow groups are ignored
644 * \warning NO TEST is performed on processor "Endiannity".
645 * @param fileName name of the file to be created
646 * (any already existing file is overwritten)
647 * @return false if write fails
650 bool FileHelper::WriteAcr (std::string const &fileName)
653 return Write(fileName);
657 * \brief Writes on disk A SINGLE Dicom file,
658 * @param fileName name of the file to be created
659 * (any already existing file is overwritten)
660 * @return false if write fails
662 bool FileHelper::Write(std::string const &fileName)
669 SetWriteFileTypeToImplicitVR();
672 case Unknown: // should never happen; ExplicitVR is the default value
675 // User should ask gdcm to write an image in Explicit VR mode
676 // only when he is sure *all* the VR of *all* the DataElements is known.
677 // i.e : when there are *only* Public Groups
678 // or *all* the Shadow Groups are fully described in the relevant Shadow
680 // Let's just *dream* about it; *never* trust a user !
681 // We turn to Implicit VR if at least the VR of one element is unknown.
683 e = FileInternal->GetFirstEntry();
686 if (e->GetVR() == " ")
688 SetWriteTypeToDcmImplVR();
689 SetWriteFileTypeToImplicitVR();
693 e = FileInternal->GetNextEntry();
698 SetWriteFileTypeToExplicitVR();
704 // NOTHING is done here just for LibIDO.
705 // Just to avoid further trouble if user creates a file ex-nihilo,
706 // wants to write it as an ACR-NEMA file,
707 // and forgets to create any Entry belonging to group 0008
709 // We add Recognition Code (RET)
710 if ( ! FileInternal->GetDataEntry(0x0008, 0x0010) )
711 FileInternal->InsertEntryString("ACR-NEMA V1.0 ", 0x0008, 0x0010);
712 SetWriteFileTypeToACR();
713 // SetWriteFileTypeToImplicitVR(); // ACR IS implicit VR !
716 /// \todo FIXME : JPEG may be either ExplicitVR or ImplicitVR
718 SetWriteFileTypeToJPEG();
722 CheckMandatoryElements();
724 // --------------------------------------------------------------
725 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
727 // if recognition code tells us we dealt with a LibIDO image
728 // we reproduce on disk the switch between lineNumber and columnNumber
729 // just before writting ...
730 /// \todo the best trick would be *change* the recognition code
731 /// but pb expected if user deals with, e.g. COMPLEX images
733 if ( WriteType == ACR_LIBIDO )
739 SetWriteToNoLibido();
741 // ----------------- End of Special Patch ----------------
746 SetWriteToRaw(); // modifies and pushes to the archive, when necessary
749 SetWriteToRGB(); // modifies and pushes to the archive, when necessary
753 bool check = CheckWriteIntegrity(); // verifies length
754 if (WriteType == JPEG ) check = true;
757 check = FileInternal->Write(fileName,WriteType);
761 RestoreWriteFileType();
762 RestoreWriteMandatory();
764 // --------------------------------------------------------------
765 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
767 // ...and we restore the header to be Dicom Compliant again
768 // just after writting
769 RestoreWriteOfLibido();
770 // ----------------- End of Special Patch ----------------
775 //-----------------------------------------------------------------------------
778 * \brief Checks the write integrity
780 * The tests made are :
781 * - verify the size of the image to write with the possible write
782 * when the user set an image data
783 * @return true if check is successfull
785 bool FileHelper::CheckWriteIntegrity()
787 if ( PixelWriteConverter->GetUserData() )
789 int numberBitsAllocated = FileInternal->GetBitsAllocated();
790 if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
792 gdcmWarningMacro( "numberBitsAllocated changed from "
793 << numberBitsAllocated << " to 16 "
794 << " for consistency purpose" );
795 numberBitsAllocated = 16;
798 size_t decSize = FileInternal->GetXSize()
799 * FileInternal->GetYSize()
800 * FileInternal->GetZSize()
801 * FileInternal->GetTSize()
802 * FileInternal->GetSamplesPerPixel()
803 * ( numberBitsAllocated / 8 );
804 size_t rgbSize = decSize;
805 if ( FileInternal->HasLUT() )
806 rgbSize = decSize * 3;
811 if ( decSize!=PixelWriteConverter->GetUserDataSize() )
813 gdcmWarningMacro( "Data size (Raw) is incorrect. Should be "
814 << decSize << " / Found :"
815 << PixelWriteConverter->GetUserDataSize() );
820 if ( rgbSize!=PixelWriteConverter->GetUserDataSize() )
822 gdcmWarningMacro( "Data size (RGB) is incorrect. Should be "
823 << decSize << " / Found "
824 << PixelWriteConverter->GetUserDataSize() );
835 * \brief Updates the File to write RAW data (as opposed to RGB data)
836 * (modifies, when necessary, photochromatic interpretation,
837 * bits allocated, Pixels element VR)
839 void FileHelper::SetWriteToRaw()
841 if ( FileInternal->GetNumberOfScalarComponents() == 3
842 && !FileInternal->HasLUT() )
848 DataEntry *photInt = CopyDataEntry(0x0028,0x0004);
849 if (FileInternal->HasLUT() )
851 photInt->SetString("PALETTE COLOR ");
855 photInt->SetString("MONOCHROME2 ");
858 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
859 PixelReadConverter->GetRawSize());
861 std::string vr = "OB";
862 if ( FileInternal->GetBitsAllocated()>8 )
864 if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
867 CopyDataEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
868 pixel->SetFlag(DataEntry::FLAG_PIXELDATA);
869 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
870 pixel->SetLength(PixelWriteConverter->GetDataSize());
872 Archive->Push(photInt);
873 Archive->Push(pixel);
881 * \brief Updates the File to write RGB data (as opposed to RAW data)
882 * (modifies, when necessary, photochromatic interpretation,
883 * samples per pixel, Planar configuration,
884 * bits allocated, bits stored, high bit -ACR 24 bits-
885 * Pixels element VR, pushes out the LUT, )
887 void FileHelper::SetWriteToRGB()
889 if ( FileInternal->GetNumberOfScalarComponents()==3 )
891 PixelReadConverter->BuildRGBImage();
893 DataEntry *spp = CopyDataEntry(0x0028,0x0002);
894 spp->SetString("3 ");
896 DataEntry *planConfig = CopyDataEntry(0x0028,0x0006);
897 planConfig->SetString("0 ");
899 DataEntry *photInt = CopyDataEntry(0x0028,0x0004);
900 photInt->SetString("RGB ");
902 if ( PixelReadConverter->GetRGB() )
904 PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
905 PixelReadConverter->GetRGBSize());
909 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
910 PixelReadConverter->GetRawSize());
913 std::string vr = "OB";
914 if ( FileInternal->GetBitsAllocated()>8 )
916 if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
919 CopyDataEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
920 pixel->SetFlag(DataEntry::FLAG_PIXELDATA);
921 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
922 pixel->SetLength(PixelWriteConverter->GetDataSize());
925 Archive->Push(planConfig);
926 Archive->Push(photInt);
927 Archive->Push(pixel);
930 planConfig->Delete();
935 Archive->Push(0x0028,0x1101);
936 Archive->Push(0x0028,0x1102);
937 Archive->Push(0x0028,0x1103);
938 Archive->Push(0x0028,0x1201);
939 Archive->Push(0x0028,0x1202);
940 Archive->Push(0x0028,0x1203);
942 // push out Palette Color Lookup Table UID, if any
943 Archive->Push(0x0028,0x1199);
945 // For old '24 Bits' ACR-NEMA
946 // Thus, we have a RGB image and the bits allocated = 24 and
947 // samples per pixels = 1 (in the read file)
948 if ( FileInternal->GetBitsAllocated()==24 )
950 DataEntry *bitsAlloc = CopyDataEntry(0x0028,0x0100);
951 bitsAlloc->SetString("8 ");
953 DataEntry *bitsStored = CopyDataEntry(0x0028,0x0101);
954 bitsStored->SetString("8 ");
956 DataEntry *highBit = CopyDataEntry(0x0028,0x0102);
957 highBit->SetString("7 ");
959 Archive->Push(bitsAlloc);
960 Archive->Push(bitsStored);
961 Archive->Push(highBit);
964 bitsStored->Delete();
975 * \brief Restore the File write mode
977 void FileHelper::RestoreWrite()
979 Archive->Restore(0x0028,0x0002);
980 Archive->Restore(0x0028,0x0004);
981 Archive->Restore(0x0028,0x0006);
982 Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
984 // For old ACR-NEMA (24 bits problem)
985 Archive->Restore(0x0028,0x0100);
986 Archive->Restore(0x0028,0x0101);
987 Archive->Restore(0x0028,0x0102);
990 Archive->Restore(0x0028,0x1101);
991 Archive->Restore(0x0028,0x1102);
992 Archive->Restore(0x0028,0x1103);
993 Archive->Restore(0x0028,0x1201);
994 Archive->Restore(0x0028,0x1202);
995 Archive->Restore(0x0028,0x1203);
997 // For the Palette Color Lookup Table UID
998 Archive->Restore(0x0028,0x1203);
1001 // group 0002 may be pushed out for ACR-NEMA writting purposes
1002 Archive->Restore(0x0002,0x0000);
1003 Archive->Restore(0x0002,0x0001);
1004 Archive->Restore(0x0002,0x0002);
1005 Archive->Restore(0x0002,0x0003);
1006 Archive->Restore(0x0002,0x0010);
1007 Archive->Restore(0x0002,0x0012);
1008 Archive->Restore(0x0002,0x0013);
1009 Archive->Restore(0x0002,0x0016);
1010 Archive->Restore(0x0002,0x0100);
1011 Archive->Restore(0x0002,0x0102);
1015 * \brief Pushes out the whole group 0002
1016 * FIXME : better, set a flag to tell the writer not to write it ...
1017 * FIXME : method should probably have an other name !
1018 * SetWriteFileTypeToACR is NOT opposed to
1019 * SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
1021 void FileHelper::SetWriteFileTypeToACR()
1023 Archive->Push(0x0002,0x0000);
1024 Archive->Push(0x0002,0x0001);
1025 Archive->Push(0x0002,0x0002);
1026 Archive->Push(0x0002,0x0003);
1027 Archive->Push(0x0002,0x0010);
1028 Archive->Push(0x0002,0x0012);
1029 Archive->Push(0x0002,0x0013);
1030 Archive->Push(0x0002,0x0016);
1031 Archive->Push(0x0002,0x0100);
1032 Archive->Push(0x0002,0x0102);
1036 * \brief Sets in the File the TransferSyntax to 'JPEG'
1038 void FileHelper::SetWriteFileTypeToJPEG()
1040 std::string ts = Util::DicomString(
1041 Global::GetTS()->GetSpecialTransferSyntax(TS::JPEGBaselineProcess1) );
1043 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1051 * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"
1053 void FileHelper::SetWriteFileTypeToExplicitVR()
1055 std::string ts = Util::DicomString(
1056 Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
1058 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1065 * \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"
1067 void FileHelper::SetWriteFileTypeToImplicitVR()
1069 std::string ts = Util::DicomString(
1070 Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
1072 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1080 * \brief Restore in the File the initial group 0002
1082 void FileHelper::RestoreWriteFileType()
1087 * \brief Set the Write not to Libido format
1089 void FileHelper::SetWriteToLibido()
1091 DataEntry *oldRow = FileInternal->GetDataEntry(0x0028, 0x0010);
1092 DataEntry *oldCol = FileInternal->GetDataEntry(0x0028, 0x0011);
1094 if ( oldRow && oldCol )
1096 std::string rows, columns;
1098 DataEntry *newRow=DataEntry::New(oldRow->GetDictEntry());
1099 DataEntry *newCol=DataEntry::New(oldCol->GetDictEntry());
1101 newRow->Copy(oldCol);
1102 newCol->Copy(oldRow);
1104 newRow->SetString(oldCol->GetString());
1105 newCol->SetString(oldRow->GetString());
1107 Archive->Push(newRow);
1108 Archive->Push(newCol);
1114 DataEntry *libidoCode = CopyDataEntry(0x0008,0x0010);
1115 libidoCode->SetString("ACRNEMA_LIBIDO_1.1");
1116 Archive->Push(libidoCode);
1117 libidoCode->Delete();
1121 * \brief Set the Write not to No Libido format
1123 void FileHelper::SetWriteToNoLibido()
1125 DataEntry *recCode = FileInternal->GetDataEntry(0x0008,0x0010);
1128 if ( recCode->GetString() == "ACRNEMA_LIBIDO_1.1" )
1130 DataEntry *libidoCode = CopyDataEntry(0x0008,0x0010);
1131 libidoCode->SetString("");
1132 Archive->Push(libidoCode);
1133 libidoCode->Delete();
1139 * \brief Restore the Write format
1141 void FileHelper::RestoreWriteOfLibido()
1143 Archive->Restore(0x0028,0x0010);
1144 Archive->Restore(0x0028,0x0011);
1145 Archive->Restore(0x0008,0x0010);
1147 // Restore 'LibIDO-special' entries, if any
1148 Archive->Restore(0x0028,0x0015);
1149 Archive->Restore(0x0028,0x0016);
1150 Archive->Restore(0x0028,0x0017);
1151 Archive->Restore(0x0028,0x00199);
1155 * \brief Duplicates a DataEntry or creates it.
1156 * @param group Group number of the Entry
1157 * @param elem Element number of the Entry
1158 * @param vr Value Representation of the Entry
1159 * FIXME : what is it used for?
1160 * \return pointer to the new Bin Entry (NULL when creation failed).
1162 DataEntry *FileHelper::CopyDataEntry(uint16_t group, uint16_t elem,
1165 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1168 if ( oldE && vr != GDCM_VRUNKNOWN )
1169 if ( oldE->GetVR() != vr )
1174 newE = DataEntry::New(oldE->GetDictEntry());
1179 newE = GetFile()->NewDataEntry(group, elem, vr);
1186 * \brief This method is called automatically, just before writting
1187 * in order to produce a 'True Dicom V3' image.
1189 * We cannot know *how* the user made the File :
1190 * (reading an old ACR-NEMA file or a not very clean DICOM file ...)
1191 * Just before writting :
1192 * - we check the Entries
1193 * - we create the mandatory entries if they are missing
1194 * - we modify the values if necessary
1195 * - we push the sensitive entries to the Archive
1196 * The writing process will restore the entries as they where before
1197 * entering FileHelper::CheckMandatoryElements, so the user will always
1198 * see the entries just as they were before he decided to write.
1201 * - Entries whose type is 1 are mandatory, with a mandatory value
1202 * - Entries whose type is 1c are mandatory-inside-a-Sequence,
1203 * with a mandatory value
1204 * - Entries whose type is 2 are mandatory, with an optional value
1205 * - Entries whose type is 2c are mandatory-inside-a-Sequence,
1206 * with an optional value
1207 * - Entries whose type is 3 are optional
1210 * - warn the user if we had to add some entries :
1211 * even if a mandatory entry is missing, we add it, with a default value
1212 * (we don't want to give up the writting process if user forgot to
1213 * specify Lena's Patient ID, for instance ...)
1214 * - read the whole PS 3.3 Part of DICOM (890 pages)
1215 * and write a *full* checker (probably one method per Modality ...)
1216 * Any contribution is welcome.
1217 * - write a user callable full checker, to allow post reading
1218 * and/or pre writting image consistency check.
1221 /* -------------------------------------------------------------------------------------
1222 To be moved to User's guide / WIKI ?
1224 We have to deal with 4 *very* different cases :
1225 -1) user created ex nihilo his own image and wants to write it as a Dicom image.
1227 -2) user modified the pixels of an existing image.
1229 -3) user created a new image, using existing images (eg MIP, MPR, cartography image)
1231 -4) user modified/added some tags *without processing* the pixels (anonymization..
1232 UNMODIFIED_PIXELS_IMAGE
1234 gdcm::FileHelper::CheckMandatoryElements() deals automatically with these cases.
1237 0008 0012 Instance Creation Date
1238 0008 0013 Instance Creation Time
1239 0008 0018 SOP Instance UID
1240 are *always* created with the current values; user has *no* possible intervention on
1243 'Serie Instance UID'(0x0020,0x000e)
1244 'Study Instance UID'(0x0020,0x000d) are kept as is if already exist,
1245 created if it doesn't.
1246 The user is allowed to create his own Series/Studies,
1247 keeping the same 'Serie Instance UID' / 'Study Instance UID' for various images
1249 The user shouldn't add any image to a 'Manufacturer Serie'
1250 but there is no way no to allow him to do that
1252 None of the 'shadow elements' are droped out.
1256 'Conversion Type (0x0008,0x0064) is forced to 'SYN' (Synthetic Image).
1259 'Media Storage SOP Class UID' (0x0002,0x0002)
1260 'SOP Class UID' (0x0008,0x0016) are set to
1261 [Secondary Capture Image Storage]
1262 'Image Type' (0x0008,0x0008) is forced to "DERIVED\PRIMARY"
1263 Conversion Type (0x0008,0x0064) is forced to 'SYN' (Synthetic Image)
1266 If 'SOP Class UID' exists in the native image ('true DICOM' image)
1267 we create the 'Source Image Sequence' SeqEntry (0x0008, 0x2112)
1268 --> 'Referenced SOP Class UID' (0x0008, 0x1150)
1269 whose value is the original 'SOP Class UID'
1270 --> 'Referenced SOP Instance UID' (0x0008, 0x1155)
1271 whose value is the original 'SOP Class UID'
1273 3) TODO : find a trick to allow user to pass to the writter the list of the Dicom images
1274 or the Series, (or the Study ?) he used to created his image
1275 (MIP, MPR, cartography image, ...)
1276 These info should be stored (?)
1277 0008 1110 SQ 1 Referenced Study Sequence
1278 0008 1115 SQ 1 Referenced Series Sequence
1279 0008 1140 SQ 1 Referenced Image Sequence
1281 4) When user *knows* he didn't modified the pixels, we keep some informations unchanged :
1282 'Media Storage SOP Class UID' (0x0002,0x0002)
1283 'SOP Class UID' (0x0008,0x0016)
1284 'Image Type' (0x0008,0x0008)
1285 'Conversion Type' (0x0008,0x0064)
1288 Bellow follows the full description (hope so !) of the consistency checks performed
1289 by gdcm::FileHelper::CheckMandatoryElements()
1292 -->'Media Storage SOP Class UID' (0x0002,0x0002)
1293 -->'SOP Class UID' (0x0008,0x0016) are defaulted to
1294 [Secondary Capture Image Storage]
1295 --> 'Image Type' (0x0008,0x0008)
1296 is forced to "DERIVED\PRIMARY"
1297 (The written image is no longer an 'ORIGINAL' one)
1298 Except if user knows he didn't modify the image (e.g. : he just anonymized the file)
1300 --> Conversion Type (0x0008,0x0064)
1301 is defaulted to 'SYN' (Synthetic Image)
1302 when *he* knows he created his own image ex nihilo
1304 --> 'Modality' (0x0008,0x0060)
1305 is defaulted to "OT" (other) if missing.
1306 (a fully user created image belongs to *no* modality)
1308 --> 'Media Storage SOP Instance UID' (0x0002,0x0003)
1309 --> 'Implementation Class UID' (0x0002,0x0012)
1310 are automatically generated; no user intervention possible
1312 --> 'Serie Instance UID'(0x0020,0x000e)
1313 --> 'Study Instance UID'(0x0020,0x000d) are kept as is if already exist
1314 created if it doesn't.
1315 The user is allowed to create his own Series/Studies,
1316 keeping the same 'Serie Instance UID' / 'Study Instance UID'
1319 The user shouldn't add any image to a 'Manufacturer Serie'
1320 but there is no way no to allowed him to do that
1322 --> If 'SOP Class UID' exists in the native image ('true DICOM' image)
1323 we create the 'Source Image Sequence' SeqEntry (0x0008, 0x2112)
1325 --> 'Referenced SOP Class UID' (0x0008, 0x1150)
1326 whose value is the original 'SOP Class UID'
1327 --> 'Referenced SOP Instance UID' (0x0008, 0x1155)
1328 whose value is the original 'SOP Class UID'
1330 --> Bits Stored, Bits Allocated, Hight Bit Position are checked for consistency
1331 --> Pixel Spacing (0x0028,0x0030) is defaulted to "1.0\1.0"
1332 --> Samples Per Pixel (0x0028,0x0002) is defaulted to 1 (grayscale)
1334 --> Imager Pixel Spacing (0x0018,0x1164) : defaulted to Pixel Spacing value
1336 --> Instance Creation Date, Instance Creation Time are forced to current Date and Time
1338 --> Study Date, Study Time are defaulted to current Date and Time
1339 (they remain unchanged if they exist)
1341 --> Patient Orientation : (0x0020,0x0020), if not present, is deduced from
1342 Image Orientation (Patient) : (0020|0037) or from
1343 Image Orientation (RET) : (0020 0035)
1345 --> Study ID, Series Number, Instance Number, Patient Orientation (Type 2)
1346 are created, with empty value if there are missing.
1348 --> Manufacturer, Institution Name, Patient's Name, (Type 2)
1349 are defaulted with a 'gdcm' value.
1351 --> Patient ID, Patient's Birth Date, Patient's Sex, (Type 2)
1352 --> Referring Physician's Name (Type 2)
1353 are created, with empty value if there are missing.
1355 -------------------------------------------------------------------------------------*/
1357 void FileHelper::CheckMandatoryElements()
1359 std::string sop = Util::CreateUniqueUID();
1361 // --------------------- For Meta Elements ---------------------
1362 // just to remember : 'official' 0002 group
1363 if ( WriteType != ACR && WriteType != ACR_LIBIDO )
1365 // Group 000002 (Meta Elements) already pushed out
1367 //0002 0000 UL 1 Meta Group Length
1368 //0002 0001 OB 1 File Meta Information Version
1369 //0002 0002 UI 1 Media Stored SOP Class UID
1370 //0002 0003 UI 1 Media Stored SOP Instance UID
1371 //0002 0010 UI 1 Transfer Syntax UID
1372 //0002 0012 UI 1 Implementation Class UID
1373 //0002 0013 SH 1 Implementation Version Name
1374 //0002 0016 AE 1 Source Application Entity Title
1375 //0002 0100 UI 1 Private Information Creator
1376 //0002 0102 OB 1 Private Information
1378 // Create them if not found
1379 // Always modify the value
1380 // Push the entries to the archive.
1381 CopyMandatoryEntry(0x0002,0x0000,"0");
1383 DataEntry *e_0002_0001 = CopyDataEntry(0x0002,0x0001, "OB");
1384 e_0002_0001->SetBinArea((uint8_t*)Util::GetFileMetaInformationVersion(),
1386 e_0002_0001->SetLength(2);
1387 Archive->Push(e_0002_0001);
1388 e_0002_0001->Delete();
1390 if ( ContentType == FILTERED_IMAGE || ContentType == UNMODIFIED_PIXELS_IMAGE)
1392 // we keep the original 'Media Storage SOP Class UID', we default it if missing
1393 CheckMandatoryEntry(0x0002,0x0002,"1.2.840.10008.5.1.4.1.1.7");
1397 // It's *not* an image comming straight from a source. We force
1398 // 'Media Storage SOP Class UID' --> [Secondary Capture Image Storage]
1399 CopyMandatoryEntry(0x0002,0x0002,"1.2.840.10008.5.1.4.1.1.7");
1402 // 'Media Storage SOP Instance UID'
1403 CopyMandatoryEntry(0x0002,0x0003,sop);
1405 // 'Implementation Class UID'
1406 // FIXME : in all examples we have, 0x0002,0x0012 is not so long :
1407 // seems to be Root UID + 4 digits (?)
1408 CopyMandatoryEntry(0x0002,0x0012,Util::CreateUniqueUID());
1410 // 'Implementation Version Name'
1411 std::string version = "GDCM ";
1412 version += Util::GetVersion();
1413 CopyMandatoryEntry(0x0002,0x0013,version);
1416 // --------------------- For DataSet ---------------------
1418 if ( ContentType == FILTERED_IMAGE || ContentType == UNMODIFIED_PIXELS_IMAGE)
1420 // we keep the original 'Media Storage SOP Class UID', we default it if missing (it should be present !)
1421 CheckMandatoryEntry(0x0008,0x0016,"1.2.840.10008.5.1.4.1.1.7");
1425 // It's *not* an image comming straight from a source. We force
1426 // 'Media Storage SOP Class UID' --> [Secondary Capture Image Storage]
1427 CopyMandatoryEntry(0x0008,0x0016,"1.2.840.10008.5.1.4.1.1.7");
1430 // Push out 'LibIDO-special' entries, if any
1431 Archive->Push(0x0028,0x0015);
1432 Archive->Push(0x0028,0x0016);
1433 Archive->Push(0x0028,0x0017);
1434 Archive->Push(0x0028,0x00199);
1436 // Deal with the pb of (Bits Stored = 12)
1437 // - we're gonna write the image as Bits Stored = 16
1438 if ( FileInternal->GetEntryString(0x0028,0x0100) == "12")
1440 CopyMandatoryEntry(0x0028,0x0100,"16");
1443 // Check if user wasn't drunk ;-)
1445 std::ostringstream s;
1446 // check 'Bits Allocated' vs decent values
1447 int nbBitsAllocated = FileInternal->GetBitsAllocated();
1448 if ( nbBitsAllocated == 0 || nbBitsAllocated > 32)
1450 CopyMandatoryEntry(0x0028,0x0100,"16");
1451 gdcmWarningMacro("(0028,0100) changed from "
1452 << nbBitsAllocated << " to 16 for consistency purpose");
1453 nbBitsAllocated = 16;
1455 // check 'Bits Stored' vs 'Bits Allocated'
1456 int nbBitsStored = FileInternal->GetBitsStored();
1457 if ( nbBitsStored == 0 || nbBitsStored > nbBitsAllocated )
1460 s << nbBitsAllocated;
1461 CopyMandatoryEntry(0x0028,0x0101,s.str());
1462 gdcmWarningMacro("(0028,0101) changed from "
1463 << nbBitsStored << " to " << nbBitsAllocated
1464 << " for consistency purpose" );
1465 nbBitsStored = nbBitsAllocated;
1467 // check 'Hight Bit Position' vs 'Bits Allocated' and 'Bits Stored'
1468 int highBitPosition = FileInternal->GetHighBitPosition();
1469 if ( highBitPosition == 0 ||
1470 highBitPosition > nbBitsAllocated-1 ||
1471 highBitPosition < nbBitsStored-1 )
1474 s << nbBitsStored - 1;
1475 CopyMandatoryEntry(0x0028,0x0102,s.str());
1476 gdcmWarningMacro("(0028,0102) changed from "
1477 << highBitPosition << " to " << nbBitsAllocated-1
1478 << " for consistency purpose");
1481 std::string pixelSpacing = FileInternal->GetEntryString(0x0028,0x0030);
1482 if ( pixelSpacing == GDCM_UNFOUND )
1484 pixelSpacing = "1.0\\1.0";
1485 // if missing, Pixel Spacing forced to "1.0\1.0"
1486 CopyMandatoryEntry(0x0028,0x0030,pixelSpacing);
1489 // 'Imager Pixel Spacing' : defaulted to 'Pixel Spacing'
1490 // --> This one is the *legal* one !
1491 if ( ContentType != USER_OWN_IMAGE)
1492 // we write it only when we are *sure* the image comes from
1493 // an imager (see also 0008,0x0064)
1494 CheckMandatoryEntry(0x0018,0x1164,pixelSpacing);
1496 // Samples Per Pixel (type 1) : default to grayscale
1497 CheckMandatoryEntry(0x0028,0x0002,"1");
1499 // --- Check UID-related Entries ---
1502 if ( ContentType != USER_OWN_IMAGE) // when it's not a user made image
1504 // If 'SOP Class UID' exists ('true DICOM' image)
1505 // we create the 'Source Image Sequence' SeqEntry
1506 // to hold informations about the Source Image
1508 DataEntry *e_0008_0016 = FileInternal->GetDataEntry(0x0008, 0x0016);
1511 // Create 'Source Image Sequence' SeqEntry
1512 SeqEntry *sis = SeqEntry::New (
1513 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x2112) );
1514 SQItem *sqi = SQItem::New(1);
1515 // (we assume 'SOP Instance UID' exists too)
1516 // create 'Referenced SOP Class UID'
1517 DataEntry *e_0008_1150 = DataEntry::New(
1518 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1150) );
1519 e_0008_1150->SetString( e_0008_0016->GetString());
1520 sqi->AddEntry(e_0008_1150);
1521 e_0008_1150->Delete();
1523 // create 'Referenced SOP Instance UID'
1524 DataEntry *e_0008_0018 = FileInternal->GetDataEntry(0x0008, 0x0018);
1525 DataEntry *e_0008_1155 = DataEntry::New(
1526 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1155) );
1527 e_0008_1155->SetString( e_0008_0018->GetString());
1528 sqi->AddEntry(e_0008_1155);
1529 e_0008_1155->Delete();
1531 sis->AddSQItem(sqi,1);
1534 // temporarily replaces any previous 'Source Image Sequence'
1538 // FIXME : is 'Image Type' *really* depending on the presence of 'SOP Class UID'?
1539 if ( ContentType == FILTERED_IMAGE)
1540 // the user *knows* he just modified the pixels
1541 // the image is no longer an 'Original' one
1542 CopyMandatoryEntry(0x0008,0x0008,"DERIVED\\PRIMARY");
1546 // At the end, not to overwrite the original ones,
1547 // needed by 'Referenced SOP Instance UID', 'Referenced SOP Class UID'
1548 // 'SOP Instance UID'
1549 CopyMandatoryEntry(0x0008,0x0018,sop);
1551 if ( ContentType == USER_OWN_IMAGE)
1554 // Other possible values are :
1555 // See PS 3.3, Page 408
1557 // DV = Digitized Video
1558 // DI = Digital Interface
1559 // DF = Digitized Film
1560 // WSD = Workstation
1561 // SD = Scanned Document
1562 // SI = Scanned Image
1564 // SYN = Synthetic Image
1566 CheckMandatoryEntry(0x0008,0x0064,"SYN"); // Why not?
1569 if ( ContentType == CREATED_IMAGE)
1571 /// \todo : find a trick to pass the Media Storage SOP Instance UID of the images used to create the current image
1576 // ---- The user will never have to take any action on the following ----
1578 // new value for 'SOP Instance UID'
1579 //SetMandatoryEntry(0x0008,0x0018,Util::CreateUniqueUID());
1581 // Instance Creation Date
1582 const std::string &date = Util::GetCurrentDate();
1583 CopyMandatoryEntry(0x0008,0x0012,date);
1585 // Instance Creation Time
1586 const std::string &time = Util::GetCurrentTime();
1587 CopyMandatoryEntry(0x0008,0x0013,time);
1590 CheckMandatoryEntry(0x0008,0x0020,date);
1592 CheckMandatoryEntry(0x0008,0x0030,time);
1595 //CopyMandatoryEntry(0x0008,0x0050,"");
1596 CheckMandatoryEntry(0x0008,0x0050,"");
1599 // ----- Add Mandatory Entries if missing ---
1600 // Entries whose type is 1 are mandatory, with a mandatory value
1601 // Entries whose type is 1c are mandatory-inside-a-Sequence,
1602 // with a mandatory value
1603 // Entries whose type is 2 are mandatory, with an optional value
1604 // Entries whose type is 2c are mandatory-inside-a-Sequence,
1605 // with an optional value
1606 // Entries whose type is 3 are optional
1608 // 'Study Instance UID'
1609 // Keep the value if exists
1610 // The user is allowed to create his own Study,
1611 // keeping the same 'Study Instance UID' for various images
1612 // The user may add images to a 'Manufacturer Study',
1613 // adding new Series to an already existing Study
1614 CheckMandatoryEntry(0x0020,0x000d,Util::CreateUniqueUID());
1616 // 'Serie Instance UID'
1617 // Keep the value if exists
1618 // The user is allowed to create his own Series,
1619 // keeping the same 'Serie Instance UID' for various images
1620 // The user shouldn't add any image to a 'Manufacturer Serie'
1621 // but there is no way no to prevent him for doing that
1622 CheckMandatoryEntry(0x0020,0x000e,Util::CreateUniqueUID());
1625 CheckMandatoryEntry(0x0020,0x0010,"");
1628 CheckMandatoryEntry(0x0020,0x0011,"");
1631 CheckMandatoryEntry(0x0020,0x0013,"");
1633 // Patient Orientation
1634 // Can be computed from (0020|0037) : Image Orientation (Patient)
1635 gdcm::Orientation *o = gdcm::Orientation::New();
1636 std::string ori = o->GetOrientation ( FileInternal );
1638 if (ori != "\\" && ori != GDCM_UNFOUND)
1639 CheckMandatoryEntry(0x0020,0x0020,ori);
1641 CheckMandatoryEntry(0x0020,0x0020,"");
1643 // Default Patient Position to HFS
1644 CheckMandatoryEntry(0x0018,0x5100,"HFS");
1646 // Modality : if missing we set it to 'OTher'
1647 CheckMandatoryEntry(0x0008,0x0060,"OT");
1649 // Manufacturer : if missing we set it to 'GDCM Factory'
1650 CheckMandatoryEntry(0x0008,0x0070,"GDCM Factory");
1652 // Institution Name : if missing we set it to 'GDCM Hospital'
1653 CheckMandatoryEntry(0x0008,0x0080,"GDCM Hospital");
1655 // Patient's Name : if missing, we set it to 'GDCM^Patient'
1656 CheckMandatoryEntry(0x0010,0x0010,"GDCM^Patient");
1659 CheckMandatoryEntry(0x0010,0x0020,"");
1661 // Patient's Birth Date : 'type 2' entry -> must exist, value not mandatory
1662 CheckMandatoryEntry(0x0010,0x0030,"");
1664 // Patient's Sex :'type 2' entry -> must exist, value not mandatory
1665 CheckMandatoryEntry(0x0010,0x0040,"");
1667 // Referring Physician's Name :'type 2' entry -> must exist, value not mandatory
1668 CheckMandatoryEntry(0x0008,0x0090,"");
1671 // Deal with element 0x0000 (group length) of each group.
1672 // First stage : get all the different Groups
1675 DocEntry *d = FileInternal->GetFirstEntry();
1678 grHT[d->GetGroup()] = 0;
1679 d=FileInternal->GetNextEntry();
1681 // Second stage : add the missing ones (if any)
1682 for (GroupHT::iterator it = grHT.begin(); it != grHT.end(); ++it)
1684 CheckMandatoryEntry(it->first, 0x0000, "0");
1686 // Third stage : update all 'zero level' groups length
1691 void FileHelper::CheckMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1693 DataEntry *entry = FileInternal->GetDataEntry(group,elem);
1696 entry = DataEntry::New(Global::GetDicts()->GetDefaultPubDict()->GetEntry(group,elem));
1697 entry->SetString(value);
1698 Archive->Push(entry);
1703 void FileHelper::SetMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1705 DataEntry *entry = DataEntry::New(Global::GetDicts()->GetDefaultPubDict()->GetEntry(group,elem));
1706 entry->SetString(value);
1707 Archive->Push(entry);
1711 void FileHelper::CopyMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1713 DataEntry *entry = CopyDataEntry(group,elem);
1714 entry->SetString(value);
1715 Archive->Push(entry);
1720 * \brief Restore in the File the initial group 0002
1722 void FileHelper::RestoreWriteMandatory()
1724 // group 0002 may be pushed out for ACR-NEMA writting purposes
1725 Archive->Restore(0x0002,0x0000);
1726 Archive->Restore(0x0002,0x0001);
1727 Archive->Restore(0x0002,0x0002);
1728 Archive->Restore(0x0002,0x0003);
1729 Archive->Restore(0x0002,0x0010);
1730 Archive->Restore(0x0002,0x0012);
1731 Archive->Restore(0x0002,0x0013);
1732 Archive->Restore(0x0002,0x0016);
1733 Archive->Restore(0x0002,0x0100);
1734 Archive->Restore(0x0002,0x0102);
1736 // FIXME : Check if none is missing !
1738 Archive->Restore(0x0008,0x0012);
1739 Archive->Restore(0x0008,0x0013);
1740 Archive->Restore(0x0008,0x0016);
1741 Archive->Restore(0x0008,0x0018);
1742 Archive->Restore(0x0008,0x0060);
1743 Archive->Restore(0x0008,0x0070);
1744 Archive->Restore(0x0008,0x0080);
1745 Archive->Restore(0x0008,0x0090);
1746 Archive->Restore(0x0008,0x2112);
1748 Archive->Restore(0x0010,0x0010);
1749 Archive->Restore(0x0010,0x0030);
1750 Archive->Restore(0x0010,0x0040);
1752 Archive->Restore(0x0020,0x000d);
1753 Archive->Restore(0x0020,0x000e);
1758 * \brief CallStartMethod
1760 void FileHelper::CallStartMethod()
1764 CommandManager::ExecuteCommand(this,CMD_STARTPROGRESS);
1768 * \brief CallProgressMethod
1770 void FileHelper::CallProgressMethod()
1772 CommandManager::ExecuteCommand(this,CMD_PROGRESS);
1776 * \brief CallEndMethod
1778 void FileHelper::CallEndMethod()
1781 CommandManager::ExecuteCommand(this,CMD_ENDPROGRESS);
1784 //-----------------------------------------------------------------------------
1787 * \brief Factorization for various forms of constructors.
1789 void FileHelper::Initialize()
1792 ContentType = USER_OWN_IMAGE;
1794 WriteMode = WMODE_RAW;
1795 WriteType = ExplicitVR;
1797 PixelReadConverter = new PixelReadConvert;
1798 PixelWriteConverter = new PixelWriteConvert;
1799 Archive = new DocEntryArchive( FileInternal );
1803 * \brief Reads/[decompresses] the pixels,
1804 * *without* making RGB from Palette Colors
1805 * @return the pixels area, whatever its type
1806 * (uint8_t is just for prototyping : feel free to Cast it)
1808 uint8_t *FileHelper::GetRaw()
1810 PixelReadConverter->SetUserFunction( UserFunction );
1812 uint8_t *raw = PixelReadConverter->GetRaw();
1815 // The Raw image migth not be loaded yet:
1816 std::ifstream *fp = FileInternal->OpenFile();
1817 PixelReadConverter->ReadAndDecompressPixelData( fp );
1819 FileInternal->CloseFile();
1821 raw = PixelReadConverter->GetRaw();
1824 gdcmWarningMacro( "Read/decompress of pixel data apparently went wrong.");
1831 //-----------------------------------------------------------------------------
1833 * \brief Prints the common part of DataEntry, SeqEntry
1834 * @param os ostream we want to print in
1835 * @param indent (unused)
1837 void FileHelper::Print(std::ostream &os, std::string const &)
1839 FileInternal->SetPrintLevel(PrintLevel);
1840 FileInternal->Print(os);
1842 if ( FileInternal->IsReadable() )
1844 PixelReadConverter->SetPrintLevel(PrintLevel);
1845 PixelReadConverter->Print(os);
1849 //-----------------------------------------------------------------------------
1850 } // end namespace gdcm