xyb_range.cc (2733B)
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 <jxl/cms.h> 7 #include <stdio.h> 8 9 #include <utility> 10 11 #include "lib/jxl/base/compiler_specific.h" 12 #include "lib/jxl/base/data_parallel.h" 13 #include "lib/jxl/base/printf_macros.h" 14 #include "lib/jxl/base/status.h" 15 #include "lib/jxl/codec_in_out.h" 16 #include "lib/jxl/color_encoding_internal.h" 17 #include "lib/jxl/enc_xyb.h" 18 #include "lib/jxl/image.h" 19 #include "lib/jxl/image_bundle.h" 20 21 namespace jpegxl { 22 namespace tools { 23 namespace { 24 25 using ::jxl::CodecInOut; 26 using ::jxl::ColorEncoding; 27 using ::jxl::Image3F; 28 using ::jxl::ImageBundle; 29 using ::jxl::Status; 30 using ::jxl::ThreadPool; 31 32 Status PrintXybRange() { 33 JXL_ASSIGN_OR_RETURN(Image3F linear, Image3F::Create(1u << 16, 257)); 34 for (int b = 0; b < 256; ++b) { 35 float* JXL_RESTRICT row0 = linear.PlaneRow(0, b + 1); 36 float* JXL_RESTRICT row1 = linear.PlaneRow(1, b + 1); 37 float* JXL_RESTRICT row2 = linear.PlaneRow(2, b + 1); 38 for (int r = 0; r < 256; ++r) { 39 for (int g = 0; g < 256; ++g) { 40 const int x = (r << 8) + g; 41 row0[x] = r; 42 row1[x] = g; 43 row2[x] = b; 44 } 45 } 46 } 47 CodecInOut io; 48 io.metadata.m.SetUintSamples(8); 49 io.metadata.m.color_encoding = ColorEncoding::LinearSRGB(); 50 io.SetFromImage(std::move(linear), io.metadata.m.color_encoding); 51 const ImageBundle& ib = io.Main(); 52 ThreadPool* null_pool = nullptr; 53 JXL_ASSIGN_OR_RETURN(Image3F opsin, Image3F::Create(ib.xsize(), ib.ysize())); 54 (void)jxl::ToXYB(ib, null_pool, &opsin, *JxlGetDefaultCms()); 55 for (size_t c = 0; c < 3; ++c) { 56 float minval = 1e10f; 57 float maxval = -1e10f; 58 int rgb_min = 0; 59 int rgb_max = 0; 60 for (int b = 0; b < 256; ++b) { 61 const float* JXL_RESTRICT row = opsin.PlaneRow(c, b); 62 for (int r = 0; r < 256; ++r) { 63 for (int g = 0; g < 256; ++g) { 64 float val = row[(r << 8) + g]; 65 if (val < minval) { 66 minval = val; 67 rgb_min = (r << 16) + (g << 8) + b; 68 } 69 if (val > maxval) { 70 maxval = val; 71 rgb_max = (r << 16) + (g << 8) + b; 72 } 73 } 74 } 75 } 76 printf("Opsin image plane %" PRIuS 77 " range: [%8.4f, %8.4f] " 78 "center: %.12f, range: %.12f (RGBmin=%06x, RGBmax=%06x)\n", 79 c, minval, maxval, 0.5 * (minval + maxval), 0.5 * (maxval - minval), 80 rgb_min, rgb_max); 81 // Ensure our constants are at least as wide as those obtained from sRGB. 82 } 83 return true; 84 } 85 86 } // namespace 87 } // namespace tools 88 } // namespace jpegxl 89 90 int main() { JXL_CHECK(jpegxl::tools::PrintXybRange()); }