libjxl

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

common_internal.h (4164B)


      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_JPEGLI_COMMON_INTERNAL_H_
      7 #define LIB_JPEGLI_COMMON_INTERNAL_H_
      8 
      9 #include <stddef.h>
     10 #include <stdint.h>
     11 #include <string.h>
     12 
     13 #include <algorithm>
     14 #include <hwy/aligned_allocator.h>
     15 
     16 #include "lib/jpegli/memory_manager.h"
     17 #include "lib/jpegli/simd.h"
     18 #include "lib/jxl/base/compiler_specific.h"  // for ssize_t
     19 #include "lib/jxl/base/status.h"             // for JXL_CHECK
     20 
     21 namespace jpegli {
     22 
     23 enum State {
     24   kDecNull,
     25   kDecStart,
     26   kDecInHeader,
     27   kDecHeaderDone,
     28   kDecProcessMarkers,
     29   kDecProcessScan,
     30   kEncNull,
     31   kEncStart,
     32   kEncHeader,
     33   kEncReadImage,
     34   kEncWriteCoeffs,
     35 };
     36 
     37 template <typename T1, typename T2>
     38 constexpr inline T1 DivCeil(T1 a, T2 b) {
     39   return (a + b - 1) / b;
     40 }
     41 
     42 template <typename T1, typename T2>
     43 constexpr inline T1 RoundUpTo(T1 a, T2 b) {
     44   return DivCeil(a, b) * b;
     45 }
     46 
     47 constexpr size_t kDCTBlockSize = 64;
     48 // This is set to the same value as MAX_COMPS_IN_SCAN, because that is the
     49 // maximum number of channels the libjpeg-turbo decoder can decode.
     50 constexpr int kMaxComponents = 4;
     51 constexpr int kMaxQuantTables = 4;
     52 constexpr int kJpegPrecision = 8;
     53 constexpr int kMaxHuffmanTables = 4;
     54 constexpr size_t kJpegHuffmanMaxBitLength = 16;
     55 constexpr int kJpegHuffmanAlphabetSize = 256;
     56 constexpr int kJpegDCAlphabetSize = 12;
     57 constexpr int kMaxDHTMarkers = 512;
     58 constexpr int kMaxDimPixels = 65535;
     59 constexpr uint8_t kApp1 = 0xE1;
     60 constexpr uint8_t kApp2 = 0xE2;
     61 const uint8_t kIccProfileTag[12] = "ICC_PROFILE";
     62 const uint8_t kExifTag[6] = "Exif\0";
     63 const uint8_t kXMPTag[29] = "http://ns.adobe.com/xap/1.0/";
     64 
     65 /* clang-format off */
     66 constexpr uint32_t kJPEGNaturalOrder[80] = {
     67   0,   1,  8, 16,  9,  2,  3, 10,
     68   17, 24, 32, 25, 18, 11,  4,  5,
     69   12, 19, 26, 33, 40, 48, 41, 34,
     70   27, 20, 13,  6,  7, 14, 21, 28,
     71   35, 42, 49, 56, 57, 50, 43, 36,
     72   29, 22, 15, 23, 30, 37, 44, 51,
     73   58, 59, 52, 45, 38, 31, 39, 46,
     74   53, 60, 61, 54, 47, 55, 62, 63,
     75   // extra entries for safety in decoder
     76   63, 63, 63, 63, 63, 63, 63, 63,
     77   63, 63, 63, 63, 63, 63, 63, 63
     78 };
     79 
     80 constexpr uint32_t kJPEGZigZagOrder[64] = {
     81   0,   1,  5,  6, 14, 15, 27, 28,
     82   2,   4,  7, 13, 16, 26, 29, 42,
     83   3,   8, 12, 17, 25, 30, 41, 43,
     84   9,  11, 18, 24, 31, 40, 44, 53,
     85   10, 19, 23, 32, 39, 45, 52, 54,
     86   20, 22, 33, 38, 46, 51, 55, 60,
     87   21, 34, 37, 47, 50, 56, 59, 61,
     88   35, 36, 48, 49, 57, 58, 62, 63
     89 };
     90 /* clang-format on */
     91 
     92 template <typename T>
     93 class RowBuffer {
     94  public:
     95   template <typename CInfoType>
     96   void Allocate(CInfoType cinfo, size_t num_rows, size_t rowsize) {
     97     size_t vec_size = std::max(VectorSize(), sizeof(T));
     98     JXL_CHECK(vec_size % sizeof(T) == 0);
     99     size_t alignment = std::max<size_t>(HWY_ALIGNMENT, vec_size);
    100     size_t min_memstride = alignment + rowsize * sizeof(T) + vec_size;
    101     size_t memstride = RoundUpTo(min_memstride, alignment);
    102     xsize_ = rowsize;
    103     ysize_ = num_rows;
    104     stride_ = memstride / sizeof(T);
    105     offset_ = alignment / sizeof(T);
    106     data_ = ::jpegli::Allocate<T>(cinfo, ysize_ * stride_, JPOOL_IMAGE_ALIGNED);
    107   }
    108 
    109   T* Row(ssize_t y) const {
    110     return &data_[((ysize_ + y) % ysize_) * stride_ + offset_];
    111   }
    112 
    113   size_t xsize() const { return xsize_; };
    114   size_t ysize() const { return ysize_; };
    115   size_t stride() const { return stride_; }
    116 
    117   void PadRow(size_t y, size_t from, int border) {
    118     float* row = Row(y);
    119     for (int offset = -border; offset < 0; ++offset) {
    120       row[offset] = row[0];
    121     }
    122     float last_val = row[from - 1];
    123     for (size_t x = from; x < xsize_ + border; ++x) {
    124       row[x] = last_val;
    125     }
    126   }
    127 
    128   void CopyRow(ssize_t dst_row, ssize_t src_row, int border) {
    129     memcpy(Row(dst_row) - border, Row(src_row) - border,
    130            (xsize_ + 2 * border) * sizeof(T));
    131   }
    132 
    133   void FillRow(ssize_t y, T val, size_t len) {
    134     T* row = Row(y);
    135     for (size_t x = 0; x < len; ++x) {
    136       row[x] = val;
    137     }
    138   }
    139 
    140  private:
    141   size_t xsize_;
    142   size_t ysize_;
    143   size_t stride_;
    144   size_t offset_;
    145   T* data_;
    146 };
    147 
    148 }  // namespace jpegli
    149 
    150 #endif  // LIB_JPEGLI_COMMON_INTERNAL_H_