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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
libheif thumbnailer for Gnome desktop.
MIT License
Copyright (c) 2018 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.
*/
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <string>
#include <iostream>
#include <cassert>
#include <memory>
#include <libheif/heif.h>
#include "heifio/encoder.h"
#if HAVE_LIBPNG
# include "heifio/encoder_png.h"
#include "common.h"
#endif
#if defined(_MSC_VER)
#include "getopt.h"
#endif
static int usage(const char* command)
{
fprintf(stderr, "usage: %s [-s size] [-p] <filename> <output>\n", command);
fprintf(stderr, " -p Render thumbnail from primary image, even if thumbnail is stored in image.\n");
return 1;
}
class LibHeifInitializer {
public:
LibHeifInitializer() { heif_init(nullptr); }
~LibHeifInitializer() { heif_deinit(); }
};
int main(int argc, char** argv)
{
// This takes care of initializing libheif and also deinitializing it at the end to free all resources.
LibHeifInitializer initializer;
int opt;
int size = 512; // default thumbnail size
bool thumbnail_from_primary_image_only = false;
while ((opt = getopt(argc, argv, "s:hpv")) != -1) {
switch (opt) {
case 's':
size = atoi(optarg);
break;
case 'p':
thumbnail_from_primary_image_only = true;
break;
case 'v':
heif_examples::show_version();
return 0;
case 'h':
default:
return usage(argv[0]);
}
}
if (optind + 2 > argc) {
// Need input and output filenames as additional arguments.
return usage(argv[0]);
}
std::string input_filename(argv[optind++]);
std::string output_filename(argv[optind++]);
// --- read heif file
std::shared_ptr<heif_context> context(heif_context_alloc(),
[](heif_context* c) { heif_context_free(c); });
struct heif_error err;
err = heif_context_read_from_file(context.get(), input_filename.c_str(), nullptr);
if (err.code != 0) {
std::cerr << "Could not read HEIF file: " << err.message << "\n";
return 1;
}
// --- get primary image
struct heif_image_handle* image_handle = NULL;
err = heif_context_get_primary_image_handle(context.get(), &image_handle);
if (err.code) {
std::cerr << "Could not read HEIF image : " << err.message << "\n";
return 1;
}
// --- if image has a thumbnail, use that instead
if (!thumbnail_from_primary_image_only) {
heif_item_id thumbnail_ID;
int nThumbnails = heif_image_handle_get_list_of_thumbnail_IDs(image_handle, &thumbnail_ID, 1);
if (nThumbnails > 0) {
struct heif_image_handle* thumbnail_handle;
err = heif_image_handle_get_thumbnail(image_handle, thumbnail_ID, &thumbnail_handle);
if (err.code) {
std::cerr << "Could not read HEIF image : " << err.message << "\n";
return 1;
}
// replace image handle with thumbnail handle
heif_image_handle_release(image_handle);
image_handle = thumbnail_handle;
}
}
// --- decode the image (or its thumbnail)
std::unique_ptr<Encoder> encoder(new PngEncoder());
struct heif_decoding_options* decode_options = heif_decoding_options_alloc();
encoder->UpdateDecodingOptions(image_handle, decode_options);
decode_options->convert_hdr_to_8bit = true;
int bit_depth = 8;
struct heif_image* image = NULL;
err = heif_decode_image(image_handle,
&image,
encoder->colorspace(false),
encoder->chroma(false, bit_depth),
decode_options);
if (err.code) {
std::cerr << "Could not decode HEIF image : " << err.message << "\n";
return 1;
}
assert(image);
// --- compute output thumbnail size
int input_width = heif_image_handle_get_width(image_handle);
int input_height = heif_image_handle_get_height(image_handle);
if (input_width > size || input_height > size) {
int thumbnail_width;
int thumbnail_height;
if (input_width > input_height) {
thumbnail_height = input_height * size / input_width;
thumbnail_width = size;
}
else if (input_height > 0) {
thumbnail_width = input_width * size / input_height;
thumbnail_height = size;
}
else {
thumbnail_width = thumbnail_height = 0;
}
if (thumbnail_width == 0 || thumbnail_height == 0) {
std::cerr << "Zero thumbnail output size\n";
return 1;
}
// --- output thumbnail smaller than HEIF thumbnail -> scale down
struct heif_image* scaled_image = NULL;
err = heif_image_scale_image(image, &scaled_image,
thumbnail_width, thumbnail_height,
NULL);
if (err.code) {
std::cerr << "Could not scale image : " << err.message << "\n";
return 1;
}
heif_image_release(image);
image = scaled_image;
}
// --- write thumbnail image to PNG
bool written = encoder->Encode(image_handle, image, output_filename.c_str());
if (!written) {
fprintf(stderr, "could not write image\n");
return 1;
}
heif_image_release(image);
heif_image_handle_release(image_handle);
return 0;
}
|