From: jpr Date: Fri, 24 Aug 2007 10:48:08 +0000 (+0000) Subject: Deal with JPEG/JPEG2000 writting X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=888439e26839d7963a380256453361bdbecd1826;p=gdcm.git Deal with JPEG/JPEG2000 writting --- diff --git a/Example/CMakeLists.txt b/Example/CMakeLists.txt index 2345d2b6..cebd96f5 100644 --- a/Example/CMakeLists.txt +++ b/Example/CMakeLists.txt @@ -57,8 +57,8 @@ SET(EXAMPLE_SOURCES WriteRead ## Troubles on Windows ! - #WriteDicomAsJPEG2000 - #WriteDicomAsJPEG + WriteDicomAsJPEG2000 + WriteDicomAsJPEG exCTPET #Slice diff --git a/Example/ReWrite.cxx b/Example/ReWrite.cxx index 442751b3..6e01d868 100644 --- a/Example/ReWrite.cxx +++ b/Example/ReWrite.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: ReWrite.cxx,v $ Language: C++ - Date: $Date: 2007/08/21 13:17:51 $ - Version: $Revision: 1.29 $ + Date: $Date: 2007/08/24 10:48:08 $ + Version: $Revision: 1.30 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) " [firstframe=beg] [lastframe=end] ", " ", " mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels) ", + " j (jpeg lossless), 2 (jpeg2000) ", " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ", " noshadowseq: user doesn't want to load Private Sequences ", " noshadow : user doesn't want to load Private groups (odd number)", @@ -49,7 +50,8 @@ int main(int argc, char *argv[]) " debug : developper wants to run the program in 'debug mode' ", FINISH_USAGE - // ----- Initialize Arguments Manager ------ + // ----- Initialize Arguments Manager ------ + GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv); if (argc == 1 || am->ArgMgrDefined("usage")) @@ -168,10 +170,10 @@ int main(int argc, char *argv[]) } GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f); - void *imageData; + uint8_t *imageData; int dataSize; - int nX,nY,nZ,sPP,planarConfig; + int nX,nY,nZ,sPP,planarConfig; std::string pixelType, transferSyntaxName; nX=f->GetXSize(); nY=f->GetYSize(); @@ -273,6 +275,87 @@ int main(int argc, char *argv[]) } } + +//------------------------------ Set the Writing mode --------------------------------- + + switch (mode[0]) + { + case 'A' : + case 'a' : + // Writting an ACR file + // from a full gdcm readable File + std::cout << "WriteACR" << std::endl; + fh->SetWriteTypeToAcr(); + break; + + case 'D' : // Not documented in the 'usage', because the method + case 'd' : // is known to be bugged. + // Writting a DICOM Implicit VR file + // from a full gdcm readable File + std::cout << "WriteDCM Implicit VR" << std::endl; + fh->SetWriteTypeToDcmImplVR(); + break; + + case 'X' : + case 'x' : + // writting a DICOM Explicit VR + // from a full gdcm readable File + std::cout << "WriteDCM Explicit VR" << std::endl; + // fh->WriteDcmExplVR(outputFileName); + // Try this one : + fh->SetWriteTypeToDcmExplVR(); + + break; + + case 'R' : + case 'r' : + // Writting a Raw File, + std::cout << "WriteRaw" << std::endl; + fh->WriteRawData(outputFileName); + break; + + case 'J' : + case 'j' : + // writting a DICOM Jpeg Lossless + // from a full gdcm readable File + std::cout << "WriteDCM Jpeg Lossless" << std::endl; + fh->SetWriteTypeToJPEG(); + break; + + case '2' : + // writting a DICOM Jpeg 2000 + // from a full gdcm readable File + std::cout << "WriteDCM Jpeg 2000" << std::endl; + fh->SetWriteTypeToJPEG2000(); + break; + + // Just for fun : + // Write a 'Video inverse' version of the file. + // *Not* described, on purpose, in the USAGE + case 'V' : + case 'v' : + if ( fh->GetFile()->GetBitsAllocated() == 8) + { + std::cout << "videoinv for 8 bits" << std::endl; + for (int i=0; iSetWriteTypeToDcmExplVR(); + break; + } + + // // user wants to keep only a part of the image (ROI, and/or some frames) // --------------------------------------------------------------------- @@ -357,7 +440,7 @@ int main(int argc, char *argv[]) { for (int lineNb=roiBoundVal[2], lineCount=0; lineNb<=roiBoundVal[3]; lineNb++, lineCount++) { - /// \todo increment data pointer, don't multiply so much! + /// \todo : increment data pointer, don't multiply so much! memcpy( (void *)(destCopy + frameCount*lgrSubFrame + lineCount*lgrSubLine), (void *)(srcCopy + frameNb*frameSize + lineNb*lineSize + lineOffset ), lgrSubLine); @@ -376,85 +459,20 @@ int main(int argc, char *argv[]) str << end-beg+1; fh->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames - fh->SetImageData(subImage,lgrSubImage); - - } - - -//----------------------------------- Write, now! --------------------------------- + //fh->SetImageData(subImage,lgrSubImage); + fh->SetUserData(subImage,lgrSubImage); // ensures the compression (if any) + } + else + { + fh->SetUserData(imageData,dataSize); // ensures the compression (if any) + } - switch (mode[0]) - { - case 'A' : - case 'a' : - // Writting an ACR file - // from a full gdcm readable File - std::cout << "WriteACR" << std::endl; - fh->WriteAcr(outputFileName); - break; - case 'D' : // Not documented in the 'usage', because the method - case 'd' : // is known to be bugged. - // Writting a DICOM Implicit VR file - // from a full gdcm readable File - std::cout << "WriteDCM Implicit VR" << std::endl; - fh->WriteDcmImplVR(outputFileName); - break; - case 'X' : - case 'x' : - // writting a DICOM Explicit VR - // from a full gdcm readable File - std::cout << "WriteDCM Explicit VR" << std::endl; - // fh->WriteDcmExplVR(outputFileName); - // Try this one : - fh->SetWriteTypeToDcmExplVR(); - fh->Write(outputFileName); - break; +//----------------------------------- Write, now! --------------------------------- - case 'R' : - case 'r' : - // Writting a Raw File, - std::cout << "WriteRaw" << std::endl; - fh->WriteRawData(outputFileName); - break; - - // UNDOCUMENT on purpose, because it's still bugged :-( - case 'J' : - case 'j' : - // writting a DICOM Jpeg Lossless - // from a full gdcm readable File - std::cout << "WriteDCM Jpeg Lossless" << std::endl; - //fh->SetWriteTypeToDcmExplVR(); - fh->SetWriteTypeToJPEG(); - fh->Write(outputFileName); - break; - - // Just for fun : - // Write a 'Video inverse' version of the file. - // *Not* described, on purpose, in the USAGE - case 'V' : - case 'v' : - if ( fh->GetFile()->GetBitsAllocated() == 8) - { - std::cout << "videoinv for 8 bits" << std::endl; - for (int i=0; iWriteDcmExplVR(outputFileName); - break; - } + if (mode[0] != 'R' && mode[0] != 'r') + fh->Write(outputFileName); f->Delete(); fh->Delete(); diff --git a/Example/WriteDicomAsJPEG.cxx b/Example/WriteDicomAsJPEG.cxx index c2d74a02..b6187943 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/21 15:10:04 $ - Version: $Revision: 1.15 $ + Date: $Date: 2007/08/24 10:48:08 $ + Version: $Revision: 1.16 $ 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 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 ); @@ -49,15 +54,16 @@ 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() ) + 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; @@ -69,13 +75,13 @@ int main(int argc, char *argv[]) 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(); @@ -109,14 +115,19 @@ int main(int argc, char *argv[]) * samplesPerPixel * bitsallocated / 8; GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild); + assert( size == testedDataSize ); fileH->SetWriteTypeToJPEG( ); - fileH->SetImageData(testedImageData, testedDataSize); + + //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(); diff --git a/Example/WriteDicomAsJPEG2000.cxx b/Example/WriteDicomAsJPEG2000.cxx index e60a9b4c..ec9e206e 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/07/13 08:17:20 $ - Version: $Revision: 1.5 $ + Date: $Date: 2007/08/24 10:48:08 $ + Version: $Revision: 1.6 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -106,7 +106,8 @@ int main(int argc, char *argv[]) // Step 2 : Create the output image GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild); fileH->SetWriteTypeToJPEG2000( ); - fileH->SetImageData(testedImageData, testedDataSize); + //fileH->SetImageData(testedImageData, testedDataSize); + fileH->SetUserData(testedImageData, testedDataSize); if( !fileH->Write(outfilename) ) { std::cerr << "write fails" << std::endl;