Imported Upstream version 0.9.0
[platform/upstream/libjxl.git] / tools / jxl_from_tree.cc
index aa85ff8..92a0874 100644 (file)
@@ -3,17 +3,18 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <jxl/cms.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <fstream>
 #include <iostream>
+#include <istream>
 #include <unordered_map>
 
-#include "lib/jxl/base/file_io.h"
+#include "lib/jxl/codec_in_out.h"
 #include "lib/jxl/enc_cache.h"
-#include "lib/jxl/enc_color_management.h"
-#include "lib/jxl/enc_file.h"
+#include "lib/jxl/enc_fields.h"
 #include "lib/jxl/enc_frame.h"
 #include "lib/jxl/enc_heuristics.h"
 #include "lib/jxl/modular/encoding/context_predict.h"
 #include "lib/jxl/modular/encoding/enc_ma.h"
 #include "lib/jxl/modular/encoding/encoding.h"
 #include "lib/jxl/splines.h"
+#include "lib/jxl/test_utils.h"  // TODO(eustas): cut this dependency
+#include "tools/file_io.h"
 
-namespace jxl {
+namespace jpegxl {
+namespace tools {
+
+using ::jxl::BitWriter;
+using ::jxl::BlendMode;
+using ::jxl::CodecInOut;
+using ::jxl::CodecMetadata;
+using ::jxl::ColorCorrelationMap;
+using ::jxl::ColorEncoding;
+using ::jxl::ColorTransform;
+using ::jxl::CompressParams;
+using ::jxl::DefaultEncoderHeuristics;
+using ::jxl::FrameDimensions;
+using ::jxl::FrameInfo;
+using ::jxl::Image3F;
+using ::jxl::ImageF;
+using ::jxl::PaddedBytes;
+using ::jxl::PassesEncoderState;
+using ::jxl::Predictor;
+using ::jxl::PropertyDecisionNode;
+using ::jxl::QuantizedSpline;
+using ::jxl::Spline;
+using ::jxl::Splines;
+using ::jxl::Tree;
 
 namespace {
 struct SplineData {
@@ -196,7 +222,7 @@ bool ParseNode(F& tok, Tree& tree, SplineData& spline_data,
   } else if (t == "Alpha") {
     io.metadata.m.SetAlphaBits(io.metadata.m.bit_depth.bits_per_sample);
     ImageF alpha(W, H);
-    io.frames[0].SetAlpha(std::move(alpha), false);
+    io.frames[0].SetAlpha(std::move(alpha));
   } else if (t == "Bitdepth") {
     t = tok();
     size_t num = 0;
@@ -392,7 +418,7 @@ bool ParseNode(F& tok, Tree& tree, SplineData& spline_data,
 
 class Heuristics : public DefaultEncoderHeuristics {
  public:
-  bool CustomFixedTreeLossless(const jxl::FrameDimensions& frame_dim,
+  bool CustomFixedTreeLossless(const FrameDimensions& frame_dim,
                                Tree* tree) override {
     *tree = tree_;
     return true;
@@ -412,16 +438,24 @@ int JxlFromTree(const char* in, const char* out, const char* tree_out) {
   size_t width = 1024, height = 1024;
   int x0 = 0, y0 = 0;
   cparams.SetLossless();
+  cparams.responsive = false;
   cparams.resampling = 1;
   cparams.ec_resampling = 1;
   cparams.modular_group_size_shift = 3;
   CodecInOut io;
   int have_next = 0;
 
-  std::ifstream f(in);
+  std::istream* f = &std::cin;
+  std::ifstream file;
+
+  if (strcmp(in, "-")) {
+    file.open(in, std::ifstream::in);
+    f = &file;
+  }
+
   auto tok = [&f]() {
     std::string out;
-    f >> out;
+    *f >> out;
     return out;
   };
   if (!ParseNode(tok, tree, spline_data, cparams, width, height, io, have_next,
@@ -436,7 +470,7 @@ int JxlFromTree(const char* in, const char* out, const char* tree_out) {
   io.SetFromImage(std::move(image), ColorEncoding::SRGB());
   io.SetSize((width + x0) * cparams.resampling,
              (height + y0) * cparams.resampling);
-  io.metadata.m.color_encoding.DecideIfWantICC();
+  io.metadata.m.color_encoding.DecideIfWantICC(*JxlGetDefaultCms());
   cparams.options.zero_tokens = true;
   cparams.palette_colors = 0;
   cparams.channel_colors_pre_transform_percent = 0;
@@ -452,14 +486,14 @@ int JxlFromTree(const char* in, const char* out, const char* tree_out) {
   *metadata = io.metadata;
   JXL_RETURN_IF_ERROR(metadata->size.Set(io.xsize(), io.ysize()));
 
-  metadata->m.xyb_encoded = cparams.color_transform == ColorTransform::kXYB;
+  metadata->m.xyb_encoded = (cparams.color_transform == ColorTransform::kXYB);
 
-  JXL_RETURN_IF_ERROR(WriteHeaders(metadata.get(), &writer, nullptr));
+  JXL_RETURN_IF_ERROR(WriteCodestreamHeaders(metadata.get(), &writer, nullptr));
   writer.ZeroPadToByte();
 
   while (true) {
     PassesEncoderState enc_state;
-    enc_state.heuristics = make_unique<Heuristics>(tree);
+    enc_state.heuristics = jxl::make_unique<Heuristics>(tree);
     enc_state.shared.image_features.splines =
         SplinesFromSplineData(spline_data, enc_state.shared.cmap);
 
@@ -469,14 +503,16 @@ int JxlFromTree(const char* in, const char* out, const char* tree_out) {
 
     io.frames[0].origin.x0 = x0;
     io.frames[0].origin.y0 = y0;
+    info.clamp = false;
 
-    JXL_RETURN_IF_ERROR(EncodeFrame(cparams, info, metadata.get(), io.frames[0],
-                                    &enc_state, GetJxlCms(), nullptr, &writer,
-                                    nullptr));
+    JXL_RETURN_IF_ERROR(jxl::EncodeFrame(
+        cparams, info, metadata.get(), io.frames[0], &enc_state,
+        *JxlGetDefaultCms(), nullptr, &writer, nullptr));
     if (!have_next) break;
     tree.clear();
     spline_data.splines.clear();
     have_next = 0;
+    cparams.manual_noise.clear();
     if (!ParseNode(tok, tree, spline_data, cparams, width, height, io,
                    have_next, x0, y0)) {
       return 1;
@@ -488,19 +524,22 @@ int JxlFromTree(const char* in, const char* out, const char* tree_out) {
 
   compressed = std::move(writer).TakeBytes();
 
-  if (!WriteFile(compressed, out)) {
+  if (!WriteFile(out, compressed)) {
     fprintf(stderr, "Failed to write to \"%s\"\n", out);
     return 1;
   }
 
   return 0;
 }
-}  // namespace jxl
+}  // namespace tools
+}  // namespace jpegxl
 
 int main(int argc, char** argv) {
-  if ((argc != 3 && argc != 4) || !strcmp(argv[1], argv[2])) {
+  if ((argc != 3 && argc != 4) ||
+      (strcmp(argv[1], "-") && !strcmp(argv[1], argv[2]))) {
     fprintf(stderr, "Usage: %s tree_in.txt out.jxl [tree_drawing]\n", argv[0]);
     return 1;
   }
-  return jxl::JxlFromTree(argv[1], argv[2], argc < 4 ? nullptr : argv[3]);
+  return jpegxl::tools::JxlFromTree(argv[1], argv[2],
+                                    argc < 4 ? nullptr : argv[3]);
 }