]> Creatis software - gdcm.git/blobdiff - src/jdatasrc.cxx
ENH: Grealty simplify the JPEg decompression, no need to differenciate based on the...
[gdcm.git] / src / jdatasrc.cxx
index e6c644307dd4b8840ba28903a23c4637b90cef67..199431c31444c7542b07af05e58a6ca2d2c933f3 100644 (file)
@@ -24,6 +24,9 @@ typedef struct {
   std::ifstream *infile;  /* source stream */ 
   JOCTET * buffer;        /* start of buffer */
   boolean start_of_file;  /* have we gotten any data yet? */
+  //PixelReadConvert *pixels;
+  gdcm::JPEGFragment *frag;
+  size_t bytes_read;
 } my_source_mgr;
 
 typedef my_source_mgr * my_src_ptr;
@@ -87,8 +90,25 @@ fill_input_buffer (j_decompress_ptr cinfo)
 {
   my_src_ptr src = (my_src_ptr) cinfo->src;
 
-  src->infile->read( (char*)src->buffer, INPUT_BUF_SIZE);
+  //std::cerr << "Before comp:" << src->bytes_read << " / " << src->frag->Length << std::endl;
+  if( src->bytes_read == src->frag->Length )
+    {
+    //std::cerr << "Sweet finished this fragment" << std::endl;
+    return FALSE;
+    }
+
+  size_t input_buf_size = INPUT_BUF_SIZE;
+  if( (src->bytes_read + INPUT_BUF_SIZE) > src->frag->Length )
+    {
+    //std::cerr << "Woula error:" << src->bytes_read << " / " << src->frag->Length << std::endl;
+    input_buf_size = src->frag->Length - src->bytes_read;
+    //std::cerr << "Ok only reading: " << input_buf_size << " / " << INPUT_BUF_SIZE << std::endl;
+     }
+
+  //std::cerr << "infile read:" << src->pub.bytes_in_buffer << std::endl;
+  src->infile->read( (char*)src->buffer, input_buf_size);
   size_t nbytes = src->infile->gcount();
+  //std::cerr << "input_buf_size=" << input_buf_size << " and nbytes=" << nbytes << std::endl;
 
   if (nbytes <= 0) {
     if (src->start_of_file)  /* Treat empty input file as fatal error */
@@ -103,8 +123,13 @@ fill_input_buffer (j_decompress_ptr cinfo)
   src->pub.next_input_byte = src->buffer;
   src->pub.bytes_in_buffer = nbytes;
   src->start_of_file = FALSE;
+  src->bytes_read += nbytes;
+
 
   return TRUE;
+  // otherwise cause a suspension return
+  //std::cerr << "fill_input_buffer" << std::endl;
+  //return FALSE;
 }
 
 
@@ -123,6 +148,7 @@ fill_input_buffer (j_decompress_ptr cinfo)
 METHODDEF(void)
 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
 {
+  //std::cerr << "skip_input_data:" << num_bytes << std::endl;
   my_src_ptr src = (my_src_ptr) cinfo->src;
 
   /* Just a dumb implementation for now.  Could use fseek() except
@@ -176,7 +202,7 @@ term_source (j_decompress_ptr cinfo)
  */
 
 GLOBAL(void)
-jpeg_stdio_src (j_decompress_ptr cinfo, std::ifstream * infile)
+jpeg_stdio_src (j_decompress_ptr cinfo, std::ifstream * infile, gdcm::JPEGFragment *frag, int flag)
 {
   my_src_ptr src;
 
@@ -204,6 +230,18 @@ jpeg_stdio_src (j_decompress_ptr cinfo, std::ifstream * infile)
   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   src->pub.term_source = term_source;
   src->infile = infile;
+  
+  if( flag )
+    {
   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   src->pub.next_input_byte = NULL; /* until buffer loaded */
+  src->frag = frag;
+  src->bytes_read = 0;
+    }
+  else
+    {
+    //only upate the new fragment
+    src->frag = frag;
+  src->bytes_read = 0;
+    }
 }