libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

frame_dimensions.h (4087B)


      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/image.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, size_t ysize, size_t group_size_shift,
     35            size_t max_hshift, size_t max_vshift, bool modular_mode,
     36            size_t upsampling) {
     37     group_dim = (kGroupDim >> 1) << group_size_shift;
     38     dc_group_dim = group_dim * kBlockDim;
     39     xsize_upsampled = xsize;
     40     ysize_upsampled = ysize;
     41     this->xsize = DivCeil(xsize, upsampling);
     42     this->ysize = DivCeil(ysize, upsampling);
     43     xsize_blocks = DivCeil(this->xsize, kBlockDim << max_hshift) << max_hshift;
     44     ysize_blocks = DivCeil(this->ysize, kBlockDim << max_vshift) << max_vshift;
     45     xsize_padded = xsize_blocks * kBlockDim;
     46     ysize_padded = ysize_blocks * kBlockDim;
     47     if (modular_mode) {
     48       // Modular mode doesn't have any padding.
     49       xsize_padded = this->xsize;
     50       ysize_padded = this->ysize;
     51     }
     52     xsize_upsampled_padded = xsize_padded * upsampling;
     53     ysize_upsampled_padded = ysize_padded * upsampling;
     54     xsize_groups = DivCeil(this->xsize, group_dim);
     55     ysize_groups = DivCeil(this->ysize, group_dim);
     56     xsize_dc_groups = DivCeil(xsize_blocks, group_dim);
     57     ysize_dc_groups = DivCeil(ysize_blocks, group_dim);
     58     num_groups = xsize_groups * ysize_groups;
     59     num_dc_groups = xsize_dc_groups * ysize_dc_groups;
     60   }
     61 
     62   Rect GroupRect(size_t group_index) const {
     63     const size_t gx = group_index % xsize_groups;
     64     const size_t gy = group_index / xsize_groups;
     65     const Rect rect(gx * group_dim, gy * group_dim, group_dim, group_dim, xsize,
     66                     ysize);
     67     return rect;
     68   }
     69 
     70   Rect BlockGroupRect(size_t group_index) const {
     71     const size_t gx = group_index % xsize_groups;
     72     const size_t gy = group_index / xsize_groups;
     73     const Rect rect(gx * (group_dim >> 3), gy * (group_dim >> 3),
     74                     group_dim >> 3, group_dim >> 3, xsize_blocks, ysize_blocks);
     75     return rect;
     76   }
     77 
     78   Rect DCGroupRect(size_t group_index) const {
     79     const size_t gx = group_index % xsize_dc_groups;
     80     const size_t gy = group_index / xsize_dc_groups;
     81     const Rect rect(gx * group_dim, gy * group_dim, group_dim, group_dim,
     82                     xsize_blocks, ysize_blocks);
     83     return rect;
     84   }
     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_