palette.h (5362B)
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_TRANSFORM_PALETTE_H_ 7 #define LIB_JXL_MODULAR_TRANSFORM_PALETTE_H_ 8 9 #include <atomic> 10 11 #include "lib/jxl/base/data_parallel.h" 12 #include "lib/jxl/base/status.h" 13 #include "lib/jxl/modular/encoding/context_predict.h" 14 #include "lib/jxl/modular/modular_image.h" 15 #include "lib/jxl/modular/transform/transform.h" // CheckEqualChannels 16 17 namespace jxl { 18 19 namespace palette_internal { 20 21 static constexpr int kMaxPaletteLookupTableSize = 1 << 16; 22 23 static constexpr int kRgbChannels = 3; 24 25 // 5x5x5 color cube for the larger cube. 26 static constexpr int kLargeCube = 5; 27 28 // Smaller interleaved color cube to fill the holes of the larger cube. 29 static constexpr int kSmallCube = 4; 30 static constexpr int kSmallCubeBits = 2; 31 // kSmallCube ** 3 32 static constexpr int kLargeCubeOffset = kSmallCube * kSmallCube * kSmallCube; 33 34 static inline pixel_type Scale(uint64_t value, uint64_t bit_depth, 35 uint64_t denom) { 36 // return (value * ((static_cast<pixel_type_w>(1) << bit_depth) - 1)) / denom; 37 // We only call this function with kSmallCube or kLargeCube - 1 as denom, 38 // allowing us to avoid a division here. 39 JXL_ASSERT(denom == 4); 40 return (value * ((static_cast<uint64_t>(1) << bit_depth) - 1)) >> 2; 41 } 42 43 // The purpose of this function is solely to extend the interpretation of 44 // palette indices to implicit values. If index < nb_deltas, indicating that the 45 // result is a delta palette entry, it is the responsibility of the caller to 46 // treat it as such. 47 static JXL_MAYBE_UNUSED pixel_type 48 GetPaletteValue(const pixel_type *const palette, int index, const size_t c, 49 const int palette_size, const int onerow, const int bit_depth) { 50 if (index < 0) { 51 static constexpr std::array<std::array<pixel_type, 3>, 72> kDeltaPalette = { 52 { 53 {{0, 0, 0}}, {{4, 4, 4}}, {{11, 0, 0}}, 54 {{0, 0, -13}}, {{0, -12, 0}}, {{-10, -10, -10}}, 55 {{-18, -18, -18}}, {{-27, -27, -27}}, {{-18, -18, 0}}, 56 {{0, 0, -32}}, {{-32, 0, 0}}, {{-37, -37, -37}}, 57 {{0, -32, -32}}, {{24, 24, 45}}, {{50, 50, 50}}, 58 {{-45, -24, -24}}, {{-24, -45, -45}}, {{0, -24, -24}}, 59 {{-34, -34, 0}}, {{-24, 0, -24}}, {{-45, -45, -24}}, 60 {{64, 64, 64}}, {{-32, 0, -32}}, {{0, -32, 0}}, 61 {{-32, 0, 32}}, {{-24, -45, -24}}, {{45, 24, 45}}, 62 {{24, -24, -45}}, {{-45, -24, 24}}, {{80, 80, 80}}, 63 {{64, 0, 0}}, {{0, 0, -64}}, {{0, -64, -64}}, 64 {{-24, -24, 45}}, {{96, 96, 96}}, {{64, 64, 0}}, 65 {{45, -24, -24}}, {{34, -34, 0}}, {{112, 112, 112}}, 66 {{24, -45, -45}}, {{45, 45, -24}}, {{0, -32, 32}}, 67 {{24, -24, 45}}, {{0, 96, 96}}, {{45, -24, 24}}, 68 {{24, -45, -24}}, {{-24, -45, 24}}, {{0, -64, 0}}, 69 {{96, 0, 0}}, {{128, 128, 128}}, {{64, 0, 64}}, 70 {{144, 144, 144}}, {{96, 96, 0}}, {{-36, -36, 36}}, 71 {{45, -24, -45}}, {{45, -45, -24}}, {{0, 0, -96}}, 72 {{0, 128, 128}}, {{0, 96, 0}}, {{45, 24, -45}}, 73 {{-128, 0, 0}}, {{24, -45, 24}}, {{-45, 24, -45}}, 74 {{64, 0, -64}}, {{64, -64, -64}}, {{96, 0, 96}}, 75 {{45, -45, 24}}, {{24, 45, -45}}, {{64, 64, -64}}, 76 {{128, 128, 0}}, {{0, 0, -128}}, {{-24, 45, -45}}, 77 }}; 78 if (c >= kRgbChannels) { 79 return 0; 80 } 81 // Do not open the brackets, otherwise INT32_MIN negation could overflow. 82 index = -(index + 1); 83 index %= 1 + 2 * (kDeltaPalette.size() - 1); 84 static constexpr int kMultiplier[] = {-1, 1}; 85 pixel_type result = 86 kDeltaPalette[((index + 1) >> 1)][c] * kMultiplier[index & 1]; 87 if (bit_depth > 8) { 88 result *= static_cast<pixel_type>(1) << (bit_depth - 8); 89 } 90 return result; 91 } else if (palette_size <= index && index < palette_size + kLargeCubeOffset) { 92 if (c >= kRgbChannels) return 0; 93 index -= palette_size; 94 index >>= c * kSmallCubeBits; 95 return Scale(index % kSmallCube, bit_depth, kSmallCube) + 96 (1 << (std::max(0, bit_depth - 3))); 97 } else if (palette_size + kLargeCubeOffset <= index) { 98 if (c >= kRgbChannels) return 0; 99 index -= palette_size + kLargeCubeOffset; 100 // TODO(eustas): should we take care of ambiguity created by 101 // index >= kLargeCube ** 3 ? 102 switch (c) { 103 case 0: 104 default: 105 break; 106 case 1: 107 index /= kLargeCube; 108 break; 109 case 2: 110 index /= kLargeCube * kLargeCube; 111 break; 112 } 113 return Scale(index % kLargeCube, bit_depth, kLargeCube - 1); 114 } 115 return palette[c * onerow + static_cast<size_t>(index)]; 116 } 117 118 } // namespace palette_internal 119 120 Status InvPalette(Image &input, uint32_t begin_c, uint32_t nb_colors, 121 uint32_t nb_deltas, Predictor predictor, 122 const weighted::Header &wp_header, ThreadPool *pool); 123 124 Status MetaPalette(Image &input, uint32_t begin_c, uint32_t end_c, 125 uint32_t nb_colors, uint32_t nb_deltas, bool lossy); 126 127 } // namespace jxl 128 129 #endif // LIB_JXL_MODULAR_TRANSFORM_PALETTE_H_