hlg.cc (1990B)
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 const auto process_row = [&](const int y, const int thread) -> Status { 30 float* const JXL_RESTRICT rows[3] = {ib->color()->PlaneRow(0, y), 31 ib->color()->PlaneRow(1, y), 32 ib->color()->PlaneRow(2, y)}; 33 for (size_t x = 0; x < ib->xsize(); ++x) { 34 float& red = rows[0][x]; 35 float& green = rows[1][x]; 36 float& blue = rows[2][x]; 37 const float luminance = 0.2627f * red + 0.6780f * green + 0.0593f * blue; 38 const float ratio = std::pow(luminance, gamma - 1); 39 if (std::isfinite(ratio)) { 40 red *= ratio; 41 green *= ratio; 42 blue *= ratio; 43 } 44 } 45 return true; 46 }; 47 48 JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, ib->ysize(), ThreadPool::NoInit, 49 process_row, "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