Skip to content

object: do not panic on a sampled function with an inverted /Domain - #296

Open
apollo-2006 wants to merge 1 commit into
pdf-rs:masterfrom
apollo-2006:sampled-function-inverted-domain
Open

apollo-2006 wants to merge 1 commit into
pdf-rs:masterfrom
apollo-2006:sampled-function-inverted-domain

Conversation

@apollo-2006

Copy link
Copy Markdown

SampledFunctionInput::map clamps its input with x.clamp(self.domain.0, self.domain.1), and f32::clamp panics when the lower bound is greater than the upper one. /Domain is used as written in the file, so a Type 0 function with /Domain [10 1], like the one attached to #288, panics as soon as it is evaluated.

This orders the bounds before clamping, the same way StitchingFunction::apply already does. The issue suggests rejecting such a function when it is loaded instead. I went with the lenient version to match the stitching function, but I am happy to switch to the strict one if you prefer it.

Testing:

  • A new test in object::function::tests builds a two-sample ramp with /Domain [1 0] through Function::from_primitive and evaluates it at points inside and outside the domain. Without the change it panics with min > max, or either was NaN. min = 1.0, max = 0.0.
  • The file from the issue now evaluates without panicking.
  • cargo test --workspace passes.

While testing I noticed a separate problem: map applies /Encode as x * Encode[1] + Encode[0], without taking /Domain into account. The spec maps the domain onto the encode range, so a well-formed two-sample ramp with /Domain [0 2] gives 1.0 at x = 1 where it should give 0.5. That one affects valid files, so I have left it out of this PR and can send it separately.

Fixes #288

f32::clamp panics when min > max, and /Domain is used as written in the
file, so evaluating a Type 0 function with /Domain [10 1] panicked.
Order the bounds before clamping, as the stitching function does.

Fixes pdf-rs#288
Copilot AI lite review requested due to automatic review settings September 11, 2026 17:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in SampledFunction::apply when Type 0 /Domain bounds are inverted

2 participants