| 1 |
/* |
| 2 |
libheif example application. |
| 3 |
This file is part of heif-dec, an example application using libheif. |
| 4 |
|
| 5 |
MIT License |
| 6 |
|
| 7 |
Copyright (c) 2018 struktur AG, Joachim Bauch <[email protected]> |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in all |
| 17 |
copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 |
SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <cstring> |
| 30 |
|
| 31 |
#include "encoder.h" |
| 32 |
|
| 33 |
static const char kMetadataTypeExif[] = "Exif"; |
| 34 |
|
| 35 |
// static |
| 36 |
bool Encoder::HasExifMetaData(const struct heif_image_handle* handle) |
| 37 |
{ |
| 38 |
|
| 39 |
heif_item_id metadata_id; |
| 40 |
int count = heif_image_handle_get_list_of_metadata_block_IDs(handle, kMetadataTypeExif, |
| 41 |
&metadata_id, 1); |
| 42 |
return count > 0; |
| 43 |
} |
| 44 |
|
| 45 |
// static |
| 46 |
uint8_t* Encoder::GetExifMetaData(const struct heif_image_handle* handle, size_t* size) |
| 47 |
{ |
| 48 |
heif_item_id metadata_id; |
| 49 |
int count = heif_image_handle_get_list_of_metadata_block_IDs(handle, kMetadataTypeExif, |
| 50 |
&metadata_id, 1); |
| 51 |
|
| 52 |
for (int i = 0; i < count; i++) { |
| 53 |
size_t datasize = heif_image_handle_get_metadata_size(handle, metadata_id); |
| 54 |
uint8_t* data = static_cast<uint8_t*>(malloc(datasize)); |
| 55 |
if (!data) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
heif_error error = heif_image_handle_get_metadata(handle, metadata_id, data); |
| 60 |
if (error.code != heif_error_Ok) { |
| 61 |
free(data); |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
*size = datasize; |
| 66 |
return data; |
| 67 |
} |
| 68 |
|
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
std::vector<uint8_t> Encoder::get_xmp_metadata(const struct heif_image_handle* handle) |
| 74 |
{ |
| 75 |
std::vector<uint8_t> xmp; |
| 76 |
|
| 77 |
heif_item_id metadata_ids[16]; |
| 78 |
int count = heif_image_handle_get_list_of_metadata_block_IDs(handle, nullptr, metadata_ids, 16); |
| 79 |
|
| 80 |
for (int i = 0; i < count; i++) { |
| 81 |
if (strcmp(heif_image_handle_get_metadata_type(handle, metadata_ids[i]), "mime") == 0 && |
| 82 |
strcmp(heif_image_handle_get_metadata_content_type(handle, metadata_ids[i]), "application/rdf+xml") == 0) { |
| 83 |
|
| 84 |
size_t datasize = heif_image_handle_get_metadata_size(handle, metadata_ids[i]); |
| 85 |
xmp.resize(datasize); |
| 86 |
|
| 87 |
heif_error error = heif_image_handle_get_metadata(handle, metadata_ids[i], xmp.data()); |
| 88 |
if (error.code != heif_error_Ok) { |
| 89 |
// TODO: return error |
| 90 |
return {}; |
| 91 |
} |
| 92 |
|
| 93 |
return xmp; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return {}; |
| 98 |
} |