Make setjmp in jpeg code thread safe.

This commit is contained in:
Steve Herrell
2010-12-07 11:25:37 -05:00
parent f4796cc776
commit dca28c8f6e
6 changed files with 115 additions and 88 deletions

View File

@@ -31,11 +31,11 @@
#endif
#include <iptcpairs.h>
#include <libiptcdata/iptc-jpeg.h>
extern "C" {
#include "jdatasrc.c"
#include <jpeglib.h>
#include <iccjpeg.h>
}
#include "jpeg.h"
Glib::ustring safe_locale_to_utf8 (const std::string& src);
@@ -257,68 +257,63 @@ int ImageIO::loadPNG (Glib::ustring fname) {
return IMIO_SUCCESS;
}
extern jmp_buf jpeg_jmp_buf;
extern "C" {
void jpeg_memory_src (jpeg_decompress_struct* cinfo, const char* buffer, int bufsize);
}
int ImageIO::loadJPEGFromMemory (const char* buffer, int bufsize)
{
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
cinfo.err = my_jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_memory_src (&cinfo,buffer,bufsize);
if (pl) {
pl->setProgressStr ("Loading JPEG file...");
pl->setProgress (0.0);
jpeg_memory_src (&cinfo,(const JOCTET*)buffer,bufsize);
if ( setjmp(((rt_jpeg_error_mgr*)cinfo.src)->error_jmp_buf) == 0 )
{
if (pl) {
pl->setProgressStr ("Loading JPEG file...");
pl->setProgress (0.0);
}
}
setup_read_icc_profile (&cinfo);
setup_read_icc_profile (&cinfo);
if (!setjmp(jpeg_jmp_buf)) {
jpeg_memory_src (&cinfo,buffer,bufsize);
jpeg_read_header(&cinfo, TRUE);
//jpeg_memory_src (&cinfo,buffer,bufsize);
jpeg_read_header(&cinfo, TRUE);
unsigned int proflen;
delete loadedProfileData;
loadedProfileData = NULL;
bool hasprofile = read_icc_profile (&cinfo, (JOCTET**)&loadedProfileData, (unsigned int*)&loadedProfileLength);
if (hasprofile)
embProfile = cmsOpenProfileFromMem (loadedProfileData, loadedProfileLength);
else
if (hasprofile)
embProfile = cmsOpenProfileFromMem (loadedProfileData, loadedProfileLength);
else
embProfile = NULL;
jpeg_start_decompress(&cinfo);
jpeg_start_decompress(&cinfo);
int width = cinfo.output_width;
int height = cinfo.output_height;
allocate (width, height);
unsigned char *row=new unsigned char[width*3];
while (cinfo.output_scanline < height) {
if (jpeg_read_scanlines(&cinfo,&row,1) < 1) {
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
delete [] row;
return IMIO_READERROR;
unsigned char *row=new unsigned char[width*3];
while (cinfo.output_scanline < height) {
if (jpeg_read_scanlines(&cinfo,&row,1) < 1) {
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
delete [] row;
return IMIO_READERROR;
}
setScanline (cinfo.output_scanline-1, row, 8);
if (pl && !(cinfo.output_scanline%100))
pl->setProgress ((double)(cinfo.output_scanline)/cinfo.output_height);
}
delete [] row;
pl->setProgress ((double)(cinfo.output_scanline)/cinfo.output_height);
}
delete [] row;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
if (pl) {
pl->setProgressStr ("Ready.");
pl->setProgress (1.0);
pl->setProgressStr ("Ready.");
pl->setProgress (1.0);
}
return IMIO_SUCCESS;
}
@@ -338,57 +333,59 @@ int ImageIO::loadJPEG (Glib::ustring fname) {
jpeg_error_mgr jerr;
cinfo.err = my_jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
my_jpeg_stdio_src (&cinfo,file);
if ( setjmp(((rt_jpeg_error_mgr*)cinfo.src)->error_jmp_buf) == 0 )
{
if (pl) {
pl->setProgressStr ("Loading JPEG file...");
pl->setProgress (0.0);
if (pl) {
pl->setProgressStr ("Loading JPEG file...");
pl->setProgress (0.0);
}
}
setup_read_icc_profile (&cinfo);
setup_read_icc_profile (&cinfo);
if (!setjmp(jpeg_jmp_buf)) {
jpeg_stdio_src(&cinfo,file);
jpeg_read_header(&cinfo, TRUE);
//jpeg_stdio_src(&cinfo,file);
jpeg_read_header(&cinfo, TRUE);
unsigned int proflen;
delete loadedProfileData;
loadedProfileData = NULL;
bool hasprofile = read_icc_profile (&cinfo, (JOCTET**)&loadedProfileData, (unsigned int*)&loadedProfileLength);
if (hasprofile)
embProfile = cmsOpenProfileFromMem (loadedProfileData, loadedProfileLength);
else
if (hasprofile)
embProfile = cmsOpenProfileFromMem (loadedProfileData, loadedProfileLength);
else
embProfile = NULL;
jpeg_start_decompress(&cinfo);
jpeg_start_decompress(&cinfo);
int width = cinfo.output_width;
int height = cinfo.output_height;
allocate (width, height);
unsigned char *row=new unsigned char[width*3];
while (cinfo.output_scanline < height) {
if (jpeg_read_scanlines(&cinfo,&row,1) < 1) {
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
delete [] row;
return IMIO_READERROR;
unsigned char *row=new unsigned char[width*3];
while (cinfo.output_scanline < height) {
if (jpeg_read_scanlines(&cinfo,&row,1) < 1) {
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
delete [] row;
return IMIO_READERROR;
}
setScanline (cinfo.output_scanline-1, row, 8);
if (pl && !(cinfo.output_scanline%100))
pl->setProgress ((double)(cinfo.output_scanline)/cinfo.output_height);
}
delete [] row;
pl->setProgress ((double)(cinfo.output_scanline)/cinfo.output_height);
}
delete [] row;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file);
if (pl) {
pl->setProgressStr ("Ready.");
pl->setProgress (1.0);
pl->setProgressStr ("Ready.");
pl->setProgress (1.0);
}
return IMIO_SUCCESS;
}