| 1 |
/* |
| 2 |
libheif example application "heif". |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2023 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 "common.h" |
| 28 |
#include "libheif/heif.h" |
| 29 |
#include <iostream> |
| 30 |
#include <fstream> |
| 31 |
#include <array> |
| 32 |
#include <vector> |
| 33 |
#include <algorithm> |
| 34 |
|
| 35 |
#include "common_utils.h" |
| 36 |
|
| 37 |
namespace heif_examples { |
| 38 |
void show_version() |
| 39 |
{ |
| 40 |
std::cout << LIBHEIF_VERSION << '\n' |
| 41 |
<< "libheif: " << heif_get_version() << '\n'; |
| 42 |
{ |
| 43 |
auto paths = heif_get_plugin_directories(); |
| 44 |
for (int i = 0; paths[i]; i++) { |
| 45 |
std::cout << "plugin path: " << paths[i] << '\n'; |
| 46 |
} |
| 47 |
|
| 48 |
if (paths[0] == nullptr) { |
| 49 |
std::cout << "plugin path: plugins are disabled\n"; |
| 50 |
} |
| 51 |
|
| 52 |
heif_free_plugin_directories(paths); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#define MAX_DECODERS 20 |
| 58 |
|
| 59 |
void list_decoders(heif_compression_format format) |
| 60 |
{ |
| 61 |
const heif_decoder_descriptor* decoders[MAX_DECODERS]; |
| 62 |
int n = heif_get_decoder_descriptors(format, decoders, MAX_DECODERS); |
| 63 |
|
| 64 |
for (int i = 0; i < n; i++) { |
| 65 |
const char* id = heif_decoder_descriptor_get_id_name(decoders[i]); |
| 66 |
if (id == nullptr) { |
| 67 |
id = "---"; |
| 68 |
} |
| 69 |
|
| 70 |
std::cout << "- " << id << " = " << heif_decoder_descriptor_get_name(decoders[i]) << "\n"; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
void list_all_decoders() |
| 76 |
{ |
| 77 |
std::cout << "AVC decoders:\n"; |
| 78 |
list_decoders(heif_compression_AVC); |
| 79 |
|
| 80 |
std::cout << "AVIF decoders:\n"; |
| 81 |
list_decoders(heif_compression_AV1); |
| 82 |
|
| 83 |
std::cout << "HEIC decoders:\n"; |
| 84 |
list_decoders(heif_compression_HEVC); |
| 85 |
|
| 86 |
std::cout << "JPEG decoders:\n"; |
| 87 |
list_decoders(heif_compression_JPEG); |
| 88 |
|
| 89 |
std::cout << "JPEG 2000 decoders:\n"; |
| 90 |
list_decoders(heif_compression_JPEG2000); |
| 91 |
|
| 92 |
std::cout << "JPEG 2000 (HT) decoders:\n"; |
| 93 |
list_decoders(heif_compression_HTJ2K); |
| 94 |
|
| 95 |
std::cout << "uncompressed:\n"; |
| 96 |
list_decoders(heif_compression_uncompressed); |
| 97 |
|
| 98 |
std::cout << "VVIC decoders:\n"; |
| 99 |
list_decoders(heif_compression_VVC); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
std::string fourcc_to_string(uint32_t fourcc) |
| 104 |
{ |
| 105 |
char s[5]; |
| 106 |
s[0] = static_cast<char>((fourcc >> 24) & 0xFF); |
| 107 |
s[1] = static_cast<char>((fourcc >> 16) & 0xFF); |
| 108 |
s[2] = static_cast<char>((fourcc >> 8) & 0xFF); |
| 109 |
s[3] = static_cast<char>((fourcc) & 0xFF); |
| 110 |
s[4] = 0; |
| 111 |
|
| 112 |
return s; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
int check_for_valid_input_HEIF_file(const std::string& input_filename) |
| 117 |
{ |
| 118 |
std::ifstream istr(input_filename.c_str(), std::ios_base::binary); |
| 119 |
if (istr.fail()) { |
| 120 |
fprintf(stderr, "Input file does not exist.\n"); |
| 121 |
return 10; |
| 122 |
} |
| 123 |
|
| 124 |
std::array<uint8_t, 4> length{}; |
| 125 |
istr.read((char*) length.data(), length.size()); |
| 126 |
uint32_t box_size = four_bytes_to_uint32(length[0], length[1], length[2], length[3]); |
| 127 |
if ((box_size < 16) || (box_size > 512)) { |
| 128 |
fprintf(stderr, "Input file does not appear to start with a valid box length."); |
| 129 |
if ((box_size & 0xFFFFFFF0) == 0xFFD8FFE0) { |
| 130 |
fprintf(stderr, " Possibly could be a JPEG file instead.\n"); |
| 131 |
} |
| 132 |
else { |
| 133 |
fprintf(stderr, "\n"); |
| 134 |
} |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
std::vector<uint8_t> ftyp_bytes(box_size); |
| 139 |
std::copy(length.begin(), length.end(), ftyp_bytes.begin()); |
| 140 |
istr.read((char*) ftyp_bytes.data() + 4, ftyp_bytes.size() - 4); |
| 141 |
|
| 142 |
heif_error filetype_check = heif_has_compatible_filetype(ftyp_bytes.data(), (int) ftyp_bytes.size()); |
| 143 |
if (filetype_check.code != heif_error_Ok) { |
| 144 |
fprintf(stderr, "Input file is not a supported format. %s\n", filetype_check.message); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
} |