hlg.cc (1987B)
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 #include "lib/extras/hlg.h" 7 8 #include <jxl/cms.h> 9 10 #include <cmath> 11 12 namespace jxl { 13 14 float GetHlgGamma(const float peak_luminance, const float surround_luminance) { 15 return 1.2f * std::pow(1.111f, std::log2(peak_luminance / 1000.f)) * 16 std::pow(0.98f, std::log2(surround_luminance / 5.f)); 17 } 18 19 Status HlgOOTF(ImageBundle* ib, const float gamma, ThreadPool* pool) { 20 ColorEncoding linear_rec2020; 21 linear_rec2020.SetColorSpace(ColorSpace::kRGB); 22 JXL_RETURN_IF_ERROR(linear_rec2020.SetPrimariesType(Primaries::k2100)); 23 JXL_RETURN_IF_ERROR(linear_rec2020.SetWhitePointType(WhitePoint::kD65)); 24 linear_rec2020.Tf().SetTransferFunction(TransferFunction::kLinear); 25 JXL_RETURN_IF_ERROR(linear_rec2020.CreateICC()); 26 JXL_RETURN_IF_ERROR( 27 ib->TransformTo(linear_rec2020, *JxlGetDefaultCms(), pool)); 28 29 JXL_RETURN_IF_ERROR(RunOnPool( 30 pool, 0, ib->ysize(), ThreadPool::NoInit, 31 [&](const int y, const int thread) { 32 float* const JXL_RESTRICT rows[3] = {ib->color()->PlaneRow(0, y), 33 ib->color()->PlaneRow(1, y), 34 ib->color()->PlaneRow(2, y)}; 35 for (size_t x = 0; x < ib->xsize(); ++x) { 36 float& red = rows[0][x]; 37 float& green = rows[1][x]; 38 float& blue = rows[2][x]; 39 const float luminance = 40 0.2627f * red + 0.6780f * green + 0.0593f * blue; 41 const float ratio = std::pow(luminance, gamma - 1); 42 if (std::isfinite(ratio)) { 43 red *= ratio; 44 green *= ratio; 45 blue *= ratio; 46 } 47 } 48 }, 49 "HlgOOTF")); 50 return true; 51 } 52 53 Status HlgInverseOOTF(ImageBundle* ib, const float gamma, ThreadPool* pool) { 54 return HlgOOTF(ib, 1.f / gamma, pool); 55 } 56 57 } // namespace jxl