Coverage Report

Created: 2025-12-31 07:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/modular/encoding/encoding.h
Line
Count
Source
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_MODULAR_ENCODING_ENCODING_H_
7
#define LIB_JXL_MODULAR_ENCODING_ENCODING_H_
8
9
#include <array>
10
#include <cstddef>
11
#include <cstdint>
12
#include <limits>
13
#include <vector>
14
15
#include "lib/jxl/base/compiler_specific.h"
16
#include "lib/jxl/base/status.h"
17
#include "lib/jxl/field_encodings.h"
18
#include "lib/jxl/modular/encoding/context_predict.h"
19
#include "lib/jxl/modular/encoding/dec_ma.h"
20
#include "lib/jxl/modular/modular_image.h"
21
#include "lib/jxl/modular/options.h"
22
#include "lib/jxl/modular/transform/transform.h"
23
24
namespace jxl {
25
26
struct ANSCode;
27
class BitReader;
28
29
// Valid range of properties for using lookup tables instead of trees.
30
constexpr int32_t kPropRangeFast = 512 << 4;
31
32
struct GroupHeader : public Fields {
33
  GroupHeader();
34
35
  JXL_FIELDS_NAME(GroupHeader)
36
37
580k
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
38
580k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &use_global_tree));
39
575k
    JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&wp_header));
40
574k
    uint32_t num_transforms = static_cast<uint32_t>(transforms.size());
41
574k
    JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Val(0), Val(1), BitsOffset(4, 2),
42
574k
                                           BitsOffset(8, 18), 0,
43
574k
                                           &num_transforms));
44
574k
    if (visitor->IsReading()) transforms.resize(num_transforms);
45
611k
    for (size_t i = 0; i < num_transforms; i++) {
46
44.4k
      JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&transforms[i]));
47
44.4k
    }
48
567k
    return true;
49
574k
  }
50
51
  bool use_global_tree;
52
  weighted::Header wp_header;
53
54
  std::vector<Transform> transforms;
55
};
56
57
FlatTree FilterTree(const Tree &global_tree,
58
                    std::array<pixel_type, kNumStaticProperties> &static_props,
59
                    size_t *num_props, bool *use_wp, bool *wp_only,
60
                    bool *gradient_only);
61
62
template <typename T, bool HAS_OFFSETS, bool HAS_MULTIPLIERS>
63
struct TreeLut {
64
  std::array<T, 2 * kPropRangeFast> context_lookup;
65
  std::array<int8_t, HAS_OFFSETS ? (2 * kPropRangeFast) : 0> offsets;
66
  std::array<int8_t, HAS_MULTIPLIERS ? (2 * kPropRangeFast) : 0> multipliers;
67
};
68
69
template <typename T, bool HAS_OFFSETS, bool HAS_MULTIPLIERS>
70
bool TreeToLookupTable(const FlatTree &tree,
71
43.3k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
43.3k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
43.3k
    int begin, end;
76
43.3k
    size_t pos;
77
43.3k
  };
78
43.3k
  std::vector<TreeRange> ranges;
79
43.3k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
263k
  while (!ranges.empty()) {
81
225k
    TreeRange cur = ranges.back();
82
225k
    ranges.pop_back();
83
225k
    if (cur.begin < -kPropRangeFast - 1 || cur.begin >= kPropRangeFast - 1 ||
84
225k
        cur.end > kPropRangeFast - 1) {
85
      // Tree is outside the allowed range, exit.
86
0
      return false;
87
0
    }
88
225k
    auto &node = tree[cur.pos];
89
    // Leaf.
90
225k
    if (node.property0 == -1) {
91
155k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
155k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
179
        return false;
94
179
      }
95
155k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
155k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
1.22k
        return false;
98
1.22k
      }
99
153k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
2.83k
        return false;
101
2.83k
      }
102
151k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
928
        return false;
104
928
      }
105
625M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
625M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
625M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
625M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
625M
      }
114
150k
      continue;
115
151k
    }
116
    // > side of top node.
117
70.0k
    if (node.properties[0] >= kNumStaticProperties) {
118
18.8k
      ranges.push_back(TreeRange({node.splitvals[0], cur.end, node.childID}));
119
18.8k
      ranges.push_back(
120
18.8k
          TreeRange({node.splitval0, node.splitvals[0], node.childID + 1}));
121
51.2k
    } else {
122
51.2k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
51.2k
    }
124
    // <= side
125
70.0k
    if (node.properties[1] >= kNumStaticProperties) {
126
23.2k
      ranges.push_back(
127
23.2k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
23.2k
      ranges.push_back(
129
23.2k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
46.8k
    } else {
131
46.8k
      ranges.push_back(
132
46.8k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
46.8k
    }
134
70.0k
  }
135
38.1k
  return true;
136
43.3k
}
bool jxl::TreeToLookupTable<unsigned char, false, false>(std::__1::vector<jxl::FlatDecisionNode, std::__1::allocator<jxl::FlatDecisionNode> > const&, jxl::TreeLut<unsigned char, false, false>&)
Line
Count
Source
71
22.0k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
22.0k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
22.0k
    int begin, end;
76
22.0k
    size_t pos;
77
22.0k
  };
