#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "iqa.h"
#include "convolve.h"
#include "ssim_tools.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
IQA_INLINE static double _calc_luminance(float mu1, float mu2, float C1, float alpha)
{
double result;
float sign;
if (C1 == 0 && mu1*mu1 == 0 && mu2*mu2 == 0)
return 1.0;
result = (2.0 * mu1 * mu2 + C1) / (mu1*mu1 + mu2*mu2 + C1);
if (alpha == 1.0f)
return result;
sign = result < 0.0 ? -1.0f : 1.0f;
return sign * pow(fabs(result),(double)alpha);
}
IQA_INLINE static double _calc_contrast(double sigma_comb_12, float sigma1_sqd, float sigma2_sqd, float C2, float beta)
{
double result;
float sign;
if (C2 == 0 && sigma1_sqd + sigma2_sqd == 0)
return 1.0;
result = (2.0 * sigma_comb_12 + C2) / (sigma1_sqd + sigma2_sqd + C2);
if (beta == 1.0f)
return result;
sign = result < 0.0 ? -1.0f : 1.0f;
return sign * pow(fabs(result),(double)beta);
}
IQA_INLINE static double _calc_structure(float sigma_12, double sigma_comb_12, float sigma1, float sigma2, float C3, float gamma)
{
double result;
float sign;
if (C3 == 0 && sigma_comb_12 == 0) {
if (sigma1 == 0 && sigma2 == 0)
return 1.0;
else if (sigma1 == 0 || sigma2 == 0)
return 0.0;
}
result = (sigma_12 + C3) / (sigma_comb_12 + C3);
if (gamma == 1.0f)
return result;
sign = result < 0.0 ? -1.0f : 1.0f;
return sign * pow(fabs(result),(double)gamma);
}
float _iqa_ssim(float *ref, float *cmp, int w, int h, const struct _kernel *k,
const struct _map_reduce *mr, const struct iqa_ssim_args *args
, float *l_mean, float *c_mean, float *s_mean
)
{
float alpha=1.0f, beta=1.0f, gamma=1.0f;
int L=255;
float K1=0.01f, K2=0.03f;
float C1,C2,C3;
int x,y,offset;
float *ref_mu,*cmp_mu,*ref_sigma_sqd,*cmp_sigma_sqd,*sigma_both;
double ssim_sum;
double luminance_comp, contrast_comp, structure_comp, sigma_root;
struct _ssim_int sint;
double l_sum, c_sum, s_sum, l, c, s;
float sigma_ref_sigma_cmp;
assert(!args);
if (args) {
if (!mr)
return INFINITY;
alpha = args->alpha;
beta = args->beta;
gamma = args->gamma;
L = args->L;
K1 = args->K1;
K2 = args->K2;
}
C1 = (K1*L)*(K1*L);
C2 = (K2*L)*(K2*L);
C3 = C2 / 2.0f;
ref_mu = (float*)malloc(w*h*sizeof(float));
cmp_mu = (float*)malloc(w*h*sizeof(float));
ref_sigma_sqd = (float*)malloc(w*h*sizeof(float));
cmp_sigma_sqd = (float*)malloc(w*h*sizeof(float));
sigma_both = (float*)malloc(w*h*sizeof(float));
if (!ref_mu || !cmp_mu || !ref_sigma_sqd || !cmp_sigma_sqd || !sigma_both) {
if (ref_mu) free(ref_mu);
if (cmp_mu) free(cmp_mu);
if (ref_sigma_sqd) free(ref_sigma_sqd);
if (cmp_sigma_sqd) free(cmp_sigma_sqd);
if (sigma_both) free(sigma_both);
return INFINITY;
}
_iqa_convolve(ref, w, h, k, ref_mu, 0, 0);
_iqa_convolve(cmp, w, h, k, cmp_mu, 0, 0);
for (y=0; y<h; ++y) {
offset = y*w;
for (x=0; x<w; ++x, ++offset) {
ref_sigma_sqd[offset] = ref[offset] * ref[offset];
cmp_sigma_sqd[offset] = cmp[offset] * cmp[offset];
sigma_both[offset] = ref[offset] * cmp[offset];
}
}
_iqa_convolve(ref_sigma_sqd, w, h, k, 0, 0, 0);
_iqa_convolve(cmp_sigma_sqd, w, h, k, 0, 0, 0);
_iqa_convolve(sigma_both, w, h, k, 0, &w, &h);
for (y=0; y<h; ++y) {
offset = y*w;
for (x=0; x<w; ++x, ++offset) {
ref_sigma_sqd[offset] -= ref_mu[offset] * ref_mu[offset];
cmp_sigma_sqd[offset] -= cmp_mu[offset] * cmp_mu[offset];
ref_sigma_sqd[offset] = MAX(0.0, ref_sigma_sqd[offset]);
cmp_sigma_sqd[offset] = MAX(0.0, cmp_sigma_sqd[offset]);
sigma_both[offset] -= ref_mu[offset] * cmp_mu[offset];
}
}
ssim_sum = 0.0;
l_sum = 0.0;
c_sum = 0.0;
s_sum = 0.0;
for (y=0; y<h; ++y) {
offset = y*w;
for (x=0; x<w; ++x, ++offset) {
if (!args) {
sigma_ref_sigma_cmp = sqrt(ref_sigma_sqd[offset] * cmp_sigma_sqd[offset]);
l = (2.0 * ref_mu[offset] * cmp_mu[offset] + C1) / (ref_mu[offset]*ref_mu[offset] + cmp_mu[offset]*cmp_mu[offset] + C1);
c = (2.0 * sigma_ref_sigma_cmp + C2) / (ref_sigma_sqd[offset] + cmp_sigma_sqd[offset] + C2);
const float clamped_sigma_both = (sigma_both[offset] < 0.0f &&
sigma_ref_sigma_cmp <= 0.0f) ? 0.0f : sigma_both[offset];
s = (clamped_sigma_both + C3) / (sigma_ref_sigma_cmp + C3);
ssim_sum += l * c * s;
l_sum += l;
c_sum += c;
s_sum += s;
}
else {
if (ref_sigma_sqd[offset] < 0.0f)
ref_sigma_sqd[offset] = 0.0f;
if (cmp_sigma_sqd[offset] < 0.0f)
cmp_sigma_sqd[offset] = 0.0f;
sigma_root = sqrt(ref_sigma_sqd[offset] * cmp_sigma_sqd[offset]);
luminance_comp = _calc_luminance(ref_mu[offset], cmp_mu[offset], C1, alpha);
contrast_comp = _calc_contrast(sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C2, beta);
structure_comp = _calc_structure(sigma_both[offset], sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C3, gamma);
sint.l = luminance_comp;
sint.c = contrast_comp;
sint.s = structure_comp;
if (mr->map(&sint, mr->context))
return INFINITY;
}
}
}
free(ref_mu);
free(cmp_mu);
free(ref_sigma_sqd);
free(cmp_sigma_sqd);
free(sigma_both);
if (!args) {
*l_mean = (float)(l_sum / (double)(w*h));
*c_mean = (float)(c_sum / (double)(w*h));
*s_mean = (float)(s_sum / (double)(w*h));
return (float)(ssim_sum / (double)(w*h));
}
return mr->reduce(w, h, mr->context);
}