Merge changes from The Witness.
parent
2075d740c9
commit
9489aed825
@ -1,460 +1,513 @@
|
|||||||
|
|
||||||
#include "ErrorMetric.h"
|
#include "ErrorMetric.h"
|
||||||
#include "FloatImage.h"
|
#include "FloatImage.h"
|
||||||
#include "Filter.h"
|
#include "Filter.h"
|
||||||
|
|
||||||
#include "nvmath/Matrix.h"
|
#include "nvmath/Matrix.h"
|
||||||
#include "nvmath/Vector.inl"
|
#include "nvmath/Vector.inl"
|
||||||
|
|
||||||
#include <float.h> // FLT_MAX
|
#include <float.h> // FLT_MAX
|
||||||
|
|
||||||
using namespace nv;
|
using namespace nv;
|
||||||
|
|
||||||
float nv::rmsColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
float nv::rmsColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||||
{
|
{
|
||||||
if (!sameLayout(img, ref)) {
|
if (!sameLayout(img, ref)) {
|
||||||
return FLT_MAX;
|
return FLT_MAX;
|
||||||
}
|
}
|
||||||
nvDebugCheck(img->componentCount() == 4);
|
nvDebugCheck(img->componentCount() == 4);
|
||||||
nvDebugCheck(ref->componentCount() == 4);
|
nvDebugCheck(ref->componentCount() == 4);
|
||||||
|
|
||||||
double mse = 0;
|
double mse = 0;
|
||||||
|
|
||||||
const uint count = img->pixelCount();
|
const uint count = img->pixelCount();
|
||||||
for (uint i = 0; i < count; i++)
|
for (uint i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
float r0 = ref->pixel(i + count * 0);
|
float r0 = ref->pixel(i + count * 0);
|
||||||
float g0 = ref->pixel(i + count * 1);
|
float g0 = ref->pixel(i + count * 1);
|
||||||
float b0 = ref->pixel(i + count * 2);
|
float b0 = ref->pixel(i + count * 2);
|
||||||
float a0 = ref->pixel(i + count * 3);
|
float a0 = ref->pixel(i + count * 3);
|
||||||
float r1 = img->pixel(i + count * 0);
|
float r1 = img->pixel(i + count * 0);
|
||||||
float g1 = img->pixel(i + count * 1);
|
float g1 = img->pixel(i + count * 1);
|
||||||
float b1 = img->pixel(i + count * 2);
|
float b1 = img->pixel(i + count * 2);
|
||||||
//float a1 = img->pixel(i + count * 3);
|
//float a1 = img->pixel(i + count * 3);
|
||||||
|
|
||||||
float r = r0 - r1;
|
float r = r0 - r1;
|
||||||
float g = g0 - g1;
|
float g = g0 - g1;
|
||||||
float b = b0 - b1;
|
float b = b0 - b1;
|
||||||
|
|
||||||
float a = 1;
|
float a = 1;
|
||||||
if (alphaWeight) a = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
if (alphaWeight) a = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
||||||
|
|
||||||
mse += (r * r) * a;
|
mse += (r * r) * a;
|
||||||
mse += (g * g) * a;
|
mse += (g * g) * a;
|
||||||
mse += (b * b) * a;
|
mse += (b * b) * a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return float(sqrt(mse / count));
|
return float(sqrt(mse / count));
|
||||||
}
|
}
|
||||||
|
|
||||||
float nv::rmsAlphaError(const FloatImage * ref, const FloatImage * img)
|
float nv::rmsAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||||
{
|
{
|
||||||
if (!sameLayout(img, ref)) {
|
if (!sameLayout(img, ref)) {
|
||||||
return FLT_MAX;
|
return FLT_MAX;
|
||||||
}
|
}
|
||||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||||
|
|
||||||
double mse = 0;
|
double mse = 0;
|
||||||
|
|
||||||
const uint count = img->pixelCount();
|
const uint count = img->pixelCount();
|
||||||
for (uint i = 0; i < count; i++)
|
for (uint i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
float a0 = img->pixel(i + count * 3);
|
float a0 = img->pixel(i + count * 3);
|
||||||
float a1 = ref->pixel(i + count * 3);
|
float a1 = ref->pixel(i + count * 3);
|
||||||
|
|
||||||
float a = a0 - a1;
|
float a = a0 - a1;
|
||||||
|
|
||||||
mse += a * a;
|
mse += a * a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return float(sqrt(mse / count));
|
return float(sqrt(mse / count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float nv::averageColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
float nv::averageColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||||
{
|
{
|
||||||
if (!sameLayout(img, ref)) {
|
if (!sameLayout(img, ref)) {
|
||||||
return FLT_MAX;
|
return FLT_MAX;
|
||||||
}
|
}
|
||||||
nvDebugCheck(img->componentCount() == 4);
|
nvDebugCheck(img->componentCount() == 4);
|
||||||
nvDebugCheck(ref->componentCount() == 4);
|
nvDebugCheck(ref->componentCount() == 4);
|
||||||
|
|
||||||
double mae = 0;
|
double mae = 0;
|
||||||
|
|
||||||
const uint count = img->pixelCount();
|
const uint count = img->pixelCount();
|
||||||
for (uint i = 0; i < count; i++)
|
for (uint i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
float r0 = img->pixel(i + count * 0);
|
float r0 = img->pixel(i + count * 0);
|
||||||
float g0 = img->pixel(i + count * 1);
|
float g0 = img->pixel(i + count * 1);
|
||||||
float b0 = img->pixel(i + count * 2);
|
float b0 = img->pixel(i + count * 2);
|
||||||
//float a0 = img->pixel(i + count * 3);
|
//float a0 = img->pixel(i + count * 3);
|
||||||
float r1 = ref->pixel(i + count * 0);
|
float r1 = ref->pixel(i + count * 0);
|
||||||
float g1 = ref->pixel(i + count * 1);
|
float g1 = ref->pixel(i + count * 1);
|
||||||
float b1 = ref->pixel(i + count * 2);
|
float b1 = ref->pixel(i + count * 2);
|
||||||
float a1 = ref->pixel(i + count * 3);
|
float a1 = ref->pixel(i + count * 3);
|
||||||
|
|
||||||
float r = fabs(r0 - r1);
|
float r = fabs(r0 - r1);
|
||||||
float g = fabs(g0 - g1);
|
float g = fabs(g0 - g1);
|
||||||
float b = fabs(b0 - b1);
|
float b = fabs(b0 - b1);
|
||||||
|
|
||||||
float a = 1;
|
float a = 1;
|
||||||
if (alphaWeight) a = a1;
|
if (alphaWeight) a = a1;
|
||||||
|
|
||||||
mae += r * a;
|
mae += r * a;
|
||||||
mae += g * a;
|
mae += g * a;
|
||||||
mae += b * a;
|
mae += b * a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return float(mae / count);
|
return float(mae / count);
|
||||||
}
|
}
|
||||||
|
|
||||||
float nv::averageAlphaError(const FloatImage * ref, const FloatImage * img)
|
float nv::averageAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||||
{
|
{
|
||||||
if (img == NULL || ref == NULL || img->width() != ref->width() || img->height() != ref->height()) {
|
if (img == NULL || ref == NULL || img->width() != ref->width() || img->height() != ref->height()) {
|
||||||
return FLT_MAX;
|
return FLT_MAX;
|
||||||
}
|
}
|
||||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||||
|
|
||||||
double mae = 0;
|
double mae = 0;
|
||||||
|
|
||||||
const uint count = img->width() * img->height();
|
const uint count = img->width() * img->height();
|
||||||
for (uint i = 0; i < count; i++)
|
for (uint i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
float a0 = img->pixel(i + count * 3);
|
float a0 = img->pixel(i + count * 3);
|
||||||
float a1 = ref->pixel(i + count * 3);
|
float a1 = ref->pixel(i + count * 3);
|
||||||
|
|
||||||
float a = a0 - a1;
|
float a = a0 - a1;
|
||||||
|
|
||||||
mae += fabs(a);
|
mae += fabs(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
return float(mae / count);
|
return float(mae / count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Color space conversions based on:
|
float nv::rmsBilinearColorError(const FloatImage * ref, const FloatImage * img, FloatImage::WrapMode wm, bool alphaWeight)
|
||||||
// http://www.brucelindbloom.com/
|
{
|
||||||
|
nvDebugCheck(img->componentCount() == 4);
|
||||||
// Assumes input is in *linear* sRGB color space.
|
nvDebugCheck(ref->componentCount() == 4);
|
||||||
static Vector3 rgbToXyz(Vector3::Arg c)
|
|
||||||
{
|
double mse = 0;
|
||||||
Vector3 xyz;
|
|
||||||
xyz.x = 0.412453f * c.x + 0.357580f * c.y + 0.180423f * c.z;
|
const uint w0 = ref->width();
|
||||||
xyz.y = 0.212671f * c.x + 0.715160f * c.y + 0.072169f * c.z;
|
const uint h0 = ref->height();
|
||||||
xyz.z = 0.019334f * c.x + 0.119193f * c.y + 0.950227f * c.z;
|
const uint d0 = ref->depth();
|
||||||
return xyz;
|
|
||||||
}
|
const uint w1 = img->width();
|
||||||
|
const uint h1 = img->height();
|
||||||
static Vector3 xyzToRgb(Vector3::Arg c)
|
const uint d1 = img->depth();
|
||||||
{
|
|
||||||
Vector3 rgb;
|
for (uint z = 0; z < d0; z++) {
|
||||||
rgb.x = 3.2404542f * c.x - 1.5371385f * c.y - 0.4985314f * c.z;
|
for (uint y = 0; y < h0; y++) {
|
||||||
rgb.y = -0.9692660f * c.x + 1.8760108f * c.y + 0.0415560f * c.z;
|
for (uint x = 0; x < w0; x++) {
|
||||||
rgb.z = 0.0556434f * c.x - 0.2040259f * c.y + 1.0572252f * c.z;
|
float r0 = ref->pixel(0, x, y, z);
|
||||||
return rgb;
|
float g0 = ref->pixel(1, x, y, z);
|
||||||
}
|
float b0 = ref->pixel(2, x, y, z);
|
||||||
|
float a0 = ref->pixel(3, x, y, z);
|
||||||
static float toLinear(float f)
|
|
||||||
{
|
float fx = float(x) / w0;
|
||||||
return powf(f, 2.2f);
|
float fy = float(y) / h0;
|
||||||
}
|
float fz = float(z) / d0;
|
||||||
|
|
||||||
static float toGamma(float f)
|
float r1 = img->sampleLinear(0, fx, fy, fz, wm);
|
||||||
{
|
float g1 = img->sampleLinear(1, fx, fy, fz, wm);
|
||||||
// @@ Use sRGB space?
|
float b1 = img->sampleLinear(2, fx, fy, fz, wm);
|
||||||
return powf(f, 1.0f/2.2f);
|
float a1 = img->sampleLinear(2, fx, fy, fz, wm);
|
||||||
}
|
|
||||||
|
float dr = r0 - r1;
|
||||||
static Vector3 toLinear(Vector3::Arg c)
|
float dg = g0 - g1;
|
||||||
{
|
float db = b0 - b1;
|
||||||
return Vector3(toLinear(c.x), toLinear(c.y), toLinear(c.z));
|
float da = a0 - a1;
|
||||||
}
|
|
||||||
|
float w = 1;
|
||||||
static Vector3 toGamma(Vector3::Arg c)
|
if (alphaWeight) w = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
||||||
{
|
|
||||||
return Vector3(toGamma(c.x), toGamma(c.y), toGamma(c.z));
|
mse += (dr * dr) * w;
|
||||||
}
|
mse += (dg * dg) * w;
|
||||||
|
mse += (db * db) * w;
|
||||||
static float f(float t)
|
mse += (da * da);
|
||||||
{
|
}
|
||||||
const float epsilon = powf(6.0f/29.0f, 3);
|
}
|
||||||
|
}
|
||||||
if (t > epsilon) {
|
|
||||||
return powf(t, 1.0f/3.0f);
|
int count = w0 * h0 * d0;
|
||||||
}
|
return float(sqrt(mse / count));
|
||||||
else {
|
}
|
||||||
return 1.0f/3.0f * powf(29.0f/6.0f, 2) * t + 4.0f / 29.0f;
|
|
||||||
}
|
|
||||||
}
|
// Color space conversions based on:
|
||||||
|
// http://www.brucelindbloom.com/
|
||||||
static float finv(float t)
|
|
||||||
{
|
// Assumes input is in *linear* sRGB color space.
|
||||||
if (t > 6.0f / 29.0f) {
|
static Vector3 rgbToXyz(Vector3::Arg c)
|
||||||
return 3.0f * powf(6.0f / 29.0f, 2) * (t - 4.0f / 29.0f);
|
{
|
||||||
}
|
Vector3 xyz;
|
||||||
else {
|
xyz.x = 0.412453f * c.x + 0.357580f * c.y + 0.180423f * c.z;
|
||||||
return powf(t, 3.0f);
|
xyz.y = 0.212671f * c.x + 0.715160f * c.y + 0.072169f * c.z;
|
||||||
}
|
xyz.z = 0.019334f * c.x + 0.119193f * c.y + 0.950227f * c.z;
|
||||||
}
|
return xyz;
|
||||||
|
}
|
||||||
static Vector3 xyzToCieLab(Vector3::Arg c)
|
|
||||||
{
|
static Vector3 xyzToRgb(Vector3::Arg c)
|
||||||
// Normalized white point.
|
{
|
||||||
const float Xn = 0.950456f;
|
Vector3 rgb;
|
||||||
const float Yn = 1.0f;
|
rgb.x = 3.2404542f * c.x - 1.5371385f * c.y - 0.4985314f * c.z;
|
||||||
const float Zn = 1.088754f;
|
rgb.y = -0.9692660f * c.x + 1.8760108f * c.y + 0.0415560f * c.z;
|
||||||
|
rgb.z = 0.0556434f * c.x - 0.2040259f * c.y + 1.0572252f * c.z;
|
||||||
float Xr = c.x / Xn;
|
return rgb;
|
||||||
float Yr = c.y / Yn;
|
}
|
||||||
float Zr = c.z / Zn;
|
|
||||||
|
static float toLinear(float f)
|
||||||
float fx = f(Xr);
|
{
|
||||||
float fy = f(Yr);
|
return powf(f, 2.2f);
|
||||||
float fz = f(Zr);
|
}
|
||||||
|
|
||||||
float L = 116 * fx - 16;
|
static float toGamma(float f)
|
||||||
float a = 500 * (fx - fy);
|
{
|
||||||
float b = 200 * (fy - fz);
|
// @@ Use sRGB space?
|
||||||
|
return powf(f, 1.0f/2.2f);
|
||||||
return Vector3(L, a, b);
|
}
|
||||||
}
|
|
||||||
|
static Vector3 toLinear(Vector3::Arg c)
|
||||||
static Vector3 rgbToCieLab(Vector3::Arg c)
|
{
|
||||||
{
|
return Vector3(toLinear(c.x), toLinear(c.y), toLinear(c.z));
|
||||||
return xyzToCieLab(rgbToXyz(toLinear(c)));
|
}
|
||||||
}
|
|
||||||
|
static Vector3 toGamma(Vector3::Arg c)
|
||||||
// h is hue-angle in radians
|
{
|
||||||
static Vector3 cieLabToLCh(Vector3::Arg c)
|
return Vector3(toGamma(c.x), toGamma(c.y), toGamma(c.z));
|
||||||
{
|
}
|
||||||
return Vector3(c.x, sqrtf(c.y*c.y + c.z*c.z), atan2f(c.y, c.z));
|
|
||||||
}
|
static float f(float t)
|
||||||
|
{
|
||||||
static void rgbToCieLab(const FloatImage * rgbImage, FloatImage * LabImage)
|
const float epsilon = powf(6.0f/29.0f, 3);
|
||||||
{
|
|
||||||
nvDebugCheck(rgbImage != NULL && LabImage != NULL);
|
if (t > epsilon) {
|
||||||
nvDebugCheck(rgbImage->width() == LabImage->width() && rgbImage->height() == LabImage->height());
|
return powf(t, 1.0f/3.0f);
|
||||||
nvDebugCheck(rgbImage->componentCount() >= 3 && LabImage->componentCount() >= 3);
|
}
|
||||||
|
else {
|
||||||
const uint w = rgbImage->width();
|
return 1.0f/3.0f * powf(29.0f/6.0f, 2) * t + 4.0f / 29.0f;
|
||||||
const uint h = LabImage->height();
|
}
|
||||||
|
}
|
||||||
const float * R = rgbImage->channel(0);
|
|
||||||
const float * G = rgbImage->channel(1);
|
static float finv(float t)
|
||||||
const float * B = rgbImage->channel(2);
|
{
|
||||||
|
if (t > 6.0f / 29.0f) {
|
||||||
float * L = LabImage->channel(0);
|
return 3.0f * powf(6.0f / 29.0f, 2) * (t - 4.0f / 29.0f);
|
||||||
float * a = LabImage->channel(1);
|
}
|
||||||
float * b = LabImage->channel(2);
|
else {
|
||||||
|
return powf(t, 3.0f);
|
||||||
const uint count = w*h;
|
}
|
||||||
for (uint i = 0; i < count; i++)
|
}
|
||||||
{
|
|
||||||
Vector3 Lab = rgbToCieLab(Vector3(R[i], G[i], B[i]));
|
static Vector3 xyzToCieLab(Vector3::Arg c)
|
||||||
L[i] = Lab.x;
|
{
|
||||||
a[i] = Lab.y;
|
// Normalized white point.
|
||||||
b[i] = Lab.z;
|
const float Xn = 0.950456f;
|
||||||
}
|
const float Yn = 1.0f;
|
||||||
}
|
const float Zn = 1.088754f;
|
||||||
|
|
||||||
|
float Xr = c.x / Xn;
|
||||||
// Assumes input images are in linear sRGB space.
|
float Yr = c.y / Yn;
|
||||||
float nv::cieLabError(const FloatImage * img0, const FloatImage * img1)
|
float Zr = c.z / Zn;
|
||||||
{
|
|
||||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
float fx = f(Xr);
|
||||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
float fy = f(Yr);
|
||||||
|
float fz = f(Zr);
|
||||||
const float * r0 = img0->channel(0);
|
|
||||||
const float * g0 = img0->channel(1);
|
float L = 116 * fx - 16;
|
||||||
const float * b0 = img0->channel(2);
|
float a = 500 * (fx - fy);
|
||||||
|
float b = 200 * (fy - fz);
|
||||||
const float * r1 = img1->channel(0);
|
|
||||||
const float * g1 = img1->channel(1);
|
return Vector3(L, a, b);
|
||||||
const float * b1 = img1->channel(2);
|
}
|
||||||
|
|
||||||
double error = 0.0f;
|
static Vector3 rgbToCieLab(Vector3::Arg c)
|
||||||
|
{
|
||||||
const uint count = img0->pixelCount();
|
return xyzToCieLab(rgbToXyz(toLinear(c)));
|
||||||
for (uint i = 0; i < count; i++)
|
}
|
||||||
{
|
|
||||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
// h is hue-angle in radians
|
||||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
static Vector3 cieLabToLCh(Vector3::Arg c)
|
||||||
|
{
|
||||||
// @@ Measure Delta E.
|
return Vector3(c.x, sqrtf(c.y*c.y + c.z*c.z), atan2f(c.y, c.z));
|
||||||
Vector3 delta = lab0 - lab1;
|
}
|
||||||
|
|
||||||
error += length(delta);
|
static void rgbToCieLab(const FloatImage * rgbImage, FloatImage * LabImage)
|
||||||
}
|
{
|
||||||
|
nvDebugCheck(rgbImage != NULL && LabImage != NULL);
|
||||||
return float(error / count);
|
nvDebugCheck(rgbImage->width() == LabImage->width() && rgbImage->height() == LabImage->height());
|
||||||
}
|
nvDebugCheck(rgbImage->componentCount() >= 3 && LabImage->componentCount() >= 3);
|
||||||
|
|
||||||
// Assumes input images are in linear sRGB space.
|
const uint w = rgbImage->width();
|
||||||
float nv::cieLab94Error(const FloatImage * img0, const FloatImage * img1)
|
const uint h = LabImage->height();
|
||||||
{
|
|
||||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
const float * R = rgbImage->channel(0);
|
||||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
const float * G = rgbImage->channel(1);
|
||||||
|
const float * B = rgbImage->channel(2);
|
||||||
const float kL = 1;
|
|
||||||
const float kC = 1;
|
float * L = LabImage->channel(0);
|
||||||
const float kH = 1;
|
float * a = LabImage->channel(1);
|
||||||
const float k1 = 0.045f;
|
float * b = LabImage->channel(2);
|
||||||
const float k2 = 0.015f;
|
|
||||||
|
const uint count = w*h;
|
||||||
const float sL = 1;
|
for (uint i = 0; i < count; i++)
|
||||||
|
{
|
||||||
const float * r0 = img0->channel(0);
|
Vector3 Lab = rgbToCieLab(Vector3(R[i], G[i], B[i]));
|
||||||
const float * g0 = img0->channel(1);
|
L[i] = Lab.x;
|
||||||
const float * b0 = img0->channel(2);
|
a[i] = Lab.y;
|
||||||
|
b[i] = Lab.z;
|
||||||
const float * r1 = img1->channel(0);
|
}
|
||||||
const float * g1 = img1->channel(1);
|
}
|
||||||
const float * b1 = img1->channel(2);
|
|
||||||
|
|
||||||
double error = 0.0f;
|
// Assumes input images are in linear sRGB space.
|
||||||
|
float nv::cieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||||
const uint count = img0->pixelCount();
|
{
|
||||||
for (uint i = 0; i < count; ++i)
|
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||||
{
|
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
|
||||||
Vector3 lch0 = cieLabToLCh(lab0);
|
const float * r0 = img0->channel(0);
|
||||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
const float * g0 = img0->channel(1);
|
||||||
Vector3 lch1 = cieLabToLCh(lab1);
|
const float * b0 = img0->channel(2);
|
||||||
|
|
||||||
const float sC = 1 + k1*lch0.x;
|
const float * r1 = img1->channel(0);
|
||||||
const float sH = 1 + k2*lch0.x;
|
const float * g1 = img1->channel(1);
|
||||||
|
const float * b1 = img1->channel(2);
|
||||||
// @@ Measure Delta E using the 1994 definition
|
|
||||||
Vector3 labDelta = lab0 - lab1;
|
double error = 0.0f;
|
||||||
Vector3 lchDelta = lch0 - lch1;
|
|
||||||
|
const uint count = img0->pixelCount();
|
||||||
double deltaLsq = powf(lchDelta.x / (kL*sL), 2);
|
for (uint i = 0; i < count; i++)
|
||||||
double deltaCsq = powf(lchDelta.y / (kC*sC), 2);
|
{
|
||||||
|
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||||
// avoid possible sqrt of negative value by computing (deltaH/(kH*sH))^2
|
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||||
double deltaHsq = powf(labDelta.y, 2) + powf(labDelta.z, 2) - powf(lchDelta.y, 2);
|
|
||||||
deltaHsq /= powf(kH*sH, 2);
|
// @@ Measure Delta E.
|
||||||
|
Vector3 delta = lab0 - lab1;
|
||||||
error += sqrt(deltaLsq + deltaCsq + deltaHsq);
|
|
||||||
}
|
error += length(delta);
|
||||||
|
}
|
||||||
return float(error / count);
|
|
||||||
}
|
return float(error / count);
|
||||||
|
}
|
||||||
float nv::spatialCieLabError(const FloatImage * img0, const FloatImage * img1)
|
|
||||||
{
|
// Assumes input images are in linear sRGB space.
|
||||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
float nv::cieLab94Error(const FloatImage * img0, const FloatImage * img1)
|
||||||
return FLT_MAX;
|
{
|
||||||
}
|
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||||
|
|
||||||
uint w = img0->width();
|
const float kL = 1;
|
||||||
uint h = img0->height();
|
const float kC = 1;
|
||||||
uint d = img0->depth();
|
const float kH = 1;
|
||||||
|
const float k1 = 0.045f;
|
||||||
FloatImage lab0, lab1; // Original images in CIE-Lab space.
|
const float k2 = 0.015f;
|
||||||
lab0.allocate(3, w, h, d);
|
|
||||||
lab1.allocate(3, w, h, d);
|
const float sL = 1;
|
||||||
|
|
||||||
// Convert input images to CIE-Lab.
|
const float * r0 = img0->channel(0);
|
||||||
rgbToCieLab(img0, &lab0);
|
const float * g0 = img0->channel(1);
|
||||||
rgbToCieLab(img1, &lab1);
|
const float * b0 = img0->channel(2);
|
||||||
|
|
||||||
// @@ Convolve each channel by the corresponding filter.
|
const float * r1 = img1->channel(0);
|
||||||
/*
|
const float * g1 = img1->channel(1);
|
||||||
GaussianFilter LFilter(5);
|
const float * b1 = img1->channel(2);
|
||||||
GaussianFilter aFilter(5);
|
|
||||||
GaussianFilter bFilter(5);
|
double error = 0.0f;
|
||||||
|
|
||||||
lab0.convolve(0, LFilter);
|
const uint count = img0->pixelCount();
|
||||||
lab0.convolve(1, aFilter);
|
for (uint i = 0; i < count; ++i)
|
||||||
lab0.convolve(2, bFilter);
|
{
|
||||||
|
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||||
lab1.convolve(0, LFilter);
|
Vector3 lch0 = cieLabToLCh(lab0);
|
||||||
lab1.convolve(1, aFilter);
|
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||||
lab1.convolve(2, bFilter);
|
Vector3 lch1 = cieLabToLCh(lab1);
|
||||||
*/
|
|
||||||
// @@ Measure Delta E between lab0 and lab1.
|
const float sC = 1 + k1*lch0.x;
|
||||||
|
const float sH = 1 + k2*lch0.x;
|
||||||
return 0.0f;
|
|
||||||
}
|
// @@ Measure Delta E using the 1994 definition
|
||||||
|
Vector3 labDelta = lab0 - lab1;
|
||||||
|
Vector3 lchDelta = lch0 - lch1;
|
||||||
// Assumes input images are normal maps.
|
|
||||||
float nv::averageAngularError(const FloatImage * img0, const FloatImage * img1)
|
double deltaLsq = powf(lchDelta.x / (kL*sL), 2);
|
||||||
{
|
double deltaCsq = powf(lchDelta.y / (kC*sC), 2);
|
||||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
|
||||||
return FLT_MAX;
|
// avoid possible sqrt of negative value by computing (deltaH/(kH*sH))^2
|
||||||
}
|
double deltaHsq = powf(labDelta.y, 2) + powf(labDelta.z, 2) - powf(lchDelta.y, 2);
|
||||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
deltaHsq /= powf(kH*sH, 2);
|
||||||
|
|
||||||
uint w = img0->width();
|
error += sqrt(deltaLsq + deltaCsq + deltaHsq);
|
||||||
uint h = img0->height();
|
}
|
||||||
|
|
||||||
const float * x0 = img0->channel(0);
|
return float(error / count);
|
||||||
const float * y0 = img0->channel(1);
|
}
|
||||||
const float * z0 = img0->channel(2);
|
|
||||||
|
float nv::spatialCieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||||
const float * x1 = img1->channel(0);
|
{
|
||||||
const float * y1 = img1->channel(1);
|
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||||
const float * z1 = img1->channel(2);
|
return FLT_MAX;
|
||||||
|
}
|
||||||
double error = 0.0f;
|
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||||
|
|
||||||
const uint count = w*h;
|
uint w = img0->width();
|
||||||
for (uint i = 0; i < count; i++)
|
uint h = img0->height();
|
||||||
{
|
uint d = img0->depth();
|
||||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
|
||||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
FloatImage lab0, lab1; // Original images in CIE-Lab space.
|
||||||
|
lab0.allocate(3, w, h, d);
|
||||||
n0 = 2.0f * n0 - Vector3(1);
|
lab1.allocate(3, w, h, d);
|
||||||
n1 = 2.0f * n1 - Vector3(1);
|
|
||||||
|
// Convert input images to CIE-Lab.
|
||||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
rgbToCieLab(img0, &lab0);
|
||||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
rgbToCieLab(img1, &lab1);
|
||||||
|
|
||||||
error += acos(clamp(dot(n0, n1), -1.0f, 1.0f));
|
// @@ Convolve each channel by the corresponding filter.
|
||||||
}
|
/*
|
||||||
|
GaussianFilter LFilter(5);
|
||||||
return float(error / count);
|
GaussianFilter aFilter(5);
|
||||||
}
|
GaussianFilter bFilter(5);
|
||||||
|
|
||||||
float nv::rmsAngularError(const FloatImage * img0, const FloatImage * img1)
|
lab0.convolve(0, LFilter);
|
||||||
{
|
lab0.convolve(1, aFilter);
|
||||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
lab0.convolve(2, bFilter);
|
||||||
return FLT_MAX;
|
|
||||||
}
|
lab1.convolve(0, LFilter);
|
||||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
lab1.convolve(1, aFilter);
|
||||||
|
lab1.convolve(2, bFilter);
|
||||||
uint w = img0->width();
|
*/
|
||||||
uint h = img0->height();
|
// @@ Measure Delta E between lab0 and lab1.
|
||||||
|
|
||||||
const float * x0 = img0->channel(0);
|
return 0.0f;
|
||||||
const float * y0 = img0->channel(1);
|
}
|
||||||
const float * z0 = img0->channel(2);
|
|
||||||
|
|
||||||
const float * x1 = img1->channel(0);
|
// Assumes input images are normal maps.
|
||||||
const float * y1 = img1->channel(1);
|
float nv::averageAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||||
const float * z1 = img1->channel(2);
|
{
|
||||||
|
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||||
double error = 0.0f;
|
return FLT_MAX;
|
||||||
|
}
|
||||||
const uint count = w*h;
|
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||||
for (uint i = 0; i < count; i++)
|
|
||||||
{
|
uint w = img0->width();
|
||||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
uint h = img0->height();
|
||||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
|
||||||
|
const float * x0 = img0->channel(0);
|
||||||
n0 = 2.0f * n0 - Vector3(1);
|
const float * y0 = img0->channel(1);
|
||||||
n1 = 2.0f * n1 - Vector3(1);
|
const float * z0 = img0->channel(2);
|
||||||
|
|
||||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
const float * x1 = img1->channel(0);
|
||||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
const float * y1 = img1->channel(1);
|
||||||
|
const float * z1 = img1->channel(2);
|
||||||
float angle = acosf(clamp(dot(n0, n1), -1.0f, 1.0f));
|
|
||||||
error += angle * angle;
|
double error = 0.0f;
|
||||||
}
|
|
||||||
|
const uint count = w*h;
|
||||||
return float(sqrt(error / count));
|
for (uint i = 0; i < count; i++)
|
||||||
}
|
{
|
||||||
|
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||||
|
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||||
|
|
||||||
|
n0 = 2.0f * n0 - Vector3(1);
|
||||||
|
n1 = 2.0f * n1 - Vector3(1);
|
||||||
|
|
||||||
|
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||||
|
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||||
|
|
||||||
|
error += acos(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
return float(error / count);
|
||||||
|
}
|
||||||
|
|
||||||
|
float nv::rmsAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||||
|
{
|
||||||
|
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||||
|
return FLT_MAX;
|
||||||
|
}
|
||||||
|
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||||
|
|
||||||
|
uint w = img0->width();
|
||||||
|
uint h = img0->height();
|
||||||
|
|
||||||
|
const float * x0 = img0->channel(0);
|
||||||
|
const float * y0 = img0->channel(1);
|
||||||
|
const float * z0 = img0->channel(2);
|
||||||
|
|
||||||
|
const float * x1 = img1->channel(0);
|
||||||
|
const float * y1 = img1->channel(1);
|
||||||
|
const float * z1 = img1->channel(2);
|
||||||
|
|
||||||
|
double error = 0.0f;
|
||||||
|
|
||||||
|
const uint count = w*h;
|
||||||
|
for (uint i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||||
|
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||||
|
|
||||||
|
n0 = 2.0f * n0 - Vector3(1);
|
||||||
|
n1 = 2.0f * n1 - Vector3(1);
|
||||||
|
|
||||||
|
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||||
|
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||||
|
|
||||||
|
float angle = acosf(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||||
|
error += angle * angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
return float(sqrt(error / count));
|
||||||
|
}
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,208 +1,208 @@
|
|||||||
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person
|
// Permission is hereby granted, free of charge, to any person
|
||||||
// obtaining a copy of this software and associated documentation
|
// obtaining a copy of this software and associated documentation
|
||||||
// files (the "Software"), to deal in the Software without
|
// files (the "Software"), to deal in the Software without
|
||||||
// restriction, including without limitation the rights to use,
|
// restriction, including without limitation the rights to use,
|
||||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// copies of the Software, and to permit persons to whom the
|
// copies of the Software, and to permit persons to whom the
|
||||||
// Software is furnished to do so, subject to the following
|
// Software is furnished to do so, subject to the following
|
||||||
// conditions:
|
// conditions:
|
||||||
//
|
//
|
||||||
// The above copyright notice and this permission notice shall be
|
// The above copyright notice and this permission notice shall be
|
||||||
// included in all copies or substantial portions of the Software.
|
// included in all copies or substantial portions of the Software.
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include "NormalMap.h"
|
#include "NormalMap.h"
|
||||||
#include "Filter.h"
|
#include "Filter.h"
|
||||||
#include "FloatImage.h"
|
#include "FloatImage.h"
|
||||||
#include "Image.h"
|
#include "Image.h"
|
||||||
|
|
||||||
#include "nvmath/Color.inl"
|
#include "nvmath/Color.inl"
|
||||||
#include "nvmath/Vector.h"
|
#include "nvmath/Vector.h"
|
||||||
|
|
||||||
#include "nvcore/Ptr.h"
|
#include "nvcore/Ptr.h"
|
||||||
|
|
||||||
#include <string.h> // memcpy
|
#include <string.h> // memcpy
|
||||||
|
|
||||||
|
|
||||||
using namespace nv;
|
using namespace nv;
|
||||||
|
|
||||||
// Create normal map using the given kernels.
|
// Create normal map using the given kernels.
|
||||||
static FloatImage * createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, const Kernel2 * kdu, const Kernel2 * kdv)
|
static FloatImage * createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||||
{
|
{
|
||||||
nvDebugCheck(kdu != NULL);
|
nvDebugCheck(kdu != NULL);
|
||||||
nvDebugCheck(kdv != NULL);
|
nvDebugCheck(kdv != NULL);
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
const uint w = img->width();
|
const uint w = img->width();
|
||||||
const uint h = img->height();
|
const uint h = img->height();
|
||||||
|
|
||||||
AutoPtr<FloatImage> fimage(new FloatImage());
|
AutoPtr<FloatImage> fimage(new FloatImage());
|
||||||
fimage->allocate(4, w, h);
|
fimage->allocate(4, w, h);
|
||||||
|
|
||||||
// Compute height and store in alpha channel:
|
// Compute height and store in alpha channel:
|
||||||
float * alphaChannel = fimage->channel(3);
|
float * alphaChannel = fimage->channel(3);
|
||||||
for(uint i = 0; i < w * h; i++)
|
for(uint i = 0; i < w * h; i++)
|
||||||
{
|
{
|
||||||
Vector4 color = toVector4(img->pixel(i));
|
Vector4 color = toVector4(img->pixel(i));
|
||||||
alphaChannel[i] = dot(color, heightWeights);
|
alphaChannel[i] = dot(color, heightWeights);
|
||||||
}
|
}
|
||||||
|
|
||||||
float heightScale = 1.0f / 16.0f; // @@ Use a user defined factor.
|
float heightScale = 1.0f / 16.0f; // @@ Use a user defined factor.
|
||||||
|
|
||||||
for(uint y = 0; y < h; y++)
|
for(uint y = 0; y < h; y++)
|
||||||
{
|
{
|
||||||
for(uint x = 0; x < w; x++)
|
for(uint x = 0; x < w; x++)
|
||||||
{
|
{
|
||||||
const float du = fimage->applyKernelXY(kdu, x, y, 0, 3, wm);
|
const float du = fimage->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||||
const float dv = fimage->applyKernelXY(kdv, x, y, 0, 3, wm);
|
const float dv = fimage->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||||
|
|
||||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||||
|
|
||||||
fimage->pixel(0, x, y, 0) = 0.5f * n.x + 0.5f;
|
fimage->pixel(0, x, y, 0) = 0.5f * n.x + 0.5f;
|
||||||
fimage->pixel(1, x, y, 0) = 0.5f * n.y + 0.5f;
|
fimage->pixel(1, x, y, 0) = 0.5f * n.y + 0.5f;
|
||||||
fimage->pixel(2, x, y, 0) = 0.5f * n.z + 0.5f;
|
fimage->pixel(2, x, y, 0) = 0.5f * n.z + 0.5f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fimage.release();
|
return fimage.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create normal map using the given kernels.
|
// Create normal map using the given kernels.
|
||||||
static FloatImage * createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, const Kernel2 * kdu, const Kernel2 * kdv)
|
static FloatImage * createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||||
{
|
{
|
||||||
nvDebugCheck(kdu != NULL);
|
nvDebugCheck(kdu != NULL);
|
||||||
nvDebugCheck(kdv != NULL);
|
nvDebugCheck(kdv != NULL);
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
#pragma NV_MESSAGE("FIXME: Height scale parameter should go away. It should be a sensible value that produces good results when the heightmap is in the [0, 1] range.")
|
#pragma NV_MESSAGE("FIXME: Height scale parameter should go away. It should be a sensible value that produces good results when the heightmap is in the [0, 1] range.")
|
||||||
const float heightScale = 1.0f / 16.0f;
|
const float heightScale = 1.0f / 16.0f;
|
||||||
|
|
||||||
const uint w = img->width();
|
const uint w = img->width();
|
||||||
const uint h = img->height();
|
const uint h = img->height();
|
||||||
|
|
||||||
AutoPtr<FloatImage> img_out(new FloatImage());
|
AutoPtr<FloatImage> img_out(new FloatImage());
|
||||||
img_out->allocate(4, w, h);
|
img_out->allocate(4, w, h);
|
||||||
|
|
||||||
for (uint y = 0; y < h; y++)
|
for (uint y = 0; y < h; y++)
|
||||||
{
|
{
|
||||||
for (uint x = 0; x < w; x++)
|
for (uint x = 0; x < w; x++)
|
||||||
{
|
{
|
||||||
const float du = img->applyKernelXY(kdu, x, y, 0, 3, wm);
|
const float du = img->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||||
const float dv = img->applyKernelXY(kdv, x, y, 0, 3, wm);
|
const float dv = img->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||||
|
|
||||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||||
|
|
||||||
img_out->pixel(0, x, y, 0) = n.x;
|
img_out->pixel(0, x, y, 0) = n.x;
|
||||||
img_out->pixel(1, x, y, 0) = n.y;
|
img_out->pixel(1, x, y, 0) = n.y;
|
||||||
img_out->pixel(2, x, y, 0) = n.z;
|
img_out->pixel(2, x, y, 0) = n.z;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy alpha channel.
|
// Copy alpha channel.
|
||||||
/*for (uint y = 0; y < h; y++)
|
/*for (uint y = 0; y < h; y++)
|
||||||
{
|
{
|
||||||
for (uint x = 0; x < w; x++)
|
for (uint x = 0; x < w; x++)
|
||||||
{
|
{
|
||||||
|
|
||||||
img_out->pixel(3, x, y, 0) = img->pixel(3, x, y, 0);
|
img_out->pixel(3, x, y, 0) = img->pixel(3, x, y, 0);
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
memcpy(img_out->channel(3), img->channel(3), w * h * sizeof(float));
|
memcpy(img_out->channel(3), img->channel(3), w * h * sizeof(float));
|
||||||
|
|
||||||
return img_out.release();
|
return img_out.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Create normal map using the given filter.
|
/// Create normal map using the given filter.
|
||||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, NormalMapFilter filter /*= Sobel3x3*/)
|
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, NormalMapFilter filter /*= Sobel3x3*/)
|
||||||
{
|
{
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
// Init the kernels.
|
// Init the kernels.
|
||||||
Kernel2 * kdu = NULL;
|
Kernel2 * kdu = NULL;
|
||||||
Kernel2 * kdv = NULL;
|
Kernel2 * kdv = NULL;
|
||||||
|
|
||||||
switch(filter)
|
switch(filter)
|
||||||
{
|
{
|
||||||
case NormalMapFilter_Sobel3x3:
|
case NormalMapFilter_Sobel3x3:
|
||||||
kdu = new Kernel2(3);
|
kdu = new Kernel2(3);
|
||||||
break;
|
break;
|
||||||
case NormalMapFilter_Sobel5x5:
|
case NormalMapFilter_Sobel5x5:
|
||||||
kdu = new Kernel2(5);
|
kdu = new Kernel2(5);
|
||||||
break;
|
break;
|
||||||
case NormalMapFilter_Sobel7x7:
|
case NormalMapFilter_Sobel7x7:
|
||||||
kdu = new Kernel2(7);
|
kdu = new Kernel2(7);
|
||||||
break;
|
break;
|
||||||
case NormalMapFilter_Sobel9x9:
|
case NormalMapFilter_Sobel9x9:
|
||||||
kdu = new Kernel2(9);
|
kdu = new Kernel2(9);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
nvDebugCheck(false);
|
nvDebugCheck(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
kdu->initSobel();
|
kdu->initSobel();
|
||||||
kdu->normalize();
|
kdu->normalize();
|
||||||
|
|
||||||
kdv = new Kernel2(*kdu);
|
kdv = new Kernel2(*kdu);
|
||||||
kdv->transpose();
|
kdv->transpose();
|
||||||
|
|
||||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Create normal map combining multiple sobel filters.
|
/// Create normal map combining multiple sobel filters.
|
||||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, Vector4::Arg filterWeights)
|
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, Vector4::Arg filterWeights)
|
||||||
{
|
{
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
Kernel2 * kdu = NULL;
|
Kernel2 * kdu = NULL;
|
||||||
Kernel2 * kdv = NULL;
|
Kernel2 * kdv = NULL;
|
||||||
|
|
||||||
kdu = new Kernel2(9);
|
kdu = new Kernel2(9);
|
||||||
kdu->initBlendedSobel(filterWeights);
|
kdu->initBlendedSobel(filterWeights);
|
||||||
kdu->normalize();
|
kdu->normalize();
|
||||||
|
|
||||||
kdv = new Kernel2(*kdu);
|
kdv = new Kernel2(*kdu);
|
||||||
kdv->transpose();
|
kdv->transpose();
|
||||||
|
|
||||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FloatImage * nv::createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, Vector4::Arg filterWeights)
|
FloatImage * nv::createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, Vector4::Arg filterWeights)
|
||||||
{
|
{
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
Kernel2 * kdu = NULL;
|
Kernel2 * kdu = NULL;
|
||||||
Kernel2 * kdv = NULL;
|
Kernel2 * kdv = NULL;
|
||||||
|
|
||||||
kdu = new Kernel2(9);
|
kdu = new Kernel2(9);
|
||||||
kdu->initBlendedSobel(filterWeights);
|
kdu->initBlendedSobel(filterWeights);
|
||||||
kdu->normalize();
|
kdu->normalize();
|
||||||
|
|
||||||
kdv = new Kernel2(*kdu);
|
kdv = new Kernel2(*kdu);
|
||||||
kdv->transpose();
|
kdv->transpose();
|
||||||
|
|
||||||
return ::createNormalMap(img, wm, kdu, kdv);
|
return ::createNormalMap(img, wm, kdu, kdv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Normalize the given image in place.
|
/// Normalize the given image in place.
|
||||||
void nv::normalizeNormalMap(FloatImage * img)
|
void nv::normalizeNormalMap(FloatImage * img)
|
||||||
{
|
{
|
||||||
nvDebugCheck(img != NULL);
|
nvDebugCheck(img != NULL);
|
||||||
|
|
||||||
img->normalize(0);
|
img->normalize(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,441 +1,487 @@
|
|||||||
// This code is in the public domain -- castanyo@yahoo.es
|
// This code is in the public domain -- castanyo@yahoo.es
|
||||||
|
|
||||||
#include "Matrix.inl"
|
#include "Matrix.inl"
|
||||||
#include "Vector.inl"
|
#include "Vector.inl"
|
||||||
|
|
||||||
#include "nvcore/Array.inl"
|
#include "nvcore/Array.inl"
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#if !NV_CC_MSVC && !NV_OS_ORBIS
|
#if !NV_CC_MSVC && !NV_OS_ORBIS
|
||||||
#include <alloca.h>
|
#include <alloca.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using namespace nv;
|
using namespace nv;
|
||||||
|
|
||||||
|
|
||||||
// Given a matrix a[1..n][1..n], this routine replaces it by the LU decomposition of a rowwise
|
// Given a matrix a[1..n][1..n], this routine replaces it by the LU decomposition of a rowwise
|
||||||
// permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above;
|
// permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above;
|
||||||
// indx[1..n] is an output vector that records the row permutation effected by the partial
|
// indx[1..n] is an output vector that records the row permutation effected by the partial
|
||||||
// pivoting; d is output as -1 depending on whether the number of row interchanges was even
|
// pivoting; d is output as -1 depending on whether the number of row interchanges was even
|
||||||
// or odd, respectively. This routine is used in combination with lubksb to solve linear equations
|
// or odd, respectively. This routine is used in combination with lubksb to solve linear equations
|
||||||
// or invert a matrix.
|
// or invert a matrix.
|
||||||
static bool ludcmp(float **a, int n, int *indx, float *d)
|
static bool ludcmp(float **a, int n, int *indx, float *d)
|
||||||
{
|
{
|
||||||
const float TINY = 1.0e-20f;
|
const float TINY = 1.0e-20f;
|
||||||
|
|
||||||
float * vv = (float*)alloca(sizeof(float) * n); // vv stores the implicit scaling of each row.
|
float * vv = (float*)alloca(sizeof(float) * n); // vv stores the implicit scaling of each row.
|
||||||
|
|
||||||
*d = 1.0; // No row interchanges yet.
|
*d = 1.0; // No row interchanges yet.
|
||||||
for (int i = 0; i < n; i++) { // Loop over rows to get the implicit scaling information.
|
for (int i = 0; i < n; i++) { // Loop over rows to get the implicit scaling information.
|
||||||
|
|
||||||
float big = 0.0;
|
float big = 0.0;
|
||||||
for (int j = 0; j < n; j++) {
|
for (int j = 0; j < n; j++) {
|
||||||
big = max(big, fabsf(a[i][j]));
|
big = max(big, fabsf(a[i][j]));
|
||||||
}
|
}
|
||||||
if (big == 0) {
|
if (big == 0) {
|
||||||
return false; // Singular matrix
|
return false; // Singular matrix
|
||||||
}
|
}
|
||||||
|
|
||||||
// No nonzero largest element.
|
// No nonzero largest element.
|
||||||
vv[i] = 1.0f / big; // Save the scaling.
|
vv[i] = 1.0f / big; // Save the scaling.
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int j = 0; j < n; j++) { // This is the loop over columns of Crout's method.
|
for (int j = 0; j < n; j++) { // This is the loop over columns of Crout's method.
|
||||||
for (int i = 0; i < j; i++) { // This is equation (2.3.12) except for i = j.
|
for (int i = 0; i < j; i++) { // This is equation (2.3.12) except for i = j.
|
||||||
float sum = a[i][j];
|
float sum = a[i][j];
|
||||||
for (int k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
|
for (int k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
|
||||||
a[i][j] = sum;
|
a[i][j] = sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
int imax = -1;
|
int imax = -1;
|
||||||
float big = 0.0; // Initialize for the search for largest pivot element.
|
float big = 0.0; // Initialize for the search for largest pivot element.
|
||||||
for (int i = j; i < n; i++) { // This is i = j of equation (2.3.12) and i = j+ 1 : : : N
|
for (int i = j; i < n; i++) { // This is i = j of equation (2.3.12) and i = j+ 1 : : : N
|
||||||
float sum = a[i][j]; // of equation (2.3.13).
|
float sum = a[i][j]; // of equation (2.3.13).
|
||||||
for (int k = 0; k < j; k++) {
|
for (int k = 0; k < j; k++) {
|
||||||
sum -= a[i][k]*a[k][j];
|
sum -= a[i][k]*a[k][j];
|
||||||
}
|
}
|
||||||
a[i][j]=sum;
|
a[i][j]=sum;
|
||||||
|
|
||||||
float dum = vv[i]*fabs(sum);
|
float dum = vv[i]*fabs(sum);
|
||||||
if (dum >= big) {
|
if (dum >= big) {
|
||||||
// Is the figure of merit for the pivot better than the best so far?
|
// Is the figure of merit for the pivot better than the best so far?
|
||||||
big = dum;
|
big = dum;
|
||||||
imax = i;
|
imax = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nvDebugCheck(imax != -1);
|
nvDebugCheck(imax != -1);
|
||||||
|
|
||||||
if (j != imax) { // Do we need to interchange rows?
|
if (j != imax) { // Do we need to interchange rows?
|
||||||
for (int k = 0; k < n; k++) { // Yes, do so...
|
for (int k = 0; k < n; k++) { // Yes, do so...
|
||||||
swap(a[imax][k], a[j][k]);
|
swap(a[imax][k], a[j][k]);
|
||||||
}
|
}
|
||||||
*d = -(*d); // ...and change the parity of d.
|
*d = -(*d); // ...and change the parity of d.
|
||||||
vv[imax]=vv[j]; // Also interchange the scale factor.
|
vv[imax]=vv[j]; // Also interchange the scale factor.
|
||||||
}
|
}
|
||||||
|
|
||||||
indx[j]=imax;
|
indx[j]=imax;
|
||||||
if (a[j][j] == 0.0) a[j][j] = TINY;
|
if (a[j][j] == 0.0) a[j][j] = TINY;
|
||||||
|
|
||||||
// If the pivot element is zero the matrix is singular (at least to the precision of the
|
// If the pivot element is zero the matrix is singular (at least to the precision of the
|
||||||
// algorithm). For some applications on singular matrices, it is desirable to substitute
|
// algorithm). For some applications on singular matrices, it is desirable to substitute
|
||||||
// TINY for zero.
|
// TINY for zero.
|
||||||
if (j != n-1) { // Now, finally, divide by the pivot element.
|
if (j != n-1) { // Now, finally, divide by the pivot element.
|
||||||
float dum = 1.0f / a[j][j];
|
float dum = 1.0f / a[j][j];
|
||||||
for (int i = j+1; i < n; i++) a[i][j] *= dum;
|
for (int i = j+1; i < n; i++) a[i][j] *= dum;
|
||||||
}
|
}
|
||||||
} // Go back for the next column in the reduction.
|
} // Go back for the next column in the reduction.
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Solves the set of n linear equations Ax = b. Here a[1..n][1..n] is input, not as the matrix
|
// Solves the set of n linear equations Ax = b. Here a[1..n][1..n] is input, not as the matrix
|
||||||
// A but rather as its LU decomposition, determined by the routine ludcmp. indx[1..n] is input
|
// A but rather as its LU decomposition, determined by the routine ludcmp. indx[1..n] is input
|
||||||
// as the permutation vector returned by ludcmp. b[1..n] is input as the right-hand side vector
|
// as the permutation vector returned by ludcmp. b[1..n] is input as the right-hand side vector
|
||||||
// B, and returns with the solution vector X. a, n, and indx are not modified by this routine
|
// B, and returns with the solution vector X. a, n, and indx are not modified by this routine
|
||||||
// and can be left in place for successive calls with different right-hand sides b. This routine takes
|
// and can be left in place for successive calls with different right-hand sides b. This routine takes
|
||||||
// into account the possibility that b will begin with many zero elements, so it is efficient for use
|
// into account the possibility that b will begin with many zero elements, so it is efficient for use
|
||||||
// in matrix inversion.
|
// in matrix inversion.
|
||||||
static void lubksb(float **a, int n, int *indx, float b[])
|
static void lubksb(float **a, int n, int *indx, float b[])
|
||||||
{
|
{
|
||||||
int ii = 0;
|
int ii = 0;
|
||||||
for (int i=0; i<n; i++) { // When ii is set to a positive value, it will become
|
for (int i=0; i<n; i++) { // When ii is set to a positive value, it will become
|
||||||
int ip = indx[i]; // the index of the first nonvanishing element of b. We now
|
int ip = indx[i]; // the index of the first nonvanishing element of b. We now
|
||||||
float sum = b[ip]; // do the forward substitution, equation (2.3.6). The
|
float sum = b[ip]; // do the forward substitution, equation (2.3.6). The
|
||||||
b[ip] = b[i]; // only new wrinkle is to unscramble the permutation as we go.
|
b[ip] = b[i]; // only new wrinkle is to unscramble the permutation as we go.
|
||||||
if (ii != 0) {
|
if (ii != 0) {
|
||||||
for (int j = ii-1; j < i; j++) sum -= a[i][j]*b[j];
|
for (int j = ii-1; j < i; j++) sum -= a[i][j]*b[j];
|
||||||
}
|
}
|
||||||
else if (sum != 0.0f) {
|
else if (sum != 0.0f) {
|
||||||
ii = i+1; // A nonzero element was encountered, so from now on we
|
ii = i+1; // A nonzero element was encountered, so from now on we
|
||||||
}
|
}
|
||||||
b[i] = sum; // will have to do the sums in the loop above.
|
b[i] = sum; // will have to do the sums in the loop above.
|
||||||
}
|
}
|
||||||
for (int i=n-1; i>=0; i--) { // Now we do the backsubstitution, equation (2.3.7).
|
for (int i=n-1; i>=0; i--) { // Now we do the backsubstitution, equation (2.3.7).
|
||||||
float sum = b[i];
|
float sum = b[i];
|
||||||
for (int j = i+1; j < n; j++) {
|
for (int j = i+1; j < n; j++) {
|
||||||
sum -= a[i][j]*b[j];
|
sum -= a[i][j]*b[j];
|
||||||
}
|
}
|
||||||
b[i] = sum/a[i][i]; // Store a component of the solution vector X.
|
b[i] = sum/a[i][i]; // Store a component of the solution vector X.
|
||||||
} // All done!
|
} // All done!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool nv::solveLU(const Matrix & A, const Vector4 & b, Vector4 * x)
|
bool nv::solveLU(const Matrix & A, const Vector4 & b, Vector4 * x)
|
||||||
{
|
{
|
||||||
nvDebugCheck(x != NULL);
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
float m[4][4];
|
float m[4][4];
|
||||||
float *a[4] = {m[0], m[1], m[2], m[3]};
|
float *a[4] = {m[0], m[1], m[2], m[3]};
|
||||||
int idx[4];
|
int idx[4];
|
||||||
float d;
|
float d;
|
||||||
|
|
||||||
for (int y = 0; y < 4; y++) {
|
for (int y = 0; y < 4; y++) {
|
||||||
for (int x = 0; x < 4; x++) {
|
for (int x = 0; x < 4; x++) {
|
||||||
a[x][y] = A(x, y);
|
a[x][y] = A(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create LU decomposition.
|
// Create LU decomposition.
|
||||||
if (!ludcmp(a, 4, idx, &d)) {
|
if (!ludcmp(a, 4, idx, &d)) {
|
||||||
// Singular matrix.
|
// Singular matrix.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init solution.
|
// Init solution.
|
||||||
*x = b;
|
*x = b;
|
||||||
|
|
||||||
// Do back substitution.
|
// Do back substitution.
|
||||||
lubksb(a, 4, idx, x->component);
|
lubksb(a, 4, idx, x->component);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @@ Not tested.
|
// @@ Not tested.
|
||||||
Matrix nv::inverseLU(const Matrix & A)
|
Matrix nv::inverseLU(const Matrix & A)
|
||||||
{
|
{
|
||||||
Vector4 Ai[4];
|
Vector4 Ai[4];
|
||||||
|
|
||||||
solveLU(A, Vector4(1, 0, 0, 0), &Ai[0]);
|
solveLU(A, Vector4(1, 0, 0, 0), &Ai[0]);
|
||||||
solveLU(A, Vector4(0, 1, 0, 0), &Ai[1]);
|
solveLU(A, Vector4(0, 1, 0, 0), &Ai[1]);
|
||||||
solveLU(A, Vector4(0, 0, 1, 0), &Ai[2]);
|
solveLU(A, Vector4(0, 0, 1, 0), &Ai[2]);
|
||||||
solveLU(A, Vector4(0, 0, 0, 1), &Ai[3]);
|
solveLU(A, Vector4(0, 0, 0, 1), &Ai[3]);
|
||||||
|
|
||||||
return Matrix(Ai[0], Ai[1], Ai[2], Ai[3]);
|
return Matrix(Ai[0], Ai[1], Ai[2], Ai[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool nv::solveLU(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
bool nv::solveLU(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
||||||
{
|
{
|
||||||
nvDebugCheck(x != NULL);
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
float m[3][3];
|
float m[3][3];
|
||||||
float *a[3] = {m[0], m[1], m[2]};
|
float *a[3] = {m[0], m[1], m[2]};
|
||||||
int idx[3];
|
int idx[3];
|
||||||
float d;
|
float d;
|
||||||
|
|
||||||
for (int y = 0; y < 3; y++) {
|
for (int y = 0; y < 3; y++) {
|
||||||
for (int x = 0; x < 3; x++) {
|
for (int x = 0; x < 3; x++) {
|
||||||
a[x][y] = A(x, y);
|
a[x][y] = A(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create LU decomposition.
|
// Create LU decomposition.
|
||||||
if (!ludcmp(a, 3, idx, &d)) {
|
if (!ludcmp(a, 3, idx, &d)) {
|
||||||
// Singular matrix.
|
// Singular matrix.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init solution.
|
// Init solution.
|
||||||
*x = b;
|
*x = b;
|
||||||
|
|
||||||
// Do back substitution.
|
// Do back substitution.
|
||||||
lubksb(a, 3, idx, x->component);
|
lubksb(a, 3, idx, x->component);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool nv::solveLU(const Matrix2 & A, const Vector2 & b, Vector2 * x)
|
||||||
bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
|
{
|
||||||
{
|
nvDebugCheck(x != NULL);
|
||||||
nvDebugCheck(x != NULL);
|
|
||||||
|
float m[2][2];
|
||||||
*x = transform(inverseCramer(A), b);
|
float *a[2] = {m[0], m[1]};
|
||||||
|
int idx[2];
|
||||||
return true; // @@ Return false if determinant(A) == 0 !
|
float d;
|
||||||
}
|
|
||||||
|
for (int y = 0; y < 2; y++) {
|
||||||
bool nv::solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
for (int x = 0; x < 2; x++) {
|
||||||
{
|
a[x][y] = A(x, y);
|
||||||
nvDebugCheck(x != NULL);
|
}
|
||||||
|
}
|
||||||
const float det = A.determinant();
|
|
||||||
if (equal(det, 0.0f)) { // @@ Use input epsilon.
|
// Create LU decomposition.
|
||||||
return false;
|
if (!ludcmp(a, 2, idx, &d)) {
|
||||||
}
|
// Singular matrix.
|
||||||
|
return false;
|
||||||
Matrix3 Ai = inverseCramer(A);
|
}
|
||||||
|
|
||||||
*x = transform(Ai, b);
|
// Init solution.
|
||||||
|
*x = b;
|
||||||
return true;
|
|
||||||
}
|
// Do back substitution.
|
||||||
|
lubksb(a, 2, idx, x->component);
|
||||||
|
|
||||||
|
return true;
|
||||||
// Inverse using gaussian elimination. From Jon's code.
|
}
|
||||||
Matrix nv::inverse(const Matrix & m) {
|
|
||||||
|
|
||||||
Matrix A = m;
|
bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
|
||||||
Matrix B(identity);
|
{
|
||||||
|
nvDebugCheck(x != NULL);
|
||||||
int i, j, k;
|
|
||||||
float max, t, det, pivot;
|
*x = transform(inverseCramer(A), b);
|
||||||
|
|
||||||
det = 1.0;
|
return true; // @@ Return false if determinant(A) == 0 !
|
||||||
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
|
}
|
||||||
max = -1.;
|
|
||||||
for (k=i; k<4; k++) /* find pivot for column i */
|
bool nv::solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
||||||
if (fabs(A(k, i)) > max) {
|
{
|
||||||
max = fabs(A(k, i));
|
nvDebugCheck(x != NULL);
|
||||||
j = k;
|
|
||||||
}
|
const float det = A.determinant();
|
||||||
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
if (equal(det, 0.0f)) { // @@ Use input epsilon.
|
||||||
if (j!=i) { /* swap rows i and j */
|
return false;
|
||||||
for (k=i; k<4; k++)
|
}
|
||||||
swap(A(i, k), A(j, k));
|
|
||||||
for (k=0; k<4; k++)
|
Matrix3 Ai = inverseCramer(A);
|
||||||
swap(B(i, k), B(j, k));
|
|
||||||
det = -det;
|
*x = transform(Ai, b);
|
||||||
}
|
|
||||||
pivot = A(i, i);
|
return true;
|
||||||
det *= pivot;
|
}
|
||||||
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
|
|
||||||
A(i, k) /= pivot;
|
bool nv::solveCramer(const Matrix2 & A, const Vector2 & b, Vector2 * x)
|
||||||
for (k=0; k<4; k++)
|
{
|
||||||
B(i, k) /= pivot;
|
nvDebugCheck(x != NULL);
|
||||||
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
|
||||||
|
const float det = A.determinant();
|
||||||
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
|
if (equal(det, 0.0f)) { // @@ Use input epsilon.
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
return false;
|
||||||
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
|
}
|
||||||
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
|
||||||
for (k=0; k<4; k++)
|
Matrix2 Ai = inverseCramer(A);
|
||||||
B(j, k) -= B(i, k)*t;
|
|
||||||
}
|
*x = transform(Ai, b);
|
||||||
}
|
|
||||||
|
return true;
|
||||||
/*---------- backward elimination ----------*/
|
}
|
||||||
|
|
||||||
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
|
|
||||||
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
// Inverse using gaussian elimination. From Jon's code.
|
||||||
for (k=0; k<4; k++) /* subtract scaled row i from row j */
|
Matrix nv::inverse(const Matrix & m) {
|
||||||
B(j, k) -= B(i, k)*t;
|
|
||||||
}
|
Matrix A = m;
|
||||||
}
|
Matrix B(identity);
|
||||||
|
|
||||||
return B;
|
int i, j, k;
|
||||||
}
|
float max, t, det, pivot;
|
||||||
|
|
||||||
|
det = 1.0;
|
||||||
Matrix3 nv::inverse(const Matrix3 & m) {
|
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
|
||||||
|
max = -1.;
|
||||||
Matrix3 A = m;
|
for (k=i; k<4; k++) /* find pivot for column i */
|
||||||
Matrix3 B(identity);
|
if (fabs(A(k, i)) > max) {
|
||||||
|
max = fabs(A(k, i));
|
||||||
int i, j, k;
|
j = k;
|
||||||
float max, t, det, pivot;
|
}
|
||||||
|
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
||||||
det = 1.0;
|
if (j!=i) { /* swap rows i and j */
|
||||||
for (i=0; i<3; i++) { /* eliminate in column i, below diag */
|
for (k=i; k<4; k++)
|
||||||
max = -1.;
|
swap(A(i, k), A(j, k));
|
||||||
for (k=i; k<3; k++) /* find pivot for column i */
|
for (k=0; k<4; k++)
|
||||||
if (fabs(A(k, i)) > max) {
|
swap(B(i, k), B(j, k));
|
||||||
max = fabs(A(k, i));
|
det = -det;
|
||||||
j = k;
|
}
|
||||||
}
|
pivot = A(i, i);
|
||||||
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
det *= pivot;
|
||||||
if (j!=i) { /* swap rows i and j */
|
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
|
||||||
for (k=i; k<3; k++)
|
A(i, k) /= pivot;
|
||||||
swap(A(i, k), A(j, k));
|
for (k=0; k<4; k++)
|
||||||
for (k=0; k<3; k++)
|
B(i, k) /= pivot;
|
||||||
swap(B(i, k), B(j, k));
|
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
||||||
det = -det;
|
|
||||||
}
|
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
|
||||||
pivot = A(i, i);
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
det *= pivot;
|
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
|
||||||
for (k=i+1; k<3; k++) /* only do elems to right of pivot */
|
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
||||||
A(i, k) /= pivot;
|
for (k=0; k<4; k++)
|
||||||
for (k=0; k<3; k++)
|
B(j, k) -= B(i, k)*t;
|
||||||
B(i, k) /= pivot;
|
}
|
||||||
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
}
|
||||||
|
|
||||||
for (j=i+1; j<3; j++) { /* eliminate in rows below i */
|
/*---------- backward elimination ----------*/
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
|
||||||
for (k=i+1; k<3; k++) /* subtract scaled row i from row j */
|
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
|
||||||
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
||||||
for (k=0; k<3; k++)
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
B(j, k) -= B(i, k)*t;
|
for (k=0; k<4; k++) /* subtract scaled row i from row j */
|
||||||
}
|
B(j, k) -= B(i, k)*t;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/*---------- backward elimination ----------*/
|
|
||||||
|
return B;
|
||||||
for (i=3-1; i>0; i--) { /* eliminate in column i, above diag */
|
}
|
||||||
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
|
||||||
for (k=0; k<3; k++) /* subtract scaled row i from row j */
|
Matrix3 nv::inverse(const Matrix3 & m) {
|
||||||
B(j, k) -= B(i, k)*t;
|
|
||||||
}
|
Matrix3 A = m;
|
||||||
}
|
Matrix3 B(identity);
|
||||||
|
|
||||||
return B;
|
int i, j, k;
|
||||||
}
|
float max, t, det, pivot;
|
||||||
|
|
||||||
|
det = 1.0;
|
||||||
|
for (i=0; i<3; i++) { /* eliminate in column i, below diag */
|
||||||
|
max = -1.;
|
||||||
|
for (k=i; k<3; k++) /* find pivot for column i */
|
||||||
#if 0
|
if (fabs(A(k, i)) > max) {
|
||||||
|
max = fabs(A(k, i));
|
||||||
// Copyright (C) 1999-2004 Michael Garland.
|
j = k;
|
||||||
//
|
}
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
||||||
// copy of this software and associated documentation files (the
|
if (j!=i) { /* swap rows i and j */
|
||||||
// "Software"), to deal in the Software without restriction, including
|
for (k=i; k<3; k++)
|
||||||
// without limitation the rights to use, copy, modify, merge, publish,
|
swap(A(i, k), A(j, k));
|
||||||
// distribute, and/or sell copies of the Software, and to permit persons
|
for (k=0; k<3; k++)
|
||||||
// to whom the Software is furnished to do so, provided that the above
|
swap(B(i, k), B(j, k));
|
||||||
// copyright notice(s) and this permission notice appear in all copies of
|
det = -det;
|
||||||
// the Software and that both the above copyright notice(s) and this
|
}
|
||||||
// permission notice appear in supporting documentation.
|
pivot = A(i, i);
|
||||||
//
|
det *= pivot;
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
for (k=i+1; k<3; k++) /* only do elems to right of pivot */
|
||||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
A(i, k) /= pivot;
|
||||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
for (k=0; k<3; k++)
|
||||||
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
B(i, k) /= pivot;
|
||||||
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
|
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
||||||
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
|
|
||||||
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
for (j=i+1; j<3; j++) { /* eliminate in rows below i */
|
||||||
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
for (k=i+1; k<3; k++) /* subtract scaled row i from row j */
|
||||||
//
|
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
||||||
// Except as contained in this notice, the name of a copyright holder
|
for (k=0; k<3; k++)
|
||||||
// shall not be used in advertising or otherwise to promote the sale, use
|
B(j, k) -= B(i, k)*t;
|
||||||
// or other dealings in this Software without prior written authorization
|
}
|
||||||
// of the copyright holder.
|
}
|
||||||
|
|
||||||
|
/*---------- backward elimination ----------*/
|
||||||
// Matrix inversion code for 4x4 matrices using Gaussian elimination
|
|
||||||
// with partial pivoting. This is a specialized version of a
|
for (i=3-1; i>0; i--) { /* eliminate in column i, above diag */
|
||||||
// procedure originally due to Paul Heckbert <ph@cs.cmu.edu>.
|
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
||||||
//
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
// Returns determinant of A, and B=inverse(A)
|
for (k=0; k<3; k++) /* subtract scaled row i from row j */
|
||||||
// If matrix A is singular, returns 0 and leaves trash in B.
|
B(j, k) -= B(i, k)*t;
|
||||||
//
|
}
|
||||||
#define SWAP(a, b, t) {t = a; a = b; b = t;}
|
}
|
||||||
double invert(Mat4& B, const Mat4& m)
|
|
||||||
{
|
return B;
|
||||||
Mat4 A = m;
|
}
|
||||||
int i, j, k;
|
|
||||||
double max, t, det, pivot;
|
|
||||||
|
|
||||||
/*---------- forward elimination ----------*/
|
|
||||||
|
|
||||||
for (i=0; i<4; i++) /* put identity matrix in B */
|
#if 0
|
||||||
for (j=0; j<4; j++)
|
|
||||||
B(i, j) = (double)(i==j);
|
// Copyright (C) 1999-2004 Michael Garland.
|
||||||
|
//
|
||||||
det = 1.0;
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
|
// copy of this software and associated documentation files (the
|
||||||
max = -1.;
|
// "Software"), to deal in the Software without restriction, including
|
||||||
for (k=i; k<4; k++) /* find pivot for column i */
|
// without limitation the rights to use, copy, modify, merge, publish,
|
||||||
if (fabs(A(k, i)) > max) {
|
// distribute, and/or sell copies of the Software, and to permit persons
|
||||||
max = fabs(A(k, i));
|
// to whom the Software is furnished to do so, provided that the above
|
||||||
j = k;
|
// copyright notice(s) and this permission notice appear in all copies of
|
||||||
}
|
// the Software and that both the above copyright notice(s) and this
|
||||||
if (max<=0.) return 0.; /* if no nonzero pivot, PUNT */
|
// permission notice appear in supporting documentation.
|
||||||
if (j!=i) { /* swap rows i and j */
|
//
|
||||||
for (k=i; k<4; k++)
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
SWAP(A(i, k), A(j, k), t);
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
for (k=0; k<4; k++)
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||||
SWAP(B(i, k), B(j, k), t);
|
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||||
det = -det;
|
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
|
||||||
}
|
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
|
||||||
pivot = A(i, i);
|
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
det *= pivot;
|
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||||
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
|
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
A(i, k) /= pivot;
|
//
|
||||||
for (k=0; k<4; k++)
|
// Except as contained in this notice, the name of a copyright holder
|
||||||
B(i, k) /= pivot;
|
// shall not be used in advertising or otherwise to promote the sale, use
|
||||||
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
// or other dealings in this Software without prior written authorization
|
||||||
|
// of the copyright holder.
|
||||||
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
|
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
|
||||||
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
|
// Matrix inversion code for 4x4 matrices using Gaussian elimination
|
||||||
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
// with partial pivoting. This is a specialized version of a
|
||||||
for (k=0; k<4; k++)
|
// procedure originally due to Paul Heckbert <ph@cs.cmu.edu>.
|
||||||
B(j, k) -= B(i, k)*t;
|
//
|
||||||
}
|
// Returns determinant of A, and B=inverse(A)
|
||||||
}
|
// If matrix A is singular, returns 0 and leaves trash in B.
|
||||||
|
//
|
||||||
/*---------- backward elimination ----------*/
|
#define SWAP(a, b, t) {t = a; a = b; b = t;}
|
||||||
|
double invert(Mat4& B, const Mat4& m)
|
||||||
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
|
{
|
||||||
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
Mat4 A = m;
|
||||||
t = A(j, i); /* we're gonna zero this guy */
|
int i, j, k;
|
||||||
for (k=0; k<4; k++) /* subtract scaled row i from row j */
|
double max, t, det, pivot;
|
||||||
B(j, k) -= B(i, k)*t;
|
|
||||||
}
|
/*---------- forward elimination ----------*/
|
||||||
}
|
|
||||||
|
for (i=0; i<4; i++) /* put identity matrix in B */
|
||||||
return det;
|
for (j=0; j<4; j++)
|
||||||
}
|
B(i, j) = (double)(i==j);
|
||||||
|
|
||||||
#endif // 0
|
det = 1.0;
|
||||||
|
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
|
||||||
|
max = -1.;
|
||||||
|
for (k=i; k<4; k++) /* find pivot for column i */
|
||||||
|
if (fabs(A(k, i)) > max) {
|
||||||
|
max = fabs(A(k, i));
|
||||||
|
j = k;
|
||||||
|
}
|
||||||
|
if (max<=0.) return 0.; /* if no nonzero pivot, PUNT */
|
||||||
|
if (j!=i) { /* swap rows i and j */
|
||||||
|
for (k=i; k<4; k++)
|
||||||
|
SWAP(A(i, k), A(j, k), t);
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
SWAP(B(i, k), B(j, k), t);
|
||||||
|
det = -det;
|
||||||
|
}
|
||||||
|
pivot = A(i, i);
|
||||||
|
det *= pivot;
|
||||||
|
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
|
||||||
|
A(i, k) /= pivot;
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
B(i, k) /= pivot;
|
||||||
|
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
||||||
|
|
||||||
|
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
|
||||||
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
|
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
|
||||||
|
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
B(j, k) -= B(i, k)*t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*---------- backward elimination ----------*/
|
||||||
|
|
||||||
|
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
|
||||||
|
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
||||||
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
|
for (k=0; k<4; k++) /* subtract scaled row i from row j */
|
||||||
|
B(j, k) -= B(i, k)*t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return det;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,20 @@
|
|||||||
|
#include "nvcore/nvcore.h"
|
||||||
|
|
||||||
|
namespace nv {
|
||||||
|
|
||||||
|
class Vector3;
|
||||||
|
class Vector4;
|
||||||
|
|
||||||
|
void decompress_etc(const void * input_block, Vector4 output_colors[16]);
|
||||||
|
void decompress_eac(const void * input_block, Vector4 output_colors[16], int output_channel);
|
||||||
|
void decompress_etc_eac(const void * input_block, Vector4 output_colors[16]);
|
||||||
|
|
||||||
|
float compress_etc1(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||||
|
float compress_etc2(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||||
|
float compress_etc2_a1(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||||
|
float compress_eac(Vector4 input_colors[16], float input_weights[16], int input_channel, int search_radius, bool use_11bit_mode, void * output);
|
||||||
|
float compress_etc2_eac(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue