diff --git a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp index b88c0d2e86..4a3fcbfc6c 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp @@ -180,7 +180,10 @@ static ErrorOr decode_segment_header(SeekableStream& stream) static ErrorOr decode_segment_headers(JBIG2LoadingContext& context) { - FixedMemoryStream stream(context.data.slice(sizeof(id_string) + sizeof(u8) + (context.number_of_pages.has_value() ? sizeof(u32) : 0))); + ReadonlyBytes data = context.data; + if (context.organization != Organization::Embedded) + data = data.slice(sizeof(id_string) + sizeof(u8) + (context.number_of_pages.has_value() ? sizeof(u32) : 0)); + FixedMemoryStream stream(data); while (!stream.is_eof()) { auto segment_header = TRY(decode_segment_header(stream)); if (context.organization != Organization::RandomAccess) { @@ -232,4 +235,12 @@ ErrorOr JBIG2ImageDecoderPlugin::frame(size_t index, Optio return Error::from_string_literal("JBIG2ImageDecoderPlugin: Draw the rest of the owl"); } +ErrorOr JBIG2ImageDecoderPlugin::decode_embedded(ReadonlyBytes data) +{ + auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin(data))); + plugin->m_context->organization = Organization::Embedded; + TRY(decode_segment_headers(*plugin->m_context)); + return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode embedded JBIG2 yet"); +} + } diff --git a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.h b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.h index 94779c095a..2203a27df6 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.h @@ -25,6 +25,8 @@ public: virtual ErrorOr frame(size_t index, Optional ideal_size = {}) override; + static ErrorOr decode_embedded(ReadonlyBytes); + private: JBIG2ImageDecoderPlugin(ReadonlyBytes); diff --git a/Userland/Libraries/LibPDF/CommonNames.h b/Userland/Libraries/LibPDF/CommonNames.h index 36c3a38c34..55091ec137 100644 --- a/Userland/Libraries/LibPDF/CommonNames.h +++ b/Userland/Libraries/LibPDF/CommonNames.h @@ -112,6 +112,7 @@ X(Info) \ X(Intent) \ X(JBIG2Decode) \ + X(JBIG2Globals) \ X(JPXDecode) \ X(K) \ X(Keywords) \ diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp index 616e9e55ef..b12cd85f86 100644 --- a/Userland/Libraries/LibPDF/Filter.cpp +++ b/Userland/Libraries/LibPDF/Filter.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -33,7 +34,7 @@ PDFErrorOr Filter::decode(ReadonlyBytes bytes, DeprecatedFlyString c if (encoding_type == CommonNames::CCITTFaxDecode) return decode_ccitt(bytes, decode_parms); if (encoding_type == CommonNames::JBIG2Decode) - return decode_jbig2(bytes); + return decode_jbig2(bytes, decode_parms); if (encoding_type == CommonNames::DCTDecode) return decode_dct(bytes); if (encoding_type == CommonNames::JPXDecode) @@ -333,9 +334,15 @@ PDFErrorOr Filter::decode_ccitt(ReadonlyBytes bytes, RefPtr Filter::decode_jbig2(ReadonlyBytes) +PDFErrorOr Filter::decode_jbig2(ReadonlyBytes bytes, RefPtr decode_parms) { - return Error::rendering_unsupported_error("JBIG2 Filter is unsupported"); + // 3.3.6 JBIG2Decode Filter + if (decode_parms) { + if (decode_parms->contains(CommonNames::JBIG2Globals)) + return Error::rendering_unsupported_error("JBIG2Globals is not yet supported"); + } + + return TRY(Gfx::JBIG2ImageDecoderPlugin::decode_embedded(bytes)); } PDFErrorOr Filter::decode_dct(ReadonlyBytes bytes) diff --git a/Userland/Libraries/LibPDF/Filter.h b/Userland/Libraries/LibPDF/Filter.h index 8b09d9bcd5..17a453b694 100644 --- a/Userland/Libraries/LibPDF/Filter.h +++ b/Userland/Libraries/LibPDF/Filter.h @@ -26,7 +26,7 @@ private: static PDFErrorOr decode_flate(ReadonlyBytes bytes, RefPtr decode_parms); static PDFErrorOr decode_run_length(ReadonlyBytes bytes); static PDFErrorOr decode_ccitt(ReadonlyBytes bytes, RefPtr decode_parms); - static PDFErrorOr decode_jbig2(ReadonlyBytes bytes); + static PDFErrorOr decode_jbig2(ReadonlyBytes bytes, RefPtr decode_parms); static PDFErrorOr decode_dct(ReadonlyBytes bytes); static PDFErrorOr decode_jpx(ReadonlyBytes bytes); static PDFErrorOr decode_crypt(ReadonlyBytes bytes);