Coverage Report

Created: 2025-06-22 08:04

/src/libjxl/lib/jxl/enc_patch_dictionary.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_ENC_PATCH_DICTIONARY_H_
7
#define LIB_JXL_ENC_PATCH_DICTIONARY_H_
8
9
// Chooses reference patches, and avoids encoding them once per occurrence.
10
11
#include <jxl/cms_interface.h>
12
#include <sys/types.h>
13
14
#include <cstddef>
15
#include <cstdint>
16
#include <cstring>
17
#include <utility>
18
#include <vector>
19
20
#include "lib/jxl/base/compiler_specific.h"
21
#include "lib/jxl/base/data_parallel.h"
22
#include "lib/jxl/base/status.h"
23
#include "lib/jxl/dec_patch_dictionary.h"
24
#include "lib/jxl/enc_bit_writer.h"
25
#include "lib/jxl/enc_cache.h"
26
#include "lib/jxl/enc_params.h"
27
#include "lib/jxl/image.h"
28
29
namespace jxl {
30
31
struct AuxOut;
32
enum class LayerType : uint8_t;
33
34
constexpr size_t kMaxPatchSize = 32;
35
36
struct QuantizedPatch {
37
  size_t xsize;
38
  size_t ysize;
39
0
  QuantizedPatch() {
40
0
    for (size_t i = 0; i < 3; i++) {
41
0
      pixels[i].resize(kMaxPatchSize * kMaxPatchSize);
42
0
      fpixels[i].resize(kMaxPatchSize * kMaxPatchSize);
43
0
    }
44
0
  }
45
  std::vector<int8_t> pixels[3] = {};
46
  // Not compared. Used only to retrieve original pixels to construct the
47
  // reference image.
48
  std::vector<float> fpixels[3] = {};
49
0
  bool operator==(const QuantizedPatch& other) const {
50
0
    if (xsize != other.xsize) return false;
51
0
    if (ysize != other.ysize) return false;
52
0
    for (size_t c = 0; c < 3; c++) {
53
0
      if (memcmp(pixels[c].data(), other.pixels[c].data(),
54
0
                 sizeof(int8_t) * xsize * ysize) != 0)
55
0
        return false;
56
0
    }
57
0
    return true;
58
0
  }
59
60
0
  bool operator<(const QuantizedPatch& other) const {
61
0
    if (xsize != other.xsize) return xsize < other.xsize;
62
0
    if (ysize != other.ysize) return ysize < other.ysize;
63
0
    for (size_t c = 0; c < 3; c++) {
64
0
      int cmp = memcmp(pixels[c].data(), other.pixels[c].data(),
65
0
                       sizeof(int8_t) * xsize * ysize);
66
0
      if (cmp > 0) return false;
67
0
      if (cmp < 0) return true;
68
0
    }
69
0
    return false;
70
0
  }
71
};
72
73
// Pair (patch, vector of occurrences).
74
using PatchInfo =
75
    std::pair<QuantizedPatch, std::vector<std::pair<uint32_t, uint32_t>>>;
76
77
// Friend class of PatchDictionary.
78
class PatchDictionaryEncoder {
79
 public:
80
  // Only call if HasAny().
81
  static Status Encode(const PatchDictionary& pdic, BitWriter* writer,
82
                       LayerType layer, AuxOut* aux_out);
83
84
  static void SetPositions(PatchDictionary* pdic,
85
                           std::vector<PatchPosition> positions,
86
                           std::vector<PatchReferencePosition> ref_positions,
87
                           std::vector<PatchBlending> blendings,
88
0
                           size_t blendings_stride) {
89
0
    pdic->positions_ = std::move(positions);
90
0
    pdic->ref_positions_ = std::move(ref_positions);
91
0
    pdic->blendings_ = std::move(blendings);
92
0
    pdic->blendings_stride_ = blendings_stride;
93
0
    pdic->ComputePatchTree();
94
0
  }
95
96
  static Status SubtractFrom(const PatchDictionary& pdic, Image3F* opsin);
97
};
98
99
Status FindBestPatchDictionary(const Image3F& opsin,
100
                               PassesEncoderState* JXL_RESTRICT state,
101
                               const JxlCmsInterface& cms, ThreadPool* pool,
102
                               AuxOut* aux_out, bool is_xyb = true);
103
104
Status RoundtripPatchFrame(Image3F* reference_frame,
105
                           PassesEncoderState* JXL_RESTRICT state, int idx,
106
                           CompressParams& cparams, const JxlCmsInterface& cms,
107
                           ThreadPool* pool, AuxOut* aux_out, bool subtract);
108
109
}  // namespace jxl
110
111
#endif  // LIB_JXL_ENC_PATCH_DICTIONARY_H_