Browse Source

Fix initializers and error handling.

src/Fl_JPEG_Image.cxx:
    - Add custom error manager structure and update error and output
      handlers so we properly handle bad JPEG files.

src/Fl_Shared_Image.cxx:
    - Add initializers for num_handlers, alloc_handlers, and handlers
      variables.



git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4040 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
pull/168/head
Michael R Sweet 21 years ago
parent
commit
80b9844b53
  1. 99
      src/Fl_JPEG_Image.cxx
  2. 10
      src/Fl_Shared_Image.cxx

99
src/Fl_JPEG_Image.cxx

@ -55,14 +55,29 @@ extern "C"
} }
//
// Custom JPEG error handling structure...
//
struct fl_jpeg_error_mgr {
jpeg_error_mgr pub_; // Destination manager...
int err_; // Error flag
};
// //
// Error handler for JPEG files... // Error handler for JPEG files...
// //
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
static void static void
jpeg_error_handler(j_common_ptr) fl_jpeg_error_handler(j_common_ptr dinfo) { // I - Decompressor info
{ ((fl_jpeg_error_mgr *)(dinfo->err))->err_ = 1;
return;
}
static void
fl_jpeg_output_handler(j_common_ptr dinfo) { // I - Decompressor info
return; return;
} }
#endif // HAVE_LIBJPEG #endif // HAVE_LIBJPEG
@ -75,70 +90,82 @@ jpeg_error_handler(j_common_ptr)
Fl_JPEG_Image::Fl_JPEG_Image(const char *jpeg) // I - File to load Fl_JPEG_Image::Fl_JPEG_Image(const char *jpeg) // I - File to load
: Fl_RGB_Image(0,0,0) { : Fl_RGB_Image(0,0,0) {
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
FILE *fp; // File pointer FILE *fp; // File pointer
struct jpeg_decompress_struct cinfo; // Decompressor info jpeg_decompress_struct dinfo; // Decompressor info
struct jpeg_error_mgr jerr; // Error handler info fl_jpeg_error_mgr jerr; // Error handler info
JSAMPROW row; // Sample row pointer JSAMPROW row; // Sample row pointer
// Clear data...
alloc_array = 0;
array = (uchar *)0;
// Open the image file...
if ((fp = fopen(jpeg, "rb")) == NULL) return; if ((fp = fopen(jpeg, "rb")) == NULL) return;
cinfo.err = jpeg_std_error(&jerr); // Setup the decompressor info and read the header...
jerr.error_exit = jpeg_error_handler; dinfo.err = jpeg_std_error((jpeg_error_mgr *)&jerr);
jerr.output_message = jpeg_error_handler; jerr.pub_.error_exit = fl_jpeg_error_handler;
jerr.pub_.output_message = fl_jpeg_output_handler;
jerr.err_ = 0;
jpeg_create_decompress(&dinfo);
jpeg_stdio_src(&dinfo, fp);
jpeg_read_header(&dinfo, 1);
jpeg_create_decompress(&cinfo); if (jerr.err_) goto error_return;
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, 1);
cinfo.quantize_colors = (boolean)FALSE; dinfo.quantize_colors = (boolean)FALSE;
cinfo.out_color_space = JCS_RGB; dinfo.out_color_space = JCS_RGB;
cinfo.out_color_components = 3; dinfo.out_color_components = 3;
cinfo.output_components = 3; dinfo.output_components = 3;
jpeg_calc_output_dimensions(&cinfo); jpeg_calc_output_dimensions(&dinfo);
w(cinfo.output_width); w(dinfo.output_width);
h(cinfo.output_height); h(dinfo.output_height);
d(cinfo.output_components); d(dinfo.output_components);
if (!w() || !h() || !d() || jerr.err_) goto error_return;
array = new uchar[w() * h() * d()]; array = new uchar[w() * h() * d()];
alloc_array = 1; alloc_array = 1;
jpeg_start_decompress(&cinfo); jpeg_start_decompress(&dinfo);
while (dinfo.output_scanline < dinfo.output_height) {
if (jerr.err_) goto error_return;
while (cinfo.output_scanline < cinfo.output_height)
{
row = (JSAMPROW)(array + row = (JSAMPROW)(array +
cinfo.output_scanline * cinfo.output_width * dinfo.output_scanline * dinfo.output_width *
cinfo.output_components); dinfo.output_components);
jpeg_read_scanlines(&cinfo, &row, (JDIMENSION)1); jpeg_read_scanlines(&dinfo, &row, (JDIMENSION)1);
} }
jpeg_finish_decompress(&cinfo); jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&dinfo);
fclose(fp); fclose(fp);
# if 0 return;
// JPEG error handling... // JPEG error handling...
error_return: error_return:
if (array) jpeg_finish_decompress(&cinfo); if (array) jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&dinfo);
fclose(fp); fclose(fp);
if (array) { w(0);
w(0); h(0);
h(0); d(0);
d(0);
if (array) {
delete[] (uchar *)array; delete[] (uchar *)array;
array = 0; array = 0;
alloc_array = 0; alloc_array = 0;
} }
# endif // 0
#endif // HAVE_LIBJPEG #endif // HAVE_LIBJPEG
} }

10
src/Fl_Shared_Image.cxx

@ -1,5 +1,5 @@
// //
// "$Id: Fl_Shared_Image.cxx,v 1.23.2.22 2004/12/03 02:51:03 easysw Exp $" // "$Id$"
// //
// Shared image code for the Fast Light Tool Kit (FLTK). // Shared image code for the Fast Light Tool Kit (FLTK).
// //
@ -41,9 +41,9 @@ Fl_Shared_Image **Fl_Shared_Image::images_ = 0; // Shared images
int Fl_Shared_Image::num_images_ = 0; // Number of shared images int Fl_Shared_Image::num_images_ = 0; // Number of shared images
int Fl_Shared_Image::alloc_images_ = 0; // Allocated shared images int Fl_Shared_Image::alloc_images_ = 0; // Allocated shared images
Fl_Shared_Handler *Fl_Shared_Image::handlers_; // Additional format handlers Fl_Shared_Handler *Fl_Shared_Image::handlers_ = 0;// Additional format handlers
int Fl_Shared_Image::num_handlers_; // Number of format handlers int Fl_Shared_Image::num_handlers_ = 0; // Number of format handlers
int Fl_Shared_Image::alloc_handlers_; // Allocated format handlers int Fl_Shared_Image::alloc_handlers_ = 0; // Allocated format handlers
// //
@ -461,5 +461,5 @@ Fl_Shared_Image::remove_handler(Fl_Shared_Handler f) {
// //
// End of "$Id: Fl_Shared_Image.cxx,v 1.23.2.22 2004/12/03 02:51:03 easysw Exp $". // End of "$Id$".
// //

Loading…
Cancel
Save