1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 06:04:57 +00:00

LibPDF+LibGfx: Pass jbig2-filtered data to JBIG2ImageDecoderPlugin

Except for /JBIG2Globals, which we bail out on for now. In my 1000
files, 13 use JBIG2, and of those, 2 use JBIG2Globals (0000372.pdf e.g.
page 11 and 0000857.pdf e.g. page 1), and only one (the latter) of the
two uses the same JBIG2Globals stream for more than a single image.

JBIG2ImageDecoderPlugin cannot decode the data yet, so no behavior
change, but with `#define JBIG2_DEBUG 1` at the top of that file,
it now prints segment header info for PDFs containing JBIG2 data :^)
This commit is contained in:
Nico Weber 2024-03-03 21:05:53 -05:00 committed by Andreas Kling
parent bd60d1db7e
commit 953f6c5d9b
5 changed files with 26 additions and 5 deletions

View file

@ -180,7 +180,10 @@ static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
static ErrorOr<void> 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<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optio
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Draw the rest of the owl");
}
ErrorOr<ByteBuffer> 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");
}
}

View file

@ -25,6 +25,8 @@ public:
virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override;
static ErrorOr<ByteBuffer> decode_embedded(ReadonlyBytes);
private:
JBIG2ImageDecoderPlugin(ReadonlyBytes);

View file

@ -112,6 +112,7 @@
X(Info) \
X(Intent) \
X(JBIG2Decode) \
X(JBIG2Globals) \
X(JPXDecode) \
X(K) \
X(Keywords) \

View file

@ -10,6 +10,7 @@
#include <LibCompress/LZWDecoder.h>
#include <LibCompress/PackBitsDecoder.h>
#include <LibGfx/ImageFormats/CCITTDecoder.h>
#include <LibGfx/ImageFormats/JBIG2Loader.h>
#include <LibGfx/ImageFormats/JPEGLoader.h>
#include <LibGfx/ImageFormats/PNGLoader.h>
#include <LibPDF/CommonNames.h>
@ -33,7 +34,7 @@ PDFErrorOr<ByteBuffer> 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<ByteBuffer> Filter::decode_ccitt(ReadonlyBytes bytes, RefPtr<DictObje
return decoded;
}
PDFErrorOr<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes)
PDFErrorOr<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes bytes, RefPtr<DictObject> 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<ByteBuffer> Filter::decode_dct(ReadonlyBytes bytes)

View file

@ -26,7 +26,7 @@ private:
static PDFErrorOr<ByteBuffer> decode_flate(ReadonlyBytes bytes, RefPtr<DictObject> decode_parms);
static PDFErrorOr<ByteBuffer> decode_run_length(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_ccitt(ReadonlyBytes bytes, RefPtr<DictObject> decode_parms);
static PDFErrorOr<ByteBuffer> decode_jbig2(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_jbig2(ReadonlyBytes bytes, RefPtr<DictObject> decode_parms);
static PDFErrorOr<ByteBuffer> decode_dct(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_jpx(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_crypt(ReadonlyBytes bytes);