1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelReadConvert.cxx,v $
6 Date: $Date: 2005/01/26 16:28:58 $
7 Version: $Revision: 1.36 $
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 =========================================================================*/
19 #include "gdcmDebug.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmPixelReadConvert.h"
24 #include "gdcmDocEntry.h"
25 #include "gdcmRLEFramesInfo.h"
26 #include "gdcmJPEGFragmentsInfo.h"
29 #include <stdio.h> //for sscanf
33 #define str2num(str, typeNum) *((typeNum *)(str))
36 //-----------------------------------------------------------------------------
37 // Constructor / Destructor
38 PixelReadConvert::PixelReadConvert()
50 void PixelReadConvert::Squeeze()
65 PixelReadConvert::~PixelReadConvert()
70 void PixelReadConvert::AllocateRGB()
74 RGB = new uint8_t[RGBSize];
77 void PixelReadConvert::AllocateRaw()
81 Raw = new uint8_t[RawSize];
85 * \brief Read from file a 12 bits per pixel image and decompress it
86 * into a 16 bits per pixel image.
88 void PixelReadConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream *fp )
91 int nbPixels = XSize * YSize;
92 uint16_t* localDecompres = (uint16_t*)Raw;
94 for( int p = 0; p < nbPixels; p += 2 )
98 fp->read( (char*)&b0, 1);
99 if ( fp->fail() || fp->eof() )//Fp->gcount() == 1
101 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
102 "Unfound first block" );
105 fp->read( (char*)&b1, 1 );
106 if ( fp->fail() || fp->eof())//Fp->gcount() == 1
108 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
109 "Unfound second block" );
112 fp->read( (char*)&b2, 1 );
113 if ( fp->fail() || fp->eof())//Fp->gcount() == 1
115 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
116 "Unfound second block" );
119 // Two steps are necessary to please VC++
121 // 2 pixels 12bit = [0xABCDEF]
122 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
124 *localDecompres++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
126 *localDecompres++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
128 /// \todo JPR Troubles expected on Big-Endian processors ?
133 * \brief Try to deal with RLE 16 Bits.
134 * We assume the RLE has already been parsed and loaded in
135 * Raw (through \ref ReadAndDecompressJPEGFile ).
136 * We here need to make 16 Bits Pixels from Low Byte and
137 * High Byte 'Planes'...(for what it may mean)
140 bool PixelReadConvert::DecompressRLE16BitsFromRLE8Bits( int NumberOfFrames )
142 size_t pixelNumber = XSize * YSize;
143 size_t rawSize = XSize * YSize * NumberOfFrames;
145 // We assumed Raw contains the decoded RLE pixels but as
146 // 8 bits per pixel. In order to convert those pixels to 16 bits
147 // per pixel we cannot work in place within Raw and hence
148 // we copy it in a safe place, say copyRaw.
150 uint8_t* copyRaw = new uint8_t[rawSize * 2];
151 memmove( copyRaw, Raw, rawSize * 2 );
154 uint8_t* a = copyRaw;
155 uint8_t* b = a + pixelNumber;
157 for ( int i = 0; i < NumberOfFrames; i++ )
159 for ( unsigned int j = 0; j < pixelNumber; j++ )
168 /// \todo check that operator new []didn't fail, and sometimes return false
173 * \brief Implementation of the RLE decoding algorithm for decompressing
174 * a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
175 * @param subRaw Sub region of \ref Raw where the decoded fragment
177 * @param fragmentSize The length of the binary fragment as found on the disk.
178 * @param RawSegmentSize The expected length of the fragment ONCE
180 * @param fp File Pointer: on entry the position should be the one of
181 * the fragment to be decoded.
183 bool PixelReadConvert::ReadAndDecompressRLEFragment( uint8_t *subRaw,
189 long numberOfOutputBytes = 0;
190 long numberOfReadBytes = 0;
192 while( numberOfOutputBytes < RawSegmentSize )
194 fp->read( (char*)&count, 1 );
195 numberOfReadBytes += 1;
197 // Note: count <= 127 comparison is always true due to limited range
198 // of data type int8_t [since the maximum of an exact width
199 // signed integer of width N is 2^(N-1) - 1, which for int8_t
202 fp->read( (char*)subRaw, count + 1);
203 numberOfReadBytes += count + 1;
205 numberOfOutputBytes += count + 1;
209 if ( ( count <= -1 ) && ( count >= -127 ) )
212 fp->read( (char*)&newByte, 1);
213 numberOfReadBytes += 1;
214 for( int i = 0; i < -count + 1; i++ )
218 subRaw += -count + 1;
219 numberOfOutputBytes += -count + 1;
222 // if count = 128 output nothing
224 if ( numberOfReadBytes > fragmentSize )
226 gdcmVerboseMacro( "Read more bytes than the segment size.");
234 * \brief Reads from disk the Pixel Data of 'Run Length Encoded'
235 * Dicom encapsulated file and decompress it.
236 * @param fp already open File Pointer
237 * at which the pixel data should be copied
240 bool PixelReadConvert::ReadAndDecompressRLEFile( std::ifstream *fp )
242 uint8_t *subRaw = Raw;
243 long RawSegmentSize = XSize * YSize;
245 // Loop on the frame[s]
246 for( RLEFramesInfo::RLEFrameList::iterator
247 it = RLEInfo->Frames.begin();
248 it != RLEInfo->Frames.end();
251 // Loop on the fragments
252 for( unsigned int k = 1; k <= (*it)->GetNumberOfFragments(); k++ )
254 fp->seekg( (*it)->GetOffset(k) , std::ios::beg );
255 (void)ReadAndDecompressRLEFragment( subRaw,
259 subRaw += RawSegmentSize;
263 if ( BitsAllocated == 16 )
265 // Try to deal with RLE 16 Bits
266 (void)DecompressRLE16BitsFromRLE8Bits( ZSize );
273 * \brief Swap the bytes, according to \ref SwapCode.
275 void PixelReadConvert::ConvertSwapZone()
279 if( BitsAllocated == 16 )
281 uint16_t *im16 = (uint16_t*)Raw;
289 for( i = 0; i < RawSize / 2; i++ )
291 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
295 gdcmVerboseMacro("SwapCode value (16 bits) not allowed.");
298 else if( BitsAllocated == 32 )
303 uint32_t* im32 = (uint32_t*)Raw;
309 for( i = 0; i < RawSize / 4; i++ )
311 low = im32[i] & 0x0000ffff; // 4321
312 high = im32[i] >> 16;
313 high = ( high >> 8 ) | ( high << 8 );
314 low = ( low >> 8 ) | ( low << 8 );
316 im32[i] = ( s32 << 16 ) | high;
320 for( i = 0; i < RawSize / 4; i++ )
322 low = im32[i] & 0x0000ffff; // 2143
323 high = im32[i] >> 16;
324 high = ( high >> 8 ) | ( high << 8 );
325 low = ( low >> 8 ) | ( low << 8 );
327 im32[i] = ( s32 << 16 ) | low;
331 for( i = 0; i < RawSize / 4; i++ )
333 low = im32[i] & 0x0000ffff; // 3412
334 high = im32[i] >> 16;
336 im32[i] = ( s32 << 16 ) | high;
340 gdcmVerboseMacro("SwapCode value (32 bits) not allowed." );
346 * \brief Deal with endianness i.e. re-arange bytes inside the integer
348 void PixelReadConvert::ConvertReorderEndianity()
350 if ( BitsAllocated != 8 )
355 // Special kludge in order to deal with xmedcon broken images:
356 if ( BitsAllocated == 16
357 && BitsStored < BitsAllocated
360 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
361 uint16_t *deb = (uint16_t *)Raw;
362 for(int i = 0; i<l; i++)
375 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
376 * file and decompress it. This function assumes that each
377 * jpeg fragment contains a whole frame (jpeg file).
378 * @param fp File Pointer
381 bool PixelReadConvert::ReadAndDecompressJPEGFramesFromFile( std::ifstream *fp )
383 // Pointer to the Raw image
384 //uint8_t *localRaw = Raw;
386 // Precompute the offset localRaw will be shifted with
387 int length = XSize * YSize * SamplesPerPixel;
388 int numberBytes = BitsAllocated / 8;
390 // // Loop on the fragment[s]
391 // for( JPEGFragmentsInfo::JPEGFragmentsList::iterator
392 // it = JPEGInfo->Fragments.begin();
393 // it != JPEGInfo->Fragments.end();
396 // (*it)->DecompressJPEGFramesFromFile(fp, localRaw, BitsStored );
398 // // Advance to next free location in Raw
399 // // for next fragment decompression (if any)
401 // localRaw += length * numberBytes;
403 JPEGInfo->DecompressJPEGFramesFromFile(fp, Raw, BitsStored, numberBytes, length );
408 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
409 * file and decompress it. This function assumes that the dicom
410 * image is a single frame split into several JPEG fragments.
411 * Those fragments will be glued together into a memory buffer
413 * @param fp File Pointer
416 bool PixelReadConvert::
417 ReadAndDecompressJPEGSingleFrameFragmentsFromFile( std::ifstream *fp )
419 // Loop on the fragment[s] to get total length
420 size_t totalLength = JPEGInfo->GetFragmentsLength();
422 // Concatenate the jpeg fragments into a local buffer
423 JOCTET *buffer = new JOCTET [totalLength];
424 // Fill in the buffer:
425 JPEGInfo->ReadAllFragments(fp, buffer);
428 JPEGFragmentsInfo::JPEGFragmentsList::const_iterator it = JPEGInfo->Fragments.begin();
429 (*it)->DecompressJPEGSingleFrameFragmentsFromFile(buffer, totalLength, Raw, BitsStored);
438 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
439 * file and decompress it. This function handles the generic
440 * and complex case where the DICOM contains several frames,
441 * and some of the frames are possibly split into several JPEG
443 * @param fp File Pointer
446 bool PixelReadConvert::
447 ReadAndDecompressJPEGFragmentedFramesFromFile( std::ifstream *fp )
449 // Loop on the fragment[s] to get total length
450 size_t totalLength = JPEGInfo->GetFragmentsLength();
452 // Concatenate the jpeg fragments into a local buffer
453 JOCTET *buffer = new JOCTET [totalLength];
454 // Fill in the buffer:
455 JPEGInfo->ReadAllFragments(fp, buffer);
457 size_t howManyRead = 0;
458 size_t howManyWritten = 0;
459 size_t fragmentLength = 0;
461 JPEGFragmentsInfo::JPEGFragmentsList::const_iterator it;
462 for( it = JPEGInfo->Fragments.begin() ;
463 (it != JPEGInfo->Fragments.end()) && (howManyRead < totalLength);
466 fragmentLength += (*it)->GetLength();
468 if (howManyRead > fragmentLength) continue;
470 (*it)->DecompressJPEGFragmentedFramesFromFile(buffer, Raw, BitsStored, howManyRead, howManyWritten, totalLength);
472 if (howManyRead < fragmentLength)
473 howManyRead = fragmentLength;
483 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
484 * file and decompress it.
485 * @param fp File Pointer
488 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
492 gdcmVerboseMacro( "Sorry, JPEG2000 not yet taken into account" );
493 fp->seekg( (*JPEGInfo->Fragments.begin())->GetOffset(), std::ios::beg);
494 // if ( ! gdcm_read_JPEG2000_file( fp,Raw ) )
495 gdcmVerboseMacro( "Wrong Blue LUT descriptor" );
501 gdcmVerboseMacro( "Sorry, JPEG-LS not yet taken into account" );
502 fp->seekg( (*JPEGInfo->Fragments.begin())->GetOffset(), std::ios::beg);
503 // if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
507 if ( ( ZSize == 1 ) && ( JPEGInfo->Fragments.size() > 1 ) )
509 // we have one frame split into several fragments
510 // we will pack those fragments into a single buffer and
512 return ReadAndDecompressJPEGSingleFrameFragmentsFromFile( fp );
514 else if (JPEGInfo->Fragments.size() == (size_t)ZSize)
517 // if ( ( ZSize == 1 ) && ( JPEGInfo->Fragments.size() > 1 ) )
519 // // we have one frame split into several fragments
520 // // we will pack those fragments into a single buffer and
522 // return ReadAndDecompressJPEGSingleFrameFragmentsFromFile( fp );
524 // else if (JPEGInfo->Fragments.size() == (size_t)ZSize)
526 // suppose each fragment is a frame
527 return ReadAndDecompressJPEGFramesFromFile( fp );
531 // // The dicom image contains frames containing fragments of images
532 // // a more complex algorithm :-)
533 // return ReadAndDecompressJPEGFragmentedFramesFromFile( fp );
538 * \brief Re-arrange the bits within the bytes.
541 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
543 if ( BitsStored != BitsAllocated )
545 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
546 if ( BitsAllocated == 16 )
548 uint16_t mask = 0xffff;
549 mask = mask >> ( BitsAllocated - BitsStored );
550 uint16_t* deb = (uint16_t*)Raw;
551 for(int i = 0; i<l; i++)
553 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & mask;
557 else if ( BitsAllocated == 32 )
559 uint32_t mask = 0xffffffff;
560 mask = mask >> ( BitsAllocated - BitsStored );
561 uint32_t* deb = (uint32_t*)Raw;
562 for(int i = 0; i<l; i++)
564 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & mask;
570 gdcmVerboseMacro("Weird image");
571 throw FormatError( "Weird image !?" );
578 * \brief Convert (cY plane, cB plane, cR plane) to RGB pixels
579 * \warning Works on all the frames at a time
581 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
583 uint8_t *localRaw = Raw;
584 uint8_t *copyRaw = new uint8_t[ RawSize ];
585 memmove( copyRaw, localRaw, RawSize );
587 // to see the tricks about YBR_FULL, YBR_FULL_422,
588 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
589 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
590 // and be *very* affraid
592 int l = XSize * YSize;
593 int nbFrames = ZSize;
595 uint8_t *a = copyRaw;
596 uint8_t *b = copyRaw + l;
597 uint8_t *c = copyRaw + l + l;
600 /// \todo : Replace by the 'well known' integer computation
601 /// counterpart. Refer to
602 /// http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
603 /// for code optimisation.
605 for ( int i = 0; i < nbFrames; i++ )
607 for ( int j = 0; j < l; j++ )
609 R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
610 G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
611 B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
613 if (R < 0.0) R = 0.0;
614 if (G < 0.0) G = 0.0;
615 if (B < 0.0) B = 0.0;
616 if (R > 255.0) R = 255.0;
617 if (G > 255.0) G = 255.0;
618 if (B > 255.0) B = 255.0;
620 *(localRaw++) = (uint8_t)R;
621 *(localRaw++) = (uint8_t)G;
622 *(localRaw++) = (uint8_t)B;
632 * \brief Convert (Red plane, Green plane, Blue plane) to RGB pixels
633 * \warning Works on all the frames at a time
635 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
637 uint8_t *localRaw = Raw;
638 uint8_t *copyRaw = new uint8_t[ RawSize ];
639 memmove( copyRaw, localRaw, RawSize );
641 int l = XSize * YSize * ZSize;
643 uint8_t* a = copyRaw;
644 uint8_t* b = copyRaw + l;
645 uint8_t* c = copyRaw + l + l;
647 for (int j = 0; j < l; j++)
649 *(localRaw++) = *(a++);
650 *(localRaw++) = *(b++);
651 *(localRaw++) = *(c++);
656 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
658 // ComputeRawAndRGBSizes is already made by
659 // ::GrabInformationsFromfile. So, the structure sizes are
663 //////////////////////////////////////////////////
664 //// First stage: get our hands on the Pixel Data.
667 gdcmVerboseMacro( "Unavailable file pointer." );
671 fp->seekg( PixelOffset, std::ios::beg );
672 if( fp->fail() || fp->eof())
674 gdcmVerboseMacro( "Unable to find PixelOffset in file." );
680 //////////////////////////////////////////////////
681 //// Second stage: read from disk dans decompress.
682 if ( BitsAllocated == 12 )
684 ReadAndDecompress12BitsTo16Bits( fp);
688 // This problem can be found when some obvious informations are found
689 // after the field containing the image data. In this case, these
690 // bad data are added to the size of the image (in the PixelDataLength
691 // variable). But RawSize is the right size of the image !
692 if( PixelDataLength != RawSize)
694 gdcmVerboseMacro( "Mismatch between PixelReadConvert and RawSize." );
696 if( PixelDataLength > RawSize)
698 fp->read( (char*)Raw, RawSize);
702 fp->read( (char*)Raw, PixelDataLength);
705 if ( fp->fail() || fp->eof())
707 gdcmVerboseMacro( "Reading of Raw pixel data failed." );
711 else if ( IsRLELossless )
713 if ( ! ReadAndDecompressRLEFile( fp ) )
715 gdcmVerboseMacro( "RLE decompressor failed." );
721 // Default case concerns JPEG family
722 if ( ! ReadAndDecompressJPEGFile( fp ) )
724 gdcmVerboseMacro( "JPEG decompressor failed." );
729 ////////////////////////////////////////////
730 //// Third stage: twigle the bytes and bits.
731 ConvertReorderEndianity();
732 ConvertReArrangeBits();
733 ConvertHandleColor();
738 void PixelReadConvert::ConvertHandleColor()
740 //////////////////////////////////
741 // Deal with the color decoding i.e. handle:
742 // - R, G, B planes (as opposed to RGB pixels)
743 // - YBR (various) encodings.
744 // - LUT[s] (or "PALETTE COLOR").
746 // The classification in the color decoding schema is based on the blending
747 // of two Dicom tags values:
748 // * "Photometric Interpretation" for which we have the cases:
749 // - [Photo A] MONOCHROME[1|2] pictures,
750 // - [Photo B] RGB or YBR_FULL_422 (which acts as RGB),
751 // - [Photo C] YBR_* (with the above exception of YBR_FULL_422)
752 // - [Photo D] "PALETTE COLOR" which indicates the presence of LUT[s].
753 // * "Planar Configuration" for which we have the cases:
754 // - [Planar 0] 0 then Pixels are already RGB
755 // - [Planar 1] 1 then we have 3 planes : R, G, B,
756 // - [Planar 2] 2 then we have 1 gray Plane and 3 LUTs
758 // Now in theory, one could expect some coherence when blending the above
759 // cases. For example we should not encounter files belonging at the
760 // time to case [Planar 0] and case [Photo D].
761 // Alas, this was only theory ! Because in practice some odd (read ill
762 // formated Dicom) files (e.g. gdcmData/US-PAL-8-10x-echo.dcm) we encounter:
763 // - "Planar Configuration" = 0,
764 // - "Photometric Interpretation" = "PALETTE COLOR".
765 // Hence gdcm will use the folowing "heuristic" in order to be tolerant
766 // towards Dicom-non-conformance files:
767 // << whatever the "Planar Configuration" value might be, a
768 // "Photometric Interpretation" set to "PALETTE COLOR" forces
769 // a LUT intervention >>
771 // Now we are left with the following handling of the cases:
772 // - [Planar 0] OR [Photo A] no color decoding (since respectively
773 // Pixels are already RGB and monochrome pictures have no color :),
774 // - [Planar 1] AND [Photo B] handled with ConvertRGBPlanesToRGBPixels()
775 // - [Planar 1] AND [Photo C] handled with ConvertYcBcRPlanesToRGBPixels()
776 // - [Planar 2] OR [Photo D] requires LUT intervention.
780 // [Planar 2] OR [Photo D]: LUT intervention done outside
784 if ( PlanarConfiguration == 1 )
788 // [Planar 1] AND [Photo C] (remember YBR_FULL_422 acts as RGB)
789 ConvertYcBcRPlanesToRGBPixels();
793 // [Planar 1] AND [Photo C]
794 ConvertRGBPlanesToRGBPixels();
799 // When planarConf is 0, and RLELossless (forbidden by Dicom norm)
800 // pixels need to be RGB-fied anyway
803 ConvertRGBPlanesToRGBPixels();
805 // In *normal *case, when planarConf is 0, pixels are already in RGB
809 * \brief Predicate to know wether the image[s] (once Raw) is RGB.
810 * \note See comments of \ref ConvertHandleColor
812 bool PixelReadConvert::IsRawRGB()
815 || PlanarConfiguration == 2
823 void PixelReadConvert::ComputeRawAndRGBSizes()
825 int bitsAllocated = BitsAllocated;
826 // Number of "Bits Allocated" is fixed to 16 when it's 12, since
827 // in this case we will expand the image to 16 bits (see
828 // \ref ReadAndDecompress12BitsTo16Bits() )
829 if ( BitsAllocated == 12 )
834 RawSize = XSize * YSize * ZSize
835 * ( bitsAllocated / 8 )
839 RGBSize = 3 * RawSize;
847 void PixelReadConvert::GrabInformationsFromFile( File *file )
849 // Number of Bits Allocated for storing a Pixel is defaulted to 16
850 // when absent from the file.
851 BitsAllocated = file->GetBitsAllocated();
852 if ( BitsAllocated == 0 )
857 // Number of "Bits Stored", defaulted to number of "Bits Allocated"
858 // when absent from the file.
859 BitsStored = file->GetBitsStored();
860 if ( BitsStored == 0 )
862 BitsStored = BitsAllocated;
865 // High Bit Position, defaulted to "Bits Allocated" - 1
866 HighBitPosition = file->GetHighBitPosition();
867 if ( HighBitPosition == 0 )
869 HighBitPosition = BitsAllocated - 1;
872 XSize = file->GetXSize();
873 YSize = file->GetYSize();
874 ZSize = file->GetZSize();
875 SamplesPerPixel = file->GetSamplesPerPixel();
876 PixelSize = file->GetPixelSize();
877 PixelSign = file->IsSignedPixelData();
878 SwapCode = file->GetSwapCode();
879 std::string ts = file->GetTransferSyntax();
881 ( ! file->IsDicomV3() )
882 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian
883 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndianDLXGE
884 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian
885 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian
886 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian;
888 IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts);
889 IsJPEGLS = Global::GetTS()->IsJPEGLS(ts);
890 IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts);
891 IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts);
892 IsRLELossless = Global::GetTS()->IsRLELossless(ts);
894 PixelOffset = file->GetPixelOffset();
895 PixelDataLength = file->GetPixelAreaLength();
896 RLEInfo = file->GetRLEInfo();
897 JPEGInfo = file->GetJPEGInfo();
899 PlanarConfiguration = file->GetPlanarConfiguration();
900 IsMonochrome = file->IsMonochrome();
901 IsPaletteColor = file->IsPaletteColor();
902 IsYBRFull = file->IsYBRFull();
904 /////////////////////////////////////////////////////////////////
906 HasLUT = file->HasLUT();
909 // Just in case some access to a File element requires disk access.
910 LutRedDescriptor = file->GetEntryValue( 0x0028, 0x1101 );
911 LutGreenDescriptor = file->GetEntryValue( 0x0028, 0x1102 );
912 LutBlueDescriptor = file->GetEntryValue( 0x0028, 0x1103 );
914 // Depending on the value of Document::MAX_SIZE_LOAD_ELEMENT_VALUE
915 // [ refer to invocation of Document::SetMaxSizeLoadEntry() in
916 // Document::Document() ], the loading of the value (content) of a
917 // [Bin|Val]Entry occurence migth have been hindered (read simply NOT
918 // loaded). Hence, we first try to obtain the LUTs data from the file
919 // and when this fails we read the LUTs data directly from disk.
920 /// \TODO Reading a [Bin|Val]Entry directly from disk is a kludge.
921 /// We should NOT bypass the [Bin|Val]Entry class. Instead
922 /// an access to an UNLOADED content of a [Bin|Val]Entry occurence
923 /// (e.g. BinEntry::GetBinArea()) should force disk access from
924 /// within the [Bin|Val]Entry class itself. The only problem
925 /// is that the [Bin|Val]Entry is unaware of the FILE* is was
926 /// parsed from. Fix that. FIXME.
929 file->LoadEntryBinArea(0x0028, 0x1201);
930 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
933 gdcmVerboseMacro( "Unable to read Red LUT data" );
937 file->LoadEntryBinArea(0x0028, 0x1202);
938 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
941 gdcmVerboseMacro( "Unable to read Green LUT data" );
945 file->LoadEntryBinArea(0x0028, 0x1203);
946 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
949 gdcmVerboseMacro( "Unable to read Blue LUT data" );
953 ComputeRawAndRGBSizes();
957 * \brief Build Red/Green/Blue/Alpha LUT from File
958 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
959 * and (0028,1101),(0028,1102),(0028,1102)
960 * - xxx Palette Color Lookup Table Descriptor - are found
961 * and (0028,1201),(0028,1202),(0028,1202)
962 * - xxx Palette Color Lookup Table Data - are found
963 * \warning does NOT deal with :
964 * 0028 1100 Gray Lookup Table Descriptor (Retired)
965 * 0028 1221 Segmented Red Palette Color Lookup Table Data
966 * 0028 1222 Segmented Green Palette Color Lookup Table Data
967 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
968 * no known Dicom reader deals with them :-(
969 * @return a RGBA Lookup Table
971 void PixelReadConvert::BuildLUTRGBA()
978 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
980 if ( ! IsPaletteColor )
985 if ( LutRedDescriptor == GDCM_UNFOUND
986 || LutGreenDescriptor == GDCM_UNFOUND
987 || LutBlueDescriptor == GDCM_UNFOUND )
992 ////////////////////////////////////////////
993 // Extract the info from the LUT descriptors
994 int lengthR; // Red LUT length in Bytes
995 int debR; // Subscript of the first Lut Value
996 int nbitsR; // Lut item size (in Bits)
997 int nbRead = sscanf( LutRedDescriptor.c_str(),
999 &lengthR, &debR, &nbitsR );
1002 gdcmVerboseMacro( "Wrong Red LUT descriptor" );
1005 int lengthG; // Green LUT length in Bytes
1006 int debG; // Subscript of the first Lut Value
1007 int nbitsG; // Lut item size (in Bits)
1008 nbRead = sscanf( LutGreenDescriptor.c_str(),
1010 &lengthG, &debG, &nbitsG );
1013 gdcmVerboseMacro( "Wrong Green LUT descriptor" );
1016 int lengthB; // Blue LUT length in Bytes
1017 int debB; // Subscript of the first Lut Value
1018 int nbitsB; // Lut item size (in Bits)
1019 nbRead = sscanf( LutRedDescriptor.c_str(),
1021 &lengthB, &debB, &nbitsB );
1024 gdcmVerboseMacro( "Wrong Blue LUT descriptor" );
1027 ////////////////////////////////////////////////////////
1028 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
1033 ////////////////////////////////////////////////
1034 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1035 LutRGBA = new uint8_t[ 1024 ]; // 256 * 4 (R, G, B, Alpha)
1039 memset( LutRGBA, 0, 1024 );
1042 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
1044 // when LUT item size is different than pixel size
1045 mult = 2; // high byte must be = low byte
1049 // See PS 3.3-2003 C.11.1.1.2 p 619
1053 // if we get a black image, let's just remove the '+1'
1054 // from 'i*mult+1' and check again
1055 // if it works, we shall have to check the 3 Palettes
1056 // to see which byte is ==0 (first one, or second one)
1058 // We give up the checking to avoid some (useless ?) overhead
1059 // (optimistic asumption)
1061 uint8_t* a = LutRGBA + 0;
1062 for( i=0; i < lengthR; ++i )
1064 *a = LutRedData[i*mult+1];
1069 for( i=0; i < lengthG; ++i)
1071 *a = LutGreenData[i*mult+1];
1076 for(i=0; i < lengthB; ++i)
1078 *a = LutBlueData[i*mult+1];
1083 for(i=0; i < 256; ++i)
1085 *a = 1; // Alpha component
1091 * \brief Build the RGB image from the Raw imagage and the LUTs.
1093 bool PixelReadConvert::BuildRGBImage()
1097 // The job is already done.
1103 // The job can't be done
1110 // The job can't be done
1116 uint8_t* localRGB = RGB;
1117 for (size_t i = 0; i < RawSize; ++i )
1120 *localRGB++ = LutRGBA[j];
1121 *localRGB++ = LutRGBA[j+1];
1122 *localRGB++ = LutRGBA[j+2];
1128 * \brief Print self.
1129 * @param indent Indentation string to be prepended during printing.
1130 * @param os Stream to print to.
1132 void PixelReadConvert::Print( std::ostream &os, std::string const & indent )
1135 << "--- Pixel information -------------------------"
1138 << "Pixel Data: offset " << PixelOffset
1139 << " x(" << std::hex << PixelOffset << std::dec
1140 << ") length " << PixelDataLength
1141 << " x(" << std::hex << PixelDataLength << std::dec
1142 << ")" << std::endl;
1144 if ( IsRLELossless )
1148 RLEInfo->Print( os, indent );
1152 gdcmVerboseMacro("Set as RLE file but NO RLEinfo present.");
1156 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
1160 JPEGInfo->Print( os, indent );
1164 gdcmVerboseMacro("Set as JPEG file but NO JPEGinfo present.");
1169 } // end namespace gdcm
1171 // NOTES on File internal calls
1173 // ---> GetImageData
1174 // ---> GetImageDataIntoVector
1175 // |---> GetImageDataIntoVectorRaw
1176 // | lut intervention
1178 // ---> GetImageDataRaw
1179 // ---> GetImageDataIntoVectorRaw