/src/libheif/libheif/codecs/jpeg2000_enc.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "jpeg2000_enc.h" |
22 | | #include "jpeg2000_boxes.h" |
23 | | #include "error.h" |
24 | | #include "context.h" |
25 | | #include "api_structs.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | |
30 | | Result<Encoder::CodedImageData> Encoder_JPEG2000::encode(const std::shared_ptr<HeifPixelImage>& image, |
31 | | struct heif_encoder* encoder, |
32 | | const struct heif_encoding_options& options, |
33 | | enum heif_image_input_class input_class) |
34 | 0 | { |
35 | 0 | Encoder::CodedImageData codedImageData; |
36 | |
|
37 | 0 | heif_image c_api_image; |
38 | 0 | c_api_image.image = image; |
39 | |
|
40 | 0 | encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
41 | | |
42 | | // get compressed data |
43 | 0 | for (;;) { |
44 | 0 | uint8_t* data; |
45 | 0 | int size; |
46 | |
|
47 | 0 | encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr); |
48 | |
|
49 | 0 | if (data == nullptr) { |
50 | 0 | break; |
51 | 0 | } |
52 | | |
53 | 0 | codedImageData.append(data, size); |
54 | 0 | } |
55 | | |
56 | | // add 'j2kH' property |
57 | 0 | auto j2kH = std::make_shared<Box_j2kH>(); |
58 | | |
59 | | // add 'cdef' to 'j2kH' |
60 | 0 | auto cdef = std::make_shared<Box_cdef>(); |
61 | 0 | cdef->set_channels(image->get_colorspace()); |
62 | 0 | j2kH->append_child_box(cdef); |
63 | |
|
64 | 0 | codedImageData.properties.push_back(j2kH); |
65 | |
|
66 | 0 | codedImageData.codingConstraints.intra_pred_used = false; |
67 | 0 | codedImageData.codingConstraints.all_ref_pics_intra = true; |
68 | 0 | codedImageData.codingConstraints.max_ref_per_pic = 0; |
69 | |
|
70 | 0 | return codedImageData; |
71 | 0 | } |
72 | | |
73 | | |
74 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_JPEG2000::get_sample_description_box(const CodedImageData& data) const |
75 | 0 | { |
76 | 0 | auto j2ki = std::make_shared<Box_j2ki>(); |
77 | 0 | j2ki->get_VisualSampleEntry().compressorname = "JPEG2000"; |
78 | |
|
79 | 0 | for (auto prop : data.properties) { |
80 | 0 | if (prop->get_short_type() == fourcc("j2kH")) { |
81 | 0 | j2ki->append_child_box(prop); |
82 | 0 | return j2ki; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | 0 | assert(false); // no hvcC generated |
87 | 0 | return nullptr; |
88 | 0 | } |
89 | | |
90 | | |
91 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_HTJ2K::get_sample_description_box(const CodedImageData& data) const |
92 | 0 | { |
93 | 0 | auto j2ki = std::make_shared<Box_j2ki>(); |
94 | 0 | j2ki->get_VisualSampleEntry().compressorname = "HTJ2K"; |
95 | |
|
96 | 0 | for (auto prop : data.properties) { |
97 | 0 | if (prop->get_short_type() == fourcc("j2kH")) { |
98 | 0 | j2ki->append_child_box(prop); |
99 | 0 | return j2ki; |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | assert(false); // no hvcC generated |
104 | 0 | return nullptr; |
105 | 0 | } |