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 namespace gdcm
00032 {
00033
00034
00035 bool gdcm_read_JPEG2000_file (void* raw,
00036 char *inputdata, size_t inputlength);
00037
00038 #define str2num(str, typeNum) *((typeNum *)(str))
00039
00040
00041
00043 PixelReadConvert::PixelReadConvert()
00044 {
00045 RGB = 0;
00046 RGBSize = 0;
00047 Raw = 0;
00048 RawSize = 0;
00049 LutRGBA = 0;
00050 LutRedData = 0;
00051 LutGreenData = 0;
00052 LutBlueData = 0;
00053 RLEInfo = 0;
00054 JPEGInfo = 0;
00055 UserFunction = 0;
00056 FileInternal = 0;
00057 }
00058
00060 PixelReadConvert::~PixelReadConvert()
00061 {
00062 Squeeze();
00063 }
00064
00065
00066
00071 bool PixelReadConvert::IsRawRGB()
00072 {
00073 if ( IsMonochrome
00074 || PlanarConfiguration == 2
00075 || IsPaletteColor )
00076 {
00077 return false;
00078 }
00079 return true;
00080 }
00085 void PixelReadConvert::GrabInformationsFromFile( File *file,
00086 FileHelper *fileHelper )
00087 {
00088
00089
00090 BitsAllocated = file->GetBitsAllocated();
00091 if ( BitsAllocated == 0 )
00092 {
00093 BitsAllocated = 16;
00094 }
00095
00096
00097
00098 BitsStored = file->GetBitsStored();
00099 if ( BitsStored == 0 )
00100 {
00101 BitsStored = BitsAllocated;
00102 }
00103
00104
00105 HighBitPosition = file->GetHighBitPosition();
00106 if ( HighBitPosition == 0 )
00107 {
00108 HighBitPosition = BitsAllocated - 1;
00109 }
00110
00111 XSize = file->GetXSize();
00112 YSize = file->GetYSize();
00113 ZSize = file->GetZSize();
00114 SamplesPerPixel = file->GetSamplesPerPixel();
00115
00116 PixelSign = file->IsSignedPixelData();
00117 SwapCode = file->GetSwapCode();
00118
00119 IsPrivateGETransferSyntax = IsMPEG
00120 = IsJPEG2000 = IsJPEGLS = IsJPEGLossy
00121 = IsJPEGLossless = IsRLELossless
00122 = false;
00123
00124 if (! file->IsDicomV3() )
00125 {
00126 IsRaw = true;
00127 }
00128 else
00129 {
00130 std::string ts = file->GetTransferSyntax();
00131
00132 IsRaw = false;
00133 while (true)
00134 {
00135
00136 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRLittleEndian) break;
00137 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian ) break;
00138 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian) break;
00139 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE) break;
00140 if( IsRaw = Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian) break;
00141 break;
00142 }
00143
00144
00145
00146 IsPrivateGETransferSyntax =
00147 ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRBigEndianPrivateGE );
00148
00149 IsMPEG = IsJPEG2000 = IsJPEGLS = IsJPEGLossy = IsJPEGLossless = IsRLELossless = false;
00150 if (!IsRaw)
00151 {
00152 while(true)
00153 {
00154
00155 if( IsJPEGLossy = Global::GetTS()->IsJPEGLossy(ts) ) break;
00156 if( IsJPEGLossless = Global::GetTS()->IsJPEGLossless(ts) ) break;
00157 if( IsRLELossless = Global::GetTS()->IsRLELossless(ts) ) break;
00158 if( IsJPEG2000 = Global::GetTS()->IsJPEG2000(ts) ) break;
00159 if( IsMPEG = Global::GetTS()->IsMPEG(ts) ) break;
00160 if( IsJPEGLS = Global::GetTS()->IsJPEGLS(ts) ) break;
00161 gdcmWarningMacro("Unexpected Transfer Syntax :[" << ts << "]");
00162 break;
00163 }
00164 }
00165 }
00166
00167 PixelOffset = file->GetPixelOffset();
00168 PixelDataLength = file->GetPixelAreaLength();
00169 RLEInfo = file->GetRLEInfo();
00170 JPEGInfo = file->GetJPEGInfo();
00171
00172 IsMonochrome = file->IsMonochrome();
00173 IsMonochrome1 = file->IsMonochrome1();
00174 IsPaletteColor = file->IsPaletteColor();
00175 IsYBRFull = file->IsYBRFull();
00176
00177 PlanarConfiguration = file->GetPlanarConfiguration();
00178
00180
00181 HasLUT = file->HasLUT();
00182 if ( HasLUT )
00183 {
00184
00185 LutRedDescriptor = file->GetEntryString( 0x0028, 0x1101 );
00186 LutGreenDescriptor = file->GetEntryString( 0x0028, 0x1102 );
00187 LutBlueDescriptor = file->GetEntryString( 0x0028, 0x1103 );
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 file->LoadEntryBinArea(0x0028, 0x1201);
00208 LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
00209 if ( ! LutRedData )
00210 {
00211 gdcmWarningMacro("Unable to read Red Palette Color Lookup Table data");
00212 }
00213
00214
00215 file->LoadEntryBinArea(0x0028, 0x1202);
00216 LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
00217 if ( ! LutGreenData)
00218 {
00219 gdcmWarningMacro("Unable to read Green Palette Color Lookup Table data");
00220 }
00221
00222
00223 file->LoadEntryBinArea(0x0028, 0x1203);
00224 LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
00225 if ( ! LutBlueData )
00226 {
00227 gdcmWarningMacro("Unable to read Blue Palette Color Lookup Table data");
00228 }
00229 }
00230 FileInternal = file;
00231 FH = fileHelper;
00232 ComputeRawAndRGBSizes();
00233 }
00234
00236 bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
00237 {
00238
00239
00240
00241 Squeeze();
00242
00245 if ( !fp )
00246 {
00247 gdcmWarningMacro( "Unavailable file pointer." );
00248 return false;
00249 }
00250
00251 fp->seekg( PixelOffset, std::ios::beg );
00252 if ( fp->fail() || fp->eof() )
00253 {
00254 gdcmWarningMacro( "Unable to find PixelOffset in file." );
00255 return false;
00256 }
00257
00258 AllocateRaw();
00259
00261
00262 CallStartMethod();
00263 unsigned int count = 0;
00264 unsigned int frameSize;
00265 unsigned int bitsAllocated = BitsAllocated;
00266 if(bitsAllocated == 12)
00267 bitsAllocated = 16;
00268 frameSize = XSize*YSize*SamplesPerPixel*bitsAllocated/8;
00269
00271
00272 if ( BitsAllocated == 12 )
00273 {
00274 ReadAndDecompress12BitsTo16Bits( fp);
00275 }
00276 else if ( IsRaw )
00277 {
00278
00279
00280
00281
00282 if ( PixelDataLength != RawSize )
00283 {
00284 gdcmWarningMacro( "Mismatch between PixelReadConvert : "
00285 << PixelDataLength << " and RawSize : " << RawSize );
00286 }
00287
00288
00289 char *raw = (char*)Raw;
00290 uint32_t remainingLength;
00291 unsigned int i;
00292 unsigned int lengthToRead;
00293
00294 if ( PixelDataLength > RawSize )
00295 lengthToRead = RawSize;
00296 else
00297 lengthToRead = PixelDataLength;
00298
00299
00300 remainingLength = lengthToRead;
00301 unsigned int nbFrames = lengthToRead / frameSize;
00302 for (i=0;i<nbFrames; i++)
00303 {
00304 Progress = (float)(count+1)/(float)nbFrames;
00305 fp->read( raw, frameSize);
00306 raw += frameSize;
00307 remainingLength -= frameSize;
00308 count++;
00309 }
00310 if (remainingLength !=0 )
00311 fp->read( raw, remainingLength);
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 if ( fp->fail() || fp->eof())
00323 {
00324 gdcmWarningMacro( "Reading of Raw pixel data failed." );
00325 return false;
00326 }
00327 }
00328 else if ( IsRLELossless )
00329 {
00330 if ( ! RLEInfo->DecompressRLEFile
00331 ( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) )
00332 {
00333 gdcmWarningMacro( "RLE decompressor failed." );
00334 return false;
00335 }
00336 }
00337 else if ( IsMPEG )
00338 {
00339
00340
00341
00342
00343 return true;
00344 }
00345 else
00346 {
00347
00348 if ( ! ReadAndDecompressJPEGFile( fp ) )
00349 {
00350 gdcmWarningMacro( "JPEG decompressor ( ReadAndDecompressJPEGFile()"
00351 << " method ) failed." );
00352 return false;
00353 }
00354 }
00355
00358 ConvertReorderEndianity();
00359 ConvertReArrangeBits();
00360 ConvertFixGreyLevels();
00361 if (UserFunction)
00362 UserFunction( Raw, FileInternal);
00363 ConvertHandleColor();
00364
00365 return true;
00366 }
00367
00369 void PixelReadConvert::Squeeze()
00370 {
00371 if ( RGB )
00372 delete [] RGB;
00373 RGB = 0;
00374
00375 if ( Raw )
00376 delete [] Raw;
00377 Raw = 0;
00378
00379 if ( LutRGBA )
00380 delete [] LutRGBA;
00381 LutRGBA = 0;
00382 }
00383
00387 bool PixelReadConvert::BuildRGBImage()
00388 {
00389 if ( RGB )
00390 {
00391
00392 return true;
00393 }
00394
00395 if ( ! Raw )
00396 {
00397
00398 return false;
00399 }
00400
00401 BuildLUTRGBA();
00402 if ( ! LutRGBA )
00403 {
00404
00405 return false;
00406 }
00407
00408 gdcmDebugMacro( "--> BuildRGBImage" );
00409
00410
00411 AllocateRGB();
00412
00413 int j;
00414 if ( BitsAllocated <= 8 )
00415 {
00416 uint8_t *localRGB = RGB;
00417 for (size_t i = 0; i < RawSize; ++i )
00418 {
00419 j = Raw[i] * 4;
00420 *localRGB++ = LutRGBA[j];
00421 *localRGB++ = LutRGBA[j+1];
00422 *localRGB++ = LutRGBA[j+2];
00423 }
00424 }
00425
00426 else
00427 {
00428 uint16_t *localRGB = (uint16_t *)RGB;
00429 for (size_t i = 0; i < RawSize/2; ++i )
00430 {
00431 j = ((uint16_t *)Raw)[i] * 4;
00432 *localRGB++ = ((uint16_t *)LutRGBA)[j];
00433 *localRGB++ = ((uint16_t *)LutRGBA)[j+1];
00434 *localRGB++ = ((uint16_t *)LutRGBA)[j+2];
00435 }
00436 }
00437
00438 return true;
00439 }
00440
00441
00442
00443
00444
00445
00450 void PixelReadConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream *fp )
00451 throw ( FormatError )
00452 {
00453 int nbPixels = XSize * YSize;
00454 uint16_t *localDecompres = (uint16_t*)Raw;
00455
00456 for( int p = 0; p < nbPixels; p += 2 )
00457 {
00458 uint8_t b0, b1, b2;
00459
00460 fp->read( (char*)&b0, 1);
00461 if ( fp->fail() || fp->eof() )
00462 {
00463 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00464 "Unfound first block" );
00465 }
00466
00467 fp->read( (char*)&b1, 1 );
00468 if ( fp->fail() || fp->eof())
00469 {
00470 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00471 "Unfound second block" );
00472 }
00473
00474 fp->read( (char*)&b2, 1 );
00475 if ( fp->fail() || fp->eof())
00476 {
00477 throw FormatError( "PixelReadConvert::ReadAndDecompress12BitsTo16Bits()",
00478 "Unfound second block" );
00479 }
00480
00481
00482
00483
00484
00485
00486 *localDecompres++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
00487
00488 *localDecompres++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
00489
00491 }
00492 }
00493
00500 bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
00501 {
00502 if ( IsJPEG2000 )
00503 {
00504
00505 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEGLS );
00506
00507
00508
00509
00510 unsigned long inputlength = 0;
00511 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
00512 while( jpegfrag )
00513 {
00514 inputlength += jpegfrag->GetLength();
00515 jpegfrag = JPEGInfo->GetNextFragment();
00516 }
00517 gdcmAssertMacro( inputlength != 0);
00518 uint8_t *inputdata = new uint8_t[inputlength];
00519 char *pinputdata = (char*)inputdata;
00520 jpegfrag = JPEGInfo->GetFirstFragment();
00521 while( jpegfrag )
00522 {
00523 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
00524 fp->read(pinputdata, jpegfrag->GetLength());
00525 pinputdata += jpegfrag->GetLength();
00526 jpegfrag = JPEGInfo->GetNextFragment();
00527 }
00528
00529 if ( ! gdcm_read_JPEG2000_file( Raw,
00530 (char*)inputdata, inputlength ) )
00531 {
00532 return true;
00533 }
00534
00535 gdcmWarningMacro( "gdcm_read_JPEG2000_file() failed ");
00536 return false;
00537 }
00538 else if ( IsJPEGLS )
00539 {
00540
00541 assert( !IsJPEGLossless || !IsJPEGLossy || !IsJPEG2000 );
00542
00543
00544
00545
00546
00547
00548
00549
00550 #if 0
00551 std::cerr << "count:" << JPEGInfo->GetFragmentCount() << std::endl;
00552 unsigned long inputlength = 0;
00553 JPEGFragment *jpegfrag = JPEGInfo->GetFirstFragment();
00554 while( jpegfrag )
00555 {
00556 inputlength += jpegfrag->GetLength();
00557 jpegfrag = JPEGInfo->GetNextFragment();
00558 }
00559 gdcmAssertMacro( inputlength != 0);
00560 uint8_t *inputdata = new uint8_t[inputlength];
00561 char *pinputdata = (char*)inputdata;
00562 jpegfrag = JPEGInfo->GetFirstFragment();
00563 while( jpegfrag )
00564 {
00565 fp->seekg( jpegfrag->GetOffset(), std::ios::beg);
00566 fp->read(pinputdata, jpegfrag->GetLength());
00567 pinputdata += jpegfrag->GetLength();
00568 jpegfrag = JPEGInfo->GetNextFragment();
00569 }
00570
00571
00572
00573 std::ofstream out("/tmp/jpegls.jpg");
00574 out.write((char*)inputdata, inputlength);
00575 out.close();
00576 delete[] inputdata;
00577 #endif
00578
00579 gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
00580 fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
00581
00582 return false;
00583 }
00584 else
00585 {
00586
00587 assert( !IsJPEGLS || !IsJPEG2000 );
00588
00589 int length = XSize * YSize * SamplesPerPixel;
00590 int numberBytes = BitsAllocated / 8;
00591
00592 JPEGInfo->DecompressFromFile(fp, Raw, BitsStored, numberBytes, length );
00593 return true;
00594 }
00595 }
00596
00614 void PixelReadConvert::BuildLUTRGBA()
00615 {
00616
00617
00618
00619
00620
00621 if ( LutRGBA )
00622
00623 {
00624 return;
00625 }
00626
00627
00628
00629 if ( ! IsPaletteColor )
00630 {
00631 return;
00632 }
00633
00634 if ( LutRedDescriptor == GDCM_UNFOUND
00635 || LutGreenDescriptor == GDCM_UNFOUND
00636 || LutBlueDescriptor == GDCM_UNFOUND )
00637 {
00638 gdcmWarningMacro( "(At least) a LUT Descriptor is missing" );
00639 return;
00640 }
00641
00643
00644 int lengthR;
00645 int debR;
00646 int nbitsR;
00647 int nbRead;
00648
00649 nbRead = sscanf( LutRedDescriptor.c_str(),
00650 "%d\\%d\\%d",
00651 &lengthR, &debR, &nbitsR );
00652 if ( nbRead != 3 )
00653 {
00654 gdcmWarningMacro( "Wrong Red LUT descriptor" );
00655 }
00656 int lengthG;
00657 int debG;
00658 int nbitsG;
00659
00660 nbRead = sscanf( LutGreenDescriptor.c_str(),
00661 "%d\\%d\\%d",
00662 &lengthG, &debG, &nbitsG );
00663 if ( nbRead != 3 )
00664 {
00665 gdcmWarningMacro( "Wrong Green LUT descriptor" );
00666 }
00667
00668 int lengthB;
00669 int debB;
00670 int nbitsB;
00671 nbRead = sscanf( LutRedDescriptor.c_str(),
00672 "%d\\%d\\%d",
00673 &lengthB, &debB, &nbitsB );
00674 if ( nbRead != 3 )
00675 {
00676 gdcmWarningMacro( "Wrong Blue LUT descriptor" );
00677 }
00678
00679 gdcmDebugMacro(" lengthR " << lengthR << " debR "
00680 << debR << " nbitsR " << nbitsR);
00681 gdcmDebugMacro(" lengthG " << lengthG << " debG "
00682 << debG << " nbitsG " << nbitsG);
00683 gdcmDebugMacro(" lengthB " << lengthB << " debB "
00684 << debB << " nbitsB " << nbitsB);
00685
00686 if ( !lengthR )
00687 lengthR=65536;
00688 if ( !lengthG )
00689 lengthG=65536;
00690 if ( !lengthB )
00691 lengthB=65536;
00692
00694
00695 if ( ( ! LutRedData ) || ( ! LutGreenData ) || ( ! LutBlueData ) )
00696 {
00697 gdcmWarningMacro( "(At least) a LUT is missing" );
00698 return;
00699 }
00700
00701
00702
00703 if ( BitsAllocated <= 8 )
00704 {
00705
00706 LutRGBA = new uint8_t[ 1024 ];
00707 if ( !LutRGBA )
00708 return;
00709 LutItemNumber = 256;
00710 LutItemSize = 8;
00711 memset( LutRGBA, 0, 1024 );
00712
00713 int mult;
00714 if ( ( nbitsR == 16 ) && ( BitsAllocated == 8 ) )
00715 {
00716
00717 mult = 2;
00718 }
00719 else
00720 {
00721
00722 mult = 1;
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732 int i;
00733 uint8_t *a;
00734
00735
00736
00737
00738
00739
00740
00741 a = LutRGBA + 0 + debR;
00742 for( i=0; i < lengthR; ++i )
00743 {
00744 *a = LutRedData[i*mult+1];
00745 a += 4;
00746 }
00747
00748 a = LutRGBA + 1 + debG;
00749 for( i=0; i < lengthG; ++i)
00750 {
00751 *a = LutGreenData[i*mult+1];
00752 a += 4;
00753 }
00754
00755 a = LutRGBA + 2 + debB;
00756 for(i=0; i < lengthB; ++i)
00757 {
00758 *a = LutBlueData[i*mult+1];
00759 a += 4;
00760 }
00761
00762 a = LutRGBA + 3 ;
00763 for(i=0; i < 256; ++i)
00764 {
00765 *a = 1;
00766 a += 4;
00767 }
00768 }
00769 else
00770 {
00771
00772
00773
00774
00775 gdcmWarningMacro( "Sorry Palette Color Lookup Tables not yet dealt with"
00776 << " for 16 Bits Per Pixel images" );
00777
00778
00779
00780 LutRGBA = (uint8_t *)new uint16_t[ 65536*4 ];
00781 if ( !LutRGBA )
00782 return;
00783 memset( LutRGBA, 0, 65536*4*2 );
00784
00785 LutItemNumber = 65536;
00786 LutItemSize = 16;
00787
00788 int i;
00789 uint16_t *a16;
00790
00791
00792
00793 a16 = (uint16_t*)LutRGBA + 0 + debR;
00794 for( i=0; i < lengthR; ++i )
00795 {
00796 *a16 = ((uint16_t*)LutRedData)[i];
00797 a16 += 4;
00798 }
00799
00800 a16 = (uint16_t*)LutRGBA + 1 + debG;
00801 for( i=0; i < lengthG; ++i)
00802 {
00803 *a16 = ((uint16_t*)LutGreenData)[i];
00804 a16 += 4;
00805 }
00806
00807 a16 = (uint16_t*)LutRGBA + 2 + debB;
00808 for(i=0; i < lengthB; ++i)
00809 {
00810 *a16 = ((uint16_t*)LutBlueData)[i];
00811 a16 += 4;
00812 }
00813
00814 a16 = (uint16_t*)LutRGBA + 3 ;
00815 for(i=0; i < 65536; ++i)
00816 {
00817 *a16 = 1;
00818 a16 += 4;
00819 }
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 }
00832 }
00833
00837 void PixelReadConvert::ConvertSwapZone()
00838 {
00839 unsigned int i;
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868 int tempSwapCode = SwapCode;
00869 if ( IsPrivateGETransferSyntax )
00870 {
00871 gdcmWarningMacro(" IsPrivateGETransferSyntax found; turn the SwapCode");
00872
00873
00874 if (SwapCode == 1234)
00875 {
00876 tempSwapCode = 4321;
00877 }
00878 else if (SwapCode == 4321)
00879 {
00880 tempSwapCode = 1234;
00881 }
00882 }
00883
00884 if ( BitsAllocated == 16 )
00885 {
00886 uint16_t *im16 = (uint16_t*)Raw;
00887 switch( tempSwapCode )
00888 {
00889 case 1234:
00890 break;
00891 case 3412:
00892 case 2143:
00893 case 4321:
00894 for( i = 0; i < RawSize / 2; i++ )
00895 {
00896 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
00897 }
00898 break;
00899 default:
00900 gdcmWarningMacro("SwapCode value (16 bits) not allowed."
00901 << tempSwapCode);
00902 }
00903 }
00904 else if ( BitsAllocated == 32 )
00905 {
00906 uint32_t s32;
00907 uint16_t high;
00908 uint16_t low;
00909 uint32_t *im32 = (uint32_t*)Raw;
00910 switch ( tempSwapCode )
00911 {
00912 case 1234:
00913 break;
00914 case 4321:
00915 for( i = 0; i < RawSize / 4; i++ )
00916 {
00917 low = im32[i] & 0x0000ffff;
00918 high = im32[i] >> 16;
00919 high = ( high >> 8 ) | ( high << 8 );
00920 low = ( low >> 8 ) | ( low << 8 );
00921 s32 = low;
00922 im32[i] = ( s32 << 16 ) | high;
00923 }
00924 break;
00925 case 2143:
00926 for( i = 0; i < RawSize / 4; i++ )
00927 {
00928 low = im32[i] & 0x0000ffff;
00929 high = im32[i] >> 16;
00930 high = ( high >> 8 ) | ( high << 8 );
00931 low = ( low >> 8 ) | ( low << 8 );
00932 s32 = high;
00933 im32[i] = ( s32 << 16 ) | low;
00934 }
00935 break;
00936 case 3412:
00937 for( i = 0; i < RawSize / 4; i++ )
00938 {
00939 low = im32[i] & 0x0000ffff;
00940 high = im32[i] >> 16;
00941 s32 = low;
00942 im32[i] = ( s32 << 16 ) | high;
00943 }
00944 break;
00945 default:
00946 gdcmWarningMacro("SwapCode value (32 bits) not allowed." << tempSwapCode );
00947 }
00948 }
00949 }
00950
00954 void PixelReadConvert::ConvertReorderEndianity()
00955 {
00956 if ( BitsAllocated != 8 )
00957 {
00958 ConvertSwapZone();
00959 }
00960
00961
00962 if ( BitsAllocated == 16
00963 && BitsStored < BitsAllocated
00964 && !PixelSign )
00965 {
00966 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
00967 uint16_t *deb = (uint16_t *)Raw;
00968 for(int i = 0; i<l; i++)
00969 {
00970 if ( *deb == 0xffff )
00971 {
00972 *deb = 0;
00973 }
00974 deb++;
00975 }
00976 }
00977 }
00978
00983 void PixelReadConvert::ConvertFixGreyLevels()
00984 {
00985 if (!IsMonochrome1)
00986 return;
00987
00988 uint32_t i;
00989 int16_t j;
00990
00991 if (!PixelSign)
00992 {
00993 if ( BitsAllocated == 8 )
00994 {
00995 uint8_t *deb = (uint8_t *)Raw;
00996 for (i=0; i<RawSize; i++)
00997 {
00998 *deb = 255 - *deb;
00999 deb++;
01000 }
01001 return;
01002 }
01003
01004 if ( BitsAllocated == 16 )
01005 {
01006 uint16_t mask =1;
01007 for (j=0; j<BitsStored-1; j++)
01008 {
01009 mask = (mask << 1) +1;
01010 }
01011
01012 uint16_t *deb = (uint16_t *)Raw;
01013 for (i=0; i<RawSize/2; i++)
01014 {
01015 *deb = mask - *deb;
01016 deb++;
01017 }
01018 return;
01019 }
01020 }
01021 else
01022 {
01023 if ( BitsAllocated == 8 )
01024 {
01025 uint8_t smask8 = 255;
01026 uint8_t *deb = (uint8_t *)Raw;
01027 for (i=0; i<RawSize; i++)
01028 {
01029 *deb = smask8 - *deb;
01030 deb++;
01031 }
01032 return;
01033 }
01034 if ( BitsAllocated == 16 )
01035 {
01036 uint16_t smask16 = 65535;
01037 uint16_t *deb = (uint16_t *)Raw;
01038 for (i=0; i<RawSize/2; i++)
01039 {
01040 *deb = smask16 - *deb;
01041 deb++;
01042 }
01043 return;
01044 }
01045 }
01046 }
01047
01052 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
01053 {
01054
01055 if ( BitsStored != BitsAllocated )
01056 {
01057 int l = (int)( RawSize / ( BitsAllocated / 8 ) );
01058 if ( BitsAllocated == 16 )
01059 {
01060
01061 uint16_t pmask = 0xffff;
01062 pmask = pmask >> ( BitsAllocated - BitsStored );
01063
01064 uint16_t *deb = (uint16_t*)Raw;
01065
01066 if ( !PixelSign )
01067 {
01068 for(int i = 0; i<l; i++)
01069 {
01070 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
01071 deb++;
01072 }
01073 }
01074 else
01075 {
01076
01077 uint16_t smask = 0x0001;
01078 smask = smask << ( 16 - (BitsAllocated - BitsStored + 1) );
01079
01080 int16_t nmask = (int16_t)0x8000;
01081 nmask = nmask >> ( BitsAllocated - BitsStored - 1 );
01082
01083 for(int i = 0; i<l; i++)
01084 {
01085 *deb = *deb >> (BitsStored - HighBitPosition - 1);
01086 if ( *deb & smask )
01087 {
01088 *deb = *deb | nmask;
01089 }
01090 else
01091 {
01092 *deb = *deb & pmask;
01093 }
01094 deb++;
01095 }
01096 }
01097 }
01098 else if ( BitsAllocated == 32 )
01099 {
01100
01101 uint32_t pmask = 0xffffffff;
01102 pmask = pmask >> ( BitsAllocated - BitsStored );
01103
01104 uint32_t *deb = (uint32_t*)Raw;
01105
01106 if ( !PixelSign )
01107 {
01108 for(int i = 0; i<l; i++)
01109 {
01110 *deb = (*deb >> (BitsStored - HighBitPosition - 1)) & pmask;
01111 deb++;
01112 }
01113 }
01114 else
01115 {
01116
01117 uint32_t smask = 0x00000001;
01118 smask = smask >> ( 32 - (BitsAllocated - BitsStored +1 ));
01119
01120 int32_t nmask = 0x80000000;
01121 nmask = nmask >> ( BitsAllocated - BitsStored -1 );
01122
01123 for(int i = 0; i<l; i++)
01124 {
01125 *deb = *deb >> (BitsStored - HighBitPosition - 1);
01126 if ( *deb & smask )
01127 *deb = *deb | nmask;
01128 else
01129 *deb = *deb & pmask;
01130 deb++;
01131 }
01132 }
01133 }
01134 else
01135 {
01136 gdcmWarningMacro("Weird image (BitsAllocated !=8, 12, 16, 32)");
01137 throw FormatError( "Weird image !?" );
01138 }
01139 }
01140 return true;
01141 }
01142
01147 void PixelReadConvert::ConvertRGBPlanesToRGBPixels()
01148 {
01149 gdcmWarningMacro("--> ConvertRGBPlanesToRGBPixels");
01150
01151 uint8_t *localRaw = Raw;
01152 uint8_t *copyRaw = new uint8_t[ RawSize ];
01153 memmove( copyRaw, localRaw, RawSize );
01154
01155 int l = XSize * YSize * ZSize;
01156
01157 uint8_t *a = copyRaw;
01158 uint8_t *b = copyRaw + l;
01159 uint8_t *c = copyRaw + l + l;
01160
01161 for (int j = 0; j < l; j++)
01162 {
01163 *(localRaw++) = *(a++);
01164 *(localRaw++) = *(b++);
01165 *(localRaw++) = *(c++);
01166 }
01167 delete[] copyRaw;
01168 }
01169
01174 void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
01175 {
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185 gdcmWarningMacro("--> ConvertYcBcRPlanesToRGBPixels");
01186
01187 uint8_t *localRaw = Raw;
01188 uint8_t *copyRaw = new uint8_t[ RawSize ];
01189 memmove( copyRaw, localRaw, RawSize );
01190
01191
01192
01193
01194
01195
01196 int l = XSize * YSize;
01197 int nbFrames = ZSize;
01198
01199 uint8_t *a = copyRaw + 0;
01200 uint8_t *b = copyRaw + l;
01201 uint8_t *c = copyRaw + l+ l;
01202 int32_t R, G, B;
01203
01209
01210 for ( int i = 0; i < nbFrames; i++ )
01211 {
01212 for ( int j = 0; j < l; j++ )
01213 {
01214 R = 38142 *(*a-16) + 52298 *(*c -128);
01215 G = 38142 *(*a-16) - 26640 *(*c -128) - 12845 *(*b -128);
01216 B = 38142 *(*a-16) + 66093 *(*b -128);
01217
01218 R = (R+16384)>>15;
01219 G = (G+16384)>>15;
01220 B = (B+16384)>>15;
01221
01222 if (R < 0) R = 0;
01223 if (G < 0) G = 0;
01224 if (B < 0) B = 0;
01225 if (R > 255) R = 255;
01226 if (G > 255) G = 255;
01227 if (B > 255) B = 255;
01228
01229 *(localRaw++) = (uint8_t)R;
01230 *(localRaw++) = (uint8_t)G;
01231 *(localRaw++) = (uint8_t)B;
01232 a++;
01233 b++;
01234 c++;
01235 }
01236 }
01237 delete[] copyRaw;
01238 }
01239
01244
01245 void PixelReadConvert::ConvertHandleColor()
01246 {
01248
01249
01250
01251
01252
01253
01254
01255
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 gdcmDebugMacro("--> ConvertHandleColor "
01286 << "Planar Configuration " << PlanarConfiguration );
01287
01288 if ( ! IsRawRGB() )
01289 {
01290
01291 gdcmDebugMacro("--> RawRGB : LUT intervention done outside");
01292 return;
01293 }
01294
01295 if ( PlanarConfiguration == 1 )
01296 {
01297 if ( IsYBRFull )
01298 {
01299
01300 gdcmDebugMacro("--> YBRFull");
01301 ConvertYcBcRPlanesToRGBPixels();
01302 }
01303 else
01304 {
01305
01306 gdcmDebugMacro("--> YBRFull");
01307 ConvertRGBPlanesToRGBPixels();
01308 }
01309 return;
01310 }
01311
01312
01313
01314
01315 if (IsRLELossless)
01316 {
01317 gdcmDebugMacro("--> RLE Lossless");
01318 ConvertRGBPlanesToRGBPixels();
01319 }
01320
01321
01322 }
01323
01325 void PixelReadConvert::ComputeRawAndRGBSizes()
01326 {
01327 int bitsAllocated = BitsAllocated;
01328
01329
01330
01331 if ( BitsAllocated == 12 )
01332 {
01333 bitsAllocated = 16;
01334 }
01335
01336 RawSize = XSize * YSize * ZSize
01337 * ( bitsAllocated / 8 )
01338 * SamplesPerPixel;
01339 if ( HasLUT )
01340 {
01341 RGBSize = 3 * RawSize;
01342 }
01343 else
01344 {
01345 RGBSize = RawSize;
01346 }
01347 }
01348
01350 void PixelReadConvert::AllocateRGB()
01351 {
01352 if ( RGB )
01353 delete [] RGB;
01354 RGB = new uint8_t[RGBSize];
01355 }
01356
01358 void PixelReadConvert::AllocateRaw()
01359 {
01360 if ( Raw )
01361 delete [] Raw;
01362 Raw = new uint8_t[RawSize];
01363 }
01364
01365
01366
01372 void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
01373 {
01374 os << indent
01375 << "--- Pixel information -------------------------"
01376 << std::endl;
01377 os << indent
01378 << "Pixel Data: offset " << PixelOffset
01379 << " x(" << std::hex << PixelOffset << std::dec
01380 << ") length " << PixelDataLength
01381 << " x(" << std::hex << PixelDataLength << std::dec
01382 << ")" << std::endl;
01383
01384 if ( IsRLELossless )
01385 {
01386 if ( RLEInfo )
01387 {
01388 RLEInfo->Print( os, indent );
01389 }
01390 else
01391 {
01392 gdcmWarningMacro("Set as RLE file but NO RLEinfo present.");
01393 }
01394 }
01395
01396 if ( IsJPEG2000 || IsJPEGLossless || IsJPEGLossy || IsJPEGLS )
01397 {
01398 if ( JPEGInfo )
01399 {
01400 JPEGInfo->Print( os, indent );
01401 }
01402 else
01403 {
01404 gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present.");
01405 }
01406 }
01407 }
01408
01412 void PixelReadConvert::CallStartMethod()
01413 {
01414 Progress = 0.0f;
01415 Abort = false;
01416 CommandManager::ExecuteCommand(FH,CMD_STARTPROGRESS);
01417 }
01418
01422 void PixelReadConvert::CallProgressMethod()
01423 {
01424 CommandManager::ExecuteCommand(FH,CMD_PROGRESS);
01425 }
01426
01430 void PixelReadConvert::CallEndMethod()
01431 {
01432 Progress = 1.0f;
01433 CommandManager::ExecuteCommand(FH,CMD_ENDPROGRESS);
01434 }
01435
01436
01437
01438 }
01439
01440
01441
01442
01443
01444
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