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