@@ -51,26 +51,26 @@ namespace MR
5151namespace ImageLoad
5252{
5353
54- Expected<Image> fromJpeg ( const std::filesystem::path& path )
54+ Expected<Image> fromJpeg ( const std::filesystem::path& path, bool ignoreDecompressErrors )
5555{
5656 std::ifstream in ( path, std::ios::binary );
5757 if ( !in )
5858 return unexpected ( " Cannot open file " + utf8string ( path ) );
5959
60- return addFileNameInError ( fromJpeg ( in ), path );
60+ return addFileNameInError ( fromJpeg ( in, ignoreDecompressErrors ), path );
6161}
6262
63- Expected<Image> fromJpeg ( std::istream& in )
63+ Expected<Image> fromJpeg ( std::istream& in, bool ignoreDecompressErrors )
6464{
6565 return
6666 readCharBuffer ( in )
67- .and_then ( [] ( auto && buffer )
67+ .and_then ( [ignoreDecompressErrors ] ( auto && buffer )
6868 {
69- return fromJpeg ( buffer.data (), buffer.size () );
69+ return fromJpeg ( buffer.data (), buffer.size (), ignoreDecompressErrors );
7070 } );
7171}
7272
73- Expected<Image> fromJpeg ( const char * data, size_t size )
73+ Expected<Image> fromJpeg ( const char * data, size_t size, bool ignoreDecompressErrors )
7474{
7575 JpegReader reader;
7676 if ( !reader.tjInstance )
@@ -85,13 +85,14 @@ Expected<Image> fromJpeg( const char* data, size_t size )
8585 image.pixels .resize ( width * height );
8686 image.resolution = { width, height };
8787 res = tjDecompress2 ( reader.tjInstance , ( const unsigned char * )data, ( unsigned long )size, reinterpret_cast < unsigned char * >( image.pixels .data () ), width, 0 , height, TJPF_RGBA, TJFLAG_BOTTOMUP );
88- if ( res != 0 )
88+ if ( res != 0 && !ignoreDecompressErrors )
8989 return unexpected ( " Failed to decompress JPEG file" );
9090
9191 return image;
9292}
9393
94- MR_ADD_IMAGE_LOADER_WITH_PRIORITY ( IOFilter( " JPEG (.jpg,.jpeg)" , " *.jpg;*.jpeg" ), fromJpeg, -1 )
94+ MR_ADD_IMAGE_LOADER_WITH_PRIORITY ( IOFilter( " JPEG (.jpg,.jpeg)" , " *.jpg;*.jpeg" ),
95+ []( const std::filesystem::path& path ){ return fromJpeg ( path ); }, -1 )
9596
9697} // namespace ImageLoad
9798
0 commit comments