| 1 |
/* |
| 2 |
libheif example application "heif". |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2024 Joachim Bauch <[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 <cstring> |
| 28 |
#include <iostream> |
| 29 |
#include <memory> |
| 30 |
#include <utility> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
extern "C" { |
| 34 |
#include <tiff.h> |
| 35 |
#include <tiffio.h> |
| 36 |
} |
| 37 |
|
| 38 |
#include "decoder_tiff.h" |
| 39 |
|
| 40 |
static struct heif_error heif_error_ok = {heif_error_Ok, heif_suberror_Unspecified, "Success"}; |
| 41 |
|
| 42 |
static bool seekTIFF(TIFF* tif, toff_t offset, int whence) { |
| 43 |
TIFFSeekProc seekProc = TIFFGetSeekProc(tif); |
| 44 |
if (!seekProc) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
thandle_t handle = TIFFClientdata(tif); |
| 49 |
if (!handle) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
return seekProc(handle, offset, whence) != static_cast<toff_t>(-1); |
| 54 |
} |
| 55 |
|
| 56 |
static bool readTIFF(TIFF* tif, void* dest, size_t size) { |
| 57 |
TIFFReadWriteProc readProc = TIFFGetReadProc(tif); |
| 58 |
if (!readProc) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
thandle_t handle = TIFFClientdata(tif); |
| 63 |
if (!handle) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
tmsize_t result = readProc(handle, dest, size); |
| 68 |
if (result < 0 || static_cast<size_t>(result) != size) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static bool readTIFFUint16(TIFF* tif, uint16_t* dest) { |
| 76 |
if (!readTIFF(tif, &dest, 2)) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
if (TIFFIsByteSwapped(tif)) { |
| 81 |
TIFFSwabShort(dest); |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
static bool readTIFFUint32(TIFF* tif, uint32_t* dest) { |
| 87 |
if (!readTIFF(tif, &dest, 4)) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
if (TIFFIsByteSwapped(tif)) { |
| 92 |
TIFFSwabLong(dest); |
| 93 |
} |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
class ExifTags { |
| 98 |
public: |
| 99 |
~ExifTags() = default; |
| 100 |
|
| 101 |
void Encode(std::vector<uint8_t>* dest); |
| 102 |
|
| 103 |
static std::unique_ptr<ExifTags> Parse(TIFF* tif); |
| 104 |
|
| 105 |
private: |
| 106 |
class Tag { |
| 107 |
public: |
| 108 |
uint16_t tag; |
| 109 |
uint16_t type; |
| 110 |
uint32_t len; |
| 111 |
|
| 112 |
uint32_t offset; |
| 113 |
std::vector<uint8_t> data; |
| 114 |
}; |
| 115 |
|
| 116 |
ExifTags(uint16_t count); |
| 117 |
void writeUint16(std::vector<uint8_t>* dest, uint16_t value); |
| 118 |
void writeUint32(std::vector<uint8_t>* dest, uint32_t value); |
| 119 |
void writeUint32(std::vector<uint8_t>* dest, size_t pos, uint32_t value); |
| 120 |
void writeData(std::vector<uint8_t>* dest, const std::vector<uint8_t>& value); |
| 121 |
|
| 122 |
std::vector<std::unique_ptr<Tag>> tags_; |
| 123 |
}; |
| 124 |
|
| 125 |
ExifTags::ExifTags(uint16_t count) { |
| 126 |
tags_.reserve(count); |
| 127 |
} |
| 128 |
|
| 129 |
// static |
| 130 |
std::unique_ptr<ExifTags> ExifTags::Parse(TIFF* tif) { |
| 131 |
toff_t exif_offset; |
| 132 |
if (!TIFFGetField(tif, TIFFTAG_EXIFIFD, &exif_offset)) { |
| 133 |
// Image doesn't contain EXIF data. |
| 134 |
return nullptr; |
| 135 |
} |
| 136 |
|
| 137 |
if (!seekTIFF(tif, exif_offset, SEEK_SET)) { |
| 138 |
return nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
uint16_t count; |
| 142 |
if (!readTIFFUint16(tif, &count)) { |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
|
| 146 |
if (count == 0) { |
| 147 |
return nullptr; |
| 148 |
} |
| 149 |
|
| 150 |
std::unique_ptr<ExifTags> tags(new ExifTags(count)); |
| 151 |
for (uint16_t i = 0; i < count; i++) { |
| 152 |
std::unique_ptr<Tag> tag(new Tag()); |
| 153 |
if (!readTIFFUint16(tif, &tag->tag)) { |
| 154 |
return nullptr; |
| 155 |
} |
| 156 |
|
| 157 |
if (!readTIFFUint16(tif, &tag->type) || tag->type > TIFF_IFD8) { |
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
if (TIFFDataWidth(static_cast<TIFFDataType>(tag->type)) == 0) { |
| 162 |
return nullptr; |
| 163 |
} |
| 164 |
|
| 165 |
if (!readTIFFUint32(tif, &tag->len)) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
|
| 169 |
if (!readTIFFUint32(tif, &tag->offset)) { |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
tags->tags_.push_back(std::move(tag)); |
| 174 |
} |
| 175 |
|
| 176 |
for (const auto& tag : tags->tags_) { |
| 177 |
size_t size = tag->len * TIFFDataWidth(static_cast<TIFFDataType>(tag->type)); |
| 178 |
if (size <= 4) { |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
if (!seekTIFF(tif, tag->offset, SEEK_SET)) { |
| 183 |
return nullptr; |
| 184 |
} |
| 185 |
|
| 186 |
tag->data.resize(size); |
| 187 |
if (!readTIFF(tif, tag->data.data(), size)) { |
| 188 |
return nullptr; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return tags; |
| 193 |
} |
| 194 |
|
| 195 |
void ExifTags::writeUint16(std::vector<uint8_t>* dest, uint16_t value) { |
| 196 |
dest->resize(dest->size() + sizeof(value)); |
| 197 |
void* d = dest->data() + dest->size() - sizeof(value); |
| 198 |
memcpy(d, &value, sizeof(value)); |
| 199 |
} |
| 200 |
|
| 201 |
void ExifTags::writeUint32(std::vector<uint8_t>* dest, uint32_t value) { |
| 202 |
dest->resize(dest->size() + sizeof(value)); |
| 203 |
writeUint32(dest, dest->size() - sizeof(value), value); |
| 204 |
} |
| 205 |
|
| 206 |
void ExifTags::writeUint32(std::vector<uint8_t>* dest, size_t pos, uint32_t value) { |
| 207 |
void* d = dest->data() + pos; |
| 208 |
memcpy(d, &value, sizeof(value)); |
| 209 |
} |
| 210 |
|
| 211 |
void ExifTags::writeData(std::vector<uint8_t>* dest, const std::vector<uint8_t>& value) { |
| 212 |
dest->resize(dest->size() + value.size()); |
| 213 |
void* d = dest->data() + dest->size() - value.size(); |
| 214 |
memcpy(d, value.data(), value.size()); |
| 215 |
} |
| 216 |
|
| 217 |
void ExifTags::Encode(std::vector<uint8_t>* dest) { |
| 218 |
if (tags_.empty()) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
#if HOST_BIGENDIAN |
| 223 |
dest->push_back('M'); |
| 224 |
dest->push_back('M'); |
| 225 |
#else |
| 226 |
dest->push_back('I'); |
| 227 |
dest->push_back('I'); |
| 228 |
#endif |
| 229 |
writeUint16(dest, 42); |
| 230 |
// Offset of IFD0. |
| 231 |
writeUint32(dest, 8); |
| 232 |
|
| 233 |
writeUint16(dest, static_cast<uint16_t>(tags_.size())); |
| 234 |
for (const auto& tag : tags_) { |
| 235 |
writeUint16(dest, tag->tag); |
| 236 |
writeUint16(dest, tag->type); |
| 237 |
writeUint32(dest, tag->len); |
| 238 |
writeUint32(dest, tag->offset); |
| 239 |
} |
| 240 |
// No IFD1 dictionary. |
| 241 |
writeUint32(dest, 0); |
| 242 |
|
| 243 |
// Update offsets of tags that have their data stored separately. |
| 244 |
for (size_t i = 0; i < tags_.size(); i++) { |
| 245 |
const auto& tag = tags_[i]; |
| 246 |
size_t size = tag->data.size(); |
| 247 |
if (size <= 4) { |
| 248 |
continue; |
| 249 |
} |
| 250 |
|
| 251 |
// StartOfTags + (TagIndex * sizeof(Tag)) + OffsetOfTagData |
| 252 |
size_t pos = 10 + (i * 12) + 8; |
| 253 |
size_t offset = dest->size(); |
| 254 |
writeUint32(dest, pos, static_cast<uint32_t>(offset)); |
| 255 |
writeData(dest, tag->data); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
heif_error getImageWidthAndHeight(TIFF *tif, uint32_t &width, uint32_t &height) |
| 260 |
{ |
| 261 |
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) || |
| 262 |
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height)) |
| 263 |
{ |
| 264 |
struct heif_error err = { |
| 265 |
.code = heif_error_Invalid_input, |
| 266 |
.subcode = heif_suberror_Unspecified, |
| 267 |
.message = "Can not read width and/or height from TIFF image."}; |
| 268 |
return err; |
| 269 |
} |
| 270 |
return heif_error_ok; |
| 271 |
} |
| 272 |
|
| 273 |
heif_error readMono(TIFF *tif, heif_image **image) |
| 274 |
{ |
| 275 |
uint32_t width, height; |
| 276 |
heif_error err = getImageWidthAndHeight(tif, width, height); |
| 277 |
if (err.code != heif_error_Ok) { |
| 278 |
return err; |
| 279 |
} |
| 280 |
err = heif_image_create((int) width, (int) height, heif_colorspace_monochrome, heif_chroma_monochrome, image); |
| 281 |
if (err.code != heif_error_Ok) { |
| 282 |
return err; |
| 283 |
} |
| 284 |
heif_image_add_plane(*image, heif_channel_Y, (int)width, (int)height, 8); |
| 285 |
|
| 286 |
size_t y_stride; |
| 287 |
uint8_t *py = heif_image_get_plane2(*image, heif_channel_Y, &y_stride); |
| 288 |
for (uint32_t row = 0; row < height; row++) |
| 289 |
{ |
| 290 |
TIFFReadScanline(tif, py, row, 0); |
| 291 |
py += y_stride; |
| 292 |
} |
| 293 |
return heif_error_ok; |
| 294 |
} |
| 295 |
|
| 296 |
heif_error readPixelInterleaveRGB(TIFF *tif, uint16_t samplesPerPixel, heif_image **image) |
| 297 |
{ |
| 298 |
uint32_t width, height; |
| 299 |
heif_error err = getImageWidthAndHeight(tif, width, height); |
| 300 |
if (err.code != heif_error_Ok) { |
| 301 |
return err; |
| 302 |
} |
| 303 |
heif_chroma chroma = heif_chroma_interleaved_RGB; |
| 304 |
if (samplesPerPixel == 4) { |
| 305 |
chroma = heif_chroma_interleaved_RGBA; |
| 306 |
} |
| 307 |
|
| 308 |
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, chroma, image); |
| 309 |
if (err.code != heif_error_Ok) |
| 310 |
{ |
| 311 |
return err; |
| 312 |
} |
| 313 |
heif_channel channel = heif_channel_interleaved; |
| 314 |
heif_image_add_plane(*image, channel, (int)width, (int)height, samplesPerPixel * 8); |
| 315 |
|
| 316 |
size_t y_stride; |
| 317 |
uint8_t *py = heif_image_get_plane2(*image, channel, &y_stride); |
| 318 |
|
| 319 |
tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif)); |
| 320 |
for (uint32_t row = 0; row < height; row++) |
| 321 |
{ |
| 322 |
TIFFReadScanline(tif, buf, row, 0); |
| 323 |
memcpy(py, buf, width * samplesPerPixel); |
| 324 |
py += y_stride; |
| 325 |
} |
| 326 |
_TIFFfree(buf); |
| 327 |
return heif_error_ok; |
| 328 |
} |
| 329 |
|
| 330 |
heif_error readPixelInterleave(TIFF *tif, uint16_t samplesPerPixel, heif_image **image) |
| 331 |
{ |
| 332 |
if (samplesPerPixel == 1) { |
| 333 |
return readMono(tif, image); |
| 334 |
} else { |
| 335 |
return readPixelInterleaveRGB(tif, samplesPerPixel, image); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
heif_error readBandInterleaveRGB(TIFF *tif, uint16_t samplesPerPixel, heif_image **image) |
| 340 |
{ |
| 341 |
uint32_t width, height; |
| 342 |
heif_error err = getImageWidthAndHeight(tif, width, height); |
| 343 |
if (err.code != heif_error_Ok) { |
| 344 |
return err; |
| 345 |
} |
| 346 |
if (samplesPerPixel == 3) { |
| 347 |
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, heif_chroma_interleaved_RGB, image); |
| 348 |
} else { |
| 349 |
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, image); |
| 350 |
} |
| 351 |
if (err.code != heif_error_Ok) { |
| 352 |
return err; |
| 353 |
} |
| 354 |
heif_channel channel = heif_channel_interleaved; |
| 355 |
heif_image_add_plane(*image, channel, (int)width, (int)height, samplesPerPixel * 8); |
| 356 |
|
| 357 |
size_t y_stride; |
| 358 |
uint8_t *py = heif_image_get_plane2(*image, channel, &y_stride); |
| 359 |
|
| 360 |
uint8_t *buf = static_cast<uint8_t *>(_TIFFmalloc(TIFFScanlineSize(tif))); |
| 361 |
for (uint16_t i = 0; i < samplesPerPixel; i++) |
| 362 |
{ |
| 363 |
uint8_t *dest = py + i; |
| 364 |
for (uint32_t row = 0; row < height; row++) |
| 365 |
{ |
| 366 |
TIFFReadScanline(tif, buf, row, i); |
| 367 |
for (uint32_t x = 0; x < width; x++, dest += samplesPerPixel) |
| 368 |
{ |
| 369 |
*dest = buf[x]; |
| 370 |
} |
| 371 |
dest += (y_stride - width * samplesPerPixel); |
| 372 |
} |
| 373 |
} |
| 374 |
_TIFFfree(buf); |
| 375 |
return heif_error_ok; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
heif_error readBandInterleave(TIFF *tif, uint16_t samplesPerPixel, heif_image **image) |
| 380 |
{ |
| 381 |
if (samplesPerPixel == 1) { |
| 382 |
return readMono(tif, image); |
| 383 |
} else if (samplesPerPixel == 3) { |
| 384 |
return readBandInterleaveRGB(tif, samplesPerPixel, image); |
| 385 |
} else if (samplesPerPixel == 4) { |
| 386 |
return readBandInterleaveRGB(tif, samplesPerPixel, image); |
| 387 |
} else { |
| 388 |
struct heif_error err = { |
| 389 |
.code = heif_error_Unsupported_feature, |
| 390 |
.subcode = heif_suberror_Unspecified, |
| 391 |
.message = "Only 1, 3 and 4 bands are supported"}; |
| 392 |
return err; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
static void suppress_warnings(const char* module, const char* fmt, va_list ap) { |
| 398 |
// Do nothing |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
heif_error loadTIFF(const char* filename, InputImage *input_image) { |
| 403 |
TIFFSetWarningHandler(suppress_warnings); |
| 404 |
|
| 405 |
std::unique_ptr<TIFF, void(*)(TIFF*)> tifPtr(TIFFOpen(filename, "r"), [](TIFF* tif) { TIFFClose(tif); }); |
| 406 |
if (!tifPtr) { |
| 407 |
struct heif_error err = { |
| 408 |
.code = heif_error_Invalid_input, |
| 409 |
.subcode = heif_suberror_Unspecified, |
| 410 |
.message = "Cannot open TIFF ile"}; |
| 411 |
return err; |
| 412 |
} |
| 413 |
|
| 414 |
TIFF* tif = tifPtr.get(); |
| 415 |
if (TIFFIsTiled(tif)) { |
| 416 |
struct heif_error err = { |
| 417 |
.code = heif_error_Unsupported_feature, |
| 418 |
.subcode = heif_suberror_Unspecified, |
| 419 |
.message = "Tiled TIFF images are not supported yet"}; |
| 420 |
return err; |
| 421 |
} |
| 422 |
|
| 423 |
uint16_t shortv, samplesPerPixel, bps, config, format; |
| 424 |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &shortv) && shortv == PHOTOMETRIC_PALETTE) { |
| 425 |
struct heif_error err = { |
| 426 |
.code = heif_error_Unsupported_feature, |
| 427 |
.subcode = heif_suberror_Unspecified, |
| 428 |
.message = "Palette TIFF images are not supported yet"}; |
| 429 |
return err; |
| 430 |
} |
| 431 |
|
| 432 |
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config); |
| 433 |
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel); |
| 434 |
if (samplesPerPixel != 1 && samplesPerPixel != 3 && samplesPerPixel != 4) { |
| 435 |
struct heif_error err = { |
| 436 |
.code = heif_error_Invalid_input, |
| 437 |
.subcode = heif_suberror_Unspecified, |
| 438 |
.message = "Only 1, 3 and 4 samples per pixel are supported."}; |
| 439 |
return err; |
| 440 |
} |
| 441 |
|
| 442 |
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps); |
| 443 |
if (bps != 8) { |
| 444 |
struct heif_error err = { |
| 445 |
.code = heif_error_Invalid_input, |
| 446 |
.subcode = heif_suberror_Unspecified, |
| 447 |
.message = "Only 8 bits per sample are supported."}; |
| 448 |
return err; |
| 449 |
} |
| 450 |
|
| 451 |
if (TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &format) && format != SAMPLEFORMAT_UINT) { |
| 452 |
struct heif_error err = { |
| 453 |
.code = heif_error_Invalid_input, |
| 454 |
.subcode = heif_suberror_Unspecified, |
| 455 |
.message = "Only UINT sample format is supported."}; |
| 456 |
return err; |
| 457 |
} |
| 458 |
|
| 459 |
struct heif_error err; |
| 460 |
struct heif_image* image = nullptr; |
| 461 |
|
| 462 |
switch (config) { |
| 463 |
case PLANARCONFIG_CONTIG: |
| 464 |
err = readPixelInterleave(tif, samplesPerPixel, &image); |
| 465 |
break; |
| 466 |
case PLANARCONFIG_SEPARATE: |
| 467 |
err = readBandInterleave(tif, samplesPerPixel, &image); |
| 468 |
break; |
| 469 |
default: |
| 470 |
struct heif_error err = { |
| 471 |
.code = heif_error_Invalid_input, |
| 472 |
.subcode = heif_suberror_Unspecified, |
| 473 |
.message = "Unsupported planar configuration"}; |
| 474 |
return err; |
| 475 |
} |
| 476 |
if (err.code != heif_error_Ok) { |
| 477 |
return err; |
| 478 |
} |
| 479 |
|
| 480 |
input_image->image = std::shared_ptr<heif_image>(image, |
| 481 |
[](heif_image* img) { heif_image_release(img); }); |
| 482 |
|
| 483 |
// Unfortunately libtiff doesn't provide a way to read a raw dictionary. |
| 484 |
// Therefore we manually parse the EXIF data, extract the tags and encode |
| 485 |
// them for use in the HEIF image. |
| 486 |
std::unique_ptr<ExifTags> tags = ExifTags::Parse(tif); |
| 487 |
if (tags) { |
| 488 |
tags->Encode(&(input_image->exif)); |
| 489 |
} |
| 490 |
return heif_error_ok; |
| 491 |
} |
| 492 |
|