Coverage Report

Created: 2025-09-08 07:52

/src/libjxl/lib/jxl/base/matrix_ops.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#ifndef LIB_JXL_BASE_MATRIX_OPS_H_
7
#define LIB_JXL_BASE_MATRIX_OPS_H_
8
9
// 3x3 matrix operations.
10
11
#include <array>
12
#include <cmath>  // abs
13
#include <cstddef>
14
15
#include "lib/jxl/base/status.h"
16
17
namespace jxl {
18
19
typedef std::array<float, 3> Vector3;
20
typedef std::array<double, 3> Vector3d;
21
typedef std::array<Vector3, 3> Matrix3x3;
22
typedef std::array<Vector3d, 3> Matrix3x3d;
23
24
// Computes C = A * B, where A, B, C are 3x3 matrices.
25
template <typename Matrix>
26
4.17M
void Mul3x3Matrix(const Matrix& a, const Matrix& b, Matrix& c) {
27
16.7M
  for (size_t x = 0; x < 3; x++) {
28
12.5M
    alignas(16) Vector3d temp{b[0][x], b[1][x], b[2][x]};  // transpose
29
50.1M
    for (size_t y = 0; y < 3; y++) {
30
37.6M
      c[y][x] = a[y][0] * temp[0] + a[y][1] * temp[1] + a[y][2] * temp[2];
31
37.6M
    }
32
12.5M
  }
33
4.17M
}
34
35
// Computes C = A * B, where A is 3x3 matrix and B is vector.
36
template <typename Matrix, typename Vector>
37
3.15M
void Mul3x3Vector(const Matrix& a, const Vector& b, Vector& c) {
38
12.6M
  for (size_t y = 0; y < 3; y++) {
39
9.47M
    double e = 0;
40
37.9M
    for (size_t x = 0; x < 3; x++) {
41
28.4M
      e += a[y][x] * b[x];
42
28.4M
    }
43
9.47M
    c[y] = e;
44
9.47M
  }
45
3.15M
}
46
47
// Inverts a 3x3 matrix in place.
48
template <typename Matrix>
49
1.01M
Status Inv3x3Matrix(Matrix& matrix) {
50
  // Intermediate computation is done in double precision.
51
1.01M
  Matrix3x3d temp;
52
1.01M
  temp[0][0] = static_cast<double>(matrix[1][1]) * matrix[2][2] -
53
1.01M
               static_cast<double>(matrix[1][2]) * matrix[2][1];
54
1.01M
  temp[0][1] = static_cast<double>(matrix[0][2]) * matrix[2][1] -
55
1.01M
               static_cast<double>(matrix[0][1]) * matrix[2][2];
56
1.01M
  temp[0][2] = static_cast<double>(matrix[0][1]) * matrix[1][2] -
57
1.01M
               static_cast<double>(matrix[0][2]) * matrix[1][1];
58
1.01M
  temp[1][0] = static_cast<double>(matrix[1][2]) * matrix[2][0] -
59
1.01M
               static_cast<double>(matrix[1][0]) * matrix[2][2];
60
1.01M
  temp[1][1] = static_cast<double>(matrix[0][0]) * matrix[2][2] -
61
1.01M
               static_cast<double>(matrix[0][2]) * matrix[2][0];
62
1.01M
  temp[1][2] = static_cast<double>(matrix[0][2]) * matrix[1][0] -
63
1.01M
               static_cast<double>(matrix[0][0]) * matrix[1][2];
64
1.01M
  temp[2][0] = static_cast<double>(matrix[1][0]) * matrix[2][1] -
65
1.01M
               static_cast<double>(matrix[1][1]) * matrix[2][0];
66
1.01M
  temp[2][1] = static_cast<double>(matrix[0][1]) * matrix[2][0] -
67
1.01M
               static_cast<double>(matrix[0][0]) * matrix[2][1];
68
1.01M
  temp[2][2] = static_cast<double>(matrix[0][0]) * matrix[1][1] -
69
1.01M
               static_cast<double>(matrix[0][1]) * matrix[1][0];
70
1.01M
  double det = matrix[0][0] * temp[0][0] + matrix[0][1] * temp[1][0] +
71
1.01M
               matrix[0][2] * temp[2][0];
72
1.01M
  if (std::abs(det) < 1e-10) {
73
0
    return JXL_FAILURE("Matrix determinant is too close to 0");
74
0
  }
75
1.01M
  double idet = 1.0 / det;
76
4.07M
  for (size_t j = 0; j < 3; j++) {
77
12.2M
    for (size_t i = 0; i < 3; i++) {
78
9.16M
      matrix[j][i] = temp[j][i] * idet;
79
9.16M
    }
80
3.05M
  }
81
1.01M
  return true;
82
1.01M
}
jxl::Status jxl::Inv3x3Matrix<std::__1::array<std::__1::array<float, 3ul>, 3ul> >(std::__1::array<std::__1::array<float, 3ul>, 3ul>&)
Line
Count
Source
49
1.01M
Status Inv3x3Matrix(Matrix& matrix) {
50
  // Intermediate computation is done in double precision.
51
1.01M
  Matrix3x3d temp;
52
1.01M
  temp[0][0] = static_cast<double>(matrix[1][1]) * matrix[2][2] -
53
1.01M
               static_cast<double>(matrix[1][2]) * matrix[2][1];
54
1.01M
  temp[0][1] = static_cast<double>(matrix[0][2]) * matrix[2][1] -
55
1.01M
               static_cast<double>(matrix[0][1]) * matrix[2][2];
56
1.01M
  temp[0][2] = static_cast<double>(matrix[0][1]) * matrix[1][2] -
57
1.01M
               static_cast<double>(matrix[0][2]) * matrix[1][1];
58
1.01M
  temp[1][0] = static_cast<double>(matrix[1][2]) * matrix[2][0] -
59
1.01M
               static_cast<double>(matrix[1][0]) * matrix[2][2];
60
1.01M
  temp[1][1] = static_cast<double>(matrix[0][0]) * matrix[2][2] -
61
1.01M
               static_cast<double>(matrix[0][2]) * matrix[2][0];
62
1.01M
  temp[1][2] = static_cast<double>(matrix[0][2]) * matrix[1][0] -
63
1.01M
               static_cast<double>(matrix[0][0]) * matrix[1][2];
64
1.01M
  temp[2][0] = static_cast<double>(matrix[1][0]) * matrix[2][1] -
65
1.01M
               static_cast<double>(matrix[1][1]) * matrix[2][0];
66
1.01M
  temp[2][1] = static_cast<double>(matrix[0][1]) * matrix[2][0] -
67
1.01M
               static_cast<double>(matrix[0][0]) * matrix[2][1];
68
1.01M
  temp[2][2] = static_cast<double>(matrix[0][0]) * matrix[1][1] -
69
1.01M
               static_cast<double>(matrix[0][1]) * matrix[1][0];
70
1.01M
  double det = matrix[0][0] * temp[0][0] + matrix[0][1] * temp[1][0] +
71
1.01M
               matrix[0][2] * temp[2][0];
72
1.01M
  if (std::abs(det) < 1e-10) {
73
0
    return JXL_FAILURE("Matrix determinant is too close to 0");
74
0
  }
75
1.01M
  double idet = 1.0 / det;
76
4.07M
  for (size_t j = 0; j < 3; j++) {
77
12.2M
    for (size_t i = 0; i < 3; i++) {
78
9.16M
      matrix[j][i] = temp[j][i] * idet;
79
9.16M
    }
80
3.05M
  }
81
1.01M
  return true;
82
1.01M
}
Unexecuted instantiation: jxl::Status jxl::Inv3x3Matrix<std::__1::array<std::__1::array<double, 3ul>, 3ul> >(std::__1::array<std::__1::array<double, 3ul>, 3ul>&)
83
84
}  // namespace jxl
85
86
#endif  // LIB_JXL_BASE_MATRIX_OPS_H_