Commit 62bf0384 authored by Davis King's avatar Davis King

setup proper error handling for libpng in the png_loader. Now if the PNG

file is corrupted in the middle it won't just print a message and abort
the program.
parent 12e01302
......@@ -87,6 +87,16 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// Don't do anything when libpng calls us to tell us about an error. Just return to
// our own code and throw an exception (at the long jump target).
void png_loader_user_error_fn_silent(png_structp png_struct, png_const_charp )
{
longjmp(png_jmpbuf(png_struct),1);
}
void png_loader_user_warning_fn_silent(png_structp , png_const_charp )
{
}
void png_loader::read_image( const char* filename )
{
ld_.reset(new LibpngData);
......@@ -110,7 +120,7 @@ namespace dlib
fclose( fp );
throw image_load_error(std::string("png_loader: format error in file ") + filename);
}
ld_->png_ptr_ = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
ld_->png_ptr_ = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, &png_loader_user_error_fn_silent, &png_loader_user_warning_fn_silent );
if ( ld_->png_ptr_ == NULL )
{
fclose( fp );
......@@ -130,6 +140,16 @@ namespace dlib
png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), ( png_infopp )NULL );
throw image_load_error(std::string("png_loader: parse error in file ") + filename);
}
if (setjmp(png_jmpbuf(ld_->png_ptr_)))
{
// If we get here, we had a problem writing the file
fclose(fp);
png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) );
throw image_load_error(std::string("png_loader: parse error in file ") + filename);
}
png_init_io( ld_->png_ptr_, fp );
png_set_sig_bytes( ld_->png_ptr_, 8 );
// flags force one byte per channel output
......@@ -149,11 +169,11 @@ namespace dlib
}
ld_->row_pointers_ = png_get_rows( ld_->png_ptr_, ld_->info_ptr_ );
// FIXME: the following call makes libpng crash. Why?
//png_read_end( ld_->png_ptr_, ld_->end_info_ );
fclose( fp );
if ( ld_->row_pointers_ == NULL )
{
png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) );
throw image_load_error(std::string("png_loader: parse error in file ") + filename);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment