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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
/*
libheif example application "heif".
MIT License
Copyright (c) 2024 Joachim Bauch <bauch@struktur.de>
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.
*/
#include <cstring>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
extern "C" {
#include <tiff.h>
#include <tiffio.h>
}
#include "decoder_tiff.h"
static struct heif_error heif_error_ok = {heif_error_Ok, heif_suberror_Unspecified, "Success"};
static bool seekTIFF(TIFF* tif, toff_t offset, int whence) {
TIFFSeekProc seekProc = TIFFGetSeekProc(tif);
if (!seekProc) {
return false;
}
thandle_t handle = TIFFClientdata(tif);
if (!handle) {
return false;
}
return seekProc(handle, offset, whence) != static_cast<toff_t>(-1);
}
static bool readTIFF(TIFF* tif, void* dest, size_t size) {
TIFFReadWriteProc readProc = TIFFGetReadProc(tif);
if (!readProc) {
return false;
}
thandle_t handle = TIFFClientdata(tif);
if (!handle) {
return false;
}
tmsize_t result = readProc(handle, dest, size);
if (result < 0 || static_cast<size_t>(result) != size) {
return false;
}
return true;
}
static bool readTIFFUint16(TIFF* tif, uint16_t* dest) {
if (!readTIFF(tif, &dest, 2)) {
return false;
}
if (TIFFIsByteSwapped(tif)) {
TIFFSwabShort(dest);
}
return true;
}
static bool readTIFFUint32(TIFF* tif, uint32_t* dest) {
if (!readTIFF(tif, &dest, 4)) {
return false;
}
if (TIFFIsByteSwapped(tif)) {
TIFFSwabLong(dest);
}
return true;
}
class ExifTags {
public:
~ExifTags() = default;
void Encode(std::vector<uint8_t>* dest);
static std::unique_ptr<ExifTags> Parse(TIFF* tif);
private:
class Tag {
public:
uint16_t tag;
uint16_t type;
uint32_t len;
uint32_t offset;
std::vector<uint8_t> data;
};
ExifTags(uint16_t count);
void writeUint16(std::vector<uint8_t>* dest, uint16_t value);
void writeUint32(std::vector<uint8_t>* dest, uint32_t value);
void writeUint32(std::vector<uint8_t>* dest, size_t pos, uint32_t value);
void writeData(std::vector<uint8_t>* dest, const std::vector<uint8_t>& value);
std::vector<std::unique_ptr<Tag>> tags_;
};
ExifTags::ExifTags(uint16_t count) {
tags_.reserve(count);
}
// static
std::unique_ptr<ExifTags> ExifTags::Parse(TIFF* tif) {
toff_t exif_offset;
if (!TIFFGetField(tif, TIFFTAG_EXIFIFD, &exif_offset)) {
// Image doesn't contain EXIF data.
return nullptr;
}
if (!seekTIFF(tif, exif_offset, SEEK_SET)) {
return nullptr;
}
uint16_t count;
if (!readTIFFUint16(tif, &count)) {
return nullptr;
}
if (count == 0) {
return nullptr;
}
std::unique_ptr<ExifTags> tags(new ExifTags(count));
for (uint16_t i = 0; i < count; i++) {
std::unique_ptr<Tag> tag(new Tag());
if (!readTIFFUint16(tif, &tag->tag)) {
return nullptr;
}
if (!readTIFFUint16(tif, &tag->type) || tag->type > TIFF_IFD8) {
return nullptr;
}
if (TIFFDataWidth(static_cast<TIFFDataType>(tag->type)) == 0) {
return nullptr;
}
if (!readTIFFUint32(tif, &tag->len)) {
return nullptr;
}
if (!readTIFFUint32(tif, &tag->offset)) {
return nullptr;
}
tags->tags_.push_back(std::move(tag));
}
for (const auto& tag : tags->tags_) {
size_t size = tag->len * TIFFDataWidth(static_cast<TIFFDataType>(tag->type));
if (size <= 4) {
continue;
}
if (!seekTIFF(tif, tag->offset, SEEK_SET)) {
return nullptr;
}
tag->data.resize(size);
if (!readTIFF(tif, tag->data.data(), size)) {
return nullptr;
}
}
return tags;
}
void ExifTags::writeUint16(std::vector<uint8_t>* dest, uint16_t value) {
dest->resize(dest->size() + sizeof(value));
void* d = dest->data() + dest->size() - sizeof(value);
memcpy(d, &value, sizeof(value));
}
void ExifTags::writeUint32(std::vector<uint8_t>* dest, uint32_t value) {
dest->resize(dest->size() + sizeof(value));
writeUint32(dest, dest->size() - sizeof(value), value);
}
void ExifTags::writeUint32(std::vector<uint8_t>* dest, size_t pos, uint32_t value) {
void* d = dest->data() + pos;
memcpy(d, &value, sizeof(value));
}
void ExifTags::writeData(std::vector<uint8_t>* dest, const std::vector<uint8_t>& value) {
dest->resize(dest->size() + value.size());
void* d = dest->data() + dest->size() - value.size();
memcpy(d, value.data(), value.size());
}
void ExifTags::Encode(std::vector<uint8_t>* dest) {
if (tags_.empty()) {
return;
}
#if HOST_BIGENDIAN
dest->push_back('M');
dest->push_back('M');
#else
dest->push_back('I');
dest->push_back('I');
#endif
writeUint16(dest, 42);
// Offset of IFD0.
writeUint32(dest, 8);
writeUint16(dest, static_cast<uint16_t>(tags_.size()));
for (const auto& tag : tags_) {
writeUint16(dest, tag->tag);
writeUint16(dest, tag->type);
writeUint32(dest, tag->len);
writeUint32(dest, tag->offset);
}
// No IFD1 dictionary.
writeUint32(dest, 0);
// Update offsets of tags that have their data stored separately.
for (size_t i = 0; i < tags_.size(); i++) {
const auto& tag = tags_[i];
size_t size = tag->data.size();
if (size <= 4) {
continue;
}
// StartOfTags + (TagIndex * sizeof(Tag)) + OffsetOfTagData
size_t pos = 10 + (i * 12) + 8;
size_t offset = dest->size();
writeUint32(dest, pos, static_cast<uint32_t>(offset));
writeData(dest, tag->data);
}
}
heif_error getImageWidthAndHeight(TIFF *tif, uint32_t &width, uint32_t &height)
{
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) ||
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height))
{
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Can not read width and/or height from TIFF image."};
return err;
}
return heif_error_ok;
}
heif_error readMono(TIFF *tif, heif_image **image)
{
uint32_t width, height;
heif_error err = getImageWidthAndHeight(tif, width, height);
if (err.code != heif_error_Ok) {
return err;
}
err = heif_image_create((int) width, (int) height, heif_colorspace_monochrome, heif_chroma_monochrome, image);
if (err.code != heif_error_Ok) {
return err;
}
heif_image_add_plane(*image, heif_channel_Y, (int)width, (int)height, 8);
size_t y_stride;
uint8_t *py = heif_image_get_plane2(*image, heif_channel_Y, &y_stride);
for (uint32_t row = 0; row < height; row++)
{
TIFFReadScanline(tif, py, row, 0);
py += y_stride;
}
return heif_error_ok;
}
heif_error readPixelInterleaveRGB(TIFF *tif, uint16_t samplesPerPixel, heif_image **image)
{
uint32_t width, height;
heif_error err = getImageWidthAndHeight(tif, width, height);
if (err.code != heif_error_Ok) {
return err;
}
heif_chroma chroma = heif_chroma_interleaved_RGB;
if (samplesPerPixel == 4) {
chroma = heif_chroma_interleaved_RGBA;
}
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, chroma, image);
if (err.code != heif_error_Ok)
{
return err;
}
heif_channel channel = heif_channel_interleaved;
heif_image_add_plane(*image, channel, (int)width, (int)height, samplesPerPixel * 8);
size_t y_stride;
uint8_t *py = heif_image_get_plane2(*image, channel, &y_stride);
tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
for (uint32_t row = 0; row < height; row++)
{
TIFFReadScanline(tif, buf, row, 0);
memcpy(py, buf, width * samplesPerPixel);
py += y_stride;
}
_TIFFfree(buf);
return heif_error_ok;
}
heif_error readPixelInterleave(TIFF *tif, uint16_t samplesPerPixel, heif_image **image)
{
if (samplesPerPixel == 1) {
return readMono(tif, image);
} else {
return readPixelInterleaveRGB(tif, samplesPerPixel, image);
}
}
heif_error readBandInterleaveRGB(TIFF *tif, uint16_t samplesPerPixel, heif_image **image)
{
uint32_t width, height;
heif_error err = getImageWidthAndHeight(tif, width, height);
if (err.code != heif_error_Ok) {
return err;
}
if (samplesPerPixel == 3) {
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, heif_chroma_interleaved_RGB, image);
} else {
err = heif_image_create((int)width, (int)height, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, image);
}
if (err.code != heif_error_Ok) {
return err;
}
heif_channel channel = heif_channel_interleaved;
heif_image_add_plane(*image, channel, (int)width, (int)height, samplesPerPixel * 8);
size_t y_stride;
uint8_t *py = heif_image_get_plane2(*image, channel, &y_stride);
uint8_t *buf = static_cast<uint8_t *>(_TIFFmalloc(TIFFScanlineSize(tif)));
for (uint16_t i = 0; i < samplesPerPixel; i++)
{
uint8_t *dest = py + i;
for (uint32_t row = 0; row < height; row++)
{
TIFFReadScanline(tif, buf, row, i);
for (uint32_t x = 0; x < width; x++, dest += samplesPerPixel)
{
*dest = buf[x];
}
dest += (y_stride - width * samplesPerPixel);
}
}
_TIFFfree(buf);
return heif_error_ok;
}
heif_error readBandInterleave(TIFF *tif, uint16_t samplesPerPixel, heif_image **image)
{
if (samplesPerPixel == 1) {
return readMono(tif, image);
} else if (samplesPerPixel == 3) {
return readBandInterleaveRGB(tif, samplesPerPixel, image);
} else if (samplesPerPixel == 4) {
return readBandInterleaveRGB(tif, samplesPerPixel, image);
} else {
struct heif_error err = {
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Only 1, 3 and 4 bands are supported"};
return err;
}
}
static void suppress_warnings(const char* module, const char* fmt, va_list ap) {
// Do nothing
}
heif_error loadTIFF(const char* filename, InputImage *input_image) {
TIFFSetWarningHandler(suppress_warnings);
std::unique_ptr<TIFF, void(*)(TIFF*)> tifPtr(TIFFOpen(filename, "r"), [](TIFF* tif) { TIFFClose(tif); });
if (!tifPtr) {
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Cannot open TIFF ile"};
return err;
}
TIFF* tif = tifPtr.get();
if (TIFFIsTiled(tif)) {
struct heif_error err = {
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Tiled TIFF images are not supported yet"};
return err;
}
uint16_t shortv, samplesPerPixel, bps, config, format;
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &shortv) && shortv == PHOTOMETRIC_PALETTE) {
struct heif_error err = {
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Palette TIFF images are not supported yet"};
return err;
}
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
if (samplesPerPixel != 1 && samplesPerPixel != 3 && samplesPerPixel != 4) {
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Only 1, 3 and 4 samples per pixel are supported."};
return err;
}
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps);
if (bps != 8) {
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Only 8 bits per sample are supported."};
return err;
}
if (TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &format) && format != SAMPLEFORMAT_UINT) {
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Only UINT sample format is supported."};
return err;
}
struct heif_error err;
struct heif_image* image = nullptr;
switch (config) {
case PLANARCONFIG_CONTIG:
err = readPixelInterleave(tif, samplesPerPixel, &image);
break;
case PLANARCONFIG_SEPARATE:
err = readBandInterleave(tif, samplesPerPixel, &image);
break;
default:
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Unsupported planar configuration"};
return err;
}
if (err.code != heif_error_Ok) {
return err;
}
input_image->image = std::shared_ptr<heif_image>(image,
[](heif_image* img) { heif_image_release(img); });
// Unfortunately libtiff doesn't provide a way to read a raw dictionary.
// Therefore we manually parse the EXIF data, extract the tags and encode
// them for use in the HEIF image.
std::unique_ptr<ExifTags> tags = ExifTags::Parse(tif);
if (tags) {
tags->Encode(&(input_image->exif));
}
return heif_error_ok;
}
|