]> Creatis software - gdcm.git/commitdiff
ENH: Adding *very* experimental support for writting DICOM file as JPEG
authormalaterre <malaterre>
Tue, 18 Oct 2005 18:39:49 +0000 (18:39 +0000)
committermalaterre <malaterre>
Tue, 18 Oct 2005 18:39:49 +0000 (18:39 +0000)
src/gdcmCommon.h
src/gdcmFileHelper.cxx
src/gdcmFileHelper.h
src/gdcmJpeg.cxx
src/jdatadst.cxx
src/jdatasrc.cxx

index b6fdd0bf9ceeb0884637acde04920ece476ac75c..5f1685c83d599479f56e8ff63f0e9f1e749010f4 100644 (file)
@@ -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)
index 1b454c60de9f0534104818aa9eadddb932910319..c4435ba4b2759d590b88da7f117446f2d58b9fad 100644 (file)
@@ -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);
index 448afcc0446f5048356c19f54a9a5758fea9dee9..9c7e2d659b63e0189c2518d6e3bc049cbc7562d7 100644 (file)
@@ -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 <iostream>
-
 
 
 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;       }
index b98a9acfb888613eb781968d4516197c2f3d5ad6..3edb58af2b46d2b733750d58ea80927a45232af5 100644 (file)
@@ -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 */
 
index 547eb2fda4e889cbf3ffc48a33b8934003c1cd1f..50980d41fd5ac8191766679716190591c383f573 100644 (file)
@@ -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<boolean_jpeg_compress_struct>(empty_output_buffer);
   dest->pub.term_destination = reinterpret_cast<void_jpeg_compress_struct>(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;
+
 }
index 53717cd5006b1ae246d6cc8b4b8576be511c1ffb..bf658ef10b2fc0bab7c07f7ee547ace1450ae594 100644 (file)
@@ -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;