]> Creatis software - gdcm.git/blobdiff - src/gdcmJpeg.cxx
BUG: Remove demangle code this was seg faulting on some linux gcc 3.3.2 machine
[gdcm.git] / src / gdcmJpeg.cxx
index a2c3250ca8dbd2bb73fae5f9735cad103b4515a3..86403c62c8fd471a1921b02030c05531ee97746a 100644 (file)
@@ -1,14 +1,22 @@
-// gdcmJpeg.cxx
-//-----------------------------------------------------------------------------
-#include <stdio.h>
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: gdcmJpeg.cxx,v $
+  Language:  C++
+  Date:      $Date: 2004/10/22 03:05:42 $
+  Version:   $Revision: 1.28 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
 #include "gdcmFile.h"
 
-#define BITS_IN_JSAMPLE 8
-
-#ifdef GDCM_DEBUG
-#define GDCM_jpr_DEBUG 0
-#endif   //GDCM_DEBUG
-
 /*
 DICOM provides a mechanism for supporting the use of JPEG Image Compression 
 through the Encapsulated Format (see PS 3.3 of the DICOM Standard). 
@@ -76,12 +84,13 @@ of the uncompressed pixel data from which the compressed data is derived
  * You may also wish to include "jerror.h".
  */
 
-extern "C" {
-#include "jpeglib.h"
 #include <setjmp.h>
-}
-
+#include <fstream>
+#include "jdatasrc.cxx"
+#include "jdatadst.cxx"
 
