From 5933681dcd56380dd0f7a1477fc1fb3246be8809 Mon Sep 17 00:00:00 2001 From: jpr Date: Tue, 28 Aug 2007 09:40:19 +0000 Subject: [PATCH] Unify WriteDicomAsJPEG2000.cxx and WriteDicomAsJPEG.cxx --- Example/WriteDicomAsJPEG.cxx | 23 +++++++----- Example/WriteDicomAsJPEG2000.cxx | 60 +++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/Example/WriteDicomAsJPEG.cxx b/Example/WriteDicomAsJPEG.cxx index b6187943..0448b603 100644 --- a/Example/WriteDicomAsJPEG.cxx +++ b/Example/WriteDicomAsJPEG.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: WriteDicomAsJPEG.cxx,v $ Language: C++ - Date: $Date: 2007/08/24 10:48:08 $ - Version: $Revision: 1.16 $ + Date: $Date: 2007/08/28 09:40:19 $ + Version: $Revision: 1.17 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -54,10 +54,10 @@ int main(int argc, char *argv[]) int xsize = f->GetXSize(); int ysize = f->GetYSize(); int zsize = f->GetZSize(); + //tested->Print( std::cout ); int samplesPerPixel = f->GetSamplesPerPixel(); size_t testedDataSize = tested->GetImageDataSize(); - std::cerr << "testedDataSize:" << testedDataSize << std::endl; uint8_t *testedImageData = tested->GetImageData(); if( GDCM_NAME_SPACE::Debug::GetDebugFlag() ) @@ -74,17 +74,17 @@ int main(int argc, char *argv[]) str.str(""); str << ysize; fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows - + if(zsize>1) { str.str(""); str << zsize; fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames } - + int bitsallocated = f->GetBitsAllocated(); - int bitsstored = f->GetBitsStored(); - int highbit = f->GetHighBitPosition(); + int bitsstored = f->GetBitsStored(); + int highbit = f->GetHighBitPosition(); //std::string pixtype = f->GetPixelType(); int sign = f->IsSignedPixelData(); @@ -110,6 +110,12 @@ int main(int argc, char *argv[]) str << samplesPerPixel; //img.components; fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel + // Set the Pixel Aspect Ratio + std::string par = f->GetEntryString(0x0028,0x0034); + std::cerr <<"Pixel Aspect Ratio [" << par << "]" << std::endl; + if ( par != GDCM_NAME_SPACE::GDCM_UNFOUND ) + fileToBuild->InsertEntryString(par,0x0028,0x0034,"IS"); // Pixel Aspect Ratio + // Step 2 : Create the output image size_t size = xsize * ysize * zsize * samplesPerPixel * bitsallocated / 8; @@ -118,9 +124,8 @@ int main(int argc, char *argv[]) assert( size == testedDataSize ); fileH->SetWriteTypeToJPEG( ); - //fileH->SetImageData(testedImageData, testedDataSize); - + // SetUserData will ensure the compression fileH->SetUserData(testedImageData, testedDataSize); if( !fileH->Write(outfilename) ) diff --git a/Example/WriteDicomAsJPEG2000.cxx b/Example/WriteDicomAsJPEG2000.cxx index ec9e206e..41640e09 100644 --- a/Example/WriteDicomAsJPEG2000.cxx +++ b/Example/WriteDicomAsJPEG2000.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: WriteDicomAsJPEG2000.cxx,v $ Language: C++ - Date: $Date: 2007/08/24 10:48:08 $ - Version: $Revision: 1.6 $ + Date: $Date: 2007/08/28 09:40:19 $ + Version: $Revision: 1.7 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -19,13 +19,15 @@ #include "gdcmFile.h" #include "gdcmFileHelper.h" #include "gdcmUtil.h" +#include "gdcmDebug.h" -// Open a dicom file and compress it as JPEG 2000 stream +// Open a dicom file and compress it as JPEG2000 stream int main(int argc, char *argv[]) { if( argc < 2) { - std::cerr << argv[0] << " inputfilename.dcm\n"; + std::cerr << argv[0] << " inputfilename.dcm [ outputfilename.dcm" + << "quality debug]\n"; return 1; } @@ -37,8 +39,11 @@ int main(int argc, char *argv[]) if( argc >= 4 ) quality = atoi(argv[3]); std::cerr << "Using quality: " << quality << std::endl; - -// Step 1 : Create the header of the image + + if (argc > 4) + GDCM_NAME_SPACE::Debug::DebugOn(); + +// Step 1 : Read the image GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New(); f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything f->SetFileName( filename ); @@ -54,65 +59,80 @@ int main(int argc, char *argv[]) int samplesPerPixel = f->GetSamplesPerPixel(); size_t testedDataSize = tested->GetImageDataSize(); uint8_t *testedImageData = tested->GetImageData(); + + if( GDCM_NAME_SPACE::Debug::GetDebugFlag() ) + tested->Print( std::cout ); -// Step 1 : Create the header of the image - +// Step 1 : Create the header of the new file GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New(); std::ostringstream str; // Set the image size str.str(""); str << xsize; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns + fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns str.str(""); str << ysize; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows + fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows if(zsize>1) { str.str(""); str << zsize; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames + fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames } + int bitsallocated = f->GetBitsAllocated(); - int bitsstored = f->GetBitsStored(); - int highbit = f->GetHighBitPosition(); + int bitsstored = f->GetBitsStored(); + int highbit = f->GetHighBitPosition(); //std::string pixtype = f->GetPixelType(); int sign = f->IsSignedPixelData(); // Set the pixel type str.str(""); str << bitsallocated; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated - + fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US");// Bits Allocated str.str(""); str << bitsstored; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored + fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored str.str(""); str << highbit; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit + fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit // Set the pixel representation str.str(""); str << sign; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation + fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation // Set the samples per pixel str.str(""); str << samplesPerPixel; //img.components; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel + fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel + + // Set the Pixel Aspect Ratio + std::string par = f->GetEntryString(0x0028,0x0034); + std::cerr <<"Pixel Aspect Ratio [" << par << "]" << std::endl; + if ( par != GDCM_NAME_SPACE::GDCM_UNFOUND ) + fileToBuild->InsertEntryString(par,0x0028,0x0034,"IS"); // Pixel Aspect Ratio // Step 2 : Create the output image + size_t size = xsize * ysize * zsize + * samplesPerPixel * bitsallocated / 8; + GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild); + + assert( size == testedDataSize ); fileH->SetWriteTypeToJPEG2000( ); //fileH->SetImageData(testedImageData, testedDataSize); + + // SetUserData will ensure the compression fileH->SetUserData(testedImageData, testedDataSize); if( !fileH->Write(outfilename) ) { std::cerr << "write fails" << std::endl; } - + f->Delete(); tested->Delete(); fileToBuild->Delete(); -- 2.48.1