stats.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FastStats = void 0;
  6. exports.ssim = ssim;
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the 'License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. // Image channel has a 8-bit depth.
  23. const DYNAMIC_RANGE = 2 ** 8 - 1;
  24. function ssim(stats, x1, y1, x2, y2) {
  25. const mean1 = stats.meanC1(x1, y1, x2, y2);
  26. const mean2 = stats.meanC2(x1, y1, x2, y2);
  27. const var1 = stats.varianceC1(x1, y1, x2, y2);
  28. const var2 = stats.varianceC2(x1, y1, x2, y2);
  29. const cov = stats.covariance(x1, y1, x2, y2);
  30. const c1 = (0.01 * DYNAMIC_RANGE) ** 2;
  31. const c2 = (0.03 * DYNAMIC_RANGE) ** 2;
  32. return (2 * mean1 * mean2 + c1) * (2 * cov + c2) / (mean1 ** 2 + mean2 ** 2 + c1) / (var1 + var2 + c2);
  33. }
  34. class FastStats {
  35. constructor(c1, c2) {
  36. this.c1 = void 0;
  37. this.c2 = void 0;
  38. this._partialSumC1 = void 0;
  39. this._partialSumC2 = void 0;
  40. this._partialSumMult = void 0;
  41. this._partialSumSq1 = void 0;
  42. this._partialSumSq2 = void 0;
  43. this.c1 = c1;
  44. this.c2 = c2;
  45. const {
  46. width,
  47. height
  48. } = c1;
  49. this._partialSumC1 = new Array(width * height);
  50. this._partialSumC2 = new Array(width * height);
  51. this._partialSumSq1 = new Array(width * height);
  52. this._partialSumSq2 = new Array(width * height);
  53. this._partialSumMult = new Array(width * height);
  54. const recalc = (mx, idx, initial, x, y) => {
  55. mx[idx] = initial;
  56. if (y > 0) mx[idx] += mx[(y - 1) * width + x];
  57. if (x > 0) mx[idx] += mx[y * width + x - 1];
  58. if (x > 0 && y > 0) mx[idx] -= mx[(y - 1) * width + x - 1];
  59. };
  60. for (let y = 0; y < height; ++y) {
  61. for (let x = 0; x < width; ++x) {
  62. const idx = y * width + x;
  63. recalc(this._partialSumC1, idx, this.c1.data[idx], x, y);
  64. recalc(this._partialSumC2, idx, this.c2.data[idx], x, y);
  65. recalc(this._partialSumSq1, idx, this.c1.data[idx] * this.c1.data[idx], x, y);
  66. recalc(this._partialSumSq2, idx, this.c2.data[idx] * this.c2.data[idx], x, y);
  67. recalc(this._partialSumMult, idx, this.c1.data[idx] * this.c2.data[idx], x, y);
  68. }
  69. }
  70. }
  71. _sum(partialSum, x1, y1, x2, y2) {
  72. const width = this.c1.width;
  73. let result = partialSum[y2 * width + x2];
  74. if (y1 > 0) result -= partialSum[(y1 - 1) * width + x2];
  75. if (x1 > 0) result -= partialSum[y2 * width + x1 - 1];
  76. if (x1 > 0 && y1 > 0) result += partialSum[(y1 - 1) * width + x1 - 1];
  77. return result;
  78. }
  79. meanC1(x1, y1, x2, y2) {
  80. const N = (y2 - y1 + 1) * (x2 - x1 + 1);
  81. return this._sum(this._partialSumC1, x1, y1, x2, y2) / N;
  82. }
  83. meanC2(x1, y1, x2, y2) {
  84. const N = (y2 - y1 + 1) * (x2 - x1 + 1);
  85. return this._sum(this._partialSumC2, x1, y1, x2, y2) / N;
  86. }
  87. varianceC1(x1, y1, x2, y2) {
  88. const N = (y2 - y1 + 1) * (x2 - x1 + 1);
  89. return (this._sum(this._partialSumSq1, x1, y1, x2, y2) - this._sum(this._partialSumC1, x1, y1, x2, y2) ** 2 / N) / N;
  90. }
  91. varianceC2(x1, y1, x2, y2) {
  92. const N = (y2 - y1 + 1) * (x2 - x1 + 1);
  93. return (this._sum(this._partialSumSq2, x1, y1, x2, y2) - this._sum(this._partialSumC2, x1, y1, x2, y2) ** 2 / N) / N;
  94. }
  95. covariance(x1, y1, x2, y2) {
  96. const N = (y2 - y1 + 1) * (x2 - x1 + 1);
  97. return (this._sum(this._partialSumMult, x1, y1, x2, y2) - this._sum(this._partialSumC1, x1, y1, x2, y2) * this._sum(this._partialSumC2, x1, y1, x2, y2) / N) / N;
  98. }
  99. }
  100. exports.FastStats = FastStats;