crossxtex/DirectXTex/filters.h

424 lines
13 KiB
C
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// filters.h
//
// Utility header with helpers for implementing image filters
//
// Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-24 06:24:46 +00:00
// Licensed under the MIT License.
2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
#pragma once
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
2016-08-22 18:26:36 +00:00
#include <memory>
#include "scoped.h"
namespace DirectX
{
//-------------------------------------------------------------------------------------
// Box filtering helpers
//-------------------------------------------------------------------------------------
2017-07-12 07:56:51 +00:00
XMGLOBALCONST XMVECTORF32 g_boxScale = { { { 0.25f, 0.25f, 0.25f, 0.25f } } };
XMGLOBALCONST XMVECTORF32 g_boxScale3D = { { { 0.125f, 0.125f, 0.125f, 0.125f } } };
2016-08-22 18:26:36 +00:00
#define AVERAGE4( res, p0, p1, p2, p3 ) \
{ \
2018-03-16 19:52:21 +00:00
XMVECTOR v = XMVectorAdd((p0), (p1)); \
v = XMVectorAdd(v, (p2)); \
v = XMVectorAdd(v, (p3)); \
res = XMVectorMultiply(v, g_boxScale); \
2016-08-22 18:26:36 +00:00
}
#define AVERAGE8( res, p0, p1, p2, p3, p4, p5, p6, p7) \
{ \
2018-03-16 19:52:21 +00:00
XMVECTOR v = XMVectorAdd((p0), (p1)); \
v = XMVectorAdd(v, (p2)); \
v = XMVectorAdd(v, (p3)); \
v = XMVectorAdd(v, (p4)); \
v = XMVectorAdd(v, (p5)); \
v = XMVectorAdd(v, (p6)); \
v = XMVectorAdd(v, (p7)); \
res = XMVectorMultiply(v, g_boxScale3D); \
2016-08-22 18:26:36 +00:00
}
//-------------------------------------------------------------------------------------
// Linear filtering helpers
//-------------------------------------------------------------------------------------
struct LinearFilter
{
size_t u0;
float weight0;
size_t u1;
float weight1;
};
2016-09-09 02:09:46 +00:00
inline void _CreateLinearFilter(_In_ size_t source, _In_ size_t dest, _In_ bool wrap, _Out_writes_(dest) LinearFilter* lf)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(source > 0);
assert(dest > 0);
2018-06-13 01:07:34 +00:00
assert(lf != nullptr);
2016-08-22 18:26:36 +00:00
float scale = float(source) / float(dest);
// Mirror is the same case as clamp for linear
2016-09-09 02:09:46 +00:00
for (size_t u = 0; u < dest; ++u)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
float srcB = (float(u) + 0.5f) * scale + 0.5f;
2016-08-22 18:26:36 +00:00
ptrdiff_t isrcB = ptrdiff_t(srcB);
ptrdiff_t isrcA = isrcB - 1;
2016-09-09 02:09:46 +00:00
if (isrcA < 0)
2016-08-22 18:26:36 +00:00
{
isrcA = (wrap) ? (ptrdiff_t(source) - 1) : 0;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (size_t(isrcB) >= source)
2016-08-22 18:26:36 +00:00
{
isrcB = (wrap) ? 0 : (ptrdiff_t(source) - 1);
2016-08-22 18:26:36 +00:00
}
float weight = 1.0f + float(isrcB) - srcB;
2016-09-09 02:09:46 +00:00
auto& entry = lf[u];
2016-08-22 18:26:36 +00:00
entry.u0 = size_t(isrcA);
entry.weight0 = weight;
entry.u1 = size_t(isrcB);
entry.weight1 = 1.0f - weight;
}
}
#define BILINEAR_INTERPOLATE( res, x, y, r0, r1 ) \
2018-03-16 19:52:21 +00:00
res = XMVectorAdd(XMVectorScale(XMVectorAdd(XMVectorScale((r0)[ x.u0 ], x.weight0), XMVectorScale((r0)[ x.u1 ], x.weight1)), y.weight0), \
XMVectorScale(XMVectorAdd(XMVectorScale((r1)[ x.u0 ], x.weight0), XMVectorScale((r1)[ x.u1 ], x.weight1)), y.weight1) )
2016-08-22 18:26:36 +00:00
#define TRILINEAR_INTERPOLATE( res, x, y, z, r0, r1, r2, r3 ) \
{\
2018-03-16 19:52:21 +00:00
XMVECTOR a0 = XMVectorScale(XMVectorAdd(XMVectorScale((r0)[ x.u0 ], x.weight0 ), XMVectorScale((r0)[ x.u1 ], x.weight1)), y.weight0); \
XMVECTOR a1 = XMVectorScale(XMVectorAdd(XMVectorScale((r1)[ x.u0 ], x.weight0 ), XMVectorScale((r1)[ x.u1 ], x.weight1)), y.weight1); \
XMVECTOR a2 = XMVectorScale(XMVectorAdd(XMVectorScale((r2)[ x.u0 ], x.weight0 ), XMVectorScale((r2)[ x.u1 ], x.weight1)), y.weight0); \
XMVECTOR a3 = XMVectorScale(XMVectorAdd(XMVectorScale((r3)[ x.u0 ], x.weight0 ), XMVectorScale((r3)[ x.u1 ], x.weight1)), y.weight1); \
res = XMVectorAdd(XMVectorScale(XMVectorAdd(a0, a1), z.weight0), XMVectorScale(XMVectorAdd(a2, a3), z.weight1)); \
}
2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// Cubic filtering helpers
//-------------------------------------------------------------------------------------
2017-07-12 07:56:51 +00:00
XMGLOBALCONST XMVECTORF32 g_cubicThird = { { { 1.f / 3.f, 1.f / 3.f, 1.f / 3.f, 1.f / 3.f } } };
XMGLOBALCONST XMVECTORF32 g_cubicSixth = { { { 1.f / 6.f, 1.f / 6.f, 1.f / 6.f, 1.f / 6.f } } };
XMGLOBALCONST XMVECTORF32 g_cubicHalf = { { { 1.f / 2.f, 1.f / 2.f, 1.f / 2.f, 1.f / 2.f } } };
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
inline ptrdiff_t bounduvw(ptrdiff_t u, ptrdiff_t maxu, bool wrap, bool mirror)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (wrap)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (u < 0)
2016-08-22 18:26:36 +00:00
{
u = maxu + u + 1;
}
2016-09-09 02:09:46 +00:00
else if (u > maxu)
2016-08-22 18:26:36 +00:00
{
u = u - maxu - 1;
}
}
2016-09-09 02:09:46 +00:00
else if (mirror)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (u < 0)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
u = (-u) - 1;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else if (u > maxu)
2016-08-22 18:26:36 +00:00
{
u = maxu - (u - maxu - 1);
}
}
// Handles clamp, but also a safety factor for degenerate images for wrap/mirror
2016-09-09 02:09:46 +00:00
u = std::min<ptrdiff_t>(u, maxu);
u = std::max<ptrdiff_t>(u, 0);
2016-08-22 18:26:36 +00:00
return u;
}
struct CubicFilter
{
size_t u0;
size_t u1;
size_t u2;
size_t u3;
float x;
};
2016-09-09 02:09:46 +00:00
inline void _CreateCubicFilter(_In_ size_t source, _In_ size_t dest, _In_ bool wrap, _In_ bool mirror, _Out_writes_(dest) CubicFilter* cf)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(source > 0);
assert(dest > 0);
2018-06-13 01:07:34 +00:00
assert(cf != nullptr);
2016-08-22 18:26:36 +00:00
float scale = float(source) / float(dest);
2016-09-09 02:09:46 +00:00
for (size_t u = 0; u < dest; ++u)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
float srcB = (float(u) + 0.5f) * scale - 0.5f;
2016-08-22 18:26:36 +00:00
ptrdiff_t isrcB = bounduvw(ptrdiff_t(srcB), ptrdiff_t(source) - 1, wrap, mirror);
ptrdiff_t isrcA = bounduvw(isrcB - 1, ptrdiff_t(source) - 1, wrap, mirror);
ptrdiff_t isrcC = bounduvw(isrcB + 1, ptrdiff_t(source) - 1, wrap, mirror);
ptrdiff_t isrcD = bounduvw(isrcB + 2, ptrdiff_t(source) - 1, wrap, mirror);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
auto& entry = cf[u];
2016-08-22 18:26:36 +00:00
entry.u0 = size_t(isrcA);
entry.u1 = size_t(isrcB);
entry.u2 = size_t(isrcC);
entry.u3 = size_t(isrcD);
float x = srcB - float(isrcB);
entry.x = x;
}
}
#define CUBIC_INTERPOLATE( res, dx, p0, p1, p2, p3 ) \
{ \
XMVECTOR a0 = (p1); \
2018-03-16 19:52:21 +00:00
XMVECTOR d0 = XMVectorSubtract(p0, a0); \
XMVECTOR d2 = XMVectorSubtract(p2, a0); \
XMVECTOR d3 = XMVectorSubtract(p3, a0); \
XMVECTOR a1 = XMVectorSubtract(d2, XMVectorMultiply(g_cubicThird, d0)); \
a1 = XMVectorSubtract(a1, XMVectorMultiply(g_cubicSixth, d3)); \
XMVECTOR a2 = XMVectorAdd(XMVectorMultiply(g_cubicHalf, d0), XMVectorMultiply(g_cubicHalf, d2)); \
XMVECTOR a3 = XMVectorSubtract(XMVectorMultiply(g_cubicSixth, d3), XMVectorMultiply(g_cubicSixth, d0)); \
a3 = XMVectorSubtract(a3, XMVectorMultiply(g_cubicHalf, d2)); \
XMVECTOR vdx = XMVectorReplicate(dx); \
XMVECTOR vdx2 = XMVectorMultiply(vdx, vdx); \
XMVECTOR vdx3 = XMVectorMultiply(vdx2, vdx); \
res = XMVectorAdd(XMVectorAdd(XMVectorAdd(a0, XMVectorMultiply(a1, vdx)), XMVectorMultiply(a2, vdx2)), XMVectorMultiply(a3, vdx3)); \
2016-08-22 18:26:36 +00:00
}
//-------------------------------------------------------------------------------------
// Triangle filtering helpers
//-------------------------------------------------------------------------------------
namespace TriangleFilter
{
struct FilterTo
{
size_t u;
float weight;
};
struct FilterFrom
{
size_t count;
size_t sizeInBytes;
FilterTo to[1]; // variable-sized array
};
struct Filter
{
size_t sizeInBytes;
size_t totalSize;
FilterFrom from[1]; // variable-sized array
};
struct TriangleRow
{
size_t remaining;
TriangleRow* next;
ScopedAlignedArrayXMVECTOR scanline;
TriangleRow() noexcept : remaining(0), next(nullptr) {}
2016-08-22 18:26:36 +00:00
};
static const size_t TF_FILTER_SIZE = sizeof(Filter) - sizeof(FilterFrom);
static const size_t TF_FROM_SIZE = sizeof(FilterFrom) - sizeof(FilterTo);
static const size_t TF_TO_SIZE = sizeof(FilterTo);
static const float TF_EPSILON = 0.00001f;
2016-09-09 02:09:46 +00:00
inline HRESULT _Create(_In_ size_t source, _In_ size_t dest, _In_ bool wrap, _Inout_ std::unique_ptr<Filter>& tf)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(source > 0);
assert(dest > 0);
2016-08-22 18:26:36 +00:00
float scale = float(dest) / float(source);
float scaleInv = 0.5f / scale;
// Determine storage required for filter and allocate memory if needed
size_t totalSize = TF_FILTER_SIZE + TF_FROM_SIZE + TF_TO_SIZE;
float repeat = (wrap) ? 1.f : 0.f;
2016-09-09 02:09:46 +00:00
for (size_t u = 0; u < source; ++u)
2016-08-22 18:26:36 +00:00
{
float src = float(u) - 0.5f;
float destMin = src * scale;
float destMax = destMin + scale;
2018-05-02 07:20:07 +00:00
float t = destMax - destMin + repeat + 1.f;
totalSize += TF_FROM_SIZE + TF_TO_SIZE + size_t(t) * TF_TO_SIZE * 2;
2016-08-22 18:26:36 +00:00
}
uint8_t* pFilter = nullptr;
2016-09-09 02:09:46 +00:00
if (tf)
2016-08-22 18:26:36 +00:00
{
// See if existing filter memory block is large enough to reuse
2016-09-09 02:09:46 +00:00
if (tf->totalSize >= totalSize)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
pFilter = reinterpret_cast<uint8_t*>(tf.get());
2016-08-22 18:26:36 +00:00
}
else
{
// Need to reallocate filter memory block
2016-09-09 02:09:46 +00:00
tf.reset(nullptr);
2016-08-22 18:26:36 +00:00
}
}
2016-09-09 02:09:46 +00:00
if (!tf)
2016-08-22 18:26:36 +00:00
{
// Allocate filter memory block
2016-09-09 02:09:46 +00:00
pFilter = new (std::nothrow) uint8_t[totalSize];
if (!pFilter)
2016-08-22 18:26:36 +00:00
return E_OUTOFMEMORY;
2016-09-09 02:09:46 +00:00
tf.reset(reinterpret_cast<Filter*>(pFilter));
2016-08-22 18:26:36 +00:00
tf->totalSize = totalSize;
}
2018-06-13 01:07:34 +00:00
assert(pFilter != nullptr);
_Analysis_assume_(pFilter != 0);
2016-08-22 18:26:36 +00:00
// Filter setup
size_t sizeInBytes = TF_FILTER_SIZE;
size_t accumU = 0;
float accumWeight = 0.f;
2016-09-09 02:09:46 +00:00
for (size_t u = 0; u < source; ++u)
2016-08-22 18:26:36 +00:00
{
// Setup from entry
size_t sizeFrom = sizeInBytes;
2016-09-09 02:09:46 +00:00
auto pFrom = reinterpret_cast<FilterFrom*>(pFilter + sizeInBytes);
2016-08-22 18:26:36 +00:00
sizeInBytes += TF_FROM_SIZE;
2016-09-09 02:09:46 +00:00
if (sizeInBytes > totalSize)
2016-08-22 18:26:36 +00:00
return E_FAIL;
size_t toCount = 0;
// Perform two passes to capture the influences from both sides
2016-09-09 02:09:46 +00:00
for (size_t j = 0; j < 2; ++j)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
float src = float(u + j) - 0.5f;
2016-08-22 18:26:36 +00:00
float destMin = src * scale;
float destMax = destMin + scale;
2016-09-09 02:09:46 +00:00
if (!wrap)
2016-08-22 18:26:36 +00:00
{
// Clamp
2016-09-09 02:09:46 +00:00
if (destMin < 0.f)
2016-08-22 18:26:36 +00:00
destMin = 0.f;
2016-09-09 02:09:46 +00:00
if (destMax > float(dest))
2016-08-22 18:26:36 +00:00
destMax = float(dest);
}
2016-09-09 02:09:46 +00:00
for (auto k = static_cast<ptrdiff_t>(floorf(destMin)); float(k) < destMax; ++k)
2016-08-22 18:26:36 +00:00
{
float d0 = float(k);
float d1 = d0 + 1.f;
size_t u0;
2016-09-09 02:09:46 +00:00
if (k < 0)
2016-08-22 18:26:36 +00:00
{
// Handle wrap
2016-09-09 02:09:46 +00:00
u0 = size_t(k + ptrdiff_t(dest));
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else if (k >= ptrdiff_t(dest))
2016-08-22 18:26:36 +00:00
{
// Handle wrap
2016-09-09 02:09:46 +00:00
u0 = size_t(k - ptrdiff_t(dest));
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
u0 = size_t(k);
2016-08-22 18:26:36 +00:00
}
// Save previous accumulated weight (if any)
2016-09-09 02:09:46 +00:00
if (u0 != accumU)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (accumWeight > TF_EPSILON)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
auto pTo = reinterpret_cast<FilterTo*>(pFilter + sizeInBytes);
2016-08-22 18:26:36 +00:00
sizeInBytes += TF_TO_SIZE;
++toCount;
2016-09-09 02:09:46 +00:00
if (sizeInBytes > totalSize)
2016-08-22 18:26:36 +00:00
return E_FAIL;
pTo->u = accumU;
pTo->weight = accumWeight;
}
accumWeight = 0.f;
accumU = u0;
}
// Clip destination
2016-09-09 02:09:46 +00:00
if (d0 < destMin)
2016-08-22 18:26:36 +00:00
d0 = destMin;
2016-09-09 02:09:46 +00:00
if (d1 > destMax)
2016-08-22 18:26:36 +00:00
d1 = destMax;
// Calculate average weight over destination pixel
float weight;
2016-09-09 02:09:46 +00:00
if (!wrap && src < 0.f)
2016-08-22 18:26:36 +00:00
weight = 1.f;
2016-09-09 02:09:46 +00:00
else if (!wrap && ((src + 1.f) >= float(source)))
2016-08-22 18:26:36 +00:00
weight = 0.f;
else
weight = (d0 + d1) * scaleInv - src;
2016-09-09 02:09:46 +00:00
accumWeight += (d1 - d0) * (j ? (1.f - weight) : weight);
2016-08-22 18:26:36 +00:00
}
}
// Store accumulated weight
2016-09-09 02:09:46 +00:00
if (accumWeight > TF_EPSILON)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
auto pTo = reinterpret_cast<FilterTo*>(pFilter + sizeInBytes);
2016-08-22 18:26:36 +00:00
sizeInBytes += TF_TO_SIZE;
++toCount;
2016-09-09 02:09:46 +00:00
if (sizeInBytes > totalSize)
2016-08-22 18:26:36 +00:00
return E_FAIL;
pTo->u = accumU;
pTo->weight = accumWeight;
}
accumWeight = 0.f;
// Finalize from entry
pFrom->count = toCount;
pFrom->sizeInBytes = sizeInBytes - sizeFrom;
}
tf->sizeInBytes = sizeInBytes;
return S_OK;
}
2018-02-24 06:42:26 +00:00
} // namespace TriangleFilter
2016-08-22 18:26:36 +00:00
2018-02-24 06:42:26 +00:00
} // namespace DirectX