Skip to content

Commit

Permalink
LibPDF: Add test for SampledFunction and fix bugs found by it
Browse files Browse the repository at this point in the history
* SampledFunction now keeps the StreamObject it gets data from alive
  (doesn't matter too much in practice, but does matter in the test,
  where nothing else keeps the stream alive).

* If a sample is an integer, we would previously sample that value
  twice and then divide by zero when interpolating. Make sure to
  sample 1 unit apart.
  • Loading branch information
nico committed Nov 10, 2023
1 parent 323ba74 commit 84e9962
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
38 changes: 35 additions & 3 deletions Tests/LibPDF/TestPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,53 @@ static PDF::Value make_array(Vector<float> floats)
return PDF::Value { adopt_ref(*new PDF::ArrayObject(move(values))) };
}

static PDF::PDFErrorOr<NonnullRefPtr<PDF::Function>> make_postscript_function(StringView program, Vector<float> domain, Vector<float> range)
static PDF::PDFErrorOr<NonnullRefPtr<PDF::Function>> make_function(int type, ReadonlyBytes data, Vector<float> domain, Vector<float> range, Function<void(HashMap<DeprecatedFlyString, PDF::Value>&)> extra_keys = nullptr)
{
HashMap<DeprecatedFlyString, PDF::Value> map;
map.set(PDF::CommonNames::FunctionType, PDF::Value { 4 });
map.set(PDF::CommonNames::FunctionType, PDF::Value { type });
map.set(PDF::CommonNames::Domain, make_array(move(domain)));
map.set(PDF::CommonNames::Range, make_array(move(range)));
if (extra_keys)
extra_keys(map);
auto dict = adopt_ref(*new PDF::DictObject(move(map)));
auto stream = adopt_ref(*new PDF::StreamObject(dict, MUST(ByteBuffer::copy(program.bytes()))));
auto stream = adopt_ref(*new PDF::StreamObject(dict, MUST(ByteBuffer::copy(data))));

// document isn't used for anything, but UBSan complains about a (harmless) method call on a null object without it.
auto file = MUST(Core::MappedFile::map("linearized.pdf"sv));
auto document = MUST(PDF::Document::create(file->bytes()));
return PDF::Function::create(document, stream);
}

static PDF::PDFErrorOr<NonnullRefPtr<PDF::Function>> make_sampled_function(ReadonlyBytes data, Vector<float> domain, Vector<float> range, Vector<float> sizes)
{
return make_function(0, data, move(domain), move(range), [&sizes](auto& map) {
map.set(PDF::CommonNames::Size, make_array(sizes));
map.set(PDF::CommonNames::BitsPerSample, PDF::Value { 8 });
});;
}

TEST_CASE(sampled)
{
auto f1 = MUST(make_sampled_function(Vector<u8> { { 0, 255, 0 } }, { 0.0f, 1.0f }, { 0.0f, 10.0f }, { 3 }));
EXPECT_EQ(MUST(f1->evaluate(Vector<float> { 0.0f })), Vector<float> { 0.0f });
EXPECT_EQ(MUST(f1->evaluate(Vector<float> { 0.25f })), Vector<float> { 5.0f });
EXPECT_EQ(MUST(f1->evaluate(Vector<float> { 0.5f })), Vector<float> { 10.0f });
EXPECT_EQ(MUST(f1->evaluate(Vector<float> { 0.75f })), Vector<float> { 5.0f });
EXPECT_EQ(MUST(f1->evaluate(Vector<float> { 1.0f })), Vector<float> { 0.0f });

auto f2 = MUST(make_sampled_function(Vector<u8> { { 0, 255, 0, 255, 0, 255 } }, { 0.0f, 1.0f }, { 0.0f, 10.0f, 0.0f, 8.0f }, { 3 }));
EXPECT_EQ(MUST(f2->evaluate(Vector<float> { 0.0f })), (Vector<float> { 0.0f, 8.0f }));
EXPECT_EQ(MUST(f2->evaluate(Vector<float> { 0.25f })), (Vector<float> { 5.0f, 4.0f }));
EXPECT_EQ(MUST(f2->evaluate(Vector<float> { 0.5f })), (Vector<float> { 10.0f, 0.0f }));
EXPECT_EQ(MUST(f2->evaluate(Vector<float> { 0.75f })), (Vector<float> { 5.0f, 4.0f }));
EXPECT_EQ(MUST(f2->evaluate(Vector<float> { 1.0f })), (Vector<float> { 0.0f, 8.0f }));
}

static PDF::PDFErrorOr<NonnullRefPtr<PDF::Function>> make_postscript_function(StringView program, Vector<float> domain, Vector<float> range)
{
return make_function(4, program.bytes(), move(domain), move(range));
}

static NonnullRefPtr<PDF::Function> check_postscript_function(StringView program, Vector<float> domain, Vector<float> range)
{
auto function = make_postscript_function(program, move(domain), move(range));
Expand Down
18 changes: 16 additions & 2 deletions Userland/Libraries/LibPDF/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SampledFunction final : public Function {
virtual PDFErrorOr<ReadonlySpan<float>> evaluate(ReadonlySpan<float>) const override;

private:
SampledFunction(NonnullRefPtr<StreamObject>);

Vector<Bound> m_domain;
Vector<Bound> m_range;

Expand All @@ -41,11 +43,18 @@ class SampledFunction final : public Function {
Vector<Bound> m_encode;
Vector<Bound> m_decode;

NonnullRefPtr<StreamObject> m_stream;
ReadonlyBytes m_sample_data;

Vector<float> mutable m_outputs;
};

SampledFunction::SampledFunction(NonnullRefPtr<StreamObject> stream)
: m_stream(move(stream))
, m_sample_data(m_stream->bytes())
{
}

PDFErrorOr<NonnullRefPtr<SampledFunction>>
SampledFunction::create(Document* document, Vector<Bound> domain, Optional<Vector<Bound>> range, NonnullRefPtr<StreamObject> stream)
{
Expand Down Expand Up @@ -127,15 +136,14 @@ SampledFunction::create(Document* document, Vector<Bound> domain, Optional<Vecto
if (stream->bytes().size() < ceil_div(total_bits, 8ull))
return Error { Error::Type::MalformedPDF, "Function type 0 stream too small" };

auto function = adopt_ref(*new SampledFunction());
auto function = adopt_ref(*new SampledFunction(stream));
function->m_domain = move(domain);
function->m_range = move(range.value());
function->m_sizes = move(sizes);
function->m_bits_per_sample = bits_per_sample;
function->m_order = order;
function->m_encode = move(encode);
function->m_decode = move(decode);
function->m_sample_data = stream->bytes();
function->m_outputs.resize(function->m_range.size());
return function;
}
Expand Down Expand Up @@ -164,6 +172,12 @@ PDFErrorOr<ReadonlySpan<float>> SampledFunction::evaluate(ReadonlySpan<float> x)

float e0 = floor(ec);
float e1 = ceil(ec);
if (e0 == e1) {
if (e0 == 0.0f)
e1 = 1.0f;
else
e0 = e1 - 1.0f;
}
size_t plane_size = m_sizes[0];
for (size_t i = 0; i < m_range.size(); ++i) {
float s0 = m_sample_data[(size_t)e0 + i * plane_size];
Expand Down

0 comments on commit 84e9962

Please sign in to comment.