| 1 |
/* |
| 2 |
libheif thumbnailer for Gnome desktop. |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2018 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 |
#if defined(HAVE_UNISTD_H) |
| 28 |
# include <unistd.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <string> |
| 32 |
#include <iostream> |
| 33 |
#include <cassert> |
| 34 |
#include <memory> |
| 35 |
#include <libheif/heif.h> |
| 36 |
#include "heifio/encoder.h" |
| 37 |
|
| 38 |
#if HAVE_LIBPNG |
| 39 |
# include "heifio/encoder_png.h" |
| 40 |
#include "common.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(_MSC_VER) |
| 44 |
#include "getopt.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
static int usage(const char* command) |
| 49 |
{ |
| 50 |
fprintf(stderr, "usage: %s [-s size] [-p] <filename> <output>\n", command); |
| 51 |
fprintf(stderr, " -p Render thumbnail from primary image, even if thumbnail is stored in image.\n"); |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
class LibHeifInitializer { |
| 57 |
public: |
| 58 |
LibHeifInitializer() { heif_init(nullptr); } |
| 59 |
~LibHeifInitializer() { heif_deinit(); } |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
int main(int argc, char** argv) |
| 64 |
{ |
| 65 |
// This takes care of initializing libheif and also deinitializing it at the end to free all resources. |
| 66 |
LibHeifInitializer initializer; |
| 67 |
|
| 68 |
int opt; |
| 69 |
int size = 512; // default thumbnail size |
| 70 |
bool thumbnail_from_primary_image_only = false; |
| 71 |
|
| 72 |
while ((opt = getopt(argc, argv, "s:hpv")) != -1) { |
| 73 |
switch (opt) { |
| 74 |
case 's': |
| 75 |
size = atoi(optarg); |
| 76 |
break; |
| 77 |
case 'p': |
| 78 |
thumbnail_from_primary_image_only = true; |
| 79 |
break; |
| 80 |
case 'v': |
| 81 |
heif_examples::show_version(); |
| 82 |
return 0; |
| 83 |
case 'h': |
| 84 |
default: |
| 85 |
return usage(argv[0]); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (optind + 2 > argc) { |
| 90 |
// Need input and output filenames as additional arguments. |
| 91 |
return usage(argv[0]); |
| 92 |
} |
| 93 |
|
| 94 |
std::string input_filename(argv[optind++]); |
| 95 |
std::string output_filename(argv[optind++]); |
| 96 |
|
| 97 |
|
| 98 |
// --- read heif file |
| 99 |
|
| 100 |
std::shared_ptr<heif_context> context(heif_context_alloc(), |
| 101 |
[](heif_context* c) { heif_context_free(c); }); |
| 102 |
|
| 103 |
struct heif_error err; |
| 104 |
err = heif_context_read_from_file(context.get(), input_filename.c_str(), nullptr); |
| 105 |
if (err.code != 0) { |
| 106 |
std::cerr << "Could not read HEIF file: " << err.message << "\n"; |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
// --- get primary image |
| 113 |
|
| 114 |
struct heif_image_handle* image_handle = NULL; |
| 115 |
err = heif_context_get_primary_image_handle(context.get(), &image_handle); |
| 116 |
if (err.code) { |
| 117 |
std::cerr << "Could not read HEIF image : " << err.message << "\n"; |
| 118 |
return 1; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
// --- if image has a thumbnail, use that instead |
| 123 |
|
| 124 |
if (!thumbnail_from_primary_image_only) { |
| 125 |
heif_item_id thumbnail_ID; |
| 126 |
int nThumbnails = heif_image_handle_get_list_of_thumbnail_IDs(image_handle, &thumbnail_ID, 1); |
| 127 |
if (nThumbnails > 0) { |
| 128 |
struct heif_image_handle* thumbnail_handle; |
| 129 |
err = heif_image_handle_get_thumbnail(image_handle, thumbnail_ID, &thumbnail_handle); |
| 130 |
if (err.code) { |
| 131 |
std::cerr << "Could not read HEIF image : " << err.message << "\n"; |
| 132 |
return 1; |
| 133 |
} |
| 134 |
|
| 135 |
// replace image handle with thumbnail handle |
| 136 |
|
| 137 |
heif_image_handle_release(image_handle); |
| 138 |
image_handle = thumbnail_handle; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
// --- decode the image (or its thumbnail) |
| 145 |
|
| 146 |
std::unique_ptr<Encoder> encoder(new PngEncoder()); |
| 147 |
|
| 148 |
struct heif_decoding_options* decode_options = heif_decoding_options_alloc(); |
| 149 |
encoder->UpdateDecodingOptions(image_handle, decode_options); |
| 150 |
decode_options->convert_hdr_to_8bit = true; |
| 151 |
|
| 152 |
int bit_depth = 8; |
| 153 |
|
| 154 |
struct heif_image* image = NULL; |
| 155 |
err = heif_decode_image(image_handle, |
| 156 |
&image, |
| 157 |
encoder->colorspace(false), |
| 158 |
encoder->chroma(false, bit_depth), |
| 159 |
decode_options); |
| 160 |
if (err.code) { |
| 161 |
std::cerr << "Could not decode HEIF image : " << err.message << "\n"; |
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
assert(image); |
| 166 |
|
| 167 |
// --- compute output thumbnail size |
| 168 |
|
| 169 |
int input_width = heif_image_handle_get_width(image_handle); |
| 170 |
int input_height = heif_image_handle_get_height(image_handle); |
| 171 |
|
| 172 |
if (input_width > size || input_height > size) { |
| 173 |
int thumbnail_width; |
| 174 |
int thumbnail_height; |
| 175 |
|
| 176 |
if (input_width > input_height) { |
| 177 |
thumbnail_height = input_height * size / input_width; |
| 178 |
thumbnail_width = size; |
| 179 |
} |
| 180 |
else if (input_height > 0) { |
| 181 |
thumbnail_width = input_width * size / input_height; |
| 182 |
thumbnail_height = size; |
| 183 |
} |
| 184 |
else { |
| 185 |
thumbnail_width = thumbnail_height = 0; |
| 186 |
} |
| 187 |
|
| 188 |
if (thumbnail_width == 0 || thumbnail_height == 0) { |
| 189 |
std::cerr << "Zero thumbnail output size\n"; |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
// --- output thumbnail smaller than HEIF thumbnail -> scale down |
| 195 |
|
| 196 |
struct heif_image* scaled_image = NULL; |
| 197 |
err = heif_image_scale_image(image, &scaled_image, |
| 198 |
thumbnail_width, thumbnail_height, |
| 199 |
NULL); |
| 200 |
if (err.code) { |
| 201 |
std::cerr << "Could not scale image : " << err.message << "\n"; |
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
heif_image_release(image); |
| 206 |
image = scaled_image; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
// --- write thumbnail image to PNG |
| 212 |
|
| 213 |
bool written = encoder->Encode(image_handle, image, output_filename.c_str()); |
| 214 |
if (!written) { |
| 215 |
fprintf(stderr, "could not write image\n"); |
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
heif_image_release(image); |
| 220 |
|
| 221 |
heif_image_handle_release(image_handle); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |