/src/libjxl/lib/jxl/image_bundle.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/image_bundle.h" |
7 | | |
8 | | #include <utility> |
9 | | |
10 | | #include "lib/jxl/base/printf_macros.h" |
11 | | #include "lib/jxl/base/status.h" |
12 | | #include "lib/jxl/image.h" |
13 | | |
14 | | namespace jxl { |
15 | | |
16 | 0 | Status ImageBundle::ShrinkTo(size_t xsize, size_t ysize) { |
17 | 0 | if (HasColor()) { |
18 | 0 | JXL_RETURN_IF_ERROR(color_.ShrinkTo(xsize, ysize)); |
19 | 0 | } |
20 | 0 | for (ImageF& ec : extra_channels_) { |
21 | 0 | JXL_RETURN_IF_ERROR(ec.ShrinkTo(xsize, ysize)); |
22 | 0 | } |
23 | 0 | return true; |
24 | 0 | } |
25 | | |
26 | | // Called by all other SetFrom*. |
27 | | Status ImageBundle::SetFromImage(Image3F&& color, |
28 | 59.1k | const ColorEncoding& c_current) { |
29 | 59.1k | JXL_ENSURE(color.xsize() != 0 && color.ysize() != 0); |
30 | 59.1k | JXL_ENSURE(metadata_->color_encoding.IsGray() == c_current.IsGray()); |
31 | 59.1k | color_ = std::move(color); |
32 | 59.1k | c_current_ = c_current; |
33 | 59.1k | JXL_RETURN_IF_ERROR(VerifySizes()); |
34 | 59.1k | return true; |
35 | 59.1k | } |
36 | | |
37 | 0 | Status ImageBundle::VerifyMetadata() const { |
38 | 0 | JXL_ENSURE(!c_current_.ICC().empty()); |
39 | 0 | JXL_ENSURE(metadata_->color_encoding.IsGray() == IsGray()); |
40 | | |
41 | 0 | if (metadata_->HasAlpha()) { |
42 | 0 | const ImageF* a = alpha(); |
43 | 0 | if (a->xsize() == 0) { |
44 | 0 | return JXL_UNREACHABLE("MD alpha_bits %u IB alpha %" PRIuS " x %" PRIuS |
45 | 0 | "\n", |
46 | 0 | metadata_->GetAlphaBits(), a->xsize(), a->ysize()); |
47 | 0 | } |
48 | 0 | } |
49 | 0 | const uint32_t alpha_bits = metadata_->GetAlphaBits(); |
50 | 0 | JXL_ENSURE(alpha_bits <= 32); |
51 | | |
52 | | // metadata_->num_extra_channels may temporarily differ from |
53 | | // extra_channels_.size(), e.g. after SetAlpha. They are synced by the next |
54 | | // call to VisitFields. |
55 | 0 | return true; |
56 | 0 | } |
57 | | |
58 | 59.1k | Status ImageBundle::VerifySizes() const { |
59 | 59.1k | const size_t xs = xsize(); |
60 | 59.1k | const size_t ys = ysize(); |
61 | | |
62 | 59.1k | if (HasExtraChannels()) { |
63 | 0 | JXL_ENSURE(xs != 0 && ys != 0); |
64 | 0 | for (const ImageF& ec : extra_channels_) { |
65 | 0 | JXL_ENSURE(ec.xsize() == xs); |
66 | 0 | JXL_ENSURE(ec.ysize() == ys); |
67 | 0 | } |
68 | 0 | } |
69 | 59.1k | return true; |
70 | 59.1k | } |
71 | | |
72 | 0 | size_t ImageBundle::DetectRealBitdepth() const { |
73 | 0 | return metadata_->bit_depth.bits_per_sample; |
74 | | |
75 | | // TODO(lode): let this function return lower bit depth if possible, e.g. |
76 | | // return 8 bits in case the original image came from a 16-bit PNG that |
77 | | // was in fact representable as 8-bit PNG. Ensure that the implementation |
78 | | // returns 16 if e.g. two consecutive 16-bit values appeared in the original |
79 | | // image (such as 32768 and 32769), take into account that e.g. the values |
80 | | // 3-bit can represent is not a superset of the values 2-bit can represent, |
81 | | // and there may be slight imprecisions in the floating point image. |
82 | 0 | } |
83 | | |
84 | 0 | const ImageF* ImageBundle::black() const { |
85 | 0 | if (!HasBlack()) return nullptr; |
86 | 0 | const size_t ec = metadata_->Find(ExtraChannel::kBlack) - |
87 | 0 | metadata_->extra_channel_info.data(); |
88 | 0 | JXL_DASSERT(ec < extra_channels_.size()); |
89 | 0 | return &extra_channels_[ec]; |
90 | 0 | } |
91 | 0 | const ImageF* ImageBundle::alpha() const { |
92 | 0 | if (!HasAlpha()) return nullptr; |
93 | 0 | const size_t ec = metadata_->Find(ExtraChannel::kAlpha) - |
94 | 0 | metadata_->extra_channel_info.data(); |
95 | 0 | JXL_DASSERT(ec < extra_channels_.size()); |
96 | 0 | return &extra_channels_[ec]; |
97 | 0 | } |
98 | 0 | ImageF* ImageBundle::alpha() { |
99 | 0 | if (!HasAlpha()) return nullptr; |
100 | 0 | const size_t ec = metadata_->Find(ExtraChannel::kAlpha) - |
101 | 0 | metadata_->extra_channel_info.data(); |
102 | 0 | JXL_DASSERT(ec < extra_channels_.size()); |
103 | 0 | return &extra_channels_[ec]; |
104 | 0 | } |
105 | | |
106 | 0 | Status ImageBundle::SetAlpha(ImageF&& alpha) { |
107 | 0 | const ExtraChannelInfo* eci = metadata_->Find(ExtraChannel::kAlpha); |
108 | | // Must call SetAlphaBits first, otherwise we don't know which channel index |
109 | 0 | JXL_ENSURE(eci != nullptr); |
110 | 0 | JXL_ENSURE(alpha.xsize() != 0 && alpha.ysize() != 0); |
111 | 0 | if (extra_channels_.size() < metadata_->extra_channel_info.size()) { |
112 | | // TODO(jon): get rid of this case |
113 | 0 | extra_channels_.insert( |
114 | 0 | extra_channels_.begin() + (eci - metadata_->extra_channel_info.data()), |
115 | 0 | std::move(alpha)); |
116 | 0 | } else { |
117 | 0 | extra_channels_[eci - metadata_->extra_channel_info.data()] = |
118 | 0 | std::move(alpha); |
119 | 0 | } |
120 | | // num_extra_channels is automatically set in visitor |
121 | 0 | JXL_RETURN_IF_ERROR(VerifySizes()); |
122 | 0 | return true; |
123 | 0 | } |
124 | | |
125 | 0 | Status ImageBundle::SetExtraChannels(std::vector<ImageF>&& extra_channels) { |
126 | 0 | for (const ImageF& plane : extra_channels) { |
127 | 0 | JXL_ENSURE(plane.xsize() != 0 && plane.ysize() != 0); |
128 | 0 | } |
129 | 0 | extra_channels_ = std::move(extra_channels); |
130 | 0 | JXL_RETURN_IF_ERROR(VerifySizes()); |
131 | 0 | return true; |
132 | 0 | } |
133 | | } // namespace jxl |