libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

generate_lut_template.cc (2210B)


      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 <stdio.h>
      7 #include <stdlib.h>
      8 
      9 #include "lib/extras/codec.h"
     10 #include "lib/extras/packed_image_convert.h"
     11 #include "tools/cmdline.h"
     12 #include "tools/file_io.h"
     13 #include "tools/thread_pool_internal.h"
     14 
     15 using jxl::Image3F;
     16 
     17 int main(int argc, const char** argv) {
     18   jpegxl::tools::ThreadPoolInternal pool;
     19 
     20   jpegxl::tools::CommandLineParser parser;
     21   size_t N = 64;
     22   parser.AddOptionValue('N', "lut_size", "N", "linear size of the LUT", &N,
     23                         &jpegxl::tools::ParseUnsigned, 0);
     24   const char* output_filename = nullptr;
     25   auto output_filename_option = parser.AddPositionalOption(
     26       "output", true, "output LUT", &output_filename, 0);
     27 
     28   if (!parser.Parse(argc, argv)) {
     29     fprintf(stderr, "See -h for help.\n");
     30     return EXIT_FAILURE;
     31   }
     32 
     33   if (parser.HelpFlagPassed()) {
     34     parser.PrintHelp();
     35     return EXIT_SUCCESS;
     36   }
     37 
     38   if (!parser.GetOption(output_filename_option)->matched()) {
     39     fprintf(stderr, "Missing output filename.\nSee -h for help.\n");
     40     return EXIT_FAILURE;
     41   }
     42 
     43   JXL_ASSIGN_OR_RETURN(Image3F image, Image3F::Create(N * N, N));
     44   const float scale = 1.0 / (N - 1);
     45   JXL_CHECK(jxl::RunOnPool(
     46       &pool, 0, N, jxl::ThreadPool::NoInit,
     47       [&](const uint32_t y, size_t /* thread */) {
     48         const float g = y * scale;
     49         float* const JXL_RESTRICT rows[3] = {
     50             image.PlaneRow(0, y), image.PlaneRow(1, y), image.PlaneRow(2, y)};
     51         for (size_t x = 0; x < N * N; ++x) {
     52           size_t r = x % N;
     53           size_t q = x / N;
     54           rows[0][x] = r * scale;
     55           rows[1][x] = g;
     56           rows[2][x] = q * scale;
     57         }
     58       },
     59       "GenerateTemplate"));
     60 
     61   JxlPixelFormat format = {3, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};
     62   jxl::extras::PackedPixelFile ppf =
     63       jxl::extras::ConvertImage3FToPackedPixelFile(
     64           image, jxl::ColorEncoding::SRGB(), format, &pool);
     65   std::vector<uint8_t> encoded;
     66   JXL_CHECK(jxl::Encode(ppf, output_filename, &encoded, &pool));
     67   JXL_CHECK(jpegxl::tools::WriteFile(output_filename, encoded));
     68 }