PDF colour spaces and functions: bound sizes, depth and stacks
A PDF could abort the process through its colour spaces and functions. An Indexed hival of 10^12 asked an image for a 24 TB palette. A sampled function's outputs (one per /Range pair) multiplied its table without a bound, and copies of the table (one per q, one per reference) multiplied it again. Self-referencing colour spaces and functions overflowed the stack, a calculator's copy doubled its operand stack, index, idiv and mod could panic, and f64::clamp panicked on a reversed /Domain or /Range. Clamp hival to 255, count samples with checked arithmetic (at most 32 inputs and outputs, 2^24 samples a table), share the samples, give one function read a budget of nodes and samples, limit nesting to 8 levels and the calculator stack to 100, and clamp with the bounds in either order. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
813c7c4650
commit
ff38282674
@@ -485,6 +485,343 @@ fn tiling_patterns_coloured_and_uncoloured() {
|
||||
assert_eq!(at(115, 15)[3], 0.0);
|
||||
}
|
||||
|
||||
/// Object 1 of a file holding just `objs`, read as a function.
|
||||
fn read_function(objs: Objs) -> color::Func {
|
||||
color::Func::read(&object::File::parse(&pdf(&objs, 1)), &object::Obj::Ref(1, 0))
|
||||
}
|
||||
|
||||
/// A sampled (type 0) function, read from a file holding just its stream.
|
||||
fn read_sampled_function(dict: &str, data: Vec<u8>) -> color::Func {
|
||||
read_function(vec![(1, dict.into(), Some(data))])
|
||||
}
|
||||
|
||||
/// Object 1 of a file holding just `objs`, read as a colour space.
|
||||
fn read_color_space(objs: Objs) -> color::Cs {
|
||||
color::color_space(&object::File::parse(&pdf(&objs, 1)), &object::Obj::Ref(1, 0), None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_function_over_the_sample_cap_is_unsupported() {
|
||||
// 4 Mi entries, as many as `/Size` allows; every `/Range` pair adds an output, 4 Mi more samples.
|
||||
let dict = |outputs: usize| format!("<< /FunctionType 0 /Domain [0 1] /Size [4194304] /Range [{}] /BitsPerSample 1 >>", "0 1 ".repeat(outputs));
|
||||
// Five outputs ask for 20 Mi samples, past the per-table cap (16 Mi); refused by the sample
|
||||
// count before any decode, so this is cheap and no lost cap reaches the large table below.
|
||||
assert!(matches!(read_sampled_function(&dict(5), vec![]), color::Func::Unsupported));
|
||||
// 32 outputs of an empty stream ask for 128 Mi zeros; also past the cap. Through parse() the
|
||||
// refused tint transform takes the Separation fallback: tint 0.25 paints grey 0.75, not black.
|
||||
let bytes =
|
||||
page_pdf("/Tint cs 0.25 scn 0 0 200 100 re f", "/ColorSpace << /Tint [/Separation /Ink /DeviceRGB 10 0 R] >>", vec![(10, dict(32), Some(vec![]))]);
|
||||
let doc = parse(&bytes).unwrap();
|
||||
assert!(doc.skipped.is_empty(), "{:?}", doc.skipped);
|
||||
assert_eq!(px(&doc, 50, 50), [0.75, 0.75, 0.75, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_function_one_input_two_outputs() {
|
||||
let func =
|
||||
read_sampled_function("<< /FunctionType 0 /Domain [0 1] /Size [4] /Range [0 1 -1 1] /BitsPerSample 8 >>", vec![0, 255, 85, 170, 170, 85, 255, 0]);
|
||||
assert!(matches!(func, color::Func::Sampled { outputs: 2, .. }));
|
||||
// 0.5 → coordinate 1.5, halfway between samples 1 and 2; the second output decodes to [-1, 1].
|
||||
assert_eq!(func.eval(&[0.5]), vec![0.5, 0.0]);
|
||||
// Inputs outside the domain take the end samples.
|
||||
assert_eq!(func.eval(&[-1.0]), vec![0.0, 1.0]);
|
||||
assert_eq!(func.eval(&[2.0]), vec![1.0, -1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_function_two_inputs() {
|
||||
let func = read_sampled_function(
|
||||
"<< /FunctionType 0 /Domain [0 1 0 1] /Size [3 3] /Range [0 1 0 1] /BitsPerSample 8 >>",
|
||||
vec![0, 0, 85, 0, 170, 0, 0, 85, 85, 85, 170, 85, 0, 170, 85, 170, 170, 170],
|
||||
);
|
||||
assert!(matches!(func, color::Func::SampledN { outputs: 2, .. }));
|
||||
// Entry (i, j), the first input varying fastest, holds (i / 3, j / 3); (0.25, 0.75) → (0.5, 1.5).
|
||||
assert_eq!(func.eval(&[0.25, 0.75]), vec![1.0 / 6.0, 0.5]);
|
||||
assert_eq!(func.eval(&[0.0, 0.0]), vec![0.0, 0.0]);
|
||||
assert_eq!(func.eval(&[1.0, 1.0]), vec![2.0 / 3.0, 2.0 / 3.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_functions_have_at_most_32_inputs_and_outputs() {
|
||||
// Each evaluation allocates and loops over every output, and shadings evaluate millions of
|
||||
// times; no colour space has more than 32 components.
|
||||
let outputs = |n: usize| format!("<< /FunctionType 0 /Domain [0 1] /Size [2] /Range [{}] /BitsPerSample 8 >>", "0 1 ".repeat(n));
|
||||
assert!(matches!(read_sampled_function(&outputs(32), vec![255; 64]), color::Func::Sampled { outputs: 32, .. }));
|
||||
assert_eq!(read_sampled_function(&outputs(33), vec![255; 66]), color::Func::Unsupported);
|
||||
let inputs = |m: usize| format!("<< /FunctionType 0 /Domain [{}] /Size [{}] /Range [0 1] /BitsPerSample 8 >>", "0 1 ".repeat(m), "1 ".repeat(m));
|
||||
let f = read_sampled_function(&inputs(32), vec![255]);
|
||||
assert!(matches!(f, color::Func::SampledN { .. }), "{f:?}");
|
||||
assert_eq!(f.eval(&[0.5; 32]), vec![1.0]);
|
||||
assert_eq!(read_sampled_function(&inputs(33), vec![255]), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
/// Object 10: a sampled function with one input, one output and samples 0 and 1.
|
||||
fn ramp() -> (u32, String, Option<Vec<u8>>) {
|
||||
(10, "<< /FunctionType 0 /Domain [0 1] /Size [2] /Range [0 1] /BitsPerSample 8 >>".into(), Some(vec![0, 255]))
|
||||
}
|
||||
|
||||
/// A stitching function of `n` equal pieces, each function object 10.
|
||||
fn stitching_of_10(n: usize) -> String {
|
||||
let bounds: Vec<String> = (1..n).map(|i| (i as f64 / n as f64).to_string()).collect();
|
||||
format!("<< /FunctionType 3 /Domain [0 1] /Functions [{}] /Bounds [{}] /Encode [{}] >>", "10 0 R ".repeat(n), bounds.join(" "), "0 1 ".repeat(n))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_arrays_hold_at_most_32_functions() {
|
||||
// An array of one-output functions gives an output per function: 32 at most, as for a single
|
||||
// function. (A stitching function's pieces are NOT capped this way; see the 100-piece test.)
|
||||
let array = |n: usize| format!("[{}]", "10 0 R ".repeat(n));
|
||||
assert!(matches!(read_function(vec![(1, array(32), None), ramp()]), color::Func::Array(fs) if fs.len() == 32));
|
||||
assert_eq!(read_function(vec![(1, array(33), None), ramp()]), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stitching_function_with_100_pieces_builds_and_evaluates() {
|
||||
// A repeating gradient (Cairo, Inkscape) lists one sub-function once per repeat, often far
|
||||
// more than 32 times; such a stitching function is valid and must build. Here object 10 (a
|
||||
// 0 → 1 ramp) is listed 100 times, read once and shared.
|
||||
let f = read_function(vec![(1, stitching_of_10(100), None), ramp()]);
|
||||
let color::Func::Stitch { funcs, .. } = &f else { panic!("{f:?}") };
|
||||
assert_eq!(funcs.len(), 100);
|
||||
let table = |g: &color::Func| match g {
|
||||
color::Func::Sampled { samples, .. } => Some(samples.as_ptr()),
|
||||
_ => None,
|
||||
};
|
||||
assert!(table(&funcs[0]).is_some());
|
||||
assert!(funcs.iter().all(|g| g == &funcs[0] && table(g) == table(&funcs[0])));
|
||||
// The sawtooth's ends: piece 0 at t = 0 and piece 99 at t = 1 both map through the ramp.
|
||||
assert_eq!(f.eval1(0.0), vec![0.0]);
|
||||
assert_eq!(f.eval1(1.0), vec![1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_function_repeated_in_an_array_is_read_once() {
|
||||
// Read once per reference, 32 references to one 32 MiB table made 1 GiB. Now the object is
|
||||
// read once: the copies are equal and share its table.
|
||||
let table = |f: &color::Func| match f {
|
||||
color::Func::Sampled { samples, .. } => samples.as_ptr(),
|
||||
_ => std::ptr::null(),
|
||||
};
|
||||
let array = read_function(vec![(1, format!("[{}]", "10 0 R ".repeat(32)), None), ramp()]);
|
||||
let stitch = read_function(vec![(1, stitching_of_10(32), None), ramp()]);
|
||||
for f in [&array, &stitch] {
|
||||
let (color::Func::Array(fs) | color::Func::Stitch { funcs: fs, .. }) = f else { panic!("{f:?}") };
|
||||
assert_eq!(fs.len(), 32);
|
||||
assert!(!table(&fs[0]).is_null());
|
||||
assert!(fs.iter().all(|g| g == &fs[0] && table(g) == table(&fs[0])));
|
||||
}
|
||||
assert_eq!(array.eval1(0.5), vec![0.5; 32]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_graphics_states_share_a_sampled_tint() {
|
||||
// `q` saves a copy of the graphics state, colour spaces included: the copies share the tint
|
||||
// transform's table.
|
||||
let tint = "<< /FunctionType 0 /Domain [0 1] /Size [4096] /Range [0 1 0 1 0 1] /BitsPerSample 8 >>";
|
||||
let data: Vec<u8> = (0..4096u32).flat_map(|i| [(i >> 4) as u8, 0, 255 - (i >> 4) as u8]).collect();
|
||||
let cs = read_color_space(vec![(1, "[/Separation /Ink /DeviceRGB 10 0 R]".into(), None), (10, tint.into(), Some(data.clone()))]);
|
||||
let table = |cs: &color::Cs| match cs {
|
||||
color::Cs::Tint { func: color::Func::Sampled { samples, .. }, .. } => samples.as_ptr(),
|
||||
_ => std::ptr::null(),
|
||||
};
|
||||
assert!(!table(&cs).is_null());
|
||||
assert_eq!(table(&cs.clone()), table(&cs));
|
||||
// A thousand nested `q` around a fill in that space: tint 1 is the last entry, red.
|
||||
let content = format!("/Tint cs 1 scn {}0 0 200 100 re f {}", "q ".repeat(1000), "Q ".repeat(1000));
|
||||
let doc = parse(&page_pdf(&content, "/ColorSpace << /Tint [/Separation /Ink /DeviceRGB 10 0 R] >>", vec![(10, tint.into(), Some(data))])).unwrap();
|
||||
assert!(doc.skipped.is_empty(), "{:?}", doc.skipped);
|
||||
assert_eq!(px(&doc, 100, 50), [1.0, 0.0, 0.0, 1.0]);
|
||||
}
|
||||
|
||||
/// A one-page PDF drawing image object 10 over the whole 200 × 100 page; `extra` holds more objects.
|
||||
fn image_page(dict: &str, data: Vec<u8>, extra: Objs) -> Vec<u8> {
|
||||
let mut objs: Objs = vec![(10, dict.into(), Some(data))];
|
||||
objs.extend(extra);
|
||||
page_pdf("q 200 0 0 100 0 0 cm /Im1 Do Q", "/XObject << /Im1 10 0 R >>", objs)
|
||||
}
|
||||
|
||||
/// Whether two colours match within rendering error.
|
||||
fn near(a: [f32; 4], b: [f32; 4]) -> bool {
|
||||
a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.02)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_space_reads_at_most_256_entries() {
|
||||
// hival 10^12 asked the image palette for 10^12 colours (24 TB); an indexed palette holds at
|
||||
// most 256 entries, so hival is 0 to 255.
|
||||
let im = "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace [/Indexed /DeviceRGB 1000000000000 <00>] /BitsPerComponent 8 >>";
|
||||
let doc = parse(&image_page(im, vec![200], vec![])).unwrap();
|
||||
assert!(doc.skipped.is_empty(), "{:?}", doc.skipped);
|
||||
// Entry 200 is past the one-byte lookup string: black.
|
||||
assert!(near(px(&doc, 100, 50), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&doc, 100, 50));
|
||||
let cs = read_color_space(vec![(1, "[/Indexed /DeviceRGB 1000000000000 <00>]".into(), None)]);
|
||||
assert!(matches!(cs, color::Cs::Indexed { hival: 255, .. }), "{cs:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_images_with_4_and_256_entries() {
|
||||
let im = |cs: &str, w: usize| format!("<< /Type /XObject /Subtype /Image /Width {w} /Height 1 /ColorSpace {cs} /BitsPerComponent 8 >>");
|
||||
let doc = parse(&image_page(&im("[/Indexed /DeviceRGB 3 <FF000000FF000000FFFFFFFF>]", 4), vec![0, 1, 2, 3], vec![])).unwrap();
|
||||
for (x, c) in [(25, [1.0, 0.0, 0.0, 1.0]), (75, [0.0, 1.0, 0.0, 1.0]), (125, [0.0, 0.0, 1.0, 1.0]), (175, [1.0; 4])] {
|
||||
assert!(near(px(&doc, x, 50), c), "x = {x}: {:?}", px(&doc, x, 50));
|
||||
}
|
||||
// 256 entries, the lookup table in a stream: entry i is (i, 0, 255 − i).
|
||||
let lookup: Vec<u8> = (0..=255u8).flat_map(|i| [i, 0, 255 - i]).collect();
|
||||
let doc = parse(&image_page(&im("[/Indexed /DeviceRGB 255 11 0 R]", 2), vec![255, 0], vec![(11, "<< >>".into(), Some(lookup))])).unwrap();
|
||||
assert!(near(px(&doc, 50, 50), [1.0, 0.0, 0.0, 1.0]), "{:?}", px(&doc, 50, 50));
|
||||
assert!(near(px(&doc, 150, 50), [0.0, 0.0, 1.0, 1.0]), "{:?}", px(&doc, 150, 50));
|
||||
}
|
||||
|
||||
/// Functions nested down the first element of arrays and stitching functions, outermost first.
|
||||
fn nested_functions(f: &color::Func) -> Vec<&color::Func> {
|
||||
let mut out = vec![f];
|
||||
let mut f = f;
|
||||
while let color::Func::Array(fs) | color::Func::Stitch { funcs: fs, .. } = f
|
||||
&& let Some(first) = fs.first()
|
||||
{
|
||||
out.push(first);
|
||||
f = first;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Colour spaces nested down Indexed bases and Separation or DeviceN alternates, outermost first.
|
||||
fn nested_spaces(cs: &color::Cs) -> Vec<&color::Cs> {
|
||||
let mut out = vec![cs];
|
||||
let mut cs = cs;
|
||||
while let color::Cs::Indexed { base: next, .. } | color::Cs::Tint { alt: next, .. } = cs {
|
||||
cs = next;
|
||||
out.push(cs);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_array_containing_itself_stops_at_depth_8() {
|
||||
// `[1 0 R]` as object 1: the reader recursed until the stack overflowed (an abort).
|
||||
let f = read_function(vec![(1, "[1 0 R]".into(), None)]);
|
||||
let chain = nested_functions(&f);
|
||||
assert_eq!(chain.len(), 9);
|
||||
assert!(chain[..8].iter().all(|f| matches!(f, color::Func::Array(_))));
|
||||
assert_eq!(chain[8], &color::Func::Unsupported);
|
||||
// As a tint transform: the innermost function's 0.5, in DeviceGray.
|
||||
let tint = "/ColorSpace << /Tint [/Separation /Ink /DeviceGray 10 0 R] >>";
|
||||
let doc = parse(&page_pdf("/Tint cs 1 scn 0 0 200 100 re f", tint, vec![(10, "[10 0 R]".into(), None)])).unwrap();
|
||||
assert_eq!(px(&doc, 100, 50), [0.5, 0.5, 0.5, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stitching_function_containing_itself_stops_at_depth_8() {
|
||||
let stitch = |me: u32| format!("<< /FunctionType 3 /Domain [0 1] /Functions [{me} 0 R] /Bounds [] /Encode [0 1] >>");
|
||||
let f = read_function(vec![(1, stitch(1), None)]);
|
||||
let chain = nested_functions(&f);
|
||||
assert_eq!(chain.len(), 9);
|
||||
assert!(chain[..8].iter().all(|f| matches!(f, color::Func::Stitch { .. })));
|
||||
assert_eq!(chain[8], &color::Func::Unsupported);
|
||||
let tint = "/ColorSpace << /Tint [/Separation /Ink /DeviceGray 10 0 R] >>";
|
||||
let doc = parse(&page_pdf("/Tint cs 1 scn 0 0 200 100 re f", tint, vec![(10, stitch(10), None)])).unwrap();
|
||||
assert_eq!(px(&doc, 100, 50), [0.5, 0.5, 0.5, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colour_spaces_based_on_themselves_stop_at_depth_8() {
|
||||
// Object 1 as its own Indexed base, Separation alternate or ICCBased alternate: the reader
|
||||
// recursed until the stack overflowed (an abort).
|
||||
let indexed = read_color_space(vec![(1, "[/Indexed 1 0 R 1 <00>]".into(), None)]);
|
||||
let separation = read_color_space(vec![(1, "[/Separation /Ink 1 0 R << /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 1 >>]".into(), None)]);
|
||||
for cs in [&indexed, &separation] {
|
||||
let chain = nested_spaces(cs);
|
||||
assert_eq!(chain.len(), 9);
|
||||
assert!(chain[..8].iter().all(|c| matches!(c, color::Cs::Indexed { .. } | color::Cs::Tint { .. })));
|
||||
assert_eq!(chain[8], &color::Cs::Gray);
|
||||
}
|
||||
// An ICCBased space reads as its alternate: DeviceGray once the depth runs out.
|
||||
let icc = read_color_space(vec![(1, "[/ICCBased 2 0 R]".into(), None), (2, "<< /N 3 /Alternate 1 0 R >>".into(), Some(vec![0; 16]))]);
|
||||
assert_eq!(icc, color::Cs::Gray);
|
||||
// A fill in the Indexed one paints black.
|
||||
let doc = parse(&page_pdf("/CS0 cs 0 scn 0 0 200 100 re f", "/ColorSpace << /CS0 10 0 R >>", vec![(10, "[/Indexed 10 0 R 1 <00>]".into(), None)])).unwrap();
|
||||
assert_eq!(px(&doc, 100, 50), [0.0, 0.0, 0.0, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nesting_deeper_than_8_is_refused() {
|
||||
// Chains of 100 distinct objects, each nesting the next (real files nest two or three
|
||||
// levels): read down to depth 7, refused at depth 8.
|
||||
let functions = |n: u32| -> Objs {
|
||||
let stitch = |next: u32| format!("<< /FunctionType 3 /Domain [0 1] /Functions [{next} 0 R] /Bounds [] /Encode [0 1] >>");
|
||||
(1..n).map(|i| (i, stitch(i + 1), None)).chain([(n, "<< /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 1 >>".into(), None)]).collect()
|
||||
};
|
||||
let f = read_function(functions(100));
|
||||
let chain = nested_functions(&f);
|
||||
assert_eq!(chain.len(), 9);
|
||||
assert!(chain[..8].iter().all(|f| matches!(f, color::Func::Stitch { .. })));
|
||||
assert_eq!(chain[8], &color::Func::Unsupported);
|
||||
// Three levels are read to the end.
|
||||
let f = read_function(functions(3));
|
||||
assert_eq!(nested_functions(&f).len(), 3);
|
||||
assert_eq!(f.eval1(0.25), vec![0.25]);
|
||||
// Indexed bases.
|
||||
let spaces: Objs =
|
||||
(1..100).map(|i| (i, format!("[/Indexed {} 0 R 0 <00>]", i + 1), None)).chain([(100, "[/Indexed /DeviceRGB 0 <FF0000>]".into(), None)]).collect();
|
||||
let cs = read_color_space(spaces);
|
||||
let chain = nested_spaces(&cs);
|
||||
assert_eq!(chain.len(), 9);
|
||||
assert!(chain[..8].iter().all(|c| matches!(c, color::Cs::Indexed { .. })));
|
||||
assert_eq!(chain[8], &color::Cs::Gray);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fanning_out_reference_graph_is_refused_within_the_budget() {
|
||||
// Eight objects, each an array of 32 references to the next, with a function at the bottom:
|
||||
// depth 8 alone would still expand 32^7 copies. Each object is read once per array but its
|
||||
// nodes are charged per copy, so the per-read node budget is spent after a few levels and the
|
||||
// whole function is refused — fast and with bounded memory, not 32^7 nodes.
|
||||
let objs: Objs = (1..=7u32)
|
||||
.map(|i| (i, format!("[{}]", format!("{} 0 R ", i + 1).repeat(32)), None))
|
||||
.chain([(8, "<< /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 1 >>".into(), None)])
|
||||
.collect();
|
||||
assert_eq!(read_function(objs), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colour_space_chain_of_depth_3() {
|
||||
// Indexed → Separation → ICCBased → DeviceRGB: entry 0 is tint 1 (blue), entry 1 tint 0 (white).
|
||||
let doc = parse(&image_page(
|
||||
"<< /Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace [/Indexed 11 0 R 1 <FF00>] /BitsPerComponent 8 >>",
|
||||
vec![0, 1],
|
||||
vec![
|
||||
(11, "[/Separation /Ink 12 0 R 13 0 R]".into(), None),
|
||||
(12, "[/ICCBased 14 0 R]".into(), None),
|
||||
(13, "<< /FunctionType 2 /Domain [0 1] /C0 [1 1 1] /C1 [0 0 1] /N 1 >>".into(), None),
|
||||
(14, "<< /N 3 /Alternate /DeviceRGB >>".into(), Some(vec![0; 16])),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
assert!(doc.skipped.is_empty(), "{:?}", doc.skipped);
|
||||
assert!(near(px(&doc, 50, 50), [0.0, 0.0, 1.0, 1.0]), "{:?}", px(&doc, 50, 50));
|
||||
assert!(near(px(&doc, 150, 50), [1.0; 4]), "{:?}", px(&doc, 150, 50));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stitching_function_with_a_reversed_domain() {
|
||||
// `f64::clamp` panics on bounds in the wrong order, as `/Domain [1 0]` gives them.
|
||||
let exp = "<< /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 1 >>";
|
||||
let f = read_function(vec![(1, format!("<< /FunctionType 3 /Domain [1 0] /Functions [{exp}] /Bounds [] /Encode [0 1] >>"), None)]);
|
||||
assert_eq!(f.eval(&[0.25]), vec![0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_function_with_a_reversed_domain() {
|
||||
let f = read_sampled_function("<< /FunctionType 0 /Domain [1 0] /Size [2] /Range [0 1] /BitsPerSample 8 >>", vec![0, 255]);
|
||||
assert_eq!(f.eval(&[0.25]), vec![0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calculator_function_with_a_reversed_range() {
|
||||
let f = read_function(vec![(1, "<< /FunctionType 4 /Domain [0 1] /Range [1 0] >>".into(), Some(b"{ 0.5 add }".to_vec()))]);
|
||||
assert_eq!(f.eval(&[0.25]), vec![0.75]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_pages_and_calculator_shadings() {
|
||||
let objs: Objs = vec![
|
||||
|
||||
Reference in New Issue
Block a user