Debian Codegraph @

examples/heif_test.cc

raw
1 /*
2 libheif example application "heif-test".
3
4 MIT License
5
6 Copyright (c) 2017 Dirk Farin <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 #include <errno.h>
28 #include <string.h>
29
30 #if defined(HAVE_UNISTD_H)
31
32 #include <unistd.h>
33
34 #else
35 #define STDOUT_FILENO 1
36 #endif
37
38 #include <libheif/heif_cxx.h>
39
40 #include <fstream>
41 #include <iostream>
42 #include <memory>
43 #include <getopt.h>
44 #include <assert.h>
45 #include "common.h"
46
47
48 static struct option long_options[] = {
49 //{"write-raw", required_argument, 0, 'w' },
50 //{"output", required_argument, 0, 'o' },
51 {(char* const) "decode-img", required_argument, 0, 'd'},
52 {(char* const) "metadata", required_argument, 0, 'm'},
53 {(char* const) "version", required_argument, 0, 'v'},
54 {0, 0, 0, 0}
55 };
56
57 void show_help(const char* argv0)
58 {
59 fprintf(stderr, " heif-test libheif version: %s\n", heif_get_version());
60 fprintf(stderr, "------------------------------------\n");
61 fprintf(stderr, "usage: heif-test [options] image.heic\n");
62 fprintf(stderr, "\n");
63 fprintf(stderr, "options:\n");
64 fprintf(stderr, " -d, --decode-img ID decode image and output raw pixel data of all planes\n");
65 fprintf(stderr, " -m, --metadata ID output metadata\n");
66 fprintf(stderr, " -h, --help show help\n");
67 fprintf(stderr, " -v, --version show version\n");
68 }
69
70
71 std::pair<heif_item_id, heif_item_id> parse_id_pair(const std::string& s)
72 {
73 std::string::size_type p = s.find_first_of(':');
74 if (p == std::string::npos) {
75 fprintf(stderr, "id pair has to be in this format: 'ID:ID'\n");
76 exit(1);
77 }
78
79 std::pair<heif_item_id, heif_item_id> pair;
80 pair.first = atoi(s.substr(0, p).c_str());
81 pair.second = atoi(s.substr(p + 1).c_str());
82 return pair;
83 }
84
85
86 int main(int argc, char** argv)
87 {
88 std::vector<heif_item_id> image_IDs;
89 std::vector<std::pair<heif_item_id, heif_item_id>> metadata_IDs; // first: image, second: metadata
90
91 while (true) {
92 int option_index = 0;
93 int c = getopt_long(argc, argv, "d:m:hv", long_options, &option_index);
94 if (c == -1)
95 break;
96
97 switch (c) {
98 case 'd':
99 image_IDs.push_back(atoi(optarg));
100 break;
101 case 'm':
102 metadata_IDs.push_back(parse_id_pair(optarg));
103 break;
104 case 'h':
105 show_help(argv[0]);
106 return 0;
107 case 'v':
108 heif_examples::show_version();
109 return 0;
110 }
111 }
112
113 if (optind != argc - 1) {
114 show_help(argv[0]);
115 return 0;
116 }
117
118
119 const char* input_filename = argv[optind];
120
121 // ==============================================================================
122
123 try {
124 heif::Context ctx;
125 ctx.read_from_file(input_filename);
126
127
128 // --- dump images
129
130 for (auto id : image_IDs) {
131 heif::ImageHandle handle = ctx.get_image_handle(id);
132
133 heif::Image img = handle.decode_image(heif_colorspace_undefined, heif_chroma_undefined);
134
135 std::vector<heif_channel> channel_candidates{
136 heif_channel_Y,
137 heif_channel_Cb,
138 heif_channel_Cr,
139 heif_channel_R,
140 heif_channel_G,
141 heif_channel_B,
142 heif_channel_Alpha,
143 heif_channel_interleaved
144 };
145
146 for (heif_channel channel : channel_candidates) {
147 if (img.has_channel(channel)) {
148 int width = img.get_width(channel);
149 int height = img.get_height(channel);
150 int bytes = (img.get_bits_per_pixel(channel) + 7) / 8;
151
152 size_t stride;
153 const uint8_t* p = img.get_plane2(channel, &stride);
154 for (int y = 0; y < height; y++) {
155 fwrite(p + y * stride, width, bytes, stdout);
156 }
157 }
158 }
159 }
160
161
162 // --- dump metadata
163
164 for (auto idpair : metadata_IDs) {
165 heif::ImageHandle handle = ctx.get_image_handle(idpair.first);
166 std::vector<uint8_t> data = handle.get_metadata(idpair.second);
167 fwrite(data.data(), data.size(), 1, stdout);
168 }
169 }
170 catch (const heif::Error& err) {
171 std::cerr << err.get_message() << "\n";
172 }
173
174 return 0;
175 }