78
22.0k
  std::vector<TreeRange> ranges;
79
22.0k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
91.6k
  while (!ranges.empty()) {
81
74.7k
    TreeRange cur = ranges.back();
82
74.7k
    ranges.pop_back();
83
74.7k
    if (cur.begin < -kPropRangeFast - 1 || cur.begin >= kPropRangeFast - 1 ||
84
74.7k
        cur.end > kPropRangeFast - 1) {
85
      // Tree is outside the allowed range, exit.
86
0
      return false;
87
0
    }
88
74.7k
    auto &node = tree[cur.pos];
89
    // Leaf.
90
74.7k
    if (node.property0 == -1) {
91
53.6k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
53.6k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
179
        return false;
94
179
      }
95
53.4k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
53.4k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
1.22k
        return false;
98
1.22k
      }
99
52.2k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
2.83k
        return false;
101
2.83k
      }
102
49.4k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
928
        return false;
104
928
      }
105
277M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
277M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
277M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
277M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
277M
      }
114
48.5k
      continue;
115
49.4k
    }
116
    // > side of top node.
117
21.0k
    if (node.properties[0] >= kNumStaticProperties) {
118
4.92k
      ranges.push_back(TreeRange({node.splitvals[0], cur.end, node.childID}));
119
4.92k
      ranges.push_back(
120
4.92k
          TreeRange({node.splitval0, node.splitvals[0], node.childID + 1}));
121
16.1k
    } else {
122
16.1k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
16.1k
    }
124
    // <= side
125
21.0k
    if (node.properties[1] >= kNumStaticProperties) {
126
5.66k
      ranges.push_back(
127
5.66k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
5.66k
      ranges.push_back(
129
5.66k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
15.4k
    } else {
131
15.4k
      ranges.push_back(
132
15.4k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
15.4k
    }
134
21.0k
  }
135
16.9k
  return true;
136
22.0k
}
bool jxl::TreeToLookupTable<unsigned short, false, false>(std::__1::vector<jxl::FlatDecisionNode, std::__1::allocator<jxl::FlatDecisionNode> > const&, jxl::TreeLut<unsigned short, false, false>&)
Line
Count
Source
71
21.2k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
21.2k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
21.2k
    int begin, end;
76
21.2k
    size_t pos;
77
21.2k
  };
78
21.2k
  std::vector<TreeRange> ranges;
79
21.2k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
171k
  while (!ranges.empty()) {
81
150k
    TreeRange cur = ranges.back();
82
150k
    ranges.pop_back();
83
150k
    if (cur.begin < -kPropRangeFast - 1 || cur.begin >= kPropRangeFast - 1 ||
84
150k
        cur.end > kPropRangeFast - 1) {
85
      // Tree is outside the allowed range, exit.
86
0
      return false;
87
0
    }
88
150k
    auto &node = tree[cur.pos];
89
    // Leaf.
90
150k
    if (node.property0 == -1) {
91
101k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
101k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
0
        return false;
94
0
      }
95
101k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
101k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
0
        return false;
98
0
      }
99
101k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
0
        return false;
101
0
      }
102
101k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
0
        return false;
104
0
      }
105
348M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
348M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
348M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
348M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
348M
      }
114
101k
      continue;
115
101k
    }
116
    // > side of top node.
117
49.0k
    if (node.properties[0] >= kNumStaticProperties) {
118
13.8k
      ranges.push_back(TreeRange({node.splitvals[0], cur.end, node.childID}));
119
13.8k
      ranges.push_back(
120
13.8k
          TreeRange({node.splitval0, node.splitvals[0], node.childID + 1}));
121
35.1k
    } else {
122
35.1k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
35.1k
    }
124
    // <= side
125
49.0k
    if (node.properties[1] >= kNumStaticProperties) {
126
17.5k
      ranges.push_back(
127
17.5k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
17.5k
      ranges.push_back(
129
17.5k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
31.4k
    } else {
131
31.4k
      ranges.push_back(
132
31.4k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
31.4k
    }
134
49.0k
  }
135
21.2k
  return true;
136
21.2k
}
137
// TODO(veluca): make cleaner interfaces.
138
139
Status ValidateChannelDimensions(const Image &image,
140
                                 const ModularOptions &options);
141
142
Status ModularGenericDecompress(BitReader *br, Image &image,
143
                                GroupHeader *header, size_t group_id,
144
                                ModularOptions *options,
145
                                bool undo_transforms = true,
146
                                const Tree *tree = nullptr,
147
                                const ANSCode *code = nullptr,
148
                                const std::vector<uint8_t> *ctx_map = nullptr,
149
                                bool allow_truncated_group = false);
150
}  // namespace jxl
151
152
#endif  // LIB_JXL_MODULAR_ENCODING_ENCODING_H_