1 /*=========================================================================
4 Module: $RCSfile: gdcmFileHelper.cxx,v $
7 Date: $Date: 2005/03/02 17:18:32 $
8 Version: $Revision: 1.22 $
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 "gdcmBinEntry.h"
27 #include "gdcmValEntry.h"
28 #include "gdcmSeqEntry.h"
29 #include "gdcmSQItem.h"
30 #include "gdcmContentEntry.h"
32 #include "gdcmPixelReadConvert.h"
33 #include "gdcmPixelWriteConvert.h"
34 #include "gdcmDocEntryArchive.h"
35 #include "gdcmDictSet.h"
41 //-------------------------------------------------------------------------
42 // Constructor / Destructor
44 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
45 * file (gdcm::File only deals with the ... header)
46 * Opens (in read only and when possible) an existing file and checks
47 * for DICOM compliance. Returns NULL on failure.
48 * It will be up to the user to load the pixels into memory
49 * \note the in-memory representation of all available tags found in
50 * the DICOM header is post-poned to first header information access.
51 * This avoid a double parsing of public part of the header when
52 * one sets an a posteriori shadow dictionary (efficiency can be
53 * seen as a side effect).
55 FileHelper::FileHelper( )
57 FileInternal = new File( );
63 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
64 * file (File only deals with the ... header)
65 * Opens (in read only and when possible) an existing file and checks
66 * for DICOM compliance. Returns NULL on failure.
67 * It will be up to the user to load the pixels into memory
68 * \note the in-memory representation of all available tags found in
69 * the DICOM header is post-poned to first header information access.
70 * This avoid a double parsing of public part of the header when
71 * user sets an a posteriori shadow dictionary (efficiency can be
72 * seen as a side effect).
73 * @param header already built Header
75 FileHelper::FileHelper(File *header)
77 FileInternal = header;
83 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
84 * file (gdcm::File only deals with the ... header)
85 * Opens (in read only and when possible) an existing file and checks
86 * for DICOM compliance. Returns NULL on failure.
87 * It will be up to the user to load the pixels into memory
88 * \note the in-memory representation of all available tags found in
89 * the DICOM header is post-poned to first header information access.
90 * This avoid a double parsing of public part of the header when
91 * one sets an a posteriori shadow dictionary (efficiency can be
92 * seen as a side effect).
93 * @param filename file to be opened for parsing
95 FileHelper::FileHelper(std::string const &filename )
97 FileInternal = new File( filename );
103 * \brief canonical destructor
104 * \note If the header (gdcm::File) was created by the FileHelper constructor,
105 * it is destroyed by the FileHelper
107 FileHelper::~FileHelper()
109 if( PixelReadConverter )
111 delete PixelReadConverter;
113 if( PixelWriteConverter )
115 delete PixelWriteConverter;
129 //-----------------------------------------------------------------------------
132 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
133 * through it's (group, element) and modifies it's content with
135 * @param content new value (string) to substitute with
136 * @param group group number of the Dicom Element to modify
137 * @param elem element number of the Dicom Element to modify
138 * \return false if DocEntry not found
140 bool FileHelper::SetValEntry(std::string const &content,
141 uint16_t group, uint16_t elem)
143 return FileInternal->SetValEntry(content, group, elem);
148 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
149 * through it's (group, element) and modifies it's content with
151 * @param content new value (void* -> uint8_t*) to substitute with
152 * @param lgth new value length
153 * @param group group number of the Dicom Element to modify
154 * @param elem element number of the Dicom Element to modify
155 * \return false if DocEntry not found
157 bool FileHelper::SetBinEntry(uint8_t *content, int lgth,
158 uint16_t group, uint16_t elem)
160 return FileInternal->SetBinEntry(content, lgth, group, elem);
164 * \brief Modifies the value of a given DocEntry (Dicom entry)
165 * when it exists. Creates it with the given value when unexistant.
166 * @param content (string)value to be set
167 * @param group Group number of the Entry
168 * @param elem Element number of the Entry
169 * \return pointer to the modified/created Dicom entry (NULL when creation
172 ValEntry *FileHelper::InsertValEntry(std::string const &content,
173 uint16_t group, uint16_t elem)
175 return FileInternal->InsertValEntry(content,group,elem);
179 * \brief Modifies the value of a given DocEntry (Dicom entry)
180 * when it exists. Creates it with the given value when unexistant.
181 * A copy of the binArea is made to be kept in the Document.
182 * @param binArea (binary)value to be set
183 * @param lgth new value length
184 * @param group Group number of the Entry
185 * @param elem Element number of the Entry
186 * \return pointer to the modified/created Dicom entry (NULL when creation
189 BinEntry *FileHelper::InsertBinEntry(uint8_t *binArea, int lgth,
190 uint16_t group, uint16_t elem)
192 return FileInternal->InsertBinEntry(binArea, lgth, group, elem);
196 * \brief Modifies the value of a given DocEntry (Dicom entry)
197 * when it exists. Creates it, empty (?!) when unexistant.
198 * @param group Group number of the Entry
199 * @param elem Element number of the Entry
200 * \return pointer to the modified/created Dicom entry (NULL when creation
203 SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
205 return FileInternal->InsertSeqEntry(group, elem);
209 * \brief Get the size of the image data
210 * If the image can be RGB (with a lut or by default), the size
211 * corresponds to the RGB image
212 * (use GetImageDataRawSize if you want to be sure to get *only*
213 * the size of the pixels)
214 * @return The image size
216 size_t FileHelper::GetImageDataSize()
218 if ( PixelWriteConverter->GetUserData() )
220 return PixelWriteConverter->GetUserDataSize();
223 return PixelReadConverter->GetRGBSize();
227 * \brief Get the size of the image data
228 * If the image could be converted to RGB using a LUT,
229 * this transformation is not taken into account by GetImageDataRawSize
230 * (use GetImageDataSize if you wish)
231 * @return The raw image size
233 size_t FileHelper::GetImageDataRawSize()
235 if ( PixelWriteConverter->GetUserData() )
237 return PixelWriteConverter->GetUserDataSize();
240 return PixelReadConverter->GetRawSize();
244 * \brief - Allocates necessary memory,
245 * - Reads the pixels from disk (uncompress if necessary),
246 * - Transforms YBR pixels, if any, into RGB pixels,
247 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
248 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
249 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
250 * @return Pointer to newly allocated pixel data.
251 * NULL if alloc fails
253 uint8_t *FileHelper::GetImageData()
255 if ( PixelWriteConverter->GetUserData() )
257 return PixelWriteConverter->GetUserData();
262 // If the decompression failed nothing can be done.
266 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
268 return PixelReadConverter->GetRGB();
272 // When no LUT or LUT conversion fails, return the Raw
273 return PixelReadConverter->GetRaw();
278 * \brief Allocates necessary memory,
279 * Transforms YBR pixels (if any) into RGB pixels
280 * Transforms 3 planes R, G, B (if any) into a single RGB Plane
281 * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
282 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
283 * @return Pointer to newly allocated pixel data.
284 * NULL if alloc fails
286 uint8_t *FileHelper::GetImageDataRaw ()
293 * Read the pixels from disk (uncompress if necessary),
294 * Transforms YBR pixels, if any, into RGB pixels
295 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
296 * Transforms single Grey plane + 3 Palettes into a RGB Plane
297 * Copies at most MaxSize bytes of pixel data to caller allocated
299 * \warning This function allows people that want to build a volume
300 * from an image stack *not to* have, first to get the image pixels,
301 * and then move them to the volume area.
302 * It's absolutely useless for any VTK user since vtk chooses
303 * to invert the lines of an image, that is the last line comes first
304 * (for some axis related reasons?). Hence he will have
305 * to load the image line by line, starting from the end.
306 * VTK users have to call GetImageData
308 * @param destination Address (in caller's memory space) at which the
309 * pixel data should be copied
310 * @param maxSize Maximum number of bytes to be copied. When MaxSize
311 * is not sufficient to hold the pixel data the copy is not
312 * executed (i.e. no partial copy).
313 * @return On success, the number of bytes actually copied. Zero on
314 * failure e.g. MaxSize is lower than necessary.
316 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
320 // If the decompression failed nothing can be done.
324 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
326 if ( PixelReadConverter->GetRGBSize() > maxSize )
328 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
332 (void*)PixelReadConverter->GetRGB(),
333 PixelReadConverter->GetRGBSize() );
334 return PixelReadConverter->GetRGBSize();
337 // Either no LUT conversion necessary or LUT conversion failed
338 if ( PixelReadConverter->GetRawSize() > maxSize )
340 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
344 (void*)PixelReadConverter->GetRaw(),
345 PixelReadConverter->GetRawSize() );
346 return PixelReadConverter->GetRawSize();
350 * \brief Points the internal pointer to the callers inData
351 * image representation, BUT WITHOUT COPYING THE DATA.
352 * 'image' Pixels are presented as C-like 2D arrays : line per line.
353 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
354 * \warning Since the pixels are not copied, it is the caller's responsability
355 * not to deallocate its data before gdcm uses them (e.g. with
356 * the Write() method )
357 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
358 * user is allowed to pass any kind of pixelsn since the size is
360 * @param expectedSize total image size, *in Bytes*
362 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
364 SetUserData(inData, expectedSize);
368 * \brief Set the image data defined by the user
369 * \warning When writting the file, this data are get as default data to write
370 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
371 * user is allowed to pass any kind of pixels since the size is
373 * @param expectedSize total image size, *in Bytes*
375 void FileHelper::SetUserData(uint8_t *inData, size_t expectedSize)
377 PixelWriteConverter->SetUserData(inData, expectedSize);
381 * \brief Get the image data defined by the user
382 * \warning When writting the file, this data are get as default data to write
384 uint8_t *FileHelper::GetUserData()
386 return PixelWriteConverter->GetUserData();
390 * \brief Get the image data size defined by the user
391 * \warning When writting the file, this data are get as default data to write
393 size_t FileHelper::GetUserDataSize()
395 return PixelWriteConverter->GetUserDataSize();
399 * \brief Get the image data from the file.
400 * If a LUT is found, the data are expanded to be RGB
402 uint8_t *FileHelper::GetRGBData()
404 return PixelReadConverter->GetRGB();
408 * \brief Get the image data size from the file.
409 * If a LUT is found, the data are expanded to be RGB
411 size_t FileHelper::GetRGBDataSize()
413 return PixelReadConverter->GetRGBSize();
417 * \brief Get the image data from the file.
418 * Even when a LUT is found, the data are not expanded to RGB!
420 uint8_t *FileHelper::GetRawData()
422 return PixelReadConverter->GetRaw();
426 * \brief Get the image data size from the file.
427 * Even when a LUT is found, the data are not expanded to RGB!
429 size_t FileHelper::GetRawDataSize()
431 return PixelReadConverter->GetRawSize();
435 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
437 uint8_t* FileHelper::GetLutRGBA()
439 return PixelReadConverter->GetLutRGBA();
443 * \brief Writes on disk A SINGLE Dicom file
444 * NO test is performed on processor "Endiannity".
445 * It's up to the user to call his Reader properly
446 * @param fileName name of the file to be created
447 * (any already existing file is over written)
448 * @return false if write fails
450 bool FileHelper::WriteRawData(std::string const &fileName)
452 std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
455 gdcmWarningMacro( "Fail to open (write) file:" << fileName.c_str());
459 if( PixelWriteConverter->GetUserData() )
461 fp1.write( (char*)PixelWriteConverter->GetUserData(),
462 PixelWriteConverter->GetUserDataSize() );
464 else if ( PixelReadConverter->GetRGB() )
466 fp1.write( (char*)PixelReadConverter->GetRGB(),
467 PixelReadConverter->GetRGBSize());
469 else if ( PixelReadConverter->GetRaw() )
471 fp1.write( (char*)PixelReadConverter->GetRaw(),
472 PixelReadConverter->GetRawSize());
476 gdcmErrorMacro( "Nothing written." );
485 * \brief Writes on disk A SINGLE Dicom file,
486 * using the Implicit Value Representation convention
487 * NO test is performed on processor "Endiannity".
488 * @param fileName name of the file to be created
489 * (any already existing file is overwritten)
490 * @return false if write fails
493 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
495 SetWriteTypeToDcmImplVR();
496 return Write(fileName);
500 * \brief Writes on disk A SINGLE Dicom file,
501 * using the Explicit Value Representation convention
502 * NO test is performed on processor "Endiannity".
503 * @param fileName name of the file to be created
504 * (any already existing file is overwritten)
505 * @return false if write fails
508 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
510 SetWriteTypeToDcmExplVR();
511 return Write(fileName);
515 * \brief Writes on disk A SINGLE Dicom file,
516 * using the ACR-NEMA convention
517 * NO test is performed on processor "Endiannity".
518 * (a l'attention des logiciels cliniques
519 * qui ne prennent en entrée QUE des images ACR ...
520 * \warning if a DICOM_V3 header is supplied,
521 * groups < 0x0008 and shadow groups are ignored
522 * \warning NO TEST is performed on processor "Endiannity".
523 * @param fileName name of the file to be created
524 * (any already existing file is overwritten)
525 * @return false if write fails
528 bool FileHelper::WriteAcr (std::string const &fileName)
531 return Write(fileName);
535 * \brief Writes on disk A SINGLE Dicom file,
536 * @param fileName name of the file to be created
537 * (any already existing file is overwritten)
538 * @return false if write fails
540 bool FileHelper::Write(std::string const &fileName)
545 SetWriteFileTypeToImplicitVR();
546 CheckMandatoryElements();
549 SetWriteFileTypeToExplicitVR();
550 CheckMandatoryElements();
554 SetWriteFileTypeToACR();
557 SetWriteFileTypeToExplicitVR();
558 CheckMandatoryElements();
561 // --------------------------------------------------------------
562 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
564 // if recognition code tells us we dealt with a LibIDO image
565 // we reproduce on disk the switch between lineNumber and columnNumber
566 // just before writting ...
567 /// \todo the best trick would be *change* the recognition code
568 /// but pb expected if user deals with, e.g. COMPLEX images
569 if( WriteType == ACR_LIBIDO )
575 SetWriteToNoLibido();
577 // ----------------- End of Special Patch ----------------
582 SetWriteToRaw(); // modifies and pushes to the archive, when necessary
585 SetWriteToRGB(); // modifies and pushes to the archive, when necessary
589 bool check = CheckWriteIntegrity(); // verifies length
592 check = FileInternal->Write(fileName,WriteType);
596 RestoreWriteFileType();
598 // --------------------------------------------------------------
599 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
601 // ...and we restore the header to be Dicom Compliant again
602 // just after writting
603 RestoreWriteOfLibido();
604 // ----------------- End of Special Patch ----------------
609 //-----------------------------------------------------------------------------
612 * \brief Checks the write integrity
614 * The tests made are :
615 * - verify the size of the image to write with the possible write
616 * when the user set an image data
617 * @return true if check is successfull
619 bool FileHelper::CheckWriteIntegrity()
621 if(PixelWriteConverter->GetUserData())
623 int numberBitsAllocated = FileInternal->GetBitsAllocated();
624 if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
626 numberBitsAllocated = 16;
629 size_t decSize = FileInternal->GetXSize()
630 * FileInternal->GetYSize()
631 * FileInternal->GetZSize()
632 * ( numberBitsAllocated / 8 )
633 * FileInternal->GetSamplesPerPixel();
634 size_t rgbSize = decSize;
635 if( FileInternal->HasLUT() )
636 rgbSize = decSize * 3;
641 if( decSize!=PixelWriteConverter->GetUserDataSize() )
643 gdcmWarningMacro( "Data size (Raw) is incorrect. Should be "
644 << decSize << " / Found :"
645 << PixelWriteConverter->GetUserDataSize() );
650 if( rgbSize!=PixelWriteConverter->GetUserDataSize() )
652 gdcmWarningMacro( "Data size (RGB) is incorrect. Should be "
653 << decSize << " / Found "
654 << PixelWriteConverter->GetUserDataSize() );
665 * \brief Updates the File to write RAW data (as opposed to RGB data)
666 * (modifies, when necessary, photochromatic interpretation,
667 * bits allocated, Pixels element VR)
669 void FileHelper::SetWriteToRaw()
671 if( FileInternal->GetNumberOfScalarComponents() == 3
672 && !FileInternal->HasLUT())
678 ValEntry *photInt = CopyValEntry(0x0028,0x0004);
679 if(FileInternal->HasLUT())
681 photInt->SetValue("PALETTE COLOR ");
685 photInt->SetValue("MONOCHROME2 ");
688 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
689 PixelReadConverter->GetRawSize());
691 std::string vr = "OB";
692 if( FileInternal->GetBitsAllocated()>8 )
694 if( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
697 CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
698 pixel->SetValue(GDCM_BINLOADED);
699 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
700 pixel->SetLength(PixelWriteConverter->GetDataSize());
702 Archive->Push(photInt);
703 Archive->Push(pixel);
708 * \brief Updates the File to write RGB data (as opposed to RAW data)
709 * (modifies, when necessary, photochromatic interpretation,
710 * samples per pixel, Planar configuration,
711 * bits allocated, bits stored, high bit -ACR 24 bits-
712 * Pixels element VR, pushes out the LUT, )
714 void FileHelper::SetWriteToRGB()
716 if(FileInternal->GetNumberOfScalarComponents()==3)
718 PixelReadConverter->BuildRGBImage();
720 ValEntry *spp = CopyValEntry(0x0028,0x0002);
723 ValEntry *planConfig = CopyValEntry(0x0028,0x0006);
724 planConfig->SetValue("0 ");
726 ValEntry *photInt = CopyValEntry(0x0028,0x0004);
727 photInt->SetValue("RGB ");
729 if(PixelReadConverter->GetRGB())
731 PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
732 PixelReadConverter->GetRGBSize());
736 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
737 PixelReadConverter->GetRawSize());
740 std::string vr = "OB";
741 if( FileInternal->GetBitsAllocated()>8 )
743 if( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
746 CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
747 pixel->SetValue(GDCM_BINLOADED);
748 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
749 pixel->SetLength(PixelWriteConverter->GetDataSize());
752 Archive->Push(planConfig);
753 Archive->Push(photInt);
754 Archive->Push(pixel);
757 Archive->Push(0x0028,0x1101);
758 Archive->Push(0x0028,0x1102);
759 Archive->Push(0x0028,0x1103);
760 Archive->Push(0x0028,0x1201);
761 Archive->Push(0x0028,0x1202);
762 Archive->Push(0x0028,0x1203);
764 // For old '24 Bits' ACR-NEMA
765 // Thus, we have a RGB image and the bits allocated = 24 and
766 // samples per pixels = 1 (in the read file)
767 if(FileInternal->GetBitsAllocated()==24)
769 ValEntry *bitsAlloc = CopyValEntry(0x0028,0x0100);
770 bitsAlloc->SetValue("8 ");
772 ValEntry *bitsStored = CopyValEntry(0x0028,0x0101);
773 bitsStored->SetValue("8 ");
775 ValEntry *highBit = CopyValEntry(0x0028,0x0102);
776 highBit->SetValue("7 ");
778 Archive->Push(bitsAlloc);
779 Archive->Push(bitsStored);
780 Archive->Push(highBit);
790 * \brief Restore the File write mode
792 void FileHelper::RestoreWrite()
794 Archive->Restore(0x0028,0x0002);
795 Archive->Restore(0x0028,0x0004);
796 Archive->Restore(0x0028,0x0006);
797 Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
799 // For old ACR-NEMA (24 bits problem)
800 Archive->Restore(0x0028,0x0100);
801 Archive->Restore(0x0028,0x0101);
802 Archive->Restore(0x0028,0x0102);
805 Archive->Restore(0x0028,0x1101);
806 Archive->Restore(0x0028,0x1102);
807 Archive->Restore(0x0028,0x1103);
808 Archive->Restore(0x0028,0x1201);
809 Archive->Restore(0x0028,0x1202);
810 Archive->Restore(0x0028,0x1203);
812 // group 0002 may be pushed out for ACR-NEMA writting purposes
813 Archive->Restore(0x0002,0x0000);
814 Archive->Restore(0x0002,0x0001);
815 Archive->Restore(0x0002,0x0002);
816 Archive->Restore(0x0002,0x0003);
817 Archive->Restore(0x0002,0x0010);
818 Archive->Restore(0x0002,0x0012);
819 Archive->Restore(0x0002,0x0013);
820 Archive->Restore(0x0002,0x0016);
821 Archive->Restore(0x0002,0x0100);
822 Archive->Restore(0x0002,0x0102);
826 * \brief Pushes out the whole group 0002
827 * FIXME : better, set a flag to tell the writer not to write it ...
828 * FIXME : method should probably have an other name !
829 * SetWriteFileTypeToACR is NOT opposed to
830 * SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
832 void FileHelper::SetWriteFileTypeToACR()
834 Archive->Push(0x0002,0x0000);
835 Archive->Push(0x0002,0x0001);
836 Archive->Push(0x0002,0x0002);
837 Archive->Push(0x0002,0x0003);
838 Archive->Push(0x0002,0x0010);
839 Archive->Push(0x0002,0x0012);
840 Archive->Push(0x0002,0x0013);
841 Archive->Push(0x0002,0x0016);
842 Archive->Push(0x0002,0x0100);
843 Archive->Push(0x0002,0x0102);
847 * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"
849 void FileHelper::SetWriteFileTypeToExplicitVR()
851 std::string ts = Util::DicomString(
852 Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
854 ValEntry *tss = CopyValEntry(0x0002,0x0010);
861 * \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"
863 void FileHelper::SetWriteFileTypeToImplicitVR()
865 std::string ts = Util::DicomString(
866 Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
868 ValEntry *tss = CopyValEntry(0x0002,0x0010);
876 * \brief Restore in the File the initial group 0002
878 void FileHelper::RestoreWriteFileType()
880 // group 0002 may be pushed out for ACR-NEMA writting purposes
881 Archive->Restore(0x0002,0x0000);
882 Archive->Restore(0x0002,0x0001);
883 Archive->Restore(0x0002,0x0002);
884 Archive->Restore(0x0002,0x0003);
885 Archive->Restore(0x0002,0x0010);
886 Archive->Restore(0x0002,0x0012);
887 Archive->Restore(0x0002,0x0013);
888 Archive->Restore(0x0002,0x0016);
889 Archive->Restore(0x0002,0x0100);
890 Archive->Restore(0x0002,0x0102);
894 * \brief Set the Write not to Libido format
896 void FileHelper::SetWriteToLibido()
898 ValEntry *oldRow = dynamic_cast<ValEntry *>
899 (FileInternal->GetDocEntry(0x0028, 0x0010));
900 ValEntry *oldCol = dynamic_cast<ValEntry *>
901 (FileInternal->GetDocEntry(0x0028, 0x0011));
903 if( oldRow && oldCol )
905 std::string rows, columns;
907 ValEntry *newRow=new ValEntry(oldRow->GetDictEntry());
908 ValEntry *newCol=new ValEntry(oldCol->GetDictEntry());
910 newRow->Copy(oldCol);
911 newCol->Copy(oldRow);
913 newRow->SetValue(oldCol->GetValue());
914 newCol->SetValue(oldRow->GetValue());
916 Archive->Push(newRow);
917 Archive->Push(newCol);
920 ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
921 libidoCode->SetValue("ACRNEMA_LIBIDO_1.1");
922 Archive->Push(libidoCode);
926 * \brief Set the Write not to No Libido format
928 void FileHelper::SetWriteToNoLibido()
930 ValEntry *recCode = dynamic_cast<ValEntry *>
931 (FileInternal->GetDocEntry(0x0008,0x0010));
934 if( recCode->GetValue() == "ACRNEMA_LIBIDO_1.1" )
936 ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
937 libidoCode->SetValue("");
938 Archive->Push(libidoCode);
944 * \brief Restore the Write format
946 void FileHelper::RestoreWriteOfLibido()
948 Archive->Restore(0x0028,0x0010);
949 Archive->Restore(0x0028,0x0011);
950 Archive->Restore(0x0008,0x0010);
954 * \brief Duplicates a ValEntry or creates it.
955 * @param group Group number of the Entry
956 * @param elem Element number of the Entry
957 * \return pointer to the new Val Entry (NULL when creation failed).
959 ValEntry *FileHelper::CopyValEntry(uint16_t group, uint16_t elem)
961 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
966 newE = new ValEntry(oldE->GetDictEntry());
971 newE = GetFile()->NewValEntry(group, elem);
978 * \brief Duplicates a BinEntry or creates it.
979 * @param group Group number of the Entry
980 * @param elem Element number of the Entry
981 * @param vr Value Representation of the Entry
982 * FIXME : what is it used for?
983 * \return pointer to the new Bin Entry (NULL when creation failed).
985 BinEntry *FileHelper::CopyBinEntry(uint16_t group, uint16_t elem,
986 const std::string &vr)
988 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
992 if( oldE->GetVR()!=vr )
997 newE = new BinEntry(oldE->GetDictEntry());
1002 newE = GetFile()->NewBinEntry(group, elem, vr);
1009 * \brief This method is called automatically, just before writting
1010 * in order to produce a 'True Dicom V3' image
1011 * We cannot know *how* the user made the File (reading an old ACR-NEMA
1012 * file or a not very clean DICOM file ...)
1014 * Just before writting :
1015 * - we check the Entries
1016 * - we create the mandatory entries if they are missing
1017 * - we modify the values if necessary
1018 * - we push the sensitive entries to the Archive
1019 * The writing process will restore the entries as they where before
1020 * entering FileHelper::CheckMandatoryElements, so the user will always
1021 * see the entries just as he
1023 * \todo : - warn the user if we had to add some entries :
1024 * even if a mandatory entry is missing, we add it, with a default value
1025 * (we don't want to give up the writting process if user forgot to
1026 * specify Lena's Patient ID, for instance ...)
1027 * - read the whole PS 3.3 Part of DICOM (890 pages)
1028 * and write a *full* checker (probably one method per Modality ...)
1029 * Any contribution is welcome.
1030 * - write a user callable full checker, to allow post reading
1031 * and/or pre writting image consistency check.
1034 void FileHelper::CheckMandatoryElements()
1036 // just to remember : 'official' 0002 group
1038 //0002 0000 UL 1 Meta Group Length
1039 //0002 0001 OB 1 File Meta Information Version
1040 //0002 0002 UI 1 Media Stored SOP Class UID
1041 //0002 0003 UI 1 Media Stored SOP Instance UID
1042 //0002 0010 UI 1 Transfer Syntax UID
1043 //0002 0012 UI 1 Implementation Class UID
1044 //0002 0013 SH 1 Implementation Version Name
1045 //0002 0016 AE 1 Source Application Entity Title
1046 //0002 0100 UI 1 Private Information Creator
1047 //0002 0102 OB 1 Private Information
1049 // Create them if not found
1050 // Always modify the value
1051 // Push the entries to the archive.
1053 ValEntry *e_0002_0000 = CopyValEntry(0x0002,0x0000);
1054 e_0002_0000->SetValue("0"); // for the moment
1055 Archive->Push(e_0002_0000);
1057 BinEntry *e_0002_0001 = CopyBinEntry(0x0002,0x0001, "OB");
1058 e_0002_0001->SetBinArea((uint8_t*)Util::GetFileMetaInformationVersion(),
1060 e_0002_0001->SetLength(2);
1062 ValEntry *e_0002_0002 = CopyValEntry(0x0002,0x0002);
1063 // [Secondary Capture Image Storage]
1064 e_0002_0002->SetValue("1.2.840.10008.5.1.4.1.1.7");
1065 Archive->Push(e_0002_0002);
1067 // 'Media Stored SOP Instance UID'
1068 ValEntry *e_0002_0003 = CopyValEntry(0x0002,0x0003);
1069 e_0002_0003->SetValue(Util::CreateUniqueUID());
1070 Archive->Push(e_0002_0003);
1072 ValEntry *e_0002_0010 = CopyValEntry(0x0002,0x0010);
1073 //[Explicit VR - Little Endian]
1074 e_0002_0010->SetValue("1.2.840.10008.1.2.1");
1075 Archive->Push(e_0002_0010);
1077 // 'Implementation Class UID'
1078 ValEntry *e_0002_0012 = CopyValEntry(0x0002,0x0012);
1079 e_0002_0012->SetValue(Util::CreateUniqueUID());
1080 Archive->Push(e_0002_0012);
1082 // 'Implementation Version Name'
1083 ValEntry *e_0002_0013 = CopyValEntry(0x0002,0x0013);
1084 e_0002_0013->SetValue("GDCM 1.0");
1085 Archive->Push(e_0002_0013);
1087 //'Source Application Entity Title' Not Mandatory
1088 //ValEntry *e_0002_0016 = CopyValEntry(0x0002,0x0016);
1089 // e_0002_0016->SetValue("1.2.840.10008.5.1.4.1.1.7");
1090 // Archive->Push(e_0002_0016);
1092 // Push out 'LibIDO-special' entries, if any
1093 Archive->Push(0x0028,0x0015);
1094 Archive->Push(0x0028,0x0016);
1095 Archive->Push(0x0028,0x0017);
1096 Archive->Push(0x0028,0x00199);
1098 // --- Check UID-related Entries ---
1100 // If 'SOP Class UID' exists ('true DICOM' image)
1101 // we create the 'Source Image Sequence' SeqEntry
1102 // to hold informations about the Source Image
1104 ValEntry *e_0008_0016 = FileInternal->GetValEntry(0x0008, 0x0016);
1105 if ( e_0008_0016 != 0 )
1107 // Create 'Source Image Sequence' SeqEntry
1108 SeqEntry *s = new SeqEntry (
1109 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x2112) );
1110 SQItem *sqi = new SQItem(1);
1111 // (we assume 'SOP Instance UID' exists too)
1112 // create 'Referenced SOP Class UID'
1113 ValEntry *e_0008_1150 = new ValEntry(
1114 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1150) );
1115 e_0008_1150->SetValue( e_0008_0016->GetValue());
1116 sqi->AddEntry(e_0008_1150);
1118 // create 'Referenced SOP Instance UID'
1119 ValEntry *e_0008_0018 = FileInternal->GetValEntry(0x0008, 0x0018);
1120 ValEntry *e_0008_1155 = new ValEntry(
1121 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1155) );
1122 e_0008_1155->SetValue( e_0008_0018->GetValue());
1123 sqi->AddEntry(e_0008_1155);
1125 s->AddSQItem(sqi,1);
1126 // temporarily replaces any previous 'Source Image Sequence'
1129 // 'Image Type' (The written image is no longer an 'ORIGINAL' one)
1130 ValEntry *e_0008_0008 = CopyValEntry(0x0008,0x0008);
1131 e_0008_0008->SetValue("DERIVED\\PRIMARY");
1132 Archive->Push(e_0008_0008);
1136 // There was no 'SOP Class UID'.
1137 // the source image was NOT a true Dicom one.
1138 // We consider the image is a 'Secondary Capture' one
1140 ValEntry *e_0008_0016 = new ValEntry(
1141 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0016) );
1142 // [Secondary Capture Image Storage]
1143 e_0008_0016 ->SetValue("1.2.840.10008.5.1.4.1.1.7");
1144 Archive->Push(e_0008_0016);
1147 // ---- The user will never have to take any action on the following ----.
1149 // new value for 'SOP Instance UID'
1150 ValEntry *e_0008_0018 = new ValEntry(
1151 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0018) );
1152 e_0008_0018->SetValue( Util::CreateUniqueUID() );
1153 Archive->Push(e_0008_0018);
1155 // Instance Creation Date
1156 ValEntry *e_0008_0012 = CopyValEntry(0x0008,0x0012);
1157 std::string date = Util::GetCurrentDate();
1158 e_0008_0012->SetValue(date.c_str());
1159 Archive->Push(e_0008_0012);
1161 // Instance Creation Time
1162 ValEntry *e_0008_0013 = CopyValEntry(0x0008,0x0013);
1163 std::string time = Util::GetCurrentTime();
1164 e_0008_0013->SetValue(time.c_str());
1165 Archive->Push(e_0008_0013);
1167 // ----- Add Mandatory Entries if missing ---
1169 // Entries whose type is 1 are mandatory, with a mandatory value
1170 // Entries whose type is 1c are mandatory-inside-a-Sequence
1171 // Entries whose type is 2 are mandatory, with a optional value
1172 // Entries whose type is 2c are mandatory-inside-a-Sequence
1173 // Entries whose type is 3 are optional
1175 // 'Serie Instance UID'
1176 // Keep the value if exists
1177 // The user is allowed to create his own Series,
1178 // keeping the same 'Serie Instance UID' for various images
1179 // The user shouldn't add any image to a 'Manufacturer Serie'
1180 // but there is no way no to allowed him to do that
1181 ValEntry *e_0020_000e = FileInternal->GetValEntry(0x0020, 0x000e);
1184 e_0020_000e = new ValEntry(
1185 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000e) );
1186 e_0020_000e->SetValue(Util::CreateUniqueUID() );
1187 Archive->Push(e_0020_000e);
1190 // 'Study Instance UID'
1191 // Keep the value if exists
1192 // The user is allowed to create his own Study,
1193 // keeping the same 'Study Instance UID' for various images
1194 // The user may add images to a 'Manufacturer Study',
1195 // adding new series to an already existing Study
1196 ValEntry *e_0020_000d = FileInternal->GetValEntry(0x0020, 0x000d);
1199 e_0020_000d = new ValEntry(
1200 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000d) );
1201 e_0020_000d->SetValue(Util::CreateUniqueUID() );
1202 Archive->Push(e_0020_000d);
1205 // Modality : if missing we set it to 'OTher'
1206 ValEntry *e_0008_0060 = FileInternal->GetValEntry(0x0008, 0x0060);
1209 e_0008_0060 = new ValEntry(
1210 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0060) );
1211 e_0008_0060->SetValue("OT");
1212 Archive->Push(e_0008_0060);
1215 // Manufacturer : if missing we set it to 'GDCM Factory'
1216 ValEntry *e_0008_0070 = FileInternal->GetValEntry(0x0008, 0x0070);
1219 e_0008_0070 = new ValEntry(
1220 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0070) );
1221 e_0008_0070->SetValue("GDCM Factory");
1222 Archive->Push(e_0008_0070);
1225 // Institution Name : if missing we set it to 'GDCM Hospital'
1226 ValEntry *e_0008_0080 = FileInternal->GetValEntry(0x0008, 0x0080);
1229 e_0008_0080 = new ValEntry(
1230 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0080) );
1231 e_0008_0080->SetValue("GDCM Hospital");
1232 Archive->Push(e_0008_0080);
1235 // Patient's Name : if missing, we set it to 'GDCM^Patient'
1236 ValEntry *e_0010_0010 = FileInternal->GetValEntry(0x0010, 0x0010);
1239 e_0010_0010 = new ValEntry(
1240 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0010) );
1241 e_0010_0010->SetValue("GDCM^Patient");
1242 Archive->Push(e_0010_0010);
1245 // Patient's ID : if missing, we set it to 'GDCM_Patient_ID'
1246 ValEntry *e_0010_0020 = FileInternal->GetValEntry(0x0010, 0x0020);
1249 e_0010_0020 = new ValEntry(
1250 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0020) );
1251 e_0010_0020->SetValue("GDCM_Patient_ID");
1252 Archive->Push(e_0010_0020);
1255 // Patient's Birth Date :'type 2' entry -> must exist, value not mandatory
1256 ValEntry *e_0010_0030 = FileInternal->GetValEntry(0x0010, 0x0030);
1259 e_0010_0030 = new ValEntry(
1260 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0030) );
1261 e_0010_0030->SetValue("");
1262 Archive->Push(e_0010_0030);
1265 // Patient's Sex :'type 2' entry -> must exist, value not mandatory
1266 ValEntry *e_0010_0040 = FileInternal->GetValEntry(0x0010, 0x0040);
1269 e_0010_0040 = new ValEntry(
1270 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0040) );
1271 e_0010_0040->SetValue("");
1272 Archive->Push(e_0010_0040);
1275 // Referring Physician's Name :'type 2' entry -> must exist, value not mandatory
1276 ValEntry *e_0008_0090 = FileInternal->GetValEntry(0x0008, 0x0090);
1279 e_0008_0090 = new ValEntry(
1280 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0090) );
1281 e_0008_0090->SetValue("");
1282 Archive->Push(e_0008_0090);
1285 // Remove some inconstencies (probably some more will be added)
1287 // Push out (0020 0052),Frame of Reference UID
1288 // if (0028 0008)Number of Frames exists
1289 // (0020 0052),Frame of Reference UID only meaningfull within a Serie
1290 ValEntry *e_0028_0008 = FileInternal->GetValEntry(0x0028, 0x0008);
1293 Archive->Push(0x0020, 0X0052);
1297 //-----------------------------------------------------------------------------
1300 * \brief Factorization for various forms of constructors.
1302 void FileHelper::Initialize()
1304 WriteMode = WMODE_RAW;
1305 WriteType = ExplicitVR;
1307 PixelReadConverter = new PixelReadConvert;
1308 PixelWriteConverter = new PixelWriteConvert;
1309 Archive = new DocEntryArchive( FileInternal );
1311 if ( FileInternal->IsReadable() )
1313 PixelReadConverter->GrabInformationsFromFile( FileInternal );
1320 uint8_t *FileHelper::GetRaw()
1322 uint8_t *raw = PixelReadConverter->GetRaw();
1325 // The Raw image migth not be loaded yet:
1326 std::ifstream *fp = FileInternal->OpenFile();
1327 PixelReadConverter->ReadAndDecompressPixelData( fp );
1329 FileInternal->CloseFile();
1331 raw = PixelReadConverter->GetRaw();
1334 gdcmWarningMacro( "Read/decompress of pixel data apparently went wrong.");
1342 //-----------------------------------------------------------------------------
1344 void FileHelper::Print(std::ostream &os, std::string const &)
1346 FileInternal->SetPrintLevel(PrintLevel);
1347 FileInternal->Print(os);
1349 PixelReadConverter->SetPrintLevel(PrintLevel);
1350 PixelReadConverter->Print(os);
1353 //-----------------------------------------------------------------------------
1354 } // end namespace gdcm