]> Creatis software - gdcm.git/blobdiff - src/gdcmJpeg.cxx
ENH: Adding jpeg error handling. As quoted we could be more fancy and throw an except...
[gdcm.git] / src / gdcmJpeg.cxx
index 66a6dca66289224b3200d18e7242790fef72588b..ae4c268510be8414f3e1bd653bf9798e2908c836 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmJpeg.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/31 04:00:04 $
-  Version:   $Revision: 1.39 $
+  Date:      $Date: 2005/02/04 23:45:22 $
+  Version:   $Revision: 1.43 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -56,11 +56,11 @@ 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::ofstream *fp, void *im_buf, 
                            int image_width, int image_height, int quality)
 {
 
-   JSAMPLEimage_buffer = (JSAMPLE*) im_buf;
+   JSAMPLE *image_buffer = (JSAMPLE*) im_buf;
 
   /* This struct contains the JPEG compression parameters and pointers to
    * working space (which is allocated as needed by the JPEG library).
@@ -173,7 +173,6 @@ bool gdcm_write_JPEG_file (std::ofstream* fp, void*  im_buf,
   return true; //???
 }
 
-
 //-----------------------------------------------------------------------------
 struct my_error_mgr {
    struct jpeg_error_mgr pub; /* "public" fields */
@@ -185,6 +184,7 @@ typedef struct my_error_mgr* my_error_ptr;
 /*
  * Here's the routine that will replace the standard error_exit method:
  */
+extern "C" {
 METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
    /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
    my_error_ptr myerr = (my_error_ptr) cinfo->err;
@@ -197,6 +197,18 @@ METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
    longjmp(myerr->setjmp_buffer, 1);
 }
 
+METHODDEF(void) my_output_message (j_common_ptr cinfo)
+{
+   char buffer[JMSG_LENGTH_MAX];
+   /* Create the message */
+   (*cinfo->err->format_message) (cinfo, buffer);
+
+   // Custom display message, we could be more fancy and throw an exception:
+   gdcmErrorMacro( buffer );
+}
+
+}
 //-----------------------------------------------------------------------------
  
  /**
@@ -208,7 +220,7 @@ METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
  * @return 1 on success, 0 on error
  */
 void *SampBuffer; 
-bool JPEGFragment::ReadJPEGFile (std::ifstream* fp, void* image_buffer , int& statesuspension)
+bool JPEGFragment::ReadJPEGFile (std::ifstream *fp, void *image_buffer, int &statesuspension)
 {
    pImage = (uint8_t*)image_buffer;
    // This struct contains the JPEG decompression parameters and pointers to
@@ -243,7 +255,10 @@ bool JPEGFragment::ReadJPEGFile (std::ifstream* fp, void* image_buffer , int& st
   // We set up the normal JPEG error routines, then override error_exit.
   
   cinfo.err = jpeg_std_error(&jerr.pub);
+  // for any jpeg error call my_error_exit
   jerr.pub.error_exit = my_error_exit;
+  // for any output message call my_output_message
+  jerr.pub.output_message = my_output_message;
   
   // Establish the setjmp return context for my_error_exit to use.
   if (setjmp(jerr.setjmp_buffer))