Program: gdcm
Module: $RCSfile: gdcmJpeg.cxx,v $
Language: C++
- Date: $Date: 2004/12/12 17:21:08 $
- Version: $Revision: 1.29 $
+ Date: $Date: 2005/01/26 11:42:02 $
+ Version: $Revision: 1.37 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
-#include "gdcmFile.h"
+#include "gdcmFileHelper.h"
+#include "gdcmJPEGFragment.h"
/*
DICOM provides a mechanism for supporting the use of JPEG Image Compression
* (stdio.h is sufficient on ANSI-conforming systems.)
* You may also wish to include "jerror.h".
*/
+#if defined(__sgi) && !defined(__GNUC__)
+// Try to get rid of the warning:
+//cc-3505 CC: WARNING File = /usr/include/internal/setjmp_core.h, Line = 74
+// setjmp not marked as unknown_control_flow because it is not declared as a
+// function
+//
+// #pragma unknown_control_flow (setjmp)
+# if (_COMPILER_VERSION >= 730)
+# pragma set woff 3505
+# endif
+#endif
+#ifdef _MSC_VER
+// Let us get rid of this funny warning on /W4:
+// warning C4611: interaction between '_setjmp' and C++ object
+// destruction is non-portable
+#pragma warning( disable : 4611 )
+#endif
#include <setjmp.h>
#include <fstream>
*/
/**
- * \ingroup File
* \brief routine for JPEG decompression
* @param fp pointer to an already open file descriptor
* 8 significant bits per pixel
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
-
-//-----------------------------------------------------------------------------
typedef struct my_error_mgr* my_error_ptr;
+//-----------------------------------------------------------------------------
/*
* Here's the routine that will replace the standard error_exit method:
* @param image_buffer to receive uncompressed pixels
* @return 1 on success, 0 on error
*/
-
-bool gdcm_read_JPEG_file ( std::ifstream* fp, void* image_buffer )
+void *SampBuffer;
+bool JPEGFragment::gdcm_read_JPEG_file (std::ifstream* fp, void* image_buffer , int& statesuspension)
{
- char* pimage;
-
+ //static int fragimage = 0;
+ //std::cerr << "Image Fragment:" << fragimage++ << std::endl;
+ pImage = (uint8_t*)image_buffer;
/* This struct contains the JPEG decompression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
*/
- struct jpeg_decompress_struct cinfo;
+ static struct jpeg_decompress_struct cinfo;
/* -------------- inside, we found :
* JDIMENSION image_width; // input image width
int row_stride;/* physical row width in output buffer */
+ //std::cerr << "StateSuspension: " << statesuspension << std::endl;
+//#define GDCM_JPG_DEBUG
#ifdef GDCM_JPG_DEBUG
printf("entree dans File::gdcm_read_JPEG_file (i.e. 8), depuis gdcmJpeg\n");
#endif //GDCM_JPG_DEBUG
/* 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.
*/
+ std::cerr << "Qu'est c'est ce bordel !!!!!" << std::endl;
jpeg_destroy_decompress(&cinfo);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
+ if( statesuspension == 0 )
+ {
jpeg_create_decompress(&cinfo);
-
/* Step 2: specify data source (eg, a file) */
#ifdef GDCM_JPG_DEBUG
printf("Entree Step 2\n");
#endif //GDCM_JPG_DEBUG
- jpeg_stdio_src(&cinfo, fp);
+ jpeg_stdio_src(&cinfo, fp, this, 1);
+ }
+ else
+ {
+ jpeg_stdio_src(&cinfo, fp, this, 0);
+ }
/* Step 3: read file parameters with jpeg_read_header() */
#ifdef GDCM_JPG_DEBUG
printf("Entree Step 3\n");
#endif //GDCM_JPG_DEBUG
- (void) jpeg_read_header(&cinfo, TRUE);
+ if( statesuspension < 2 )
+ {
+ if( jpeg_read_header(&cinfo, TRUE) == JPEG_SUSPENDED )
+ {
+ std::cerr << "Suspension: jpeg_read_header" << std::endl;
+ }
/* We can ignore the return value from jpeg_read_header since
* (a) suspension is not possible with the stdio data source, and
cinfo.out_color_space = JCS_UNKNOWN;
}
+ } //statesuspension < 2
#ifdef GDCM_JPG_DEBUG
printf("--------------Header contents :----------------\n");
printf("Entree Step 5\n");
#endif //GDCM_JPG_DEBUG
- (void) jpeg_start_decompress(&cinfo);
+ if(statesuspension < 3 )
+ {
+ if( jpeg_start_decompress(&cinfo) == FALSE )
+ {
+ std::cerr << "Suspension: jpeg_start_decompress" << std::endl;
+ }
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
*/
printf ("cinfo.output_height %d cinfo.output_width %d\n",
cinfo.output_height,cinfo.output_width);
#endif //GDCM_JPG_DEBUG
- pimage=(char *)image_buffer;
+ SampBuffer = buffer;
+ } // statesuspension < 3
+ else
+ {
+ buffer = (JSAMPARRAY)SampBuffer;
+ }
int bufsize = cinfo.output_width * cinfo.output_components;
size_t rowsize = bufsize * sizeof(JSAMPLE);
*/
//printf( "scanlines: %d\n",cinfo.output_scanline);
- (void) jpeg_read_scanlines(&cinfo, buffer, 1);
- memcpy( pimage, *buffer,rowsize);
- pimage+=rowsize;
+ if( jpeg_read_scanlines(&cinfo, buffer, 1) == 0 )
+ {
+ std::cerr << "Suspension: jpeg_read_scanlines" << std::endl;
+ statesuspension = 3;
+ return true;
+ }
+// The ijg has no notion of big endian, therefore always swap the jpeg stream
+#if defined(GDCM_WORDS_BIGENDIAN) && (CMAKE_BITS_IN_JSAMPLE != 8)
+ uint16_t *buffer16 = (uint16_t*)*buffer;
+ uint16_t *pimage16 = (uint16_t*)pImage;
+ for(unsigned int i=0;i<rowsize/2;i++)
+ pimage16[i] = (buffer16[i] >> 8) | (buffer16[i] << 8 );
+#else
+ memcpy( pImage, *buffer,rowsize);
+#endif //GDCM_WORDS_BIGENDIAN
+ pImage+=rowsize;
}
/* Step 7: Finish decompression */
printf("Entree Step 7\n");
#endif //GDCM_JPG_DEBUG
- (void) jpeg_finish_decompress(&cinfo);
+ if( jpeg_finish_decompress(&cinfo) == FALSE )
+ {
+ std::cerr << "Suspension: jpeg_finish_decompress" << std::endl;
+ }
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_decompress(&cinfo);
+ //std::cerr << "jpeg_destroy_decompress" << std::endl;
/* After finish_decompress, we can close the input file.
* Here we postpone it until after no more JPEG errors are possible,
* On some systems you may need to set up a signal handler to ensure that
* temporary files are deleted if the program is interrupted. See libjpeg.doc.
*/
+#ifdef _MSC_VER
+// Put the warning back
+#pragma warning( default : 4611 )
+#endif
//----------------------------------------------------------------------------
void* image_buffer,
size_t *howManyRead, size_t *howManyWritten)
{
- char* pimage=(char *)image_buffer;
+ volatile char * pimage=(volatile char *)image_buffer;
JOCTET* input = (JOCTET*) input_buffer;
/* This struct contains the JPEG decompression parameters and pointers to
//printf( "scanlines: %d\n",cinfo.output_scanline);
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
- memcpy( pimage, *buffer,rowsize);
+#if defined(GDCM_WORDS_BIGENDIAN) && (CMAKE_BITS_IN_JSAMPLE != 8)
+ uint16_t *buffer16 = (uint16_t*)*buffer;
+ uint16_t *pimage16 = (uint16_t*)pimage;
+ for(unsigned int i=0;i<rowsize/2;i++)
+ pimage16[i] = (buffer16[i] >> 8) | (buffer16[i] << 8 );
+#else
+ memcpy( (void*)pimage, *buffer,rowsize);
+#endif //GDCM_WORDS_BIGENDIAN
pimage+=rowsize;
}
-
+
/* Step 7: Finish decompression */
#ifdef GDCM_JPG_DEBUG
printf("Entree Step 7\n");