Coverage Report

Created: 2026-04-01 07:49

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
808k
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
38
808k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &use_global_tree));
39
800k
    JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&wp_header));
40
798k
    uint32_t num_transforms = static_cast<uint32_t>(transforms.size());
41
798k
    JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Val(0), Val(1), BitsOffset(4, 2),
42
798k
                                           BitsOffset(8, 18), 0,
43
798k
                                           &num_transforms));
44
797k
    if (visitor->IsReading()) transforms.resize(num_transforms);
45
854k
    for (size_t i = 0; i < num_transforms; i++) {
46
65.6k
      JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&transforms[i]));
47
65.6k
    }
48
788k
    return true;
49
797k
  }
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
47.5k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
47.5k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
47.5k
    int begin, end;
76
47.5k
    size_t pos;
77
47.5k
  };
78
47.5k
  std::vector<TreeRange> ranges;
79
47.5k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
268k
  while (!ranges.empty()) {
81
226k
    TreeRange cur = ranges.back();
82
226k
    ranges.pop_back();
83
226k
    if (cur.begin < -kPropRangeFast - 1 || cur.begin >= kPropRangeFast - 1 ||
84
226k
        cur.end > kPropRangeFast - 1) {
85
      // Tree is outside the allowed range, exit.
86
0
      return false;
87
0
    }
88
226k
    auto &node = tree[cur.pos];
89
    // Leaf.
90
226k
    if (node.property0 == -1) {
91
157k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
157k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
169
        return false;
94
169
      }
95
157k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
157k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
1.27k
        return false;
98
1.27k
      }
99
156k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
3.58k
        return false;
101
3.58k
      }
102
152k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
919
        return false;
104
919
      }
105
681M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
681M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
681M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
681M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
681M
      }
114
151k
      continue;
115
152k
    }
116
    // > side of top node.
117
68.8k
    if (node.properties[0] >= kNumStaticProperties) {
118
18.4k
      ranges.push_back(TreeRange({node.splitvals[0], cur.end, node.childID}));
119
18.4k
      ranges.push_back(
120
18.4k
          TreeRange({node.splitval0, node.splitvals[0], node.childID + 1}));
121
50.4k
    } else {
122
50.4k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
50.4k
    }
124
    // <= side
125
68.8k
    if (node.properties[1] >= kNumStaticProperties) {
126
22.8k
      ranges.push_back(
127
22.8k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
22.8k
      ranges.push_back(
129
22.8k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
46.0k
    } else {
131
46.0k
      ranges.push_back(
132
46.0k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
46.0k
    }
134
68.8k
  }
135
41.5k
  return true;
136
47.5k
}
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
24.7k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
24.7k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
24.7k
    int begin, end;
76
24.7k
    size_t pos;
77
24.7k
  };
78
24.7k
  std::vector<TreeRange> ranges;
79
24.7k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
94.5k
  while (!ranges.empty()) {
81
75.7k
    TreeRange cur = ranges.back();
82
75.7k
    ranges.pop_back();
83
75.7k
    if (cur.begin < -kPropRangeFast - 1 || cur.begin >= kPropRangeFast - 1 ||
84
75.7k
        cur.end > kPropRangeFast - 1) {
85
      // Tree is outside the allowed range, exit.
86
0
      return false;
87
0
    }
88
75.7k
    auto &node = tree[cur.pos];
89
    // Leaf.
90
75.7k
    if (node.property0 == -1) {
91
55.2k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
55.1k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
169
        return false;
94
169
      }
95
55.0k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
55.0k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
1.27k
        return false;
98
1.27k
      }
99
53.7k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
3.58k
        return false;
101
3.58k
      }
102
50.1k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
919
        return false;
104
919
      }
105
308M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
308M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
308M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
308M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
308M
      }
114
49.2k
      continue;
115
50.1k
    }
116
    // > side of top node.
117
20.4k
    if (node.properties[0] >= kNumStaticProperties) {
118
4.61k
      ranges.push_back(TreeRange({node.splitvals[0], cur.end, node.childID}));
119
4.61k
      ranges.push_back(
120
4.61k
          TreeRange({node.splitval0, node.splitvals[0], node.childID + 1}));
121
15.8k
    } else {
122
15.8k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
15.8k
    }
124
    // <= side
125
20.4k
    if (node.properties[1] >= kNumStaticProperties) {
126
5.39k
      ranges.push_back(
127
5.39k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
5.39k
      ranges.push_back(
129
5.39k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
15.0k
    } else {
131
15.0k
      ranges.push_back(
132
15.0k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
15.0k
    }
134
20.4k
  }
135
18.8k
  return true;
136
24.7k
}
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
22.7k
                       TreeLut<T, HAS_OFFSETS, HAS_MULTIPLIERS> &lut) {
72
22.7k
  struct TreeRange {
73
    // Begin *excluded*, end *included*. This works best with > vs <= decision
74
    // nodes.
75
22.7k
    int begin, end;
76
22.7k
    size_t pos;
77
22.7k
  };
78
22.7k
  std::vector<TreeRange> ranges;
79
22.7k
  ranges.push_back(TreeRange{-kPropRangeFast - 1, kPropRangeFast - 1, 0});
80
173k
  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
102k
      if (node.predictor_offset < std::numeric_limits<int8_t>::min() ||
92
102k
          node.predictor_offset > std::numeric_limits<int8_t>::max()) {
93
0
        return false;
94
0
      }
95
102k
      if (node.multiplier < std::numeric_limits<int8_t>::min() ||
96
102k
          node.multiplier > std::numeric_limits<int8_t>::max()) {
97
0
        return false;
98
0
      }
99
102k
      if (!HAS_MULTIPLIERS && node.multiplier != 1) {
100
0
        return false;
101
0
      }
102
102k
      if (!HAS_OFFSETS && node.predictor_offset != 0) {
103
0
        return false;
104
0
      }
105
372M
      for (int i = cur.begin + 1; i < cur.end + 1; i++) {
106
372M
        lut.context_lookup[i + kPropRangeFast] = node.childID;
107
372M
        if (HAS_MULTIPLIERS) {
108
0
          lut.multipliers[i + kPropRangeFast] = node.multiplier;
109
0
        }
110
372M
        if (HAS_OFFSETS) {
111
0
          lut.offsets[i + kPropRangeFast] = node.predictor_offset;
112
0
        }
113
372M
      }
114
102k
      continue;
115
102k
    }
116
    // > side of top node.
117
48.4k
    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
34.5k
    } else {
122
34.5k
      ranges.push_back(TreeRange({node.splitval0, cur.end, node.childID}));
123
34.5k
    }
124
    // <= side
125
48.4k
    if (node.properties[1] >= kNumStaticProperties) {
126
17.4k
      ranges.push_back(
127
17.4k
          TreeRange({node.splitvals[1], node.splitval0, node.childID + 2}));
128
17.4k
      ranges.push_back(
129
17.4k
          TreeRange({cur.begin, node.splitvals[1], node.childID + 3}));
130
30.9k
    } else {
131
30.9k
      ranges.push_back(
132
30.9k
          TreeRange({cur.begin, node.splitval0, node.childID + 2}));
133
30.9k
    }
134
48.4k
  }
135
22.7k
  return true;
136
22.7k
}
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_