1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelReadConvert.cxx,v $
6 Date: $Date: 2005/01/31 03:22:26 $
7 Version: $Revision: 1.38 $
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 RLEFrame *frame = RLEInfo->GetFirstFrame();
249 // Loop on the fragments
250 for( unsigned int k = 1; k <= frame->GetNumberOfFragments(); k++ )
252 fp->seekg(frame->GetOffset(k),std::ios::beg);
253 ReadAndDecompressRLEFragment(subRaw,
257 subRaw += RawSegmentSize;
259 frame = RLEInfo->GetNexttFrame();
262 if ( BitsAllocated == 16 )
264 // Try to deal with RLE 16 Bits
265 (void)DecompressRLE16BitsFromRLE8Bits( ZSize );
272 * \brief Swap the bytes, according to \ref SwapCode.
274 void PixelReadConvert::ConvertSwapZone()
278 if( BitsAllocated == 16 )
280 uint16_t *im16 = (uint16_t*)Raw;
288 for( i = 0; i < RawSize / 2; i++ )
290 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
294 gdcmVerboseMacro("SwapCode value (16 bits) not allowed.");
297 else if( BitsAllocated == 32 )
302 uint32_t* im32 = (uint32_t*)Raw;
308 for( i = 0; i < RawSize / 4; i++ )
310 low = im32[i] & 0x0000ffff; // 4321
311 high = im32[i] >> 16;
312 high = ( high >> 8 ) | ( high << 8 );
313 low = ( low >> 8 ) | ( low << 8 );
315 im32[i] = ( s32 << 16 ) | high;
319 for( i = 0; i < RawSize / 4; i++ )
321 low = im32[i] & 0x0000ffff; // 2143
322 high = im32[i] >> 16;
323 high = ( high >> 8 ) | ( high << 8 );
324 low = ( low >> 8 ) | ( low << 8 );
326 im32[i] = ( s32 << 16 ) | low;
330 for( i = 0; i < RawSize / 4; i++ )
332 low = im32[i] & 0x0000ffff; // 3412
333 high = im32[i] >> 16;
335 im32[i] = ( s32 << 16 ) | high;
339 gdcmVerboseMacro("SwapCode value (32 bits) not allowed." );
345 * \brief Deal with endianness i.e. re-arange bytes inside the integer
347 void PixelReadConvert::ConvertReorderEndianity()
349 if ( BitsAllocated != 8 )
354 // Special kludge in order to deal with xmedcon broken images:
355 if ( BitsAllocated == 16
356 && BitsStored < BitsAllocated
359 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
360 uint16_t *deb = (uint16_t *)Raw;
361 for(int i = 0; i<l; i++)
373 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
374 * file and decompress it.
375 * @param fp File Pointer
378 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
382 gdcmVerboseMacro( "Sorry, JPEG2000 not yet taken into account" );
383 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
384 // if ( ! gdcm_read_JPEG2000_file( fp,Raw ) )
390 gdcmVerboseMacro( "Sorry, JPEG-LS not yet taken into account" );
391 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
392 // if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
397 // Precompute the offset localRaw will be shifted with
398 int length = XSize * YSize * SamplesPerPixel;
399 int numberBytes = BitsAllocated / 8;
401 JPEGInfo->DecompressJPEGFramesFromFile(fp, Raw, BitsStored, numberBytes, length );
406 * \brief Re-arrange the bits within the bytes.
409 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
411 if ( BitsStored != BitsAllocated )
413 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
414 if ( BitsAllocated == 16 )
416 uint16_t mask = 0xffff;
417 mask = mask >> ( BitsAllocated - BitsStored );
418 uint16_t* deb = (uint16_t*)Raw;
419 for(int i = 0; i<l; i++)
421 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & mask;
425 else if ( BitsAllocated == 32 )
427 uint32_t mask = 0xffffffff;
428 mask = mask >> ( BitsAllocated - BitsStored );
429 uint32_t* deb = (uint32_t*)Raw;
430 for(int i = 0; i<l; i++)
432 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & mask;
438 gdcmVerboseMacro("Weird image");
439 throw FormatError( "Weird image !?" );
446 * \brief Convert (cY plane, cB plane, cR plane) to RGB pixels
447 * \warning Works on all the frames at a time
449 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
451 uint8_t *localRaw = Raw;
452 uint8_t *copyRaw = new uint8_t[ RawSize ];
453 memmove( copyRaw, localRaw, RawSize );
455 // to see the tricks about YBR_FULL, YBR_FULL_422,
456 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
457 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
458 // and be *very* affraid
460 int l = XSize * YSize;
461 int nbFrames = ZSize;
463 uint8_t *a = copyRaw;
464 uint8_t *b = copyRaw + l;
465 uint8_t *c = copyRaw + l + l;
468 /// \todo : Replace by the 'well known' integer computation
469 /// counterpart. Refer to
470 /// http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
471 /// for code optimisation.
473 for ( int i = 0; i < nbFrames; i++ )
475 for ( int j = 0; j < l; j++ )
477 R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
478 G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
479 B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
481 if (R < 0.0) R = 0.0;
482 if (G < 0.0) G = 0.0;
483 if (B < 0.0) B = 0.0;
484 if (R > 255.0) R = 255.0;
485 if (G > 255.0) G = 255.0;
486 if (B > 255.0) B = 255.0;
488 *(localRaw++) = (uint8_t)R;
489 *(localRaw++) = (uint8_t)G;
490 *(localRaw++) = (uint8_t)B;
500 * \brief Convert (Red plane, Green plane, Blue plane) to RGB pixels
501 * \warning Works on all the frames at a time
503 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
505 uint8_t *localRaw = Raw;
506 uint8_t *copyRaw = new uint8_t[ RawSize ];
507 memmove( copyRaw, localRaw, RawSize );
509 int l = XSize * YSize * ZSize;
511 uint8_t* a = copyRaw;
512 uint8_t* b = copyRaw + l;
513 uint8_t* c = copyRaw + l + l;
515 for (int j = 0; j < l; j++)
517 *(localRaw++) = *(a++);
518 *(localRaw++) = *(b++);
519 *(localRaw++) = *(c++);
524 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
526 // ComputeRawAndRGBSizes is already made by
527 // ::GrabInformationsFromfile. So, the structure sizes are
531 //////////////////////////////////////////////////
532 //// First stage: get our hands on the Pixel Data.
535 gdcmVerboseMacro( "Unavailable file pointer." );
539 fp->seekg( PixelOffset, std::ios::beg );
540 if( fp->fail() || fp->eof())
542 gdcmVerboseMacro( "Unable to find PixelOffset in file." );
548 //////////////////////////////////////////////////
549 //// Second stage: read from disk dans decompress.
550 if ( BitsAllocated == 12 )
552 ReadAndDecompress12BitsTo16Bits( fp);
556 // This problem can be found when some obvious informations are found
557 // after the field containing the image data. In this case, these
558 // bad data are added to the size of the image (in the PixelDataLength
559 // variable). But RawSize is the right size of the image !
560 if( PixelDataLength != RawSize)
562 gdcmVerboseMacro( "Mismatch between PixelReadConvert and RawSize." );
564 if( PixelDataLength > RawSize)
566 fp->read( (char*)Raw, RawSize);
570 fp->read( (char*)Raw, PixelDataLength);
573 if ( fp->fail() || fp->eof())
575 gdcmVerboseMacro( "Reading of Raw pixel data failed." );
579 else if ( IsRLELossless )
581 if ( ! ReadAndDecompressRLEFile( fp ) )
583 gdcmVerboseMacro( "RLE decompressor failed." );
589 // Default case concerns JPEG family
590 if ( ! ReadAndDecompressJPEGFile( fp ) )
592 gdcmVerboseMacro( "JPEG decompressor failed." );
597 ////////////////////////////////////////////
598 //// Third stage: twigle the bytes and bits.
599 ConvertReorderEndianity();
600 ConvertReArrangeBits();
601 ConvertHandleColor();
606 void PixelReadConvert::ConvertHandleColor()
608 //////////////////////////////////
609 // Deal with the color decoding i.e. handle:
610 // - R, G, B planes (as opposed to RGB pixels)
611 // - YBR (various) encodings.
612 // - LUT[s] (or "PALETTE COLOR").
614 // The classification in the color decoding schema is based on the blending
615 // of two Dicom tags values:
616 // * "Photometric Interpretation" for which we have the cases:
617 // - [Photo A] MONOCHROME[1|2] pictures,
618 // - [Photo B] RGB or YBR_FULL_422 (which acts as RGB),
619 // - [Photo C] YBR_* (with the above exception of YBR_FULL_422)
620 // - [Photo D] "PALETTE COLOR" which indicates the presence of LUT[s].
621 // * "Planar Configuration" for which we have the cases:
622 // - [Planar 0] 0 then Pixels are already RGB
623 // - [Planar 1] 1 then we have 3 planes : R, G, B,
624 // - [Planar 2] 2 then we have 1 gray Plane and 3 LUTs
626 // Now in theory, one could expect some coherence when blending the above
627 // cases. For example we should not encounter files belonging at the
628 // time to case [Planar 0] and case [Photo D].
629 // Alas, this was only theory ! Because in practice some odd (read ill
630 // formated Dicom) files (e.g. gdcmData/US-PAL-8-10x-echo.dcm) we encounter:
631 // - "Planar Configuration" = 0,
632 // - "Photometric Interpretation" = "PALETTE COLOR".
633 // Hence gdcm will use the folowing "heuristic" in order to be tolerant
634 // towards Dicom-non-conformance files:
635 // << whatever the "Planar Configuration" value might be, a
636 // "Photometric Interpretation" set to "PALETTE COLOR" forces
637 // a LUT intervention >>
639 // Now we are left with the following handling of the cases:
640 // - [Planar 0] OR [Photo A] no color decoding (since respectively
641 // Pixels are already RGB and monochrome pictures have no color :),
642 // - [Planar 1] AND [Photo B] handled with ConvertRGBPlanesToRGBPixels()
643 // - [Planar 1] AND [Photo C] handled with ConvertYcBcRPlanesToRGBPixels()
644 // - [Planar 2] OR [Photo D] requires LUT intervention.
648 // [Planar 2] OR [Photo D]: LUT intervention done outside
652 if ( PlanarConfiguration == 1 )
656 // [Planar 1] AND [Photo C] (remember YBR_FULL_422 acts as RGB)
657 ConvertYcBcRPlanesToRGBPixels();
661 // [Planar 1] AND [Photo C]
662 ConvertRGBPlanesToRGBPixels();
667 // When planarConf is 0, and RLELossless (forbidden by Dicom norm)
668 // pixels need to be RGB-fied anyway
671 ConvertRGBPlanesToRGBPixels();
673 // In *normal *case, when planarConf is 0, pixels are already in RGB
677 * \brief Predicate to know wether the image[s] (once Raw) is RGB.
678 * \note See comments of \ref ConvertHandleColor
680 bool PixelReadConvert::IsRawRGB()
683 || PlanarConfiguration == 2
691 void PixelReadConvert::ComputeRawAndRGBSizes()
693 int bitsAllocated = BitsAllocated;
694 // Number of "Bits Allocated" is fixed to 16 when it's 12, since
695 // in this case we will expand the image to 16 bits (see
696 // \ref ReadAndDecompress12BitsTo16Bits() )
697 if ( BitsAllocated == 12 )
702 RawSize = XSize * YSize * ZSize
703 * ( bitsAllocated / 8 )
707 RGBSize = 3 * RawSize;
715 void PixelReadConvert::GrabInformationsFromFile( File *file )
717 // Number of Bits Allocated for storing a Pixel is defaulted to 16
718 // when absent from the file.
719 BitsAllocated = file->GetBitsAllocated();
720 if ( BitsAllocated == 0 )
725 // Number of "Bits Stored", defaulted to number of "Bits Allocated"
726 // when absent from the file.
727 BitsStored = file->GetBitsStored();
728 if ( BitsStored == 0 )
730 BitsStored = BitsAllocated;
733 // High Bit Position, defaulted to "Bits Allocated" - 1
734 HighBitPosition = file->GetHighBitPosition();
735 if ( HighBitPosition == 0 )
737 HighBitPosition = BitsAllocated - 1;
740 XSize = file->GetXSize();
741 YSize = file->GetYSize();
742 ZSize = file->GetZSize();
743 SamplesPerPixel = file->GetSamplesPerPixel();
744 PixelSize = file->GetPixelSize();
745 PixelSign = file->IsSignedPixelData();
746 SwapCode = file->GetSwapCode();
747 std::string ts = file->GetTransferSyntax();
749 ( ! file->IsDicomV3() )
750 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian
751 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndianDLXGE
752 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian
753 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian
754 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian;
756 IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts);
757 IsJPEGLS = Global::GetTS()->IsJPEGLS(ts);
758 IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts);
759 IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts);
760 IsRLELossless = Global::GetTS()->IsRLELossless(ts);
762 PixelOffset = file->GetPixelOffset();
763 PixelDataLength = file->GetPixelAreaLength();
764 RLEInfo = file->GetRLEInfo();
765 JPEGInfo = file->GetJPEGInfo();
767 PlanarConfiguration = file->GetPlanarConfiguration();
768 IsMonochrome = file->IsMonochrome();
769 IsPaletteColor = file->IsPaletteColor();
770 IsYBRFull = file->IsYBRFull();
772 /////////////////////////////////////////////////////////////////
774 HasLUT = file->HasLUT();
777 // Just in case some access to a File element requires disk access.
778 LutRedDescriptor = file->GetEntryValue( 0x0028, 0x1101 );
779 LutGreenDescriptor = file->GetEntryValue( 0x0028, 0x1102 );
780 LutBlueDescriptor = file->GetEntryValue( 0x0028, 0x1103 );
782 // Depending on the value of Document::MAX_SIZE_LOAD_ELEMENT_VALUE
783 // [ refer to invocation of Document::SetMaxSizeLoadEntry() in
784 // Document::Document() ], the loading of the value (content) of a
785 // [Bin|Val]Entry occurence migth have been hindered (read simply NOT
786 // loaded). Hence, we first try to obtain the LUTs data from the file
787 // and when this fails we read the LUTs data directly from disk.
788 /// \TODO Reading a [Bin|Val]Entry directly from disk is a kludge.
789 /// We should NOT bypass the [Bin|Val]Entry class. Instead
790 /// an access to an UNLOADED content of a [Bin|Val]Entry occurence
791 /// (e.g. BinEntry::GetBinArea()) should force disk access from
792 /// within the [Bin|Val]Entry class itself. The only problem
793 /// is that the [Bin|Val]Entry is unaware of the FILE* is was
794 /// parsed from. Fix that. FIXME.
797 file->LoadEntryBinArea(0x0028, 0x1201);
798 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
801 gdcmVerboseMacro( "Unable to read Red LUT data" );
805 file->LoadEntryBinArea(0x0028, 0x1202);
806 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
809 gdcmVerboseMacro( "Unable to read Green LUT data" );
813 file->LoadEntryBinArea(0x0028, 0x1203);
814 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
817 gdcmVerboseMacro( "Unable to read Blue LUT data" );
821 ComputeRawAndRGBSizes();
825 * \brief Build Red/Green/Blue/Alpha LUT from File
826 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
827 * and (0028,1101),(0028,1102),(0028,1102)
828 * - xxx Palette Color Lookup Table Descriptor - are found
829 * and (0028,1201),(0028,1202),(0028,1202)
830 * - xxx Palette Color Lookup Table Data - are found
831 * \warning does NOT deal with :
832 * 0028 1100 Gray Lookup Table Descriptor (Retired)
833 * 0028 1221 Segmented Red Palette Color Lookup Table Data
834 * 0028 1222 Segmented Green Palette Color Lookup Table Data
835 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
836 * no known Dicom reader deals with them :-(
837 * @return a RGBA Lookup Table
839 void PixelReadConvert::BuildLUTRGBA()
846 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
848 if ( ! IsPaletteColor )
853 if ( LutRedDescriptor == GDCM_UNFOUND
854 || LutGreenDescriptor == GDCM_UNFOUND
855 || LutBlueDescriptor == GDCM_UNFOUND )
860 ////////////////////////////////////////////
861 // Extract the info from the LUT descriptors
862 int lengthR; // Red LUT length in Bytes
863 int debR; // Subscript of the first Lut Value
864 int nbitsR; // Lut item size (in Bits)
865 int nbRead = sscanf( LutRedDescriptor.c_str(),
867 &lengthR, &debR, &nbitsR );
870 gdcmVerboseMacro( "Wrong Red LUT descriptor" );
873 int lengthG; // Green LUT length in Bytes
874 int debG; // Subscript of the first Lut Value
875 int nbitsG; // Lut item size (in Bits)
876 nbRead = sscanf( LutGreenDescriptor.c_str(),
878 &lengthG, &debG, &nbitsG );
881 gdcmVerboseMacro( "Wrong Green LUT descriptor" );
884 int lengthB; // Blue LUT length in Bytes
885 int debB; // Subscript of the first Lut Value
886 int nbitsB; // Lut item size (in Bits)
887 nbRead = sscanf( LutRedDescriptor.c_str(),
889 &lengthB, &debB, &nbitsB );
892 gdcmVerboseMacro( "Wrong Blue LUT descriptor" );
895 ////////////////////////////////////////////////////////
896 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
901 ////////////////////////////////////////////////
902 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
903 LutRGBA = new uint8_t[ 1024 ]; // 256 * 4 (R, G, B, Alpha)
907 memset( LutRGBA, 0, 1024 );
910 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
912 // when LUT item size is different than pixel size
913 mult = 2; // high byte must be = low byte
917 // See PS 3.3-2003 C.11.1.1.2 p 619
921 // if we get a black image, let's just remove the '+1'
922 // from 'i*mult+1' and check again
923 // if it works, we shall have to check the 3 Palettes
924 // to see which byte is ==0 (first one, or second one)
926 // We give up the checking to avoid some (useless ?) overhead
927 // (optimistic asumption)
929 uint8_t* a = LutRGBA + 0;
930 for( i=0; i < lengthR; ++i )
932 *a = LutRedData[i*mult+1];
937 for( i=0; i < lengthG; ++i)
939 *a = LutGreenData[i*mult+1];
944 for(i=0; i < lengthB; ++i)
946 *a = LutBlueData[i*mult+1];
951 for(i=0; i < 256; ++i)
953 *a = 1; // Alpha component
959 * \brief Build the RGB image from the Raw imagage and the LUTs.
961 bool PixelReadConvert::BuildRGBImage()
965 // The job is already done.
971 // The job can't be done
978 // The job can't be done
984 uint8_t* localRGB = RGB;
985 for (size_t i = 0; i < RawSize; ++i )
988 *localRGB++ = LutRGBA[j];
989 *localRGB++ = LutRGBA[j+1];
990 *localRGB++ = LutRGBA[j+2];
997 * @param indent Indentation string to be prepended during printing.
998 * @param os Stream to print to.
1000 void PixelReadConvert::Print( std::ostream &os, std::string const & indent )
1003 << "--- Pixel information -------------------------"
1006 << "Pixel Data: offset " << PixelOffset
1007 << " x(" << std::hex << PixelOffset << std::dec
1008 << ") length " << PixelDataLength
1009 << " x(" << std::hex << PixelDataLength << std::dec
1010 << ")" << std::endl;
1012 if ( IsRLELossless )
1016 RLEInfo->Print( os, indent );
1020 gdcmVerboseMacro("Set as RLE file but NO RLEinfo present.");
1024 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
1028 JPEGInfo->Print( os, indent );
1032 gdcmVerboseMacro("Set as JPEG file but NO JPEGinfo present.");
1037 } // end namespace gdcm
1039 // NOTES on File internal calls
1041 // ---> GetImageData
1042 // ---> GetImageDataIntoVector
1043 // |---> GetImageDataIntoVectorRaw
1044 // | lut intervention
1046 // ---> GetImageDataRaw
1047 // ---> GetImageDataIntoVectorRaw