mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:08:12 +00:00
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.
This commit is contained in:
parent
e818c955b3
commit
2331fe5e68
3 changed files with 63 additions and 0 deletions
|
@ -11,6 +11,7 @@ set(SOURCES
|
||||||
Fonts/TrueTypeFont.cpp
|
Fonts/TrueTypeFont.cpp
|
||||||
Fonts/Type0Font.cpp
|
Fonts/Type0Font.cpp
|
||||||
Fonts/Type1Font.cpp
|
Fonts/Type1Font.cpp
|
||||||
|
Interpolation.cpp
|
||||||
ObjectDerivatives.cpp
|
ObjectDerivatives.cpp
|
||||||
Parser.cpp
|
Parser.cpp
|
||||||
Reader.cpp
|
Reader.cpp
|
||||||
|
|
35
Userland/Libraries/LibPDF/Interpolation.cpp
Normal file
35
Userland/Libraries/LibPDF/Interpolation.cpp
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Rodrigo Tobar <rtobarc@gmail.com>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibPDF/Interpolation.h>
|
||||||
|
|
||||||
|
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<float> const& x, Span<float> y) const
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < x.size(); ++i) {
|
||||||
|
y[i] = interpolate(x[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
Userland/Libraries/LibPDF/Interpolation.h
Normal file
27
Userland/Libraries/LibPDF/Interpolation.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Rodrigo Tobar <rtobarc@gmail.com>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Span.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
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<float> const& x, Span<float> y) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
float m_x_min;
|
||||||
|
float m_y_min;
|
||||||
|
float m_slope;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue