alpha.h (2348B)
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_ALPHA_H_ 7 #define LIB_JXL_ALPHA_H_ 8 9 #include <stddef.h> 10 #include <stdint.h> 11 12 #include <limits> 13 14 #include "lib/jxl/base/compiler_specific.h" 15 16 namespace jxl { 17 18 // A very small value to avoid divisions by zero when converting to 19 // unpremultiplied alpha. Page 21 of the technical introduction to OpenEXR 20 // (https://www.openexr.com/documentation/TechnicalIntroduction.pdf) recommends 21 // "a power of two" that is "less than half of the smallest positive 16-bit 22 // floating-point value". That smallest value happens to be the denormal number 23 // 2^-24, so 2^-26 should be a good choice. 24 static constexpr float kSmallAlpha = 1.f / (1u << 26u); 25 26 struct AlphaBlendingInputLayer { 27 const float* r; 28 const float* g; 29 const float* b; 30 const float* a; 31 }; 32 33 struct AlphaBlendingOutput { 34 float* r; 35 float* g; 36 float* b; 37 float* a; 38 }; 39 40 // Note: The pointers in `out` are allowed to alias those in `bg` or `fg`. 41 // No pointer shall be null. 42 void PerformAlphaBlending(const AlphaBlendingInputLayer& bg, 43 const AlphaBlendingInputLayer& fg, 44 const AlphaBlendingOutput& out, size_t num_pixels, 45 bool alpha_is_premultiplied, bool clamp); 46 // Single plane alpha blending 47 void PerformAlphaBlending(const float* bg, const float* bga, const float* fg, 48 const float* fga, float* out, size_t num_pixels, 49 bool alpha_is_premultiplied, bool clamp); 50 51 void PerformAlphaWeightedAdd(const float* bg, const float* fg, const float* fga, 52 float* out, size_t num_pixels, bool clamp); 53 54 void PerformMulBlending(const float* bg, const float* fg, float* out, 55 size_t num_pixels, bool clamp); 56 57 void PremultiplyAlpha(float* JXL_RESTRICT r, float* JXL_RESTRICT g, 58 float* JXL_RESTRICT b, const float* JXL_RESTRICT a, 59 size_t num_pixels); 60 void UnpremultiplyAlpha(float* JXL_RESTRICT r, float* JXL_RESTRICT g, 61 float* JXL_RESTRICT b, const float* JXL_RESTRICT a, 62 size_t num_pixels); 63 64 } // namespace jxl 65 66 #endif // LIB_JXL_ALPHA_H_