X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmPixelReadConvert.cxx;h=66d41820237be7a95129a27023d397562e14ffb4;hb=f2892379e89dd5236abb60ea3c84c3fe9d5ef45f;hp=424514ed153305253a807297cde5445f564ae18b;hpb=8e10ae94ea66aca5adf40e66521fba9c736628c4;p=gdcm.git diff --git a/src/gdcmPixelReadConvert.cxx b/src/gdcmPixelReadConvert.cxx index 424514ed..66d41820 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/02/03 17:12:46 $ - Version: $Revision: 1.47 $ + Date: $Date: 2005/05/30 00:24:10 $ + Version: $Revision: 1.62 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -30,11 +30,15 @@ namespace gdcm { + +//bool ReadMPEGFile (std::ifstream *fp, void *image_buffer, size_t lenght); +bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* raw, size_t inputlength); //----------------------------------------------------------------------------- #define str2num(str, typeNum) *((typeNum *)(str)) //----------------------------------------------------------------------------- // Constructor / Destructor +/// Constructor PixelReadConvert::PixelReadConvert() { RGB = 0; @@ -47,6 +51,7 @@ PixelReadConvert::PixelReadConvert() LutBlueData = 0; } +/// Canonical Destructor PixelReadConvert::~PixelReadConvert() { Squeeze(); @@ -113,6 +118,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) || 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); @@ -121,13 +127,15 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) PixelOffset = file->GetPixelOffset(); PixelDataLength = file->GetPixelAreaLength(); - RLEInfo = file->GetRLEInfo(); - JPEGInfo = file->GetJPEGInfo(); + RLEInfo = file->GetRLEInfo(); + JPEGInfo = file->GetJPEGInfo(); + + IsMonochrome = file->IsMonochrome(); + IsMonochrome1 = file->IsMonochrome1(); + IsPaletteColor = file->IsPaletteColor(); + IsYBRFull = file->IsYBRFull(); PlanarConfiguration = file->GetPlanarConfiguration(); - IsMonochrome = file->IsMonochrome(); - IsPaletteColor = file->IsPaletteColor(); - IsYBRFull = file->IsYBRFull(); ///////////////////////////////////////////////////////////////// // LUT section: @@ -158,7 +166,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 ); if ( ! LutRedData ) { - gdcmVerboseMacro( "Unable to read Red LUT data" ); + gdcmWarningMacro( "Unable to read Red LUT data" ); } // //// Green round: @@ -166,7 +174,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 ); if ( ! LutGreenData) { - gdcmVerboseMacro( "Unable to read Green LUT data" ); + gdcmWarningMacro( "Unable to read Green LUT data" ); } // //// Blue round: @@ -174,13 +182,14 @@ void PixelReadConvert::GrabInformationsFromFile( File *file ) LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 ); if ( ! LutBlueData ) { - gdcmVerboseMacro( "Unable to read Blue LUT data" ); + gdcmWarningMacro( "Unable to read Blue LUT data" ); } } ComputeRawAndRGBSizes(); } +/// \brief Reads from disk and decompresses Pixels bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) { // ComputeRawAndRGBSizes is already made by @@ -192,14 +201,14 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) //// First stage: get our hands on the Pixel Data. if ( !fp ) { - gdcmVerboseMacro( "Unavailable file pointer." ); + gdcmWarningMacro( "Unavailable file pointer." ); return false; } fp->seekg( PixelOffset, std::ios::beg ); if( fp->fail() || fp->eof()) { - gdcmVerboseMacro( "Unable to find PixelOffset in file." ); + gdcmWarningMacro( "Unable to find PixelOffset in file." ); return false; } @@ -219,7 +228,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) // variable). But RawSize is the right size of the image ! if( PixelDataLength != RawSize) { - gdcmVerboseMacro( "Mismatch between PixelReadConvert : " + gdcmWarningMacro( "Mismatch between PixelReadConvert : " << PixelDataLength << " and RawSize : " << RawSize ); } if( PixelDataLength > RawSize) @@ -233,7 +242,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) if ( fp->fail() || fp->eof()) { - gdcmVerboseMacro( "Reading of Raw pixel data failed." ); + gdcmWarningMacro( "Reading of Raw pixel data failed." ); return false; } } @@ -241,16 +250,23 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) { if ( ! RLEInfo->DecompressRLEFile( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) ) { - gdcmVerboseMacro( "RLE decompressor failed." ); + gdcmWarningMacro( "RLE decompressor failed." ); return false; } } + else if ( IsMPEG ) + { + //gdcmWarningMacro( "Sorry, MPEG not yet taken into account" ); + //return false; +// ReadMPEGFile(fp, Raw, PixelDataLength); // fp has already been seek to start of mpeg + return true; + } else { // Default case concerns JPEG family if ( ! ReadAndDecompressJPEGFile( fp ) ) { - gdcmVerboseMacro( "JPEG decompressor failed." ); + gdcmWarningMacro( "JPEG decompressor failed." ); return false; } } @@ -259,11 +275,13 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp ) //// Third stage: twigle the bytes and bits. ConvertReorderEndianity(); ConvertReArrangeBits(); + ConvertFixGreyLevels(); ConvertHandleColor(); return true; } +/// Deletes Pixels Area void PixelReadConvert::Squeeze() { if ( RGB ) @@ -379,27 +397,41 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp ) { if ( IsJPEG2000 ) { - gdcmVerboseMacro( "Sorry, JPEG2000 not yet taken into account" ); + // I don't think we'll ever be able to deal with multiple fragments + assert( JPEGInfo->GetFragmentCount() == 1 ); fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg); -// if ( ! gdcm_read_JPEG2000_file( fp,Raw ) ) - return false; + if ( ! gdcm_read_JPEG2000_file( fp,Raw, + JPEGInfo->GetFirstFragment()->GetLength() ) ) + { + return true; + } } if ( IsJPEGLS ) { - gdcmVerboseMacro( "Sorry, JPEG-LS not yet taken into account" ); + // WARNING : JPEG-LS is NOT the 'classical' Jpeg Lossless : + // [JPEG-LS is the basis for new lossless/near-lossless compression + // standard for continuous-tone images intended for JPEG2000. The standard + // is based on the LOCO-I algorithm (LOw COmplexity LOssless COmpression + // for Images) developed at Hewlett-Packard Laboratories] + // + // see http://datacompression.info/JPEGLS.shtml + // + + gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" ); fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg); // if ( ! gdcm_read_JPEGLS_file( fp,Raw ) ) return false; } - - // else ?? - // Precompute the offset localRaw will be shifted with - int length = XSize * YSize * SamplesPerPixel; - int numberBytes = BitsAllocated / 8; - - JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length ); - return true; + else + { + // Precompute the offset localRaw will be shifted with + int length = XSize * YSize * SamplesPerPixel; + int numberBytes = BitsAllocated / 8; + + JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length ); + return true; + } } /** @@ -448,7 +480,7 @@ void PixelReadConvert::BuildLUTRGBA() &lengthR, &debR, &nbitsR ); if( nbRead != 3 ) { - gdcmVerboseMacro( "Wrong Red LUT descriptor" ); + gdcmWarningMacro( "Wrong Red LUT descriptor" ); } int lengthG; // Green LUT length in Bytes @@ -459,7 +491,7 @@ void PixelReadConvert::BuildLUTRGBA() &lengthG, &debG, &nbitsG ); if( nbRead != 3 ) { - gdcmVerboseMacro( "Wrong Green LUT descriptor" ); + gdcmWarningMacro( "Wrong Green LUT descriptor" ); } int lengthB; // Blue LUT length in Bytes @@ -470,7 +502,7 @@ void PixelReadConvert::BuildLUTRGBA() &lengthB, &debB, &nbitsB ); if( nbRead != 3 ) { - gdcmVerboseMacro( "Wrong Blue LUT descriptor" ); + gdcmWarningMacro( "Wrong Blue LUT descriptor" ); } //////////////////////////////////////////////////////// @@ -559,7 +591,7 @@ void PixelReadConvert::ConvertSwapZone() } break; default: - gdcmVerboseMacro("SwapCode value (16 bits) not allowed."); + gdcmWarningMacro("SwapCode value (16 bits) not allowed."); } } else if( BitsAllocated == 32 ) @@ -604,7 +636,7 @@ void PixelReadConvert::ConvertSwapZone() } break; default: - gdcmVerboseMacro("SwapCode value (32 bits) not allowed." ); + gdcmWarningMacro("SwapCode value (32 bits) not allowed." ); } } } @@ -637,9 +669,78 @@ void PixelReadConvert::ConvertReorderEndianity() } } +/** + * \brief Deal with Grey levels i.e. re-arange them + * to have low values = dark, high values = bright + */ +void PixelReadConvert::ConvertFixGreyLevels() +{ + if (!IsMonochrome1) + return; + + uint32_t i; // to please M$VC6 + int16_t j; + + if (!PixelSign) + { + if ( BitsAllocated == 8 ) + { + uint8_t *deb = (uint8_t *)Raw; + for (i=0; i>15; + G = (G+16384)>>15; + B = (B+16384)>>15; - if (R < 0.0) R = 0.0; - if (G < 0.0) G = 0.0; - if (B < 0.0) B = 0.0; - if (R > 255.0) R = 255.0; - if (G > 255.0) G = 255.0; - if (B > 255.0) B = 255.0; + if (R < 0) R = 0; + if (G < 0) G = 0; + if (B < 0) B = 0; + if (R > 255) R = 255; + if (G > 255) G = 255; + if (B > 255) B = 255; *(localRaw++) = (uint8_t)R; *(localRaw++) = (uint8_t)G; @@ -756,6 +861,11 @@ void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels() delete[] copyRaw; } +/// \brief Deals with the color decoding i.e. handle: +/// - R, G, B planes (as opposed to RGB pixels) +/// - YBR (various) encodings. +/// - LUT[s] (or "PALETTE COLOR"). + void PixelReadConvert::ConvertHandleColor() { ////////////////////////////////// @@ -826,6 +936,7 @@ void PixelReadConvert::ConvertHandleColor() // In *normal *case, when planarConf is 0, pixels are already in RGB } +/// Computes the Pixels Size void PixelReadConvert::ComputeRawAndRGBSizes() { int bitsAllocated = BitsAllocated; @@ -850,6 +961,7 @@ void PixelReadConvert::ComputeRawAndRGBSizes() } } +/// Allocates room for RGB Pixels void PixelReadConvert::AllocateRGB() { if ( RGB ) @@ -857,6 +969,7 @@ void PixelReadConvert::AllocateRGB() RGB = new uint8_t[RGBSize]; } +/// Allocates room for RAW Pixels void PixelReadConvert::AllocateRaw() { if ( Raw ) @@ -891,7 +1004,7 @@ void PixelReadConvert::Print( std::ostream &os, std::string const &indent ) } else { - gdcmVerboseMacro("Set as RLE file but NO RLEinfo present."); + gdcmWarningMacro("Set as RLE file but NO RLEinfo present."); } } @@ -903,7 +1016,7 @@ void PixelReadConvert::Print( std::ostream &os, std::string const &indent ) } else { - gdcmVerboseMacro("Set as JPEG file but NO JPEGinfo present."); + gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present."); } } }