Coverage Report

Created: 2025-06-16 07:00

/src/libjxl/lib/jxl/frame_dimensions.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_FRAME_DIMENSIONS_H_
7
#define LIB_JXL_FRAME_DIMENSIONS_H_
8
9
// FrameDimensions struct, block and group dimensions constants.
10
11
#include <cstddef>
12
13
#include "lib/jxl/base/common.h"
14
#include "lib/jxl/base/rect.h"
15
16
namespace jxl {
17
// Some enums and typedefs used by more than one header file.
18
19
// Block is the square grid of pixels to which an "energy compaction"
20
// transformation (e.g. DCT) is applied. Each block has its own AC quantizer.
21
constexpr size_t kBlockDim = 8;
22
23
constexpr size_t kDCTBlockSize = kBlockDim * kBlockDim;
24
25
constexpr size_t kGroupDim = 256;
26
static_assert(kGroupDim % kBlockDim == 0,
27
              "Group dim should be divisible by block dim");
28
constexpr size_t kGroupDimInBlocks = kGroupDim / kBlockDim;
29
30
// Dimensions of a frame, in pixels, and other derived dimensions.
31
// Computed from FrameHeader.
32
// TODO(veluca): add extra channels.
33
struct FrameDimensions {
34
  void Set(size_t xsize_px, size_t ysize_px, size_t group_size_shift,
35
           size_t max_hshift, size_t max_vshift, bool modular_mode,
36
107k
           size_t upsampling) {
37
107k
    group_dim = (kGroupDim >> 1) << group_size_shift;
38
107k
    dc_group_dim = group_dim * kBlockDim;
39
107k
    xsize_upsampled = xsize_px;
40
107k
    ysize_upsampled = ysize_px;
41
107k
    xsize = DivCeil(xsize_px, upsampling);
42
107k
    ysize = DivCeil(ysize_px, upsampling);
43
107k
    xsize_blocks = DivCeil(xsize, kBlockDim << max_hshift) << max_hshift;
44
107k
    ysize_blocks = DivCeil(ysize, kBlockDim << max_vshift) << max_vshift;
45
107k
    xsize_padded = xsize_blocks * kBlockDim;
46
107k
    ysize_padded = ysize_blocks * kBlockDim;
47
107k
    if (modular_mode) {
48
      // Modular mode doesn't have any padding.
49
63.4k
      xsize_padded = xsize;
50
63.4k
      ysize_padded = ysize;
51
63.4k
    }
52
107k
    xsize_upsampled_padded = xsize_padded * upsampling;
53
107k
    ysize_upsampled_padded = ysize_padded * upsampling;
54
107k
    xsize_groups = DivCeil(xsize, group_dim);
55
107k
    ysize_groups = DivCeil(ysize, group_dim);
56
107k
    xsize_dc_groups = DivCeil(xsize_blocks, group_dim);
57
107k
    ysize_dc_groups = DivCeil(ysize_blocks, group_dim);
58
107k
    num_groups = xsize_groups * ysize_groups;
59
107k
    num_dc_groups = xsize_dc_groups * ysize_dc_groups;
60
107k
  }
61
62
20.7k
  Rect GroupRect(size_t group_index) const {
63
20.7k
    const size_t gx = group_index % xsize_groups;
64
20.7k
    const size_t gy = group_index / xsize_groups;
65
20.7k
    const Rect rect(gx * group_dim, gy * group_dim, group_dim, group_dim, xsize,
66
20.7k
                    ysize);
67
20.7k
    return rect;
68
20.7k
  }
69
70
8.15k
  Rect BlockGroupRect(size_t group_index) const {
71
8.15k
    const size_t gx = group_index % xsize_groups;
72
8.15k
    const size_t gy = group_index / xsize_groups;
73
8.15k
    const Rect rect(gx * (group_dim >> 3), gy * (group_dim >> 3),
74
8.15k
                    group_dim >> 3, group_dim >> 3, xsize_blocks, ysize_blocks);
75
8.15k
    return rect;
76
8.15k
  }
77
78
8.37k
  Rect DCGroupRect(size_t group_index) const {
79
8.37k
    const size_t gx = group_index % xsize_dc_groups;
80
8.37k
    const size_t gy = group_index / xsize_dc_groups;
81
8.37k
    const Rect rect(gx * group_dim, gy * group_dim, group_dim, group_dim,
82
8.37k
                    xsize_blocks, ysize_blocks);
83
8.37k
    return rect;
84
8.37k
  }
85
86
  // Image size without any upsampling, i.e. original_size / upsampling.
87
  size_t xsize;
88
  size_t ysize;
89
  // Original image size.
90
  size_t xsize_upsampled;
91
  size_t ysize_upsampled;
92
  // Image size after upsampling the padded image.
93
  size_t xsize_upsampled_padded;
94
  size_t ysize_upsampled_padded;
95
  // Image size after padding to a multiple of kBlockDim (if VarDCT mode).
96
  size_t xsize_padded;
97
  size_t ysize_padded;
98
  // Image size in kBlockDim blocks.
99
  size_t xsize_blocks;
100
  size_t ysize_blocks;
101
  // Image size in number of groups.
102
  size_t xsize_groups;
103
  size_t ysize_groups;
104
  // Image size in number of DC groups.
105
  size_t xsize_dc_groups;
106
  size_t ysize_dc_groups;
107
  // Number of AC or DC groups.
108
  size_t num_groups;
109
  size_t num_dc_groups;
110
  // Size of a group.
111
  size_t group_dim;
112
  size_t dc_group_dim;
113
};
114
115
}  // namespace jxl
116
117
#endif  // LIB_JXL_FRAME_DIMENSIONS_H_