libjxl

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

ssimulacra_main.cc (2388B)


      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 
      8 #include "lib/extras/codec.h"
      9 // TODO(eustas): we should, but we can't?
     10 // #include "lib/jxl/base/span.h"
     11 #include <jxl/cms.h>
     12 
     13 #include "lib/jxl/base/status.h"
     14 #include "lib/jxl/image_bundle.h"
     15 #include "tools/file_io.h"
     16 #include "tools/ssimulacra.h"
     17 
     18 namespace ssimulacra {
     19 namespace {
     20 
     21 int PrintUsage(char** argv) {
     22   fprintf(stderr, "Usage: %s [-v] [-s] orig.png distorted.png\n", argv[0]);
     23   return 1;
     24 }
     25 
     26 int Run(int argc, char** argv) {
     27   if (argc < 2) return PrintUsage(argv);
     28 
     29   bool verbose = false;
     30   bool simple = false;
     31   int input_arg = 1;
     32   if (!strcmp(argv[input_arg], "-v")) {
     33     verbose = true;
     34     input_arg++;
     35   }
     36   if (!strcmp(argv[input_arg], "-s")) {
     37     simple = true;
     38     input_arg++;
     39   }
     40   if (argc < input_arg + 2) return PrintUsage(argv);
     41 
     42   jxl::CodecInOut io[2];
     43   for (size_t i = 0; i < 2; ++i) {
     44     std::vector<uint8_t> encoded;
     45     JXL_CHECK(jpegxl::tools::ReadFile(argv[input_arg + i], &encoded));
     46     JXL_CHECK(jxl::SetFromBytes(jxl::Bytes(encoded), jxl::extras::ColorHints(),
     47                                 &io[i]));
     48   }
     49   jxl::ImageBundle& ib1 = io[0].Main();
     50   jxl::ImageBundle& ib2 = io[1].Main();
     51   JXL_CHECK(ib1.TransformTo(jxl::ColorEncoding::LinearSRGB(ib1.IsGray()),
     52                             *JxlGetDefaultCms(), nullptr));
     53   JXL_CHECK(ib2.TransformTo(jxl::ColorEncoding::LinearSRGB(ib2.IsGray()),
     54                             *JxlGetDefaultCms(), nullptr));
     55   jxl::Image3F& img1 = *ib1.color();
     56   jxl::Image3F& img2 = *ib2.color();
     57   if (img1.xsize() != img2.xsize() || img1.ysize() != img2.ysize()) {
     58     fprintf(stderr, "Image size mismatch\n");
     59     return 1;
     60   }
     61   if (img1.xsize() < 8 || img1.ysize() < 8) {
     62     fprintf(stderr, "Minimum image size is 8x8 pixels\n");
     63     return 1;
     64   }
     65 
     66   jxl::StatusOr<Ssimulacra> ssimulacra_or = ComputeDiff(img1, img2, simple);
     67   if (!ssimulacra_or.ok()) {
     68     fprintf(stderr, "ComputeDiff failed\n");
     69     return 1;
     70   }
     71 
     72   Ssimulacra ssimulacra = std::move(ssimulacra_or).value();
     73 
     74   if (verbose) {
     75     ssimulacra.PrintDetails();
     76   }
     77   printf("%.8f\n", ssimulacra.Score());
     78   return 0;
     79 }
     80 
     81 }  // namespace
     82 }  // namespace ssimulacra
     83 
     84 int main(int argc, char** argv) { return ssimulacra::Run(argc, argv); }