/src/libheif/libheif/image-items/mask_image.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF mask image codec. |
3 | | * |
4 | | * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com> |
5 | | * Copyright (c) 2023 Brad Hards <bradh@frogmouth.net> |
6 | | * |
7 | | * This file is part of libheif. |
8 | | * |
9 | | * libheif is free software: you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as |
11 | | * published by the Free Software Foundation, either version 3 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * libheif is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <cstdint> |
24 | | #include <cstring> |
25 | | #include <algorithm> |
26 | | #include <map> |
27 | | #include <cstring> |
28 | | |
29 | | #include "libheif/heif.h" |
30 | | #include "logging.h" |
31 | | #include "mask_image.h" |
32 | | #include "image_item.h" |
33 | | #include "security_limits.h" |
34 | | |
35 | | |
36 | | Error Box_mskC::parse(BitstreamRange& range, const heif_security_limits* limits) |
37 | 206 | { |
38 | 206 | parse_full_box_header(range); |
39 | 206 | m_bits_per_pixel = range.read8(); |
40 | 206 | return range.get_error(); |
41 | 206 | } |
42 | | |
43 | | void Box_mskC::dump(std::ostream& sstr, Indent& indent, bool full_log) const |
44 | 0 | { |
45 | 0 | Box::dump(sstr, indent, full_log); |
46 | 0 | sstr << indent << "bits_per_pixel: " << ((int)m_bits_per_pixel) << "\n"; |
47 | 0 | return; |
48 | 0 | } |
49 | | |
50 | | Error Box_mskC::write(StreamWriter& writer) const |
51 | 0 | { |
52 | 0 | size_t box_start = reserve_box_header_space(writer); |
53 | 0 | writer.write8(m_bits_per_pixel); |
54 | 0 | prepend_header(writer, box_start); |
55 | 0 | return Error::Ok; |
56 | 0 | } |
57 | | |
58 | | |
59 | | Error MaskImageCodec::decode_mask_image(const HeifContext* context, |
60 | | heif_item_id ID, |
61 | | std::shared_ptr<HeifPixelImage>& img, |
62 | | const std::vector<uint8_t>& data) |
63 | 0 | { |
64 | 0 | auto image = context->get_image(ID, false); |
65 | 0 | if (!image) { |
66 | 0 | return {heif_error_Invalid_input, |
67 | 0 | heif_suberror_Nonexisting_item_referenced}; |
68 | 0 | } |
69 | | |
70 | 0 | std::shared_ptr<Box_ispe> ispe = image->get_property<Box_ispe>(); |
71 | 0 | std::shared_ptr<Box_mskC> mskC = image->get_property<Box_mskC>(); |
72 | |
|
73 | 0 | if (!ispe || !mskC) { |
74 | 0 | return Error(heif_error_Unsupported_feature, |
75 | 0 | heif_suberror_Unsupported_data_version, |
76 | 0 | "Missing required box for mask codec"); |
77 | 0 | } |
78 | | |
79 | 0 | uint32_t width = ispe->get_width(); |
80 | 0 | uint32_t height = ispe->get_height(); |
81 | |
|
82 | 0 | Error error = check_for_valid_image_size(context->get_security_limits(), width, height); |
83 | 0 | if (error) { |
84 | 0 | return error; |
85 | 0 | } |
86 | | |
87 | 0 | if ((mskC->get_bits_per_pixel() != 8) && (mskC->get_bits_per_pixel() != 16)) |
88 | 0 | { |
89 | 0 | return Error(heif_error_Unsupported_feature, |
90 | 0 | heif_suberror_Unsupported_data_version, |
91 | 0 | "Unsupported bit depth for mask item"); |
92 | 0 | } |
93 | | |
94 | 0 | uint32_t bytes_per_pixel = (mskC->get_bits_per_pixel() + 7) / 8; |
95 | |
|
96 | 0 | if (data.size() / (static_cast<size_t>(width) * bytes_per_pixel) < height) { |
97 | 0 | return {heif_error_Invalid_input, |
98 | 0 | heif_suberror_Unspecified, |
99 | 0 | "Mask image data is too short"}; |
100 | 0 | } |
101 | | |
102 | 0 | img = std::make_shared<HeifPixelImage>(); |
103 | 0 | img->create(width, height, heif_colorspace_monochrome, heif_chroma_monochrome); |
104 | 0 | auto err = img->add_channel(heif_channel_Y, width, height, mskC->get_bits_per_pixel(), |
105 | 0 | context->get_security_limits()); |
106 | 0 | if (err) { |
107 | 0 | return err; |
108 | 0 | } |
109 | | |
110 | 0 | size_t stride; |
111 | 0 | uint8_t* dst = img->get_channel_memory(heif_channel_Y, &stride); |
112 | 0 | if (stride == static_cast<size_t>(width) * bytes_per_pixel) { |
113 | 0 | memcpy(dst, data.data(), static_cast<size_t>(width) * bytes_per_pixel * height); |
114 | 0 | } |
115 | 0 | else |
116 | 0 | { |
117 | 0 | for (size_t i = 0; i < height; i++) |
118 | 0 | { |
119 | 0 | memcpy(dst + i * stride, data.data() + i * width * bytes_per_pixel, width * bytes_per_pixel); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | return Error::Ok; |
123 | 0 | } |
124 | | |
125 | | |
126 | | Result<std::shared_ptr<HeifPixelImage>> ImageItem_mask::decode_compressed_image(const heif_decoding_options& options, |
127 | | bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0, |
128 | | DecodeTraversalState decode_state) const |
129 | 2 | { |
130 | 2 | std::shared_ptr<HeifPixelImage> img; |
131 | | |
132 | 2 | std::vector<uint8_t> data; |
133 | | |
134 | | // image data, usually from 'mdat' |
135 | | |
136 | 2 | Error error = get_file()->append_data_from_iloc(get_id(), data); |
137 | 2 | if (error) { |
138 | 2 | return error; |
139 | 2 | } |
140 | | |
141 | 0 | Error err = MaskImageCodec::decode_mask_image(get_context(), |
142 | 0 | get_id(), |
143 | 0 | img, |
144 | 0 | data); |
145 | 0 | if (err) { |
146 | 0 | return err; |
147 | 0 | } |
148 | 0 | else { |
149 | 0 | return img; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | |
154 | | Result<Encoder::CodedImageData> ImageItem_mask::encode(const std::shared_ptr<HeifPixelImage>& image, |
155 | | heif_encoder* encoder, |
156 | | const heif_encoding_options& options, |
157 | | heif_image_input_class input_class) |
158 | 0 | { |
159 | 0 | Encoder::CodedImageData codedImageData; |
160 | |
|
161 | 0 | if (image->get_colorspace() != heif_colorspace_monochrome) |
162 | 0 | { |
163 | 0 | return Error(heif_error_Unsupported_feature, |
164 | 0 | heif_suberror_Unsupported_data_version, |
165 | 0 | "Unsupported colourspace for mask region"); |
166 | 0 | } |
167 | | |
168 | 0 | if (image->get_bits_per_pixel(heif_channel_Y) != 8) |
169 | 0 | { |
170 | 0 | return Error(heif_error_Unsupported_feature, |
171 | 0 | heif_suberror_Unsupported_data_version, |
172 | 0 | "Unsupported bit depth for mask region"); |
173 | 0 | } |
174 | | |
175 | | // TODO: we could add an option to lossless-compress this data |
176 | 0 | std::vector<uint8_t> data; |
177 | 0 | size_t src_stride; |
178 | 0 | uint8_t* src_data = image->get_channel_memory(heif_channel_Y, &src_stride); |
179 | |
|
180 | 0 | uint32_t w = image->get_width(); |
181 | 0 | uint32_t h = image->get_height(); |
182 | |
|
183 | 0 | data.resize(w * h); |
184 | |
|
185 | 0 | if (w == (uint32_t)src_stride) { |
186 | 0 | codedImageData.append(src_data, w*h); |
187 | 0 | } |
188 | 0 | else { |
189 | 0 | for (uint32_t y = 0; y < h; y++) { |
190 | 0 | codedImageData.append(src_data + y * src_stride, w); |
191 | 0 | } |
192 | 0 | } |
193 | |
|
194 | 0 | std::shared_ptr<Box_mskC> mskC = std::make_shared<Box_mskC>(); |
195 | 0 | uint16_t bpp = image->get_bits_per_pixel(heif_channel_Y); |
196 | 0 | if (bpp > 255) { |
197 | 0 | return Error{ |
198 | 0 | heif_error_Invalid_input, |
199 | 0 | heif_suberror_Unspecified, |
200 | 0 | "Too many bits per pixel for mask image." |
201 | 0 | }; |
202 | 0 | } |
203 | 0 | mskC->set_bits_per_pixel(static_cast<uint8_t>(bpp)); |
204 | 0 | codedImageData.properties.push_back(mskC); |
205 | |
|
206 | 0 | return codedImageData; |
207 | 0 | } |
208 | | |
209 | | |
210 | | int ImageItem_mask::get_luma_bits_per_pixel_unlocked() const |
211 | 19 | { |
212 | 19 | auto mskC = get_property<Box_mskC>(); |
213 | 19 | if (!mskC) { |
214 | 19 | return -1; |
215 | 19 | } |
216 | | |
217 | 0 | return mskC->get_bits_per_pixel(); |
218 | 19 | } |