1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelReadConvert.cxx,v $
6 Date: $Date: 2006/01/26 09:16:41 $
7 Version: $Revision: 1.108 $
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, char *inputdata, 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,
86 FileHelper *fileHelper )
88 // Number of Bits Allocated for storing a Pixel is defaulted to 16
89 // when absent from the file.
90 BitsAllocated = file->GetBitsAllocated();
91 if ( BitsAllocated == 0 )
96 // Number of "Bits Stored", defaulted to number of "Bits Allocated"
97 // when absent from the file.
98 BitsStored = file->GetBitsStored();
99 if ( BitsStored == 0 )
101 BitsStored = BitsAllocated;
104 // High Bit Position, defaulted to "Bits Allocated" - 1
105 HighBitPosition = file->GetHighBitPosition();
106 if ( HighBitPosition == 0 )
108 HighBitPosition = BitsAllocated - 1;
111 XSize = file->GetXSize();
112 YSize = file->GetYSize();
113 ZSize = file->GetZSize();
114 SamplesPerPixel = file->GetSamplesPerPixel();
115 //PixelSize = file->GetPixelSize(); Useless
116 PixelSign = file->IsSignedPixelData();
117 SwapCode = file->GetSwapCode();
119 IsPrivateGETransferSyntax = IsMPEG
120 = IsJPEG2000 = IsJPEGLS = IsJPEGLossy
121 = IsJPEGLossless = IsRLELossless
124 if (! file->IsDicomV3() ) // Should be ACR-NEMA file
130 std::string ts = file->GetTransferSyntax();
133 while (true) // shorter to write than 'if elseif elseif elseif' ...
135 // mind the order : check the most usual first.
136 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian) break;
137 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian ) break;
138 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian) break;
139 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE) break;
140 // DeflatedExplicitVRLittleEndian syntax means the whole Dataset (Header + Pixels) is compressed !
141 // Not dealt with ! (Parser hangs)
142 //if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian) break;
145 // cache whether this is a strange GE transfer syntax (which uses
146 // a little endian transfer syntax for the header and a big endian
147 // transfer syntax for the pixel data).
148 IsPrivateGETransferSyntax =
149 ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE );
151 IsMPEG = IsJPEG2000 = IsJPEGLS = IsJPEGLossy = IsJPEGLossless = IsRLELossless = false;
156 // mind the order : check the most usual first.
157 if( IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts) ) break;
158 if( IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts) ) break;
159 if( IsRLELossless = Global::GetTS()->IsRLELossless(ts) ) break;
160 if( IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts) ) break;
161 if( IsMPEG = Global::GetTS()->IsMPEG(ts) ) break;
162 if( IsJPEGLS = Global::GetTS()->IsJPEGLS(ts) ) break;
163 // DeflatedExplicitVRLittleEndian is considered as 'Unexpected' (we don't know yet haow to process !)
164 gdcmWarningMacro("Unexpected Transfer Syntax :[" << ts << "]");
170 PixelOffset = file->GetPixelOffset();
171 PixelDataLength = file->GetPixelAreaLength();
172 RLEInfo = file->GetRLEInfo();
173 JPEGInfo = file->GetJPEGInfo();
175 IsMonochrome = file->IsMonochrome();
176 IsMonochrome1 = file->IsMonochrome1();
177 IsPaletteColor = file->IsPaletteColor();
178 IsYBRFull = file->IsYBRFull();
180 PlanarConfiguration = file->GetPlanarConfiguration();
182 /////////////////////////////////////////////////////////////////
184 HasLUT = file->HasLUT();
187 // Just in case some access to a File element requires disk access.
188 LutRedDescriptor = file->GetEntryString( 0x0028, 0x1101 );
189 LutGreenDescriptor = file->GetEntryString( 0x0028, 0x1102 );
190 LutBlueDescriptor = file->GetEntryString( 0x0028, 0x1103 );
192 // FIXME : The following comment is probabely meaningless, since LUT are *always*
193 // loaded at parsing time, whatever their length is.
195 // Depending on the value of Document::MAX_SIZE_LOAD_ELEMENT_VALUE
196 // [ refer to invocation of Document::SetMaxSizeLoadEntry() in
197 // Document::Document() ], the loading of the value (content) of a
198 // [Bin|Val]Entry occurence migth have been hindered (read simply NOT
199 // loaded). Hence, we first try to obtain the LUTs data from the file
200 // and when this fails we read the LUTs data directly from disk.
201 // \TODO Reading a [Bin|Val]Entry directly from disk is a kludge.
202 // We should NOT bypass the [Bin|Val]Entry class. Instead
203 // an access to an UNLOADED content of a [Bin|Val]Entry occurence
204 // (e.g. DataEntry::GetBinArea()) should force disk access from
205 // within the [Bin|Val]Entry class itself. The only problem
206 // is that the [Bin|Val]Entry is unaware of the FILE* is was
207 // parsed from. Fix that. FIXME.
210 file->LoadEntryBinArea(0x0028, 0x1201);
211 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
214 gdcmWarningMacro("Unable to read Red Palette Color Lookup Table data");
218 file->LoadEntryBinArea(0x0028, 0x1202);
219 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
222 gdcmWarningMacro("Unable to read Green Palette Color Lookup Table data");
226 file->LoadEntryBinArea(0x0028, 0x1203);
227 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
230 gdcmWarningMacro("Unable to read Blue Palette Color Lookup Table data");
235 ComputeRawAndRGBSizes();
238 /// \brief Reads from disk and decompresses Pixels
239 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
241 // ComputeRawAndRGBSizes is already made by
242 // ::GrabInformationsFromfile. So, the structure sizes are
246 //////////////////////////////////////////////////
247 //// First stage: get our hands on the Pixel Data.
250 gdcmWarningMacro( "Unavailable file pointer." );
254 fp->seekg( PixelOffset, std::ios::beg );
255 if ( fp->fail() || fp->eof() )
257 gdcmWarningMacro( "Unable to find PixelOffset in file." );
263 //////////////////////////////////////////////////
265 CallStartMethod(); // for progress bar
266 unsigned int count = 0;
267 unsigned int frameSize;
268 unsigned int bitsAllocated = BitsAllocated;
269 if(bitsAllocated == 12)
271 frameSize = XSize*YSize*SamplesPerPixel*bitsAllocated/8;
273 //// Second stage: read from disk and decompress.
275 if ( BitsAllocated == 12 ) // We suppose 'BitsAllocated' = 12 only exist for uncompressed files
277 ReadAndDecompress12BitsTo16Bits( fp);
281 // This problem can be found when some obvious informations are found
282 // after the field containing the image data. In this case, these
283 // bad data are added to the size of the image (in the PixelDataLength
284 // variable). But RawSize is the right size of the image !
285 if ( PixelDataLength != RawSize )
287 gdcmWarningMacro( "Mismatch between PixelReadConvert : "
288 << PixelDataLength << " and RawSize : " << RawSize );
291 //todo : is it the right patch?
292 char *raw = (char*)Raw;
293 uint32_t remainingLength;
295 unsigned int lengthToRead;
297 if ( PixelDataLength > RawSize )
298 lengthToRead = RawSize;
300 lengthToRead = PixelDataLength;
302 // perform a frame by frame reading
303 remainingLength = lengthToRead;
304 unsigned int nbFrames = lengthToRead / frameSize;
305 for (i=0;i<nbFrames; i++)
307 Progress = (float)(count+1)/(float)nbFrames;
308 fp->read( raw, frameSize);
310 remainingLength -= frameSize;
313 if (remainingLength !=0 )
314 fp->read( raw, remainingLength);
316 //if ( PixelDataLength > RawSize )
318 // fp->read( (char*)Raw, RawSize); // Read all the frames with a single fread
322 // fp->read( (char*)Raw, PixelDataLength); // Read all the frames with a single fread
325 if ( fp->fail() || fp->eof())
327 gdcmWarningMacro( "Reading of Raw pixel data failed." );
331 else if ( IsRLELossless )
333 if ( ! RLEInfo->DecompressRLEFile
334 ( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) )
336 gdcmWarningMacro( "RLE decompressor failed." );
342 //gdcmWarningMacro( "Sorry, MPEG not yet taken into account" );
344 // fp has already been seek to start of mpeg
345 //ReadMPEGFile(fp, (char*)Raw, PixelDataLength);
350 // Default case concerns JPEG family
351 if ( ! ReadAndDecompressJPEGFile( fp ) )
353 gdcmWarningMacro( "JPEG decompressor ( ReadAndDecompressJPEGFile()"
354 << " method ) failed." );
359 ////////////////////////////////////////////
360 //// Third stage: twigle the bytes and bits.
361 ConvertReorderEndianity();
362 ConvertReArrangeBits();
363 ConvertFixGreyLevels();
364 if (UserFunction) // user is allowed to Mirror, TopDown, Rotate,...the image
365 UserFunction( Raw, FileInternal);
366 ConvertHandleColor();
371 /// Deletes Pixels Area
372 void PixelReadConvert::Squeeze()
388 * \brief Build the RGB image from the Raw image and the LUTs.
390 bool PixelReadConvert::BuildRGBImage()
394 // The job is already done.
400 // The job can't be done
407 // The job can't be done
411 gdcmDebugMacro( "--> BuildRGBImage" );
417 if ( BitsAllocated <= 8 )
419 uint8_t *localRGB = RGB;
420 for (size_t i = 0; i < RawSize; ++i )
423 *localRGB++ = LutRGBA[j];
424 *localRGB++ = LutRGBA[j+1];
425 *localRGB++ = LutRGBA[j+2];
429 else // deal with 16 bits pixels and 16 bits Palette color
431 uint16_t *localRGB = (uint16_t *)RGB;
432 for (size_t i = 0; i < RawSize/2; ++i )
434 j = ((uint16_t *)Raw)[i] * 4;
435 *localRGB++ = ((uint16_t *)LutRGBA)[j];
436 *localRGB++ = ((uint16_t *)LutRGBA)[j+1];
437 *localRGB++ = ((uint16_t *)LutRGBA)[j+2];
444 //-----------------------------------------------------------------------------
447 //-----------------------------------------------------------------------------
450 * \brief Read from file a 12 bits per pixel image and decompress it
451 * into a 16 bits per pixel image.
453 void PixelReadConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream *fp )
454 throw ( FormatError )
456 int nbPixels = XSize * YSize;
457 uint16_t *localDecompres = (uint16_t*)Raw;
459 for( int p = 0; p < nbPixels; p += 2 )
463 fp->read( (char*)&b0, 1);
464 if ( fp->fail() || fp->eof() )
466 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
467 "Unfound first block" );
470 fp->read( (char*)&b1, 1 );
471 if ( fp->fail() || fp->eof())
473 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
474 "Unfound second block" );
477 fp->read( (char*)&b2, 1 );
478 if ( fp->fail() || fp->eof())
480 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
481 "Unfound second block" );
484 // Two steps are necessary to please VC++
486 // 2 pixels 12bit = [0xABCDEF]
487 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
489 *localDecompres++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
491 *localDecompres++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
493 /// \todo JPR Troubles expected on Big-Endian processors ?
498 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
499 * file and decompress it.
500 * @param fp File Pointer
503 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
507 // make sure this is the right JPEG compression
508 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEGLS );
509 // FIXME this is really ugly but it seems I have to load the complete
510 // jpeg2000 stream to use jasper:
511 // I don't think we'll ever be able to deal with multiple fragments properly
513 unsigned long inputlength = 0;
514 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
517 inputlength += jpegfrag->GetLength();
518 jpegfrag = JPEGInfo->GetNextFragment();
520 gdcmAssertMacro( inputlength != 0);
521 uint8_t *inputdata = new uint8_t[inputlength];
522 char *pinputdata = (char*)inputdata;
523 jpegfrag = JPEGInfo->GetFirstFragment();
526 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
527 fp->read(pinputdata, jpegfrag->GetLength());
528 pinputdata += jpegfrag->GetLength();
529 jpegfrag = JPEGInfo->GetNextFragment();
531 // Warning the inputdata buffer is delete in the function
532 if ( ! gdcm_read_JPEG2000_file( Raw,
533 (char*)inputdata, inputlength ) )
537 // wow what happen, must be an error
538 gdcmWarningMacro( "gdcm_read_JPEG2000_file() failed ");
543 // make sure this is the right JPEG compression
544 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEG2000 );
545 // WARNING : JPEG-LS is NOT the 'classical' Jpeg Lossless :
546 // [JPEG-LS is the basis for new lossless/near-lossless compression
547 // standard for continuous-tone images intended for JPEG2000. The standard
548 // is based on the LOCO-I algorithm (LOw COmplexity LOssless COmpression
549 // for Images) developed at Hewlett-Packard Laboratories]
551 // see http://datacompression.info/JPEGLS.shtml
554 std::cerr << "count:" << JPEGInfo->GetFragmentCount() << std::endl;
555 unsigned long inputlength = 0;
556 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
559 inputlength += jpegfrag->GetLength();
560 jpegfrag = JPEGInfo->GetNextFragment();
562 gdcmAssertMacro( inputlength != 0);
563 uint8_t *inputdata = new uint8_t[inputlength];
564 char *pinputdata = (char*)inputdata;
565 jpegfrag = JPEGInfo->GetFirstFragment();
568 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
569 fp->read(pinputdata, jpegfrag->GetLength());
570 pinputdata += jpegfrag->GetLength();
571 jpegfrag = JPEGInfo->GetNextFragment();
574 //fp->read((char*)Raw, PixelDataLength);
576 std::ofstream out("/tmp/jpegls.jpg");
577 out.write((char*)inputdata, inputlength);
582 gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
583 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
584 // if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
589 // make sure this is the right JPEG compression
590 assert( !IsJPEGLS || !IsJPEG2000 );
591 // Precompute the offset localRaw will be shifted with
592 int length = XSize * YSize * SamplesPerPixel;
593 int numberBytes = BitsAllocated / 8;
595 JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length );
601 * \brief Build Red/Green/Blue/Alpha LUT from File when :
602 * - (0028,0004) : Photometric Interpretation == [PALETTE COLOR ]
604 * - (0028,1101),(0028,1102),(0028,1102)
605 * xxx Palette Color Lookup Table Descriptor are found
607 * - (0028,1201),(0028,1202),(0028,1202)
608 * xxx Palette Color Lookup Table Data - are found
609 * \warning does NOT deal with :
610 * - 0028 1100 Gray Lookup Table Descriptor (Retired)
611 * - 0028 1221 Segmented Red Palette Color Lookup Table Data
612 * - 0028 1222 Segmented Green Palette Color Lookup Table Data
613 * - 0028 1223 Segmented Blue Palette Color Lookup Table Data
614 * no known Dicom reader deals with them :-(
615 * @return a RGBA Lookup Table
617 void PixelReadConvert::BuildLUTRGBA()
620 // Note to code reviewers :
621 // The problem is *much more* complicated, since a lot of manufacturers
622 // Don't follow the norm :
623 // have a look at David Clunie's remark at the end of this .cxx file.
630 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
632 if ( ! IsPaletteColor )
637 if ( LutRedDescriptor == GDCM_UNFOUND
638 || LutGreenDescriptor == GDCM_UNFOUND
639 || LutBlueDescriptor == GDCM_UNFOUND )
641 gdcmWarningMacro( "(At least) a LUT Descriptor is missing" );
645 ////////////////////////////////////////////
646 // Extract the info from the LUT descriptors
647 int lengthR; // Red LUT length in Bytes
648 int debR; // Subscript of the first Lut Value
649 int nbitsR; // Lut item size (in Bits)
650 int nbRead; // nb of items in LUT descriptor (must be = 3)
652 nbRead = sscanf( LutRedDescriptor.c_str(),
654 &lengthR, &debR, &nbitsR );
657 gdcmWarningMacro( "Wrong Red LUT descriptor" );
659 int lengthG; // Green LUT length in Bytes
660 int debG; // Subscript of the first Lut Value
661 int nbitsG; // Lut item size (in Bits)
663 nbRead = sscanf( LutGreenDescriptor.c_str(),
665 &lengthG, &debG, &nbitsG );
668 gdcmWarningMacro( "Wrong Green LUT descriptor" );
671 int lengthB; // Blue LUT length in Bytes
672 int debB; // Subscript of the first Lut Value
673 int nbitsB; // Lut item size (in Bits)
674 nbRead = sscanf( LutRedDescriptor.c_str(),
676 &lengthB, &debB, &nbitsB );
679 gdcmWarningMacro( "Wrong Blue LUT descriptor" );
682 gdcmDebugMacro(" lengthR " << lengthR << " debR "
683 << debR << " nbitsR " << nbitsR);
684 gdcmDebugMacro(" lengthG " << lengthG << " debG "
685 << debG << " nbitsG " << nbitsG);
686 gdcmDebugMacro(" lengthB " << lengthB << " debB "
687 << debB << " nbitsB " << nbitsB);
689 if ( !lengthR ) // if = 2^16, this shall be 0 see : CP-143
691 if ( !lengthG ) // if = 2^16, this shall be 0
693 if ( !lengthB ) // if = 2^16, this shall be 0
696 ////////////////////////////////////////////////////////
698 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
700 gdcmWarningMacro( "(At least) a LUT is missing" );
704 // -------------------------------------------------------------
706 if ( BitsAllocated <= 8 )
708 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
709 LutRGBA = new uint8_t[ 1024 ]; // 256 * 4 (R, G, B, Alpha)
714 memset( LutRGBA, 0, 1024 );
717 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
719 // when LUT item size is different than pixel size
720 mult = 2; // high byte must be = low byte
724 // See PS 3.3-2003 C.11.1.1.2 p 619
728 // if we get a black image, let's just remove the '+1'
729 // from 'i*mult+1' and check again
730 // if it works, we shall have to check the 3 Palettes
731 // to see which byte is ==0 (first one, or second one)
733 // We give up the checking to avoid some (useless ?) overhead
734 // (optimistic asumption)
738 //take "Subscript of the first Lut Value" (debR,debG,debB) into account!
740 //FIXME : +1 : to get 'low value' byte
741 // Trouble expected on Big Endian Processors ?
742 // 16 BIts Per Pixel Palette Color to be swapped?
744 a = LutRGBA + 0 + debR;
745 for( i=0; i < lengthR; ++i )
747 *a = LutRedData[i*mult+1];
751 a = LutRGBA + 1 + debG;
752 for( i=0; i < lengthG; ++i)
754 *a = LutGreenData[i*mult+1];
758 a = LutRGBA + 2 + debB;
759 for(i=0; i < lengthB; ++i)
761 *a = LutBlueData[i*mult+1];
766 for(i=0; i < 256; ++i)
768 *a = 1; // Alpha component
774 // Probabely the same stuff is to be done for 16 Bits Pixels
775 // with 65536 entries LUT ?!?
776 // Still looking for accurate info on the web :-(
778 gdcmWarningMacro( "Sorry Palette Color Lookup Tables not yet dealt with"
779 << " for 16 Bits Per Pixel images" );
781 // forge the 4 * 16 Bits Red/Green/Blue/Alpha LUT
783 LutRGBA = (uint8_t *)new uint16_t[ 65536*4 ]; // 2^16 * 4 (R, G, B, Alpha)
786 memset( LutRGBA, 0, 65536*4*2 ); // 16 bits = 2 bytes ;-)
788 LutItemNumber = 65536;
794 //take "Subscript of the first Lut Value" (debR,debG,debB) into account!
796 a16 = (uint16_t*)LutRGBA + 0 + debR;
797 for( i=0; i < lengthR; ++i )
799 *a16 = ((uint16_t*)LutRedData)[i];
803 a16 = (uint16_t*)LutRGBA + 1 + debG;
804 for( i=0; i < lengthG; ++i)
806 *a16 = ((uint16_t*)LutGreenData)[i];
810 a16 = (uint16_t*)LutRGBA + 2 + debB;
811 for(i=0; i < lengthB; ++i)
813 *a16 = ((uint16_t*)LutBlueData)[i];
817 a16 = (uint16_t*)LutRGBA + 3 ;
818 for(i=0; i < 65536; ++i)
820 *a16 = 1; // Alpha component
823 /* Just to 'see' the LUT, at debug time
824 // Don't remove this commented out code.
826 a16=(uint16_t*)LutRGBA;
827 for (int j=0;j<65536;j++)
829 std::cout << *a16 << " " << *(a16+1) << " "
830 << *(a16+2) << " " << *(a16+3) << std::endl;
838 * \brief Swap the bytes, according to \ref SwapCode.
840 void PixelReadConvert::ConvertSwapZone()
844 // If this file is 'ImplicitVR BigEndian PrivateGE Transfer Syntax',
845 // then the header is in little endian format and the pixel data is in
846 // big endian format. When reading the header, GDCM has already established
847 // a byte swapping code suitable for this machine to read the
848 // header. In TS::ImplicitVRBigEndianPrivateGE, this code will need
849 // to be switched in order to read the pixel data. This must be
850 // done REGARDLESS of the processor endianess!
852 // Example: Assume we are on a little endian machine. When
853 // GDCM reads the header, the header will match the machine
854 // endianess and the swap code will be established as a no-op.
855 // When GDCM reaches the pixel data, it will need to switch the
856 // swap code to do big endian to little endian conversion.
858 // Now, assume we are on a big endian machine. When GDCM reads the
859 // header, the header will be recognized as a different endianess
860 // than the machine endianess, and a swap code will be established
861 // to convert from little endian to big endian. When GDCM readers
862 // the pixel data, the pixel data endianess will now match the
863 // machine endianess. But we currently have a swap code that
864 // converts from little endian to big endian. In this case, we
865 // need to switch the swap code to a no-op.
867 // Therefore, in either case, if the file is in
868 // 'ImplicitVR BigEndian PrivateGE Transfer Syntax', then GDCM needs to switch
869 // the byte swapping code when entering the pixel data.
871 int tempSwapCode = SwapCode;
872 if ( IsPrivateGETransferSyntax )
874 gdcmWarningMacro(" IsPrivateGETransferSyntax found; turn the SwapCode");
875 // PrivateGETransferSyntax only exists for 'true' Dicom images
876 // we assume there is no 'exotic' 32 bits endianess!
877 if (SwapCode == 1234)
881 else if (SwapCode == 4321)
887 if ( BitsAllocated == 16 )
889 uint16_t *im16 = (uint16_t*)Raw;
890 switch( tempSwapCode )
897 for( i = 0; i < RawSize / 2; i++ )
899 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
903 gdcmWarningMacro("SwapCode value (16 bits) not allowed."
907 else if ( BitsAllocated == 32 )
912 uint32_t *im32 = (uint32_t*)Raw;
913 switch ( tempSwapCode )
918 for( i = 0; i < RawSize / 4; i++ )
920 low = im32[i] & 0x0000ffff; // 4321
921 high = im32[i] >> 16;
922 high = ( high >> 8 ) | ( high << 8 );
923 low = ( low >> 8 ) | ( low << 8 );
925 im32[i] = ( s32 << 16 ) | high;
929 for( i = 0; i < RawSize / 4; i++ )
931 low = im32[i] & 0x0000ffff; // 2143
932 high = im32[i] >> 16;
933 high = ( high >> 8 ) | ( high << 8 );
934 low = ( low >> 8 ) | ( low << 8 );
936 im32[i] = ( s32 << 16 ) | low;
940 for( i = 0; i < RawSize / 4; i++ )
942 low = im32[i] & 0x0000ffff; // 3412
943 high = im32[i] >> 16;
945 im32[i] = ( s32 << 16 ) | high;
949 gdcmWarningMacro("SwapCode value (32 bits) not allowed." << tempSwapCode );
955 * \brief Deal with endianness i.e. re-arange bytes inside the integer
957 void PixelReadConvert::ConvertReorderEndianity()
959 if ( BitsAllocated != 8 )
964 // Special kludge in order to deal with xmedcon broken images:
965 if ( BitsAllocated == 16
966 && BitsStored < BitsAllocated
969 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
970 uint16_t *deb = (uint16_t *)Raw;
971 for(int i = 0; i<l; i++)
973 if ( *deb == 0xffff )
983 * \brief Deal with Grey levels i.e. re-arange them
984 * to have low values = dark, high values = bright
986 void PixelReadConvert::ConvertFixGreyLevels()
991 uint32_t i; // to please M$VC6
996 if ( BitsAllocated == 8 )
998 uint8_t *deb = (uint8_t *)Raw;
999 for (i=0; i<RawSize; i++)
1007 if ( BitsAllocated == 16 )
1010 for (j=0; j<BitsStored-1; j++)
1012 mask = (mask << 1) +1; // will be fff when BitsStored=12
1015 uint16_t *deb = (uint16_t *)Raw;
1016 for (i=0; i<RawSize/2; i++)
1026 if ( BitsAllocated == 8 )
1028 uint8_t smask8 = 255;
1029 uint8_t *deb = (uint8_t *)Raw;
1030 for (i=0; i<RawSize; i++)
1032 *deb = smask8 - *deb;
1037 if ( BitsAllocated == 16 )
1039 uint16_t smask16 = 65535;
1040 uint16_t *deb = (uint16_t *)Raw;
1041 for (i=0; i<RawSize/2; i++)
1043 *deb = smask16 - *deb;
1052 * \brief Re-arrange the bits within the bytes.
1053 * @return Boolean always true
1055 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
1058 if ( BitsStored != BitsAllocated )
1060 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
1061 if ( BitsAllocated == 16 )
1063 // pmask : to mask the 'unused bits' (may contain overlays)
1064 uint16_t pmask = 0xffff;
1065 pmask = pmask >> ( BitsAllocated - BitsStored );
1067 uint16_t *deb = (uint16_t*)Raw;
1069 if ( !PixelSign ) // Pixels are unsigned
1071 for(int i = 0; i<l; i++)
1073 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
1077 else // Pixels are signed
1079 // smask : to check the 'sign' when BitsStored != BitsAllocated
1080 uint16_t smask = 0x0001;
1081 smask = smask << ( 16 - (BitsAllocated - BitsStored + 1) );
1082 // nmask : to propagate sign bit on negative values
1083 int16_t nmask = (int16_t)0x8000;
1084 nmask = nmask >> ( BitsAllocated - BitsStored - 1 );
1086 for(int i = 0; i<l; i++)
1088 *deb = *deb >> (BitsStored - HighBitPosition - 1);
1091 *deb = *deb | nmask;
1095 *deb = *deb & pmask;
1101 else if ( BitsAllocated == 32 )
1103 // pmask : to mask the 'unused bits' (may contain overlays)
1104 uint32_t pmask = 0xffffffff;
1105 pmask = pmask >> ( BitsAllocated - BitsStored );
1107 uint32_t *deb = (uint32_t*)Raw;
1111 for(int i = 0; i<l; i++)
1113 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
1119 // smask : to check the 'sign' when BitsStored != BitsAllocated
1120 uint32_t smask = 0x00000001;
1121 smask = smask >> ( 32 - (BitsAllocated - BitsStored +1 ));
1122 // nmask : to propagate sign bit on negative values
1123 int32_t nmask = 0x80000000;
1124 nmask = nmask >> ( BitsAllocated - BitsStored -1 );
1126 for(int i = 0; i<l; i++)
1128 *deb = *deb >> (BitsStored - HighBitPosition - 1);
1130 *deb = *deb | nmask;
1132 *deb = *deb & pmask;
1139 gdcmWarningMacro("Weird image (BitsAllocated !=8, 12, 16, 32)");
1140 throw FormatError( "Weird image !?" );
1147 * \brief Convert (Red plane, Green plane, Blue plane) to RGB pixels
1148 * \warning Works on all the frames at a time
1150 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
1152 gdcmWarningMacro("--> ConvertRGBPlanesToRGBPixels");
1154 uint8_t *localRaw = Raw;
1155 uint8_t *copyRaw = new uint8_t[ RawSize ];
1156 memmove( copyRaw, localRaw, RawSize );
1158 int l = XSize * YSize * ZSize;
1160 uint8_t *a = copyRaw;
1161 uint8_t *b = copyRaw + l;
1162 uint8_t *c = copyRaw + l + l;
1164 for (int j = 0; j < l; j++)
1166 *(localRaw++) = *(a++);
1167 *(localRaw++) = *(b++);
1168 *(localRaw++) = *(c++);
1174 * \brief Convert (cY plane, cB plane, cR plane) to RGB pixels
1175 * \warning Works on all the frames at a time
1177 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
1179 // Remarks for YBR newbees :
1180 // YBR_FULL works very much like RGB, i.e. three samples per pixel,
1181 // just the color space is YCbCr instead of RGB. This is particularly useful
1182 // for doppler ultrasound where most of the image is grayscale
1183 // (i.e. only populates the Y components) and Cb and Cr are mostly zero,
1184 // except for the few patches of color on the image.
1185 // On such images, RLE achieves a compression ratio that is much better
1186 // than the compression ratio on an equivalent RGB image.
1188 gdcmWarningMacro("--> ConvertYcBcRPlanesToRGBPixels");
1190 uint8_t *localRaw = Raw;
1191 uint8_t *copyRaw = new uint8_t[ RawSize ];
1192 memmove( copyRaw, localRaw, RawSize );
1194 // to see the tricks about YBR_FULL, YBR_FULL_422,
1195 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
1196 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
1197 // and be *very* affraid
1199 int l = XSize * YSize;
1200 int nbFrames = ZSize;
1202 uint8_t *a = copyRaw + 0;
1203 uint8_t *b = copyRaw + l;
1204 uint8_t *c = copyRaw + l+ l;
1207 /// We replaced easy to understand but time consuming floating point
1208 /// computations by the 'well known' integer computation counterpart
1210 /// http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
1211 /// for code optimisation.
1213 for ( int i = 0; i < nbFrames; i++ )
1215 for ( int j = 0; j < l; j++ )
1217 R = 38142 *(*a-16) + 52298 *(*c -128);
1218 G = 38142 *(*a-16) - 26640 *(*c -128) - 12845 *(*b -128);
1219 B = 38142 *(*a-16) + 66093 *(*b -128);
1228 if (R > 255) R = 255;
1229 if (G > 255) G = 255;
1230 if (B > 255) B = 255;
1232 *(localRaw++) = (uint8_t)R;
1233 *(localRaw++) = (uint8_t)G;
1234 *(localRaw++) = (uint8_t)B;
1243 /// \brief Deals with the color decoding i.e. handle:
1244 /// - R, G, B planes (as opposed to RGB pixels)
1245 /// - YBR (various) encodings.
1246 /// - LUT[s] (or "PALETTE COLOR").
1248 void PixelReadConvert::ConvertHandleColor()
1250 //////////////////////////////////
1251 // Deal with the color decoding i.e. handle:
1252 // - R, G, B planes (as opposed to RGB pixels)
1253 // - YBR (various) encodings.
1254 // - LUT[s] (or "PALETTE COLOR").
1256 // The classification in the color decoding schema is based on the blending
1257 // of two Dicom tags values:
1258 // * "Photometric Interpretation" for which we have the cases:
1259 // - [Photo A] MONOCHROME[1|2] pictures,
1260 // - [Photo B] RGB or YBR_FULL_422 (which acts as RGB),
1261 // - [Photo C] YBR_* (with the above exception of YBR_FULL_422)
1262 // - [Photo D] "PALETTE COLOR" which indicates the presence of LUT[s].
1263 // * "Planar Configuration" for which we have the cases:
1264 // - [Planar 0] 0 then Pixels are already RGB
1265 // - [Planar 1] 1 then we have 3 planes : R, G, B,
1266 // - [Planar 2] 2 then we have 1 gray Plane and 3 LUTs
1268 // Now in theory, one could expect some coherence when blending the above
1269 // cases. For example we should not encounter files belonging at the
1270 // time to case [Planar 0] and case [Photo D].
1271 // Alas, this was only theory ! Because in practice some odd (read ill
1272 // formated Dicom) files (e.g. gdcmData/US-PAL-8-10x-echo.dcm) we encounter:
1273 // - "Planar Configuration" = 0,
1274 // - "Photometric Interpretation" = "PALETTE COLOR".
1275 // Hence gdcm will use the folowing "heuristic" in order to be tolerant
1276 // towards Dicom-non-conformant files:
1277 // << whatever the "Planar Configuration" value might be, a
1278 // "Photometric Interpretation" set to "PALETTE COLOR" forces
1279 // a LUT intervention >>
1281 // Now we are left with the following handling of the cases:
1282 // - [Planar 0] OR [Photo A] no color decoding (since respectively
1283 // Pixels are already RGB and monochrome pictures have no color :),
1284 // - [Planar 1] AND [Photo B] handled with ConvertRGBPlanesToRGBPixels()
1285 // - [Planar 1] AND [Photo C] handled with ConvertYcBcRPlanesToRGBPixels()
1286 // - [Planar 2] OR [Photo D] requires LUT intervention.
1288 gdcmDebugMacro("--> ConvertHandleColor "
1289 << "Planar Configuration " << PlanarConfiguration );
1293 // [Planar 2] OR [Photo D]: LUT intervention done outside
1294 gdcmDebugMacro("--> RawRGB : LUT intervention done outside");
1298 if ( PlanarConfiguration == 1 )
1302 // [Planar 1] AND [Photo C] (remember YBR_FULL_422 acts as RGB)
1303 gdcmDebugMacro("--> YBRFull");
1304 ConvertYcBcRPlanesToRGBPixels();
1308 // [Planar 1] AND [Photo C]
1309 gdcmDebugMacro("--> YBRFull");
1310 ConvertRGBPlanesToRGBPixels();
1315 // When planarConf is 0, and RLELossless (forbidden by Dicom norm)
1316 // pixels need to be RGB-fyied anyway
1320 gdcmDebugMacro("--> RLE Lossless");
1321 ConvertRGBPlanesToRGBPixels();
1324 // In *normal *case, when planarConf is 0, pixels are already in RGB
1327 /// Computes the Pixels Size
1328 void PixelReadConvert::ComputeRawAndRGBSizes()
1330 int bitsAllocated = BitsAllocated;
1331 // Number of "Bits Allocated" is fixed to 16 when it's 12, since
1332 // in this case we will expand the image to 16 bits (see
1333 // \ref ReadAndDecompress12BitsTo16Bits() )
1334 if ( BitsAllocated == 12 )
1339 RawSize = XSize * YSize * ZSize
1340 * ( bitsAllocated / 8 )
1344 RGBSize = 3 * RawSize; // works for 8 and 16 bits per Pixel
1352 /// Allocates room for RGB Pixels
1353 void PixelReadConvert::AllocateRGB()
1357 RGB = new uint8_t[RGBSize];
1360 /// Allocates room for RAW Pixels
1361 void PixelReadConvert::AllocateRaw()
1365 Raw = new uint8_t[RawSize];
1368 //-----------------------------------------------------------------------------
1371 * \brief Print self.
1372 * @param indent Indentation string to be prepended during printing.
1373 * @param os Stream to print to.
1375 void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
1378 << "--- Pixel information -------------------------"
1381 << "Pixel Data: offset " << PixelOffset
1382 << " x(" << std::hex << PixelOffset << std::dec
1383 << ") length " << PixelDataLength
1384 << " x(" << std::hex << PixelDataLength << std::dec
1385 << ")" << std::endl;
1387 if ( IsRLELossless )
1391 RLEInfo->Print( os, indent );
1395 gdcmWarningMacro("Set as RLE file but NO RLEinfo present.");
1399 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
1403 JPEGInfo->Print( os, indent );
1407 gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present.");
1413 * \brief CallStartMethod
1415 void PixelReadConvert::CallStartMethod()
1419 CommandManager::ExecuteCommand(FH,CMD_STARTPROGRESS);
1423 * \brief CallProgressMethod
1425 void PixelReadConvert::CallProgressMethod()
1427 CommandManager::ExecuteCommand(FH,CMD_PROGRESS);
1431 * \brief CallEndMethod
1433 void PixelReadConvert::CallEndMethod()
1436 CommandManager::ExecuteCommand(FH,CMD_ENDPROGRESS);
1440 //-----------------------------------------------------------------------------
1441 } // end namespace gdcm
1443 // Note to developpers :
1444 // Here is a very detailled post from David Clunie, on the troubles caused
1445 // 'non standard' LUT and LUT description
1446 // We shall have to take it into accound in our code.
1451 Subject: Problem with VOI LUTs in Agfa and Fuji CR and GE DX images, was Re: VOI LUT issues
1452 Date: Sun, 06 Feb 2005 17:13:40 GMT
1453 From: David Clunie <dclunie@dclunie.com>
1454 Reply-To: dclunie@dclunie.com
1455 Newsgroups: comp.protocols.dicom
1456 References: <1107553502.040221.189550@o13g2000cwo.googlegroups.com>
1458 > THE LUT that comes with [my] image claims to be 16-bit, but none of the
1459 > values goes higher than 4095. That being said, though, none of my
1460 > original pixel values goes higher than that, either. I have read
1461 > elsewhere on this group that when that happens you are supposed to
1462 > adjust the LUT. Can someone be more specific? There was a thread from
1463 > 2002 where Marco and David were mentioning doing precisely that.
1470 You have encountered the well known "we know what the standard says but
1471 we are going to ignore it and do what we have been doing for almost
1472 a decade regardless" CR vendor bug. Agfa started this, but they are not
1473 the only vendor doing this now; GE and Fuji may have joined the club.
1475 Sadly, one needs to look at the LUT Data, figure out what the maximum
1476 value actually encoded is, and find the next highest power of 2 (e.g.
1477 212 in this case), to figure out what the range of the data is
1478 supposed to be. I have assumed that if the maximum value in the LUT
1479 data is less than a power of 2 minus 1 (e.g. 0xebc) then the intent
1480 of the vendor was not to use the maximum available grayscale range
1481 of the display (e.g. the maximum is 0xfff in this case). An alternative
1482 would be to scale to the actual maximum rather than a power of two.
1484 Very irritating, and in theory not totally reliable if one really
1485 intended the full 16 bits and only used, say 15, but that is extremely
1486 unlikely since everything would be too dark, and this heuristic
1489 There has never been anything in the standard that describes having
1490 to go through these convolutions. Since the only value in the
1491 standard that describes the bit depth of the LUT values is LUT
1492 Descriptor value 3 and that is (usually) always required to be
1493 either 8 or 16, it mystifies me how the creators' of these images
1494 imagine that the receiver is going to divine the range that is intended. Further, the standard is quite explicit that this 3rd
1495 value defines the range of LUT values, but as far as I am aware, all
1496 the vendors are ignoring the standard and indeed sending a third value
1499 This problem is not confined to CR, and is also seen with DX products.
1501 Typically I have seen:
1503 - Agfa CR, which usually (always ?) sends LUTs, values up to 0x0fff
1504 - Fuji CR, which occasionally send LUTs, values up to 0x03ff
1505 - GE DX, for presentation, which always have LUTs, up to 0x3fff
1507 Swissray, Siemens, Philips, Canon and Kodak never seem to send VOI LUTs
1508 at this point (which is a whole other problem). Note that the presence
1509 or absence of a VOI LUT as opposed to window values may be configurable
1510 on the modality in some cases, and I have just looked at what I happen
1511 to have received from a myriad of sites over whose configuration I have
1512 no control. This may be why the majority of Fuji images have no VOI LUTs,
1513 but a few do (or it may be the Siemens system that these Fuji images went
1514 through that perhaps added it). I do have some test Hologic DX images that
1515 are not from a clinical site that do actually get this right (a value
1516 of 12 for the third value and a max of 0xfff).
1518 Since almost every vendor that I have encountered that encodes LUTs
1519 makes this mistake, perhaps it is time to amend the standard to warn
1520 implementor's of receivers and/or sanction this bad behavior. We have
1521 talked about this in the past in WG 6 but so far everyone has been
1522 reluctant to write into the standard such a comment. Maybe it is time
1523 to try again, since if one is not aware of this problem, one cannot
1524 effectively implement display using VOI LUTs, and there is a vast
1525 installed base to contend with.
1527 I did not check presentation states, in which VOI LUTs could also be
1528 encountered, for the prevalence of this mistake, nor did I look at the
1529 encoding of Modality LUT's, which are unusual. Nor did I check digital
1530 mammography images. I would be interested to hear from anyone who has.
1534 PS. The following older thread in this newsgroup discusses this:
1536 "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"
1538 PPS. From a historical perspective, the following may be of interest.
1540 In the original standard in 1993, all that was said about this was a
1541 reference to the corresponding such where Modality LUTs are described
1544 "The third value specifies the number of bits for each entry in the
1545 LUT Data. It shall take the value 8 or 16. The LUT Data shall be stored
1546 in a format equivalent to 8 or 16 bits allocated and high bit equal
1549 Since the high bit hint was not apparently explicit enough, a very
1550 early CP, CP 15 (submitted by Agfa as it happens), replaced this with:
1552 "The third value conveys the range of LUT entry values. It shall take
1553 the value 8 or 16, corresponding with the LUT entry value range of
1556 Note: The third value is not required for describing the
1557 LUT data and is only included for informational usage
1558 and for maintaining compatibility with ACRNEMA 2.0.
1560 The LUT Data contains the LUT entry values."
1562 That is how it read in the 1996, 1998 and 1999 editions.
1564 By the 2000 edition, Supplement 33 that introduced presentation states
1565 extensively reworked this entire section and tried to explain this in
1568 "The output range is from 0 to 2^n-1 where n is the third value of LUT
1569 Descriptor. This range is always unsigned."
1571 and also added a note to spell out what the output range meant in the
1574 "9. The output of the Window Center/Width or VOI LUT transformation
1575 is either implicitly scaled to the full range of the display device
1576 if there is no succeeding transformation defined, or implicitly scaled
1577 to the full input range of the succeeding transformation step (such as
1578 the Presentation LUT), if present. See C.11.6.1."
1580 It still reads this way in the 2004 edition.
1582 Note that LUTs in other applications than the general VOI LUT allow for
1583 values other than 8 or 16 in the third value of LUT descriptor to permit
1584 ranges other than 0 to 255 or 65535.
1586 In addition, the DX Image Module specializes the VOI LUT
1587 attributes as follows, in PS 3.3 section C.8.11.3.1.5 (added in Sup 32):
1589 "The third value specifies the number of bits for each entry in the LUT
1590 Data (analogous to ìbits storedî). It shall be between 10-16. The LUT
1591 Data shall be stored in a format equivalent to 16 ìbits allocatedî and
1592 ìhigh bitî equal to ìbits storedî - 1. The third value conveys the range
1593 of LUT entry values. These unsigned LUT entry values shall range between
1594 0 and 2^n-1, where n is the third value of the LUT Descriptor."
1596 So in the case of the GE DX for presentation images, the third value of
1597 LUT descriptor is allowed to be and probably should be 14 rather than 16.