1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2004/10/28 03:10:58 $
7 Version: $Revision: 1.150 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 #include "gdcmDebug.h"
25 typedef std::pair<TagDocEntryHT::iterator,TagDocEntryHT::iterator> IterHT;
27 //-------------------------------------------------------------------------
28 // Constructor / Destructor
30 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
31 * file (Header only deals with the ... header)
32 * Opens (in read only and when possible) an existing file and checks
33 * for DICOM compliance. Returns NULL on failure.
34 * It will be up to the user to load the pixels into memory
35 * (see GetImageData, GetImageDataRaw)
36 * \note the in-memory representation of all available tags found in
37 * the DICOM header is post-poned to first header information access.
38 * This avoid a double parsing of public part of the header when
39 * user sets an a posteriori shadow dictionary (efficiency can be
40 * seen as a side effect).
41 * @param header already built Header
43 File::File(Header *header)
45 HeaderInternal = header;
51 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
52 * file (Header only deals with the ... header)
53 * Opens (in read only and when possible) an existing file and checks
54 * for DICOM compliance. Returns NULL on failure.
55 * It will be up to the user to load the pixels into memory
56 * (see GetImageData, GetImageDataRaw)
57 * \note the in-memory representation of all available tags found in
58 * the DICOM header is post-poned to first header information access.
59 * This avoid a double parsing of public part of the header when
60 * one sets an a posteriori shadow dictionary (efficiency can be
61 * seen as a side effect).
62 * @param filename file to be opened for parsing
64 File::File(std::string const & filename )
66 HeaderInternal = new Header( filename );
72 * \brief Factorization for various forms of constructors.
74 void File::Initialise()
76 if ( HeaderInternal->IsReadable() )
78 ImageDataSizeRaw = ComputeDecompressedPixelDataSizeFromHeader();
79 if ( HeaderInternal->HasLUT() )
81 ImageDataSize = 3 * ImageDataSizeRaw;
85 ImageDataSize = ImageDataSizeRaw;
88 PixelConverter = new PixelConvert;
89 PixelConverter->GrabInformationsFromHeader( HeaderInternal );
95 * \brief canonical destructor
96 * \note If the Header was created by the File constructor,
97 * it is destroyed by the File
103 delete HeaderInternal;
107 DeleteInitialValues();
111 * \brief Sets some initial values for the Constructor
112 * \warning not end user intended
114 void File::SaveInitialValues()
117 PixelRead = -1; // no ImageData read yet.
118 LastAllocatedPixelDataLength = 0;
123 InitialPlanConfig = "";
124 InitialBitsAllocated = "";
127 InitialRedLUTDescr = 0;
128 InitialGreenLUTDescr = 0;
129 InitialBlueLUTDescr = 0;
130 InitialRedLUTData = 0;
131 InitialGreenLUTData = 0;
132 InitialBlueLUTData = 0;
134 if ( HeaderInternal->IsReadable() )
136 // the following values *may* be modified
137 // by File::GetImageDataIntoVectorRaw
138 // we save their initial value.
139 InitialSpp = HeaderInternal->GetEntryByNumber(0x0028,0x0002);
140 InitialPhotInt = HeaderInternal->GetEntryByNumber(0x0028,0x0004);
141 InitialPlanConfig = HeaderInternal->GetEntryByNumber(0x0028,0x0006);
143 InitialBitsAllocated = HeaderInternal->GetEntryByNumber(0x0028,0x0100);
144 InitialHighBit = HeaderInternal->GetEntryByNumber(0x0028,0x0102);
146 // the following entries *may* be removed from the H table
147 // (NOT deleted ...) by File::GetImageDataIntoVectorRaw
148 // we keep a pointer on them.
149 InitialRedLUTDescr = HeaderInternal->GetDocEntryByNumber(0x0028,0x1101);
150 InitialGreenLUTDescr = HeaderInternal->GetDocEntryByNumber(0x0028,0x1102);
151 InitialBlueLUTDescr = HeaderInternal->GetDocEntryByNumber(0x0028,0x1103);
153 InitialRedLUTData = HeaderInternal->GetDocEntryByNumber(0x0028,0x1201);
154 InitialGreenLUTData = HeaderInternal->GetDocEntryByNumber(0x0028,0x1202);
155 InitialBlueLUTData = HeaderInternal->GetDocEntryByNumber(0x0028,0x1203);
160 * \brief restores some initial values
161 * \warning not end user intended
163 void File::RestoreInitialValues()
165 if ( HeaderInternal->IsReadable() )
167 // the following values *may* have been modified
168 // by File::GetImageDataIntoVectorRaw
169 // we restore their initial value.
170 if ( InitialSpp != "")
171 HeaderInternal->SetEntryByNumber(InitialSpp,0x0028,0x0002);
172 if ( InitialPhotInt != "")
173 HeaderInternal->SetEntryByNumber(InitialPhotInt,0x0028,0x0004);
174 if ( InitialPlanConfig != "")
176 HeaderInternal->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006);
177 if ( InitialBitsAllocated != "")
178 HeaderInternal->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100);
179 if ( InitialHighBit != "")
180 HeaderInternal->SetEntryByNumber(InitialHighBit,0x0028,0x0102);
182 // the following entries *may* be have been removed from the H table
183 // (NOT deleted ...) by File::GetImageDataIntoVectorRaw
186 if (InitialRedLUTDescr)
187 HeaderInternal->AddEntry(InitialRedLUTDescr);
188 if (InitialGreenLUTDescr)
189 HeaderInternal->AddEntry(InitialGreenLUTDescr);
190 if (InitialBlueLUTDescr)
191 HeaderInternal->AddEntry(InitialBlueLUTDescr);
193 if (InitialRedLUTData)
194 HeaderInternal->AddEntry(InitialBlueLUTDescr);
195 if (InitialGreenLUTData)
196 HeaderInternal->AddEntry(InitialGreenLUTData);
197 if (InitialBlueLUTData)
198 HeaderInternal->AddEntry(InitialBlueLUTData);
203 * \brief delete initial values (il they were saved)
204 * of InitialLutDescriptors and InitialLutData
206 void File::DeleteInitialValues()
209 // InitialLutDescriptors and InitialLutData
210 // will have to be deleted if the don't belong any longer
211 // to the Header H table when the header is deleted...
213 if ( InitialRedLUTDescr )
214 delete InitialRedLUTDescr;
216 if ( InitialGreenLUTDescr )
217 delete InitialGreenLUTDescr;
219 if ( InitialBlueLUTDescr )
220 delete InitialBlueLUTDescr;
222 if ( InitialRedLUTData )
223 delete InitialRedLUTData;
225 if ( InitialGreenLUTData != NULL)
226 delete InitialGreenLUTData;
228 if ( InitialBlueLUTData != NULL)
229 delete InitialBlueLUTData;
232 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
239 * \brief computes the length (in bytes) we must ALLOCATE to receive the
240 * image(s) pixels (multiframes taken into account)
241 * \warning : it is NOT the group 7FE0 length
242 * (no interest for compressed images).
244 int File::ComputeDecompressedPixelDataSizeFromHeader()
246 // see PS 3.3-2003 : C.7.6.3.2.1
256 // YBR_FULL_422 (no LUT, no Palette)
262 // ex : gdcm-US-ALOKA-16.dcm
263 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
264 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
265 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
267 // ex : OT-PAL-8-face.dcm
268 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
269 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
270 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
272 int numberBitsAllocated = HeaderInternal->GetBitsAllocated();
273 // Number of "Bits Allocated" is fixed to 16 when:
274 // - it is not defined (i.e. it's value is 0)
275 // - it's 12, since we will expand the image to 16 bits (see
276 // PixelConvert::ConvertDecompress12BitsTo16Bits() )
277 if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
279 numberBitsAllocated = 16;
282 int DecompressedSize = HeaderInternal->GetXSize()
283 * HeaderInternal->GetYSize()
284 * HeaderInternal->GetZSize()
285 * ( numberBitsAllocated / 8 )
286 * HeaderInternal->GetSamplesPerPixel();
288 return DecompressedSize;
292 * \brief - Allocates necessary memory,
293 * - Reads the pixels from disk (uncompress if necessary),
294 * - Transforms YBR pixels, if any, into RGB pixels
295 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
296 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
297 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
298 * @return Pointer to newly allocated pixel data.
299 * NULL if alloc fails
301 uint8_t* File::GetImageData()
303 if ( ! GetDecompressed() )
305 // If the decompression failed nothing can be done.
310 if ( HeaderInternal->HasLUT() && PixelConverter->BuildRGBImage() )
312 pixelData = PixelConverter->GetRGB();
316 // When no LUT or LUT conversion fails, return the decompressed
317 pixelData = PixelConverter->GetDecompressed();
320 // PIXELCONVERT CLEANME
321 // Restore the header in a disk-consistent state
322 // (if user asks twice to get the pixels from disk)
323 if ( PixelRead != -1 ) // File was "read" before
325 RestoreInitialValues();
327 if ( PixelConverter->GetRGB() )
329 // now, it's an RGB image
330 // Lets's write it in the Header
331 std::string spp = "3"; // Samples Per Pixel
332 HeaderInternal->SetEntryByNumber(spp,0x0028,0x0002);
333 std::string rgb = "RGB "; // Photometric Interpretation
334 HeaderInternal->SetEntryByNumber(rgb,0x0028,0x0004);
335 std::string planConfig = "0"; // Planar Configuration
336 HeaderInternal->SetEntryByNumber(planConfig,0x0028,0x0006);
337 PixelRead = 0; // no PixelRaw
341 if ( HeaderInternal->HasLUT() )
343 // The LUT interpretation failed
344 std::string photometricInterpretation = "MONOCHROME1 ";
345 HeaderInternal->SetEntryByNumber( photometricInterpretation,
347 PixelRead = 0; // no PixelRaw
351 if ( PixelConverter->IsDecompressedRGB() )
353 ///////////////////////////////////////////////////
354 // now, it's an RGB image
355 // Lets's write it in the Header
356 // Droping Palette Color out of the Header
357 // has been moved to the Write process.
358 // TODO : move 'values' modification to the write process
359 // : save also (in order to be able to restore)
360 // : 'high bit' -when not equal to 'bits stored' + 1
361 // : 'bits allocated', when it's equal to 12 ?!
362 std::string spp = "3"; // Samples Per Pixel
363 std::string photInt = "RGB "; // Photometric Interpretation
364 std::string planConfig = "0"; // Planar Configuration
365 HeaderInternal->SetEntryByNumber(spp,0x0028,0x0002);
366 HeaderInternal->SetEntryByNumber(photInt,0x0028,0x0004);
367 HeaderInternal->SetEntryByNumber(planConfig,0x0028,0x0006);
369 PixelRead = 1; // PixelRaw
373 // We say the value *is* loaded.
374 GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
375 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
377 // Will be 7fe0, 0010 in standard case
378 GetHeader()->SetEntryBinAreaByNumber( pixelData,
379 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
380 // END PIXELCONVERT CLEANME
387 * Read the pixels from disk (uncompress if necessary),
388 * Transforms YBR pixels, if any, into RGB pixels
389 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
390 * Transforms single Grey plane + 3 Palettes into a RGB Plane
391 * Copies at most MaxSize bytes of pixel data to caller allocated
393 * \warning This function allows people that want to build a volume
394 * from an image stack *not to* have, first to get the image pixels,
395 * and then move them to the volume area.
396 * It's absolutely useless for any VTK user since vtk chooses
397 * to invert the lines of an image, that is the last line comes first
398 * (for some axis related reasons?). Hence he will have
399 * to load the image line by line, starting from the end.
400 * VTK users have to call GetImageData
402 * @param destination Address (in caller's memory space) at which the
403 * pixel data should be copied
404 * @param maxSize Maximum number of bytes to be copied. When MaxSize
405 * is not sufficient to hold the pixel data the copy is not
406 * executed (i.e. no partial copy).
407 * @return On success, the number of bytes actually copied. Zero on
408 * failure e.g. MaxSize is lower than necessary.
410 size_t File::GetImageDataIntoVector (void* destination, size_t maxSize)
412 if ( ! GetDecompressed() )
414 // If the decompression failed nothing can be done.
418 if ( HeaderInternal->HasLUT() && PixelConverter->BuildRGBImage() )
420 if ( PixelConverter->GetRGBSize() > maxSize )
422 dbg.Verbose(0, "File::GetImageDataIntoVector: pixel data bigger"
423 "than caller's expected MaxSize");
426 memmove( destination,
427 (void*)PixelConverter->GetRGB(),
428 PixelConverter->GetRGBSize() );
429 return PixelConverter->GetRGBSize();
432 // Either no LUT conversion necessary or LUT conversion failed
433 if ( PixelConverter->GetDecompressedSize() > maxSize )
435 dbg.Verbose(0, "File::GetImageDataIntoVector: pixel data bigger"
436 "than caller's expected MaxSize");
439 memmove( destination,
440 (void*)PixelConverter->GetDecompressed(),
441 PixelConverter->GetDecompressedSize() );
442 return PixelConverter->GetDecompressedSize();
446 * \brief Allocates necessary memory,
447 * Transforms YBR pixels (if any) into RGB pixels
448 * Transforms 3 planes R, G, B (if any) into a single RGB Plane
449 * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
450 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
451 * @return Pointer to newly allocated pixel data.
452 * \ NULL if alloc fails
454 uint8_t* File::GetImageDataRaw ()
456 uint8_t* decompressed = GetDecompressed();
457 if ( ! decompressed )
462 // PIXELCONVERT CLEANME
463 // Restore the header in a disk-consistent state
464 // (if user asks twice to get the pixels from disk)
465 if ( PixelRead != -1 ) // File was "read" before
467 RestoreInitialValues();
469 if ( PixelConverter->IsDecompressedRGB() )
471 ///////////////////////////////////////////////////
472 // now, it's an RGB image
473 // Lets's write it in the Header
474 // Droping Palette Color out of the Header
475 // has been moved to the Write process.
476 // TODO : move 'values' modification to the write process
477 // : save also (in order to be able to restore)
478 // : 'high bit' -when not equal to 'bits stored' + 1
479 // : 'bits allocated', when it's equal to 12 ?!
480 std::string spp = "3"; // Samples Per Pixel
481 std::string photInt = "RGB "; // Photometric Interpretation
482 std::string planConfig = "0"; // Planar Configuration
483 HeaderInternal->SetEntryByNumber(spp,0x0028,0x0002);
484 HeaderInternal->SetEntryByNumber(photInt,0x0028,0x0004);
485 HeaderInternal->SetEntryByNumber(planConfig,0x0028,0x0006);
488 // We say the value *is* loaded.
489 GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
490 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
492 // will be 7fe0, 0010 in standard cases
493 GetHeader()->SetEntryBinAreaByNumber( decompressed,
494 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
496 PixelRead = 1; // PixelRaw
497 // END PIXELCONVERT CLEANME
502 uint8_t* File::GetDecompressed()
504 uint8_t* decompressed = PixelConverter->GetDecompressed();
505 if ( ! decompressed )
507 // The decompressed image migth not be loaded yet:
508 std::ifstream* fp = HeaderInternal->OpenFile();
509 PixelConverter->ReadAndDecompressPixelData( fp );
510 HeaderInternal->CloseFile();
511 decompressed = PixelConverter->GetDecompressed();
512 if ( ! decompressed )
514 dbg.Verbose(0, "File::GetDecompressed: read/decompress of "
515 "pixel data apparently went wrong.");
524 * \brief Points the internal Pixel_Data pointer to the callers inData
525 * image representation, BUT WITHOUT COPYING THE DATA.
526 * 'image' Pixels are presented as C-like 2D arrays : line per line.
527 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
528 * \warning Since the pixels are not copied, it is the caller's responsability
529 * not to deallocate it's data before gdcm uses them (e.g. with
530 * the Write() method.
531 * @param inData user supplied pixel area
532 * @param expectedSize total image size, in Bytes
536 bool File::SetImageData(uint8_t* inData, size_t expectedSize)
538 HeaderInternal->SetImageDataSize( expectedSize );
539 // FIXME : if already allocated, memory leak !
541 ImageDataSize = ImageDataSizeRaw = expectedSize;
543 // FIXME : 7fe0, 0010 IS NOT set ...
548 * \brief Writes on disk A SINGLE Dicom file
549 * NO test is performed on processor "Endiannity".
550 * It's up to the user to call his Reader properly
551 * @param fileName name of the file to be created
552 * (any already existing file is over written)
553 * @return false if write fails
556 bool File::WriteRawData(std::string const & fileName)
558 std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
561 printf("Fail to open (write) file [%s] \n", fileName.c_str());
564 fp1.write((char*)Pixel_Data, ImageDataSize);
571 * \brief Writes on disk A SINGLE Dicom file,
572 * using the Implicit Value Representation convention
573 * NO test is performed on processor "Endiannity".
574 * @param fileName name of the file to be created
575 * (any already existing file is overwritten)
576 * @return false if write fails
579 bool File::WriteDcmImplVR (std::string const & fileName)
581 return WriteBase(fileName, ImplicitVR);
585 * \brief Writes on disk A SINGLE Dicom file,
586 * using the Explicit Value Representation convention
587 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
588 * (any already existing file is overwritten)
589 * @return false if write fails
592 bool File::WriteDcmExplVR (std::string const & fileName)
594 return WriteBase(fileName, ExplicitVR);
598 * \brief Writes on disk A SINGLE Dicom file,
599 * using the ACR-NEMA convention
600 * NO test is performed on processor "Endiannity".
601 * (a l'attention des logiciels cliniques
602 * qui ne prennent en entrée QUE des images ACR ...
603 * \warning if a DICOM_V3 header is supplied,
604 * groups < 0x0008 and shadow groups are ignored
605 * \warning NO TEST is performed on processor "Endiannity".
606 * @param fileName name of the file to be created
607 * (any already existing file is overwritten)
608 * @return false if write fails
611 bool File::WriteAcr (std::string const & fileName)
613 return WriteBase(fileName, ACR);
616 //-----------------------------------------------------------------------------
619 * \brief NOT a end user inteded function
620 * (used by WriteDcmExplVR, WriteDcmImplVR, WriteAcr, etc)
621 * @param fileName name of the file to be created
622 * (any already existing file is overwritten)
623 * @param type file type (ExplicitVR, ImplicitVR, ...)
624 * @return false if write fails
626 bool File::WriteBase (std::string const & fileName, FileType type)
628 if ( PixelRead == -1 && type != ExplicitVR)
633 std::ofstream* fp1 = new std::ofstream(fileName.c_str(),
634 std::ios::out | std::ios::binary);
637 printf("Failed to open (write) File [%s] \n", fileName.c_str());
641 if ( type == ImplicitVR || type == ExplicitVR )
643 // writing Dicom File Preamble
644 uint8_t filePreamble[128];
645 memset(filePreamble, 0, 128);
646 fp1->write((char*)filePreamble, 128);
647 fp1->write("DICM", 4);
650 // --------------------------------------------------------------
651 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
653 // if recognition code tells us we dealt with a LibIDO image
654 // we reproduce on disk the switch between lineNumber and columnNumber
655 // just before writting ...
657 /// \todo the best trick would be *change* the recognition code
658 /// but pb expected if user deals with, e.g. COMPLEX images
660 std::string rows, columns;
661 if ( HeaderInternal->GetFileType() == ACR_LIBIDO)
663 rows = HeaderInternal->GetEntryByNumber(0x0028, 0x0010);
664 columns = HeaderInternal->GetEntryByNumber(0x0028, 0x0011);
666 HeaderInternal->SetEntryByNumber(columns, 0x0028, 0x0010);
667 HeaderInternal->SetEntryByNumber(rows , 0x0028, 0x0011);
669 // ----------------- End of Special Patch ----------------
671 uint16_t grPixel = HeaderInternal->GetGrPixel();
672 uint16_t numPixel = HeaderInternal->GetNumPixel();;
674 DocEntry* PixelElement =
675 GetHeader()->GetDocEntryByNumber(grPixel, numPixel);
677 if ( PixelRead == 1 )
679 // we read pixel 'as is' (no tranformation LUT -> RGB)
680 PixelElement->SetLength( ImageDataSizeRaw );
682 else if ( PixelRead == 0 )
684 // we tranformed GrayLevel pixels + LUT into RGB Pixel
685 PixelElement->SetLength( ImageDataSize );
688 HeaderInternal->Write(fp1, type);
690 // --------------------------------------------------------------
691 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
693 // ...and we restore the Header to be Dicom Compliant again
694 // just after writting
696 if ( HeaderInternal->GetFileType() == ACR_LIBIDO )
698 HeaderInternal->SetEntryByNumber(rows , 0x0028, 0x0010);
699 HeaderInternal->SetEntryByNumber(columns, 0x0028, 0x0011);
701 // ----------------- End of Special Patch ----------------
708 * \brief Access to the underlying \ref PixelConverter RGBA LUT
710 uint8_t* File::GetLutRGBA()
712 return PixelConverter->GetLutRGBA();
715 } // end namespace gdcm