00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmPixelReadConvert.h"
00020 #include "gdcmDebug.h"
00021 #include "gdcmFile.h"
00022 #include "gdcmGlobal.h"
00023 #include "gdcmTS.h"
00024 #include "gdcmDocEntry.h"
00025 #include "gdcmRLEFramesInfo.h"
00026 #include "gdcmJPEGFragmentsInfo.h"
00027
00028 #include <fstream>
00029 #include <stdio.h>
00030
00031 #if defined(__BORLANDC__)
00032 #include <mem.h>
00033 #endif
00034
00035 namespace GDCM_NAME_SPACE
00036 {
00037
00038
00039 bool gdcm_read_JPEG2000_file (void* raw,
00040 char *inputdata, size_t inputlength);
00041
00042 #define str2num(str, typeNum) *((typeNum *)(str))
00043
00044
00045
00047 PixelReadConvert::PixelReadConvert()
00048 {
00049 RGB = 0;
00050 RGBSize = 0;
00051 Raw = 0;
00052 RawSize = 0;
00053 LutRGBA = 0;
00054 LutRedData = 0;
00055 LutGreenData = 0;
00056 LutBlueData = 0;
00057 RLEInfo = 0;
00058 JPEGInfo = 0;
00059 UserFunction = 0;
00060 FileInternal = 0;
00061 }
00062
00064 PixelReadConvert::~PixelReadConvert()
00065 {
00066 Squeeze();
00067 }
00068
00069
00070
00075 bool PixelReadConvert::IsRawRGB()
00076 {
00077 if ( IsMonochrome
00078 || PlanarConfiguration == 2
00079 || IsPaletteColor )
00080 {
00081 return false;
00082 }
00083 return true;
00084 }
00090 void PixelReadConvert::GrabInformationsFromFile( File *file,
00091 FileHelper *fileHelper )
00092 {
00093
00094
00095 BitsAllocated = file->GetBitsAllocated();
00096 if ( BitsAllocated == 0 )
00097 {
00098 BitsAllocated = 16;
00099 }
00100
00101 else if (BitsAllocated > 8 && BitsAllocated < 16 && BitsAllocated != 12)
00102 {
00103 BitsAllocated = 16;
00104 }
00105
00106
00107 BitsStored = file->GetBitsStored();
00108 if ( BitsStored == 0 )
00109 {
00110 BitsStored = BitsAllocated;
00111 }
00112
00113
00114 HighBitPosition = file->GetHighBitPosition();
00115 if ( HighBitPosition == 0 )
00116 {
00117 HighBitPosition = BitsAllocated - 1;
00118 }
00119
00120 XSize = file->GetXSize();
00121 YSize = file->GetYSize();
00122 ZSize = file->GetZSize();
00123 TSize = file->GetTSize();
00124 SamplesPerPixel = file->GetSamplesPerPixel();
00125
00126 PixelSign = file->IsSignedPixelData();
00127 SwapCode = file->GetSwapCode();
00128
00129 IsPrivateGETransferSyntax = IsMPEG
00130 = IsJPEG2000 = IsJPEGLS = IsJPEGLossy
00131 = IsJPEGLossless = IsRLELossless
00132 = false;
00133
00134 if (! file->IsDicomV3() )
00135 {
00136 IsRaw = true;
00137 }
00138 else
00139 {
00140 std::string ts = file->GetTransferSyntax();
00141
00142 IsRaw = false;
00143 while (true)
00144 {
00145
00146 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian) break;
00147 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian ) break;
00148 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian) break;
00149 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE) break;
00150
00151
00152
00153 break;
00154 }
00155
00156
00157
00158 IsPrivateGETransferSyntax =
00159 ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE );
00160
00161 IsMPEG = IsJPEG2000 = IsJPEGLS = IsJPEGLossy = IsJPEGLossless = IsRLELossless = false;
00162 if (!IsRaw)
00163 {
00164 while(true)
00165 {
00166
00167 if( IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts) ) break;
00168 if( IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts) ) break;
00169 if( IsRLELossless = Global::GetTS()->IsRLELossless(ts) ) break;
00170 if( IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts) ) break;
00171 if( IsMPEG = Global::GetTS()->IsMPEG(ts) ) break;
00172 if( IsJPEGLS = Global::GetTS()->IsJPEGLS(ts) ) break;
00173
00174 gdcmWarningMacro("Unexpected Transfer Syntax :[" << ts << "]");
00175 break;
00176 }
00177 }
00178 }
00179
00180 PixelOffset = file->GetPixelOffset();
00181 PixelDataLength = file->GetPixelAreaLength();
00182 RLEInfo = file->GetRLEInfo();
00183 JPEGInfo = file->GetJPEGInfo();
00184
00185 IsMonochrome = file->IsMonochrome();
00186 IsMonochrome1 = file->IsMonochrome1();
00187 IsPaletteColor = file->IsPaletteColor();
00188 IsYBRFull = file->IsYBRFull();
00189
00190 PlanarConfiguration = file->GetPlanarConfiguration();
00191
00193
00194 HasLUT = file->HasLUT();
00195 if ( HasLUT )
00196 {
00197
00198 LutRedDescriptor = file->GetEntryString( 0x0028, 0x1101 );
00199 LutGreenDescriptor = file->GetEntryString( 0x0028, 0x1102 );
00200 LutBlueDescriptor = file->GetEntryString( 0x0028, 0x1103 );
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 file->LoadEntryBinArea(0x0028, 0x1201);
00221 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
00222 if ( ! LutRedData )
00223 {
00224 gdcmWarningMacro("Unable to read Red Palette Color Lookup Table data");
00225 }
00226
00227
00228 file->LoadEntryBinArea(0x0028, 0x1202);
00229 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
00230 if ( ! LutGreenData)
00231 {
00232 gdcmWarningMacro("Unable to read Green Palette Color Lookup Table data");
00233 }
00234
00235
00236 file->LoadEntryBinArea(0x0028, 0x1203);
00237 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
00238 if ( ! LutBlueData )
00239 {
00240 gdcmWarningMacro("Unable to read Blue Palette Color Lookup Table data");
00241 }
00242 }
00243 FileInternal = file;
00244 FH = fileHelper;
00245 ComputeRawAndRGBSizes();
00246 }
00247
00249 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
00250 {
00251
00252
00253
00254 Squeeze();
00255
00258 if ( !fp )
00259 {
00260 gdcmWarningMacro( "Unavailable file pointer." );
00261 return false;
00262 }
00263
00264 fp->seekg( PixelOffset, std::ios::beg );
00265 if ( fp->fail() || fp->eof() )
00266 {
00267 gdcmWarningMacro( "Unable to find PixelOffset in file." );
00268 return false;
00269 }
00270
00271 AllocateRaw();
00272
00274
00275 CallStartMethod();
00276 unsigned int count = 0;
00277 unsigned int frameSize;
00278 unsigned int bitsAllocated = BitsAllocated;
00279
00280 if(bitsAllocated > 8 && bitsAllocated < 16)
00281 bitsAllocated = 16;
00282 frameSize = XSize*YSize*SamplesPerPixel*bitsAllocated/8;
00283
00285
00286 if ( BitsAllocated == 12 )
00287 {
00288 ReadAndDecompress12BitsTo16Bits( fp);
00289 }
00290 else if ( IsRaw )
00291 {
00292
00293
00294
00295
00296 if ( PixelDataLength != RawSize )
00297 {
00298 gdcmWarningMacro( "Mismatch between PixelReadConvert : "
00299 << PixelDataLength << " and RawSize : " << RawSize );
00300 }
00301
00302
00303 char *raw = (char*)Raw;
00304 uint32_t remainingLength;
00305 unsigned int i;
00306 unsigned int lengthToRead;
00307
00308 if ( PixelDataLength > RawSize )
00309 lengthToRead = RawSize;
00310 else
00311 lengthToRead = PixelDataLength;
00312
00313
00314 remainingLength = lengthToRead;
00315 unsigned int nbFrames = lengthToRead / frameSize;
00316 for (i=0;i<nbFrames; i++)
00317 {
00318 Progress = (float)(count+1)/(float)nbFrames;
00319 fp->read( raw, frameSize);
00320 raw += frameSize;
00321 remainingLength -= frameSize;
00322 count++;
00323 }
00324 if (remainingLength !=0 )
00325 fp->read( raw, remainingLength);
00326
00327 if ( fp->fail() || fp->eof())
00328 {
00329 gdcmWarningMacro( "Reading of Raw pixel data failed." );
00330 return false;
00331 }
00332 }
00333 else if ( IsRLELossless )
00334 {
00335 if ( ! RLEInfo->DecompressRLEFile
00336 ( fp, Raw, XSize, YSize, ZSize, TSize, BitsAllocated ) )
00337 {
00338 gdcmWarningMacro( "RLE decompressor failed." );
00339 return false;
00340 }
00341 }
00342 else if ( IsMPEG )
00343 {
00344
00345
00346
00347
00348 return true;
00349 }
00350 else
00351 {
00352
00353 if ( ! ReadAndDecompressJPEGFile( fp ) )
00354 {
00355 gdcmWarningMacro( "JPEG decompressor ( ReadAndDecompressJPEGFile()"
00356 << " method ) failed." );
00357 return false;
00358 }
00359 }
00360
00363 ConvertReorderEndianity();
00364 ConvertReArrangeBits();
00365 ConvertFixGreyLevels();
00366 if (UserFunction)
00367 UserFunction( Raw, FileInternal);
00368 ConvertHandleColor();
00369
00370 return true;
00371 }
00372
00374 void PixelReadConvert::Squeeze()
00375 {
00376 if ( RGB )
00377 delete [] RGB;
00378 RGB = 0;
00379
00380 if ( Raw )
00381 delete [] Raw;
00382 Raw = 0;
00383
00384 if ( LutRGBA )
00385 delete [] LutRGBA;
00386 LutRGBA = 0;
00387 }
00388
00392 bool PixelReadConvert::BuildRGBImage()
00393 {
00394 if ( RGB )
00395 {
00396
00397 return true;
00398 }
00399
00400 if ( ! Raw )
00401 {
00402
00403 return false;
00404 }
00405
00406 BuildLUTRGBA();
00407 if ( ! LutRGBA )
00408 {
00409
00410 return false;
00411 }
00412
00413 gdcmDebugMacro( "--> BuildRGBImage" );
00414
00415
00416 AllocateRGB();
00417
00418 int j;
00419 if ( BitsAllocated <= 8 )
00420 {
00421 uint8_t *localRGB = RGB;
00422 for (size_t i = 0; i < RawSize; ++i )
00423 {
00424 j = Raw[i] * 4;
00425 *localRGB++ = LutRGBA[j];
00426 *localRGB++ = LutRGBA[j+1];
00427 *localRGB++ = LutRGBA[j+2];
00428 }
00429 }
00430
00431 else
00432 {
00433 uint16_t *localRGB = (uint16_t *)RGB;
00434 for (size_t i = 0; i < RawSize/2; ++i )
00435 {
00436 j = ((uint16_t *)Raw)[i] * 4;
00437 *localRGB++ = ((uint16_t *)LutRGBA)[j];
00438 *localRGB++ = ((uint16_t *)LutRGBA)[j+1];
00439 *localRGB++ = ((uint16_t *)LutRGBA)[j+2];
00440 }
00441 }
00442
00443 return true;
00444 }
00445
00446
00447
00448
00449
00450
00455 void PixelReadConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream *fp )
00456 throw ( FormatError )
00457 {
00459 int nbPixels = XSize * YSize * TSize;
00460 uint16_t *localDecompres = (uint16_t*)Raw;
00461
00462 for( int p = 0; p < nbPixels; p += 2 )
00463 {
00464 uint8_t b0, b1, b2;
00465
00466 fp->read( (char*)&b0, 1);
00467 if ( fp->fail() || fp->eof() )
00468 {
00469 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00470 "Unfound first block" );
00471 }
00472
00473 fp->read( (char*)&b1, 1 );
00474 if ( fp->fail() || fp->eof())
00475 {
00476 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00477 "Unfound second block" );
00478 }
00479
00480 fp->read( (char*)&b2, 1 );
00481 if ( fp->fail() || fp->eof())
00482 {
00483 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00484 "Unfound second block" );
00485 }
00486
00487
00488
00489
00490
00491
00492 *localDecompres++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
00493
00494 *localDecompres++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
00495
00497 }
00498 }
00499
00506 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
00507 {
00508 if ( IsJPEG2000 )
00509 {
00510
00511 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEGLS );
00512
00513
00514
00515
00516 unsigned long inputlength = 0;
00517 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
00518 while( jpegfrag )
00519 {
00520 inputlength += jpegfrag->GetLength();
00521 jpegfrag = JPEGInfo->GetNextFragment();
00522 }
00523 gdcmAssertMacro( inputlength != 0);
00524 uint8_t *inputdata = new uint8_t[inputlength];
00525 char *pinputdata = (char*)inputdata;
00526 jpegfrag = JPEGInfo->GetFirstFragment();
00527 while( jpegfrag )
00528 {
00529 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
00530 fp->read(pinputdata, jpegfrag->GetLength());
00531 pinputdata += jpegfrag->GetLength();
00532 jpegfrag = JPEGInfo->GetNextFragment();
00533 }
00534
00535 if ( ! gdcm_read_JPEG2000_file( Raw,
00536 (char*)inputdata, inputlength ) )
00537 {
00538 return true;
00539 }
00540
00541 gdcmWarningMacro( "gdcm_read_JPEG2000_file() failed ");
00542 return false;
00543 }
00544 else if ( IsJPEGLS )
00545 {
00546
00547 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEG2000 );
00548
00549
00550
00551
00552
00553
00554
00555
00556 #if 0
00557 std::cerr << "count:" << JPEGInfo->GetFragmentCount() << std::endl;
00558 unsigned long inputlength = 0;
00559 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
00560 while( jpegfrag )
00561 {
00562 inputlength += jpegfrag->GetLength();
00563 jpegfrag = JPEGInfo->GetNextFragment();
00564 }
00565 gdcmAssertMacro( inputlength != 0);
00566 uint8_t *inputdata = new uint8_t[inputlength];
00567 char *pinputdata = (char*)inputdata;
00568 jpegfrag = JPEGInfo->GetFirstFragment();
00569 while( jpegfrag )
00570 {
00571 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
00572 fp->read(pinputdata, jpegfrag->GetLength());
00573 pinputdata += jpegfrag->GetLength();
00574 jpegfrag = JPEGInfo->GetNextFragment();
00575 }
00576
00577
00578
00579 std::ofstream out("/tmp/jpegls.jpg");
00580 out.write((char*)inputdata, inputlength);
00581 out.close();
00582 delete[] inputdata;
00583 #endif
00584
00585 gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
00586 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
00587
00588 return false;
00589 }
00590 else
00591 {
00592
00593 assert( !IsJPEGLS || !IsJPEG2000 );
00594
00595 int length = XSize * YSize * ZSize * SamplesPerPixel;
00596 int numberBytes = BitsAllocated / 8;
00597
00598 JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length );
00599 return true;
00600 }
00601 }
00602
00620 void PixelReadConvert::BuildLUTRGBA()
00621 {
00622
00623
00624
00625
00626
00627 if ( LutRGBA )
00628
00629 {
00630 return;
00631 }
00632
00633
00634
00635 if ( ! IsPaletteColor )
00636 {
00637 return;
00638 }
00639
00640 if ( LutRedDescriptor == GDCM_UNFOUND
00641 || LutGreenDescriptor == GDCM_UNFOUND
00642 || LutBlueDescriptor == GDCM_UNFOUND )
00643 {
00644 gdcmWarningMacro( "(At least) a LUT Descriptor is missing" );
00645 return;
00646 }
00647
00649
00650 int lengthR;
00651 int debR;
00652 int nbitsR;
00653 int nbRead;
00654
00655 nbRead = sscanf( LutRedDescriptor.c_str(),
00656 "%d\\%d\\%d",
00657 &lengthR, &debR, &nbitsR );
00658 if ( nbRead != 3 )
00659 {
00660 gdcmWarningMacro( "Wrong Red LUT descriptor" );
00661 }
00662 int lengthG;
00663 int debG;
00664 int nbitsG;
00665
00666 nbRead = sscanf( LutGreenDescriptor.c_str(),
00667 "%d\\%d\\%d",
00668 &lengthG, &debG, &nbitsG );
00669 if ( nbRead != 3 )
00670 {
00671 gdcmWarningMacro( "Wrong Green LUT descriptor" );
00672 }
00673
00674 int lengthB;
00675 int debB;
00676 int nbitsB;
00677 nbRead = sscanf( LutRedDescriptor.c_str(),
00678 "%d\\%d\\%d",
00679 &lengthB, &debB, &nbitsB );
00680 if ( nbRead != 3 )
00681 {
00682 gdcmWarningMacro( "Wrong Blue LUT descriptor" );
00683 }
00684
00685 gdcmDebugMacro(" lengthR " << lengthR << " debR "
00686 << debR << " nbitsR " << nbitsR);
00687 gdcmDebugMacro(" lengthG " << lengthG << " debG "
00688 << debG << " nbitsG " << nbitsG);
00689 gdcmDebugMacro(" lengthB " << lengthB << " debB "
00690 << debB << " nbitsB " << nbitsB);
00691
00692 if ( !lengthR )
00693 lengthR=65536;
00694 if ( !lengthG )
00695 lengthG=65536;
00696 if ( !lengthB )
00697 lengthB=65536;
00698
00700
00701 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
00702 {
00703 gdcmWarningMacro( "(At least) a LUT is missing" );
00704 return;
00705 }
00706
00707
00708
00709 if ( BitsAllocated <= 8 )
00710 {
00711
00712 LutRGBA = new uint8_t[ 1024 ];
00713 if ( !LutRGBA )
00714 return;
00715 LutItemNumber = 256;
00716 LutItemSize = 8;
00717 memset( LutRGBA, 0, 1024 );
00718
00719 int mult;
00720 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
00721 {
00722
00723 mult = 2;
00724 }
00725 else
00726 {
00727
00728 mult = 1;
00729 }
00730
00731
00732
00733
00734
00735
00736
00737
00738 int i;
00739 uint8_t *a;
00740
00741
00742
00743
00744
00745
00746
00747 a = LutRGBA + 0 + debR;
00748 for( i=0; i < lengthR; ++i )
00749 {
00750 *a = LutRedData[i*mult+1];
00751 a += 4;
00752 }
00753
00754 a = LutRGBA + 1 + debG;
00755 for( i=0; i < lengthG; ++i)
00756 {
00757 *a = LutGreenData[i*mult+1];
00758 a += 4;
00759 }
00760
00761 a = LutRGBA + 2 + debB;
00762 for(i=0; i < lengthB; ++i)
00763 {
00764 *a = LutBlueData[i*mult+1];
00765 a += 4;
00766 }
00767
00768 a = LutRGBA + 3 ;
00769 for(i=0; i < 256; ++i)
00770 {
00771 *a = 1;
00772 a += 4;
00773 }
00774 }
00775 else
00776 {
00777
00778
00779
00780
00781 gdcmWarningMacro( "Sorry Palette Color Lookup Tables not yet dealt with"
00782 << " for 16 Bits Per Pixel images" );
00783
00784
00785
00786 LutRGBA = (uint8_t *)new uint16_t[ 65536*4 ];
00787 if ( !LutRGBA )
00788 return;
00789 memset( LutRGBA, 0, 65536*4*2 );
00790
00791 LutItemNumber = 65536;
00792 LutItemSize = 16;
00793
00794 int i;
00795 uint16_t *a16;
00796
00797
00798
00799 a16 = (uint16_t*)LutRGBA + 0 + debR;
00800 for( i=0; i < lengthR; ++i )
00801 {
00802 *a16 = ((uint16_t*)LutRedData)[i];
00803 a16 += 4;
00804 }
00805
00806 a16 = (uint16_t*)LutRGBA + 1 + debG;
00807 for( i=0; i < lengthG; ++i)
00808 {
00809 *a16 = ((uint16_t*)LutGreenData)[i];
00810 a16 += 4;
00811 }
00812
00813 a16 = (uint16_t*)LutRGBA + 2 + debB;
00814 for(i=0; i < lengthB; ++i)
00815 {
00816 *a16 = ((uint16_t*)LutBlueData)[i];
00817 a16 += 4;
00818 }
00819
00820 a16 = (uint16_t*)LutRGBA + 3 ;
00821 for(i=0; i < 65536; ++i)
00822 {
00823 *a16 = 1;
00824 a16 += 4;
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837 }
00838 }
00839
00843 void PixelReadConvert::ConvertSwapZone()
00844 {
00845 unsigned int i;
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874 int tempSwapCode = SwapCode;
00875 if ( IsPrivateGETransferSyntax )
00876 {
00877 gdcmWarningMacro(" IsPrivateGETransferSyntax found; turn the SwapCode");
00878
00879
00880 if (SwapCode == 1234)
00881 {
00882 tempSwapCode = 4321;
00883 }
00884 else if (SwapCode == 4321)
00885 {
00886 tempSwapCode = 1234;
00887 }
00888 }
00889
00890 if ( BitsAllocated == 16 )
00891 {
00892 uint16_t *im16 = (uint16_t*)Raw;
00893 switch( tempSwapCode )
00894 {
00895 case 1234:
00896 break;
00897 case 3412:
00898 case 2143:
00899 case 4321:
00900 for( i = 0; i < RawSize / 2; i++ )
00901 {
00902 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
00903 }
00904 break;
00905 default:
00906 gdcmWarningMacro("SwapCode value (16 bits) not allowed."
00907 << tempSwapCode);
00908 }
00909 }
00910 else if ( BitsAllocated == 32 )
00911 {
00912 uint32_t s32;
00913 uint16_t high;
00914 uint16_t low;
00915 uint32_t *im32 = (uint32_t*)Raw;
00916 switch ( tempSwapCode )
00917 {
00918 case 1234:
00919 break;
00920 case 4321:
00921 for( i = 0; i < RawSize / 4; i++ )
00922 {
00923 low = im32[i] & 0x0000ffff;
00924 high = im32[i] >> 16;
00925 high = ( high >> 8 ) | ( high << 8 );
00926 low = ( low >> 8 ) | ( low << 8 );
00927 s32 = low;
00928 im32[i] = ( s32 << 16 ) | high;
00929 }
00930 break;
00931 case 2143:
00932 for( i = 0; i < RawSize / 4; i++ )
00933 {
00934 low = im32[i] & 0x0000ffff;
00935 high = im32[i] >> 16;
00936 high = ( high >> 8 ) | ( high << 8 );
00937 low = ( low >> 8 ) | ( low << 8 );
00938 s32 = high;
00939 im32[i] = ( s32 << 16 ) | low;
00940 }
00941 break;
00942 case 3412:
00943 for( i = 0; i < RawSize / 4; i++ )
00944 {
00945 low = im32[i] & 0x0000ffff;
00946 high = im32[i] >> 16;
00947 s32 = low;
00948 im32[i] = ( s32 << 16 ) | high;
00949 }
00950 break;
00951 default:
00952 gdcmWarningMacro("SwapCode value (32 bits) not allowed." << tempSwapCode );
00953 }
00954 }
00955 }
00956
00960 void PixelReadConvert::ConvertReorderEndianity()
00961 {
00962 if ( BitsAllocated != 8 )
00963 {
00964 ConvertSwapZone();
00965 }
00966
00967
00968 if ( BitsAllocated == 16
00969 && BitsStored < BitsAllocated
00970 && !PixelSign )
00971 {
00972 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
00973 uint16_t *deb = (uint16_t *)Raw;
00974 for(int i = 0; i<l; i++)
00975 {
00976 if ( *deb == 0xffff )
00977 {
00978 *deb = 0;
00979 }
00980 deb++;
00981 }
00982 }
00983 }
00984
00989 void PixelReadConvert::ConvertFixGreyLevels()
00990 {
00991 if (!IsMonochrome1)
00992 return;
00993
00994 uint32_t i;
00995 int16_t j;
00996
00997 if (!PixelSign)
00998 {
00999 if ( BitsAllocated == 8 )
01000 {
01001 uint8_t *deb = (uint8_t *)Raw;
01002 for (i=0; i<RawSize; i++)
01003 {
01004 *deb = 255 - *deb;
01005 deb++;
01006 }
01007 return;
01008 }
01009
01010 if ( BitsAllocated == 16 )
01011 {
01012 uint16_t mask =1;
01013 for (j=0; j<BitsStored-1; j++)
01014 {
01015 mask = (mask << 1) +1;
01016 }
01017
01018 uint16_t *deb = (uint16_t *)Raw;
01019 for (i=0; i<RawSize/2; i++)
01020 {
01021 *deb = mask - *deb;
01022 deb++;
01023 }
01024 return;
01025 }
01026 }
01027 else
01028 {
01029 if ( BitsAllocated == 8 )
01030 {
01031 uint8_t smask8 = 255;
01032 uint8_t *deb = (uint8_t *)Raw;
01033 for (i=0; i<RawSize; i++)
01034 {
01035 *deb = smask8 - *deb;
01036 deb++;
01037 }
01038 return;
01039 }
01040 if ( BitsAllocated == 16 )
01041 {
01042 uint16_t smask16 = 65535;
01043 uint16_t *deb = (uint16_t *)Raw;
01044 for (i=0; i<RawSize/2; i++)
01045 {
01046 *deb = smask16 - *deb;
01047 deb++;
01048 }
01049 return;
01050 }
01051 }
01052 }
01053
01058 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
01059 {
01060
01061 if ( BitsStored != BitsAllocated )
01062 {
01063 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
01064 if ( BitsAllocated == 16 )
01065 {
01066
01067 uint16_t pmask = 0xffff;
01068 pmask = pmask >> ( BitsAllocated - BitsStored );
01069
01070 uint16_t *deb = (uint16_t*)Raw;
01071
01072 if ( !PixelSign )
01073 {
01074 for(int i = 0; i<l; i++)
01075 {
01076 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
01077 deb++;
01078 }
01079 }
01080 else
01081 {
01082
01083 uint16_t smask = 0x0001;
01084 smask = smask << ( 16 - (BitsAllocated - BitsStored + 1) );
01085
01086 int16_t nmask = (int16_t)0x8000;
01087 nmask = nmask >> ( BitsAllocated - BitsStored - 1 );
01088
01089 for(int i = 0; i<l; i++)
01090 {
01091 *deb = *deb >> (BitsStored - HighBitPosition - 1);
01092 if ( *deb & smask )
01093 {
01094 *deb = *deb | nmask;
01095 }
01096 else
01097 {
01098 *deb = *deb & pmask;
01099 }
01100 deb++;
01101 }
01102 }
01103 }
01104 else if ( BitsAllocated == 32 )
01105 {
01106
01107 uint32_t pmask = 0xffffffff;
01108 pmask = pmask >> ( BitsAllocated - BitsStored );
01109
01110 uint32_t *deb = (uint32_t*)Raw;
01111
01112 if ( !PixelSign )
01113 {
01114 for(int i = 0; i<l; i++)
01115 {
01116 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
01117 deb++;
01118 }
01119 }
01120 else
01121 {
01122
01123 uint32_t smask = 0x00000001;
01124 smask = smask >> ( 32 - (BitsAllocated - BitsStored +1 ));
01125
01126 int32_t nmask = 0x80000000;
01127 nmask = nmask >> ( BitsAllocated - BitsStored -1 );
01128
01129 for(int i = 0; i<l; i++)
01130 {
01131 *deb = *deb >> (BitsStored - HighBitPosition - 1);
01132 if ( *deb & smask )
01133 *deb = *deb | nmask;
01134 else
01135 *deb = *deb & pmask;
01136 deb++;
01137 }
01138 }
01139 }
01140 else
01141 {
01142 gdcmWarningMacro("Weird image (BitsAllocated !=8, 12, 16, 32)");
01143 throw FormatError( "Weird image !?" );
01144 }
01145 }
01146 return true;
01147 }
01148
01153 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
01154 {
01155 gdcmWarningMacro("--> ConvertRGBPlanesToRGBPixels");
01156
01157 uint8_t *localRaw = Raw;
01158 uint8_t *copyRaw = new uint8_t[ RawSize ];
01159 memmove( copyRaw, localRaw, RawSize );
01160
01161 int l = XSize * YSize * ZSize;
01162
01163 uint8_t *a = copyRaw;
01164 uint8_t *b = copyRaw + l;
01165 uint8_t *c = copyRaw + l + l;
01166
01167 for (int j = 0; j < l; j++)
01168 {
01169 *(localRaw++) = *(a++);
01170 *(localRaw++) = *(b++);
01171 *(localRaw++) = *(c++);
01172 }
01173 delete[] copyRaw;
01174 }
01175
01180 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
01181 {
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191 gdcmWarningMacro("--> ConvertYcBcRPlanesToRGBPixels");
01192
01193 uint8_t *localRaw = Raw;
01194 uint8_t *copyRaw = new uint8_t[ RawSize ];
01195 memmove( copyRaw, localRaw, RawSize );
01196
01197
01198
01199
01200
01201
01202
01204 int l = XSize * YSize * TSize;
01205 int nbFrames = ZSize;
01206
01207 uint8_t *a = copyRaw + 0;
01208 uint8_t *b = copyRaw + l;
01209 uint8_t *c = copyRaw + l+ l;
01210 int32_t R, G, B;
01211
01217
01218 for ( int i = 0; i < nbFrames; i++ )
01219 {
01220 for ( int j = 0; j < l; j++ )
01221 {
01222 R = 38142 *(*a-16) + 52298 *(*c -128);
01223 G = 38142 *(*a-16) - 26640 *(*c -128) - 12845 *(*b -128);
01224 B = 38142 *(*a-16) + 66093 *(*b -128);
01225
01226 R = (R+16384)>>15;
01227 G = (G+16384)>>15;
01228 B = (B+16384)>>15;
01229
01230 if (R < 0) R = 0;
01231 if (G < 0) G = 0;
01232 if (B < 0) B = 0;
01233 if (R > 255) R = 255;
01234 if (G > 255) G = 255;
01235 if (B > 255) B = 255;
01236
01237 *(localRaw++) = (uint8_t)R;
01238 *(localRaw++) = (uint8_t)G;
01239 *(localRaw++) = (uint8_t)B;
01240 a++;
01241 b++;
01242 c++;
01243 }
01244 }
01245 delete[] copyRaw;
01246 }
01247
01252
01253 void PixelReadConvert::ConvertHandleColor()
01254 {
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293 gdcmDebugMacro("--> ConvertHandleColor "
01294 << "Planar Configuration " << PlanarConfiguration );
01295
01296 if ( ! IsRawRGB() )
01297 {
01298
01299 gdcmDebugMacro("--> RawRGB : LUT intervention done outside");
01300 return;
01301 }
01302
01303 if ( PlanarConfiguration == 1 )
01304 {
01305 if ( IsYBRFull )
01306 {
01307
01308 gdcmDebugMacro("--> YBRFull");
01309 ConvertYcBcRPlanesToRGBPixels();
01310 }
01311 else
01312 {
01313
01314 gdcmDebugMacro("--> YBRFull");
01315 ConvertRGBPlanesToRGBPixels();
01316 }
01317 return;
01318 }
01319
01320
01321
01322
01323 if (IsRLELossless)
01324 {
01325 gdcmDebugMacro("--> RLE Lossless");
01326 ConvertRGBPlanesToRGBPixels();
01327 }
01328
01329
01330 }
01331
01333 void PixelReadConvert::ComputeRawAndRGBSizes()
01334 {
01335 int bitsAllocated = BitsAllocated;
01336
01337
01338
01339 if ( BitsAllocated == 12 )
01340 {
01341 bitsAllocated = 16;
01342 }
01343
01344 RawSize = XSize * YSize * ZSize * TSize
01345 * ( bitsAllocated / 8 )
01346 * SamplesPerPixel;
01347 if ( HasLUT )
01348 {
01349 RGBSize = 3 * RawSize;
01350 }
01351 else
01352 {
01353 RGBSize = RawSize;
01354
01355 }
01356 RawSize += RawSize%2;
01357 RGBSize += RGBSize%2;
01358 }
01359
01361 void PixelReadConvert::AllocateRGB()
01362 {
01363 if ( RGB )
01364 delete [] RGB;
01365 RGB = new uint8_t[RGBSize];
01366 }
01367
01369 void PixelReadConvert::AllocateRaw()
01370 {
01371 if ( Raw )
01372 delete [] Raw;
01373 Raw = new uint8_t[RawSize];
01374 }
01375
01376
01377
01383 void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
01384 {
01385 os << indent
01386 << "--- Pixel information -------------------------"
01387 << std::endl;
01388 os << indent
01389 << "Pixel Data: offset " << PixelOffset
01390 << " x(" << std::hex << PixelOffset << std::dec
01391 << ") length " << PixelDataLength
01392 << " x(" << std::hex << PixelDataLength << std::dec
01393 << ")" << std::endl;
01394
01395 if ( IsRLELossless )
01396 {
01397 if ( RLEInfo )
01398 {
01399 RLEInfo->Print( os, indent );
01400 }
01401 else
01402 {
01403 gdcmWarningMacro("Set as RLE file but NO RLEinfo present.");
01404 }
01405 }
01406
01407 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
01408 {
01409 if ( JPEGInfo )
01410 {
01411 JPEGInfo->Print( os, indent );
01412 }
01413 else
01414 {
01415 gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present.");
01416 }
01417 }
01418 }
01419
01423 void PixelReadConvert::CallStartMethod()
01424 {
01425 Progress = 0.0f;
01426 Abort = false;
01427 CommandManager::ExecuteCommand(FH,CMD_STARTPROGRESS);
01428 }
01429
01433 void PixelReadConvert::CallProgressMethod()
01434 {
01435 CommandManager::ExecuteCommand(FH,CMD_PROGRESS);
01436 }
01437
01441 void PixelReadConvert::CallEndMethod()
01442 {
01443 Progress = 1.0f;
01444 CommandManager::ExecuteCommand(FH,CMD_ENDPROGRESS);
01445 }
01446
01447
01448
01449 }
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607