Skip to content

Commit

Permalink
LibGfx/JBIG2: Extract check_valid_adaptive_template_pixel()
Browse files Browse the repository at this point in the history
No behavior change.
  • Loading branch information
nico committed Apr 4, 2024
1 parent 13c4b83 commit 99993bf
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,17 @@ struct AdaptiveTemplatePixel {
i8 y { 0 };
};

// Figure 7 – Field to which AT pixel locations are restricted
ErrorOr<void> check_valid_adaptive_template_pixel(AdaptiveTemplatePixel const& adaptive_template_pixel)
{
// Don't have to check < -127 or > 127: The offsets are stored in an i8, so they can't be out of those bounds.
if (adaptive_template_pixel.y > 0)
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel y too big");
if (adaptive_template_pixel.y == 0 && adaptive_template_pixel.x > -1)
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel x too big");
return {};
}

// 6.2.2 Input parameters
// Table 2 – Parameters for the generic region decoding procedure
struct GenericRegionDecodingInputParameters {
Expand Down Expand Up @@ -945,19 +956,9 @@ static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(Gener
if (inputs.is_extended_reference_template_used)
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode EXTTEMPLATE yet");

// Figure 7 – Field to which AT pixel locations are restricted
int number_of_adaptive_template_pixels = 0;
if (inputs.gb_template == 0)
number_of_adaptive_template_pixels = 4;
else
number_of_adaptive_template_pixels = 1;
for (int i = 0; i < number_of_adaptive_template_pixels; ++i) {
// Don't have to check < -127 or > 127: The offsets are stored in an i8, so they can't be out of those bounds.
if (inputs.adaptive_template_pixels[i].y > 0)
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel y too big");
if (inputs.adaptive_template_pixels[i].y == 0 && inputs.adaptive_template_pixels[i].x > -1)
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel x too big");
}
int number_of_adaptive_template_pixels = inputs.gb_template == 0 ? 4 : 1;
for (int i = 0; i < number_of_adaptive_template_pixels; ++i)
TRY(check_valid_adaptive_template_pixel(inputs.adaptive_template_pixels[i]));

if (inputs.skip_pattern.has_value())
return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode USESKIP yet");
Expand Down

0 comments on commit 99993bf

Please sign in to comment.