Skip to content

Commit bee6bb0

Browse files
committed
fromJpeg: ignoreDecompressErrors option
1 parent 3e52ac4 commit bee6bb0

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

source/MRIOExtras/MRJpeg.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ namespace MR
5151
namespace 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

source/MRIOExtras/MRJpeg.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ namespace ImageLoad
1616
{
1717

1818
/// loads from .jpg format
19-
MRIOEXTRAS_API Expected<Image> fromJpeg( const std::filesystem::path& path );
20-
MRIOEXTRAS_API Expected<Image> fromJpeg( std::istream& in );
21-
MRIOEXTRAS_API Expected<Image> fromJpeg( const char* data, size_t size );
19+
/// \param ignoreDecompressErrors if true do not return decompression error if the header was read sucessfully
20+
MRIOEXTRAS_API Expected<Image> fromJpeg( const std::filesystem::path& path, bool ignoreDecompressErrors = false );
21+
MRIOEXTRAS_API Expected<Image> fromJpeg( std::istream& in, bool ignoreDecompressErrors = false );
22+
MRIOEXTRAS_API Expected<Image> fromJpeg( const char* data, size_t size, bool ignoreDecompressErrors = false );
2223

2324
} // namespace ImageLoad
2425

0 commit comments

Comments
 (0)