/src/libheif/libheif/codecs/jpeg2000_enc.cc
Line | Count | Source |
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 | 0 | heif_error err; |
37 | |
|
38 | 0 | heif_image c_api_image; |
39 | 0 | c_api_image.image = image; |
40 | |
|
41 | 0 | err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class);\ |
42 | 0 | if (err.code != heif_error_Ok) { |
43 | 0 | return Error::from_heif_error(err); |
44 | 0 | } |
45 | | |
46 | | // get compressed data |
47 | 0 | for (;;) { |
48 | 0 | uint8_t* data; |
49 | 0 | int size; |
50 | |
|
51 | 0 | err = encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr); |
52 | 0 | if (err.code != heif_error_Ok) { |
53 | 0 | return Error::from_heif_error(err); |
54 | 0 | } |
55 | | |
56 | 0 | if (data == nullptr) { |
57 | 0 | break; |
58 | 0 | } |
59 | | |
60 | 0 | codedImageData.append(data, size); |
61 | 0 | } |
62 | | |
63 | | // add 'j2kH' property |
64 | 0 | auto j2kH = std::make_shared<Box_j2kH>(); |
65 | | |
66 | | // add 'cdef' to 'j2kH' |
67 | 0 | auto cdef = std::make_shared<Box_cdef>(); |
68 | 0 | cdef->set_channels(image->get_colorspace()); |
69 | 0 | j2kH->append_child_box(cdef); |
70 | |
|
71 | 0 | codedImageData.properties.push_back(j2kH); |
72 | |
|
73 | 0 | codedImageData.codingConstraints.intra_pred_used = false; |
74 | 0 | codedImageData.codingConstraints.all_ref_pics_intra = true; |
75 | 0 | codedImageData.codingConstraints.max_ref_per_pic = 0; |
76 | |
|
77 | 0 | return codedImageData; |
78 | 0 | } |
79 | | |
80 | | |
81 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_JPEG2000::get_sample_description_box(const CodedImageData& data) const |
82 | 0 | { |
83 | 0 | auto j2ki = std::make_shared<Box_j2ki>(); |
84 | 0 | j2ki->get_VisualSampleEntry().compressorname = "JPEG2000"; |
85 | |
|
86 | 0 | for (auto prop : data.properties) { |
87 | 0 | if (prop->get_short_type() == fourcc("j2kH")) { |
88 | 0 | j2ki->append_child_box(prop); |
89 | 0 | return j2ki; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | 0 | assert(false); // no hvcC generated |
94 | 0 | return nullptr; |
95 | 0 | } |
96 | | |
97 | | |
98 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_HTJ2K::get_sample_description_box(const CodedImageData& data) const |
99 | 0 | { |
100 | 0 | auto j2ki = std::make_shared<Box_j2ki>(); |
101 | 0 | j2ki->get_VisualSampleEntry().compressorname = "HTJ2K"; |
102 | |
|
103 | 0 | for (auto prop : data.properties) { |
104 | 0 | if (prop->get_short_type() == fourcc("j2kH")) { |
105 | 0 | j2ki->append_child_box(prop); |
106 | 0 | return j2ki; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 0 | assert(false); // no hvcC generated |
111 | 0 | return nullptr; |
112 | 0 | } |