texture_to_cube.cc (2412B)
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/jxl/image_bundle.h" 11 #include "tools/args.h" 12 #include "tools/cmdline.h" 13 #include "tools/thread_pool_internal.h" 14 15 int main(int argc, const char** argv) { 16 jpegxl::tools::ThreadPoolInternal pool; 17 18 jpegxl::tools::CommandLineParser parser; 19 const char* input_filename = nullptr; 20 auto input_filename_option = parser.AddPositionalOption( 21 "input", true, "input image", &input_filename, 0); 22 const char* output_filename = nullptr; 23 auto output_filename_option = parser.AddPositionalOption( 24 "output", true, "output Cube LUT", &output_filename, 0); 25 26 if (!parser.Parse(argc, argv)) { 27 fprintf(stderr, "See -h for help.\n"); 28 return EXIT_FAILURE; 29 } 30 31 if (parser.HelpFlagPassed()) { 32 parser.PrintHelp(); 33 return EXIT_SUCCESS; 34 } 35 36 if (!parser.GetOption(input_filename_option)->matched()) { 37 fprintf(stderr, "Missing input filename.\nSee -h for help.\n"); 38 return EXIT_FAILURE; 39 } 40 if (!parser.GetOption(output_filename_option)->matched()) { 41 fprintf(stderr, "Missing output filename.\nSee -h for help.\n"); 42 return EXIT_FAILURE; 43 } 44 45 jxl::CodecInOut image; 46 std::vector<uint8_t> encoded; 47 JXL_CHECK(jpegxl::tools::ReadFile(input_filename, &encoded)); 48 JXL_CHECK(jxl::SetFromBytes(jxl::Bytes(encoded), jxl::extras::ColorHints(), 49 &image, &pool)); 50 51 JXL_CHECK(image.xsize() == image.ysize() * image.ysize()); 52 const unsigned N = image.ysize(); 53 54 FILE* const output = fopen(output_filename, "wb"); 55 JXL_CHECK(output); 56 57 fprintf(output, "# Created by libjxl\n"); 58 fprintf(output, "LUT_3D_SIZE %u\n", N); 59 fprintf(output, "DOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n\n"); 60 61 for (size_t b = 0; b < N; ++b) { 62 for (size_t g = 0; g < N; ++g) { 63 const size_t y = g; 64 const float* const JXL_RESTRICT rows[3] = { 65 image.Main().color()->ConstPlaneRow(0, y) + N * b, 66 image.Main().color()->ConstPlaneRow(1, y) + N * b, 67 image.Main().color()->ConstPlaneRow(2, y) + N * b}; 68 for (size_t r = 0; r < N; ++r) { 69 const size_t x = r; 70 fprintf(output, "%.6f %.6f %.6f\n", rows[0][x], rows[1][x], rows[2][x]); 71 } 72 } 73 } 74 }