ssim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2011, Tom Distler (http://tdistler.com)
  3. * All rights reserved.
  4. *
  5. * The BSD License
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * - Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * - Neither the name of the tdistler.com nor the names of its contributors may
  18. * be used to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "iqa.h"
  34. #include "convolve.h"
  35. #include "decimate.h"
  36. #include "math_utils.h"
  37. #include "ssim.h"
  38. #include <stdlib.h>
  39. #include <math.h>
  40. /* Forward declarations. */
  41. IQA_INLINE static double _calc_luminance(float, float, float, float);
  42. IQA_INLINE static double _calc_contrast(double, float, float, float, float);
  43. IQA_INLINE static double _calc_structure(float, double, float, float, float, float);
  44. static int _ssim_map(const struct _ssim_int *, void *);
  45. static float _ssim_reduce(int, int, void *);
  46. /*
  47. * SSIM(x,y)=(2*ux*uy + C1)*(2sxy + C2) / (ux^2 + uy^2 + C1)*(sx^2 + sy^2 + C2)
  48. * where,
  49. * ux = SUM(w*x)
  50. * sx = (SUM(w*(x-ux)^2)^0.5
  51. * sxy = SUM(w*(x-ux)*(y-uy))
  52. *
  53. * Returns mean SSIM. MSSIM(X,Y) = 1/M * SUM(SSIM(x,y))
  54. */
  55. float iqa_ssim(const unsigned char *ref, const unsigned char *cmp, int w, int h, int stride,
  56. int gaussian, const struct iqa_ssim_args *args)
  57. {
  58. int scale;
  59. int x,y,src_offset,offset;
  60. float *ref_f,*cmp_f;
  61. struct _kernel low_pass;
  62. struct _kernel window;
  63. float result;
  64. double ssim_sum=0.0;
  65. struct _map_reduce mr;
  66. /* Initialize algorithm parameters */
  67. scale = _max( 1, _round( (float)_min(w,h) / 256.0f ) );
  68. if (args) {
  69. if(args->f)
  70. scale = args->f;
  71. mr.map = _ssim_map;
  72. mr.reduce = _ssim_reduce;
  73. mr.context = (void*)&ssim_sum;
  74. }
  75. window.kernel = (float*)g_square_window;
  76. window.w = window.h = SQUARE_LEN;
  77. window.normalized = 1;
  78. window.bnd_opt = KBND_SYMMETRIC;
  79. if (gaussian) {
  80. window.kernel = (float*)g_gaussian_window;
  81. window.w = window.h = GAUSSIAN_LEN;
  82. }
  83. /* Convert image values to floats. Forcing stride = width. */
  84. ref_f = (float*)malloc(w*h*sizeof(float));
  85. cmp_f = (float*)malloc(w*h*sizeof(float));
  86. if (!ref_f || !cmp_f) {
  87. if (ref_f) free(ref_f);
  88. if (cmp_f) free(cmp_f);
  89. return INFINITY;
  90. }
  91. for (y=0; y<h; ++y) {
  92. src_offset = y*stride;
  93. offset = y*w;
  94. for (x=0; x<w; ++x, ++offset, ++src_offset) {
  95. ref_f[offset] = (float)ref[src_offset];
  96. cmp_f[offset] = (float)cmp[src_offset];
  97. }
  98. }
  99. /* Scale the images down if required */
  100. if (scale > 1) {
  101. /* Generate simple low-pass filter */
  102. low_pass.kernel = (float*)malloc(scale*scale*sizeof(float));
  103. if (!low_pass.kernel) {
  104. free(ref_f);
  105. free(cmp_f);
  106. return INFINITY;
  107. }
  108. low_pass.w = low_pass.h = scale;
  109. low_pass.normalized = 0;
  110. low_pass.bnd_opt = KBND_SYMMETRIC;
  111. for (offset=0; offset<scale*scale; ++offset)
  112. low_pass.kernel[offset] = 1.0f/(scale*scale);
  113. /* Resample */
  114. if (_iqa_decimate(ref_f, w, h, scale, &low_pass, 0, 0, 0) ||
  115. _iqa_decimate(cmp_f, w, h, scale, &low_pass, 0, &w, &h)) { /* Update w/h */
  116. free(ref_f);
  117. free(cmp_f);
  118. free(low_pass.kernel);
  119. return INFINITY;
  120. }
  121. free(low_pass.kernel);
  122. }
  123. result = _iqa_ssim(ref_f, cmp_f, w, h, &window, &mr, args);
  124. free(ref_f);
  125. free(cmp_f);
  126. return result;
  127. }
  128. /* _iqa_ssim */
  129. 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)
  130. {
  131. float alpha=1.0f, beta=1.0f, gamma=1.0f;
  132. int L=255;
  133. float K1=0.01f, K2=0.03f;
  134. float C1,C2,C3;
  135. int x,y,offset;
  136. float *ref_mu,*cmp_mu,*ref_sigma_sqd,*cmp_sigma_sqd,*sigma_both;
  137. double ssim_sum, numerator, denominator;
  138. double luminance_comp, contrast_comp, structure_comp, sigma_root;
  139. struct _ssim_int sint;
  140. /* Initialize algorithm parameters */
  141. if (args) {
  142. if (!mr)
  143. return INFINITY;
  144. alpha = args->alpha;
  145. beta = args->beta;
  146. gamma = args->gamma;
  147. L = args->L;
  148. K1 = args->K1;
  149. K2 = args->K2;
  150. }
  151. C1 = (K1*L)*(K1*L);
  152. C2 = (K2*L)*(K2*L);
  153. C3 = C2 / 2.0f;
  154. ref_mu = (float*)malloc(w*h*sizeof(float));
  155. cmp_mu = (float*)malloc(w*h*sizeof(float));
  156. ref_sigma_sqd = (float*)malloc(w*h*sizeof(float));
  157. cmp_sigma_sqd = (float*)malloc(w*h*sizeof(float));
  158. sigma_both = (float*)malloc(w*h*sizeof(float));
  159. if (!ref_mu || !cmp_mu || !ref_sigma_sqd || !cmp_sigma_sqd || !sigma_both) {
  160. if (ref_mu) free(ref_mu);
  161. if (cmp_mu) free(cmp_mu);
  162. if (ref_sigma_sqd) free(ref_sigma_sqd);
  163. if (cmp_sigma_sqd) free(cmp_sigma_sqd);
  164. if (sigma_both) free(sigma_both);
  165. return INFINITY;
  166. }
  167. /* Calculate mean */
  168. _iqa_convolve(ref, w, h, k, ref_mu, 0, 0);
  169. _iqa_convolve(cmp, w, h, k, cmp_mu, 0, 0);
  170. for (y=0; y<h; ++y) {
  171. offset = y*w;
  172. for (x=0; x<w; ++x, ++offset) {
  173. ref_sigma_sqd[offset] = ref[offset] * ref[offset];
  174. cmp_sigma_sqd[offset] = cmp[offset] * cmp[offset];
  175. sigma_both[offset] = ref[offset] * cmp[offset];
  176. }
  177. }
  178. /* Calculate sigma */
  179. _iqa_convolve(ref_sigma_sqd, w, h, k, 0, 0, 0);
  180. _iqa_convolve(cmp_sigma_sqd, w, h, k, 0, 0, 0);
  181. _iqa_convolve(sigma_both, w, h, k, 0, &w, &h); /* Update the width and height */
  182. /* The convolution results are smaller by the kernel width and height */
  183. for (y=0; y<h; ++y) {
  184. offset = y*w;
  185. for (x=0; x<w; ++x, ++offset) {
  186. ref_sigma_sqd[offset] -= ref_mu[offset] * ref_mu[offset];
  187. cmp_sigma_sqd[offset] -= cmp_mu[offset] * cmp_mu[offset];
  188. sigma_both[offset] -= ref_mu[offset] * cmp_mu[offset];
  189. }
  190. }
  191. ssim_sum = 0.0;
  192. for (y=0; y<h; ++y) {
  193. offset = y*w;
  194. for (x=0; x<w; ++x, ++offset) {
  195. if (!args) {
  196. /* The default case */
  197. numerator = (2.0 * ref_mu[offset] * cmp_mu[offset] + C1) * (2.0 * sigma_both[offset] + C2);
  198. denominator = (ref_mu[offset]*ref_mu[offset] + cmp_mu[offset]*cmp_mu[offset] + C1) *
  199. (ref_sigma_sqd[offset] + cmp_sigma_sqd[offset] + C2);
  200. ssim_sum += numerator / denominator;
  201. }
  202. else {
  203. /* User tweaked alpha, beta, or gamma */
  204. /* passing a negative number to sqrt() cause a domain error */
  205. if (ref_sigma_sqd[offset] < 0.0f)
  206. ref_sigma_sqd[offset] = 0.0f;
  207. if (cmp_sigma_sqd[offset] < 0.0f)
  208. cmp_sigma_sqd[offset] = 0.0f;
  209. sigma_root = sqrt(ref_sigma_sqd[offset] * cmp_sigma_sqd[offset]);
  210. luminance_comp = _calc_luminance(ref_mu[offset], cmp_mu[offset], C1, alpha);
  211. contrast_comp = _calc_contrast(sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C2, beta);
  212. structure_comp = _calc_structure(sigma_both[offset], sigma_root, ref_sigma_sqd[offset], cmp_sigma_sqd[offset], C3, gamma);
  213. sint.l = luminance_comp;
  214. sint.c = contrast_comp;
  215. sint.s = structure_comp;
  216. if (mr->map(&sint, mr->context))
  217. return INFINITY;
  218. }
  219. }
  220. }
  221. free(ref_mu);
  222. free(cmp_mu);
  223. free(ref_sigma_sqd);
  224. free(cmp_sigma_sqd);
  225. free(sigma_both);
  226. if (!args)
  227. return (float)(ssim_sum / (double)(w*h));
  228. return mr->reduce(w, h, mr->context);
  229. }
  230. /* _ssim_map */
  231. int _ssim_map(const struct _ssim_int *si, void *ctx)
  232. {
  233. double *ssim_sum = (double*)ctx;
  234. *ssim_sum += si->l * si->c * si->s;
  235. return 0;
  236. }
  237. /* _ssim_reduce */
  238. float _ssim_reduce(int w, int h, void *ctx)
  239. {
  240. double *ssim_sum = (double*)ctx;
  241. return (float)(*ssim_sum / (double)(w*h));
  242. }
  243. /* _calc_luminance */
  244. IQA_INLINE static double _calc_luminance(float mu1, float mu2, float C1, float alpha)
  245. {
  246. double result;
  247. float sign;
  248. /* For MS-SSIM* */
  249. if (C1 == 0 && mu1*mu1 == 0 && mu2*mu2 == 0)
  250. return 1.0;
  251. result = (2.0 * mu1 * mu2 + C1) / (mu1*mu1 + mu2*mu2 + C1);
  252. if (alpha == 1.0f)
  253. return result;
  254. sign = result < 0.0 ? -1.0f : 1.0f;
  255. return sign * pow(fabs(result),(double)alpha);
  256. }
  257. /* _calc_contrast */
  258. IQA_INLINE static double _calc_contrast(double sigma_comb_12, float sigma1_sqd, float sigma2_sqd, float C2, float beta)
  259. {
  260. double result;
  261. float sign;
  262. /* For MS-SSIM* */
  263. if (C2 == 0 && sigma1_sqd + sigma2_sqd == 0)
  264. return 1.0;
  265. result = (2.0 * sigma_comb_12 + C2) / (sigma1_sqd + sigma2_sqd + C2);
  266. if (beta == 1.0f)
  267. return result;
  268. sign = result < 0.0 ? -1.0f : 1.0f;
  269. return sign * pow(fabs(result),(double)beta);
  270. }
  271. /* _calc_structure */
  272. IQA_INLINE static double _calc_structure(float sigma_12, double sigma_comb_12, float sigma1, float sigma2, float C3, float gamma)
  273. {
  274. double result;
  275. float sign;
  276. /* For MS-SSIM* */
  277. if (C3 == 0 && sigma_comb_12 == 0) {
  278. if (sigma1 == 0 && sigma2 == 0)
  279. return 1.0;
  280. else if (sigma1 == 0 || sigma2 == 0)
  281. return 0.0;
  282. }
  283. result = (sigma_12 + C3) / (sigma_comb_12 + C3);
  284. if (gamma == 1.0f)
  285. return result;
  286. sign = result < 0.0 ? -1.0f : 1.0f;
  287. return sign * pow(fabs(result),(double)gamma);
  288. }