1 /*=========================================================================
4 Module: $RCSfile: gdcmFileHelper.cxx,v $
7 Date: $Date: 2005/03/22 11:40:13 $
8 Version: $Revision: 1.31 $
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"
40 // ----------------------------- WARNING -------------------------
42 These lines will be moved to the document-to-be 'User's Guide'
44 // To read an image, user needs a gdcm::File
45 gdcm::File *f1 = new gdcm::File(fileName);
46 // user can now check some values
47 std::string v = f1->GetEntryValue(groupNb,ElementNb);
48 // to get the pixels, user needs a gdcm::FileHelper
49 gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
50 // user may ask not to convert Palette to RGB
51 uint8_t *pixels = fh1->GetImageDataRaw();
52 int imageLength = fh1->GetImageDataRawSize();
53 // He can now use the pixels, create a new image, ...
54 uint8_t *userPixels = ...
56 To re-write the image, user re-uses the gdcm::FileHelper
58 fh1->SetImageData( userPixels, userPixelsLength);
59 fh1->SetTypeToRaw(); // Even if it was possible to convert Palette to RGB
62 fh1->SetWriteTypeToDcmExpl(); // he wants Explicit Value Representation
63 // Little Endian is the default
64 // no other value is allowed
65 (-->SetWriteType(ExplicitVR);)
66 -->WriteType = ExplicitVR;
67 fh1->Write(newFileName); // overwrites the file, if any
70 fh1->WriteDcmExplVR(newFileName);
73 // ----------------------------- WARNING -------------------------
75 These lines will be moved to the document-to-be 'Developer's Guide'
77 WriteMode : WMODE_RAW / WMODE_RGB
78 WriteType : ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO
80 fh1->Write(newFileName);
81 SetWriteFileTypeToImplicitVR() / SetWriteFileTypeToExplicitVR();
82 (modifies TransferSyntax)
83 SetWriteToRaw(); / SetWriteToRGB();
84 (modifies, when necessary : photochromatic interpretation,
85 samples per pixel, Planar configuration,
86 bits allocated, bits stored, high bit -ACR 24 bits-
87 Pixels element VR, pushes out the LUT )
88 CheckWriteIntegrity();
89 (checks user given pixels length)
90 FileInternal->Write(fileName,WriteType)
91 fp = opens file(fileName);
92 ComputeGroup0002Length(writetype);
94 RemoveEntryNoDestroy(palettes, etc)
95 Document::WriteContent(fp, writetype);
97 (moves back to the File all the archived elements)
98 RestoreWriteFileType();
99 (pushes back group 0002, with TransferSyntax)
107 //-------------------------------------------------------------------------
108 // Constructor / Destructor
110 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
111 * file (gdcm::File only deals with the ... header)
112 * Opens (in read only and when possible) an existing file and checks
113 * for DICOM compliance. Returns NULL on failure.
114 * It will be up to the user to load the pixels into memory
115 * \note the in-memory representation of all available tags found in
116 * the DICOM header is post-poned to first header information access.
117 * This avoid a double parsing of public part of the header when
118 * one sets an a posteriori shadow dictionary (efficiency can be
119 * seen as a side effect).
121 FileHelper::FileHelper( )
123 FileInternal = new File( );
129 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
130 * file (File only deals with the ... header)
131 * Opens (in read only and when possible) an existing file and checks
132 * for DICOM compliance. Returns NULL on failure.
133 * It will be up to the user to load the pixels into memory
134 * \note the in-memory representation of all available tags found in
135 * the DICOM header is post-poned to first header information access.
136 * This avoid a double parsing of public part of the header when
137 * user sets an a posteriori shadow dictionary (efficiency can be
138 * seen as a side effect).
139 * @param header already built Header
141 FileHelper::FileHelper(File *header)
143 FileInternal = header;
149 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
150 * file (gdcm::File only deals with the ... header)
151 * Opens (in read only and when possible) an existing file and checks
152 * for DICOM compliance. Returns NULL on failure.
153 * It will be up to the user to load the pixels into memory
154 * \note the in-memory representation of all available tags found in
155 * the DICOM header is post-poned to first header information access.
156 * This avoid a double parsing of public part of the header when
157 * one sets an a posteriori shadow dictionary (efficiency can be
158 * seen as a side effect).
159 * @param filename file to be opened for parsing
161 FileHelper::FileHelper(std::string const &filename )
163 FileInternal = new File( filename );
169 * \brief canonical destructor
170 * \note If the header (gdcm::File) was created by the FileHelper constructor,
171 * it is destroyed by the FileHelper
173 FileHelper::~FileHelper()
175 if( PixelReadConverter )
177 delete PixelReadConverter;
179 if( PixelWriteConverter )
181 delete PixelWriteConverter;
195 //-----------------------------------------------------------------------------
198 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
199 * through it's (group, element) and modifies it's content with
201 * @param content new value (string) to substitute with
202 * @param group group number of the Dicom Element to modify
203 * @param elem element number of the Dicom Element to modify
204 * \return false if DocEntry not found
206 bool FileHelper::SetValEntry(std::string const &content,
207 uint16_t group, uint16_t elem)
209 return FileInternal->SetValEntry(content, group, elem);
214 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
215 * through it's (group, element) and modifies it's content with
217 * @param content new value (void* -> uint8_t*) to substitute with
218 * @param lgth new value length
219 * @param group group number of the Dicom Element to modify
220 * @param elem element number of the Dicom Element to modify
221 * \return false if DocEntry not found
223 bool FileHelper::SetBinEntry(uint8_t *content, int lgth,
224 uint16_t group, uint16_t elem)
226 return FileInternal->SetBinEntry(content, lgth, group, elem);
230 * \brief Modifies the value of a given DocEntry (Dicom entry)
231 * when it exists. Creates it with the given value when unexistant.
232 * @param content (string)value to be set
233 * @param group Group number of the Entry
234 * @param elem Element number of the Entry
235 * \return pointer to the modified/created Dicom entry (NULL when creation
238 ValEntry *FileHelper::InsertValEntry(std::string const &content,
239 uint16_t group, uint16_t elem)
241 return FileInternal->InsertValEntry(content,group,elem);
245 * \brief Modifies the value of a given DocEntry (Dicom entry)
246 * when it exists. Creates it with the given value when unexistant.
247 * A copy of the binArea is made to be kept in the Document.
248 * @param binArea (binary)value to be set
249 * @param lgth new value length
250 * @param group Group number of the Entry
251 * @param elem Element number of the Entry
252 * \return pointer to the modified/created Dicom entry (NULL when creation
255 BinEntry *FileHelper::InsertBinEntry(uint8_t *binArea, int lgth,
256 uint16_t group, uint16_t elem)
258 return FileInternal->InsertBinEntry(binArea, lgth, group, elem);
262 * \brief Modifies the value of a given DocEntry (Dicom entry)
263 * when it exists. Creates it, empty (?!) when unexistant.
264 * @param group Group number of the Entry
265 * @param elem Element number of the Entry
266 * \return pointer to the modified/created Dicom entry (NULL when creation
269 SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
271 return FileInternal->InsertSeqEntry(group, elem);
275 * \brief Get the size of the image data
276 * If the image can be RGB (with a lut or by default), the size
277 * corresponds to the RGB image
278 * (use GetImageDataRawSize if you want to be sure to get *only*
279 * the size of the pixels)
280 * @return The image size
282 size_t FileHelper::GetImageDataSize()
284 if ( PixelWriteConverter->GetUserData() )
286 return PixelWriteConverter->GetUserDataSize();
289 return PixelReadConverter->GetRGBSize();
293 * \brief Get the size of the image data
294 * If the image could be converted to RGB using a LUT,
295 * this transformation is not taken into account by GetImageDataRawSize
296 * (use GetImageDataSize if you wish)
297 * @return The raw image size
299 size_t FileHelper::GetImageDataRawSize()
301 if ( PixelWriteConverter->GetUserData() )
303 return PixelWriteConverter->GetUserDataSize();
306 return PixelReadConverter->GetRawSize();
310 * \brief - Allocates necessary memory,
311 * - Reads the pixels from disk (uncompress if necessary),
312 * - Transforms YBR pixels, if any, into RGB pixels,
313 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
314 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
315 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
316 * @return Pointer to newly allocated pixel data.
317 * NULL if alloc fails
319 uint8_t *FileHelper::GetImageData()
321 if ( PixelWriteConverter->GetUserData() )
323 return PixelWriteConverter->GetUserData();
328 // If the decompression failed nothing can be done.
332 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
334 return PixelReadConverter->GetRGB();
338 // When no LUT or LUT conversion fails, return the Raw
339 return PixelReadConverter->GetRaw();
344 * \brief Allocates necessary memory,
345 * Transforms YBR pixels (if any) into RGB pixels
346 * Transforms 3 planes R, G, B (if any) into a single RGB Plane
347 * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
348 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
349 * @return Pointer to newly allocated pixel data.
350 * NULL if alloc fails
352 uint8_t *FileHelper::GetImageDataRaw ()
359 * Read the pixels from disk (uncompress if necessary),
360 * Transforms YBR pixels, if any, into RGB pixels
361 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
362 * Transforms single Grey plane + 3 Palettes into a RGB Plane
363 * Copies at most MaxSize bytes of pixel data to caller allocated
365 * \warning This function allows people that want to build a volume
366 * from an image stack *not to* have, first to get the image pixels,
367 * and then move them to the volume area.
368 * It's absolutely useless for any VTK user since vtk chooses
369 * to invert the lines of an image, that is the last line comes first
370 * (for some axis related reasons?). Hence he will have
371 * to load the image line by line, starting from the end.
372 * VTK users have to call GetImageData
374 * @param destination Address (in caller's memory space) at which the
375 * pixel data should be copied
376 * @param maxSize Maximum number of bytes to be copied. When MaxSize
377 * is not sufficient to hold the pixel data the copy is not
378 * executed (i.e. no partial copy).
379 * @return On success, the number of bytes actually copied. Zero on
380 * failure e.g. MaxSize is lower than necessary.
382 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
386 // If the decompression failed nothing can be done.
390 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
392 if ( PixelReadConverter->GetRGBSize() > maxSize )
394 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
398 (void*)PixelReadConverter->GetRGB(),
399 PixelReadConverter->GetRGBSize() );
400 return PixelReadConverter->GetRGBSize();
403 // Either no LUT conversion necessary or LUT conversion failed
404 if ( PixelReadConverter->GetRawSize() > maxSize )
406 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
410 (void*)PixelReadConverter->GetRaw(),
411 PixelReadConverter->GetRawSize() );
412 return PixelReadConverter->GetRawSize();
416 * \brief Points the internal pointer to the callers inData
417 * image representation, BUT WITHOUT COPYING THE DATA.
418 * 'image' Pixels are presented as C-like 2D arrays : line per line.
419 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
420 * \warning Since the pixels are not copied, it is the caller's responsability
421 * not to deallocate its data before gdcm uses them (e.g. with
422 * the Write() method )
423 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
424 * user is allowed to pass any kind of pixelsn since the size is
426 * @param expectedSize total image size, *in Bytes*
428 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
430 SetUserData(inData, expectedSize);
434 * \brief Set the image data defined by the user
435 * \warning When writting the file, this data are get as default data to write
436 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
437 * user is allowed to pass any kind of pixels since the size is
439 * @param expectedSize total image size, *in Bytes*
441 void FileHelper::SetUserData(uint8_t *inData, size_t expectedSize)
443 PixelWriteConverter->SetUserData(inData, expectedSize);
447 * \brief Get the image data defined by the user
448 * \warning When writting the file, this data are get as default data to write
450 uint8_t *FileHelper::GetUserData()
452 return PixelWriteConverter->GetUserData();
456 * \brief Get the image data size defined by the user
457 * \warning When writting the file, this data are get as default data to write
459 size_t FileHelper::GetUserDataSize()
461 return PixelWriteConverter->GetUserDataSize();
465 * \brief Get the image data from the file.
466 * If a LUT is found, the data are expanded to be RGB
468 uint8_t *FileHelper::GetRGBData()
470 return PixelReadConverter->GetRGB();
474 * \brief Get the image data size from the file.
475 * If a LUT is found, the data are expanded to be RGB
477 size_t FileHelper::GetRGBDataSize()
479 return PixelReadConverter->GetRGBSize();
483 * \brief Get the image data from the file.
484 * Even when a LUT is found, the data are not expanded to RGB!
486 uint8_t *FileHelper::GetRawData()
488 return PixelReadConverter->GetRaw();
492 * \brief Get the image data size from the file.
493 * Even when a LUT is found, the data are not expanded to RGB!
495 size_t FileHelper::GetRawDataSize()
497 return PixelReadConverter->GetRawSize();
501 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
503 uint8_t* FileHelper::GetLutRGBA()
505 return PixelReadConverter->GetLutRGBA();
509 * \brief Writes on disk A SINGLE Dicom file
510 * NO test is performed on processor "Endiannity".
511 * It's up to the user to call his Reader properly
512 * @param fileName name of the file to be created
513 * (any already existing file is over written)
514 * @return false if write fails
516 bool FileHelper::WriteRawData(std::string const &fileName)
518 std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
521 gdcmWarningMacro( "Fail to open (write) file:" << fileName.c_str());
525 if( PixelWriteConverter->GetUserData() )
527 fp1.write( (char*)PixelWriteConverter->GetUserData(),
528 PixelWriteConverter->GetUserDataSize() );
530 else if ( PixelReadConverter->GetRGB() )
532 fp1.write( (char*)PixelReadConverter->GetRGB(),
533 PixelReadConverter->GetRGBSize());
535 else if ( PixelReadConverter->GetRaw() )
537 fp1.write( (char*)PixelReadConverter->GetRaw(),
538 PixelReadConverter->GetRawSize());
542 gdcmErrorMacro( "Nothing written." );
551 * \brief Writes on disk A SINGLE Dicom file,
552 * using the Implicit Value Representation convention
553 * NO test is performed on processor "Endiannity".
554 * @param fileName name of the file to be created
555 * (any already existing file is overwritten)
556 * @return false if write fails
559 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
561 SetWriteTypeToDcmImplVR();
562 return Write(fileName);
566 * \brief Writes on disk A SINGLE Dicom file,
567 * using the Explicit Value Representation convention
568 * NO test is performed on processor "Endiannity".
569 * @param fileName name of the file to be created
570 * (any already existing file is overwritten)
571 * @return false if write fails
574 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
576 SetWriteTypeToDcmExplVR();
577 return Write(fileName);
581 * \brief Writes on disk A SINGLE Dicom file,
582 * using the ACR-NEMA convention
583 * NO test is performed on processor "Endiannity".
584 * (a l'attention des logiciels cliniques
585 * qui ne prennent en entrée QUE des images ACR ...
586 * \warning if a DICOM_V3 header is supplied,
587 * groups < 0x0008 and shadow groups are ignored
588 * \warning NO TEST is performed on processor "Endiannity".
589 * @param fileName name of the file to be created
590 * (any already existing file is overwritten)
591 * @return false if write fails
594 bool FileHelper::WriteAcr (std::string const &fileName)
597 return Write(fileName);
601 * \brief Writes on disk A SINGLE Dicom file,
602 * @param fileName name of the file to be created
603 * (any already existing file is overwritten)
604 * @return false if write fails
606 bool FileHelper::Write(std::string const &fileName)
611 SetWriteFileTypeToImplicitVR();
612 CheckMandatoryElements();
615 SetWriteFileTypeToExplicitVR();
616 CheckMandatoryElements();
620 // Just to avoid further trouble if user create a file ex-nihilo,
621 // wants to write it as an ACR-NEMA file,
622 // and forget to create any Entry belonging to group 0008
624 // We add Recognition Code (RET)
625 if ( ! FileInternal->GetValEntry(0x0008, 0x0010) )
626 FileInternal->InsertValEntry("", 0x0008, 0x0010);
627 SetWriteFileTypeToACR();
630 SetWriteFileTypeToExplicitVR();
631 CheckMandatoryElements();
634 // --------------------------------------------------------------
635 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
637 // if recognition code tells us we dealt with a LibIDO image
638 // we reproduce on disk the switch between lineNumber and columnNumber
639 // just before writting ...
640 /// \todo the best trick would be *change* the recognition code
641 /// but pb expected if user deals with, e.g. COMPLEX images
642 if( WriteType == ACR_LIBIDO )
644 // if recognition code tells us we dealt with a LibIDO image
645 // we reproduce on disk the switch between lineNumber and columnNumber
646 // just before writting ...
647 /// \todo the best trick would be *change* the recognition code
648 /// but pb expected if user deals with, e.g. COMPLEX images
649 if( WriteType == ACR_LIBIDO )
655 SetWriteToNoLibido();
657 // ----------------- End of Special Patch ----------------
662 SetWriteToRaw(); // modifies and pushes to the archive, when necessary
665 SetWriteToRGB(); // modifies and pushes to the archive, when necessary
669 bool check = CheckWriteIntegrity(); // verifies length
672 check = FileInternal->Write(fileName,WriteType);
676 RestoreWriteFileType();
677 RestoreWriteMandatory();
679 // --------------------------------------------------------------
680 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
682 // ...and we restore the header to be Dicom Compliant again
683 // just after writting
684 RestoreWriteOfLibido();
685 // ----------------- End of Special Patch ----------------
690 //-----------------------------------------------------------------------------
693 * \brief Checks the write integrity
695 * The tests made are :
696 * - verify the size of the image to write with the possible write
697 * when the user set an image data
698 * @return true if check is successfull
700 bool FileHelper::CheckWriteIntegrity()
702 if(PixelWriteConverter->GetUserData())
704 int numberBitsAllocated = FileInternal->GetBitsAllocated();
705 if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
707 numberBitsAllocated = 16;
710 size_t decSize = FileInternal->GetXSize()
711 * FileInternal->GetYSize()
712 * FileInternal->GetZSize()
713 * ( numberBitsAllocated / 8 )
714 * FileInternal->GetSamplesPerPixel();
715 size_t rgbSize = decSize;
716 if( FileInternal->HasLUT() )
717 rgbSize = decSize * 3;
722 if( decSize!=PixelWriteConverter->GetUserDataSize() )
724 gdcmWarningMacro( "Data size (Raw) is incorrect. Should be "
725 << decSize << " / Found :"
726 << PixelWriteConverter->GetUserDataSize() );
731 if( rgbSize!=PixelWriteConverter->GetUserDataSize() )
733 gdcmWarningMacro( "Data size (RGB) is incorrect. Should be "
734 << decSize << " / Found "
735 << PixelWriteConverter->GetUserDataSize() );
746 * \brief Updates the File to write RAW data (as opposed to RGB data)
747 * (modifies, when necessary, photochromatic interpretation,
748 * bits allocated, Pixels element VR)
750 void FileHelper::SetWriteToRaw()
752 if( FileInternal->GetNumberOfScalarComponents() == 3
753 && !FileInternal->HasLUT())
759 ValEntry *photInt = CopyValEntry(0x0028,0x0004);
760 if(FileInternal->HasLUT())
762 photInt->SetValue("PALETTE COLOR ");
766 photInt->SetValue("MONOCHROME2 ");
769 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
770 PixelReadConverter->GetRawSize());
772 std::string vr = "OB";
773 if( FileInternal->GetBitsAllocated()>8 )
775 if( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
778 CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
779 pixel->SetValue(GDCM_BINLOADED);
780 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
781 pixel->SetLength(PixelWriteConverter->GetDataSize());
783 Archive->Push(photInt);
784 Archive->Push(pixel);
789 * \brief Updates the File to write RGB data (as opposed to RAW data)
790 * (modifies, when necessary, photochromatic interpretation,
791 * samples per pixel, Planar configuration,
792 * bits allocated, bits stored, high bit -ACR 24 bits-
793 * Pixels element VR, pushes out the LUT, )
795 void FileHelper::SetWriteToRGB()
797 if(FileInternal->GetNumberOfScalarComponents()==3)
799 PixelReadConverter->BuildRGBImage();
801 ValEntry *spp = CopyValEntry(0x0028,0x0002);
804 ValEntry *planConfig = CopyValEntry(0x0028,0x0006);
805 planConfig->SetValue("0 ");
807 ValEntry *photInt = CopyValEntry(0x0028,0x0004);
808 photInt->SetValue("RGB ");
810 if(PixelReadConverter->GetRGB())
812 PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
813 PixelReadConverter->GetRGBSize());
817 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
818 PixelReadConverter->GetRawSize());
821 std::string vr = "OB";
822 if( FileInternal->GetBitsAllocated()>8 )
824 if( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
827 CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
828 pixel->SetValue(GDCM_BINLOADED);
829 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
830 pixel->SetLength(PixelWriteConverter->GetDataSize());
833 Archive->Push(planConfig);
834 Archive->Push(photInt);
835 Archive->Push(pixel);
838 Archive->Push(0x0028,0x1101);
839 Archive->Push(0x0028,0x1102);
840 Archive->Push(0x0028,0x1103);
841 Archive->Push(0x0028,0x1201);
842 Archive->Push(0x0028,0x1202);
843 Archive->Push(0x0028,0x1203);
845 // For old '24 Bits' ACR-NEMA
846 // Thus, we have a RGB image and the bits allocated = 24 and
847 // samples per pixels = 1 (in the read file)
848 if(FileInternal->GetBitsAllocated()==24)
850 ValEntry *bitsAlloc = CopyValEntry(0x0028,0x0100);
851 bitsAlloc->SetValue("8 ");
853 ValEntry *bitsStored = CopyValEntry(0x0028,0x0101);
854 bitsStored->SetValue("8 ");
856 ValEntry *highBit = CopyValEntry(0x0028,0x0102);
857 highBit->SetValue("7 ");
859 Archive->Push(bitsAlloc);
860 Archive->Push(bitsStored);
861 Archive->Push(highBit);
871 * \brief Restore the File write mode
873 void FileHelper::RestoreWrite()
875 Archive->Restore(0x0028,0x0002);
876 Archive->Restore(0x0028,0x0004);
877 Archive->Restore(0x0028,0x0006);
878 Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
880 // For old ACR-NEMA (24 bits problem)
881 Archive->Restore(0x0028,0x0100);
882 Archive->Restore(0x0028,0x0101);
883 Archive->Restore(0x0028,0x0102);
886 Archive->Restore(0x0028,0x1101);
887 Archive->Restore(0x0028,0x1102);
888 Archive->Restore(0x0028,0x1103);
889 Archive->Restore(0x0028,0x1201);
890 Archive->Restore(0x0028,0x1202);
891 Archive->Restore(0x0028,0x1203);
893 // group 0002 may be pushed out for ACR-NEMA writting purposes
894 Archive->Restore(0x0002,0x0000);
895 Archive->Restore(0x0002,0x0001);
896 Archive->Restore(0x0002,0x0002);
897 Archive->Restore(0x0002,0x0003);
898 Archive->Restore(0x0002,0x0010);
899 Archive->Restore(0x0002,0x0012);
900 Archive->Restore(0x0002,0x0013);
901 Archive->Restore(0x0002,0x0016);
902 Archive->Restore(0x0002,0x0100);
903 Archive->Restore(0x0002,0x0102);
907 * \brief Pushes out the whole group 0002
908 * FIXME : better, set a flag to tell the writer not to write it ...
909 * FIXME : method should probably have an other name !
910 * SetWriteFileTypeToACR is NOT opposed to
911 * SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
913 void FileHelper::SetWriteFileTypeToACR()
915 Archive->Push(0x0002,0x0000);
916 Archive->Push(0x0002,0x0001);
917 Archive->Push(0x0002,0x0002);
918 Archive->Push(0x0002,0x0003);
919 Archive->Push(0x0002,0x0010);
920 Archive->Push(0x0002,0x0012);
921 Archive->Push(0x0002,0x0013);
922 Archive->Push(0x0002,0x0016);
923 Archive->Push(0x0002,0x0100);
924 Archive->Push(0x0002,0x0102);
928 * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"
930 void FileHelper::SetWriteFileTypeToExplicitVR()
932 std::string ts = Util::DicomString(
933 Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
935 ValEntry *tss = CopyValEntry(0x0002,0x0010);
942 * \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"
944 void FileHelper::SetWriteFileTypeToImplicitVR()
946 std::string ts = Util::DicomString(
947 Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
949 ValEntry *tss = CopyValEntry(0x0002,0x0010);
957 * \brief Restore in the File the initial group 0002
959 void FileHelper::RestoreWriteFileType()
964 * \brief Set the Write not to Libido format
966 void FileHelper::SetWriteToLibido()
968 ValEntry *oldRow = dynamic_cast<ValEntry *>
969 (FileInternal->GetDocEntry(0x0028, 0x0010));
970 ValEntry *oldCol = dynamic_cast<ValEntry *>
971 (FileInternal->GetDocEntry(0x0028, 0x0011));
973 if( oldRow && oldCol )
975 std::string rows, columns;
977 ValEntry *newRow=new ValEntry(oldRow->GetDictEntry());
978 ValEntry *newCol=new ValEntry(oldCol->GetDictEntry());
980 newRow->Copy(oldCol);
981 newCol->Copy(oldRow);
983 newRow->SetValue(oldCol->GetValue());
984 newCol->SetValue(oldRow->GetValue());
986 Archive->Push(newRow);
987 Archive->Push(newCol);
990 ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
991 libidoCode->SetValue("ACRNEMA_LIBIDO_1.1");
992 Archive->Push(libidoCode);
996 * \brief Set the Write not to No Libido format
998 void FileHelper::SetWriteToNoLibido()
1000 ValEntry *recCode = dynamic_cast<ValEntry *>
1001 (FileInternal->GetDocEntry(0x0008,0x0010));
1004 if( recCode->GetValue() == "ACRNEMA_LIBIDO_1.1" )
1006 ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
1007 libidoCode->SetValue("");
1008 Archive->Push(libidoCode);
1014 * \brief Restore the Write format
1016 void FileHelper::RestoreWriteOfLibido()
1018 Archive->Restore(0x0028,0x0010);
1019 Archive->Restore(0x0028,0x0011);
1020 Archive->Restore(0x0008,0x0010);
1022 // Restore 'LibIDO-special' entries, if any
1023 Archive->Restore(0x0028,0x0015);
1024 Archive->Restore(0x0028,0x0016);
1025 Archive->Restore(0x0028,0x0017);
1026 Archive->Restore(0x0028,0x00199);
1030 * \brief Duplicates a ValEntry or creates it.
1031 * @param group Group number of the Entry
1032 * @param elem Element number of the Entry
1033 * \return pointer to the new Val Entry (NULL when creation failed).
1035 ValEntry *FileHelper::CopyValEntry(uint16_t group, uint16_t elem)
1037 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1042 newE = new ValEntry(oldE->GetDictEntry());
1047 newE = GetFile()->NewValEntry(group, elem);
1054 * \brief Duplicates a BinEntry or creates it.
1055 * @param group Group number of the Entry
1056 * @param elem Element number of the Entry
1057 * @param vr Value Representation of the Entry
1058 * FIXME : what is it used for?
1059 * \return pointer to the new Bin Entry (NULL when creation failed).
1061 BinEntry *FileHelper::CopyBinEntry(uint16_t group, uint16_t elem,
1062 const std::string &vr)
1064 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1068 if( oldE->GetVR()!=vr )
1073 newE = new BinEntry(oldE->GetDictEntry());
1078 newE = GetFile()->NewBinEntry(group, elem, vr);
1085 * \brief This method is called automatically, just before writting
1086 * in order to produce a 'True Dicom V3' image
1087 * We cannot know *how* the user made the File (reading an old ACR-NEMA
1088 * file or a not very clean DICOM file ...)
1090 * Just before writting :
1091 * - we check the Entries
1092 * - we create the mandatory entries if they are missing
1093 * - we modify the values if necessary
1094 * - we push the sensitive entries to the Archive
1095 * The writing process will restore the entries as they where before
1096 * entering FileHelper::CheckMandatoryElements, so the user will always
1097 * see the entries just as he left them.
1099 * \todo : - warn the user if we had to add some entries :
1100 * even if a mandatory entry is missing, we add it, with a default value
1101 * (we don't want to give up the writting process if user forgot to
1102 * specify Lena's Patient ID, for instance ...)
1103 * - read the whole PS 3.3 Part of DICOM (890 pages)
1104 * and write a *full* checker (probably one method per Modality ...)
1105 * Any contribution is welcome.
1106 * - write a user callable full checker, to allow post reading
1107 * and/or pre writting image consistency check.
1110 void FileHelper::CheckMandatoryElements()
1112 // just to remember : 'official' 0002 group
1114 //0002 0000 UL 1 Meta Group Length
1115 //0002 0001 OB 1 File Meta Information Version
1116 //0002 0002 UI 1 Media Stored SOP Class UID
1117 //0002 0003 UI 1 Media Stored SOP Instance UID
1118 //0002 0010 UI 1 Transfer Syntax UID
1119 //0002 0012 UI 1 Implementation Class UID
1120 //0002 0013 SH 1 Implementation Version Name
1121 //0002 0016 AE 1 Source Application Entity Title
1122 //0002 0100 UI 1 Private Information Creator
1123 //0002 0102 OB 1 Private Information
1125 // Create them if not found
1126 // Always modify the value
1127 // Push the entries to the archive.
1128 ValEntry *e_0002_0000 = CopyValEntry(0x0002,0x0000);
1129 e_0002_0000->SetValue("0"); // for the moment
1130 Archive->Push(e_0002_0000);
1132 BinEntry *e_0002_0001 = CopyBinEntry(0x0002,0x0001, "OB");
1133 e_0002_0001->SetBinArea((uint8_t*)Util::GetFileMetaInformationVersion(),
1135 e_0002_0001->SetLength(2);
1136 Archive->Push(e_0002_0001);
1138 ValEntry *e_0002_0002 = CopyValEntry(0x0002,0x0002);
1139 // [Secondary Capture Image Storage]
1140 e_0002_0002->SetValue("1.2.840.10008.5.1.4.1.1.7");
1141 Archive->Push(e_0002_0002);
1143 // 'Media Stored SOP Instance UID'
1144 ValEntry *e_0002_0003 = CopyValEntry(0x0002,0x0003);
1145 e_0002_0003->SetValue(Util::CreateUniqueUID());
1146 Archive->Push(e_0002_0003);
1148 // 'Implementation Class UID'
1149 ValEntry *e_0002_0012 = CopyValEntry(0x0002,0x0012);
1150 e_0002_0012->SetValue(Util::CreateUniqueUID());
1151 Archive->Push(e_0002_0012);
1153 // 'Implementation Version Name'
1154 ValEntry *e_0002_0013 = CopyValEntry(0x0002,0x0013);
1155 e_0002_0013->SetValue("GDCM 1.0");
1156 Archive->Push(e_0002_0013);
1158 //'Source Application Entity Title' Not Mandatory
1159 //ValEntry *e_0002_0016 = CopyValEntry(0x0002,0x0016);
1160 // e_0002_0016->SetValue("1.2.840.10008.5.1.4.1.1.7");
1161 // Archive->Push(e_0002_0016);
1163 // Push out 'LibIDO-special' entries, if any
1164 Archive->Push(0x0028,0x0015);
1165 Archive->Push(0x0028,0x0016);
1166 Archive->Push(0x0028,0x0017);
1167 Archive->Push(0x0028,0x00199);
1169 // --- Check UID-related Entries ---
1171 // If 'SOP Class UID' exists ('true DICOM' image)
1172 // we create the 'Source Image Sequence' SeqEntry
1173 // to hold informations about the Source Image
1175 ValEntry *e_0008_0016 = FileInternal->GetValEntry(0x0008, 0x0016);
1176 if ( e_0008_0016 != 0 )
1178 // Create 'Source Image Sequence' SeqEntry
1179 SeqEntry *s = new SeqEntry (
1180 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x2112) );
1181 SQItem *sqi = new SQItem(1);
1182 // (we assume 'SOP Instance UID' exists too)
1183 // create 'Referenced SOP Class UID'
1184 ValEntry *e_0008_1150 = new ValEntry(
1185 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1150) );
1186 e_0008_1150->SetValue( e_0008_0016->GetValue());
1187 sqi->AddEntry(e_0008_1150);
1189 // create 'Referenced SOP Instance UID'
1190 ValEntry *e_0008_0018 = FileInternal->GetValEntry(0x0008, 0x0018);
1191 ValEntry *e_0008_1155 = new ValEntry(
1192 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1155) );
1193 e_0008_1155->SetValue( e_0008_0018->GetValue());
1194 sqi->AddEntry(e_0008_1155);
1196 s->AddSQItem(sqi,1);
1197 // temporarily replaces any previous 'Source Image Sequence'
1200 // 'Image Type' (The written image is no longer an 'ORIGINAL' one)
1201 ValEntry *e_0008_0008 = CopyValEntry(0x0008,0x0008);
1202 e_0008_0008->SetValue("DERIVED\\PRIMARY");
1203 Archive->Push(e_0008_0008);
1207 // There was no 'SOP Class UID'.
1208 // the source image was NOT a true Dicom one.
1209 // We consider the image is a 'Secondary Capture' one
1211 e_0008_0016 = new ValEntry(
1212 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0016) );
1213 // [Secondary Capture Image Storage]
1214 e_0008_0016 ->SetValue("1.2.840.10008.5.1.4.1.1.7");
1215 Archive->Push(e_0008_0016);
1218 // ---- The user will never have to take any action on the following ----.
1220 // new value for 'SOP Instance UID'
1221 ValEntry *e_0008_0018 = new ValEntry(
1222 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0018) );
1223 e_0008_0018->SetValue( Util::CreateUniqueUID() );
1224 Archive->Push(e_0008_0018);
1226 // Instance Creation Date
1227 ValEntry *e_0008_0012 = CopyValEntry(0x0008,0x0012);
1228 std::string date = Util::GetCurrentDate();
1229 e_0008_0012->SetValue(date.c_str());
1230 Archive->Push(e_0008_0012);
1232 // Instance Creation Time
1233 ValEntry *e_0008_0013 = CopyValEntry(0x0008,0x0013);
1234 std::string time = Util::GetCurrentTime();
1235 e_0008_0013->SetValue(time.c_str());
1236 Archive->Push(e_0008_0013);
1238 // ----- Add Mandatory Entries if missing ---
1240 // Entries whose type is 1 are mandatory, with a mandatory value
1241 // Entries whose type is 1c are mandatory-inside-a-Sequence
1242 // Entries whose type is 2 are mandatory, with a optional value
1243 // Entries whose type is 2c are mandatory-inside-a-Sequence
1244 // Entries whose type is 3 are optional
1246 // 'Serie Instance UID'
1247 // Keep the value if exists
1248 // The user is allowed to create his own Series,
1249 // keeping the same 'Serie Instance UID' for various images
1250 // The user shouldn't add any image to a 'Manufacturer Serie'
1251 // but there is no way no to allowed him to do that
1252 ValEntry *e_0020_000e = FileInternal->GetValEntry(0x0020, 0x000e);
1255 e_0020_000e = new ValEntry(
1256 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000e) );
1257 e_0020_000e->SetValue(Util::CreateUniqueUID() );
1258 Archive->Push(e_0020_000e);
1261 // 'Study Instance UID'
1262 // Keep the value if exists
1263 // The user is allowed to create his own Study,
1264 // keeping the same 'Study Instance UID' for various images
1265 // The user may add images to a 'Manufacturer Study',
1266 // adding new series to an already existing Study
1267 ValEntry *e_0020_000d = FileInternal->GetValEntry(0x0020, 0x000d);
1270 e_0020_000d = new ValEntry(
1271 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0020, 0x000d) );
1272 e_0020_000d->SetValue(Util::CreateUniqueUID() );
1273 Archive->Push(e_0020_000d);
1276 // Modality : if missing we set it to 'OTher'
1277 ValEntry *e_0008_0060 = FileInternal->GetValEntry(0x0008, 0x0060);
1280 e_0008_0060 = new ValEntry(
1281 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0060) );
1282 e_0008_0060->SetValue("OT");
1283 Archive->Push(e_0008_0060);
1286 // Manufacturer : if missing we set it to 'GDCM Factory'
1287 ValEntry *e_0008_0070 = FileInternal->GetValEntry(0x0008, 0x0070);
1290 e_0008_0070 = new ValEntry(
1291 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0070) );
1292 e_0008_0070->SetValue("GDCM Factory");
1293 Archive->Push(e_0008_0070);
1296 // Institution Name : if missing we set it to 'GDCM Hospital'
1297 ValEntry *e_0008_0080 = FileInternal->GetValEntry(0x0008, 0x0080);
1300 e_0008_0080 = new ValEntry(
1301 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0080) );
1302 e_0008_0080->SetValue("GDCM Hospital");
1303 Archive->Push(e_0008_0080);
1306 // Patient's Name : if missing, we set it to 'GDCM^Patient'
1307 ValEntry *e_0010_0010 = FileInternal->GetValEntry(0x0010, 0x0010);
1310 e_0010_0010 = new ValEntry(
1311 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0010) );
1312 e_0010_0010->SetValue("GDCM^Patient");
1313 Archive->Push(e_0010_0010);
1316 // Patient's Birth Date : 'type 2' entry -> must exist, value not mandatory
1317 ValEntry *e_0010_0030 = FileInternal->GetValEntry(0x0010, 0x0030);
1320 e_0010_0030 = new ValEntry(
1321 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0030) );
1322 e_0010_0030->SetValue("");
1323 Archive->Push(e_0010_0030);
1326 // Patient's Sex :'type 2' entry -> must exist, value not mandatory
1327 ValEntry *e_0010_0040 = FileInternal->GetValEntry(0x0010, 0x0040);
1330 e_0010_0040 = new ValEntry(
1331 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0010, 0x0040) );
1332 e_0010_0040->SetValue("");
1333 Archive->Push(e_0010_0040);
1336 // Referring Physician's Name :'type 2' entry -> must exist, value not mandatory
1337 ValEntry *e_0008_0090 = FileInternal->GetValEntry(0x0008, 0x0090);
1340 e_0008_0090 = new ValEntry(
1341 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x0090) );
1342 e_0008_0090->SetValue("");
1343 Archive->Push(e_0008_0090);
1346 // Remove some inconstencies (probably some more will be added)
1348 // if (0028 0008)Number of Frames exists
1349 // Push out (0020 0052),Frame of Reference UID
1350 // (only meaningfull within a Serie)
1351 ValEntry *e_0028_0008 = FileInternal->GetValEntry(0x0028, 0x0008);
1354 Archive->Push(0x0020, 0X0052);
1359 * \brief Restore in the File the initial group 0002
1361 void FileHelper::RestoreWriteMandatory()
1363 // group 0002 may be pushed out for ACR-NEMA writting purposes
1364 Archive->Restore(0x0002,0x0000);
1365 Archive->Restore(0x0002,0x0001);
1366 Archive->Restore(0x0002,0x0002);
1367 Archive->Restore(0x0002,0x0003);
1368 Archive->Restore(0x0002,0x0012);
1369 Archive->Restore(0x0002,0x0013);
1370 Archive->Restore(0x0002,0x0016);
1371 Archive->Restore(0x0002,0x0100);
1372 Archive->Restore(0x0002,0x0102);
1375 //-----------------------------------------------------------------------------
1378 * \brief Factorization for various forms of constructors.
1380 void FileHelper::Initialize()
1382 WriteMode = WMODE_RAW;
1383 WriteType = ExplicitVR;
1385 PixelReadConverter = new PixelReadConvert;
1386 PixelWriteConverter = new PixelWriteConvert;
1387 Archive = new DocEntryArchive( FileInternal );
1389 if ( FileInternal->IsReadable() )
1391 PixelReadConverter->GrabInformationsFromFile( FileInternal );
1398 uint8_t *FileHelper::GetRaw()
1400 uint8_t *raw = PixelReadConverter->GetRaw();
1403 // The Raw image migth not be loaded yet:
1404 std::ifstream *fp = FileInternal->OpenFile();
1405 PixelReadConverter->ReadAndDecompressPixelData( fp );
1407 FileInternal->CloseFile();
1409 raw = PixelReadConverter->GetRaw();
1412 gdcmWarningMacro( "Read/decompress of pixel data apparently went wrong.");
1420 //-----------------------------------------------------------------------------
1422 void FileHelper::Print(std::ostream &os, std::string const &)
1424 FileInternal->SetPrintLevel(PrintLevel);
1425 FileInternal->Print(os);
1427 PixelReadConverter->SetPrintLevel(PrintLevel);
1428 PixelReadConverter->Print(os);
1431 //-----------------------------------------------------------------------------
1432 } // end namespace gdcm