📄 gdcmfilehelper.cxx
字号:
{
case WMODE_RAW :
SetWriteToRaw(); // modifies and pushes to the archive, when necessary
break;
case WMODE_RGB :
SetWriteToRGB(); // modifies and pushes to the archive, when necessary
break;
}
bool check = CheckWriteIntegrity(); // verifies length
if (WriteType == JPEG || WriteType == JPEG2000 ) check = true;
if (check)
{
check = FileInternal->Write(fileName,WriteType);
}
RestoreWrite();
RestoreWriteFileType();
RestoreWriteMandatory();
// --------------------------------------------------------------
// Special Patch to allow gdcm to re-write ACR-LibIDO formated images
//
// ...and we restore the header to be Dicom Compliant again
// just after writting
RestoreWriteOfLibido();
// ----------------- End of Special Patch ----------------
return check;
}
//-----------------------------------------------------------------------------
// Protected
/**
* \brief Checks the write integrity
*
* The tests made are :
* - verify the size of the image to write with the possible write
* when the user set an image data
* @return true if check is successfull
*/
bool FileHelper::CheckWriteIntegrity()
{
if ( PixelWriteConverter->GetUserData() )
{
int numberBitsAllocated = FileInternal->GetBitsAllocated();
if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
{
gdcmWarningMacro( "numberBitsAllocated changed from "
<< numberBitsAllocated << " to 16 "
<< " for consistency purpose" );
numberBitsAllocated = 16;
}
size_t decSize = FileInternal->GetXSize()
* FileInternal->GetYSize()
* FileInternal->GetZSize()
* FileInternal->GetSamplesPerPixel()
* ( numberBitsAllocated / 8 );
size_t rgbSize = decSize;
if ( FileInternal->HasLUT() )
rgbSize = decSize * 3;
switch(WriteMode)
{
case WMODE_RAW :
if ( decSize!=PixelWriteConverter->GetUserDataSize() )
{
gdcmWarningMacro( "Data size (Raw) is incorrect. Should be "
<< decSize << " / Found :"
<< PixelWriteConverter->GetUserDataSize() );
return false;
}
break;
case WMODE_RGB :
if ( rgbSize!=PixelWriteConverter->GetUserDataSize() )
{
gdcmWarningMacro( "Data size (RGB) is incorrect. Should be "
<< decSize << " / Found "
<< PixelWriteConverter->GetUserDataSize() );
return false;
}
break;
}
}
return true;
}
size_t FileHelper::ComputeExpectedImageDataSize()
{
int numberBitsAllocated = FileInternal->GetBitsAllocated();
if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
{
gdcmWarningMacro( "numberBitsAllocated changed from "
<< numberBitsAllocated << " to 16 "
<< " for consistency purpose" );
numberBitsAllocated = 16;
}
size_t decSize = FileInternal->GetXSize()
* FileInternal->GetYSize()
* FileInternal->GetZSize()
* FileInternal->GetSamplesPerPixel()
* ( numberBitsAllocated / 8 );
size_t rgbSize = decSize;
if ( FileInternal->HasLUT() )
rgbSize = decSize * 3;
switch(WriteMode)
{
case WMODE_RAW :
return decSize;
case WMODE_RGB :
return rgbSize;
}
return 0;
}
/**
* \brief Updates the File to write RAW data (as opposed to RGB data)
* (modifies, when necessary, photochromatic interpretation,
* bits allocated, Pixels element VR)
*/
void FileHelper::SetWriteToRaw()
{
if ( FileInternal->GetNumberOfScalarComponents() == 3
&& !FileInternal->HasLUT() )
{
SetWriteToRGB();
}
else
{
ValEntry *photInt = CopyValEntry(0x0028,0x0004);
if (FileInternal->HasLUT() )
{
photInt->SetValue("PALETTE COLOR ");
}
else
{
photInt->SetValue("MONOCHROME2 ");
}
PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
PixelReadConverter->GetRawSize());
std::string vr = "OB";
if ( FileInternal->GetBitsAllocated()>8 )
vr = "OW";
if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
vr = "OB";
BinEntry *pixel =
CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
pixel->SetValue(GDCM_BINLOADED);
pixel->SetBinArea(PixelWriteConverter->GetData(),false);
pixel->SetLength(
static_cast< uint32_t >( PixelWriteConverter->GetDataSize()) );
Archive->Push(photInt);
Archive->Push(pixel);
}
}
/**
* \brief Updates the File to write RGB data (as opposed to RAW data)
* (modifies, when necessary, photochromatic interpretation,
* samples per pixel, Planar configuration,
* bits allocated, bits stored, high bit -ACR 24 bits-
* Pixels element VR, pushes out the LUT, )
*/
void FileHelper::SetWriteToRGB()
{
if ( FileInternal->GetNumberOfScalarComponents()==3 )
{
PixelReadConverter->BuildRGBImage();
ValEntry *spp = CopyValEntry(0x0028,0x0002);
spp->SetValue("3 ");
ValEntry *planConfig = CopyValEntry(0x0028,0x0006);
planConfig->SetValue("0 ");
ValEntry *photInt = CopyValEntry(0x0028,0x0004);
photInt->SetValue("RGB ");
if ( PixelReadConverter->GetRGB() )
{
PixelWriteConverter->SetReadData(PixelReadConverter->GetRGB(),
PixelReadConverter->GetRGBSize());
}
else // Raw data
{
PixelWriteConverter->SetReadData(PixelReadConverter->GetRaw(),
PixelReadConverter->GetRawSize());
}
std::string vr = "OB";
if ( FileInternal->GetBitsAllocated()>8 )
vr = "OW";
if ( FileInternal->GetBitsAllocated()==24 ) // For RGB ACR files
vr = "OB";
BinEntry *pixel =
CopyBinEntry(GetFile()->GetGrPixel(),GetFile()->GetNumPixel(),vr);
pixel->SetValue(GDCM_BINLOADED);
pixel->SetBinArea(PixelWriteConverter->GetData(),false);
pixel->SetLength(
static_cast< uint32_t >( PixelWriteConverter->GetDataSize()) );
Archive->Push(spp);
Archive->Push(planConfig);
Archive->Push(photInt);
Archive->Push(pixel);
// Remove any LUT
Archive->Push(0x0028,0x1101);
Archive->Push(0x0028,0x1102);
Archive->Push(0x0028,0x1103);
Archive->Push(0x0028,0x1201);
Archive->Push(0x0028,0x1202);
Archive->Push(0x0028,0x1203);
// push out Palette Color Lookup Table UID, if any
Archive->Push(0x0028,0x1199);
// For old '24 Bits' ACR-NEMA
// Thus, we have a RGB image and the bits allocated = 24 and
// samples per pixels = 1 (in the read file)
if ( FileInternal->GetBitsAllocated()==24 )
{
ValEntry *bitsAlloc = CopyValEntry(0x0028,0x0100);
bitsAlloc->SetValue("8 ");
ValEntry *bitsStored = CopyValEntry(0x0028,0x0101);
bitsStored->SetValue("8 ");
ValEntry *highBit = CopyValEntry(0x0028,0x0102);
highBit->SetValue("7 ");
Archive->Push(bitsAlloc);
Archive->Push(bitsStored);
Archive->Push(highBit);
}
}
else
{
SetWriteToRaw();
}
}
/**
* \brief Restore the File write mode
*/
void FileHelper::RestoreWrite()
{
Archive->Restore(0x0028,0x0002);
Archive->Restore(0x0028,0x0004);
Archive->Restore(0x0028,0x0006);
Archive->Restore(GetFile()->GetGrPixel(),GetFile()->GetNumPixel());
// For old ACR-NEMA (24 bits problem)
Archive->Restore(0x0028,0x0100);
Archive->Restore(0x0028,0x0101);
Archive->Restore(0x0028,0x0102);
// For the LUT
Archive->Restore(0x0028,0x1101);
Archive->Restore(0x0028,0x1102);
Archive->Restore(0x0028,0x1103);
Archive->Restore(0x0028,0x1201);
Archive->Restore(0x0028,0x1202);
Archive->Restore(0x0028,0x1203);
// For the Palette Color Lookup Table UID
Archive->Restore(0x0028,0x1203);
// group 0002 may be pushed out for ACR-NEMA writting purposes
Archive->Restore(0x0002,0x0000);
Archive->Restore(0x0002,0x0001);
Archive->Restore(0x0002,0x0002);
Archive->Restore(0x0002,0x0003);
Archive->Restore(0x0002,0x0010);
Archive->Restore(0x0002,0x0012);
Archive->Restore(0x0002,0x0013);
Archive->Restore(0x0002,0x0016);
Archive->Restore(0x0002,0x0100);
Archive->Restore(0x0002,0x0102);
}
/**
* \brief Pushes out the whole group 0002
* FIXME : better, set a flag to tell the writer not to write it ...
* FIXME : method should probably have an other name !
* SetWriteFileTypeToACR is NOT opposed to
* SetWriteFileTypeToExplicitVR and SetWriteFileTypeToImplicitVR
*/
void FileHelper::SetWriteFileTypeToACR()
{
Archive->Push(0x0002,0x0000);
Archive->Push(0x0002,0x0001);
Archive->Push(0x0002,0x0002);
Archive->Push(0x0002,0x0003);
Archive->Push(0x0002,0x0010);
Archive->Push(0x0002,0x0012);
Archive->Push(0x0002,0x0013);
Archive->Push(0x0002,0x0016);
Archive->Push(0x0002,0x0100);
Archive->Push(0x0002,0x0102);
}
/**
* \brief Sets in the File the TransferSyntax to 'JPEG2000'
*/
void FileHelper::SetWriteFileTypeToJPEG2000()
{
std::string ts = Util::DicomString(
Global::GetTS()->GetSpecialTransferSyntax(TS::JPEG2000Lossless) );
ValEntry *tss = CopyValEntry(0x0002,0x0010);
tss->SetValue(ts);
Archive->Push(tss);
}
/**
* \brief Sets in the File the TransferSyntax to 'JPEG'
*/
void FileHelper::SetWriteFileTypeToJPEG()
{
std::string ts = Util::DicomString(
Global::GetTS()->GetSpecialTransferSyntax(TS::JPEGLosslessProcess14_1) );
ValEntry *tss = CopyValEntry(0x0002,0x0010);
tss->SetValue(ts);
Archive->Push(tss);
}
/**
* \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian"
*/
void FileHelper::SetWriteFileTypeToExplicitVR()
{
std::string ts = Util::DicomString(
Global::GetTS()->GetSpecialTransferSyntax(TS::ExplicitVRLittleEndian) );
ValEntry *tss = CopyValEntry(0x0002,0x0010);
tss->SetValue(ts);
Archive->Push(tss);
}
/**
* \brief Sets in the File the TransferSyntax to 'Implicit VR Little Endian"
*/
void FileHelper::SetWriteFileTypeToImplicitVR()
{
std::string ts = Util::DicomString(
Global::GetTS()->GetSpecialTransferSyntax(TS::ImplicitVRLittleEndian) );
ValEntry *tss = CopyValEntry(0x0002,0x0010);
tss->SetValue(ts);
Archive->Push(tss);
}
/**
* \brief Restore in the File the initial group 0002
*/
void FileHelper::RestoreWriteFileType()
{
}
/**
* \brief Set the Write not to Libido format
*/
void FileHelper::SetWriteToLibido()
{
ValEntry *oldRow = dynamic_cast<ValEntry *>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -