From 2331fe5e681d11c41ea53a25a09968c15812b91e Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Fri, 25 Nov 2022 21:55:04 +0800 Subject: [PATCH] LibPDF: Add first interpolation methods Interpolation is needed in more than one place, and I couldn't find a central place where I could borrow a readily available interpolation routine, so I've implemented the first simple interpolation object. More will follow for more complex scenarios. --- Userland/Libraries/LibPDF/CMakeLists.txt | 1 + Userland/Libraries/LibPDF/Interpolation.cpp | 35 +++++++++++++++++++++ Userland/Libraries/LibPDF/Interpolation.h | 27 ++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 Userland/Libraries/LibPDF/Interpolation.cpp create mode 100644 Userland/Libraries/LibPDF/Interpolation.h diff --git a/Userland/Libraries/LibPDF/CMakeLists.txt b/Userland/Libraries/LibPDF/CMakeLists.txt index f28fba2713..27106da02f 100644 --- a/Userland/Libraries/LibPDF/CMakeLists.txt +++ b/Userland/Libraries/LibPDF/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES Fonts/TrueTypeFont.cpp Fonts/Type0Font.cpp Fonts/Type1Font.cpp + Interpolation.cpp ObjectDerivatives.cpp Parser.cpp Reader.cpp diff --git a/Userland/Libraries/LibPDF/Interpolation.cpp b/Userland/Libraries/LibPDF/Interpolation.cpp new file mode 100644 index 0000000000..d42016cc6e --- /dev/null +++ b/Userland/Libraries/LibPDF/Interpolation.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022, Rodrigo Tobar . + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace PDF { + +static float slope(float x_min, float x_max, float y_min, float y_max) +{ + return (y_max - y_min) / (x_max - x_min); +} + +LinearInterpolation1D::LinearInterpolation1D(float x_min, float x_max, float y_min, float y_max) + : m_x_min(x_min) + , m_y_min(y_min) + , m_slope(slope(x_min, x_max, y_min, y_max)) +{ +} + +float LinearInterpolation1D::interpolate(float x) const +{ + return m_y_min + ((x - m_x_min) * m_slope); +} + +void LinearInterpolation1D::interpolate(Span const& x, Span y) const +{ + for (size_t i = 0; i < x.size(); ++i) { + y[i] = interpolate(x[i]); + } +} + +} diff --git a/Userland/Libraries/LibPDF/Interpolation.h b/Userland/Libraries/LibPDF/Interpolation.h new file mode 100644 index 0000000000..39f0e49f44 --- /dev/null +++ b/Userland/Libraries/LibPDF/Interpolation.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022, Rodrigo Tobar . + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace PDF { + +class LinearInterpolation1D { + +public: + LinearInterpolation1D(float x_min, float x_max, float y_min, float y_max); + float interpolate(float) const; + void interpolate(Span const& x, Span y) const; + +private: + float m_x_min; + float m_y_min; + float m_slope; +}; + +}