/src/libjxl/lib/jxl/enc_debug_image.cc
Line | Count | Source (jump to first uncovered line) |
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 "lib/jxl/enc_debug_image.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstdint> |
12 | | |
13 | | #include "lib/jxl/base/rect.h" |
14 | | #include "lib/jxl/base/status.h" |
15 | | #include "lib/jxl/color_encoding_internal.h" |
16 | | #include "lib/jxl/dec_external_image.h" |
17 | | #include "lib/jxl/enc_params.h" |
18 | | #include "lib/jxl/image_ops.h" |
19 | | |
20 | | namespace jxl { |
21 | | |
22 | | namespace { |
23 | | template <typename From> |
24 | 0 | StatusOr<Image3F> ConvertToFloat(const Image3<From>& from) { |
25 | 0 | float factor = 1.0f / std::numeric_limits<From>::max(); |
26 | 0 | if (std::is_same<From, double>::value || std::is_same<From, float>::value) { |
27 | 0 | factor = 1.0f; |
28 | 0 | } |
29 | 0 | JxlMemoryManager* memory_manager = from.memory_manager(); |
30 | 0 | JXL_ASSIGN_OR_RETURN( |
31 | 0 | Image3F to, Image3F::Create(memory_manager, from.xsize(), from.ysize())); |
32 | 0 | for (size_t c = 0; c < 3; ++c) { |
33 | 0 | for (size_t y = 0; y < from.ysize(); ++y) { |
34 | 0 | const From* const JXL_RESTRICT row_from = from.ConstPlaneRow(c, y); |
35 | 0 | float* const JXL_RESTRICT row_to = to.PlaneRow(c, y); |
36 | 0 | for (size_t x = 0; x < from.xsize(); ++x) { |
37 | 0 | row_to[x] = row_from[x] * factor; |
38 | 0 | } |
39 | 0 | } |
40 | 0 | } |
41 | 0 | return to; |
42 | 0 | } Unexecuted instantiation: enc_debug_image.cc:jxl::StatusOr<jxl::Image3<float> > jxl::(anonymous namespace)::ConvertToFloat<float>(jxl::Image3<float> const&) Unexecuted instantiation: enc_debug_image.cc:jxl::StatusOr<jxl::Image3<float> > jxl::(anonymous namespace)::ConvertToFloat<unsigned char>(jxl::Image3<unsigned char> const&) |
43 | | |
44 | | template <typename T> |
45 | | Status DumpImageT(const CompressParams& cparams, const char* label, |
46 | 0 | const ColorEncoding& color_encoding, const Image3<T>& image) { |
47 | 0 | if (!cparams.debug_image) return true; |
48 | 0 | JXL_ASSIGN_OR_RETURN(Image3F float_image, ConvertToFloat(image)); |
49 | 0 | JxlColorEncoding color = color_encoding.ToExternal(); |
50 | 0 | size_t num_pixels = 3 * image.xsize() * image.ysize(); |
51 | 0 | std::vector<uint16_t> pixels(num_pixels); |
52 | 0 | const ImageF* channels[3]; |
53 | 0 | for (int c = 0; c < 3; ++c) { |
54 | 0 | channels[c] = &float_image.Plane(c); |
55 | 0 | } |
56 | 0 | JXL_RETURN_IF_ERROR(ConvertChannelsToExternal( |
57 | 0 | channels, 3, 16, false, JXL_BIG_ENDIAN, 6 * image.xsize(), nullptr, |
58 | 0 | pixels.data(), 2 * num_pixels, PixelCallback(), Orientation::kIdentity)); |
59 | 0 | (*cparams.debug_image)(cparams.debug_image_opaque, label, image.xsize(), |
60 | 0 | image.ysize(), &color, pixels.data()); |
61 | 0 | return true; |
62 | 0 | } Unexecuted instantiation: enc_debug_image.cc:jxl::Status jxl::(anonymous namespace)::DumpImageT<float>(jxl::CompressParams const&, char const*, jxl::ColorEncoding const&, jxl::Image3<float> const&) Unexecuted instantiation: enc_debug_image.cc:jxl::Status jxl::(anonymous namespace)::DumpImageT<unsigned char>(jxl::CompressParams const&, char const*, jxl::ColorEncoding const&, jxl::Image3<unsigned char> const&) |
63 | | |
64 | | template <typename T> |
65 | | Status DumpPlaneNormalizedT(const CompressParams& cparams, const char* label, |
66 | 0 | const Plane<T>& image) { |
67 | 0 | T min; |
68 | 0 | T max; |
69 | 0 | ImageMinMax(image, &min, &max); |
70 | 0 | JxlMemoryManager* memory_manager = image.memory_manager(); |
71 | |
|
72 | 0 | JXL_ASSIGN_OR_RETURN( |
73 | 0 | Image3B normalized, |
74 | 0 | Image3B::Create(memory_manager, image.xsize(), image.ysize())); |
75 | 0 | for (size_t c = 0; c < 3; ++c) { |
76 | 0 | float mul = min == max ? 0 : (255.0f / (max - min)); |
77 | 0 | for (size_t y = 0; y < image.ysize(); ++y) { |
78 | 0 | const T* JXL_RESTRICT row_in = image.ConstRow(y); |
79 | 0 | uint8_t* JXL_RESTRICT row_out = normalized.PlaneRow(c, y); |
80 | 0 | for (size_t x = 0; x < image.xsize(); ++x) { |
81 | 0 | row_out[x] = static_cast<uint8_t>((row_in[x] - min) * mul); |
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return DumpImageT(cparams, label, ColorEncoding::SRGB(), normalized); |
86 | 0 | } Unexecuted instantiation: enc_debug_image.cc:jxl::Status jxl::(anonymous namespace)::DumpPlaneNormalizedT<float>(jxl::CompressParams const&, char const*, jxl::Plane<float> const&) Unexecuted instantiation: enc_debug_image.cc:jxl::Status jxl::(anonymous namespace)::DumpPlaneNormalizedT<unsigned char>(jxl::CompressParams const&, char const*, jxl::Plane<unsigned char> const&) |
87 | | |
88 | | } // namespace |
89 | | |
90 | | Status DumpImage(const CompressParams& cparams, const char* label, |
91 | 0 | const Image3<float>& image) { |
92 | 0 | return DumpImageT(cparams, label, ColorEncoding::SRGB(), image); |
93 | 0 | } |
94 | | |
95 | | Status DumpImage(const CompressParams& cparams, const char* label, |
96 | 0 | const Image3<uint8_t>& image) { |
97 | 0 | return DumpImageT(cparams, label, ColorEncoding::SRGB(), image); |
98 | 0 | } |
99 | | |
100 | | Status DumpXybImage(const CompressParams& cparams, const char* label, |
101 | 0 | const Image3F& image) { |
102 | 0 | if (!cparams.debug_image) return true; |
103 | 0 | JxlMemoryManager* memory_manager = image.memory_manager(); |
104 | |
|
105 | 0 | JXL_ASSIGN_OR_RETURN( |
106 | 0 | Image3F linear, |
107 | 0 | Image3F::Create(memory_manager, image.xsize(), image.ysize())); |
108 | 0 | OpsinParams opsin_params; |
109 | 0 | opsin_params.Init(kDefaultIntensityTarget); |
110 | 0 | JXL_RETURN_IF_ERROR( |
111 | 0 | OpsinToLinear(image, Rect(linear), nullptr, &linear, opsin_params)); |
112 | | |
113 | 0 | return DumpImageT(cparams, label, ColorEncoding::LinearSRGB(), linear); |
114 | 0 | } |
115 | | |
116 | | Status DumpPlaneNormalized(const CompressParams& cparams, const char* label, |
117 | 0 | const Plane<float>& image) { |
118 | 0 | return DumpPlaneNormalizedT(cparams, label, image); |
119 | 0 | } |
120 | | |
121 | | Status DumpPlaneNormalized(const CompressParams& cparams, const char* label, |
122 | 0 | const Plane<uint8_t>& image) { |
123 | 0 | return DumpPlaneNormalizedT(cparams, label, image); |
124 | 0 | } |
125 | | |
126 | | } // namespace jxl |