From 8b7ccc5d0086fd3df994c18e2883bc0e3f7d1984 Mon Sep 17 00:00:00 2001 From: malaterre Date: Tue, 18 Oct 2005 19:54:25 +0000 Subject: [PATCH] ENH: Getting real close to having JPEG support in gdcm... jpeg offset table still missing --- Example/WriteDicomAsJPEG.cxx | 29 +++++++++++++++-------------- src/gdcmDocEntry.cxx | 6 +++--- src/gdcmDocument.cxx | 7 ++++--- src/gdcmFile.cxx | 24 ++++++++++++++---------- src/gdcmFileHelper.cxx | 16 ++++++++++++++-- src/gdcmFileHelper.h | 5 +++-- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/Example/WriteDicomAsJPEG.cxx b/Example/WriteDicomAsJPEG.cxx index 934542a6..5e40401c 100644 --- a/Example/WriteDicomAsJPEG.cxx +++ b/Example/WriteDicomAsJPEG.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: WriteDicomAsJPEG.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 19:06:28 $ - Version: $Revision: 1.2 $ + Date: $Date: 2005/10/18 19:54:25 $ + Version: $Revision: 1.3 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -210,12 +210,6 @@ int main(int argc, char *argv[]) } std::streambuf* sb = of->rdbuf(); (void)sb; - //of->close(); - std::ofstream out("/tmp/jpeg2.jpg"); - //out.write( of->str(), of - out << of->str(); //faster than going through str() - //out.rdbuf( *sb ); - out.close(); @@ -265,10 +259,6 @@ int main(int argc, char *argv[]) str << samplesPerPixel; //img.components; fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel - str.str(""); - str << "1.2.840.10008.1.2.4.50"; - fileToBuild->InsertEntryString(str.str(),0x0002,0x0010); // Transfer Syntax UID - // Step 2 : Create the output image // std::cout << "2..."; // if( img.componentSize%8 > 0 ) @@ -278,14 +268,16 @@ int main(int argc, char *argv[]) size_t size = xsize * ysize * 1 /*Z*/ * samplesPerPixel /* * img.componentSize / 8*/; - //uint8_t *imageData = new uint8_t[size]; + uint8_t *imageData = new uint8_t[size]; gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild); //fileH->SetImageData(imageData,size); assert( size == testedDataSize ); size = of->str().size(); //size = sb->in_avail(); std::cerr << "Size JPEG:" << size << std::endl; - fileH->SetImageData((uint8_t*)of->str().c_str(), size); + //fileH->SetImageData((uint8_t*)of->str().c_str(), size); + memcpy(imageData, of->str().c_str(), size); + fileH->SetImageData(imageData, size); //str::string *s = of->str(); //fileH->SetWriteTypeToDcmExplVR(); fileH->SetWriteTypeToJPEG( ); @@ -294,6 +286,15 @@ int main(int argc, char *argv[]) { std::cerr << "Badddd" << std::endl; } + //of->close(); + std::ofstream out("/tmp/jpeg2.jpg"); + //out.write( of->str(), of + //out << of->str(); //rdbuf is faster than going through str() + out.write( (char*)imageData, size); + std::cerr << "JPEG marker is: " << imageData[6] << imageData[7] << + imageData[8] << imageData[9] << std::endl; + //out.rdbuf( *sb ); + out.close(); delete of; delete f; diff --git a/src/gdcmDocEntry.cxx b/src/gdcmDocEntry.cxx index 8c117fb0..a4011aa7 100644 --- a/src/gdcmDocEntry.cxx +++ b/src/gdcmDocEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntry.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:28 $ - Version: $Revision: 1.71 $ + Date: $Date: 2005/10/18 19:54:26 $ + Version: $Revision: 1.72 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -76,7 +76,7 @@ void DocEntry::WriteContent(std::ofstream *fp, FileType filetype) binary_write( *fp, el); //element number // Dicom V3 group 0x0002 is *always* Explicit VR ! - if ( filetype == ExplicitVR || group == 0x0002 ) + if ( filetype == ExplicitVR || filetype == JPEG || group == 0x0002 ) { // Special case of delimiters: if (group == 0xfffe) diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 958dfc88..6de51761 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 14:37:18 $ - Version: $Revision: 1.295 $ + Date: $Date: 2005/10/18 19:54:26 $ + Version: $Revision: 1.296 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -604,7 +604,8 @@ void Document::WriteContent(std::ofstream *fp, FileType filetype) { // Skip if user wants to write an ACR-NEMA file - if ( filetype == ImplicitVR || filetype == ExplicitVR ) + if ( filetype == ImplicitVR || filetype == ExplicitVR || + filetype == JPEG ) { // writing Dicom File Preamble char filePreamble[128]; diff --git a/src/gdcmFile.cxx b/src/gdcmFile.cxx index fe54402e..95d4867b 100644 --- a/src/gdcmFile.cxx +++ b/src/gdcmFile.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFile.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:28 $ - Version: $Revision: 1.277 $ + Date: $Date: 2005/10/18 19:54:26 $ + Version: $Revision: 1.278 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -1761,14 +1761,18 @@ bool File::Write(std::string fileName, FileType writetype) e0000->SetString(sLen.str()); } - int i_lgPix = GetEntryLength(GrPixel, NumPixel); - if (i_lgPix != -2) - { - // no (GrPixel, NumPixel) element - std::string s_lgPix = Util::Format("%d", i_lgPix+12); - s_lgPix = Util::DicomString( s_lgPix.c_str() ); - InsertEntryString(s_lgPix,GrPixel, 0x0000); - } + // Derma?.dcm does not have it...let's remove it FIXME FIXME + if( writetype != JPEG ) + { + int i_lgPix = GetEntryLength(GrPixel, NumPixel); + if (i_lgPix != -2) + { + // no (GrPixel, NumPixel) element + std::string s_lgPix = Util::Format("%d", i_lgPix+12); + s_lgPix = Util::DicomString( s_lgPix.c_str() ); + InsertEntryString(s_lgPix,GrPixel, 0x0000); + } + } Document::WriteContent(fp, writetype); diff --git a/src/gdcmFileHelper.cxx b/src/gdcmFileHelper.cxx index c4435ba4..98f023df 100644 --- a/src/gdcmFileHelper.cxx +++ b/src/gdcmFileHelper.cxx @@ -4,8 +4,8 @@ Module: $RCSfile: gdcmFileHelper.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 18:39:49 $ - Version: $Revision: 1.63 $ + Date: $Date: 2005/10/18 19:54:27 $ + Version: $Revision: 1.64 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -709,6 +709,7 @@ bool FileHelper::Write(std::string const &fileName) // SetWriteFileTypeToImplicitVR(); // ACR IS implicit VR ! break; case JPEG: + SetWriteFileTypeToJPEG(); std::cerr << "Writting as JPEG" << std::endl; break; } @@ -1015,6 +1016,17 @@ void FileHelper::SetWriteFileTypeToACR() /** * \brief Sets in the File the TransferSyntax to 'Explicit VR Little Endian" */ +void FileHelper::SetWriteFileTypeToJPEG() +{ + std::string ts = Util::DicomString( + Global::GetTS()->GetSpecialTransferSyntax(TS::JPEGBaselineProcess1) ); + + DataEntry *tss = CopyDataEntry(0x0002,0x0010); + tss->SetString(ts); + + Archive->Push(tss); +} + void FileHelper::SetWriteFileTypeToExplicitVR() { std::string ts = Util::DicomString( diff --git a/src/gdcmFileHelper.h b/src/gdcmFileHelper.h index 9c7e2d65..acd8b013 100644 --- a/src/gdcmFileHelper.h +++ b/src/gdcmFileHelper.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFileHelper.h,v $ Language: C++ - Date: $Date: 2005/10/18 18:39:49 $ - Version: $Revision: 1.25 $ + Date: $Date: 2005/10/18 19:54:27 $ + Version: $Revision: 1.26 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -155,6 +155,7 @@ protected: void RestoreWrite(); void SetWriteFileTypeToACR(); + void SetWriteFileTypeToJPEG(); void SetWriteFileTypeToExplicitVR(); void SetWriteFileTypeToImplicitVR(); void RestoreWriteFileType(); -- 2.48.1