X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmPixelReadConvert.cxx;h=b25edb1c95de0877e34f3be9183a8b1104f1a0d7;hb=e362e154b84f087750d1ed1f6671a34b3bf5ff7a;hp=05402379a28fcb91a477ae52f2eb5262112abaf0;hpb=bd60287496e2943a7f60b9d21cba8247dbbffb88;p=gdcm.git diff --git a/src/gdcmPixelReadConvert.cxx b/src/gdcmPixelReadConvert.cxx index 05402379..b25edb1c 100644 --- a/src/gdcmPixelReadConvert.cxx +++ b/src/gdcmPixelReadConvert.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmPixelReadConvert.cxx,v $ Language: C++ - Date: $Date: 2005/10/13 07:14:45 $ - Version: $Revision: 1.78 $ + Date: $Date: 2005/11/03 08:37:57 $ + Version: $Revision: 1.96 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -31,7 +31,7 @@ namespace gdcm { -//bool ReadMPEGFile (std::ifstream *fp, void *image_buffer, size_t lenght); +//bool ReadMPEGFile (std::ifstream *fp, void *image_buffer, size_t lenght); bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength); //----------------------------------------------------------------------------- @@ -114,21 +114,53 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) //PixelSize = file->GetPixelSize(); Useless PixelSign = file->IsSignedPixelData(); SwapCode = file->GetSwapCode(); - std::string ts = file->GetTransferSyntax(); - IsRaw = - ( ! file->IsDicomV3() ) - || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian - || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndianDLXGE - || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian - || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian - || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian; - - IsMPEG = Global::GetTS()->IsMPEG(ts); - IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts); - IsJPEGLS = Global::GetTS()->IsJPEGLS(ts); - IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts); - IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts); - IsRLELossless = Global::GetTS()->IsRLELossless(ts); + + if (! file->IsDicomV3() ) // Should be ACR-NEMA file + { + IsRaw = true; + // Don't loose time checking unexistant Transfer Syntax ! + IsPrivateGETransferSyntax = IsMPEG + = IsJPEG2000 = IsJPEGLS = IsJPEGLossy + = IsJPEGLossless = IsRLELossless + = false; + } + else + { + std::string ts = file->GetTransferSyntax(); + + IsRaw = false; + while (true) + { + // mind the order : check the most usual first. + if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian) break; + if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian ) break; + if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian) break; + if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE) break; + if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian) break; + break; + } + + IsPrivateGETransferSyntax = + ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE ); + + IsMPEG = IsJPEG2000 = IsJPEGLS = IsJPEGLossy = IsJPEGLossless = IsRLELossless = false; + + if (!IsRaw) + { + while(true) + { + // mind the order : check the most usual first. + if( IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts) ) break; + if( IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts) ) break; + if( IsRLELossless = Global::GetTS()->IsRLELossless(ts) ) break; + if( IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts) ) break; + if( IsMPEG = Global::GetTS()->IsMPEG(ts) ) break; + if( IsJPEGLS = Global::GetTS()->IsJPEGLS(ts) ) break; + gdcmWarningMacro("Unexepected Transfer Syntax :[" << ts << "]"); + break; + } + } + } PixelOffset = file->GetPixelOffset(); PixelDataLength = file->GetPixelAreaLength(); @@ -148,11 +180,11 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) if ( HasLUT ) { // Just in case some access to a File element requires disk access. - LutRedDescriptor = file->GetEntryValue( 0x0028, 0x1101 ); - LutGreenDescriptor = file->GetEntryValue( 0x0028, 0x1102 ); - LutBlueDescriptor = file->GetEntryValue( 0x0028, 0x1103 ); + LutRedDescriptor = file->GetEntryString( 0x0028, 0x1101 ); + LutGreenDescriptor = file->GetEntryString( 0x0028, 0x1102 ); + LutBlueDescriptor = file->GetEntryString( 0x0028, 0x1103 ); - // The following comment is probabely meaningless, since LUT are *always* + // FIXME : The following comment is probabely meaningless, since LUT are *always* // loaded at parsing time, whatever their length is. // Depending on the value of Document::MAX_SIZE_LOAD_ELEMENT_VALUE @@ -164,7 +196,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) // \TODO Reading a [Bin|Val]Entry directly from disk is a kludge. // We should NOT bypass the [Bin|Val]Entry class. Instead // an access to an UNLOADED content of a [Bin|Val]Entry occurence - // (e.g. BinEntry::GetBinArea()) should force disk access from + // (e.g. DataEntry::GetBinArea()) should force disk access from // within the [Bin|Val]Entry class itself. The only problem // is that the [Bin|Val]Entry is unaware of the FILE* is was // parsed from. Fix that. FIXME. @@ -174,7 +206,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 ); if ( ! LutRedData ) { - gdcmWarningMacro( "Unable to read Red Palette Color Lookup Table data" ); + gdcmWarningMacro("Unable to read Red Palette Color Lookup Table data"); } // //// Green round: @@ -182,7 +214,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 ); if ( ! LutGreenData) { - gdcmWarningMacro( "Unable to read Green Palette Color Lookup Table data" ); + gdcmWarningMacro("Unable to read Green Palette Color Lookup Table data"); } // //// Blue round: @@ -190,7 +222,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 ); if ( ! LutBlueData ) { - gdcmWarningMacro( "Unable to read Blue Palette Color Lookup Table data" ); + gdcmWarningMacro("Unable to read Blue Palette Color Lookup Table data"); } } FileInternal = file; @@ -238,7 +270,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) if ( PixelDataLength != RawSize ) { gdcmWarningMacro( "Mismatch between PixelReadConvert : " - << PixelDataLength << " and RawSize : " << RawSize ); + << PixelDataLength << " and RawSize : " << RawSize ); } if ( PixelDataLength > RawSize ) { @@ -257,7 +289,8 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) } else if ( IsRLELossless ) { - if ( ! RLEInfo->DecompressRLEFile( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) ) + if ( ! RLEInfo->DecompressRLEFile + ( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) ) { gdcmWarningMacro( "RLE decompressor failed." ); return false; @@ -522,17 +555,19 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp ) } /** - * \brief Build Red/Green/Blue/Alpha LUT from File - * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ] - * and (0028,1101),(0028,1102),(0028,1102) - * - xxx Palette Color Lookup Table Descriptor - are found - * and (0028,1201),(0028,1202),(0028,1202) - * - xxx Palette Color Lookup Table Data - are found + * \brief Build Red/Green/Blue/Alpha LUT from File when : + * - (0028,0004) : Photometric Interpretation == [PALETTE COLOR ] + * and + * - (0028,1101),(0028,1102),(0028,1102) + * xxx Palette Color Lookup Table Descriptor are found + * and + * - (0028,1201),(0028,1202),(0028,1202) + * xxx Palette Color Lookup Table Data - are found * \warning does NOT deal with : - * 0028 1100 Gray Lookup Table Descriptor (Retired) - * 0028 1221 Segmented Red Palette Color Lookup Table Data - * 0028 1222 Segmented Green Palette Color Lookup Table Data - * 0028 1223 Segmented Blue Palette Color Lookup Table Data + * - 0028 1100 Gray Lookup Table Descriptor (Retired) + * - 0028 1221 Segmented Red Palette Color Lookup Table Data + * - 0028 1222 Segmented Green Palette Color Lookup Table Data + * - 0028 1223 Segmented Blue Palette Color Lookup Table Data * no known Dicom reader deals with them :-( * @return a RGBA Lookup Table */ @@ -743,6 +778,7 @@ void PixelReadConvert::BuildLUTRGBA() a16 += 4; } /* Just to 'see' the LUT, at debug time +// Don't remove this commented out code. a16=(uint16_t*)LutRGBA; for (int j=0;j<65536;j++) @@ -761,11 +797,54 @@ void PixelReadConvert::BuildLUTRGBA() void PixelReadConvert::ConvertSwapZone() { unsigned int i; + uint16_t localSwapCode = SwapCode; + + // If this file is 'ImplicitVR BigEndian PrivateGE Transfer Syntax', + // then the header is in little endian format and the pixel data is in + // big endian format. When reading the header, GDCM has already established + // a byte swapping code suitable for this machine to read the + // header. In TS::ImplicitVRBigEndianPrivateGE, this code will need + // to be switched in order to read the pixel data. This must be + // done REGARDLESS of the processor endianess! + // + // Example: Assume we are on a little endian machine. When + // GDCM reads the header, the header will match the machine + // endianess and the swap code will be established as a no-op. + // When GDCM reaches the pixel data, it will need to switch the + // swap code to do big endian to little endian conversion. + // + // Now, assume we are on a big endian machine. When GDCM reads the + // header, the header will be recognized as a different endianess + // than the machine endianess, and a swap code will be established + // to convert from little endian to big endian. When GDCM readers + // the pixel data, the pixel data endianess will now match the + // machine endianess. But we currently have a swap code that + // converts from little endian to big endian. In this case, we + // need to switch the swap code to a no-op. + // + // Therefore, in either case, if the file is in + // 'ImplicitVR BigEndian PrivateGE Transfer Syntax', then GDCM needs to switch + // the byte swapping code when entering the pixel data. + if ( IsPrivateGETransferSyntax ) + { + // PrivateGETransferSyntax only exists for 'true' Dicom images + // we assume there is no 'exotic' 32 bits endianess! + switch (localSwapCode) + { + case 1234: + localSwapCode = 4321; + break; + case 4321: + localSwapCode = 1234; + break; + } + } + if ( BitsAllocated == 16 ) { uint16_t *im16 = (uint16_t*)Raw; - switch( SwapCode ) + switch( localSwapCode ) { case 1234: break; @@ -787,7 +866,7 @@ void PixelReadConvert::ConvertSwapZone() uint16_t high; uint16_t low; uint32_t *im32 = (uint32_t*)Raw; - switch ( SwapCode ) + switch ( localSwapCode ) { case 1234: break; @@ -1162,13 +1241,13 @@ void PixelReadConvert::ConvertHandleColor() // - [Planar 1] AND [Photo C] handled with ConvertYcBcRPlanesToRGBPixels() // - [Planar 2] OR [Photo D] requires LUT intervention. - gdcmWarningMacro("--> ConvertHandleColor" + gdcmDebugMacro("--> ConvertHandleColor " << "Planar Configuration " << PlanarConfiguration ); if ( ! IsRawRGB() ) { // [Planar 2] OR [Photo D]: LUT intervention done outside - gdcmWarningMacro("--> RawRGB : LUT intervention done outside"); + gdcmDebugMacro("--> RawRGB : LUT intervention done outside"); return; }