gdcm::ImageReader ir;
ir.SetFileName(file.c_str());
if (!ir.Read()) return 1;
const gdcm::Image& gimage = ir.GetImage();
const gdcm::TransferSyntax syntax = gimage.GetTransferSyntax();
if (gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGBaselineProcess1
|| gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGExtendedProcess2_4
|| gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGLosslessProcess14_1)
{//decompress
//https://stackoverflow.com/questions/27936473/using-gdcm-grassroots-dicom-to-decompress-dicom-image-data
unsigned int numFrames = gimage.GetDimension(2);
const gdcm::Tag rawTag(0x7fe0, 0x0010); // Default to Pixel Data
//const gdcm::Tag rawTag(0xFFFE, 0xE000);
const gdcm::DataElement& pdde = ir.GetFile().GetDataSet().GetDataElement(rawTag);
const gdcm::SequenceOfFragments* sequent = pdde.GetSequenceOfFragments();//图像序列
numFrames = sequent->GetNumberOfFragments();
for (int i = 0; i < sequent->GetNumberOfFragments(); i++)
{
gdcm::Fragment frag = sequent->GetFragment(i);
const gdcm::ByteValue* bv = frag.GetByteValue();
if (!bv) return 1;
unsigned long totalLen = bv->GetLength();
std::vector<char> vbuffer;
vbuffer.resize(totalLen);
char* buffer = &vbuffer[0];
bv->GetBuffer(buffer, totalLen);
//ir.GetFile().GetHeader();
const double* origin = gimage.GetOrigin();
//gdcm::PixmapWriter writer;
//gdcm::Image newmap = writer.GetImage();
gdcm::Image* newmap = new gdcm::Image;
newmap->SetNumberOfDimensions(2);
//GetDimensions
uint px = gimage.GetDimension(0);
uint py = gimage.GetDimension(1);
newmap->SetDimension(0, px);
newmap->SetDimension(1, py);
newmap->SetTransferSyntax(syntax);
gdcm::DataElement pixeldata(gdcm::Tag(0x7fe0, 0x0010));
pixeldata.SetByteValue(buffer, totalLen);
newmap->SetDataElement(pixeldata);
gdcm::PixelFormat pf = gimage.GetPixelFormat();
newmap->SetPixelFormat(pf);
//PixelData->GetPhotometricInterpretation(
/* PhotometricInterpretation pi = PixelData->GetPhotometricInterpretation();
if( pi.GetSamplesPerPixel() != pf.GetSamplesPerPixel() )
{
gdcmWarningMacro( "Photometric Interpretation and Pixel format are not compatible: "
<< pi.GetSamplesPerPixel() << " vs " << pf.GetSamplesPerPixel() );
return false;
}*/
//dicompress
gdcm::ImageChangeTransferSyntax* change = new gdcm::ImageChangeTransferSyntax();
gdcm::TransferSyntax newsyntax(gdcm::TransferSyntax::ImplicitVRLittleEndian);
change->SetTransferSyntax(newsyntax);
change->SetInput(*newmap);
bool isok = change->Change();
//gdcm::Bitmap map = change->GetOutputAsBitmap();
gdcm::Image image = change->GetOutput();
int len = image.GetBufferLength();
image.SetIntercept(gimage.GetIntercept());
image.SetSlope(gimage.GetSlope());
image.SetPhotometricInterpretation(gimage.GetPhotometricInterpretation());
/*gdcm::PhotometricInterpretation pi = image.GetPhotometricInterpretation();
gdcm::PixelFormat pf = image.GetPixelFormat();
ushort temp1 = pi.GetSamplesPerPixel();
ushort temp2 = pf.GetSamplesPerPixel();
if (pi.GetSamplesPerPixel() != pf.GetSamplesPerPixel())
{
gdcmWarningMacro("Photometric Interpretation and Pixel format are not compatible: "
<< pi.GetSamplesPerPixel() << " vs " << pf.GetSamplesPerPixel());
return false;
}*/
gdcm::DataElement data;
data = image.GetDataElement();
//ir.GetFile().GetDataSet().Remove(gdcm::Tag(0xFFFE, 0xE000));
const double* spacing = gimage.GetSpacing();
image.SetSpacing(0, spacing[0]);
image.SetSpacing(1, spacing[1]);
image.SetSpacing(2, spacing[2]);
//gdcm::DataSet::SizeType type = ir.GetFile().GetDataSet().Remove(gdcm::Tag(0x2001,0x9000));//(0xFFFE, 0xE000));
const gdcm::Image* img2 = new gdcm::Image(image);
gdcm::ImageWriter* writer = new gdcm::ImageWriter();
gdcm::DataElement sopuid = ir.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x0008, 0x0018));
char uid[100];
dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
sopuid.SetByteValue(uid, strlen(uid));
const gdcm::ByteValue* bv2 = sopuid.GetByteValue();
std::string dup(bv2->GetPointer(), bv2->GetLength());
writer->SetFile(ir.GetFile());
writer->SetImage(*img2);
//std::string filename = outpath.c_str();
char buff3[256] = {0};
sprintf(buff3, "%s%s%d.dcm", outpath.c_str(), imstr.c_str(),i);
writer->SetFileName(buff3);
writer->GetFile().GetDataSet().Remove(gdcm::Tag(0x0008, 0x0018));
writer->GetFile().GetDataSet().Insert(sopuid);
if (!writer->Write())
{
break;
}
}
ir.SetFileName(file.c_str());
if (!ir.Read()) return 1;
const gdcm::Image& gimage = ir.GetImage();
const gdcm::TransferSyntax syntax = gimage.GetTransferSyntax();
if (gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGBaselineProcess1
|| gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGExtendedProcess2_4
|| gimage.GetTransferSyntax() == gdcm::TransferSyntax::JPEGLosslessProcess14_1)
{//decompress
//https://stackoverflow.com/questions/27936473/using-gdcm-grassroots-dicom-to-decompress-dicom-image-data
unsigned int numFrames = gimage.GetDimension(2);
const gdcm::Tag rawTag(0x7fe0, 0x0010); // Default to Pixel Data
//const gdcm::Tag rawTag(0xFFFE, 0xE000);
const gdcm::DataElement& pdde = ir.GetFile().GetDataSet().GetDataElement(rawTag);
const gdcm::SequenceOfFragments* sequent = pdde.GetSequenceOfFragments();//图像序列
numFrames = sequent->GetNumberOfFragments();
for (int i = 0; i < sequent->GetNumberOfFragments(); i++)
{
gdcm::Fragment frag = sequent->GetFragment(i);
const gdcm::ByteValue* bv = frag.GetByteValue();
if (!bv) return 1;
unsigned long totalLen = bv->GetLength();
std::vector<char> vbuffer;
vbuffer.resize(totalLen);
char* buffer = &vbuffer[0];
bv->GetBuffer(buffer, totalLen);
//ir.GetFile().GetHeader();
const double* origin = gimage.GetOrigin();
//gdcm::PixmapWriter writer;
//gdcm::Image newmap = writer.GetImage();
gdcm::Image* newmap = new gdcm::Image;
newmap->SetNumberOfDimensions(2);
//GetDimensions
uint px = gimage.GetDimension(0);
uint py = gimage.GetDimension(1);
newmap->SetDimension(0, px);
newmap->SetDimension(1, py);
newmap->SetTransferSyntax(syntax);
gdcm::DataElement pixeldata(gdcm::Tag(0x7fe0, 0x0010));
pixeldata.SetByteValue(buffer, totalLen);
newmap->SetDataElement(pixeldata);
gdcm::PixelFormat pf = gimage.GetPixelFormat();
newmap->SetPixelFormat(pf);
//PixelData->GetPhotometricInterpretation(
/* PhotometricInterpretation pi = PixelData->GetPhotometricInterpretation();
if( pi.GetSamplesPerPixel() != pf.GetSamplesPerPixel() )
{
gdcmWarningMacro( "Photometric Interpretation and Pixel format are not compatible: "
<< pi.GetSamplesPerPixel() << " vs " << pf.GetSamplesPerPixel() );
return false;
}*/
//dicompress
gdcm::ImageChangeTransferSyntax* change = new gdcm::ImageChangeTransferSyntax();
gdcm::TransferSyntax newsyntax(gdcm::TransferSyntax::ImplicitVRLittleEndian);
change->SetTransferSyntax(newsyntax);
change->SetInput(*newmap);
bool isok = change->Change();
//gdcm::Bitmap map = change->GetOutputAsBitmap();
gdcm::Image image = change->GetOutput();
int len = image.GetBufferLength();
image.SetIntercept(gimage.GetIntercept());
image.SetSlope(gimage.GetSlope());
image.SetPhotometricInterpretation(gimage.GetPhotometricInterpretation());
/*gdcm::PhotometricInterpretation pi = image.GetPhotometricInterpretation();
gdcm::PixelFormat pf = image.GetPixelFormat();
ushort temp1 = pi.GetSamplesPerPixel();
ushort temp2 = pf.GetSamplesPerPixel();
if (pi.GetSamplesPerPixel() != pf.GetSamplesPerPixel())
{
gdcmWarningMacro("Photometric Interpretation and Pixel format are not compatible: "
<< pi.GetSamplesPerPixel() << " vs " << pf.GetSamplesPerPixel());
return false;
}*/
gdcm::DataElement data;
data = image.GetDataElement();
//ir.GetFile().GetDataSet().Remove(gdcm::Tag(0xFFFE, 0xE000));
const double* spacing = gimage.GetSpacing();
image.SetSpacing(0, spacing[0]);
image.SetSpacing(1, spacing[1]);
image.SetSpacing(2, spacing[2]);
//gdcm::DataSet::SizeType type = ir.GetFile().GetDataSet().Remove(gdcm::Tag(0x2001,0x9000));//(0xFFFE, 0xE000));
const gdcm::Image* img2 = new gdcm::Image(image);
gdcm::ImageWriter* writer = new gdcm::ImageWriter();
gdcm::DataElement sopuid = ir.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x0008, 0x0018));
char uid[100];
dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
sopuid.SetByteValue(uid, strlen(uid));
const gdcm::ByteValue* bv2 = sopuid.GetByteValue();
std::string dup(bv2->GetPointer(), bv2->GetLength());
writer->SetFile(ir.GetFile());
writer->SetImage(*img2);
//std::string filename = outpath.c_str();
char buff3[256] = {0};
sprintf(buff3, "%s%s%d.dcm", outpath.c_str(), imstr.c_str(),i);
writer->SetFileName(buff3);
writer->GetFile().GetDataSet().Remove(gdcm::Tag(0x0008, 0x0018));
writer->GetFile().GetDataSet().Insert(sopuid);
if (!writer->Write())
{
break;
}
}