+namespace gdcm 
+{
 /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
 
 /* This half of the example shows how to feed data into the JPEG compressor.
@@ -105,7 +114,6 @@ extern "C" {
  * RGB color and is described by:
  */
 
-// FIXME : JPR
 
 //extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
 //extern int image_height;       /* Number of rows in image */
@@ -119,18 +127,18 @@ extern "C" {
  */
 
  /**
- * \ingroup gdcmFile
+ * \ingroup File
  * \brief   routine for JPEG decompression 
  * @param fp pointer to an already open file descriptor 
  *                      8 significant bits per pixel
- * @param image_buffer Points to array (of R,G,B-order) data to compress
+ * @param im_buf Points to array (of R,G,B-order) data to compress
  * @param quality compression quality
  * @param image_height Number of rows in image 
  * @param image_width Number of columns in image
  * @return 1 on success, 0 on error
  */
  
-bool gdcm_write_JPEG_file (FILE *fp, void * im_buf, 
+bool gdcm_write_JPEG_file (std::ofstream* fp, void*  im_buf, 
                            int image_width, int image_height, int quality)
 {
 
@@ -153,7 +161,7 @@ bool gdcm_write_JPEG_file (FILE *fp, void * im_buf,
    */
   struct jpeg_error_mgr jerr;
   /* More stuff */
-  //FILE * outfile;    /* target file */
+  //FILE*  outfile;    /* target FILE* /
   JSAMPROW row_pointer[1];   /* pointer to JSAMPLE row[s] */
   int row_stride;            /* physical row width in image buffer */
 
@@ -243,6 +251,8 @@ bool gdcm_write_JPEG_file (FILE *fp, void * im_buf,
   jpeg_destroy_compress(&cinfo);
 
   /* And we're done! */
+
+  return true; //???
 }
 
 
@@ -323,7 +333,7 @@ struct my_error_mgr {
 };
 
 //-----------------------------------------------------------------------------
-typedef struct my_error_mgr * my_error_ptr;
+typedef struct my_error_mgr* my_error_ptr;
 
 /*
  * Here's the routine that will replace the standard error_exit method:
@@ -347,7 +357,6 @@ METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
  */
  
  /**
- * \ingroup gdcmFile
  * \brief   routine for JPEG decompression 
  * @param fp pointer to an already open file descriptor 
  *                      8 significant bits per pixel
@@ -355,8 +364,9 @@ METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
  * @return 1 on success, 0 on error
  */
  
-bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
-   char *pimage;
+bool gdcm_read_JPEG_file ( std::ifstream* fp, void* image_buffer )
+{
+   char* pimage;
 
    /* This struct contains the JPEG decompression parameters and pointers to
     * working space (which is allocated as needed by the JPEG library).
@@ -390,7 +400,7 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
    int row_stride;/* physical row width in output buffer */
   
 #ifdef GDCM_JPG_DEBUG
-   printf("entree dans gdcmFile::gdcm_read_JPEG_file (i.e. 8), depuis gdcmJpeg\n");
+   printf("entree dans File::gdcm_read_JPEG_file (i.e. 8), depuis gdcmJpeg\n");
 #endif //GDCM_JPG_DEBUG
 
    /* In this example we want to open the input file before doing anything else,
@@ -410,7 +420,8 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
   jerr.pub.error_exit = my_error_exit;
   
   /* Establish the setjmp return context for my_error_exit to use. */  
-  if (setjmp(jerr.setjmp_buffer)) {
+  if (setjmp(jerr.setjmp_buffer))
+  {
     /* If we get here, the JPEG code has signaled an error.
      * We need to clean up the JPEG object, close the input file, and return.
      */
@@ -440,6 +451,14 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
     * See libjpeg.doc for more info.
     */
 
+    // prevent the library from performing any color space conversion
+   if( cinfo.process == JPROC_LOSSLESS )
+   {
+      cinfo.jpeg_color_space = JCS_UNKNOWN;
+      cinfo.out_color_space = JCS_UNKNOWN;
+   }
+
+
 #ifdef GDCM_JPG_DEBUG
       printf("--------------Header contents :----------------\n");
       printf("image_width %d image_height %d\n", 
@@ -486,7 +505,7 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
     */ 
 
    /* JSAMPLEs per row in output buffer */
-   row_stride = cinfo.output_width * cinfo.output_components;
+   row_stride = cinfo.output_width * cinfo.output_components*2;
   
 #ifdef GDCM_JPG_DEBUG
   printf ("cinfo.output_width %d cinfo.output_components %d  row_stride %d\n",
@@ -512,29 +531,21 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
 #endif //GDCM_JPG_DEBUG
    pimage=(char *)image_buffer;
   
+   int bufsize = cinfo.output_width * cinfo.output_components;
+   size_t rowsize = bufsize * sizeof(JSAMPLE);
+
    while (cinfo.output_scanline < cinfo.output_height) {
       /* jpeg_read_scanlines expects an array of pointers to scanlines.
        * Here the array is only one element long, but you could ask for
        * more than one scanline at a time if that's more convenient.
        */
-     
-     // l'image est deja allouée (et passée en param)
-     // on ecrit directement les pixels
-     // (on DEVRAIT pouvoir)
-    
-    //(void) jpeg_read_scanlines(&cinfo, pimage, 1);
-    
-     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
-      
-     if ( BITS_IN_JSAMPLE == 8) {
-         memcpy( pimage, buffer[0],row_stride); 
-         pimage+=row_stride;
-     } else {
-         memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2  car 16 bits?!?
-         pimage+=row_stride*2;                     // FIXME : *2 car 16 bits?!?     
-     }
-  }
+
+     //printf( "scanlines: %d\n",cinfo.output_scanline);
+      (void) jpeg_read_scanlines(&cinfo, buffer, 1);
+      memcpy( pimage, *buffer,rowsize); 
+      pimage+=rowsize;
+   }
+
   /* Step 7: Finish decompression */
 #ifdef GDCM_JPG_DEBUG
    printf("Entree Step 7\n");
@@ -568,7 +579,7 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
 
    /* And we're done! */
 
-   return(true);
+   return true;
 }
 
 
@@ -598,3 +609,5 @@ bool gdcmFile::gdcm_read_JPEG_file (FILE *fp,void * image_buffer) {
  */
  
 //----------------------------------------------------------------------------
+
+} // end namespace gdcm