From: malaterre Date: Tue, 18 Oct 2005 18:39:49 +0000 (+0000) Subject: ENH: Adding *very* experimental support for writting DICOM file as JPEG X-Git-Tag: OpenJPEG.Version1.2~326 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=3ccb0db5e229de47c8a1fdc09deb3561315ec282;p=gdcm.git ENH: Adding *very* experimental support for writting DICOM file as JPEG --- diff --git a/src/gdcmCommon.h b/src/gdcmCommon.h index b6fdd0bf..5f1685c8 100644 --- a/src/gdcmCommon.h +++ b/src/gdcmCommon.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmCommon.h,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:27 $ - Version: $Revision: 1.95 $ + Date: $Date: 2005/10/18 18:39:49 $ + Version: $Revision: 1.96 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -117,7 +117,8 @@ enum FileType { ExplicitVR, // DicomDir is in this case. Except when it's ImplicitVR !... ImplicitVR, ACR, - ACR_LIBIDO + ACR_LIBIDO, + JPEG }; /// \brief type of the elements composing a DICOMDIR (for internal use only) diff --git a/src/gdcmFileHelper.cxx b/src/gdcmFileHelper.cxx index 1b454c60..c4435ba4 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 12:58:28 $ - Version: $Revision: 1.62 $ + Date: $Date: 2005/10/18 18:39:49 $ + Version: $Revision: 1.63 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -708,6 +708,9 @@ bool FileHelper::Write(std::string const &fileName) SetWriteFileTypeToACR(); // SetWriteFileTypeToImplicitVR(); // ACR IS implicit VR ! break; + case JPEG: + std::cerr << "Writting as JPEG" << std::endl; + break; } CheckMandatoryElements(); @@ -741,6 +744,7 @@ bool FileHelper::Write(std::string const &fileName) } bool check = CheckWriteIntegrity(); // verifies length + if (WriteType == JPEG ) check = true; if (check) { check = FileInternal->Write(fileName,WriteType); diff --git a/src/gdcmFileHelper.h b/src/gdcmFileHelper.h index 448afcc0..9c7e2d65 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 12:58:28 $ - Version: $Revision: 1.24 $ + Date: $Date: 2005/10/18 18:39:49 $ + Version: $Revision: 1.25 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -21,8 +21,6 @@ #include "gdcmDebug.h" #include "gdcmBase.h" -//#include - namespace gdcm @@ -132,6 +130,7 @@ public: void SetWriteTypeToAcr() { SetWriteType(ACR); } /// \brief Tells the writer we want to write as LibIDO void SetWriteTypeToAcrLibido() { SetWriteType(ACR_LIBIDO); } + void SetWriteTypeToJPEG() { SetWriteType(JPEG); } /// \brief Tells the writer which format we want to write /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO) void SetWriteType(FileType format) { WriteType = format; } diff --git a/src/gdcmJpeg.cxx b/src/gdcmJpeg.cxx index b98a9acf..3edb58af 100644 --- a/src/gdcmJpeg.cxx +++ b/src/gdcmJpeg.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmJpeg.cxx,v $ Language: C++ - Date: $Date: 2005/06/24 10:55:59 $ - Version: $Revision: 1.49 $ + Date: $Date: 2005/10/18 18:39:49 $ + Version: $Revision: 1.50 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -56,7 +56,7 @@ namespace gdcm * @return 1 on success, 0 on error */ -bool gdcm_write_JPEG_file (std::ofstream *fp, void *im_buf, +bool gdcm_write_JPEG_file (std::ostream *fp, void *im_buf, int image_width, int image_height, int quality) { @@ -107,7 +107,8 @@ bool gdcm_write_JPEG_file (std::ofstream *fp, void *im_buf, // exit(1); // // } - jpeg_stdio_dest(&cinfo, fp); + assert( 0 ); + //jpeg_stdio_dest(&cinfo, fp, 0, 0, image_width, image_height, quality); /* Step 3: set parameters for compression */ diff --git a/src/jdatadst.cxx b/src/jdatadst.cxx index 547eb2fd..50980d41 100644 --- a/src/jdatadst.cxx +++ b/src/jdatadst.cxx @@ -27,8 +27,12 @@ extern "C" { typedef struct { struct jpeg_destination_mgr pub; /* public fields */ - std::ofstream * outfile; /* target stream */ + std::ostream * outfile; /* target stream */ JOCTET * buffer; /* start of buffer */ +// boolean start_of_file; /* have we gotten any data yet? */ + + size_t frag_length; //we have to control this one to spec the size of the frag + size_t bytes_written; } my_destination_mgr; typedef my_destination_mgr * my_dest_ptr; @@ -84,18 +88,29 @@ empty_output_buffer (j_compress_ptr cinfo) { my_dest_ptr dest = (my_dest_ptr) cinfo->dest; -#if 0 - if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != - (size_t) OUTPUT_BUF_SIZE) + if( dest->bytes_written == dest->frag_length ) + { + // Start the I/O suspension simply by returning false here: + return FALSE; + } + + size_t output_buf_size = OUTPUT_BUF_SIZE; + if( (dest->bytes_written + OUTPUT_BUF_SIZE) > dest->frag_length ) + { + output_buf_size = dest->frag_length - dest->bytes_written; + } + dest->outfile->write((char*)dest->buffer, output_buf_size); + size_t nbytes = output_buf_size; //dest->outfile->gcount(); + + if( nbytes <= 0 ) ERREXIT(cinfo, JERR_FILE_WRITE); -#else - dest->outfile->write((char*)dest->buffer, OUTPUT_BUF_SIZE); + if( dest->outfile->fail() ) ERREXIT(cinfo, JERR_FILE_WRITE); -#endif dest->pub.next_output_byte = dest->buffer; - dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; + dest->pub.free_in_buffer = nbytes; //OUTPUT_BUF_SIZE; + dest->bytes_written += nbytes; return TRUE; } @@ -147,7 +162,7 @@ term_destination (j_compress_ptr cinfo) */ GLOBAL(void) -jpeg_stdio_dest (j_compress_ptr cinfo, std::ofstream * outfile) +jpeg_stdio_dest (j_compress_ptr cinfo, std::ostream * outfile, size_t frag_length) //, int flag) { my_dest_ptr dest; @@ -168,4 +183,15 @@ jpeg_stdio_dest (j_compress_ptr cinfo, std::ofstream * outfile) dest->pub.empty_output_buffer = reinterpret_cast(empty_output_buffer); dest->pub.term_destination = reinterpret_cast(term_destination); dest->outfile = outfile; + + // Need to setup a new buffer, clean bytes_in_buffer and next_input_byte + //if( flag ) + { + //dest->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ + //dest->pub.next_input_byte = NULL; /* until buffer loaded */ + } + //only upate the new fragment, valid for both 'flag' value + dest->frag_length = frag_length; + dest->bytes_written = 0; + } diff --git a/src/jdatasrc.cxx b/src/jdatasrc.cxx index 53717cd5..bf658ef1 100644 --- a/src/jdatasrc.cxx +++ b/src/jdatasrc.cxx @@ -27,7 +27,7 @@ extern "C" { typedef struct { struct jpeg_source_mgr pub; /* public fields */ - std::ifstream *infile; /* source stream */ + std::istream *infile; /* source stream */ JOCTET * buffer; /* start of buffer */ boolean start_of_file; /* have we gotten any data yet? */ @@ -198,7 +198,7 @@ term_source (j_decompress_ptr cinfo) */ GLOBAL(void) -jpeg_stdio_src (j_decompress_ptr cinfo, std::ifstream * infile, gdcm::JPEGFragment *frag, int flag) +jpeg_stdio_src (j_decompress_ptr cinfo, std::istream * infile, gdcm::JPEGFragment *frag, int flag) { my_src_ptr src;