nvidia-texture-tools/src/nvimage/Filter.h

220 lines
4.0 KiB
C
Raw Normal View History

2007-04-17 08:49:19 +00:00
// This code is in the public domain -- castanyo@yahoo.es
#ifndef NV_IMAGE_FILTER_H
#define NV_IMAGE_FILTER_H
#include <nvimage/nvimage.h>
2007-12-03 08:34:32 +00:00
#include <nvcore/Debug.h>
2007-04-17 08:49:19 +00:00
namespace nv
{
class Vector4;
2007-12-02 10:31:37 +00:00
/// Base filter class.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS Filter
2007-04-17 08:49:19 +00:00
{
2007-12-02 10:31:37 +00:00
public:
2008-04-17 06:59:13 +00:00
Filter(float width);
virtual ~Filter();
2007-12-02 10:31:37 +00:00
2008-04-17 06:59:13 +00:00
float width() const { return m_width; }
float sampleDelta(float x, float scale) const;
float sampleBox(float x, float scale, int samples) const;
float sampleTriangle(float x, float scale, int samples) const;
2007-12-02 10:31:37 +00:00
virtual float evaluate(float x) const = 0;
protected:
const float m_width;
};
// Box filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS BoxFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
BoxFilter();
BoxFilter(float width);
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Triangle (bilinear/tent) filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS TriangleFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
TriangleFilter();
TriangleFilter(float width);
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Quadratic (bell) filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS QuadraticFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
QuadraticFilter();
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Cubic filter from Thatcher Ulrich.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS CubicFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
CubicFilter();
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Cubic b-spline filter from Paul Heckbert.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS BSplineFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
BSplineFilter();
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
/// Mitchell & Netravali's two-param cubic
/// @see "Reconstruction Filters in Computer Graphics", SIGGRAPH 88
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS MitchellFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
MitchellFilter();
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
2008-04-17 06:59:13 +00:00
void setParameters(float a, float b);
2007-12-02 10:31:37 +00:00
private:
float p0, p2, p3;
float q0, q1, q2, q3;
};
// Lanczos3 filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS LanczosFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
LanczosFilter();
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Sinc filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS SincFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
SincFilter(float w);
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
};
// Kaiser filter.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS KaiserFilter : public Filter
2007-12-02 10:31:37 +00:00
{
public:
2008-04-17 06:59:13 +00:00
KaiserFilter(float w);
virtual float evaluate(float x) const;
2007-12-02 10:31:37 +00:00
2008-04-17 06:59:13 +00:00
void setParameters(float a, float stretch);
2007-11-28 10:34:40 +00:00
2007-12-02 10:31:37 +00:00
private:
float alpha;
float stretch;
2007-04-17 08:49:19 +00:00
};
2007-12-02 10:31:37 +00:00
2007-04-17 08:49:19 +00:00
/// A 1D kernel. Used to precompute filter weights.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS Kernel1
2007-04-17 08:49:19 +00:00
{
2007-12-03 08:34:32 +00:00
NV_FORBID_COPY(Kernel1);
2007-04-17 08:49:19 +00:00
public:
2008-04-17 06:59:13 +00:00
Kernel1(const Filter & f, int iscale, int samples = 32);
~Kernel1();
2007-04-17 08:49:19 +00:00
float valueAt(uint x) const {
2007-12-03 08:34:32 +00:00
nvDebugCheck(x < (uint)m_windowSize);
2007-11-28 10:34:40 +00:00
return m_data[x];
2007-04-17 08:49:19 +00:00
}
2007-12-03 08:34:32 +00:00
int windowSize() const {
2007-11-28 10:34:40 +00:00
return m_windowSize;
2007-04-17 08:49:19 +00:00
}
2007-12-03 08:34:32 +00:00
float width() const {
return m_width;
}
2007-04-17 08:49:19 +00:00
2008-04-17 06:59:13 +00:00
void debugPrint();
2007-04-17 08:49:19 +00:00
private:
2007-12-03 08:34:32 +00:00
int m_windowSize;
float m_width;
2007-11-28 10:34:40 +00:00
float * m_data;
2007-04-17 08:49:19 +00:00
};
/// A 2D kernel.
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS Kernel2
2007-04-17 08:49:19 +00:00
{
public:
2008-04-17 06:59:13 +00:00
Kernel2(uint width);
Kernel2(const Kernel2 & k);
~Kernel2();
2007-04-17 08:49:19 +00:00
2008-04-17 06:59:13 +00:00
void normalize();
void transpose();
2007-04-17 08:49:19 +00:00
float valueAt(uint x, uint y) const {
2007-11-28 10:34:40 +00:00
return m_data[y * m_windowSize + x];
2007-04-17 08:49:19 +00:00
}
2007-11-28 10:34:40 +00:00
uint windowSize() const {
return m_windowSize;
2007-04-17 08:49:19 +00:00
}
2008-04-17 06:59:13 +00:00
void initLaplacian();
void initEdgeDetection();
void initSobel();
void initPrewitt();
2007-11-28 05:04:16 +00:00
2008-04-17 06:59:13 +00:00
void initBlendedSobel(const Vector4 & scale);
2007-11-28 05:04:16 +00:00
2007-04-17 08:49:19 +00:00
private:
2007-11-28 10:34:40 +00:00
const uint m_windowSize;
float * m_data;
2007-04-17 08:49:19 +00:00
};
2007-12-02 10:31:37 +00:00
2007-11-28 05:04:16 +00:00
/// A 1D polyphase kernel
2008-04-17 06:59:13 +00:00
class NVIMAGE_CLASS PolyphaseKernel
2007-11-28 05:04:16 +00:00
{
2007-12-03 08:34:32 +00:00
NV_FORBID_COPY(PolyphaseKernel);
2007-11-28 05:04:16 +00:00
public:
2008-04-17 06:59:13 +00:00
PolyphaseKernel(const Filter & f, uint srcLength, uint dstLength, int samples = 32);
~PolyphaseKernel();
2007-11-28 05:04:16 +00:00
2007-12-03 08:34:32 +00:00
int windowSize() const {
2007-12-02 10:31:37 +00:00
return m_windowSize;
2007-11-28 05:04:16 +00:00
}
uint length() const {
return m_length;
}
2007-12-02 10:31:37 +00:00
float width() const {
return m_width;
}
float valueAt(uint column, uint x) const {
nvDebugCheck(column < m_length);
2007-12-03 08:34:32 +00:00
nvDebugCheck(x < (uint)m_windowSize);
2007-12-02 10:31:37 +00:00
return m_data[column * m_windowSize + x];
}
2008-04-17 06:59:13 +00:00
void debugPrint() const;
2007-11-28 05:04:16 +00:00
private:
2007-12-03 08:34:32 +00:00
int m_windowSize;
2007-12-02 10:31:37 +00:00
uint m_length;
float m_width;
2007-11-28 05:04:16 +00:00
float * m_data;
};
2007-04-17 08:49:19 +00:00
} // nv namespace
#endif // NV_IMAGE_FILTER_H