1 /*=========================================================================
4 Module: $RCSfile: gdcmFileHelper.cxx,v $
7 Date: $Date: 2006/03/01 09:45:04 $
8 Version: $Revision: 1.96 $
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"
30 #include "gdcmPixelReadConvert.h"
31 #include "gdcmPixelWriteConvert.h"
32 #include "gdcmDocEntryArchive.h"
33 #include "gdcmDictSet.h"
34 #include "gdcmOrientation.h"
36 #if defined(__BORLANDC__)
37 #include <mem.h> // for memset
43 // ----------------------------- WARNING -------------------------
45 These lines will be moved to the document-to-be 'User's Guide'
47 // To read an image, user needs a gdcm::File
48 gdcm::File *f = new gdcm::File(fileName);
50 // user may also decide he doesn't want to load some parts of the header
51 gdcm::File *f = new gdcm::File();
52 f->SetFileName(fileName);
53 f->SetLoadMode(LD_NOSEQ); // or
54 f->SetLoadMode(LD_NOSHADOW); // or
55 f->SetLoadMode(LD_NOSEQ | LD_NOSHADOW); // or
56 f->SetLoadMode(LD_NOSHADOWSEQ);
59 // user can now check some values
60 std::string v = f->GetEntryValue(groupNb,ElementNb);
62 // to get the pixels, user needs a gdcm::FileHelper
63 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
64 // user may ask not to convert Palette to RGB
65 uint8_t *pixels = fh->GetImageDataRaw();
66 int imageLength = fh->GetImageDataRawSize();
67 // He can now use the pixels, create a new image, ...
68 uint8_t *userPixels = ...
70 To re-write the image, user re-uses the gdcm::FileHelper
72 fh->SetImageData( userPixels, userPixelsLength);
73 fh->SetTypeToRaw(); // Even if it was possible to convert Palette to RGB
76 fh->SetWriteTypeToDcmExpl(); // he wants Explicit Value Representation
77 // Little Endian is the default
78 // no other value is allowed
79 (-->SetWriteType(ExplicitVR);)
80 -->WriteType = ExplicitVR;
81 fh->Write(newFileName); // overwrites the file, if any
84 fh->WriteDcmExplVR(newFileName);
87 // ----------------------------- WARNING -------------------------
89 These lines will be moved to the document-to-be 'Developer's Guide'
91 WriteMode : WMODE_RAW / WMODE_RGB
92 WriteType : ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO
94 fh1->Write(newFileName);
95 SetWriteFileTypeToImplicitVR() / SetWriteFileTypeToExplicitVR();
96 (modifies TransferSyntax)
97 SetWriteToRaw(); / SetWriteToRGB();
98 (modifies, when necessary : photochromatic interpretation,
99 samples per pixel, Planar configuration,
100 bits allocated, bits stored, high bit -ACR 24 bits-
101 Pixels element VR, pushes out the LUT )
102 CheckWriteIntegrity();
103 (checks user given pixels length)
104 FileInternal->Write(fileName,WriteType)
105 fp = opens file(fileName);
106 ComputeGroup0002Length( );
108 RemoveEntry(palettes, etc)
109 Document::WriteContent(fp, writetype);
111 (moves back to the File all the archived elements)
112 RestoreWriteFileType();
113 (pushes back group 0002, with TransferSyntax)
121 typedef std::map<uint16_t, int> GroupHT; // Hash Table
122 //-------------------------------------------------------------------------
123 // Constructor / Destructor
125 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
126 * file (gdcm::File only deals with the ... header)
127 * Opens (in read only and when possible) an existing file and checks
128 * for DICOM compliance. Returns NULL on failure.
129 * It will be up to the user to load the pixels into memory
130 * ( GetImageDataSize() + GetImageData() methods)
131 * \note the in-memory representation of all available tags found in
132 * the DICOM header is post-poned to first header information access.
133 * This avoid a double parsing of public part of the header when
134 * one sets an a posteriori shadow dictionary (efficiency can be
135 * seen as a side effect).
137 FileHelper::FileHelper( )
139 FileInternal = File::New( );
144 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
145 * file (File only deals with the ... header)
146 * Opens (in read only and when possible) an existing file and checks
147 * for DICOM compliance. Returns NULL on failure.
148 * It will be up to the user to load the pixels into memory
149 * ( GetImageDataSize() + GetImageData() methods)
150 * \note the in-memory representation of all available tags found in
151 * the DICOM header is post-poned to first header information access.
152 * This avoid a double parsing of public part of the header when
153 * user sets an a posteriori shadow dictionary (efficiency can be
154 * seen as a side effect).
155 * @param header already built Header
157 FileHelper::FileHelper(File *header)
159 gdcmAssertMacro(header);
161 FileInternal = header;
162 FileInternal->Register();
164 if ( FileInternal->IsReadable() )
166 PixelReadConverter->GrabInformationsFromFile( FileInternal, this );
171 * \brief canonical destructor
172 * \note If the header (gdcm::File) was created by the FileHelper constructor,
173 * it is destroyed by the FileHelper
175 FileHelper::~FileHelper()
177 if ( PixelReadConverter )
179 delete PixelReadConverter;
181 if ( PixelWriteConverter )
183 delete PixelWriteConverter;
190 FileInternal->Unregister();
193 //-----------------------------------------------------------------------------
197 * \brief Sets the LoadMode of the internal gdcm::File as a boolean string.
198 * NO_SEQ, NO_SHADOW, NO_SHADOWSEQ ... (nothing more, right now)
199 * WARNING : before using NO_SHADOW, be sure *all* your files
200 * contain accurate values in the 0x0000 element (if any)
201 * of *each* Shadow Group. The parser will fail if the size is wrong !
202 * @param loadMode Load mode to be used
204 void FileHelper::SetLoadMode(int loadMode)
206 GetFile()->SetLoadMode( loadMode );
209 * \brief Sets the LoadMode of the internal gdcm::File
210 * @param fileName name of the file to be open
212 void FileHelper::SetFileName(std::string const &fileName)
214 FileInternal->SetFileName( fileName );
219 * @return false if file cannot be open or no swap info was found,
220 * or no tag was found.
222 bool FileHelper::Load()
224 if ( !FileInternal->Load() )
227 PixelReadConverter->GrabInformationsFromFile( FileInternal, this );
232 * \brief Accesses an existing DataEntry through it's (group, element)
233 * and modifies it's content with the given value.
234 * @param content new value (string) to substitute with
235 * @param group group number of the Dicom Element to modify
236 * @param elem element number of the Dicom Element to modify
237 * \return false if DataEntry not found
239 bool FileHelper::SetEntryString(std::string const &content,
240 uint16_t group, uint16_t elem)
242 return FileInternal->SetEntryString(content, group, elem);
247 * \brief Accesses an existing DataEntry through it's (group, element)
248 * and modifies it's content with the given value.
249 * @param content new value (void* -> uint8_t*) to substitute with
250 * @param lgth new value length
251 * @param group group number of the Dicom Element to modify
252 * @param elem element number of the Dicom Element to modify
253 * \return false if DataEntry not found
255 bool FileHelper::SetEntryBinArea(uint8_t *content, int lgth,
256 uint16_t group, uint16_t elem)
258 return FileInternal->SetEntryBinArea(content, lgth, group, elem);
262 * \brief Modifies the value of a given DataEntry when it exists.
263 * Creates it with the given value when unexistant.
264 * @param content (string) value to be set
265 * @param group Group number of the Entry
266 * @param elem Element number of the Entry
267 * \return pointer to the modified/created DataEntry (NULL when creation
270 DataEntry *FileHelper::InsertEntryString(std::string const &content,
271 uint16_t group, uint16_t elem)
273 return FileInternal->InsertEntryString(content, group, elem);
277 * \brief Modifies the value of a given DataEntry when it exists.
278 * Creates it with the given value when unexistant.
279 * A copy of the binArea is made to be kept in the Document.
280 * @param binArea (binary)value to be set
281 * @param lgth new value length
282 * @param group Group number of the Entry
283 * @param elem Element number of the Entry
284 * \return pointer to the modified/created DataEntry (NULL when creation
287 DataEntry *FileHelper::InsertEntryBinArea(uint8_t *binArea, int lgth,
288 uint16_t group, uint16_t elem)
290 return FileInternal->InsertEntryBinArea(binArea, lgth, group, elem);
294 * \brief Adds an empty SeqEntry
295 * (remove any existing entry with same group,elem)
296 * @param group Group number of the Entry
297 * @param elem Element number of the Entry
298 * \return pointer to the created SeqEntry (NULL when creation
301 SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
303 return FileInternal->InsertSeqEntry(group, elem);
307 * \brief Get the size of the image data
308 * If the image can be RGB (with a lut or by default), the size
309 * corresponds to the RGB image
310 * (use GetImageDataRawSize if you want to be sure to get *only*
311 * the size of the pixels)
312 * @return The image size
314 size_t FileHelper::GetImageDataSize()
316 if ( PixelWriteConverter->GetUserData() )
318 return PixelWriteConverter->GetUserDataSize();
320 return PixelReadConverter->GetRGBSize();
324 * \brief Get the size of the image data.
325 * If the image could be converted to RGB using a LUT,
326 * this transformation is not taken into account by GetImageDataRawSize
327 * (use GetImageDataSize if you wish)
328 * @return The raw image size
330 size_t FileHelper::GetImageDataRawSize()
332 if ( PixelWriteConverter->GetUserData() )
334 return PixelWriteConverter->GetUserDataSize();
336 return PixelReadConverter->GetRawSize();
340 * \brief brings pixels into memory :
341 * - Allocates necessary memory,
342 * - Reads the pixels from disk (uncompress if necessary),
343 * - Transforms YBR pixels, if any, into RGB pixels,
344 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
345 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
346 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
347 * @return Pointer to newly allocated pixel data.
348 * (uint8_t is just for prototyping. feel free to cast)
349 * NULL if alloc fails
351 uint8_t *FileHelper::GetImageData()
353 if ( PixelWriteConverter->GetUserData() )
355 return PixelWriteConverter->GetUserData();
360 // If the decompression failed nothing can be done.
364 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
366 return PixelReadConverter->GetRGB();
370 // When no LUT or LUT conversion fails, return the Raw
371 return PixelReadConverter->GetRaw();
376 * \brief brings pixels into memory :
377 * - Allocates necessary memory,
378 * - Transforms YBR pixels (if any) into RGB pixels
379 * - Transforms 3 planes R, G, B (if any) into a single RGB Plane
380 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
381 * - DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
382 * @return Pointer to newly allocated pixel data.
383 * (uint8_t is just for prototyping. feel free to cast)
384 * NULL if alloc fails
386 uint8_t *FileHelper::GetImageDataRaw ()
391 #ifndef GDCM_LEGACY_REMOVE
393 * \brief Useless function, since PixelReadConverter forces us
394 * copy the Pixels anyway.
395 * Reads the pixels from disk (uncompress if necessary),
396 * Transforms YBR pixels, if any, into RGB pixels
397 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
398 * Transforms single Grey plane + 3 Palettes into a RGB Plane
399 * Copies at most MaxSize bytes of pixel data to caller allocated
401 * \warning This function allows people that want to build a volume
402 * from an image stack *not to* have, first to get the image pixels,
403 * and then move them to the volume area.
404 * It's absolutely useless for any VTK user since vtk chooses
405 * to invert the lines of an image, that is the last line comes first
406 * (for some axis related reasons?). Hence he will have
407 * to load the image line by line, starting from the end.
408 * VTK users have to call GetImageData
410 * @param destination Address (in caller's memory space) at which the
411 * pixel data should be copied
412 * @param maxSize Maximum number of bytes to be copied. When MaxSize
413 * is not sufficient to hold the pixel data the copy is not
414 * executed (i.e. no partial copy).
415 * @return On success, the number of bytes actually copied. Zero on
416 * failure e.g. MaxSize is lower than necessary.
418 size_t FileHelper::GetImageDataIntoVector (void *destination, size_t maxSize)
422 // If the decompression failed nothing can be done.
426 if ( FileInternal->HasLUT() && PixelReadConverter->BuildRGBImage() )
428 if ( PixelReadConverter->GetRGBSize() > maxSize )
430 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
434 (void*)PixelReadConverter->GetRGB(),
435 PixelReadConverter->GetRGBSize() );
436 return PixelReadConverter->GetRGBSize();
439 // Either no LUT conversion necessary or LUT conversion failed
440 if ( PixelReadConverter->GetRawSize() > maxSize )
442 gdcmWarningMacro( "Pixel data bigger than caller's expected MaxSize");
446 (void *)PixelReadConverter->GetRaw(),
447 PixelReadConverter->GetRawSize() );
448 return PixelReadConverter->GetRawSize();
453 * \brief Points the internal pointer to the callers inData
454 * image representation, BUT WITHOUT COPYING THE DATA.
455 * 'image' Pixels are presented as C-like 2D arrays : line per line.
456 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
457 * \warning Since the pixels are not copied, it is the caller's responsability
458 * not to deallocate its data before gdcm uses them (e.g. with
459 * the Write() method )
460 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
461 * user is allowed to pass any kind of pixelsn since the size is
463 * @param expectedSize total image size, *in Bytes*
465 void FileHelper::SetImageData(uint8_t *inData, size_t expectedSize)
467 SetUserData(inData, expectedSize);
471 * \brief Set the image data defined by the user
472 * \warning When writting the file, this data are get as default data to write
473 * @param inData user supplied pixel area (uint8_t* is just for the compiler.
474 * user is allowed to pass any kind of pixels since the size is
476 * @param expectedSize total image size, *in Bytes*
478 void FileHelper::SetUserData(uint8_t *inData, size_t expectedSize)
480 PixelWriteConverter->SetUserData(inData, expectedSize);
484 * \brief Get the image data defined by the user
485 * \warning When writting the file, this data are get as default data to write
487 uint8_t *FileHelper::GetUserData()
489 return PixelWriteConverter->GetUserData();
493 * \brief Get the image data size defined by the user
494 * \warning When writting the file, this data are get as default data to write
496 size_t FileHelper::GetUserDataSize()
498 return PixelWriteConverter->GetUserDataSize();
502 * \brief Get the image data from the file.
503 * If a LUT is found, the data are expanded to be RGB
505 uint8_t *FileHelper::GetRGBData()
507 return PixelReadConverter->GetRGB();
511 * \brief Get the image data size from the file.
512 * If a LUT is found, the data are expanded to be RGB
514 size_t FileHelper::GetRGBDataSize()
516 return PixelReadConverter->GetRGBSize();
520 * \brief Get the image data from the file.
521 * Even when a LUT is found, the data are not expanded to RGB!
523 uint8_t *FileHelper::GetRawData()
525 return PixelReadConverter->GetRaw();
529 * \brief Get the image data size from the file.
530 * Even when a LUT is found, the data are not expanded to RGB!
532 size_t FileHelper::GetRawDataSize()
534 return PixelReadConverter->GetRawSize();
538 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT
540 uint8_t* FileHelper::GetLutRGBA()
542 if ( PixelReadConverter->GetLutRGBA() ==0 )
543 PixelReadConverter->BuildLUTRGBA();
544 return PixelReadConverter->GetLutRGBA();
548 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Number
550 int FileHelper::GetLutItemNumber()
552 return PixelReadConverter->GetLutItemNumber();
556 * \brief Access to the underlying \ref PixelReadConverter RGBA LUT Item Size
558 int FileHelper::GetLutItemSize()
560 return PixelReadConverter->GetLutItemSize();
564 * \brief Writes on disk A SINGLE Dicom file
565 * NO test is performed on processor "Endiannity".
566 * It's up to the user to call his Reader properly
567 * @param fileName name of the file to be created
568 * (any already existing file is over written)
569 * @return false if write fails
571 bool FileHelper::WriteRawData(std::string const &fileName)
573 std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
576 gdcmWarningMacro( "Fail to open (write) file:" << fileName.c_str());
580 if ( PixelWriteConverter->GetUserData() )
582 fp1.write( (char *)PixelWriteConverter->GetUserData(),
583 PixelWriteConverter->GetUserDataSize() );
585 else if ( PixelReadConverter->GetRGB() )
587 fp1.write( (char *)PixelReadConverter->GetRGB(),
588 PixelReadConverter->GetRGBSize());
590 else if ( PixelReadConverter->GetRaw() )
592 fp1.write( (char *)PixelReadConverter->GetRaw(),
593 PixelReadConverter->GetRawSize());
597 gdcmErrorMacro( "Nothing written." );
606 * \brief Writes on disk A SINGLE Dicom file,
607 * using the Implicit Value Representation convention
608 * NO test is performed on processor "Endianity".
609 * @param fileName name of the file to be created
610 * (any already existing file is overwritten)
611 * @return false if write fails
614 bool FileHelper::WriteDcmImplVR (std::string const &fileName)
616 SetWriteTypeToDcmImplVR();
617 return Write(fileName);
621 * \brief Writes on disk A SINGLE Dicom file,
622 * using the Explicit Value Representation convention
623 * NO test is performed on processor "Endiannity".
624 * @param fileName name of the file to be created
625 * (any already existing file is overwritten)
626 * @return false if write fails
629 bool FileHelper::WriteDcmExplVR (std::string const &fileName)
631 SetWriteTypeToDcmExplVR();
632 return Write(fileName);
636 * \brief Writes on disk A SINGLE Dicom file,
637 * using the ACR-NEMA convention
638 * NO test is performed on processor "Endiannity".
639 * (a l'attention des logiciels cliniques
640 * qui ne prennent en entrée QUE des images ACR ...
641 * \warning if a DICOM_V3 header is supplied,
642 * groups < 0x0008 and shadow groups are ignored
643 * \warning NO TEST is performed on processor "Endiannity".
644 * @param fileName name of the file to be created
645 * (any already existing file is overwritten)
646 * @return false if write fails
649 bool FileHelper::WriteAcr (std::string const &fileName)
652 return Write(fileName);
656 * \brief Writes on disk A SINGLE Dicom file,
657 * @param fileName name of the file to be created
658 * (any already existing file is overwritten)
659 * @return false if write fails
661 bool FileHelper::Write(std::string const &fileName)
666 SetWriteFileTypeToImplicitVR();
668 case Unknown: // should never happen; ExplicitVR is the default value
670 SetWriteFileTypeToExplicitVR();
674 // NOTHING is done here just for LibIDO.
675 // Just to avoid further trouble if user creates a file ex-nihilo,
676 // wants to write it as an ACR-NEMA file,
677 // and forgets to create any Entry belonging to group 0008
679 // We add Recognition Code (RET)
680 if ( ! FileInternal->GetDataEntry(0x0008, 0x0010) )
681 FileInternal->InsertEntryString("ACR-NEMA V1.0 ", 0x0008, 0x0010);
682 SetWriteFileTypeToACR();
683 // SetWriteFileTypeToImplicitVR(); // ACR IS implicit VR !
686 SetWriteFileTypeToJPEG();
689 CheckMandatoryElements();
691 // --------------------------------------------------------------
692 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
694 // if recognition code tells us we dealt with a LibIDO image
695 // we reproduce on disk the switch between lineNumber and columnNumber
696 // just before writting ...
697 /// \todo the best trick would be *change* the recognition code
698 /// but pb expected if user deals with, e.g. COMPLEX images
700 if ( WriteType == ACR_LIBIDO )
706 SetWriteToNoLibido();
708 // ----------------- End of Special Patch ----------------
713 SetWriteToRaw(); // modifies and pushes to the archive, when necessary
716 SetWriteToRGB(); // modifies and pushes to the archive, when necessary
720 bool check = CheckWriteIntegrity(); // verifies length
721 if (WriteType == JPEG ) check = true;
724 check = FileInternal->Write(fileName,WriteType);
728 RestoreWriteFileType();
729 RestoreWriteMandatory();
731 // --------------------------------------------------------------
732 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
734 // ...and we restore the header to be Dicom Compliant again
735 // just after writting
736 RestoreWriteOfLibido();
737 // ----------------- End of Special Patch ----------------
742 //-----------------------------------------------------------------------------
745 * \brief Checks the write integrity
747 * The tests made are :
748 * - verify the size of the image to write with the possible write
749 * when the user set an image data
750 * @return true if check is successfull
752 bool FileHelper::CheckWriteIntegrity()
754 if ( PixelWriteConverter->GetUserData() )
756 int numberBitsAllocated = FileInternal->GetBitsAllocated();
757 if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
759 gdcmWarningMacro( "numberBitsAllocated changed from "
760 << numberBitsAllocated << " to 16 "
761 << " for consistency purpose" );
762 numberBitsAllocated = 16;
765 size_t decSize = FileInternal->GetXSize()
766 * FileInternal->GetYSize()
767 * FileInternal->GetZSize()
768 * FileInternal->GetSamplesPerPixel()
769 * ( numberBitsAllocated / 8 );
770 size_t rgbSize = decSize;
771 if ( FileInternal->HasLUT() )
772 rgbSize = decSize * 3;
777 if ( decSize!=PixelWriteConverter->GetUserDataSize() )
779 gdcmWarningMacro( "Data size (Raw) is incorrect. Should be "
780 << decSize << " / Found :"
781 << PixelWriteConverter->GetUserDataSize() );
786 if ( rgbSize!=PixelWriteConverter->GetUserDataSize() )
788 gdcmWarningMacro( "Data size (RGB) is incorrect. Should be "
789 << decSize << " / Found "
790 << PixelWriteConverter->GetUserDataSize() );
801 * \brief Updates the File to write RAW data (as opposed to RGB data)
802 * (modifies, when necessary, photochromatic interpretation,
803 * bits allocated, Pixels element VR)
805 void FileHelper::SetWriteToRaw()
807 if ( FileInternal->GetNumberOfScalarComponents() == 3
808 && !FileInternal->HasLUT() )
814 DataEntry *photInt = CopyDataEntry(0x0028,0x0004);
815 if (FileInternal->HasLUT() )
817 photInt->SetString("PALETTE COLOR ");
821 photInt->SetString("MONOCHROME2 ");
824 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
825 PixelReadConverter->GetRawSize());
827 std::string vr = "OB";
828 if ( FileInternal->GetBitsAllocated()>8 )
830 if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
833 CopyDataEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
834 pixel->SetFlag(DataEntry::FLAG_PIXELDATA);
835 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
836 pixel->SetLength(PixelWriteConverter->GetDataSize());
838 Archive->Push(photInt);
839 Archive->Push(pixel);
847 * \brief Updates the File to write RGB data (as opposed to RAW data)
848 * (modifies, when necessary, photochromatic interpretation,
849 * samples per pixel, Planar configuration,
850 * bits allocated, bits stored, high bit -ACR 24 bits-
851 * Pixels element VR, pushes out the LUT, )
853 void FileHelper::SetWriteToRGB()
855 if ( FileInternal->GetNumberOfScalarComponents()==3 )
857 PixelReadConverter->BuildRGBImage();
859 DataEntry *spp = CopyDataEntry(0x0028,0x0002);
860 spp->SetString("3 ");
862 DataEntry *planConfig = CopyDataEntry(0x0028,0x0006);
863 planConfig->SetString("0 ");
865 DataEntry *photInt = CopyDataEntry(0x0028,0x0004);
866 photInt->SetString("RGB ");
868 if ( PixelReadConverter->GetRGB() )
870 PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
871 PixelReadConverter->GetRGBSize());
875 PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
876 PixelReadConverter->GetRawSize());
879 std::string vr = "OB";
880 if ( FileInternal->GetBitsAllocated()>8 )
882 if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
885 CopyDataEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
886 pixel->SetFlag(DataEntry::FLAG_PIXELDATA);
887 pixel->SetBinArea(PixelWriteConverter->GetData(),false);
888 pixel->SetLength(PixelWriteConverter->GetDataSize());
891 Archive->Push(planConfig);
892 Archive->Push(photInt);
893 Archive->Push(pixel);
896 planConfig->Delete();
901 Archive->Push(0x0028,0x1101);
902 Archive->Push(0x0028,0x1102);
903 Archive->Push(0x0028,0x1103);
904 Archive->Push(0x0028,0x1201);
905 Archive->Push(0x0028,0x1202);
906 Archive->Push(0x0028,0x1203);
908 // push out Palette Color Lookup Table UID, if any
909 Archive->Push(0x0028,0x1199);
911 // For old '24 Bits' ACR-NEMA
912 // Thus, we have a RGB image and the bits allocated = 24 and
913 // samples per pixels = 1 (in the read file)
914 if ( FileInternal->GetBitsAllocated()==24 )
916 DataEntry *bitsAlloc = CopyDataEntry(0x0028,0x0100);
917 bitsAlloc->SetString("8 ");
919 DataEntry *bitsStored = CopyDataEntry(0x0028,0x0101);
920 bitsStored->SetString("8 ");
922 DataEntry *highBit = CopyDataEntry(0x0028,0x0102);
923 highBit->SetString("7 ");
925 Archive->Push(bitsAlloc);
926 Archive->Push(bitsStored);
927 Archive->Push(highBit);
930 bitsStored->Delete();
941 * \brief Restore the File write mode
943 void FileHelper::RestoreWrite()
945 Archive->Restore(0x0028,0x0002);
946 Archive->Restore(0x0028,0x0004);
947 Archive->Restore(0x0028,0x0006);
948 Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
950 // For old ACR-NEMA (24 bits problem)
951 Archive->Restore(0x0028,0x0100);
952 Archive->Restore(0x0028,0x0101);
953 Archive->Restore(0x0028,0x0102);
956 Archive->Restore(0x0028,0x1101);
957 Archive->Restore(0x0028,0x1102);
958 Archive->Restore(0x0028,0x1103);
959 Archive->Restore(0x0028,0x1201);
960 Archive->Restore(0x0028,0x1202);
961 Archive->Restore(0x0028,0x1203);
963 // For the Palette Color Lookup Table UID
964 Archive->Restore(0x0028,0x1203);
967 // group 0002 may be pushed out for ACR-NEMA writting purposes
968 Archive->Restore(0x0002,0x0000);
969 Archive->Restore(0x0002,0x0001);
970 Archive->Restore(0x0002,0x0002);
971 Archive->Restore(0x0002,0x0003);
972 Archive->Restore(0x0002,0x0010);
973 Archive->Restore(0x0002,0x0012);
974 Archive->Restore(0x0002,0x0013);
975 Archive->Restore(0x0002,0x0016);
976 Archive->Restore(0x0002,0x0100);
977 Archive->Restore(0x0002,0x0102);
981 * \brief Pushes out the whole group 0002
982 * FIXME : better, set a flag to tell the writer not to write it ...
983 * FIXME : method should probably have an other name !
984 * SetWriteFileTypeToACR is NOT opposed to
985 * SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
987 void FileHelper::SetWriteFileTypeToACR()
989 Archive->Push(0x0002,0x0000);
990 Archive->Push(0x0002,0x0001);
991 Archive->Push(0x0002,0x0002);
992 Archive->Push(0x0002,0x0003);
993 Archive->Push(0x0002,0x0010);
994 Archive->Push(0x0002,0x0012);
995 Archive->Push(0x0002,0x0013);
996 Archive->Push(0x0002,0x0016);
997 Archive->Push(0x0002,0x0100);
998 Archive->Push(0x0002,0x0102);
1002 * \brief Sets in the File the TransferSyntax to 'JPEG'
1004 void FileHelper::SetWriteFileTypeToJPEG()
1006 std::string ts = Util::DicomString(
1007 Global::GetTS()->GetSpecialTransferSyntax(TS::JPEGBaselineProcess1) );
1009 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1017 * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"
1019 void FileHelper::SetWriteFileTypeToExplicitVR()
1021 std::string ts = Util::DicomString(
1022 Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
1024 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1032 * \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"
1034 void FileHelper::SetWriteFileTypeToImplicitVR()
1036 std::string ts = Util::DicomString(
1037 Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
1039 DataEntry *tss = CopyDataEntry(0x0002,0x0010);
1048 * \brief Restore in the File the initial group 0002
1050 void FileHelper::RestoreWriteFileType()
1055 * \brief Set the Write not to Libido format
1057 void FileHelper::SetWriteToLibido()
1059 DataEntry *oldRow = FileInternal->GetDataEntry(0x0028, 0x0010);
1060 DataEntry *oldCol = FileInternal->GetDataEntry(0x0028, 0x0011);
1062 if ( oldRow && oldCol )
1064 std::string rows, columns;
1066 DataEntry *newRow=DataEntry::New(oldRow->GetDictEntry());
1067 DataEntry *newCol=DataEntry::New(oldCol->GetDictEntry());
1069 newRow->Copy(oldCol);
1070 newCol->Copy(oldRow);
1072 newRow->SetString(oldCol->GetString());
1073 newCol->SetString(oldRow->GetString());
1075 Archive->Push(newRow);
1076 Archive->Push(newCol);
1082 DataEntry *libidoCode = CopyDataEntry(0x0008,0x0010);
1083 libidoCode->SetString("ACRNEMA_LIBIDO_1.1");
1084 Archive->Push(libidoCode);
1085 libidoCode->Delete();
1089 * \brief Set the Write not to No Libido format
1091 void FileHelper::SetWriteToNoLibido()
1093 DataEntry *recCode = FileInternal->GetDataEntry(0x0008,0x0010);
1096 if ( recCode->GetString() == "ACRNEMA_LIBIDO_1.1" )
1098 DataEntry *libidoCode = CopyDataEntry(0x0008,0x0010);
1099 libidoCode->SetString("");
1100 Archive->Push(libidoCode);
1101 libidoCode->Delete();
1107 * \brief Restore the Write format
1109 void FileHelper::RestoreWriteOfLibido()
1111 Archive->Restore(0x0028,0x0010);
1112 Archive->Restore(0x0028,0x0011);
1113 Archive->Restore(0x0008,0x0010);
1115 // Restore 'LibIDO-special' entries, if any
1116 Archive->Restore(0x0028,0x0015);
1117 Archive->Restore(0x0028,0x0016);
1118 Archive->Restore(0x0028,0x0017);
1119 Archive->Restore(0x0028,0x00199);
1123 * \brief Duplicates a DataEntry or creates it.
1124 * @param group Group number of the Entry
1125 * @param elem Element number of the Entry
1126 * @param vr Value Representation of the Entry
1127 * FIXME : what is it used for?
1128 * \return pointer to the new Bin Entry (NULL when creation failed).
1130 DataEntry *FileHelper::CopyDataEntry(uint16_t group, uint16_t elem,
1133 DocEntry *oldE = FileInternal->GetDocEntry(group, elem);
1136 if ( oldE && vr != GDCM_VRUNKNOWN )
1137 if ( oldE->GetVR() != vr )
1142 newE = DataEntry::New(oldE->GetDictEntry());
1147 newE = GetFile()->NewDataEntry(group, elem, vr);
1154 * \brief This method is called automatically, just before writting
1155 * in order to produce a 'True Dicom V3' image.
1157 * We cannot know *how* the user made the File :
1158 * (reading an old ACR-NEMA file or a not very clean DICOM file ...)
1159 * Just before writting :
1160 * - we check the Entries
1161 * - we create the mandatory entries if they are missing
1162 * - we modify the values if necessary
1163 * - we push the sensitive entries to the Archive
1164 * The writing process will restore the entries as they where before
1165 * entering FileHelper::CheckMandatoryElements, so the user will always
1166 * see the entries just as they were before he decided to write.
1169 * - Entries whose type is 1 are mandatory, with a mandatory value
1170 * - Entries whose type is 1c are mandatory-inside-a-Sequence,
1171 * with a mandatory value
1172 * - Entries whose type is 2 are mandatory, with an optional value
1173 * - Entries whose type is 2c are mandatory-inside-a-Sequence,
1174 * with an optional value
1175 * - Entries whose type is 3 are optional
1178 * - warn the user if we had to add some entries :
1179 * even if a mandatory entry is missing, we add it, with a default value
1180 * (we don't want to give up the writting process if user forgot to
1181 * specify Lena's Patient ID, for instance ...)
1182 * - read the whole PS 3.3 Part of DICOM (890 pages)
1183 * and write a *full* checker (probably one method per Modality ...)
1184 * Any contribution is welcome.
1185 * - write a user callable full checker, to allow post reading
1186 * and/or pre writting image consistency check.
1189 /* -------------------------------------------------------------------------------------
1190 To be moved to User's guide / WIKI ?
1192 We have to deal with 4 *very* different cases :
1193 -1) user created ex nihilo his own image and wants to write it as a Dicom image.
1195 -2) user modified the pixels of an existing image.
1197 -3) user created a new image, using existing images (eg MIP, MPR, cartography image)
1199 -4) user modified/added some tags *without processing* the pixels (anonymization..
1200 UNMODIFIED_PIXELS_IMAGE
1202 gdcm::FileHelper::CheckMandatoryElements() deals automatically with these cases.
1205 0008 0012 Instance Creation Date
1206 0008 0013 Instance Creation Time
1207 0008 0018 SOP Instance UID
1208 are *always* created with the current values; user has *no* possible intervention on
1211 'Serie Instance UID'(0x0020,0x000e)
1212 'Study Instance UID'(0x0020,0x000d) are kept as is if already exist,
1213 created if it doesn't.
1214 The user is allowed to create his own Series/Studies,
1215 keeping the same 'Serie Instance UID' / 'Study Instance UID' for various images
1217 The user shouldn't add any image to a 'Manufacturer Serie'
1218 but there is no way no to allow him to do that
1220 None of the 'shadow elements' are droped out.
1224 'Conversion Type (0x0008,0x0064) is forced to 'SYN' (Synthetic Image).
1227 'Media Storage SOP Class UID' (0x0002,0x0002)
1228 'SOP Class UID' (0x0008,0x0016) are set to
1229 [Secondary Capture Image Storage]
1230 'Image Type' (0x0008,0x0008) is forced to "DERIVED\PRIMARY"
1231 Conversion Type (0x0008,0x0064) is forced to 'SYN' (Synthetic Image)
1234 If 'SOP Class UID' exists in the native image ('true DICOM' image)
1235 we create the 'Source Image Sequence' SeqEntry (0x0008, 0x2112)
1236 --> 'Referenced SOP Class UID' (0x0008, 0x1150)
1237 whose value is the original 'SOP Class UID'
1238 --> 'Referenced SOP Instance UID' (0x0008, 0x1155)
1239 whose value is the original 'SOP Class UID'
1241 3) TODO : find a trick to allow user to pass to the writter the list of the Dicom images
1242 or the Series, (or the Study ?) he used to created his image
1243 (MIP, MPR, cartography image, ...)
1244 These info should be stored (?)
1245 0008 1110 SQ 1 Referenced Study Sequence
1246 0008 1115 SQ 1 Referenced Series Sequence
1247 0008 1140 SQ 1 Referenced Image Sequence
1249 4) When user *knows* he didn't modified the pixels, we keep some informations unchanged :
1250 'Media Storage SOP Class UID' (0x0002,0x0002)
1251 'SOP Class UID' (0x0008,0x0016)
1252 'Image Type' (0x0008,0x0008)
1253 'Conversion Type' (0x0008,0x0064)
1256 Bellow follows the full description (hope so !) of the consistency checks performed
1257 by gdcm::FileHelper::CheckMandatoryElements()
1260 -->'Media Storage SOP Class UID' (0x0002,0x0002)
1261 -->'SOP Class UID' (0x0008,0x0016) are defaulted to
1262 [Secondary Capture Image Storage]
1263 --> 'Image Type' (0x0008,0x0008)
1264 is forced to "DERIVED\PRIMARY"
1265 (The written image is no longer an 'ORIGINAL' one)
1266 Except if user knows he didn't modify the image (e.g. : he just anonymized the file)
1268 --> Conversion Type (0x0008,0x0064)
1269 is defaulted to 'SYN' (Synthetic Image)
1270 when *he* knows he created his own image ex nihilo
1272 --> 'Modality' (0x0008,0x0060)
1273 is defaulted to "OT" (other) if missing.
1274 (a fully user created image belongs to *no* modality)
1276 --> 'Media Storage SOP Instance UID' (0x0002,0x0003)
1277 --> 'Implementation Class UID' (0x0002,0x0012)
1278 are automatically generated; no user intervention possible
1280 --> 'Serie Instance UID'(0x0020,0x000e)
1281 --> 'Study Instance UID'(0x0020,0x000d) are kept as is if already exist
1282 created if it doesn't.
1283 The user is allowed to create his own Series/Studies,
1284 keeping the same 'Serie Instance UID' / 'Study Instance UID'
1287 The user shouldn't add any image to a 'Manufacturer Serie'
1288 but there is no way no to allowed him to do that
1290 --> If 'SOP Class UID' exists in the native image ('true DICOM' image)
1291 we create the 'Source Image Sequence' SeqEntry (0x0008, 0x2112)
1293 --> 'Referenced SOP Class UID' (0x0008, 0x1150)
1294 whose value is the original 'SOP Class UID'
1295 --> 'Referenced SOP Instance UID' (0x0008, 0x1155)
1296 whose value is the original 'SOP Class UID'
1298 --> Bits Stored, Bits Allocated, Hight Bit Position are checked for consistency
1299 --> Pixel Spacing (0x0028,0x0030) is defaulted to "1.0\1.0"
1300 --> Samples Per Pixel (0x0028,0x0002) is defaulted to 1 (grayscale)
1302 --> Imager Pixel Spacing (0x0018,0x1164) : defaulted to Pixel Spacing value
1304 --> Instance Creation Date, Instance Creation Time are forced to current Date and Time
1306 --> Study Date, Study Time are defaulted to current Date and Time
1307 (they remain unchanged if they exist)
1309 --> Patient Orientation : (0x0020,0x0020), if not present, is deduced from
1310 Image Orientation (Patient) : (0020|0037) or from
1311 Image Orientation (RET) : (0020 0035)
1313 --> Study ID, Series Number, Instance Number, Patient Orientation (Type 2)
1314 are created, with empty value if there are missing.
1316 --> Manufacturer, Institution Name, Patient's Name, (Type 2)
1317 are defaulted with a 'gdcm' value.
1319 --> Patient ID, Patient's Birth Date, Patient's Sex, (Type 2)
1320 --> Referring Physician's Name (Type 2)
1321 are created, with empty value if there are missing.
1323 -------------------------------------------------------------------------------------*/
1325 void FileHelper::CheckMandatoryElements()
1327 std::string sop = Util::CreateUniqueUID();
1329 // --------------------- For Meta Elements ---------------------
1330 // just to remember : 'official' 0002 group
1331 if ( WriteType != ACR && WriteType != ACR_LIBIDO )
1333 // Group 000002 (Meta Elements) already pushed out
1335 //0002 0000 UL 1 Meta Group Length
1336 //0002 0001 OB 1 File Meta Information Version
1337 //0002 0002 UI 1 Media Stored SOP Class UID
1338 //0002 0003 UI 1 Media Stored SOP Instance UID
1339 //0002 0010 UI 1 Transfer Syntax UID
1340 //0002 0012 UI 1 Implementation Class UID
1341 //0002 0013 SH 1 Implementation Version Name
1342 //0002 0016 AE 1 Source Application Entity Title
1343 //0002 0100 UI 1 Private Information Creator
1344 //0002 0102 OB 1 Private Information
1346 // Create them if not found
1347 // Always modify the value
1348 // Push the entries to the archive.
1349 CopyMandatoryEntry(0x0002,0x0000,"0");
1351 DataEntry *e_0002_0001 = CopyDataEntry(0x0002,0x0001, "OB");
1352 e_0002_0001->SetBinArea((uint8_t*)Util::GetFileMetaInformationVersion(),
1354 e_0002_0001->SetLength(2);
1355 Archive->Push(e_0002_0001);
1356 e_0002_0001->Delete();
1358 if ( ContentType == FILTERED_IMAGE || ContentType == UNMODIFIED_PIXELS_IMAGE)
1360 // we keep the original 'Media Storage SOP Class UID', we default it if missing
1361 CheckMandatoryEntry(0x0002,0x0002,"1.2.840.10008.5.1.4.1.1.7");
1365 // It's *not* an image comming straight from a source. We force
1366 // 'Media Storage SOP Class UID' --> [Secondary Capture Image Storage]
1367 CopyMandatoryEntry(0x0002,0x0002,"1.2.840.10008.5.1.4.1.1.7");
1370 // 'Media Storage SOP Instance UID'
1371 CopyMandatoryEntry(0x0002,0x0003,sop);
1373 // 'Implementation Class UID'
1374 // FIXME : in all examples we have, 0x0002,0x0012 is not so long :
1375 // seems to be Root UID + 4 digits (?)
1376 CopyMandatoryEntry(0x0002,0x0012,Util::CreateUniqueUID());
1378 // 'Implementation Version Name'
1379 std::string version = "GDCM ";
1380 version += Util::GetVersion();
1381 CopyMandatoryEntry(0x0002,0x0013,version);
1384 // --------------------- For DataSet ---------------------
1386 if ( ContentType == FILTERED_IMAGE || ContentType == UNMODIFIED_PIXELS_IMAGE)
1388 // we keep the original 'Media Storage SOP Class UID', we default it if missing (it should be present !)
1389 CheckMandatoryEntry(0x0008,0x0016,"1.2.840.10008.5.1.4.1.1.7");
1393 // It's *not* an image comming straight from a source. We force
1394 // 'Media Storage SOP Class UID' --> [Secondary Capture Image Storage]
1395 CopyMandatoryEntry(0x0008,0x0016,"1.2.840.10008.5.1.4.1.1.7");
1398 // Push out 'LibIDO-special' entries, if any
1399 Archive->Push(0x0028,0x0015);
1400 Archive->Push(0x0028,0x0016);
1401 Archive->Push(0x0028,0x0017);
1402 Archive->Push(0x0028,0x00199);
1404 // Deal with the pb of (Bits Stored = 12)
1405 // - we're gonna write the image as Bits Stored = 16
1406 if ( FileInternal->GetEntryString(0x0028,0x0100) == "12")
1408 CopyMandatoryEntry(0x0028,0x0100,"16");
1411 // Check if user wasn't drunk ;-)
1413 std::ostringstream s;
1414 // check 'Bits Allocated' vs decent values
1415 int nbBitsAllocated = FileInternal->GetBitsAllocated();
1416 if ( nbBitsAllocated == 0 || nbBitsAllocated > 32)
1418 CopyMandatoryEntry(0x0028,0x0100,"16");
1419 gdcmWarningMacro("(0028,0100) changed from "
1420 << nbBitsAllocated << " to 16 for consistency purpose");
1421 nbBitsAllocated = 16;
1423 // check 'Bits Stored' vs 'Bits Allocated'
1424 int nbBitsStored = FileInternal->GetBitsStored();
1425 if ( nbBitsStored == 0 || nbBitsStored > nbBitsAllocated )
1428 s << nbBitsAllocated;
1429 CopyMandatoryEntry(0x0028,0x0101,s.str());
1430 gdcmWarningMacro("(0028,0101) changed from "
1431 << nbBitsStored << " to " << nbBitsAllocated
1432 << " for consistency purpose" );
1433 nbBitsStored = nbBitsAllocated;
1435 // check 'Hight Bit Position' vs 'Bits Allocated' and 'Bits Stored'
1436 int highBitPosition = FileInternal->GetHighBitPosition();
1437 if ( highBitPosition == 0 ||
1438 highBitPosition > nbBitsAllocated-1 ||
1439 highBitPosition < nbBitsStored-1 )
1442 s << nbBitsStored - 1;
1443 CopyMandatoryEntry(0x0028,0x0102,s.str());
1444 gdcmWarningMacro("(0028,0102) changed from "
1445 << highBitPosition << " to " << nbBitsAllocated-1
1446 << " for consistency purpose");
1449 std::string pixelSpacing = FileInternal->GetEntryString(0x0028,0x0030);
1450 if ( pixelSpacing == GDCM_UNFOUND )
1452 pixelSpacing = "1.0\\1.0";
1453 // if missing, Pixel Spacing forced to "1.0\1.0"
1454 CopyMandatoryEntry(0x0028,0x0030,pixelSpacing);
1457 // 'Imager Pixel Spacing' : defaulted to 'Pixel Spacing'
1458 // --> This one is the *legal* one !
1459 if ( ContentType != USER_OWN_IMAGE)
1460 // we write it only when we are *sure* the image comes from
1461 // an imager (see also 0008,0x0064)
1462 CheckMandatoryEntry(0x0018,0x1164,pixelSpacing);
1464 // Samples Per Pixel (type 1) : default to grayscale
1465 CheckMandatoryEntry(0x0028,0x0002,"1");
1467 // --- Check UID-related Entries ---
1470 if ( ContentType != USER_OWN_IMAGE) // when it's not a user made image
1472 // If 'SOP Class UID' exists ('true DICOM' image)
1473 // we create the 'Source Image Sequence' SeqEntry
1474 // to hold informations about the Source Image
1476 DataEntry *e_0008_0016 = FileInternal->GetDataEntry(0x0008, 0x0016);
1479 // Create 'Source Image Sequence' SeqEntry
1480 SeqEntry *sis = SeqEntry::New (
1481 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x2112) );
1482 SQItem *sqi = SQItem::New(1);
1483 // (we assume 'SOP Instance UID' exists too)
1484 // create 'Referenced SOP Class UID'
1485 DataEntry *e_0008_1150 = DataEntry::New(
1486 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1150) );
1487 e_0008_1150->SetString( e_0008_0016->GetString());
1488 sqi->AddEntry(e_0008_1150);
1489 e_0008_1150->Delete();
1491 // create 'Referenced SOP Instance UID'
1492 DataEntry *e_0008_0018 = FileInternal->GetDataEntry(0x0008, 0x0018);
1493 DataEntry *e_0008_1155 = DataEntry::New(
1494 Global::GetDicts()->GetDefaultPubDict()->GetEntry(0x0008, 0x1155) );
1495 e_0008_1155->SetString( e_0008_0018->GetString());
1496 sqi->AddEntry(e_0008_1155);
1497 e_0008_1155->Delete();
1499 sis->AddSQItem(sqi,1);
1502 // temporarily replaces any previous 'Source Image Sequence'
1506 // FIXME : is 'Image Type' *really* depending on the presence of 'SOP Class UID'?
1507 if ( ContentType == FILTERED_IMAGE)
1508 // the user *knows* he just modified the pixels
1509 // the image is no longer an 'Original' one
1510 CopyMandatoryEntry(0x0008,0x0008,"DERIVED\\PRIMARY");
1514 // At the end, not to overwrite the original ones,
1515 // needed by 'Referenced SOP Instance UID', 'Referenced SOP Class UID'
1516 // 'SOP Instance UID'
1517 CopyMandatoryEntry(0x0008,0x0018,sop);
1519 if ( ContentType == USER_OWN_IMAGE)
1522 // Other possible values are :
1523 // See PS 3.3, Page 408
1525 // DV = Digitized Video
1526 // DI = Digital Interface
1527 // DF = Digitized Film
1528 // WSD = Workstation
1529 // SD = Scanned Document
1530 // SI = Scanned Image
1532 // SYN = Synthetic Image
1534 CheckMandatoryEntry(0x0008,0x0064,"SYN"); // Why not?
1537 if ( ContentType == CREATED_IMAGE)
1539 /// \todo : find a trick to pass the Media Storage SOP Instance UID of the images used to create the current image
1544 // ---- The user will never have to take any action on the following ----
1546 // new value for 'SOP Instance UID'
1547 //SetMandatoryEntry(0x0008,0x0018,Util::CreateUniqueUID());
1549 // Instance Creation Date
1550 const std::string &date = Util::GetCurrentDate();
1551 CopyMandatoryEntry(0x0008,0x0012,date);
1553 // Instance Creation Time
1554 const std::string &time = Util::GetCurrentTime();
1555 CopyMandatoryEntry(0x0008,0x0013,time);
1558 CheckMandatoryEntry(0x0008,0x0020,date);
1560 CheckMandatoryEntry(0x0008,0x0030,time);
1563 //CopyMandatoryEntry(0x0008,0x0050,"");
1564 CheckMandatoryEntry(0x0008,0x0050,"");
1567 // ----- Add Mandatory Entries if missing ---
1568 // Entries whose type is 1 are mandatory, with a mandatory value
1569 // Entries whose type is 1c are mandatory-inside-a-Sequence,
1570 // with a mandatory value
1571 // Entries whose type is 2 are mandatory, with an optional value
1572 // Entries whose type is 2c are mandatory-inside-a-Sequence,
1573 // with an optional value
1574 // Entries whose type is 3 are optional
1576 // 'Study Instance UID'
1577 // Keep the value if exists
1578 // The user is allowed to create his own Study,
1579 // keeping the same 'Study Instance UID' for various images
1580 // The user may add images to a 'Manufacturer Study',
1581 // adding new Series to an already existing Study
1582 CheckMandatoryEntry(0x0020,0x000d,Util::CreateUniqueUID());
1584 // 'Serie Instance UID'
1585 // Keep the value if exists
1586 // The user is allowed to create his own Series,
1587 // keeping the same 'Serie Instance UID' for various images
1588 // The user shouldn't add any image to a 'Manufacturer Serie'
1589 // but there is no way no to prevent him for doing that
1590 CheckMandatoryEntry(0x0020,0x000e,Util::CreateUniqueUID());
1593 CheckMandatoryEntry(0x0020,0x0010,"");
1596 CheckMandatoryEntry(0x0020,0x0011,"");
1599 CheckMandatoryEntry(0x0020,0x0013,"");
1601 // Patient Orientation
1602 // Can be computed from (0020|0037) : Image Orientation (Patient)
1603 gdcm::Orientation *o = gdcm::Orientation::New();
1604 std::string ori = o->GetOrientation ( FileInternal );
1606 if (ori != "\\" && ori != GDCM_UNFOUND)
1607 CheckMandatoryEntry(0x0020,0x0020,ori);
1609 CheckMandatoryEntry(0x0020,0x0020,"");
1611 // Default Patient Position to HFS
1612 CheckMandatoryEntry(0x0018,0x5100,"HFS");
1614 // Modality : if missing we set it to 'OTher'
1615 CheckMandatoryEntry(0x0008,0x0060,"OT");
1617 // Manufacturer : if missing we set it to 'GDCM Factory'
1618 CheckMandatoryEntry(0x0008,0x0070,"GDCM Factory");
1620 // Institution Name : if missing we set it to 'GDCM Hospital'
1621 CheckMandatoryEntry(0x0008,0x0080,"GDCM Hospital");
1623 // Patient's Name : if missing, we set it to 'GDCM^Patient'
1624 CheckMandatoryEntry(0x0010,0x0010,"GDCM^Patient");
1627 CheckMandatoryEntry(0x0010,0x0020,"");
1629 // Patient's Birth Date : 'type 2' entry -> must exist, value not mandatory
1630 CheckMandatoryEntry(0x0010,0x0030,"");
1632 // Patient's Sex :'type 2' entry -> must exist, value not mandatory
1633 CheckMandatoryEntry(0x0010,0x0040,"");
1635 // Referring Physician's Name :'type 2' entry -> must exist, value not mandatory
1636 CheckMandatoryEntry(0x0008,0x0090,"");
1638 // Deal with element 0x0000 (group length) of each group.
1639 // First stage : get all the different Groups
1643 DocEntry *d = FileInternal->GetFirstEntry();
1646 grHT[d->GetGroup()] = 0;
1647 d=FileInternal->GetNextEntry();
1649 // Second stage : add the missing ones (if any)
1650 for (GroupHT::iterator it = grHT.begin(); it != grHT.end(); ++it)
1652 CheckMandatoryEntry(it->first, 0x0000, "0");
1654 // Third stage : update all 'zero level' groups length
1658 void FileHelper::CheckMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1660 DataEntry *entry = FileInternal->GetDataEntry(group,elem);
1663 entry = DataEntry::New(Global::GetDicts()->GetDefaultPubDict()->GetEntry(group,elem));
1664 entry->SetString(value);
1665 Archive->Push(entry);
1670 void FileHelper::SetMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1672 DataEntry *entry = DataEntry::New(Global::GetDicts()->GetDefaultPubDict()->GetEntry(group,elem));
1673 entry->SetString(value);
1674 Archive->Push(entry);
1678 void FileHelper::CopyMandatoryEntry(uint16_t group,uint16_t elem,std::string value)
1680 DataEntry *entry = CopyDataEntry(group,elem);
1681 entry->SetString(value);
1682 Archive->Push(entry);
1687 * \brief Restore in the File the initial group 0002
1689 void FileHelper::RestoreWriteMandatory()
1691 // group 0002 may be pushed out for ACR-NEMA writting purposes
1692 Archive->Restore(0x0002,0x0000);
1693 Archive->Restore(0x0002,0x0001);
1694 Archive->Restore(0x0002,0x0002);
1695 Archive->Restore(0x0002,0x0003);
1696 Archive->Restore(0x0002,0x0010);
1697 Archive->Restore(0x0002,0x0012);
1698 Archive->Restore(0x0002,0x0013);
1699 Archive->Restore(0x0002,0x0016);
1700 Archive->Restore(0x0002,0x0100);
1701 Archive->Restore(0x0002,0x0102);
1703 // FIXME : Check if none is missing !
1705 Archive->Restore(0x0008,0x0012);
1706 Archive->Restore(0x0008,0x0013);
1707 Archive->Restore(0x0008,0x0016);
1708 Archive->Restore(0x0008,0x0018);
1709 Archive->Restore(0x0008,0x0060);
1710 Archive->Restore(0x0008,0x0070);
1711 Archive->Restore(0x0008,0x0080);
1712 Archive->Restore(0x0008,0x0090);
1713 Archive->Restore(0x0008,0x2112);
1715 Archive->Restore(0x0010,0x0010);
1716 Archive->Restore(0x0010,0x0030);
1717 Archive->Restore(0x0010,0x0040);
1719 Archive->Restore(0x0020,0x000d);
1720 Archive->Restore(0x0020,0x000e);
1725 * \brief CallStartMethod
1727 void FileHelper::CallStartMethod()
1731 CommandManager::ExecuteCommand(this,CMD_STARTPROGRESS);
1735 * \brief CallProgressMethod
1737 void FileHelper::CallProgressMethod()
1739 CommandManager::ExecuteCommand(this,CMD_PROGRESS);
1743 * \brief CallEndMethod
1745 void FileHelper::CallEndMethod()
1748 CommandManager::ExecuteCommand(this,CMD_ENDPROGRESS);
1751 //-----------------------------------------------------------------------------
1754 * \brief Factorization for various forms of constructors.
1756 void FileHelper::Initialize()
1759 ContentType = USER_OWN_IMAGE;
1761 WriteMode = WMODE_RAW;
1762 WriteType = ExplicitVR;
1764 PixelReadConverter = new PixelReadConvert;
1765 PixelWriteConverter = new PixelWriteConvert;
1766 Archive = new DocEntryArchive( FileInternal );
1770 * \brief Reads/[decompresses] the pixels,
1771 * *without* making RGB from Palette Colors
1772 * @return the pixels area, whatever its type
1773 * (uint8_t is just for prototyping : feel free to Cast it)
1775 uint8_t *FileHelper::GetRaw()
1777 PixelReadConverter->SetUserFunction( UserFunction );
1779 uint8_t *raw = PixelReadConverter->GetRaw();
1782 // The Raw image migth not be loaded yet:
1783 std::ifstream *fp = FileInternal->OpenFile();
1784 PixelReadConverter->ReadAndDecompressPixelData( fp );
1786 FileInternal->CloseFile();
1788 raw = PixelReadConverter->GetRaw();
1791 gdcmWarningMacro( "Read/decompress of pixel data apparently went wrong.");
1798 //-----------------------------------------------------------------------------
1800 * \brief Prints the common part of DataEntry, SeqEntry
1801 * @param os ostream we want to print in
1802 * @param indent (unused)
1804 void FileHelper::Print(std::ostream &os, std::string const &)
1806 FileInternal->SetPrintLevel(PrintLevel);
1807 FileInternal->Print(os);
1809 if ( FileInternal->IsReadable() )
1811 PixelReadConverter->SetPrintLevel(PrintLevel);
1812 PixelReadConverter->Print(os);
1816 //-----------------------------------------------------------------------------
1817 } // end namespace gdcm