| 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/memory_manager.h> |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <cstdint> |
| 10 |
#include <cstdio> |
| 11 |
#include <cstdlib> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include "lib/extras/codec.h" |
| 15 |
#include "lib/jxl/base/span.h" |
| 16 |
#include "lib/jxl/base/status.h" |
| 17 |
#include "lib/jxl/codec_in_out.h" |
| 18 |
#include "tools/file_io.h" |
| 19 |
#include "tools/no_memory_manager.h" |
| 20 |
#include "tools/ssimulacra2.h" |
| 21 |
|
| 22 |
#define QUIT(M) \ |
| 23 |
fprintf(stderr, "%s\n", M); \ |
| 24 |
return EXIT_FAILURE; |
| 25 |
|
| 26 |
int PrintUsage(char** argv) { |
| 27 |
fprintf(stderr, "Usage: %s orig.png distorted.png\n", argv[0]); |
| 28 |
fprintf(stderr, |
| 29 |
"Returns a score in range -inf..100, which correlates to subjective " |
| 30 |
"visual quality:\n"); |
| 31 |
fprintf(stderr, |
| 32 |
" 30 = low quality (p10 worst output of mozjpeg -quality 30)\n"); |
| 33 |
fprintf(stderr, |
| 34 |
" 50 = medium quality (average output of cjxl -q 40 or mozjpeg " |
| 35 |
"-quality 40,\n"); |
| 36 |
fprintf(stderr, |
| 37 |
" p10 output of cjxl -q 50 or mozjpeg " |
| 38 |
"-quality 60)\n"); |
| 39 |
fprintf(stderr, |
| 40 |
" 70 = high quality (average output of cjxl -q 70 or mozjpeg " |
| 41 |
"-quality 70,\n"); |
| 42 |
fprintf(stderr, |
| 43 |
" p10 output of cjxl -q 75 or mozjpeg " |
| 44 |
"-quality 80)\n"); |
| 45 |
fprintf(stderr, |
| 46 |
" 90 = very high quality (impossible to distinguish from " |
| 47 |
"original at 1:1,\n"); |
| 48 |
fprintf(stderr, |
| 49 |
" average output of cjxl -q 90 or " |
| 50 |
"mozjpeg -quality 90)\n"); |
| •51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
int main(int argc, char** argv) { |
| 55 |
if (argc != 3) return PrintUsage(argv); |
| 56 |
JxlMemoryManager* memory_manager = jpegxl::tools::NoMemoryManager(); |
| 57 |
|
| 58 |
jxl::CodecInOut io1{memory_manager}; |
| 59 |
jxl::CodecInOut io2{memory_manager}; |
| 60 |
jxl::CodecInOut* io[2] = {&io1, &io2}; |
| 61 |
const char* purpose[] = {"original", "distorted"}; |
| 62 |
for (size_t i = 0; i < 2; ++i) { |
| 63 |
std::vector<uint8_t> encoded; |
| 64 |
if (!jpegxl::tools::ReadFile(argv[1 + i], &encoded)) { |
| 65 |
fprintf(stderr, "Could not load %s image: %s\n", purpose[i], argv[1 + i]); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
if (!jxl::SetFromBytes(jxl::Bytes(encoded), jxl::extras::ColorHints(), |
| 69 |
io[i])) { |
| 70 |
fprintf(stderr, "Could not decode %s image: %s\n", purpose[i], |
| 71 |
argv[1 + i]); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
if (io[i]->xsize() < 8 || io[i]->ysize() < 8) { |
| 75 |
QUIT("Minimum image size is 8x8 pixels."); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (io1.xsize() != io2.xsize() || io1.ysize() != io2.ysize()) { |
| 80 |
QUIT("Image size mismatch."); |
| 81 |
} |
| 82 |
|
| 83 |
if (!io1.Main().HasAlpha()) { |
| 84 |
JXL_ASSIGN_OR_QUIT(Msssim msssim, |
| 85 |
ComputeSSIMULACRA2(io1.Main(), io2.Main()), |
| 86 |
"ComputeSSIMULACRA2 failed."); |
| 87 |
printf("%.8f\n", msssim.Score()); |
| 88 |
} else { |
| 89 |
// in case of alpha transparency: blend against dark and bright backgrounds |
| 90 |
// and return the worst of both scores |
| 91 |
JXL_ASSIGN_OR_QUIT(Msssim msssim0, |
| 92 |
ComputeSSIMULACRA2(io1.Main(), io2.Main(), 0.1f), |
| 93 |
"ComputeSSIMULACRA2 failed."); |
| 94 |
JXL_ASSIGN_OR_QUIT(Msssim msssim1, |
| 95 |
ComputeSSIMULACRA2(io1.Main(), io2.Main(), 0.9f), |
| 96 |
"ComputeSSIMULACRA2 failed."); |
| 97 |
printf("%.8f\n", std::min(msssim0.Score(), msssim1.Score())); |
| 98 |
} |
| 99 |
return EXIT_SUCCESS; |
| 100 |
} |