1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelReadConvert.cxx,v $
6 Date: $Date: 2005/10/20 09:17:54 $
7 Version: $Revision: 1.82 $
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 "gdcmPixelReadConvert.h"
20 #include "gdcmDebug.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDocEntry.h"
25 #include "gdcmRLEFramesInfo.h"
26 #include "gdcmJPEGFragmentsInfo.h"
29 #include <stdio.h> //for sscanf
34 //bool ReadMPEGFile (std::ifstream *fp, void *image_buffer, size_t lenght);
35 bool gdcm_read_JPEG2000_file (void* raw,
36 char *inputdata, size_t inputlength);
37 //-----------------------------------------------------------------------------
38 #define str2num(str, typeNum) *((typeNum *)(str))
40 //-----------------------------------------------------------------------------
41 // Constructor / Destructor
43 PixelReadConvert::PixelReadConvert()
59 /// Canonical Destructor
60 PixelReadConvert::~PixelReadConvert()
65 //-----------------------------------------------------------------------------
68 * \brief Predicate to know whether the image[s] (once Raw) is RGB.
69 * \note See comments of \ref ConvertHandleColor
71 bool PixelReadConvert::IsRawRGB()
74 || PlanarConfiguration == 2
82 * \brief Gets various usefull informations from the file header
83 * @param file gdcm::File pointer
85 void PixelReadConvert::GrabInformationsFromFile( File *file )
87 // Number of Bits Allocated for storing a Pixel is defaulted to 16
88 // when absent from the file.
89 BitsAllocated = file->GetBitsAllocated();
90 if ( BitsAllocated == 0 )
95 // Number of "Bits Stored", defaulted to number of "Bits Allocated"
96 // when absent from the file.
97 BitsStored = file->GetBitsStored();
98 if ( BitsStored == 0 )
100 BitsStored = BitsAllocated;
103 // High Bit Position, defaulted to "Bits Allocated" - 1
104 HighBitPosition = file->GetHighBitPosition();
105 if ( HighBitPosition == 0 )
107 HighBitPosition = BitsAllocated - 1;
110 XSize = file->GetXSize();
111 YSize = file->GetYSize();
112 ZSize = file->GetZSize();
113 SamplesPerPixel = file->GetSamplesPerPixel();
114 //PixelSize = file->GetPixelSize(); Useless
115 PixelSign = file->IsSignedPixelData();
116 SwapCode = file->GetSwapCode();
117 std::string ts = file->GetTransferSyntax();
119 ( ! file->IsDicomV3() )
120 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian
121 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE
122 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian
123 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian
124 || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian;
126 IsPrivateGETransferSyntax =
127 ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE );
129 IsMPEG = Global::GetTS()->IsMPEG(ts);
130 IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts);
131 IsJPEGLS = Global::GetTS()->IsJPEGLS(ts);
132 IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts);
133 IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts);
134 IsRLELossless = Global::GetTS()->IsRLELossless(ts);
136 PixelOffset = file->GetPixelOffset();
137 PixelDataLength = file->GetPixelAreaLength();
138 RLEInfo = file->GetRLEInfo();
139 JPEGInfo = file->GetJPEGInfo();
141 IsMonochrome = file->IsMonochrome();
142 IsMonochrome1 = file->IsMonochrome1();
143 IsPaletteColor = file->IsPaletteColor();
144 IsYBRFull = file->IsYBRFull();
146 PlanarConfiguration = file->GetPlanarConfiguration();
148 /////////////////////////////////////////////////////////////////
150 HasLUT = file->HasLUT();
153 // Just in case some access to a File element requires disk access.
154 LutRedDescriptor = file->GetEntryString( 0x0028, 0x1101 );
155 LutGreenDescriptor = file->GetEntryString( 0x0028, 0x1102 );
156 LutBlueDescriptor = file->GetEntryString( 0x0028, 0x1103 );
158 // The following comment is probabely meaningless, since LUT are *always*
159 // loaded at parsing time, whatever their length is.
161 // Depending on the value of Document::MAX_SIZE_LOAD_ELEMENT_VALUE
162 // [ refer to invocation of Document::SetMaxSizeLoadEntry() in
163 // Document::Document() ], the loading of the value (content) of a
164 // [Bin|Val]Entry occurence migth have been hindered (read simply NOT
165 // loaded). Hence, we first try to obtain the LUTs data from the file
166 // and when this fails we read the LUTs data directly from disk.
167 // \TODO Reading a [Bin|Val]Entry directly from disk is a kludge.
168 // We should NOT bypass the [Bin|Val]Entry class. Instead
169 // an access to an UNLOADED content of a [Bin|Val]Entry occurence
170 // (e.g. DataEntry::GetBinArea()) should force disk access from
171 // within the [Bin|Val]Entry class itself. The only problem
172 // is that the [Bin|Val]Entry is unaware of the FILE* is was
173 // parsed from. Fix that. FIXME.
176 file->LoadEntryBinArea(0x0028, 0x1201);
177 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
180 gdcmWarningMacro("Unable to read Red Palette Color Lookup Table data");
184 file->LoadEntryBinArea(0x0028, 0x1202);
185 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
188 gdcmWarningMacro("Unable to read Green Palette Color Lookup Table data");
192 file->LoadEntryBinArea(0x0028, 0x1203);
193 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
196 gdcmWarningMacro("Unable to read Blue Palette Color Lookup Table data");
201 ComputeRawAndRGBSizes();
204 /// \brief Reads from disk and decompresses Pixels
205 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
207 // ComputeRawAndRGBSizes is already made by
208 // ::GrabInformationsFromfile. So, the structure sizes are
212 //////////////////////////////////////////////////
213 //// First stage: get our hands on the Pixel Data.
216 gdcmWarningMacro( "Unavailable file pointer." );
220 fp->seekg( PixelOffset, std::ios::beg );
221 if ( fp->fail() || fp->eof() )
223 gdcmWarningMacro( "Unable to find PixelOffset in file." );
229 //////////////////////////////////////////////////
230 //// Second stage: read from disk and decompress.
231 if ( BitsAllocated == 12 )
233 ReadAndDecompress12BitsTo16Bits( fp);
237 // This problem can be found when some obvious informations are found
238 // after the field containing the image data. In this case, these
239 // bad data are added to the size of the image (in the PixelDataLength
240 // variable). But RawSize is the right size of the image !
241 if ( PixelDataLength != RawSize )
243 gdcmWarningMacro( "Mismatch between PixelReadConvert : "
244 << PixelDataLength << " and RawSize : " << RawSize );
246 if ( PixelDataLength > RawSize )
248 fp->read( (char*)Raw, RawSize);
252 fp->read( (char*)Raw, PixelDataLength);
255 if ( fp->fail() || fp->eof())
257 gdcmWarningMacro( "Reading of Raw pixel data failed." );
261 else if ( IsRLELossless )
263 if ( ! RLEInfo->DecompressRLEFile
264 ( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) )
266 gdcmWarningMacro( "RLE decompressor failed." );
272 //gdcmWarningMacro( "Sorry, MPEG not yet taken into account" );
274 // fp has already been seek to start of mpeg
275 //ReadMPEGFile(fp, Raw, PixelDataLength);
280 // Default case concerns JPEG family
281 if ( ! ReadAndDecompressJPEGFile( fp ) )
283 gdcmWarningMacro( "JPEG decompressor failed." );
288 ////////////////////////////////////////////
289 //// Third stage: twigle the bytes and bits.
290 ConvertReorderEndianity();
291 ConvertReArrangeBits();
292 ConvertFixGreyLevels();
293 if (UserFunction) // user is allowed to Mirror, TopDown, Rotate,...the image
294 UserFunction( Raw, FileInternal);
295 ConvertHandleColor();
300 /// Deletes Pixels Area
301 void PixelReadConvert::Squeeze()
317 * \brief Build the RGB image from the Raw image and the LUTs.
319 bool PixelReadConvert::BuildRGBImage()
323 // The job is already done.
329 // The job can't be done
336 // The job can't be done
340 gdcmWarningMacro( "--> BuildRGBImage" );
346 if ( BitsAllocated <= 8 )
348 uint8_t *localRGB = RGB;
349 for (size_t i = 0; i < RawSize; ++i )
352 *localRGB++ = LutRGBA[j];
353 *localRGB++ = LutRGBA[j+1];
354 *localRGB++ = LutRGBA[j+2];
358 else // deal with 16 bits pixels and 16 bits Palette color
360 uint16_t *localRGB = (uint16_t *)RGB;
361 for (size_t i = 0; i < RawSize/2; ++i )
363 j = ((uint16_t *)Raw)[i] * 4;
364 *localRGB++ = ((uint16_t *)LutRGBA)[j];
365 *localRGB++ = ((uint16_t *)LutRGBA)[j+1];
366 *localRGB++ = ((uint16_t *)LutRGBA)[j+2];
373 //-----------------------------------------------------------------------------
376 //-----------------------------------------------------------------------------
379 * \brief Read from file a 12 bits per pixel image and decompress it
380 * into a 16 bits per pixel image.
382 void PixelReadConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream *fp )
383 throw ( FormatError )
385 int nbPixels = XSize * YSize;
386 uint16_t *localDecompres = (uint16_t*)Raw;
388 for( int p = 0; p < nbPixels; p += 2 )
392 fp->read( (char*)&b0, 1);
393 if ( fp->fail() || fp->eof() )
395 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
396 "Unfound first block" );
399 fp->read( (char*)&b1, 1 );
400 if ( fp->fail() || fp->eof())
402 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
403 "Unfound second block" );
406 fp->read( (char*)&b2, 1 );
407 if ( fp->fail() || fp->eof())
409 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
410 "Unfound second block" );
413 // Two steps are necessary to please VC++
415 // 2 pixels 12bit = [0xABCDEF]
416 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
418 *localDecompres++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
420 *localDecompres++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
422 /// \todo JPR Troubles expected on Big-Endian processors ?
427 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
428 * file and decompress it.
429 * @param fp File Pointer
432 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
436 // make sure this is the right JPEG compression
437 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEGLS );
438 // FIXME this is really ugly but it seems I have to load the complete
439 // jpeg2000 stream to use jasper:
440 // I don't think we'll ever be able to deal with multiple fragments properly
442 unsigned long inputlength = 0;
443 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
446 inputlength += jpegfrag->GetLength();
447 jpegfrag = JPEGInfo->GetNextFragment();
449 gdcmAssertMacro( inputlength != 0);
450 uint8_t *inputdata = new uint8_t[inputlength];
451 char *pinputdata = (char*)inputdata;
452 jpegfrag = JPEGInfo->GetFirstFragment();
455 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
456 fp->read(pinputdata, jpegfrag->GetLength());
457 pinputdata += jpegfrag->GetLength();
458 jpegfrag = JPEGInfo->GetNextFragment();
460 // Warning the inputdata buffer is delete in the function
461 if ( ! gdcm_read_JPEG2000_file( Raw,
462 (char*)inputdata, inputlength ) )
466 // wow what happen, must be an error
471 // make sure this is the right JPEG compression
472 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEG2000 );
473 // WARNING : JPEG-LS is NOT the 'classical' Jpeg Lossless :
474 // [JPEG-LS is the basis for new lossless/near-lossless compression
475 // standard for continuous-tone images intended for JPEG2000. The standard
476 // is based on the LOCO-I algorithm (LOw COmplexity LOssless COmpression
477 // for Images) developed at Hewlett-Packard Laboratories]
479 // see http://datacompression.info/JPEGLS.shtml
482 std::cerr << "count:" << JPEGInfo->GetFragmentCount() << std::endl;
483 unsigned long inputlength = 0;
484 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
487 inputlength += jpegfrag->GetLength();
488 jpegfrag = JPEGInfo->GetNextFragment();
490 gdcmAssertMacro( inputlength != 0);
491 uint8_t *inputdata = new uint8_t[inputlength];
492 char *pinputdata = (char*)inputdata;
493 jpegfrag = JPEGInfo->GetFirstFragment();
496 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
497 fp->read(pinputdata, jpegfrag->GetLength());
498 pinputdata += jpegfrag->GetLength();
499 jpegfrag = JPEGInfo->GetNextFragment();
502 //fp->read((char*)Raw, PixelDataLength);
504 std::ofstream out("/tmp/jpegls.jpg");
505 out.write((char*)inputdata, inputlength);
510 gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
511 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
512 // if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
517 // make sure this is the right JPEG compression
518 assert( !IsJPEGLS || !IsJPEG2000 );
519 // Precompute the offset localRaw will be shifted with
520 int length = XSize * YSize * SamplesPerPixel;
521 int numberBytes = BitsAllocated / 8;
523 JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length );
529 * \brief Build Red/Green/Blue/Alpha LUT from File
530 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
531 * and (0028,1101),(0028,1102),(0028,1102)
532 * - xxx Palette Color Lookup Table Descriptor - are found
533 * and (0028,1201),(0028,1202),(0028,1202)
534 * - xxx Palette Color Lookup Table Data - are found
535 * \warning does NOT deal with :
536 * 0028 1100 Gray Lookup Table Descriptor (Retired)
537 * 0028 1221 Segmented Red Palette Color Lookup Table Data
538 * 0028 1222 Segmented Green Palette Color Lookup Table Data
539 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
540 * no known Dicom reader deals with them :-(
541 * @return a RGBA Lookup Table
543 void PixelReadConvert::BuildLUTRGBA()
546 // Note to code reviewers :
547 // The problem is *much more* complicated, since a lot of manufacturers
548 // Don't follow the norm :
549 // have a look at David Clunie's remark at the end of this .cxx file.
556 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
558 if ( ! IsPaletteColor )
563 if ( LutRedDescriptor == GDCM_UNFOUND
564 || LutGreenDescriptor == GDCM_UNFOUND
565 || LutBlueDescriptor == GDCM_UNFOUND )
567 gdcmWarningMacro( "(At least) a LUT Descriptor is missing" );
571 ////////////////////////////////////////////
572 // Extract the info from the LUT descriptors
573 int lengthR; // Red LUT length in Bytes
574 int debR; // Subscript of the first Lut Value
575 int nbitsR; // Lut item size (in Bits)
576 int nbRead; // nb of items in LUT descriptor (must be = 3)
578 nbRead = sscanf( LutRedDescriptor.c_str(),
580 &lengthR, &debR, &nbitsR );
583 gdcmWarningMacro( "Wrong Red LUT descriptor" );
585 int lengthG; // Green LUT length in Bytes
586 int debG; // Subscript of the first Lut Value
587 int nbitsG; // Lut item size (in Bits)
589 nbRead = sscanf( LutGreenDescriptor.c_str(),
591 &lengthG, &debG, &nbitsG );
594 gdcmWarningMacro( "Wrong Green LUT descriptor" );
597 int lengthB; // Blue LUT length in Bytes
598 int debB; // Subscript of the first Lut Value
599 int nbitsB; // Lut item size (in Bits)
600 nbRead = sscanf( LutRedDescriptor.c_str(),
602 &lengthB, &debB, &nbitsB );
605 gdcmWarningMacro( "Wrong Blue LUT descriptor" );
608 gdcmWarningMacro(" lengthR " << lengthR << " debR "
609 << debR << " nbitsR " << nbitsR);
610 gdcmWarningMacro(" lengthG " << lengthG << " debG "
611 << debG << " nbitsG " << nbitsG);
612 gdcmWarningMacro(" lengthB " << lengthB << " debB "
613 << debB << " nbitsB " << nbitsB);
615 if ( !lengthR ) // if = 2^16, this shall be 0 see : CP-143
617 if ( !lengthG ) // if = 2^16, this shall be 0
619 if ( !lengthB ) // if = 2^16, this shall be 0
622 ////////////////////////////////////////////////////////
624 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
626 gdcmWarningMacro( "(At least) a LUT is missing" );
630 // -------------------------------------------------------------
632 if ( BitsAllocated <= 8 )
634 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
635 LutRGBA = new uint8_t[ 1024 ]; // 256 * 4 (R, G, B, Alpha)
640 memset( LutRGBA, 0, 1024 );
643 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
645 // when LUT item size is different than pixel size
646 mult = 2; // high byte must be = low byte
650 // See PS 3.3-2003 C.11.1.1.2 p 619
654 // if we get a black image, let's just remove the '+1'
655 // from 'i*mult+1' and check again
656 // if it works, we shall have to check the 3 Palettes
657 // to see which byte is ==0 (first one, or second one)
659 // We give up the checking to avoid some (useless ?) overhead
660 // (optimistic asumption)
664 //take "Subscript of the first Lut Value" (debR,debG,debB) into account!
666 //FIXME : +1 : to get 'low value' byte
667 // Trouble expected on Big Endian Processors ?
668 // 16 BIts Per Pixel Palette Color to be swapped?
670 a = LutRGBA + 0 + debR;
671 for( i=0; i < lengthR; ++i )
673 *a = LutRedData[i*mult+1];
677 a = LutRGBA + 1 + debG;
678 for( i=0; i < lengthG; ++i)
680 *a = LutGreenData[i*mult+1];
684 a = LutRGBA + 2 + debB;
685 for(i=0; i < lengthB; ++i)
687 *a = LutBlueData[i*mult+1];
692 for(i=0; i < 256; ++i)
694 *a = 1; // Alpha component
700 // Probabely the same stuff is to be done for 16 Bits Pixels
701 // with 65536 entries LUT ?!?
702 // Still looking for accurate info on the web :-(
704 gdcmWarningMacro( "Sorry Palette Color Lookup Tables not yet dealt with"
705 << " for 16 Bits Per Pixel images" );
707 // forge the 4 * 16 Bits Red/Green/Blue/Alpha LUT
709 LutRGBA = (uint8_t *)new uint16_t[ 65536*4 ]; // 2^16 * 4 (R, G, B, Alpha)
712 memset( LutRGBA, 0, 65536*4*2 ); // 16 bits = 2 bytes ;-)
714 LutItemNumber = 65536;
720 //take "Subscript of the first Lut Value" (debR,debG,debB) into account!
722 a16 = (uint16_t*)LutRGBA + 0 + debR;
723 for( i=0; i < lengthR; ++i )
725 *a16 = ((uint16_t*)LutRedData)[i];
729 a16 = (uint16_t*)LutRGBA + 1 + debG;
730 for( i=0; i < lengthG; ++i)
732 *a16 = ((uint16_t*)LutGreenData)[i];
736 a16 = (uint16_t*)LutRGBA + 2 + debB;
737 for(i=0; i < lengthB; ++i)
739 *a16 = ((uint16_t*)LutBlueData)[i];
743 a16 = (uint16_t*)LutRGBA + 3 ;
744 for(i=0; i < 65536; ++i)
746 *a16 = 1; // Alpha component
749 /* Just to 'see' the LUT, at debug time
750 // Don't remove this commented out code.
752 a16=(uint16_t*)LutRGBA;
753 for (int j=0;j<65536;j++)
755 std::cout << *a16 << " " << *(a16+1) << " "
756 << *(a16+2) << " " << *(a16+3) << std::endl;
764 * \brief Swap the bytes, according to \ref SwapCode.
766 void PixelReadConvert::ConvertSwapZone()
769 uint16_t localSwapCode = SwapCode;
771 // If this file is 'ImplicitVR BigEndian PrivateGE Transfer Syntax',
772 // then the header is in little endian format and the pixel data is in
773 // big endian format. When reading the header, GDCM has already established
774 // a byte swapping code suitable for this machine to read the
775 // header. In TS::ImplicitVRBigEndianPrivateGE, this code will need
776 // to be switched in order to read the pixel data. This must be
777 // done REGARDLESS of the processor endianess!
779 // Example: Assume we are on a little endian machine. When
780 // GDCM reads the header, the header will match the machine
781 // endianess and the swap code will be established as a no-op.
782 // When GDCM reaches the pixel data, it will need to switch the
783 // swap code to do big endian to little endian conversion.
785 // Now, assume we are on a big endian machine. When GDCM reads the
786 // header, the header will be recognized as a different endianess
787 // than the machine endianess, and a swap code will be established
788 // to convert from little endian to big endian. When GDCM readers
789 // the pixel data, the pixel data endianess will now match the
790 // machine endianess. But we currently have a swap code that
791 // converts from little endian to big endian. In this case, we
792 // need to switch the swap code to a no-op.
794 // Therefore, in either case, if the file is in
795 // 'ImplicitVR BigEndian PrivateGE Transfer Syntax', then GDCM needs to switch
796 // the byte swapping code when entering the pixel data.
798 if ( IsPrivateGETransferSyntax )
800 // PrivateGETransferSyntax only exists for 'true' Dicom images
801 // we assume there is no 'exotic' 32 bits endianess!
802 switch (localSwapCode)
805 localSwapCode = 4321;
808 localSwapCode = 1234;
812 if ( BitsAllocated == 16 )
814 uint16_t *im16 = (uint16_t*)Raw;
815 switch( localSwapCode )
822 for( i = 0; i < RawSize / 2; i++ )
824 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
828 gdcmWarningMacro("SwapCode value (16 bits) not allowed.");
831 else if ( BitsAllocated == 32 )
836 uint32_t *im32 = (uint32_t*)Raw;
837 switch ( localSwapCode )
842 for( i = 0; i < RawSize / 4; i++ )
844 low = im32[i] & 0x0000ffff; // 4321
845 high = im32[i] >> 16;
846 high = ( high >> 8 ) | ( high << 8 );
847 low = ( low >> 8 ) | ( low << 8 );
849 im32[i] = ( s32 << 16 ) | high;
853 for( i = 0; i < RawSize / 4; i++ )
855 low = im32[i] & 0x0000ffff; // 2143
856 high = im32[i] >> 16;
857 high = ( high >> 8 ) | ( high << 8 );
858 low = ( low >> 8 ) | ( low << 8 );
860 im32[i] = ( s32 << 16 ) | low;
864 for( i = 0; i < RawSize / 4; i++ )
866 low = im32[i] & 0x0000ffff; // 3412
867 high = im32[i] >> 16;
869 im32[i] = ( s32 << 16 ) | high;
873 gdcmWarningMacro("SwapCode value (32 bits) not allowed." );
879 * \brief Deal with endianness i.e. re-arange bytes inside the integer
881 void PixelReadConvert::ConvertReorderEndianity()
883 if ( BitsAllocated != 8 )
888 // Special kludge in order to deal with xmedcon broken images:
889 if ( BitsAllocated == 16
890 && BitsStored < BitsAllocated
893 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
894 uint16_t *deb = (uint16_t *)Raw;
895 for(int i = 0; i<l; i++)
897 if ( *deb == 0xffff )
907 * \brief Deal with Grey levels i.e. re-arange them
908 * to have low values = dark, high values = bright
910 void PixelReadConvert::ConvertFixGreyLevels()
915 uint32_t i; // to please M$VC6
920 if ( BitsAllocated == 8 )
922 uint8_t *deb = (uint8_t *)Raw;
923 for (i=0; i<RawSize; i++)
931 if ( BitsAllocated == 16 )
934 for (j=0; j<BitsStored-1; j++)
936 mask = (mask << 1) +1; // will be fff when BitsStored=12
939 uint16_t *deb = (uint16_t *)Raw;
940 for (i=0; i<RawSize/2; i++)
950 if ( BitsAllocated == 8 )
952 uint8_t smask8 = 255;
953 uint8_t *deb = (uint8_t *)Raw;
954 for (i=0; i<RawSize; i++)
956 *deb = smask8 - *deb;
961 if ( BitsAllocated == 16 )
963 uint16_t smask16 = 65535;
964 uint16_t *deb = (uint16_t *)Raw;
965 for (i=0; i<RawSize/2; i++)
967 *deb = smask16 - *deb;
976 * \brief Re-arrange the bits within the bytes.
977 * @return Boolean always true
979 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
982 if ( BitsStored != BitsAllocated )
984 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
985 if ( BitsAllocated == 16 )
987 // pmask : to mask the 'unused bits' (may contain overlays)
988 uint16_t pmask = 0xffff;
989 pmask = pmask >> ( BitsAllocated - BitsStored );
991 uint16_t *deb = (uint16_t*)Raw;
993 if ( !PixelSign ) // Pixels are unsigned
995 for(int i = 0; i<l; i++)
997 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
1001 else // Pixels are signed
1003 // smask : to check the 'sign' when BitsStored != BitsAllocated
1004 uint16_t smask = 0x0001;
1005 smask = smask << ( 16 - (BitsAllocated - BitsStored + 1) );
1006 // nmask : to propagate sign bit on negative values
1007 int16_t nmask = (int16_t)0x8000;
1008 nmask = nmask >> ( BitsAllocated - BitsStored - 1 );
1010 for(int i = 0; i<l; i++)
1012 *deb = *deb >> (BitsStored - HighBitPosition - 1);
1015 *deb = *deb | nmask;
1019 *deb = *deb & pmask;
1025 else if ( BitsAllocated == 32 )
1027 // pmask : to mask the 'unused bits' (may contain overlays)
1028 uint32_t pmask = 0xffffffff;
1029 pmask = pmask >> ( BitsAllocated - BitsStored );
1031 uint32_t *deb = (uint32_t*)Raw;
1035 for(int i = 0; i<l; i++)
1037 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
1043 // smask : to check the 'sign' when BitsStored != BitsAllocated
1044 uint32_t smask = 0x00000001;
1045 smask = smask >> ( 32 - (BitsAllocated - BitsStored +1 ));
1046 // nmask : to propagate sign bit on negative values
1047 int32_t nmask = 0x80000000;
1048 nmask = nmask >> ( BitsAllocated - BitsStored -1 );
1050 for(int i = 0; i<l; i++)
1052 *deb = *deb >> (BitsStored - HighBitPosition - 1);
1054 *deb = *deb | nmask;
1056 *deb = *deb & pmask;
1063 gdcmWarningMacro("Weird image (BitsAllocated !=8, 12, 16, 32)");
1064 throw FormatError( "Weird image !?" );
1071 * \brief Convert (Red plane, Green plane, Blue plane) to RGB pixels
1072 * \warning Works on all the frames at a time
1074 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
1076 gdcmWarningMacro("--> ConvertRGBPlanesToRGBPixels");
1078 uint8_t *localRaw = Raw;
1079 uint8_t *copyRaw = new uint8_t[ RawSize ];
1080 memmove( copyRaw, localRaw, RawSize );
1082 int l = XSize * YSize * ZSize;
1084 uint8_t *a = copyRaw;
1085 uint8_t *b = copyRaw + l;
1086 uint8_t *c = copyRaw + l + l;
1088 for (int j = 0; j < l; j++)
1090 *(localRaw++) = *(a++);
1091 *(localRaw++) = *(b++);
1092 *(localRaw++) = *(c++);
1098 * \brief Convert (cY plane, cB plane, cR plane) to RGB pixels
1099 * \warning Works on all the frames at a time
1101 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
1103 // Remarks for YBR newbees :
1104 // YBR_FULL works very much like RGB, i.e. three samples per pixel,
1105 // just the color space is YCbCr instead of RGB. This is particularly useful
1106 // for doppler ultrasound where most of the image is grayscale
1107 // (i.e. only populates the Y components) and Cb and Cr are mostly zero,
1108 // except for the few patches of color on the image.
1109 // On such images, RLE achieves a compression ratio that is much better
1110 // than the compression ratio on an equivalent RGB image.
1112 gdcmWarningMacro("--> ConvertYcBcRPlanesToRGBPixels");
1114 uint8_t *localRaw = Raw;
1115 uint8_t *copyRaw = new uint8_t[ RawSize ];
1116 memmove( copyRaw, localRaw, RawSize );
1118 // to see the tricks about YBR_FULL, YBR_FULL_422,
1119 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
1120 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
1121 // and be *very* affraid
1123 int l = XSize * YSize;
1124 int nbFrames = ZSize;
1126 uint8_t *a = copyRaw + 0;
1127 uint8_t *b = copyRaw + l;
1128 uint8_t *c = copyRaw + l+ l;
1131 /// We replaced easy to understand but time consuming floating point
1132 /// computations by the 'well known' integer computation counterpart
1134 /// http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
1135 /// for code optimisation.
1137 for ( int i = 0; i < nbFrames; i++ )
1139 for ( int j = 0; j < l; j++ )
1141 R = 38142 *(*a-16) + 52298 *(*c -128);
1142 G = 38142 *(*a-16) - 26640 *(*c -128) - 12845 *(*b -128);
1143 B = 38142 *(*a-16) + 66093 *(*b -128);
1152 if (R > 255) R = 255;
1153 if (G > 255) G = 255;
1154 if (B > 255) B = 255;
1156 *(localRaw++) = (uint8_t)R;
1157 *(localRaw++) = (uint8_t)G;
1158 *(localRaw++) = (uint8_t)B;
1167 /// \brief Deals with the color decoding i.e. handle:
1168 /// - R, G, B planes (as opposed to RGB pixels)
1169 /// - YBR (various) encodings.
1170 /// - LUT[s] (or "PALETTE COLOR").
1172 void PixelReadConvert::ConvertHandleColor()
1174 //////////////////////////////////
1175 // Deal with the color decoding i.e. handle:
1176 // - R, G, B planes (as opposed to RGB pixels)
1177 // - YBR (various) encodings.
1178 // - LUT[s] (or "PALETTE COLOR").
1180 // The classification in the color decoding schema is based on the blending
1181 // of two Dicom tags values:
1182 // * "Photometric Interpretation" for which we have the cases:
1183 // - [Photo A] MONOCHROME[1|2] pictures,
1184 // - [Photo B] RGB or YBR_FULL_422 (which acts as RGB),
1185 // - [Photo C] YBR_* (with the above exception of YBR_FULL_422)
1186 // - [Photo D] "PALETTE COLOR" which indicates the presence of LUT[s].
1187 // * "Planar Configuration" for which we have the cases:
1188 // - [Planar 0] 0 then Pixels are already RGB
1189 // - [Planar 1] 1 then we have 3 planes : R, G, B,
1190 // - [Planar 2] 2 then we have 1 gray Plane and 3 LUTs
1192 // Now in theory, one could expect some coherence when blending the above
1193 // cases. For example we should not encounter files belonging at the
1194 // time to case [Planar 0] and case [Photo D].
1195 // Alas, this was only theory ! Because in practice some odd (read ill
1196 // formated Dicom) files (e.g. gdcmData/US-PAL-8-10x-echo.dcm) we encounter:
1197 // - "Planar Configuration" = 0,
1198 // - "Photometric Interpretation" = "PALETTE COLOR".
1199 // Hence gdcm will use the folowing "heuristic" in order to be tolerant
1200 // towards Dicom-non-conformant files:
1201 // << whatever the "Planar Configuration" value might be, a
1202 // "Photometric Interpretation" set to "PALETTE COLOR" forces
1203 // a LUT intervention >>
1205 // Now we are left with the following handling of the cases:
1206 // - [Planar 0] OR [Photo A] no color decoding (since respectively
1207 // Pixels are already RGB and monochrome pictures have no color :),
1208 // - [Planar 1] AND [Photo B] handled with ConvertRGBPlanesToRGBPixels()
1209 // - [Planar 1] AND [Photo C] handled with ConvertYcBcRPlanesToRGBPixels()
1210 // - [Planar 2] OR [Photo D] requires LUT intervention.
1212 gdcmWarningMacro("--> ConvertHandleColor"
1213 << "Planar Configuration " << PlanarConfiguration );
1217 // [Planar 2] OR [Photo D]: LUT intervention done outside
1218 gdcmWarningMacro("--> RawRGB : LUT intervention done outside");
1222 if ( PlanarConfiguration == 1 )
1226 // [Planar 1] AND [Photo C] (remember YBR_FULL_422 acts as RGB)
1227 gdcmWarningMacro("--> YBRFull");
1228 ConvertYcBcRPlanesToRGBPixels();
1232 // [Planar 1] AND [Photo C]
1233 gdcmWarningMacro("--> YBRFull");
1234 ConvertRGBPlanesToRGBPixels();
1239 // When planarConf is 0, and RLELossless (forbidden by Dicom norm)
1240 // pixels need to be RGB-fyied anyway
1244 gdcmWarningMacro("--> RLE Lossless");
1245 ConvertRGBPlanesToRGBPixels();
1248 // In *normal *case, when planarConf is 0, pixels are already in RGB
1251 /// Computes the Pixels Size
1252 void PixelReadConvert::ComputeRawAndRGBSizes()
1254 int bitsAllocated = BitsAllocated;
1255 // Number of "Bits Allocated" is fixed to 16 when it's 12, since
1256 // in this case we will expand the image to 16 bits (see
1257 // \ref ReadAndDecompress12BitsTo16Bits() )
1258 if ( BitsAllocated == 12 )
1263 RawSize = XSize * YSize * ZSize
1264 * ( bitsAllocated / 8 )
1268 RGBSize = 3 * RawSize; // works for 8 and 16 bits per Pixel
1276 /// Allocates room for RGB Pixels
1277 void PixelReadConvert::AllocateRGB()
1281 RGB = new uint8_t[RGBSize];
1284 /// Allocates room for RAW Pixels
1285 void PixelReadConvert::AllocateRaw()
1289 Raw = new uint8_t[RawSize];
1292 //-----------------------------------------------------------------------------
1295 * \brief Print self.
1296 * @param indent Indentation string to be prepended during printing.
1297 * @param os Stream to print to.
1299 void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
1302 << "--- Pixel information -------------------------"
1305 << "Pixel Data: offset " << PixelOffset
1306 << " x(" << std::hex << PixelOffset << std::dec
1307 << ") length " << PixelDataLength
1308 << " x(" << std::hex << PixelDataLength << std::dec
1309 << ")" << std::endl;
1311 if ( IsRLELossless )
1315 RLEInfo->Print( os, indent );
1319 gdcmWarningMacro("Set as RLE file but NO RLEinfo present.");
1323 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
1327 JPEGInfo->Print( os, indent );
1331 gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present.");
1336 //-----------------------------------------------------------------------------
1337 } // end namespace gdcm
1339 // Note to developpers :
1340 // Here is a very detailled post from David Clunie, on the troubles caused
1341 // 'non standard' LUT and LUT description
1342 // We shall have to take it into accound in our code.
1347 Subject: Problem with VOI LUTs in Agfa and Fuji CR and GE DX images, was Re: VOI LUT issues
1348 Date: Sun, 06 Feb 2005 17:13:40 GMT
1349 From: David Clunie <dclunie@dclunie.com>
1350 Reply-To: dclunie@dclunie.com
1351 Newsgroups: comp.protocols.dicom
1352 References: <1107553502.040221.189550@o13g2000cwo.googlegroups.com>
1354 > THE LUT that comes with [my] image claims to be 16-bit, but none of the
1355 > values goes higher than 4095. That being said, though, none of my
1356 > original pixel values goes higher than that, either. I have read
1357 > elsewhere on this group that when that happens you are supposed to
1358 > adjust the LUT. Can someone be more specific? There was a thread from
1359 > 2002 where Marco and David were mentioning doing precisely that.
1366 You have encountered the well known "we know what the standard says but
1367 we are going to ignore it and do what we have been doing for almost
1368 a decade regardless" CR vendor bug. Agfa started this, but they are not
1369 the only vendor doing this now; GE and Fuji may have joined the club.
1371 Sadly, one needs to look at the LUT Data, figure out what the maximum
1372 value actually encoded is, and find the next highest power of 2 (e.g.
1373 212 in this case), to figure out what the range of the data is
1374 supposed to be. I have assumed that if the maximum value in the LUT
1375 data is less than a power of 2 minus 1 (e.g. 0xebc) then the intent
1376 of the vendor was not to use the maximum available grayscale range
1377 of the display (e.g. the maximum is 0xfff in this case). An alternative
1378 would be to scale to the actual maximum rather than a power of two.
1380 Very irritating, and in theory not totally reliable if one really
1381 intended the full 16 bits and only used, say 15, but that is extremely
1382 unlikely since everything would be too dark, and this heuristic
1385 There has never been anything in the standard that describes having
1386 to go through these convolutions. Since the only value in the
1387 standard that describes the bit depth of the LUT values is LUT
1388 Descriptor value 3 and that is (usually) always required to be
1389 either 8 or 16, it mystifies me how the creators' of these images
1390 imagine that the receiver is going to divine the range that is intended. Further, the standard is quite explicit that this 3rd
1391 value defines the range of LUT values, but as far as I am aware, all
1392 the vendors are ignoring the standard and indeed sending a third value
1395 This problem is not confined to CR, and is also seen with DX products.
1397 Typically I have seen:
1399 - Agfa CR, which usually (always ?) sends LUTs, values up to 0x0fff
1400 - Fuji CR, which occasionally send LUTs, values up to 0x03ff
1401 - GE DX, for presentation, which always have LUTs, up to 0x3fff
1403 Swissray, Siemens, Philips, Canon and Kodak never seem to send VOI LUTs
1404 at this point (which is a whole other problem). Note that the presence
1405 or absence of a VOI LUT as opposed to window values may be configurable
1406 on the modality in some cases, and I have just looked at what I happen
1407 to have received from a myriad of sites over whose configuration I have
1408 no control. This may be why the majority of Fuji images have no VOI LUTs,
1409 but a few do (or it may be the Siemens system that these Fuji images went
1410 through that perhaps added it). I do have some test Hologic DX images that
1411 are not from a clinical site that do actually get this right (a value
1412 of 12 for the third value and a max of 0xfff).
1414 Since almost every vendor that I have encountered that encodes LUTs
1415 makes this mistake, perhaps it is time to amend the standard to warn
1416 implementor's of receivers and/or sanction this bad behavior. We have
1417 talked about this in the past in WG 6 but so far everyone has been
1418 reluctant to write into the standard such a comment. Maybe it is time
1419 to try again, since if one is not aware of this problem, one cannot
1420 effectively implement display using VOI LUTs, and there is a vast
1421 installed base to contend with.
1423 I did not check presentation states, in which VOI LUTs could also be
1424 encountered, for the prevalence of this mistake, nor did I look at the
1425 encoding of Modality LUT's, which are unusual. Nor did I check digital
1426 mammography images. I would be interested to hear from anyone who has.
1430 PS. The following older thread in this newsgroup discusses this:
1432 "http://groups-beta.google.com/group/comp.protocols.dicom/browse_frm/t hread/6a033444802a35fc/0f0a9a1e35c1468e?q=voi+lut&_done=%2Fgroup%2Fcom p.protocols.dicom%2Fsearch%3Fgroup%3Dcomp.protocols.dicom%26q%3Dvoi+lu t%26qt_g%3D1%26searchnow%3DSearch+this+group%26&_doneTitle=Back+to+Sea rch&&d#0f0a9a1e35c1468e"
1434 PPS. From a historical perspective, the following may be of interest.
1436 In the original standard in 1993, all that was said about this was a
1437 reference to the corresponding such where Modality LUTs are described
1440 "The third value specifies the number of bits for each entry in the
1441 LUT Data. It shall take the value 8 or 16. The LUT Data shall be stored
1442 in a format equivalent to 8 or 16 bits allocated and high bit equal
1445 Since the high bit hint was not apparently explicit enough, a very
1446 early CP, CP 15 (submitted by Agfa as it happens), replaced this with:
1448 "The third value conveys the range of LUT entry values. It shall take
1449 the value 8 or 16, corresponding with the LUT entry value range of
1452 Note: The third value is not required for describing the
1453 LUT data and is only included for informational usage
1454 and for maintaining compatibility with ACRNEMA 2.0.
1456 The LUT Data contains the LUT entry values."
1458 That is how it read in the 1996, 1998 and 1999 editions.
1460 By the 2000 edition, Supplement 33 that introduced presentation states
1461 extensively reworked this entire section and tried to explain this in
1464 "The output range is from 0 to 2^n-1 where n is the third value of LUT
1465 Descriptor. This range is always unsigned."
1467 and also added a note to spell out what the output range meant in the
1470 "9. The output of the Window Center/Width or VOI LUT transformation
1471 is either implicitly scaled to the full range of the display device
1472 if there is no succeeding transformation defined, or implicitly scaled
1473 to the full input range of the succeeding transformation step (such as
1474 the Presentation LUT), if present. See C.11.6.1."
1476 It still reads this way in the 2004 edition.
1478 Note that LUTs in other applications than the general VOI LUT allow for
1479 values other than 8 or 16 in the third value of LUT descriptor to permit
1480 ranges other than 0 to 255 or 65535.
1482 In addition, the DX Image Module specializes the VOI LUT
1483 attributes as follows, in PS 3.3 section C.8.11.3.1.5 (added in Sup 32):
1485 "The third value specifies the number of bits for each entry in the LUT
1486 Data (analogous to ìbits storedî). It shall be between 10-16. The LUT
1487 Data shall be stored in a format equivalent to 16 ìbits allocatedî and
1488 ìhigh bitî equal to ìbits storedî - 1. The third value conveys the range
1489 of LUT entry values. These unsigned LUT entry values shall range between
1490 0 and 2^n-1, where n is the third value of the LUT Descriptor."
1492 So in the case of the GE DX for presentation images, the third value of
1493 LUT descriptor is allowed to be and probably should be 14 rather than 16.