| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2022 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
#include "dng_jxl.h" |
| 10 |
|
| 11 |
/*****************************************************************************/ |
| 12 |
|
| 13 |
// libjxl headers. |
| 14 |
|
| 15 |
#include "jxl/encode_cxx.h" |
| 16 |
#include "jxl/decode_cxx.h" |
| 17 |
#include "jxl/resizable_parallel_runner_cxx.h" |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
#include "dng_abort_sniffer.h" |
| 22 |
#include "dng_area_task.h" |
| 23 |
#include "dng_bmff.h" |
| 24 |
#include "dng_color_space.h" |
| 25 |
#include "dng_file_stream.h" |
| 26 |
#include "dng_globals.h" |
| 27 |
#include "dng_host.h" |
| 28 |
#include "dng_image.h" |
| 29 |
#include "dng_info.h" |
| 30 |
#include "dng_memory_stream.h" |
| 31 |
#include "dng_negative.h" |
| 32 |
#include "dng_parse_utils.h" |
| 33 |
#include "dng_pixel_buffer.h" |
| 34 |
#include "dng_tag_codes.h" |
| 35 |
#include "dng_utils.h" |
| 36 |
#include "dng_xmp.h" |
| 37 |
#include "dng_xy_coord.h" |
| 38 |
|
| 39 |
#include <atomic> |
| 40 |
#include <memory> |
| 41 |
|
| 42 |
/*****************************************************************************/ |
| 43 |
|
| 44 |
#define qLogJXL (qDNGValidate && 0) |
| 45 |
|
| 46 |
/*****************************************************************************/ |
| 47 |
|
| 48 |
class jxl_memory_block |
| 49 |
{ |
| 50 |
|
| 51 |
public: |
| 52 |
|
| 53 |
dng_memory_allocator &fAllocator; |
| 54 |
|
| 55 |
void *fPhysicalBuffer = nullptr; |
| 56 |
|
| 57 |
void *fLogicalBuffer = nullptr; |
| 58 |
|
| 59 |
// fBlock is used for allocations up to 2 GB. |
| 60 |
|
| 61 |
AutoPtr<dng_memory_block> fBlock; |
| 62 |
|
| 63 |
// dng_memory_allocator::Malloc function is used for allocations above |
| 64 |
// 2 GB. |
| 65 |
|
| 66 |
bool fUseMalloc = false; |
| 67 |
|
| 68 |
public: |
| 69 |
|
| 70 |
jxl_memory_block (dng_memory_allocator &allocator, |
| 71 |
const uint64 bytesNeeded) |
| 72 |
: fAllocator (allocator) |
| 73 |
{ |
| 74 |
|
| 75 |
if (bytesNeeded > uint64 (0x80000000)) |
| 76 |
fUseMalloc = true; |
| 77 |
|
| 78 |
if (fUseMalloc) |
| 79 |
{ |
| 80 |
|
| 81 |
// See dng_memory_block::PhysicalSize. |
| 82 |
|
| 83 |
fPhysicalBuffer = allocator.Malloc (160u + bytesNeeded); |
| 84 |
|
| 85 |
fLogicalBuffer = (void *) DNG_ALIGN_SIMD (fPhysicalBuffer); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
else |
| 90 |
{ |
| 91 |
|
| 92 |
fBlock.Reset (allocator.Allocate (uint32 (bytesNeeded))); |
| 93 |
|
| 94 |
fPhysicalBuffer = fBlock->Buffer (); |
| 95 |
|
| 96 |
fLogicalBuffer = fPhysicalBuffer; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
~jxl_memory_block () |
| 103 |
{ |
| 104 |
|
| 105 |
if (fUseMalloc) |
| 106 |
fAllocator.Free (fPhysicalBuffer); |
| 107 |
|
| 108 |
fPhysicalBuffer = nullptr; |
| 109 |
|
| 110 |
fLogicalBuffer = nullptr; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
void * Buffer () |
| 115 |
{ |
| 116 |
return fLogicalBuffer; |
| 117 |
} |
| 118 |
|
| 119 |
}; |
| 120 |
|
| 121 |
/*****************************************************************************/ |
| 122 |
|
| 123 |
class dng_jxl_decoder_callback_data |
| 124 |
{ |
| 125 |
|
| 126 |
public: |
| 127 |
|
| 128 |
dng_pixel_buffer fTemplateBuffer; |
| 129 |
|
| 130 |
dng_pixel_buffer fWholeBuffer; |
| 131 |
|
| 132 |
AutoPtr<jxl_memory_block> fBlock; |
| 133 |
|
| 134 |
AutoPtr<dng_image> fImage; // 1 or 3 planes |
| 135 |
|
| 136 |
AutoPtr<dng_image> fAlphaMask; // 1 plane (or empty if no alpha) |
| 137 |
|
| 138 |
bool fAlphaPremultiplied = false; |
| 139 |
|
| 140 |
}; |
| 141 |
|
| 142 |
/*****************************************************************************/ |
| 143 |
|
| 144 |
static void dng_jxl_decoder_callback_func (void* opaque, |
| 145 |
size_t x, |
| 146 |
size_t y, |
| 147 |
size_t num_pixels, |
| 148 |
const void* pixels) |
| 149 |
{ |
| 150 |
|
| 151 |
#if qLogJXL && 0 |
| 152 |
|
| 153 |
printf ("dng_jxl_decoder_callback_func: %u, %u, %u\n", |
| 154 |
(unsigned) x, |
| 155 |
(unsigned) y, |
| 156 |
(unsigned) num_pixels); |
| 157 |
|
| 158 |
#endif |
| 159 |
|
| 160 |
// The decoder prepares a scanline or partial scanline of pixels for us to |
| 161 |
// copy elsewhere. |
| 162 |
|
| 163 |
auto *cbData = (dng_jxl_decoder_callback_data *) opaque; |
| 164 |
|
| 165 |
auto buffer = cbData->fTemplateBuffer; |
| 166 |
|
| 167 |
buffer.fArea.t = (int32) y; |
| 168 |
buffer.fArea.b = (int32) (y + 1); |
| 169 |
buffer.fArea.l = (int32) x; |
| 170 |
buffer.fArea.r = (int32) (x + num_pixels); |
| 171 |
|
| 172 |
buffer.fData = (void *) pixels; |
| 173 |
|
| 174 |
#if 0 |
| 175 |
|
| 176 |
// This is conceptually the right thing to do, but it's very inefficient. |
| 177 |
// Overall decode time in the functional/jxl/benchmark test balloons from |
| 178 |
// from 50 ms to 200 ms on my 10-core x64 iMac Pro. The issue is that the |
| 179 |
// decoder produces 1 (partial) scanline at a time, which results in two |
| 180 |
// problems: (1) we're issuing lots and lots of dng_image::Put calls, which |
| 181 |
// is in efficient because each Put needs to do at least 1 and often 2 |
| 182 |
// mutex locks to access the relevant tiles, and (2) a scanline (very |
| 183 |
// wide, minimal height) is a poor match for tiles in dng_image, which are |
| 184 |
// roughly square by design. |
| 185 |
|
| 186 |
cbData->fImage->Put (buffer); |
| 187 |
|
| 188 |
#else |
| 189 |
|
| 190 |
// Instead, copy to a temp buffer that is in dng_image-friendly planar |
| 191 |
// format. This copy performs the chunky-to-planar swizzle and hence the |
| 192 |
// swizzle overhead is covered by multi-threaded support. |
| 193 |
|
| 194 |
cbData->fWholeBuffer.CopyArea (buffer, |
| 195 |
buffer.fArea, |
| 196 |
0, // src plane |
| 197 |
0, // dst plane |
| 198 |
buffer.fPlanes); |
| 199 |
|
| 200 |
#endif |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/*****************************************************************************/ |
| 205 |
|
| 206 |
class dng_jxl_parallel_runner_data |
| 207 |
{ |
| 208 |
|
| 209 |
public: |
| 210 |
|
| 211 |
dng_host *fHost = nullptr; |
| 212 |
|
| 213 |
dng_atomic_error_code fErrorCode { dng_error_none }; |
| 214 |
|
| 215 |
}; |
| 216 |
|
| 217 |
/*****************************************************************************/ |
| 218 |
|
| 219 |
static void CheckResult (JxlDecoderStatus status, |
| 220 |
const char *msg, |
| 221 |
const dng_jxl_parallel_runner_data *data, |
| 222 |
const dng_error_code defaultErrorCode = dng_error_jxl_decoder) |
| 223 |
{ |
| 224 |
|
| 225 |
if (status != JXL_DEC_SUCCESS) |
| 226 |
{ |
| 227 |
|
| 228 |
#if qLogJXL |
| 229 |
|
| 230 |
printf ("JXL encoder (%s) error with status %d", |
| 231 |
msg, |
| 232 |
(int) status); |
| 233 |
|
| 234 |
#endif |
| 235 |
|
| 236 |
// Make a distinction between user cancellation and actual decoder |
| 237 |
// errors. |
| 238 |
|
| 239 |
if (data) |
| 240 |
{ |
| 241 |
|
| 242 |
dng_error_code code = data->fErrorCode; |
| 243 |
|
| 244 |
if (code != dng_error_none) |
| 245 |
{ |
| 246 |
|
| 247 |
if (code == dng_error_user_canceled) |
| 248 |
{ |
| 249 |
Throw_dng_error (code, "JXL decoder - user cancelled", msg); |
| 250 |
} |
| 251 |
|
| 252 |
else |
| 253 |
{ |
| 254 |
Throw_dng_error (code, "JXL decoder", msg); |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
Throw_dng_error (defaultErrorCode, "JXL decoder", msg); |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
/*****************************************************************************/ |
| 268 |
|
| 269 |
static void CheckResult (JxlEncoderStatus status, |
| 270 |
const char *msg, |
| 271 |
const dng_jxl_parallel_runner_data *data, |
| 272 |
const dng_error_code defaultErrorCode = dng_error_jxl_encoder) |
| 273 |
{ |
| 274 |
|
| 275 |
if (status != JXL_ENC_SUCCESS) |
| 276 |
{ |
| 277 |
|
| 278 |
#if qLogJXL |
| 279 |
|
| 280 |
printf ("JXL encoder (%s) error with status %d", |
| 281 |
msg, |
| 282 |
(int) status); |
| 283 |
|
| 284 |
#endif |
| 285 |
|
| 286 |
// Make a distinction between user cancellation and actual encoder |
| 287 |
// errors. |
| 288 |
|
| 289 |
if (data) |
| 290 |
{ |
| 291 |
|
| 292 |
dng_error_code code = data->fErrorCode; |
| 293 |
|
| 294 |
if (code != dng_error_none) |
| 295 |
{ |
| 296 |
|
| 297 |
if (code == dng_error_user_canceled) |
| 298 |
{ |
| 299 |
Throw_dng_error (code, "JXL encoder - user cancelled", msg); |
| 300 |
} |
| 301 |
|
| 302 |
else |
| 303 |
{ |
| 304 |
Throw_dng_error (code, "JXL encoder", msg); |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
Throw_dng_error (defaultErrorCode, "JXL encoder", msg); |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
/*****************************************************************************/ |
| 318 |
|
| 319 |
static void * dng_jxl_alloc (void *opaque, size_t size) |
| 320 |
{ |
| 321 |
|
| 322 |
#if 0 |
| 323 |
|
| 324 |
(void) opaque; |
| 325 |
|
| 326 |
return malloc (size); |
| 327 |
|
| 328 |
#else |
| 329 |
|
| 330 |
auto allocator = (dng_memory_allocator *) opaque; |
| 331 |
|
| 332 |
return allocator->Malloc (size); |
| 333 |
|
| 334 |
#endif |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
/*****************************************************************************/ |
| 339 |
|
| 340 |
static void dng_jxl_free (void *opaque, void *addr) |
| 341 |
{ |
| 342 |
|
| 343 |
#if 0 |
| 344 |
|
| 345 |
(void) opaque; |
| 346 |
|
| 347 |
free (addr); |
| 348 |
|
| 349 |
#else |
| 350 |
|
| 351 |
auto allocator = (dng_memory_allocator *) opaque; |
| 352 |
|
| 353 |
return allocator->Free (addr); |
| 354 |
|
| 355 |
#endif |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
/*****************************************************************************/ |
| 360 |
|
| 361 |
static JxlParallelRetCode dng_jxl_parallel_runner (void* runner_opaque, // dng_host |
| 362 |
void* jpegxl_opaque, |
| 363 |
JxlParallelRunInit init, |
| 364 |
JxlParallelRunFunction func, |
| 365 |
uint32_t start_range, |
| 366 |
uint32_t end_range) |
| 367 |
{ |
| 368 |
|
| 369 |
auto *data = (dng_jxl_parallel_runner_data *) runner_opaque; |
| 370 |
|
| 371 |
const size_t maxThreadCount = (size_t) data->fHost->PerformAreaTaskThreads (); |
| 372 |
|
| 373 |
JxlParallelRetCode init_ret = (*init) (jpegxl_opaque, |
| 374 |
maxThreadCount); |
| 375 |
|
| 376 |
if (init_ret != 0) |
| 377 |
return init_ret; |
| 378 |
|
| 379 |
auto &result = data->fErrorCode; |
| 380 |
|
| 381 |
try |
| 382 |
{ |
| 383 |
|
| 384 |
dng_range_parallel_task::Do |
| 385 |
(*data->fHost, |
| 386 |
dng_range_parallel_task::info ((int32) start_range, |
| 387 |
(int32) end_range, |
| 388 |
1, // min indices per thread |
| 389 |
(uint32) maxThreadCount), |
| 390 |
"dng_jxl_parallel_runner_task", |
| 391 |
[func, jpegxl_opaque] (const dng_range_parallel_task::range &ra) |
| 392 |
{ |
| 393 |
|
| 394 |
dng_abort_sniffer::SniffForAbort (ra.fSniffer); |
| 395 |
|
| 396 |
for (int32 i = ra.fBegin; i < ra.fEnd; i++) |
| 397 |
{ |
| 398 |
|
| 399 |
(*func) (jpegxl_opaque, i, (size_t) ra.fThreadIndex); |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
}); |
| 404 |
|
| 405 |
// Success. |
| 406 |
|
| 407 |
return 0; |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
// If something bad happened, at least remember the error code so that we |
| 412 |
// can report it later. |
| 413 |
|
| 414 |
catch (const dng_exception &except) |
| 415 |
{ |
| 416 |
|
| 417 |
dng_error_code codeError = except.ErrorCode (); |
| 418 |
dng_error_code codeNormal = dng_error_none; |
| 419 |
|
| 420 |
(void) result.compare_exchange_strong (codeNormal, |
| 421 |
codeError); |
| 422 |
|
| 423 |
} |
| 424 |
|
| 425 |
catch (...) |
| 426 |
{ |
| 427 |
|
| 428 |
dng_error_code codeError = dng_error_unknown; |
| 429 |
dng_error_code codeNormal = dng_error_none; |
| 430 |
|
| 431 |
(void) result.compare_exchange_strong (codeNormal, |
| 432 |
codeError); |
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
// Then tell libjxl that an error occurred, so it can stop running. |
| 437 |
|
| 438 |
return JXL_PARALLEL_RET_RUNNER_ERROR; |
| 439 |
|
| 440 |
} |
| 441 |
|
| 442 |
/*****************************************************************************/ |
| 443 |
|
| 444 |
class dng_jxl_io_buffer |
| 445 |
{ |
| 446 |
|
| 447 |
public: |
| 448 |
|
| 449 |
AutoPtr<dng_memory_block> fBlock; |
| 450 |
|
| 451 |
const size_t fMaxBufferSize; |
| 452 |
|
| 453 |
const size_t fChunkSize; |
| 454 |
|
| 455 |
size_t fDataSize = 0; |
| 456 |
|
| 457 |
size_t fIndex = 0; // start of block |
| 458 |
|
| 459 |
public: |
| 460 |
|
| 461 |
dng_jxl_io_buffer (dng_memory_allocator &allocator, |
| 462 |
size_t maxBufferSize, |
| 463 |
size_t chunkSize) |
| 464 |
|
| 465 |
: fMaxBufferSize (maxBufferSize) |
| 466 |
, fChunkSize (chunkSize) |
| 467 |
|
| 468 |
{ |
| 469 |
|
| 470 |
DNG_REQUIRE (fMaxBufferSize > 2 * fChunkSize, |
| 471 |
"invalid dng_jxl_io_buffer config"); |
| 472 |
|
| 473 |
fBlock.Reset (allocator.Allocate ((uint32) maxBufferSize)); |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
uint8 * Ptr () |
| 478 |
{ |
| 479 |
return fBlock->Buffer_uint8 () + fIndex; |
| 480 |
} |
| 481 |
|
| 482 |
size_t DataSize () const |
| 483 |
{ |
| 484 |
return fDataSize; |
| 485 |
} |
| 486 |
|
| 487 |
void Reset () |
| 488 |
{ |
| 489 |
fDataSize = 0; |
| 490 |
fIndex = 0; |
| 491 |
} |
| 492 |
|
| 493 |
void ReadChunk (dng_stream &stream, |
| 494 |
size_t remaining) |
| 495 |
{ |
| 496 |
|
| 497 |
size_t bytes_remaining_in_stream = |
| 498 |
(size_t) (stream.Length () - stream.Position ()); |
| 499 |
|
| 500 |
if (bytes_remaining_in_stream == 0) |
| 501 |
{ |
| 502 |
#if qLogJXL |
| 503 |
printf ("dng_jxl_io_buffer -- EOF reached in ReadChunk"); |
| 504 |
#endif |
| 505 |
ThrowEndOfFile ("dng_jxl_io_buffer"); |
| 506 |
} |
| 507 |
|
| 508 |
// This is the number of bytes we're going to try to read from the |
| 509 |
// stream. It cannot be more than the number of bytes available in |
| 510 |
// the stream. |
| 511 |
|
| 512 |
size_t next_read_size = std::min (fChunkSize, |
| 513 |
bytes_remaining_in_stream); |
| 514 |
|
| 515 |
// This is the number of bytes that jxl previously consumed from |
| 516 |
// our buffer. |
| 517 |
|
| 518 |
size_t prev_read_size = fDataSize - remaining; |
| 519 |
|
| 520 |
// Update our pointer past the previously-consumed bytes. |
| 521 |
|
| 522 |
fIndex += prev_read_size; |
| 523 |
|
| 524 |
// If reading the next chunk would spill beyond the end of our |
| 525 |
// buffer, then reset to the beginning of our buffer. We need to |
| 526 |
// move the remaining data. If our internal buffer is much larger |
| 527 |
// than the chunk size, then this will be relatively rare. |
| 528 |
|
| 529 |
if (fIndex + next_read_size > fMaxBufferSize) |
| 530 |
{ |
| 531 |
|
| 532 |
memmove (fBlock->Buffer (), |
| 533 |
fBlock->Buffer_uint8 () + fIndex, |
| 534 |
remaining); |
| 535 |
|
| 536 |
fIndex = 0; |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
// Read from the stream. |
| 541 |
|
| 542 |
auto ptr = Ptr () + remaining; |
| 543 |
|
| 544 |
stream.Get (ptr, (uint32) next_read_size); |
| 545 |
|
| 546 |
// Update the amount of data in our buffer. |
| 547 |
|
| 548 |
fDataSize = remaining + next_read_size; |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
}; |
| 553 |
|
| 554 |
/*****************************************************************************/ |
| 555 |
|
| 556 |
static void EnsureUseBoxes (JxlEncoder *enc, |
| 557 |
bool &once_flag) |
| 558 |
{ |
| 559 |
|
| 560 |
if (!once_flag) |
| 561 |
{ |
| 562 |
|
| 563 |
once_flag = true; |
| 564 |
|
| 565 |
CheckResult (JxlEncoderUseBoxes (enc), |
| 566 |
"JxlEncoderUseBoxes", |
| 567 |
nullptr); |
| 568 |
|
| 569 |
} |
| 570 |
|
| 571 |
} |
| 572 |
|
| 573 |
/*****************************************************************************/ |
| 574 |
|
| 575 |
// Move to operator== ? |
| 576 |
|
| 577 |
static bool SamePixelBufferGeometry (const dng_pixel_buffer &a, |
| 578 |
const dng_pixel_buffer &b) |
| 579 |
{ |
| 580 |
|
| 581 |
if (a.fArea != b.fArea) |
| 582 |
return false; |
| 583 |
|
| 584 |
if (a.fPlane != b.fPlane || |
| 585 |
a.fPlanes != b.fPlanes) |
| 586 |
return false; |
| 587 |
|
| 588 |
if (a.fRowStep != b.fRowStep || |
| 589 |
a.fColStep != b.fColStep || |
| 590 |
a.fPlaneStep != b.fPlaneStep) |
| 591 |
return false; |
| 592 |
|
| 593 |
if (a.fPixelType != b.fPixelType || |
| 594 |
a.fPixelSize != b.fPixelSize) |
| 595 |
return false; |
| 596 |
|
| 597 |
// Ignore the fData and fDirty fields. |
| 598 |
|
| 599 |
return true; |
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
/*****************************************************************************/ |
| 604 |
|
| 605 |
static void EncodeJXL (dng_host &host, |
| 606 |
dng_stream &stream, |
| 607 |
const dng_pixel_buffer &inBuffer, |
| 608 |
const dng_jxl_encode_settings &settings, |
| 609 |
const bool useContainer, |
| 610 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 611 |
const dng_metadata *metadata, |
| 612 |
const bool includeExif, |
| 613 |
const bool includeXMP, |
| 614 |
const bool includeIPTC, |
| 615 |
const dng_bmff_box_list *additionalBoxes) |
| 616 |
{ |
| 617 |
|
| 618 |
#if qDNGValidate && 0 |
| 619 |
dng_timer timerOuter ("EncodeJXL"); |
| 620 |
#endif |
| 621 |
|
| 622 |
dng_pixel_buffer buffer = inBuffer; |
| 623 |
|
| 624 |
const bool isLossless = (settings.Distance () <= 0.0f); |
| 625 |
|
| 626 |
uint32 &srcPixelType = buffer.fPixelType; |
| 627 |
|
| 628 |
DNG_REQUIRE (srcPixelType == ttByte || |
| 629 |
srcPixelType == ttShort || |
| 630 |
srcPixelType == ttHalfFloat || |
| 631 |
srcPixelType == ttFloat, |
| 632 |
"Unsupported srcPixelType in EncodeJXL"); |
| 633 |
|
| 634 |
if (srcPixelType == ttByte) |
| 635 |
DNG_REQUIRE (buffer.fPixelSize == 1, |
| 636 |
"unexpected ttByte pixel size"); |
| 637 |
|
| 638 |
else if (srcPixelType == ttShort) |
| 639 |
DNG_REQUIRE (buffer.fPixelSize == 2, |
| 640 |
"unexpected ttShort pixel size"); |
| 641 |
|
| 642 |
else if (srcPixelType == ttHalfFloat) |
| 643 |
DNG_REQUIRE (buffer.fPixelSize == 2, |
| 644 |
"unexpected ttHalfFloat pixel size"); |
| 645 |
|
| 646 |
else if (srcPixelType == ttFloat) |
| 647 |
{ |
| 648 |
|
| 649 |
DNG_REQUIRE (buffer.fPixelSize == 2 || |
| 650 |
buffer.fPixelSize == 4, |
| 651 |
"unexpected ttFlaot pixel size"); |
| 652 |
|
| 653 |
if (buffer.fPixelSize == 2) |
| 654 |
srcPixelType = ttHalfFloat; |
| 655 |
|
| 656 |
} |
| 657 |
|
| 658 |
//printf ("srcPixelType: %u\n", srcPixelType); |
| 659 |
//printf ("effort: %u\n", unsigned (settings.Effort ())); |
| 660 |
|
| 661 |
const uint32 planes = buffer.Planes (); |
| 662 |
|
| 663 |
DNG_REQUIRE (planes == 1 || // monochrome |
| 664 |
planes == 2 || // monochrome + alpha |
| 665 |
planes == 3 || // RGB |
| 666 |
planes == 4, // RGB + alpha |
| 667 |
"Unsupported plane count in EncodeJXL"); |
| 668 |
|
| 669 |
// Hook into our memory allocator. |
| 670 |
|
| 671 |
JxlMemoryManager memManager; |
| 672 |
|
| 673 |
memManager.opaque = (void *) &host.Allocator (); |
| 674 |
memManager.alloc = dng_jxl_alloc; |
| 675 |
memManager.free = dng_jxl_free; |
| 676 |
|
| 677 |
// Make encoder. |
| 678 |
|
| 679 |
auto encoder = JxlEncoderMake (&memManager); |
| 680 |
|
| 681 |
auto enc = encoder.get (); |
| 682 |
|
| 683 |
// Hook into our thread pool. |
| 684 |
|
| 685 |
dng_jxl_parallel_runner_data parallelData; |
| 686 |
|
| 687 |
parallelData.fHost = &host; |
| 688 |
|
| 689 |
if (!settings.UseSingleThread ()) |
| 690 |
{ |
| 691 |
|
| 692 |
#if qLogJXL |
| 693 |
printf ("Encoding using multiple threads\n"); |
| 694 |
#endif |
| 695 |
|
| 696 |
#if 0 |
| 697 |
|
| 698 |
auto nativeRunner = JxlResizableParallelRunnerMake (nullptr); |
| 699 |
|
| 700 |
const size_t maxThreadCount = (size_t) host.PerformAreaTaskThreads (); |
| 701 |
|
| 702 |
JxlResizableParallelRunnerSetThreads (nativeRunner.get (), |
| 703 |
maxThreadCount); |
| 704 |
|
| 705 |
CheckResult (JxlEncoderSetParallelRunner (enc, |
| 706 |
JxlResizableParallelRunner, |
| 707 |
nativeRunner.get ()), |
| 708 |
"JxlDecoderSetParallelRunner", |
| 709 |
¶llelData); |
| 710 |
|
| 711 |
(void) dng_jxl_parallel_runner; |
| 712 |
|
| 713 |
#else |
| 714 |
|
| 715 |
CheckResult (JxlEncoderSetParallelRunner (enc, |
| 716 |
dng_jxl_parallel_runner, |
| 717 |
(void *) ¶llelData), |
| 718 |
"JxlEncoderSetParallelRunner", |
| 719 |
¶llelData); |
| 720 |
|
| 721 |
#endif |
| 722 |
|
| 723 |
} |
| 724 |
|
| 725 |
#if qLogJXL |
| 726 |
|
| 727 |
else |
| 728 |
{ |
| 729 |
|
| 730 |
printf ("Encoding using 1 thread\n"); |
| 731 |
|
| 732 |
} |
| 733 |
|
| 734 |
#endif |
| 735 |
|
| 736 |
// Are we going to use a container? |
| 737 |
|
| 738 |
CheckResult |
| 739 |
(JxlEncoderUseContainer (enc, |
| 740 |
useContainer ? JXL_TRUE : JXL_FALSE), |
| 741 |
"JxlEncoderUseContainer", |
| 742 |
¶llelData); |
| 743 |
|
| 744 |
#if qLogJXL |
| 745 |
|
| 746 |
if (!useContainer) |
| 747 |
{ |
| 748 |
|
| 749 |
if (metadata && (includeExif || includeXMP || includeIPTC)) |
| 750 |
{ |
| 751 |
|
| 752 |
printf ("EncodeJXL called with useContainer==false" |
| 753 |
" -- ignoring metadata"); |
| 754 |
|
| 755 |
} |
| 756 |
|
| 757 |
} |
| 758 |
|
| 759 |
#endif // qLogJXL |
| 760 |
|
| 761 |
// Add metadata if needed. |
| 762 |
|
| 763 |
if (metadata && useContainer) |
| 764 |
{ |
| 765 |
|
| 766 |
bool useBoxesOnceFlag = false; |
| 767 |
|
| 768 |
bool didAddBox = false; |
| 769 |
|
| 770 |
// EXIF. |
| 771 |
|
| 772 |
if (includeExif) |
| 773 |
{ |
| 774 |
|
| 775 |
const dng_resolution *resolution = nullptr; |
| 776 |
|
| 777 |
AutoPtr<dng_memory_block> exifData |
| 778 |
(metadata->BuildExifBlock (host.Allocator (), |
| 779 |
resolution, |
| 780 |
false, |
| 781 |
nullptr, |
| 782 |
kNumLeadingZeroBytesForEXIF)); |
| 783 |
|
| 784 |
uint32 exifSize = exifData->LogicalSize (); |
| 785 |
|
| 786 |
if (exifSize) |
| 787 |
{ |
| 788 |
|
| 789 |
JxlBoxType type = { 'E', 'x', 'i', 'f' }; |
| 790 |
|
| 791 |
JXL_BOOL compressBox = JXL_FALSE; |
| 792 |
|
| 793 |
EnsureUseBoxes (enc, useBoxesOnceFlag); |
| 794 |
|
| 795 |
CheckResult (JxlEncoderAddBox (enc, |
| 796 |
type, |
| 797 |
exifData->Buffer_uint8 (), |
| 798 |
(size_t) exifData->LogicalSize (), |
| 799 |
compressBox), |
| 800 |
"JxlEncoderAddBox-Exif", |
| 801 |
¶llelData); |
| 802 |
|
| 803 |
didAddBox = true; |
| 804 |
|
| 805 |
} |
| 806 |
|
| 807 |
} // exif |
| 808 |
|
| 809 |
// XMP. |
| 810 |
|
| 811 |
if (includeXMP && metadata->GetXMP ()) |
| 812 |
{ |
| 813 |
|
| 814 |
// TODO(erichan): Serialize routine has a forJPEG parameter. Does |
| 815 |
// it make sense to use this for JXL? Hmm. |
| 816 |
|
| 817 |
AutoPtr<dng_memory_block> xmpBlock |
| 818 |
(metadata->GetXMP ()->Serialize (true)); |
| 819 |
|
| 820 |
uint32 xmpSize = xmpBlock->LogicalSize (); |
| 821 |
|
| 822 |
if (xmpSize > 0) |
| 823 |
{ |
| 824 |
|
| 825 |
JxlBoxType type = { 'x', 'm', 'l', ' ' }; |
| 826 |
|
| 827 |
// For now, leave XMP block uncompressed for broader |
| 828 |
// compatibility with other tools (e.g., exiftool) which |
| 829 |
// currently do not support compressed XMP. -erichan 2023-8-18 |
| 830 |
|
| 831 |
JXL_BOOL compressBox = JXL_FALSE; |
| 832 |
|
| 833 |
EnsureUseBoxes (enc, useBoxesOnceFlag); |
| 834 |
|
| 835 |
CheckResult (JxlEncoderAddBox (enc, |
| 836 |
type, |
| 837 |
xmpBlock->Buffer_uint8 (), |
| 838 |
(size_t) xmpSize, |
| 839 |
compressBox), |
| 840 |
"JxlEncoderAddBox-XMP", |
| 841 |
¶llelData); |
| 842 |
|
| 843 |
didAddBox = true; |
| 844 |
|
| 845 |
} |
| 846 |
|
| 847 |
} // xmp |
| 848 |
|
| 849 |
// IPTC. |
| 850 |
|
| 851 |
if (includeIPTC) |
| 852 |
{ |
| 853 |
|
| 854 |
auto iptcData = metadata->IPTCData (); |
| 855 |
auto iptcSize = metadata->IPTCLength (); |
| 856 |
|
| 857 |
if (iptcData && iptcSize) |
| 858 |
{ |
| 859 |
|
| 860 |
JxlBoxType type = { 'x', 'm', 'l', ' ' }; |
| 861 |
|
| 862 |
JXL_BOOL compressBox = JXL_FALSE; |
| 863 |
|
| 864 |
EnsureUseBoxes (enc, useBoxesOnceFlag); |
| 865 |
|
| 866 |
CheckResult (JxlEncoderAddBox (enc, |
| 867 |
type, |
| 868 |
(const uint8 *) iptcData, |
| 869 |
(size_t) iptcSize, |
| 870 |
compressBox), |
| 871 |
"JxlEncoderAddBox-IPTC", |
| 872 |
¶llelData); |
| 873 |
|
| 874 |
didAddBox = true; |
| 875 |
|
| 876 |
} |
| 877 |
|
| 878 |
} |
| 879 |
|
| 880 |
// Write additional boxes. |
| 881 |
|
| 882 |
if (additionalBoxes) |
| 883 |
{ |
| 884 |
|
| 885 |
for (const auto &box : *additionalBoxes) |
| 886 |
{ |
| 887 |
|
| 888 |
if (!box) |
| 889 |
continue; |
| 890 |
|
| 891 |
if (box->fName.Length () != 4) |
| 892 |
continue; |
| 893 |
|
| 894 |
if (!box->fContent) |
| 895 |
continue; |
| 896 |
|
| 897 |
const char *name = box->fName.Get (); |
| 898 |
|
| 899 |
JxlBoxType type = |
| 900 |
{ |
| 901 |
name [0], |
| 902 |
name [1], |
| 903 |
name [2], |
| 904 |
name [3] |
| 905 |
}; |
| 906 |
|
| 907 |
JXL_BOOL compressBox = JXL_FALSE; |
| 908 |
|
| 909 |
EnsureUseBoxes (enc, useBoxesOnceFlag); |
| 910 |
|
| 911 |
CheckResult (JxlEncoderAddBox (enc, |
| 912 |
type, |
| 913 |
(const uint8 *) box->fContent->Buffer (), |
| 914 |
(size_t) box->fContent->LogicalSize (), |
| 915 |
compressBox), |
| 916 |
"JxlEncoderAddBox-extra", |
| 917 |
¶llelData); |
| 918 |
|
| 919 |
didAddBox = true; |
| 920 |
|
| 921 |
} |
| 922 |
|
| 923 |
} |
| 924 |
|
| 925 |
// Tell the encoder there will be no more boxes added with |
| 926 |
// JxlEncoderAddBox. |
| 927 |
|
| 928 |
if (didAddBox) |
| 929 |
{ |
| 930 |
|
| 931 |
JxlEncoderCloseBoxes (enc); |
| 932 |
|
| 933 |
} |
| 934 |
|
| 935 |
} // metadata and container |
| 936 |
|
| 937 |
// Add color profile info. |
| 938 |
|
| 939 |
bool useJXLColorEncoding = false; |
| 940 |
|
| 941 |
JxlColorEncoding colorEncoding; |
| 942 |
|
| 943 |
if (colorSpaceInfo.fJxlColorEncoding.Get ()) |
| 944 |
{ |
| 945 |
|
| 946 |
// Provide color tag as special jxl color encoding. |
| 947 |
|
| 948 |
colorEncoding = *colorSpaceInfo.fJxlColorEncoding; |
| 949 |
|
| 950 |
useJXLColorEncoding = true; |
| 951 |
|
| 952 |
} |
| 953 |
|
| 954 |
else if (colorSpaceInfo.fICCProfile.Get ()) |
| 955 |
{ |
| 956 |
|
| 957 |
// Use ICC profile. |
| 958 |
|
| 959 |
} |
| 960 |
|
| 961 |
else |
| 962 |
{ |
| 963 |
|
| 964 |
// Fall back to assuming sRGB. |
| 965 |
|
| 966 |
memset (&colorEncoding, 0, sizeof (colorEncoding)); |
| 967 |
|
| 968 |
colorEncoding.color_space = JXL_COLOR_SPACE_RGB; |
| 969 |
colorEncoding.white_point = JXL_WHITE_POINT_D65; |
| 970 |
colorEncoding.primaries = JXL_PRIMARIES_SRGB; |
| 971 |
colorEncoding.transfer_function = JXL_TRANSFER_FUNCTION_SRGB; |
| 972 |
|
| 973 |
useJXLColorEncoding = true; |
| 974 |
|
| 975 |
} |
| 976 |
|
| 977 |
// Set basic info. |
| 978 |
|
| 979 |
const uint32 bitsPerSample = buffer.fPixelSize * 8; |
| 980 |
|
| 981 |
{ |
| 982 |
|
| 983 |
JxlBasicInfo info; |
| 984 |
|
| 985 |
JxlEncoderInitBasicInfo (&info); |
| 986 |
|
| 987 |
info.have_container = useContainer; |
| 988 |
|
| 989 |
info.xsize = buffer.fArea.W (); |
| 990 |
info.ysize = buffer.fArea.H (); |
| 991 |
|
| 992 |
info.bits_per_sample = bitsPerSample; |
| 993 |
|
| 994 |
// Set exponent_bits_per_sample for float types. |
| 995 |
|
| 996 |
if (srcPixelType == ttFloat) |
| 997 |
info.exponent_bits_per_sample = 8; // fp32 |
| 998 |
|
| 999 |
else if (srcPixelType == ttHalfFloat) |
| 1000 |
info.exponent_bits_per_sample = 5; // fp16 |
| 1001 |
|
| 1002 |
else |
| 1003 |
{ |
| 1004 |
info.exponent_bits_per_sample = 0; // integer |
| 1005 |
info.bits_per_sample = Min_uint32 (info.bits_per_sample, 16); |
| 1006 |
} |
| 1007 |
|
| 1008 |
// TODO(erichan): libjxl 0.7.0 does not yet support bits_per_sample above |
| 1009 |
// 24, hmm. |
| 1010 |
|
| 1011 |
|
| 1012 |
/* |
| 1013 |
{ |
| 1014 |
|
| 1015 |
// More than 16 bits per sample require codestream level 10. |
| 1016 |
|
| 1017 |
printf ("info.bits_per_sample %u\n", |
| 1018 |
info.bits_per_sample); |
| 1019 |
|
| 1020 |
CheckResult (JxlEncoderSetCodestreamLevel (enc, 10), |
| 1021 |
"JxlEncoderSetCodestreamLevel"); |
| 1022 |
|
| 1023 |
} |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
// If using lossless, then data must be in original space. |
| 1027 |
|
| 1028 |
if (isLossless) |
| 1029 |
info.uses_original_profile = JXL_TRUE; |
| 1030 |
|
| 1031 |
// With lossy, we have an option. For built-in spaces that we can |
| 1032 |
// represent exactly using the JxlColorEncoding, prefer xyb since the |
| 1033 |
// files tend to be smaller. |
| 1034 |
// |
| 1035 |
// However, if the caller requested to use the original encoding, then |
| 1036 |
// we'll do that. |
| 1037 |
|
| 1038 |
else if (settings.UseOriginalColorEncoding ()) |
| 1039 |
info.uses_original_profile = JXL_TRUE; |
| 1040 |
|
| 1041 |
else |
| 1042 |
info.uses_original_profile = useJXLColorEncoding ? JXL_FALSE : JXL_TRUE; |
| 1043 |
|
| 1044 |
// Deal with alpha. |
| 1045 |
|
| 1046 |
const bool hasAlpha = (planes == 2 || |
| 1047 |
planes == 4); |
| 1048 |
|
| 1049 |
if (hasAlpha) |
| 1050 |
{ |
| 1051 |
|
| 1052 |
info.num_extra_channels = 1; |
| 1053 |
|
| 1054 |
info.alpha_bits = bitsPerSample; |
| 1055 |
|
| 1056 |
info.num_color_channels = planes - 1; |
| 1057 |
|
| 1058 |
} |
| 1059 |
|
| 1060 |
else |
| 1061 |
{ |
| 1062 |
|
| 1063 |
info.num_extra_channels = 0; |
| 1064 |
|
| 1065 |
info.alpha_bits = 0; |
| 1066 |
|
| 1067 |
info.num_color_channels = planes; |
| 1068 |
|
| 1069 |
} |
| 1070 |
|
| 1071 |
// TODO(erichan): |
| 1072 |
#if 0 |
| 1073 |
info.have_preview = JXL_TRUE; |
| 1074 |
info.preview.xsize = 200; |
| 1075 |
info.preview.ysize = 200; |
| 1076 |
#endif |
| 1077 |
|
| 1078 |
// For now always assume intrinsic size matches main size. |
| 1079 |
info.intrinsic_xsize = info.xsize; |
| 1080 |
info.intrinsic_ysize = info.ysize; |
| 1081 |
|
| 1082 |
info.intensity_target = float (colorSpaceInfo.fIntensityTargetNits); |
| 1083 |
|
| 1084 |
CheckResult (JxlEncoderSetBasicInfo (enc, &info), |
| 1085 |
"JxlEncoderSetBasicInfo", |
| 1086 |
¶llelData); |
| 1087 |
|
| 1088 |
} |
| 1089 |
|
| 1090 |
// Set color encoding or profile info after setting the basic info above |
| 1091 |
// (this order of operations required by libjxl circa July 2022). |
| 1092 |
|
| 1093 |
if (useJXLColorEncoding) |
| 1094 |
{ |
| 1095 |
|
| 1096 |
CheckResult (JxlEncoderSetColorEncoding (enc, &colorEncoding), |
| 1097 |
"JxlEncoderSetColorEncoding", |
| 1098 |
¶llelData); |
| 1099 |
|
| 1100 |
} |
| 1101 |
|
| 1102 |
else |
| 1103 |
{ |
| 1104 |
|
| 1105 |
DNG_REQUIRE (colorSpaceInfo.fICCProfile.Get (), |
| 1106 |
"invalid icc profile"); |
| 1107 |
|
| 1108 |
// Provide color tag as icc color profile. |
| 1109 |
|
| 1110 |
const void *profileData = colorSpaceInfo.fICCProfile->Buffer (); |
| 1111 |
uint32 profileSize = colorSpaceInfo.fICCProfile->LogicalSize (); |
| 1112 |
|
| 1113 |
if (profileData && profileSize) |
| 1114 |
{ |
| 1115 |
|
| 1116 |
CheckResult (JxlEncoderSetICCProfile (enc, |
| 1117 |
(const uint8 *) profileData, |
| 1118 |
(size_t) profileSize), |
| 1119 |
"JxlEncoderSetICCProfile", |
| 1120 |
¶llelData); |
| 1121 |
|
| 1122 |
} |
| 1123 |
|
| 1124 |
else |
| 1125 |
{ |
| 1126 |
|
| 1127 |
#if qLogJXL |
| 1128 |
printf ("Writing untagged JXL file"); |
| 1129 |
#endif |
| 1130 |
|
| 1131 |
} |
| 1132 |
|
| 1133 |
} |
| 1134 |
|
| 1135 |
// Create settings for the frame. |
| 1136 |
|
| 1137 |
auto frameSettings = JxlEncoderFrameSettingsCreate (enc, NULL); |
| 1138 |
|
| 1139 |
// Set quality. |
| 1140 |
|
| 1141 |
if (isLossless) |
| 1142 |
{ |
| 1143 |
|
| 1144 |
// Use lossless. |
| 1145 |
|
| 1146 |
#if qLogJXL |
| 1147 |
printf ("Using lossless encoding\n"); |
| 1148 |
#endif |
| 1149 |
|
| 1150 |
CheckResult (JxlEncoderSetFrameLossless (frameSettings, |
| 1151 |
JXL_TRUE), |
| 1152 |
"JxlEncoderSetFrameLossless", |
| 1153 |
¶llelData); |
| 1154 |
|
| 1155 |
} |
| 1156 |
|
| 1157 |
else |
| 1158 |
{ |
| 1159 |
|
| 1160 |
// Use lossy. |
| 1161 |
|
| 1162 |
const real32 distance = settings.Distance (); |
| 1163 |
|
| 1164 |
#if qLogJXL |
| 1165 |
printf ("using lossy encoding with distance %.3f\n", |
| 1166 |
distance); |
| 1167 |
#endif |
| 1168 |
|
| 1169 |
CheckResult (JxlEncoderSetFrameDistance (frameSettings, |
| 1170 |
distance), |
| 1171 |
"JxlEncoderSetFrameDistance", |
| 1172 |
¶llelData); |
| 1173 |
|
| 1174 |
} |
| 1175 |
|
| 1176 |
// Set effort. |
| 1177 |
|
| 1178 |
CheckResult |
| 1179 |
(JxlEncoderFrameSettingsSetOption (frameSettings, |
| 1180 |
JXL_ENC_FRAME_SETTING_EFFORT, |
| 1181 |
(int) settings.Effort ()), |
| 1182 |
"JxlEncoderFrameSettingsSetOption", |
| 1183 |
¶llelData); |
| 1184 |
|
| 1185 |
// Set various parameters that affect the lossy case. |
| 1186 |
|
| 1187 |
// Note that as of libjxl 0.7.0 (2022-9-21), enabling the Gaborish and |
| 1188 |
// Edge Preserving Filter params in the lossless case will cause the |
| 1189 |
// result not to be lossless! |
| 1190 |
|
| 1191 |
if (!isLossless) |
| 1192 |
{ |
| 1193 |
|
| 1194 |
// Set decoding speed. |
| 1195 |
|
| 1196 |
CheckResult |
| 1197 |
(JxlEncoderFrameSettingsSetOption (frameSettings, |
| 1198 |
JXL_ENC_FRAME_SETTING_DECODING_SPEED, |
| 1199 |
(int) settings.DecodeSpeed ()), |
| 1200 |
"JxlEncoderFrameSettingsSetOption", |
| 1201 |
¶llelData); |
| 1202 |
|
| 1203 |
// Disable patches. I don't think this matters because it's only supposed |
| 1204 |
// to affect what happens between frames. |
| 1205 |
|
| 1206 |
CheckResult |
| 1207 |
(JxlEncoderFrameSettingsSetOption (frameSettings, |
| 1208 |
JXL_ENC_FRAME_SETTING_PATCHES, |
| 1209 |
0), |
| 1210 |
"JxlEncoderFrameSettingsSetOption-Patches", |
| 1211 |
¶llelData); |
| 1212 |
|
| 1213 |
if (settings.Distance () > 0.02f) |
| 1214 |
{ |
| 1215 |
|
| 1216 |
// Enable gaborish. This helps to reduce blockiness. |
| 1217 |
|
| 1218 |
CheckResult |
| 1219 |
(JxlEncoderFrameSettingsSetOption (frameSettings, |
| 1220 |
JXL_ENC_FRAME_SETTING_GABORISH, |
| 1221 |
1), |
| 1222 |
"JxlEncoderFrameSettingsSetOption-Gaborish", |
| 1223 |
¶llelData); |
| 1224 |
|
| 1225 |
// Edge preserving filter. This helps to reduce artifacts along strong |
| 1226 |
// contrast edges (mountain against sky). |
| 1227 |
|
| 1228 |
CheckResult |
| 1229 |
(JxlEncoderFrameSettingsSetOption (frameSettings, |
| 1230 |
JXL_ENC_FRAME_SETTING_EPF, |
| 1231 |
3), |
| 1232 |
"JxlEncoderFrameSettingsSetOption-EPF", |
| 1233 |
¶llelData); |
| 1234 |
|
| 1235 |
} |
| 1236 |
|
| 1237 |
// TODO(erichan): customize advanced settings? |
| 1238 |
|
| 1239 |
// ... |
| 1240 |
|
| 1241 |
} // !isLossless |
| 1242 |
|
| 1243 |
// Set color encoding. |
| 1244 |
|
| 1245 |
// TODO(erichan): detect special case where data is already linear sRGB |
| 1246 |
|
| 1247 |
// TODO(erichan): detect special case where data is already regular sRGB |
| 1248 |
|
| 1249 |
// TODO(erichan): For now it seems the encoder implementation requires the |
| 1250 |
// entire image to be allocated at once. |
| 1251 |
|
| 1252 |
JxlPixelFormat pixelFormat; |
| 1253 |
|
| 1254 |
memset (&pixelFormat, 0, sizeof (pixelFormat)); |
| 1255 |
|
| 1256 |
pixelFormat.num_channels = planes; |
| 1257 |
|
| 1258 |
switch (srcPixelType) |
| 1259 |
{ |
| 1260 |
|
| 1261 |
case ttByte: |
| 1262 |
pixelFormat.data_type = JXL_TYPE_UINT8; |
| 1263 |
break; |
| 1264 |
|
| 1265 |
case ttShort: |
| 1266 |
pixelFormat.data_type = JXL_TYPE_UINT16; |
| 1267 |
break; |
| 1268 |
|
| 1269 |
case ttHalfFloat: |
| 1270 |
pixelFormat.data_type = JXL_TYPE_FLOAT16; |
| 1271 |
break; |
| 1272 |
|
| 1273 |
case ttFloat: |
| 1274 |
pixelFormat.data_type = JXL_TYPE_FLOAT; |
| 1275 |
break; |
| 1276 |
|
| 1277 |
default: |
| 1278 |
{ |
| 1279 |
#if qLogJXL |
| 1280 |
printf ("unexpected srcPixelType"); |
| 1281 |
#endif |
| 1282 |
ThrowProgramError ("unexpected srcPixelType"); |
| 1283 |
break; |
| 1284 |
} |
| 1285 |
|
| 1286 |
} |
| 1287 |
|
| 1288 |
// Set up pixel buffer to hold data to hand off to encoder. As of version |
| 1289 |
// 0.7.0 libjxl requires starting from a single whole-image buffer |
| 1290 |
// (instead of reading chunks at a time). This is a chunky (row-col-plane |
| 1291 |
// interleaved) buffer. |
| 1292 |
|
| 1293 |
dng_pixel_buffer pixelBuffer = buffer; |
| 1294 |
|
| 1295 |
pixelBuffer.fPlaneStep = 1; |
| 1296 |
pixelBuffer.fColStep = (int32) planes; |
| 1297 |
pixelBuffer.fRowStep = (int32) planes * (int32) pixelBuffer.fArea.W (); |
| 1298 |
|
| 1299 |
const uint64 bytesNeeded = (uint64 (pixelBuffer.fRowStep) * |
| 1300 |
uint64 (pixelBuffer.fArea.H ()) * |
| 1301 |
uint64 (pixelBuffer.fPixelSize)); |
| 1302 |
|
| 1303 |
AutoPtr<jxl_memory_block> block; |
| 1304 |
|
| 1305 |
// If not already tightly-packed chunky, then allocate a temp buffer and |
| 1306 |
// convert to chunky layout. |
| 1307 |
|
| 1308 |
if (!SamePixelBufferGeometry (pixelBuffer, buffer)) |
| 1309 |
{ |
| 1310 |
|
| 1311 |
#if qLogJXL |
| 1312 |
printf ("JXL copy step\n"); |
| 1313 |
#endif |
| 1314 |
|
| 1315 |
block.Reset (new jxl_memory_block (host.Allocator (), |
| 1316 |
bytesNeeded)); |
| 1317 |
|
| 1318 |
pixelBuffer.fData = block->Buffer (); |
| 1319 |
|
| 1320 |
dng_copy_buffer_task task (buffer, |
| 1321 |
pixelBuffer); |
| 1322 |
|
| 1323 |
host.PerformAreaTask (task, |
| 1324 |
pixelBuffer.fArea); |
| 1325 |
|
| 1326 |
} |
| 1327 |
|
| 1328 |
// TODO(erichan): It seems that preview frames are not yet supported in |
| 1329 |
// libjxl 0.7.0, so turn this off for now. |
| 1330 |
|
| 1331 |
#if 0 |
| 1332 |
|
| 1333 |
dng_pixel_buffer previewPixelBuffer; |
| 1334 |
|
| 1335 |
previewPixelBuffer.fArea = dng_rect (200, 200); // TODO(erichan): |
| 1336 |
|
| 1337 |
previewPixelBuffer.fPlanes = planes; |
| 1338 |
|
| 1339 |
previewPixelBuffer.fPlaneStep = 1; |
| 1340 |
previewPixelBuffer.fColStep = (int32) planes; |
| 1341 |
previewPixelBuffer.fRowStep = (int32) planes * (int32) previewPixelBuffer.fArea.W (); |
| 1342 |
|
| 1343 |
previewPixelBuffer.fPixelType = srcPixelType; |
| 1344 |
previewPixelBuffer.fPixelSize = TagTypeSize (previewPixelBuffer.fPixelType); |
| 1345 |
|
| 1346 |
uint32 previewBytesNeeded = (previewPixelBuffer.fRowStep * |
| 1347 |
previewPixelBuffer.fArea.H () * |
| 1348 |
previewPixelBuffer.fPixelSize); |
| 1349 |
|
| 1350 |
AutoPtr<dng_memory_block> previewBlock (host.Allocate (previewBytesNeeded)); |
| 1351 |
|
| 1352 |
previewPixelBuffer.fData = previewBlock->Buffer (); |
| 1353 |
|
| 1354 |
// Try preview frame? |
| 1355 |
|
| 1356 |
auto previewFrameSettings = JxlEncoderFrameSettingsCreate (enc, |
| 1357 |
frameSettings); |
| 1358 |
|
| 1359 |
CheckResult |
| 1360 |
(JxlEncoderAddImageFrame (previewFrameSettings, |
| 1361 |
&pixelFormat, |
| 1362 |
previewPixelBuffer.fData, |
| 1363 |
previewBytesNeeded), |
| 1364 |
"JxlEncoderAddImageFrame-preview"); |
| 1365 |
|
| 1366 |
#endif // disabled |
| 1367 |
|
| 1368 |
// Setting the frame name is optional. |
| 1369 |
// |
| 1370 |
// In libjxl 0.8.0, setting a non-empty name prevents the fast lossless |
| 1371 |
// encoder from being used. |
| 1372 |
|
| 1373 |
#if 0 |
| 1374 |
|
| 1375 |
CheckResult (JxlEncoderSetFrameName (frameSettings, |
| 1376 |
"main"), |
| 1377 |
"JxlEncoderSetFrameName", |
| 1378 |
¶llelData); |
| 1379 |
|
| 1380 |
#endif |
| 1381 |
|
| 1382 |
// Add the main image. |
| 1383 |
|
| 1384 |
CheckResult |
| 1385 |
(JxlEncoderAddImageFrame (frameSettings, |
| 1386 |
&pixelFormat, |
| 1387 |
pixelBuffer.fData, |
| 1388 |
bytesNeeded), |
| 1389 |
"JxlEncoderAddImageFrame-main", |
| 1390 |
¶llelData); |
| 1391 |
|
| 1392 |
// Nothing more to encode. |
| 1393 |
|
| 1394 |
JxlEncoderCloseInput (enc); |
| 1395 |
|
| 1396 |
// Make temp buffer. |
| 1397 |
|
| 1398 |
const uint32 tempSize = 64 * 1024; |
| 1399 |
|
| 1400 |
AutoPtr<dng_memory_block> tempBlock (host.Allocate (tempSize)); |
| 1401 |
|
| 1402 |
uint8 *outBuffer = tempBlock->Buffer_uint8 (); |
| 1403 |
|
| 1404 |
size_t outAvailableBytes = (size_t) tempSize; |
| 1405 |
|
| 1406 |
uint8 *outBufferPtr = outBuffer; |
| 1407 |
|
| 1408 |
// Main encode loop. |
| 1409 |
|
| 1410 |
for (;;) |
| 1411 |
{ |
| 1412 |
|
| 1413 |
JxlEncoderStatus status = JxlEncoderProcessOutput (enc, |
| 1414 |
&outBufferPtr, |
| 1415 |
&outAvailableBytes); |
| 1416 |
|
| 1417 |
// Handle errors. |
| 1418 |
|
| 1419 |
if (status == JXL_ENC_ERROR) |
| 1420 |
{ |
| 1421 |
if (parallelData.fErrorCode == dng_error_user_canceled) |
| 1422 |
{ |
| 1423 |
ThrowUserCanceled (); |
| 1424 |
} |
| 1425 |
#if qLogJXL |
| 1426 |
printf ("Unknown jxl encoder error"); |
| 1427 |
#endif |
| 1428 |
ThrowBadFormat ("JXL_ENC_ERROR"); |
| 1429 |
} |
| 1430 |
|
| 1431 |
// Check if we're done. |
| 1432 |
|
| 1433 |
else if (status == JXL_ENC_SUCCESS) |
| 1434 |
{ |
| 1435 |
|
| 1436 |
uint32 bytesToFlush = (uint32) (outBufferPtr - outBuffer); |
| 1437 |
|
| 1438 |
#if qLogJXL |
| 1439 |
printf ("jxl encoder success!! bytes to flush: %u\n", |
| 1440 |
bytesToFlush); |
| 1441 |
#endif |
| 1442 |
|
| 1443 |
stream.Put (outBuffer, bytesToFlush); |
| 1444 |
|
| 1445 |
break; |
| 1446 |
|
| 1447 |
} |
| 1448 |
|
| 1449 |
// Check if we need to update the output buffer. |
| 1450 |
|
| 1451 |
else if (status == JXL_ENC_NEED_MORE_OUTPUT) |
| 1452 |
{ |
| 1453 |
|
| 1454 |
uint32 bytesToFlush = (uint32) (outBufferPtr - outBuffer); |
| 1455 |
|
| 1456 |
#if qLogJXL |
| 1457 |
printf ("--- jxl encoder need more output. bytes to flush: %u\n", |
| 1458 |
bytesToFlush); |
| 1459 |
#endif |
| 1460 |
|
| 1461 |
// Copy encoded data to the stream. |
| 1462 |
|
| 1463 |
stream.Put (outBuffer, bytesToFlush); |
| 1464 |
|
| 1465 |
// Reset temp buffer. |
| 1466 |
|
| 1467 |
outBufferPtr = outBuffer; |
| 1468 |
|
| 1469 |
outAvailableBytes = tempSize; |
| 1470 |
|
| 1471 |
} |
| 1472 |
|
| 1473 |
else |
| 1474 |
{ |
| 1475 |
|
| 1476 |
#if qLogJXL |
| 1477 |
|
| 1478 |
printf ("Unexpected jxl encoder status 0x%x\n", |
| 1479 |
(unsigned) status); |
| 1480 |
|
| 1481 |
#endif |
| 1482 |
|
| 1483 |
ThrowNotYetImplemented ("unhandled jxl encoder status"); |
| 1484 |
|
| 1485 |
} |
| 1486 |
|
| 1487 |
} |
| 1488 |
|
| 1489 |
} |
| 1490 |
|
| 1491 |
/*****************************************************************************/ |
| 1492 |
|
| 1493 |
static void EncodeJXL (dng_host &host, |
| 1494 |
dng_stream &stream, |
| 1495 |
const dng_image &image, |
| 1496 |
const dng_jxl_encode_settings &settings, |
| 1497 |
const bool useContainer, |
| 1498 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 1499 |
const dng_metadata *metadata, |
| 1500 |
const bool includeExif, |
| 1501 |
const bool includeXMP, |
| 1502 |
const bool includeIPTC, |
| 1503 |
const dng_bmff_box_list *additionalBoxes) |
| 1504 |
{ |
| 1505 |
|
| 1506 |
// Set up pixel buffer to hold data to hand off to encoder. As of version |
| 1507 |
// 0.7.0 libjxl requires starting from a single whole-image buffer |
| 1508 |
// (instead of reading chunks at a time). This is a chunky (row-col-plane |
| 1509 |
// interleaved) buffer. |
| 1510 |
|
| 1511 |
const uint32 planes = image.Planes (); |
| 1512 |
|
| 1513 |
DNG_REQUIRE (planes == 1 || // monochrome |
| 1514 |
planes == 2 || // monochrome + alpha |
| 1515 |
planes == 3 || // RGB |
| 1516 |
planes == 4, // RGB + alpha |
| 1517 |
"Unsupported plane count in EncodeJXL"); |
| 1518 |
|
| 1519 |
dng_pixel_buffer pixelBuffer; |
| 1520 |
|
| 1521 |
pixelBuffer.fArea = image.Bounds (); |
| 1522 |
|
| 1523 |
pixelBuffer.fPlanes = planes; |
| 1524 |
|
| 1525 |
pixelBuffer.fPlaneStep = 1; |
| 1526 |
pixelBuffer.fColStep = (int32) planes; |
| 1527 |
pixelBuffer.fRowStep = (int32) planes * (int32) pixelBuffer.fArea.W (); |
| 1528 |
|
| 1529 |
pixelBuffer.fPixelType = image.PixelType (); |
| 1530 |
pixelBuffer.fPixelSize = TagTypeSize (pixelBuffer.fPixelType); |
| 1531 |
|
| 1532 |
const uint64 bytesNeeded = (uint64 (pixelBuffer.fRowStep) * |
| 1533 |
uint64 (pixelBuffer.fArea.H ()) * |
| 1534 |
uint64 (pixelBuffer.fPixelSize)); |
| 1535 |
|
| 1536 |
jxl_memory_block block (host.Allocator (), |
| 1537 |
bytesNeeded); |
| 1538 |
|
| 1539 |
pixelBuffer.fData = block.Buffer (); |
| 1540 |
|
| 1541 |
// Doing a direct "dng_image::Get" is easy but slow because it's |
| 1542 |
// single-threaded. Using a pipe reduces time on a 10 megapixel image from |
| 1543 |
// 40 ms to 8 ms on a 2017 10-core iMac Pro. |
| 1544 |
|
| 1545 |
// dng_timer timer2 ("EncodeJXL-image-get"); |
| 1546 |
|
| 1547 |
#if 1 |
| 1548 |
|
| 1549 |
dng_get_buffer_task task (image, |
| 1550 |
pixelBuffer); |
| 1551 |
|
| 1552 |
host.PerformAreaTask (task, |
| 1553 |
image.Bounds ()); |
| 1554 |
|
| 1555 |
#else |
| 1556 |
|
| 1557 |
image.Get (pixelBuffer); |
| 1558 |
|
| 1559 |
#endif |
| 1560 |
|
| 1561 |
EncodeJXL (host, |
| 1562 |
stream, |
| 1563 |
pixelBuffer, |
| 1564 |
settings, |
| 1565 |
useContainer, |
| 1566 |
colorSpaceInfo, |
| 1567 |
metadata, |
| 1568 |
includeExif, |
| 1569 |
includeXMP, |
| 1570 |
includeIPTC, |
| 1571 |
additionalBoxes); |
| 1572 |
|
| 1573 |
} |
| 1574 |
|
| 1575 |
/*****************************************************************************/ |
| 1576 |
|
| 1577 |
class dng_jxl_box_reader |
| 1578 |
{ |
| 1579 |
|
| 1580 |
public: |
| 1581 |
|
| 1582 |
dng_host &fHost; |
| 1583 |
|
| 1584 |
JxlDecoder *fDec = nullptr; |
| 1585 |
|
| 1586 |
bool fHasCurrentBox = false; |
| 1587 |
|
| 1588 |
JxlBoxType fType; |
| 1589 |
|
| 1590 |
std::vector<uint8> fData; |
| 1591 |
|
| 1592 |
size_t fIndex = 0; |
| 1593 |
|
| 1594 |
dng_info *fInfo = nullptr; |
| 1595 |
|
| 1596 |
dng_jxl_decoder &fDecoder; |
| 1597 |
|
| 1598 |
public: |
| 1599 |
|
| 1600 |
dng_jxl_box_reader (dng_host &host, |
| 1601 |
JxlDecoder *dec, |
| 1602 |
dng_jxl_decoder &decoder) |
| 1603 |
: fHost (host) |
| 1604 |
, fDec (dec) |
| 1605 |
, fInfo (decoder.fInfo) |
| 1606 |
, fDecoder (decoder) |
| 1607 |
{ |
| 1608 |
} |
| 1609 |
|
| 1610 |
void HandleDecBox () |
| 1611 |
{ |
| 1612 |
|
| 1613 |
// Wrap up any previous box. |
| 1614 |
|
| 1615 |
CheckFinishBox (); |
| 1616 |
|
| 1617 |
// Read box type. |
| 1618 |
|
| 1619 |
CheckResult (JxlDecoderGetBoxType (fDec, |
| 1620 |
fType, |
| 1621 |
JXL_TRUE), // decompress |
| 1622 |
"JxlDecoderGetBoxType", |
| 1623 |
nullptr); |
| 1624 |
|
| 1625 |
// Read box size. |
| 1626 |
|
| 1627 |
uint64_t size; |
| 1628 |
|
| 1629 |
CheckResult (JxlDecoderGetBoxSizeRaw (fDec, &size), |
| 1630 |
"JxlDecoderGetBoxSizeRaw", |
| 1631 |
nullptr); |
| 1632 |
|
| 1633 |
#if qLogJXL |
| 1634 |
|
| 1635 |
printf ("jxl box: type: \"%c%c%c%c\" size: %llu\n", |
| 1636 |
fType [0], |
| 1637 |
fType [1], |
| 1638 |
fType [2], |
| 1639 |
fType [3], |
| 1640 |
(unsigned long long) size); |
| 1641 |
|
| 1642 |
#endif |
| 1643 |
|
| 1644 |
// Begin new box type. |
| 1645 |
|
| 1646 |
if (fType [0] == 'j' && |
| 1647 |
fType [1] == 'x' && |
| 1648 |
fType [2] == 'l' && |
| 1649 |
fType [3] == 'c') |
| 1650 |
{ |
| 1651 |
|
| 1652 |
// Code stream. Don't need to grab the data ourselves. |
| 1653 |
|
| 1654 |
// printf ("found jxlc codestream -- ignoring\n"); |
| 1655 |
|
| 1656 |
} |
| 1657 |
|
| 1658 |
// This check might be a problem for large gain maps. |
| 1659 |
|
| 1660 |
#if 1 |
| 1661 |
|
| 1662 |
else if (size > 4 * 1024 * 1024) |
| 1663 |
{ |
| 1664 |
|
| 1665 |
#if qLogJXL |
| 1666 |
|
| 1667 |
printf ("jxl: Skipping large box with size %llu", |
| 1668 |
(unsigned long long) size); |
| 1669 |
|
| 1670 |
#endif |
| 1671 |
|
| 1672 |
} |
| 1673 |
|
| 1674 |
#endif |
| 1675 |
|
| 1676 |
else |
| 1677 |
{ |
| 1678 |
|
| 1679 |
// Reset buffer. Re-use any already-allocated memory. |
| 1680 |
|
| 1681 |
fIndex = 0; |
| 1682 |
|
| 1683 |
if (fData.empty ()) |
| 1684 |
{ |
| 1685 |
fData.resize (4096); |
| 1686 |
} |
| 1687 |
|
| 1688 |
CheckResult (JxlDecoderSetBoxBuffer (fDec, |
| 1689 |
fData.data (), |
| 1690 |
fData.size ()), |
| 1691 |
"JxlDecoderSetBoxBuffer", |
| 1692 |
nullptr); |
| 1693 |
|
| 1694 |
fHasCurrentBox = true; |
| 1695 |
|
| 1696 |
} |
| 1697 |
|
| 1698 |
} |
| 1699 |
|
| 1700 |
void HandleNeedMoreOutput () |
| 1701 |
{ |
| 1702 |
|
| 1703 |
size_t remaining = JxlDecoderReleaseBoxBuffer (fDec); |
| 1704 |
|
| 1705 |
fIndex = fData.size () - remaining; |
| 1706 |
|
| 1707 |
size_t newSize = 2 * fData.size (); |
| 1708 |
|
| 1709 |
fData.resize (newSize); |
| 1710 |
|
| 1711 |
size_t availableSize = newSize - fIndex; |
| 1712 |
|
| 1713 |
#if qLogJXL |
| 1714 |
printf ("box read output: remain=%llu\n", |
| 1715 |
(unsigned long long) remaining); |
| 1716 |
#endif |
| 1717 |
|
| 1718 |
CheckResult (JxlDecoderSetBoxBuffer (fDec, |
| 1719 |
&fData [fIndex], |
| 1720 |
availableSize), |
| 1721 |
"JxlDecoderSetBoxBuffer", |
| 1722 |
nullptr); |
| 1723 |
|
| 1724 |
} |
| 1725 |
|
| 1726 |
void CheckFinishBox () |
| 1727 |
{ |
| 1728 |
|
| 1729 |
if (fHasCurrentBox) |
| 1730 |
{ |
| 1731 |
|
| 1732 |
size_t remaining = JxlDecoderReleaseBoxBuffer (fDec); |
| 1733 |
|
| 1734 |
fData.resize (fData.size () - remaining); |
| 1735 |
|
| 1736 |
fHasCurrentBox = false; |
| 1737 |
|
| 1738 |
FinishBox (); |
| 1739 |
|
| 1740 |
} |
| 1741 |
|
| 1742 |
} |
| 1743 |
|
| 1744 |
private: |
| 1745 |
|
| 1746 |
// If info provided, then contents of boxes will be written to info. |
| 1747 |
// Otherwise, contents of boxes will be ignored. |
| 1748 |
|
| 1749 |
void FinishBox () |
| 1750 |
{ |
| 1751 |
|
| 1752 |
#if qLogJXL |
| 1753 |
|
| 1754 |
printf ("finished up box type %c%c%c%c: final byte size %llu\n", |
| 1755 |
fType [0], |
| 1756 |
fType [1], |
| 1757 |
fType [2], |
| 1758 |
fType [3], |
| 1759 |
(unsigned long long) fData.size ()); |
| 1760 |
|
| 1761 |
#endif |
| 1762 |
|
| 1763 |
if (fType [0] == 'E' && |
| 1764 |
fType [1] == 'x' && |
| 1765 |
fType [2] == 'i' && |
| 1766 |
fType [3] == 'f') |
| 1767 |
{ |
| 1768 |
|
| 1769 |
fDecoder.ProcessExifBox (fHost, |
| 1770 |
fData); |
| 1771 |
|
| 1772 |
} // exif |
| 1773 |
|
| 1774 |
else if ((fType [0] == 'X' && |
| 1775 |
fType [1] == 'M' && |
| 1776 |
fType [2] == 'L' && |
| 1777 |
fType [3] == ' ') || |
| 1778 |
|
| 1779 |
(fType [0] == 'x' && |
| 1780 |
fType [1] == 'm' && |
| 1781 |
fType [2] == 'l' && |
| 1782 |
fType [3] == ' ')) |
| 1783 |
|
| 1784 |
{ |
| 1785 |
|
| 1786 |
fDecoder.ProcessXMPBox (fHost, |
| 1787 |
fData); |
| 1788 |
|
| 1789 |
} // xml |
| 1790 |
|
| 1791 |
else |
| 1792 |
{ |
| 1793 |
|
| 1794 |
// Some other box. |
| 1795 |
|
| 1796 |
const char rawTypeName [] = |
| 1797 |
{ |
| 1798 |
fType [0], |
| 1799 |
fType [1], |
| 1800 |
fType [2], |
| 1801 |
fType [3], |
| 1802 |
'\0' |
| 1803 |
}; |
| 1804 |
|
| 1805 |
dng_string typeName (rawTypeName); |
| 1806 |
|
| 1807 |
fDecoder.ProcessBox (fHost, |
| 1808 |
typeName, |
| 1809 |
fData); |
| 1810 |
|
| 1811 |
} |
| 1812 |
|
| 1813 |
// Developer code. |
| 1814 |
|
| 1815 |
#if qDNGValidate && 0 |
| 1816 |
|
| 1817 |
char buf [256]; |
| 1818 |
|
| 1819 |
snprintf (buf, |
| 1820 |
256, |
| 1821 |
"%c%c%c%c.txt", |
| 1822 |
fType [0], |
| 1823 |
fType [1], |
| 1824 |
fType [2], |
| 1825 |
fType [3]); |
| 1826 |
|
| 1827 |
dng_file_stream outStream (buf, true); |
| 1828 |
|
| 1829 |
outStream.Put (fData.data (), (uint32) fData.size ()); |
| 1830 |
|
| 1831 |
outStream.Flush (); |
| 1832 |
|
| 1833 |
#endif // dev code |
| 1834 |
|
| 1835 |
} |
| 1836 |
|
| 1837 |
}; |
| 1838 |
|
| 1839 |
/*****************************************************************************/ |
| 1840 |
|
| 1841 |
class dng_put_alpha_buffer_task : public dng_area_task |
| 1842 |
{ |
| 1843 |
|
| 1844 |
private: |
| 1845 |
|
| 1846 |
const dng_pixel_buffer &fSrc; |
| 1847 |
|
| 1848 |
dng_image &fDst; |
| 1849 |
|
| 1850 |
int32 fSrcAlphaPlane = 0; |
| 1851 |
|
| 1852 |
public: |
| 1853 |
|
| 1854 |
dng_put_alpha_buffer_task (const dng_pixel_buffer &buffer, |
| 1855 |
dng_image &image) |
| 1856 |
|
| 1857 |
: dng_area_task ("dng_put_alpha_buffer_task") |
| 1858 |
|
| 1859 |
, fSrc (buffer) |
| 1860 |
|
| 1861 |
, fDst (image) |
| 1862 |
|
| 1863 |
{ |
| 1864 |
|
| 1865 |
DNG_REQUIRE (buffer.Planes () == 2 || |
| 1866 |
buffer.Planes () == 4, |
| 1867 |
"Unsupported plane count in dng_stage_put_alpha_buffer"); |
| 1868 |
|
| 1869 |
fSrcAlphaPlane = int32 (buffer.Planes () - 1); |
| 1870 |
|
| 1871 |
} |
| 1872 |
|
| 1873 |
dng_rect RepeatingTile1 () const override |
| 1874 |
{ |
| 1875 |
|
| 1876 |
return fDst.RepeatingTile (); |
| 1877 |
|
| 1878 |
} |
| 1879 |
|
| 1880 |
void Process (uint32 /* threadIndex */, |
| 1881 |
const dng_rect &tile, |
| 1882 |
dng_abort_sniffer * /* sniffer */) override |
| 1883 |
{ |
| 1884 |
|
| 1885 |
dng_pixel_buffer temp = fSrc; |
| 1886 |
|
| 1887 |
temp.fData = (void *) fSrc.ConstPixel (tile.t, |
| 1888 |
tile.l, |
| 1889 |
fSrcAlphaPlane); |
| 1890 |
|
| 1891 |
temp.fArea = tile; |
| 1892 |
|
| 1893 |
temp.fPlane = 0; |
| 1894 |
temp.fPlanes = 1; |
| 1895 |
|
| 1896 |
fDst.Put (temp); |
| 1897 |
|
| 1898 |
} |
| 1899 |
|
| 1900 |
}; |
| 1901 |
|
| 1902 |
/*****************************************************************************/ |
| 1903 |
|
| 1904 |
dng_jxl_decoder::~dng_jxl_decoder () |
| 1905 |
{ |
| 1906 |
} |
| 1907 |
|
| 1908 |
/*****************************************************************************/ |
| 1909 |
|
| 1910 |
void dng_jxl_decoder::Decode (dng_host &host, |
| 1911 |
dng_stream &stream) |
| 1912 |
{ |
| 1913 |
|
| 1914 |
const bool needBoxMeta = fNeedBoxMeta; |
| 1915 |
const bool needImage = fNeedImage; |
| 1916 |
|
| 1917 |
// Hook into our memory allocator. |
| 1918 |
|
| 1919 |
JxlMemoryManager memManager; |
| 1920 |
|
| 1921 |
memManager.opaque = (void *) &host.Allocator (); |
| 1922 |
memManager.alloc = dng_jxl_alloc; |
| 1923 |
memManager.free = dng_jxl_free; |
| 1924 |
|
| 1925 |
// Remember stream position. |
| 1926 |
|
| 1927 |
const uint64 startPosition = stream.Position (); |
| 1928 |
|
| 1929 |
// Make decoder. |
| 1930 |
|
| 1931 |
auto decoder = JxlDecoderMake (&memManager); |
| 1932 |
|
| 1933 |
auto dec = decoder.get (); |
| 1934 |
|
| 1935 |
// Preserve orientation as-in-bitstream (do not reorient). |
| 1936 |
|
| 1937 |
CheckResult (JxlDecoderSetKeepOrientation (dec, 1), |
| 1938 |
"JxlDecoderSetKeepOrientation", |
| 1939 |
nullptr); |
| 1940 |
|
| 1941 |
// Hook into our thread pool. |
| 1942 |
|
| 1943 |
dng_jxl_parallel_runner_data parallelData; |
| 1944 |
|
| 1945 |
parallelData.fHost = &host; |
| 1946 |
|
| 1947 |
#if 1 |
| 1948 |
|
| 1949 |
// This seems slightly faster than using our own runner; this might due to |
| 1950 |
// some internal overhead. |
| 1951 |
|
| 1952 |
auto nativeRunner = JxlResizableParallelRunnerMake (nullptr); |
| 1953 |
|
| 1954 |
const size_t maxThreadCount = |
| 1955 |
(size_t) (fUseSingleThread ? 1 : host.PerformAreaTaskThreads ()); |
| 1956 |
|
| 1957 |
JxlResizableParallelRunnerSetThreads (nativeRunner.get (), |
| 1958 |
maxThreadCount); |
| 1959 |
|
| 1960 |
CheckResult (JxlDecoderSetParallelRunner (dec, |
| 1961 |
JxlResizableParallelRunner, |
| 1962 |
nativeRunner.get ()), |
| 1963 |
"JxlDecoderSetParallelRunner", |
| 1964 |
¶llelData); |
| 1965 |
|
| 1966 |
#else |
| 1967 |
|
| 1968 |
CheckResult (JxlDecoderSetParallelRunner (dec, |
| 1969 |
dng_jxl_parallel_runner, |
| 1970 |
(void *) ¶llelData), |
| 1971 |
"JxlDecoderSetParallelRunner", |
| 1972 |
¶llelData); |
| 1973 |
|
| 1974 |
#endif |
| 1975 |
|
| 1976 |
// Subscribe to some events. |
| 1977 |
|
| 1978 |
// We will always retrieve the basic and color encoding info, so start |
| 1979 |
// with those events. |
| 1980 |
|
| 1981 |
int eventsWanted = (JXL_DEC_BASIC_INFO | |
| 1982 |
JXL_DEC_COLOR_ENCODING); |
| 1983 |
|
| 1984 |
// If meta is requested, then we need to read boxes. |
| 1985 |
|
| 1986 |
if (needBoxMeta) |
| 1987 |
{ |
| 1988 |
eventsWanted = eventsWanted | JXL_DEC_BOX; |
| 1989 |
} |
| 1990 |
|
| 1991 |
// If image is requested, then subscribe to image & frame events, too. |
| 1992 |
|
| 1993 |
// TODO(erichan): deal with preview image & thumbs |
| 1994 |
|
| 1995 |
if (needImage) |
| 1996 |
{ |
| 1997 |
eventsWanted = eventsWanted | JXL_DEC_FULL_IMAGE | JXL_DEC_FRAME; |
| 1998 |
} |
| 1999 |
|
| 2000 |
CheckResult (JxlDecoderSubscribeEvents (dec, eventsWanted), |
| 2001 |
"JxlDecoderSubscribeEvents", |
| 2002 |
¶llelData); |
| 2003 |
|
| 2004 |
// Decompress boxes. |
| 2005 |
|
| 2006 |
CheckResult (JxlDecoderSetDecompressBoxes (dec, JXL_TRUE), |
| 2007 |
"JxlDecoderSetDecompressBoxes", |
| 2008 |
¶llelData); |
| 2009 |
|
| 2010 |
// Input buffer. Do we want to customize the chunk sizes? |
| 2011 |
|
| 2012 |
// Settings these too small hurts decode perf. At first I tried 128 KB and |
| 2013 |
// 16 KB for max and chunk size, respectively. |
| 2014 |
|
| 2015 |
dng_jxl_io_buffer inputBuffer (host.Allocator (), |
| 2016 |
17 * 1024 * 1024, // max |
| 2017 |
8 * 1024 * 1024); // chunk |
| 2018 |
|
| 2019 |
JxlBasicInfo basicInfo; |
| 2020 |
|
| 2021 |
bool foundBasicInfo = false; |
| 2022 |
|
| 2023 |
JxlFrameHeader frameHeader; |
| 2024 |
|
| 2025 |
dng_jxl_box_reader boxReader (host, dec, *this); |
| 2026 |
|
| 2027 |
dng_jxl_decoder_callback_data cbData; |
| 2028 |
|
| 2029 |
// Main decode loop. |
| 2030 |
|
| 2031 |
for (;;) |
| 2032 |
{ |
| 2033 |
|
| 2034 |
// The first time, this will output JXL_DEC_NEED_MORE_INPUT because no |
| 2035 |
// input is set yet, this is ok since the input is set when handling this |
| 2036 |
// event. |
| 2037 |
|
| 2038 |
JxlDecoderStatus status = JxlDecoderProcessInput (dec); |
| 2039 |
|
| 2040 |
// Deal with decoder error event. |
| 2041 |
|
| 2042 |
if (status == JXL_DEC_ERROR) |
| 2043 |
{ |
| 2044 |
#if qLogJXL |
| 2045 |
printf ("Unknown jxl decoder error"); |
| 2046 |
#endif |
| 2047 |
ThrowBadFormat ("JXL_DEC_ERROR"); |
| 2048 |
} |
| 2049 |
|
| 2050 |
// Deal with success event. |
| 2051 |
|
| 2052 |
else if (status == JXL_DEC_SUCCESS) |
| 2053 |
{ |
| 2054 |
|
| 2055 |
// Finished all processing. |
| 2056 |
|
| 2057 |
#if qLogJXL |
| 2058 |
printf ("jxl decode success\n"); |
| 2059 |
#endif |
| 2060 |
|
| 2061 |
boxReader.CheckFinishBox (); |
| 2062 |
|
| 2063 |
break; |
| 2064 |
|
| 2065 |
} |
| 2066 |
|
| 2067 |
// Deal with decoder's request for more input data. |
| 2068 |
|
| 2069 |
else if (status == JXL_DEC_NEED_MORE_INPUT) |
| 2070 |
{ |
| 2071 |
|
| 2072 |
// The first time there is nothing to release and it returns 0, but that |
| 2073 |
// is ok. |
| 2074 |
|
| 2075 |
#if qLogJXL |
| 2076 |
printf ("--- JXL_DEC_NEED_MORE_INPUT\n"); |
| 2077 |
#endif |
| 2078 |
|
| 2079 |
size_t remaining = JxlDecoderReleaseInput (dec); |
| 2080 |
|
| 2081 |
inputBuffer.ReadChunk (stream, remaining); |
| 2082 |
|
| 2083 |
CheckResult (JxlDecoderSetInput (dec, |
| 2084 |
inputBuffer.Ptr (), |
| 2085 |
inputBuffer.DataSize ()), |
| 2086 |
"JxlDecoderSetInput", |
| 2087 |
¶llelData); |
| 2088 |
|
| 2089 |
// Let the decoder now if we've reached the end of the stream. |
| 2090 |
|
| 2091 |
if (stream.Position () >= stream.Length ()) |
| 2092 |
{ |
| 2093 |
JxlDecoderCloseInput (dec); |
| 2094 |
} |
| 2095 |
|
| 2096 |
} |
| 2097 |
|
| 2098 |
// Deal with basic info received from the code stream. |
| 2099 |
|
| 2100 |
else if (status == JXL_DEC_BASIC_INFO) |
| 2101 |
{ |
| 2102 |
|
| 2103 |
CheckResult (JxlDecoderGetBasicInfo (dec, &basicInfo), |
| 2104 |
"JxlDecoderGetBasicInfo", |
| 2105 |
¶llelData); |
| 2106 |
|
| 2107 |
foundBasicInfo = true; |
| 2108 |
|
| 2109 |
#if qDNGValidate |
| 2110 |
|
| 2111 |
if (needBoxMeta && gVerbose) |
| 2112 |
{ |
| 2113 |
|
| 2114 |
printf ("\nJXL basic info:\n\n"); |
| 2115 |
|
| 2116 |
printf ("dimensions: %ux%u\n", basicInfo.xsize, basicInfo.ysize); |
| 2117 |
printf ("have_container: %d\n", basicInfo.have_container); |
| 2118 |
printf ("uses_original_profile: %d\n", basicInfo.uses_original_profile); |
| 2119 |
printf ("bits_per_sample: %d\n", basicInfo.bits_per_sample); |
| 2120 |
|
| 2121 |
if (basicInfo.exponent_bits_per_sample) |
| 2122 |
printf ("float, with exponent_bits_per_sample: %d\n", |
| 2123 |
basicInfo.exponent_bits_per_sample); |
| 2124 |
|
| 2125 |
if (basicInfo.intensity_target != 255.f || |
| 2126 |
basicInfo.min_nits != 0.f || |
| 2127 |
basicInfo.relative_to_max_display != 0 || |
| 2128 |
basicInfo.relative_to_max_display != 0.f) |
| 2129 |
{ |
| 2130 |
printf ("intensity_target: %f\n", basicInfo.intensity_target); |
| 2131 |
printf ("min_nits: %f\n", basicInfo.min_nits); |
| 2132 |
printf ("relative_to_max_display: %d\n", basicInfo.relative_to_max_display); |
| 2133 |
printf ("linear_below: %f\n", basicInfo.linear_below); |
| 2134 |
} |
| 2135 |
|
| 2136 |
printf ("have_preview: %d\n", basicInfo.have_preview); |
| 2137 |
|
| 2138 |
if (basicInfo.have_preview) |
| 2139 |
{ |
| 2140 |
printf ("preview xsize: %u\n", basicInfo.preview.xsize); |
| 2141 |
printf ("preview ysize: %u\n", basicInfo.preview.ysize); |
| 2142 |
} |
| 2143 |
|
| 2144 |
printf ("have_animation: %d\n", basicInfo.have_animation); |
| 2145 |
|
| 2146 |
if (basicInfo.have_animation) |
| 2147 |
{ |
| 2148 |
|
| 2149 |
printf ("ticks per second (numerator / denominator): %u / %u\n", |
| 2150 |
basicInfo.animation.tps_numerator, basicInfo.animation.tps_denominator); |
| 2151 |
|
| 2152 |
printf ("num_loops: %u\n", basicInfo.animation.num_loops); |
| 2153 |
|
| 2154 |
printf ("have_timecodes: %d\n", basicInfo.animation.have_timecodes); |
| 2155 |
|
| 2156 |
} |
| 2157 |
|
| 2158 |
printf ("intrinsic xsize: %u\n", basicInfo.intrinsic_xsize); |
| 2159 |
printf ("intrinsic ysize: %u\n", basicInfo.intrinsic_ysize); |
| 2160 |
|
| 2161 |
const char* const orientation_string[8] = |
| 2162 |
{ |
| 2163 |
"Normal", "Flipped horizontally", |
| 2164 |
"Upside down", "Flipped vertically", |
| 2165 |
"Transposed", "90 degrees clockwise", |
| 2166 |
"Anti-Transposed", "90 degrees counter-clockwise" |
| 2167 |
}; |
| 2168 |
|
| 2169 |
if (basicInfo.orientation > 0 && basicInfo.orientation < 9) |
| 2170 |
{ |
| 2171 |
printf ("orientation: %d (%s)\n", basicInfo.orientation, |
| 2172 |
orientation_string[basicInfo.orientation - 1]); |
| 2173 |
} |
| 2174 |
|
| 2175 |
else |
| 2176 |
{ |
| 2177 |
fprintf (stderr, "Invalid orientation\n"); |
| 2178 |
} |
| 2179 |
|
| 2180 |
printf ("num_color_channels: %d\n", basicInfo.num_color_channels); |
| 2181 |
printf ("num_extra_channels: %d\n", basicInfo.num_extra_channels); |
| 2182 |
|
| 2183 |
// Read extra channels. |
| 2184 |
|
| 2185 |
const char* const ec_type_names [7] = |
| 2186 |
{ |
| 2187 |
"Alpha", |
| 2188 |
"Depth", |
| 2189 |
"Spot color", |
| 2190 |
"Selection mask", |
| 2191 |
"K (of CMYK)", |
| 2192 |
"CFA (Bayer data)", |
| 2193 |
"Thermal" |
| 2194 |
}; |
| 2195 |
|
| 2196 |
for (uint32_t i = 0; i < basicInfo.num_extra_channels; i++) |
| 2197 |
{ |
| 2198 |
|
| 2199 |
JxlExtraChannelInfo extra; |
| 2200 |
|
| 2201 |
CheckResult |
| 2202 |
(JxlDecoderGetExtraChannelInfo (dec, |
| 2203 |
i, |
| 2204 |
&extra), |
| 2205 |
"JxlDecoderGetExtraChannelInfo", |
| 2206 |
nullptr); |
| 2207 |
|
| 2208 |
printf ("extra channel %u:\n", i); |
| 2209 |
|
| 2210 |
printf (" type: %s\n", |
| 2211 |
(extra.type < 7 ? ec_type_names [extra.type] |
| 2212 |
: (extra.type == JXL_CHANNEL_OPTIONAL |
| 2213 |
? "Unknown but can be ignored" |
| 2214 |
: "Unknown, please update your libjxl"))); |
| 2215 |
|
| 2216 |
printf (" bits_per_sample: %u\n", |
| 2217 |
extra.bits_per_sample); |
| 2218 |
|
| 2219 |
if (extra.exponent_bits_per_sample > 0) |
| 2220 |
printf (" float, with exponent_bits_per_sample: %u\n", |
| 2221 |
extra.exponent_bits_per_sample); |
| 2222 |
|
| 2223 |
if (extra.dim_shift > 0) |
| 2224 |
printf (" dim_shift: %u (upsampled %ux)\n", |
| 2225 |
extra.dim_shift, |
| 2226 |
1 << extra.dim_shift); |
| 2227 |
|
| 2228 |
if (extra.name_length) |
| 2229 |
{ |
| 2230 |
|
| 2231 |
std::vector<char> temp (extra.name_length + 1); |
| 2232 |
|
| 2233 |
char *name = temp.data (); |
| 2234 |
|
| 2235 |
CheckResult (JxlDecoderGetExtraChannelName |
| 2236 |
(dec, |
| 2237 |
i, |
| 2238 |
name, |
| 2239 |
extra.name_length + 1), |
| 2240 |
"JxlDecoderGetExtraChannelName", |
| 2241 |
nullptr); |
| 2242 |
|
| 2243 |
printf (" name: %s\n", name); |
| 2244 |
|
| 2245 |
} |
| 2246 |
|
| 2247 |
if (extra.type == JXL_CHANNEL_ALPHA) |
| 2248 |
printf (" alpha_premultiplied: %d (%s)\n", |
| 2249 |
extra.alpha_premultiplied, |
| 2250 |
extra.alpha_premultiplied ? "Premultiplied" |
| 2251 |
: "Non-premultiplied"); |
| 2252 |
|
| 2253 |
if (extra.type == JXL_CHANNEL_SPOT_COLOR) |
| 2254 |
printf (" point_color: (%f, %f, %f) with opacity %f\n", |
| 2255 |
extra.spot_color[0], |
| 2256 |
extra.spot_color[1], |
| 2257 |
extra.spot_color[2], |
| 2258 |
extra.spot_color[3]); |
| 2259 |
|
| 2260 |
if (extra.type == JXL_CHANNEL_CFA) |
| 2261 |
printf (" cfa_channel: %u\n", extra.cfa_channel); |
| 2262 |
|
| 2263 |
} // for extra channels |
| 2264 |
|
| 2265 |
} // verbose |
| 2266 |
|
| 2267 |
#endif // qDNGValidate |
| 2268 |
|
| 2269 |
} // basic info |
| 2270 |
|
| 2271 |
// Read color profile info. |
| 2272 |
|
| 2273 |
else if (status == JXL_DEC_COLOR_ENCODING) |
| 2274 |
{ |
| 2275 |
|
| 2276 |
//JxlPixelFormat format = {4, JXL_TYPE_FLOAT, JXL_LITTLE_ENDIAN, 0}; |
| 2277 |
|
| 2278 |
// TODO(erichan): Does the choice of this format unduly affect the |
| 2279 |
// result of JxlDecoderGetColorAsEncodedProfile? |
| 2280 |
|
| 2281 |
#if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) |
| 2282 |
JxlPixelFormat format = { 3, JXL_TYPE_FLOAT, JXL_NATIVE_ENDIAN, 0 }; |
| 2283 |
#endif |
| 2284 |
|
| 2285 |
#if qDNGValidate |
| 2286 |
if (gVerbose) |
| 2287 |
printf("\nJXL color profile info:\n"); |
| 2288 |
#endif |
| 2289 |
|
| 2290 |
JxlColorEncoding color_encoding; |
| 2291 |
|
| 2292 |
if (JXL_DEC_SUCCESS == |
| 2293 |
JxlDecoderGetColorAsEncodedProfile (dec, |
| 2294 |
#if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) |
| 2295 |
&format, |
| 2296 |
#endif |
| 2297 |
JXL_COLOR_PROFILE_TARGET_ORIGINAL, |
| 2298 |
&color_encoding)) |
| 2299 |
{ |
| 2300 |
|
| 2301 |
// JXL-specific color encoding. |
| 2302 |
|
| 2303 |
// TODO(erichan): factory method? |
| 2304 |
|
| 2305 |
fColorSpaceInfo.fJxlColorEncoding.Reset (new JxlColorEncoding); |
| 2306 |
|
| 2307 |
*fColorSpaceInfo.fJxlColorEncoding = color_encoding; |
| 2308 |
|
| 2309 |
#if qDNGValidate |
| 2310 |
|
| 2311 |
if (gVerbose) |
| 2312 |
{ |
| 2313 |
|
| 2314 |
printf (" format: JPEG XL encoded color profile\n"); |
| 2315 |
|
| 2316 |
const char* const cs_string [4] = |
| 2317 |
{ |
| 2318 |
"RGB color", |
| 2319 |
"Grayscale", |
| 2320 |
"XYB", |
| 2321 |
"Unknown" |
| 2322 |
}; |
| 2323 |
|
| 2324 |
const char* const wp_string [12] = |
| 2325 |
{ |
| 2326 |
"", |
| 2327 |
"D65", |
| 2328 |
"Custom", |
| 2329 |
"", |
| 2330 |
"", |
| 2331 |
"", |
| 2332 |
"", |
| 2333 |
"", |
| 2334 |
"", |
| 2335 |
"", |
| 2336 |
"E", |
| 2337 |
"P3" |
| 2338 |
}; |
| 2339 |
|
| 2340 |
const char* const pr_string [12] = |
| 2341 |
{ |
| 2342 |
"", "sRGB", "Custom", "", |
| 2343 |
"", "", "", "", |
| 2344 |
"", "Rec.2100", "", "P3" |
| 2345 |
}; |
| 2346 |
|
| 2347 |
const char* const tf_string [19] = |
| 2348 |
{ |
| 2349 |
"", "709", "Unknown", "", "", |
| 2350 |
"", "", "", "Linear", "", |
| 2351 |
"", "", "", "sRGB", |
| 2352 |
"", "", "PQ", "DCI", "HLG" |
| 2353 |
}; |
| 2354 |
|
| 2355 |
const char* const ri_string [4] = |
| 2356 |
{ |
| 2357 |
"Perceptual", |
| 2358 |
"Relative", |
| 2359 |
"Saturation", |
| 2360 |
"Absolute" |
| 2361 |
}; |
| 2362 |
|
| 2363 |
printf (" color_space: %d (%s)\n", |
| 2364 |
color_encoding.color_space, |
| 2365 |
cs_string [color_encoding.color_space]); |
| 2366 |
|
| 2367 |
printf (" white_point: %d (%s)\n", |
| 2368 |
color_encoding.white_point, |
| 2369 |
wp_string[color_encoding.white_point]); |
| 2370 |
|
| 2371 |
if (color_encoding.white_point == JXL_WHITE_POINT_CUSTOM) |
| 2372 |
{ |
| 2373 |
|
| 2374 |
printf (" white_point XY: %f %f\n", |
| 2375 |
color_encoding.white_point_xy [0], |
| 2376 |
color_encoding.white_point_xy [1]); |
| 2377 |
|
| 2378 |
} |
| 2379 |
|
| 2380 |
if (color_encoding.color_space == JXL_COLOR_SPACE_RGB || |
| 2381 |
color_encoding.color_space == JXL_COLOR_SPACE_UNKNOWN) |
| 2382 |
{ |
| 2383 |
|
| 2384 |
printf (" primaries: %d (%s)\n", |
| 2385 |
color_encoding.primaries, |
| 2386 |
pr_string[color_encoding.primaries]); |
| 2387 |
|
| 2388 |
if (color_encoding.primaries == JXL_PRIMARIES_CUSTOM) |
| 2389 |
{ |
| 2390 |
|
| 2391 |
printf (" red primaries XY: %f %f\n", |
| 2392 |
color_encoding.primaries_red_xy [0], |
| 2393 |
color_encoding.primaries_red_xy [1]); |
| 2394 |
|
| 2395 |
printf (" green primaries XY: %f %f\n", |
| 2396 |
color_encoding.primaries_green_xy [0], |
| 2397 |
color_encoding.primaries_green_xy [1]); |
| 2398 |
|
| 2399 |
printf (" blue primaries XY: %f %f\n", |
| 2400 |
color_encoding.primaries_blue_xy [0], |
| 2401 |
color_encoding.primaries_blue_xy [1]); |
| 2402 |
|
| 2403 |
} |
| 2404 |
|
| 2405 |
} |
| 2406 |
|
| 2407 |
if (color_encoding.transfer_function == |
| 2408 |
JXL_TRANSFER_FUNCTION_GAMMA) |
| 2409 |
{ |
| 2410 |
|
| 2411 |
printf (" transfer_function: gamma: %f\n", |
| 2412 |
color_encoding.gamma); |
| 2413 |
|
| 2414 |
} |
| 2415 |
|
| 2416 |
else |
| 2417 |
{ |
| 2418 |
|
| 2419 |
printf (" transfer_function: %d (%s)\n", |
| 2420 |
color_encoding.transfer_function, |
| 2421 |
tf_string [color_encoding.transfer_function]); |
| 2422 |
|
| 2423 |
} |
| 2424 |
|
| 2425 |
printf (" rendering_intent: %d (%s)\n", |
| 2426 |
color_encoding.rendering_intent, |
| 2427 |
ri_string [color_encoding.rendering_intent]); |
| 2428 |
|
| 2429 |
} |
| 2430 |
|
| 2431 |
#endif // qDNGValidate |
| 2432 |
|
| 2433 |
} |
| 2434 |
|
| 2435 |
else |
| 2436 |
{ |
| 2437 |
|
| 2438 |
// The color info cannot be represented as a JxlColorEncoding, |
| 2439 |
// so fetch as ICC profile instead. |
| 2440 |
|
| 2441 |
size_t profile_size; |
| 2442 |
|
| 2443 |
CheckResult (JxlDecoderGetICCProfileSize |
| 2444 |
(dec, |
| 2445 |
#if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) |
| 2446 |
&format, |
| 2447 |
#endif |
| 2448 |
JXL_COLOR_PROFILE_TARGET_ORIGINAL, |
| 2449 |
&profile_size), |
| 2450 |
"JxlDecoderGetICCProfileSize", |
| 2451 |
nullptr); |
| 2452 |
|
| 2453 |
if (profile_size < 132) |
| 2454 |
{ |
| 2455 |
|
| 2456 |
#if qLogJXL |
| 2457 |
|
| 2458 |
printf ("ICC profile size %llu too small -- ignoring", |
| 2459 |
(unsigned long long) profile_size); |
| 2460 |
|
| 2461 |
#endif |
| 2462 |
|
| 2463 |
continue; |
| 2464 |
|
| 2465 |
} |
| 2466 |
|
| 2467 |
AutoPtr<dng_memory_block> profileBlock |
| 2468 |
(host.Allocate ((uint32) profile_size)); |
| 2469 |
|
| 2470 |
auto *profile = profileBlock->Buffer_uint8 (); |
| 2471 |
|
| 2472 |
CheckResult (JxlDecoderGetColorAsICCProfile |
| 2473 |
(dec, |
| 2474 |
#if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) |
| 2475 |
&format, |
| 2476 |
#endif |
| 2477 |
JXL_COLOR_PROFILE_TARGET_ORIGINAL, |
| 2478 |
profile, |
| 2479 |
profile_size), |
| 2480 |
"JxlDecoderGetColorAsICCProfile", |
| 2481 |
nullptr); |
| 2482 |
|
| 2483 |
fColorSpaceInfo.fICCProfile.Reset (profileBlock.Release ()); |
| 2484 |
|
| 2485 |
#if qDNGValidate |
| 2486 |
|
| 2487 |
if (gVerbose) |
| 2488 |
{ |
| 2489 |
|
| 2490 |
printf (" format: ICC profile\n"); |
| 2491 |
|
| 2492 |
printf (" ICC profile size: %u\n", (uint32) profile_size); |
| 2493 |
|
| 2494 |
printf (" CMM type: \"%.4s\"\n", profile + 4); |
| 2495 |
printf (" color space: \"%.4s\"\n", profile + 16); |
| 2496 |
printf (" rendering intent: %d\n", (int) profile [67]); |
| 2497 |
|
| 2498 |
} |
| 2499 |
|
| 2500 |
#endif // qDNGValidate |
| 2501 |
|
| 2502 |
} |
| 2503 |
|
| 2504 |
} // color profile info |
| 2505 |
|
| 2506 |
else if (status == JXL_DEC_FRAME) |
| 2507 |
{ |
| 2508 |
|
| 2509 |
CheckResult (JxlDecoderGetFrameHeader (dec, &frameHeader), |
| 2510 |
"JxlDecoderGetFrameHeader", |
| 2511 |
¶llelData); |
| 2512 |
|
| 2513 |
#if qDNGValidate |
| 2514 |
|
| 2515 |
if (gVerbose) |
| 2516 |
{ |
| 2517 |
|
| 2518 |
printf ("frame:\n"); |
| 2519 |
|
| 2520 |
if (frameHeader.name_length) |
| 2521 |
{ |
| 2522 |
|
| 2523 |
std::vector<char> temp (frameHeader.name_length + 1); |
| 2524 |
|
| 2525 |
char *name = temp.data (); |
| 2526 |
|
| 2527 |
CheckResult (JxlDecoderGetFrameName (dec, |
| 2528 |
name, |
| 2529 |
frameHeader.name_length + 1), |
| 2530 |
"JxlDecoderGetFrameName", |
| 2531 |
¶llelData); |
| 2532 |
|
| 2533 |
printf (" name: %s\n", name); |
| 2534 |
|
| 2535 |
} |
| 2536 |
|
| 2537 |
float ms = (frameHeader.duration * 1000.f * |
| 2538 |
basicInfo.animation.tps_denominator / |
| 2539 |
basicInfo.animation.tps_numerator); |
| 2540 |
|
| 2541 |
if (basicInfo.have_animation) |
| 2542 |
{ |
| 2543 |
|
| 2544 |
printf (" duration: %u ticks (%f ms)\n", |
| 2545 |
frameHeader.duration, |
| 2546 |
ms); |
| 2547 |
|
| 2548 |
if (basicInfo.animation.have_timecodes) |
| 2549 |
printf (" time code: %X\n", |
| 2550 |
frameHeader.timecode); |
| 2551 |
|
| 2552 |
} |
| 2553 |
|
| 2554 |
if (!frameHeader.name_length && |
| 2555 |
!basicInfo.have_animation) |
| 2556 |
{ |
| 2557 |
printf (" still frame, unnamed\n"); |
| 2558 |
} |
| 2559 |
|
| 2560 |
} // verbose |
| 2561 |
|
| 2562 |
#endif // qDNGValidate |
| 2563 |
|
| 2564 |
} |
| 2565 |
|
| 2566 |
// Box event. |
| 2567 |
|
| 2568 |
else if (status == JXL_DEC_BOX) |
| 2569 |
{ |
| 2570 |
|
| 2571 |
boxReader.HandleDecBox (); |
| 2572 |
|
| 2573 |
} |
| 2574 |
|
| 2575 |
// Handle request for writing box data. |
| 2576 |
|
| 2577 |
else if (status == JXL_DEC_BOX_NEED_MORE_OUTPUT) |
| 2578 |
{ |
| 2579 |
|
| 2580 |
#if qLogJXL |
| 2581 |
printf ("JXL_DEC_BOX_NEED_MORE_OUTPUT\n"); |
| 2582 |
#endif |
| 2583 |
|
| 2584 |
boxReader.HandleNeedMoreOutput (); |
| 2585 |
|
| 2586 |
} |
| 2587 |
|
| 2588 |
// Handle request for writing decoded image data. |
| 2589 |
|
| 2590 |
else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) |
| 2591 |
{ |
| 2592 |
|
| 2593 |
const uint32 colorPlanes = basicInfo.num_color_channels; |
| 2594 |
|
| 2595 |
if (colorPlanes != 1 && |
| 2596 |
colorPlanes != 3) |
| 2597 |
{ |
| 2598 |
#if qLogJXL |
| 2599 |
printf ("jxl: unsupported plane count"); |
| 2600 |
#endif |
| 2601 |
ThrowNotYetImplemented ("unsupported plane count"); |
| 2602 |
} |
| 2603 |
|
| 2604 |
uint32 pixelType = ttByte; |
| 2605 |
|
| 2606 |
if (basicInfo.exponent_bits_per_sample == 0) |
| 2607 |
{ |
| 2608 |
|
| 2609 |
// Integer. |
| 2610 |
|
| 2611 |
if (basicInfo.bits_per_sample <= 8) |
| 2612 |
pixelType = ttByte; |
| 2613 |
|
| 2614 |
else |
| 2615 |
{ |
| 2616 |
|
| 2617 |
pixelType = ttShort; |
| 2618 |
|
| 2619 |
#if qLogJXL |
| 2620 |
|
| 2621 |
if (basicInfo.bits_per_sample > 16) |
| 2622 |
printf ("jxl: bits per sample > 16; truncating to 16"); |
| 2623 |
|
| 2624 |
#endif |
| 2625 |
|
| 2626 |
} |
| 2627 |
|
| 2628 |
} |
| 2629 |
|
| 2630 |
else |
| 2631 |
{ |
| 2632 |
|
| 2633 |
// TODO(erichan): Do we want to try reading as fp16 if |
| 2634 |
// exponent_bits_per_sample is 5? For now, just read as fp32. |
| 2635 |
|
| 2636 |
// Float. |
| 2637 |
|
| 2638 |
pixelType = ttFloat; |
| 2639 |
|
| 2640 |
} |
| 2641 |
|
| 2642 |
JxlPixelFormat format; |
| 2643 |
|
| 2644 |
// Alpha support determines total # of planes. libjxl supports |
| 2645 |
// reading RGBA and Gray + Alpha interleaved. |
| 2646 |
|
| 2647 |
// TODO(erichan): Should we support premultipled alpha? |
| 2648 |
|
| 2649 |
// For now, only read alpha if we're reading the result into an |
| 2650 |
// image object. |
| 2651 |
|
| 2652 |
const bool doReadAlpha = ((basicInfo.alpha_bits > 0) && |
| 2653 |
!fUsePixelBuffer); |
| 2654 |
|
| 2655 |
const uint32 totalPlanes = doReadAlpha ? (colorPlanes + 1) : colorPlanes; |
| 2656 |
|
| 2657 |
format.num_channels = totalPlanes; |
| 2658 |
|
| 2659 |
if (pixelType == ttByte) |
| 2660 |
format.data_type = JXL_TYPE_UINT8; |
| 2661 |
|
| 2662 |
else if (pixelType == ttShort) |
| 2663 |
format.data_type = JXL_TYPE_UINT16; |
| 2664 |
|
| 2665 |
else |
| 2666 |
format.data_type = JXL_TYPE_FLOAT; |
| 2667 |
|
| 2668 |
format.endianness = JXL_NATIVE_ENDIAN; |
| 2669 |
|
| 2670 |
format.align = 0; |
| 2671 |
|
| 2672 |
#if 1 |
| 2673 |
|
| 2674 |
// Do incremental path. This seems significantly faster than using |
| 2675 |
// the one-image path. I wonder if this has to do with parallelism. |
| 2676 |
|
| 2677 |
dng_point size; |
| 2678 |
|
| 2679 |
size.h = basicInfo.xsize; |
| 2680 |
size.v = basicInfo.ysize; |
| 2681 |
|
| 2682 |
// Set up chunky (row-col-plane interleaved) template buffer. |
| 2683 |
|
| 2684 |
auto &buffer = cbData.fTemplateBuffer; |
| 2685 |
|
| 2686 |
buffer.fArea = dng_rect (size); |
| 2687 |
|
| 2688 |
buffer.fPlanes = totalPlanes; |
| 2689 |
|
| 2690 |
buffer.fPlaneStep = 1; |
| 2691 |
buffer.fColStep = totalPlanes; |
| 2692 |
buffer.fRowStep = buffer.fColStep * size.h; |
| 2693 |
|
| 2694 |
buffer.fPixelType = pixelType; |
| 2695 |
buffer.fPixelSize = TagTypeSize (buffer.fPixelType); |
| 2696 |
|
| 2697 |
buffer.fData = nullptr; |
| 2698 |
|
| 2699 |
// Create image to hold output. |
| 2700 |
|
| 2701 |
if (!fUsePixelBuffer) |
| 2702 |
{ |
| 2703 |
|
| 2704 |
cbData.fImage.Reset (host.Make_dng_image (dng_rect (size), |
| 2705 |
colorPlanes, |
| 2706 |
pixelType)); |
| 2707 |
|
| 2708 |
if (doReadAlpha) |
| 2709 |
{ |
| 2710 |
|
| 2711 |
cbData.fAlphaMask.Reset (host.Make_dng_image (dng_rect (size), |
| 2712 |
1, |
| 2713 |
pixelType)); |
| 2714 |
|
| 2715 |
cbData.fAlphaPremultiplied = basicInfo.alpha_premultiplied; |
| 2716 |
|
| 2717 |
} |
| 2718 |
|
| 2719 |
} |
| 2720 |
|
| 2721 |
// Create temp planar (row-plane-col interleaved) buffer for the |
| 2722 |
// whole image. This is wasteful from a memory point of view but |
| 2723 |
// accelerates decoding significantly. |
| 2724 |
|
| 2725 |
auto &wholeBuffer = cbData.fWholeBuffer; |
| 2726 |
|
| 2727 |
wholeBuffer = buffer; |
| 2728 |
|
| 2729 |
wholeBuffer.fColStep = 1; |
| 2730 |
wholeBuffer.fPlaneStep = size.h; |
| 2731 |
wholeBuffer.fRowStep = wholeBuffer.fPlaneStep * totalPlanes; |
| 2732 |
|
| 2733 |
const uint64 bytesNeeded = (uint64 (wholeBuffer.fRowStep) * |
| 2734 |
uint64 (wholeBuffer.fArea.H ()) * |
| 2735 |
uint64 (wholeBuffer.fPixelSize)); |
| 2736 |
|
| 2737 |
cbData.fBlock.Reset (new jxl_memory_block (host.Allocator (), |
| 2738 |
bytesNeeded)); |
| 2739 |
|
| 2740 |
cbData.fWholeBuffer.fData = cbData.fBlock->Buffer (); |
| 2741 |
|
| 2742 |
CheckResult |
| 2743 |
(JxlDecoderSetImageOutCallback |
| 2744 |
(dec, |
| 2745 |
&format, |
| 2746 |
dng_jxl_decoder_callback_func, |
| 2747 |
&cbData), |
| 2748 |
"JxlDecoderSetImageOutCallback", |
| 2749 |
¶llelData); |
| 2750 |
|
| 2751 |
#else |
| 2752 |
|
| 2753 |
(void) dng_jxl_decoder_callback_func; |
| 2754 |
|
| 2755 |
// Full-image path. |
| 2756 |
|
| 2757 |
size_t size; |
| 2758 |
|
| 2759 |
CheckResult (JxlDecoderImageOutBufferSize (dec, |
| 2760 |
&format, |
| 2761 |
&size), |
| 2762 |
"JxlDecoderImageOutBufferSize", |
| 2763 |
¶llelData); |
| 2764 |
|
| 2765 |
block.Reset (host.Allocate ((uint32) size)); |
| 2766 |
|
| 2767 |
CheckResult (JxlDecoderSetImageOutBuffer (dec, |
| 2768 |
&format, |
| 2769 |
block->Buffer (), |
| 2770 |
size), |
| 2771 |
"JxlDecoderSetImageOutBuffer", |
| 2772 |
¶llelData); |
| 2773 |
|
| 2774 |
#endif // incremental vs full |
| 2775 |
|
| 2776 |
} |
| 2777 |
|
| 2778 |
// Event that full image has finished decoding or we're past this |
| 2779 |
// point in the code stream. |
| 2780 |
|
| 2781 |
else if (status == JXL_DEC_FULL_IMAGE) |
| 2782 |
{ |
| 2783 |
|
| 2784 |
#if qLogJXL |
| 2785 |
printf ("--- JXL_DEC_FULL_IMAGE\n"); |
| 2786 |
#endif |
| 2787 |
|
| 2788 |
if (needImage) |
| 2789 |
{ |
| 2790 |
|
| 2791 |
if (cbData.fImage.Get ()) |
| 2792 |
{ |
| 2793 |
|
| 2794 |
#if 1 |
| 2795 |
|
| 2796 |
// Multi-threaded put: much faster. |
| 2797 |
|
| 2798 |
auto &dstImage = *cbData.fImage; |
| 2799 |
|
| 2800 |
dng_put_buffer_task task (cbData.fWholeBuffer, |
| 2801 |
dstImage); |
| 2802 |
|
| 2803 |
host.PerformAreaTask (task, |
| 2804 |
dstImage.Bounds ()); |
| 2805 |
|
| 2806 |
#else |
| 2807 |
|
| 2808 |
// Single-threaded put -- expensive. |
| 2809 |
|
| 2810 |
cbData.fImage->Put (cbData.fWholeBuffer); |
| 2811 |
|
| 2812 |
#endif |
| 2813 |
|
| 2814 |
} |
| 2815 |
|
| 2816 |
if (cbData.fAlphaMask.Get ()) |
| 2817 |
{ |
| 2818 |
|
| 2819 |
auto &dstImage = *cbData.fAlphaMask; |
| 2820 |
|
| 2821 |
dng_put_alpha_buffer_task task (cbData.fWholeBuffer, |
| 2822 |
dstImage); |
| 2823 |
|
| 2824 |
host.PerformAreaTask (task, |
| 2825 |
dstImage.Bounds ()); |
| 2826 |
|
| 2827 |
} |
| 2828 |
|
| 2829 |
} |
| 2830 |
|
| 2831 |
} |
| 2832 |
|
| 2833 |
// Deal with unhandled events. |
| 2834 |
|
| 2835 |
else |
| 2836 |
{ |
| 2837 |
|
| 2838 |
#if qLogJXL |
| 2839 |
|
| 2840 |
printf ("Unexpected jxl decoder status 0x%x\n", |
| 2841 |
(unsigned) status); |
| 2842 |
|
| 2843 |
#endif |
| 2844 |
|
| 2845 |
ThrowBadFormat ("unhandled jxl decoder status"); |
| 2846 |
|
| 2847 |
} |
| 2848 |
|
| 2849 |
} // jxl decode loop |
| 2850 |
|
| 2851 |
// Finished decode loop. |
| 2852 |
|
| 2853 |
// Allocate info structures in case there is no |
| 2854 |
// exif block. |
| 2855 |
|
| 2856 |
if (foundBasicInfo) |
| 2857 |
{ |
| 2858 |
|
| 2859 |
const auto &src = basicInfo; |
| 2860 |
|
| 2861 |
if (fInfo) |
| 2862 |
{ |
| 2863 |
|
| 2864 |
auto &info = *fInfo; |
| 2865 |
|
| 2866 |
// Store magic number. |
| 2867 |
|
| 2868 |
info.fMagic = tcJXL; |
| 2869 |
|
| 2870 |
// Create core objects if not already done. |
| 2871 |
|
| 2872 |
if (!info.fExif.Get ()) |
| 2873 |
{ |
| 2874 |
info.fExif.Reset (host.Make_dng_exif ()); |
| 2875 |
} |
| 2876 |
|
| 2877 |
if (!info.fShared.Get ()) |
| 2878 |
{ |
| 2879 |
info.fShared.Reset (host.Make_dng_shared ()); |
| 2880 |
} |
| 2881 |
|
| 2882 |
if (info.IFDCount () == 0) |
| 2883 |
{ |
| 2884 |
info.fIFD.push_back (host.Make_dng_ifd ()); |
| 2885 |
} |
| 2886 |
|
| 2887 |
// Store some essential info so that the reader can make some |
| 2888 |
// early decisions about whether or not to support the file. |
| 2889 |
|
| 2890 |
auto &ifd = *info.fIFD [0]; |
| 2891 |
|
| 2892 |
ifd.fImageWidth = (uint32) src.xsize; |
| 2893 |
ifd.fImageLength = (uint32) src.ysize; |
| 2894 |
|
| 2895 |
ifd.fBitsPerSample [0] = (uint32) src.bits_per_sample; |
| 2896 |
|
| 2897 |
ifd.fSamplesPerPixel = (uint32) src.num_color_channels; |
| 2898 |
|
| 2899 |
ifd.fOrientation = (uint32) src.orientation; |
| 2900 |
|
| 2901 |
if (src.exponent_bits_per_sample > 0) |
| 2902 |
ifd.fSampleFormat [0] = sfFloatingPoint; |
| 2903 |
|
| 2904 |
else |
| 2905 |
ifd.fSampleFormat [0] = sfUnsignedInteger; |
| 2906 |
|
| 2907 |
// Remember the stream position. |
| 2908 |
|
| 2909 |
ifd.fTileOffset [0] = (uint64) startPosition; |
| 2910 |
|
| 2911 |
} |
| 2912 |
|
| 2913 |
// Store basic info. |
| 2914 |
|
| 2915 |
fUsesOriginalProfile = (src.uses_original_profile != JXL_FALSE); |
| 2916 |
|
| 2917 |
fMainImageSize.h = (int32) src.xsize; |
| 2918 |
fMainImageSize.v = (int32) src.ysize; |
| 2919 |
|
| 2920 |
fMainImagePlanes = (uint32) src.num_color_channels; |
| 2921 |
|
| 2922 |
fBitsPerSample = (uint32) src.bits_per_sample; |
| 2923 |
fExponentBitsPerSample = (uint32) src.exponent_bits_per_sample; |
| 2924 |
|
| 2925 |
fNumExtraChannels = (uint32) src.num_extra_channels; |
| 2926 |
|
| 2927 |
fHasPreview = (src.have_preview != JXL_FALSE); |
| 2928 |
|
| 2929 |
fOrientation.SetTIFF ((uint32) src.orientation); |
| 2930 |
|
| 2931 |
// Store image. |
| 2932 |
|
| 2933 |
if (fUsePixelBuffer) |
| 2934 |
{ |
| 2935 |
|
| 2936 |
fMainPixelBuffer.Reset |
| 2937 |
(new dng_pixel_buffer (cbData.fWholeBuffer)); |
| 2938 |
|
| 2939 |
DNG_REQUIRE (!cbData.fBlock->fUseMalloc, |
| 2940 |
"Expected dng_memory_block use for pixel buffer JXL decode"); |
| 2941 |
|
| 2942 |
DNG_REQUIRE (cbData.fBlock->fBlock.Get (), |
| 2943 |
"Missing dng_memory_block for pixel buffer JXL decode"); |
| 2944 |
|
| 2945 |
fMainBlock.Reset (cbData.fBlock->fBlock.Release ()); |
| 2946 |
|
| 2947 |
} |
| 2948 |
|
| 2949 |
else |
| 2950 |
{ |
| 2951 |
|
| 2952 |
fMainImage.Reset (cbData.fImage.Release ()); |
| 2953 |
|
| 2954 |
fAlphaMask.Reset (cbData.fAlphaMask.Release ()); |
| 2955 |
|
| 2956 |
if (fAlphaMask.Get ()) |
| 2957 |
fAlphaPremultiplied = cbData.fAlphaPremultiplied; |
| 2958 |
|
| 2959 |
} |
| 2960 |
|
| 2961 |
// TODO(erichan): deal with depth, etc. |
| 2962 |
|
| 2963 |
// .... |
| 2964 |
|
| 2965 |
} |
| 2966 |
|
| 2967 |
} |
| 2968 |
|
| 2969 |
/*****************************************************************************/ |
| 2970 |
|
| 2971 |
void dng_jxl_decoder::ProcessExifBox (dng_host &host, |
| 2972 |
const std::vector<uint8> &data) |
| 2973 |
{ |
| 2974 |
|
| 2975 |
// First 4 bytes are a TIFF offset which we currently assume is zero. |
| 2976 |
|
| 2977 |
if (fInfo && (data.size () > 4)) |
| 2978 |
{ |
| 2979 |
|
| 2980 |
dng_stream exifStream ((&data [0]) + 4, |
| 2981 |
uint32 (data.size () - 4)); |
| 2982 |
|
| 2983 |
fInfo->Parse (host, exifStream); |
| 2984 |
|
| 2985 |
} |
| 2986 |
|
| 2987 |
} |
| 2988 |
|
| 2989 |
/*****************************************************************************/ |
| 2990 |
|
| 2991 |
void dng_jxl_decoder::ProcessXMPBox (dng_host &host, |
| 2992 |
const std::vector<uint8> &data) |
| 2993 |
{ |
| 2994 |
|
| 2995 |
#if qDNGValidate |
| 2996 |
|
| 2997 |
if (gVerbose) |
| 2998 |
{ |
| 2999 |
|
| 3000 |
dng_memory_stream temp (host.Allocator ()); |
| 3001 |
|
| 3002 |
uint32 count = (uint32) data.size (); |
| 3003 |
|
| 3004 |
temp.Put (data.data (), count); |
| 3005 |
|
| 3006 |
temp.SetReadPosition (0); |
| 3007 |
|
| 3008 |
DumpXMP (temp, count); |
| 3009 |
|
| 3010 |
} |
| 3011 |
|
| 3012 |
#endif // qDNGValidate |
| 3013 |
|
| 3014 |
if (fInfo) |
| 3015 |
{ |
| 3016 |
|
| 3017 |
try |
| 3018 |
{ |
| 3019 |
|
| 3020 |
dng_xmp xmp (host.Allocator ()); |
| 3021 |
|
| 3022 |
uint32 count = (uint32) data.size (); |
| 3023 |
|
| 3024 |
xmp.Parse (host, data.data (), count); |
| 3025 |
|
| 3026 |
auto &block = fInfo->fXMPBlock; |
| 3027 |
|
| 3028 |
if (!block.Get ()) |
| 3029 |
{ |
| 3030 |
|
| 3031 |
block.Reset (host.Allocate (count)); |
| 3032 |
|
| 3033 |
memcpy (block->Buffer (), |
| 3034 |
data.data (), |
| 3035 |
(size_t) count); |
| 3036 |
|
| 3037 |
} |
| 3038 |
|
| 3039 |
} |
| 3040 |
|
| 3041 |
catch (...) |
| 3042 |
{ |
| 3043 |
|
| 3044 |
// Not XMP -- ignore. |
| 3045 |
|
| 3046 |
} |
| 3047 |
|
| 3048 |
} |
| 3049 |
|
| 3050 |
} |
| 3051 |
|
| 3052 |
/*****************************************************************************/ |
| 3053 |
|
| 3054 |
void dng_jxl_decoder::ProcessBox (dng_host & /* host */, |
| 3055 |
const dng_string & /* name */, |
| 3056 |
const std::vector<uint8> & /* data */) |
| 3057 |
{ |
| 3058 |
|
| 3059 |
} |
| 3060 |
|
| 3061 |
/*****************************************************************************/ |
| 3062 |
|
| 3063 |
static bool DoParseJXL (dng_host &host, |
| 3064 |
dng_stream &stream, |
| 3065 |
dng_info &info) |
| 3066 |
{ |
| 3067 |
|
| 3068 |
try |
| 3069 |
{ |
| 3070 |
|
| 3071 |
dng_jxl_decoder decoder; |
| 3072 |
|
| 3073 |
decoder.fInfo = &info; |
| 3074 |
|
| 3075 |
decoder.fNeedImage = false; |
| 3076 |
|
| 3077 |
decoder.Decode (host, |
| 3078 |
stream); |
| 3079 |
|
| 3080 |
return true; |
| 3081 |
|
| 3082 |
} |
| 3083 |
|
| 3084 |
catch (const dng_exception &except) |
| 3085 |
{ |
| 3086 |
|
| 3087 |
// If we found a decoder error while parsing, then assume this is not |
| 3088 |
// a valid JXL file. |
| 3089 |
|
| 3090 |
if (except.ErrorCode () == dng_error_jxl_decoder) |
| 3091 |
{ |
| 3092 |
return false; |
| 3093 |
} |
| 3094 |
|
| 3095 |
// For any other type of error (user cancellation, memory full, eof of |
| 3096 |
// file, etc.) re-throw. |
| 3097 |
|
| 3098 |
// TODO(erichan): do we really want to throw for end of stream/file? |
| 3099 |
|
| 3100 |
throw; |
| 3101 |
|
| 3102 |
} |
| 3103 |
|
| 3104 |
return false; |
| 3105 |
|
| 3106 |
} |
| 3107 |
|
| 3108 |
/*****************************************************************************/ |
| 3109 |
|
| 3110 |
static bool ParseJXL_Basic (dng_host &host, |
| 3111 |
dng_stream &stream, |
| 3112 |
dng_info &info) |
| 3113 |
{ |
| 3114 |
|
| 3115 |
if (stream.Length () < 2) |
| 3116 |
{ |
| 3117 |
return false; |
| 3118 |
} |
| 3119 |
|
| 3120 |
const auto pos = stream.Position (); |
| 3121 |
|
| 3122 |
// Look for JPEG marker that indicates start of JXL codestream. |
| 3123 |
|
| 3124 |
if (stream.Get_uint8 () != 0xFF) |
| 3125 |
{ |
| 3126 |
return false; |
| 3127 |
} |
| 3128 |
|
| 3129 |
if (stream.Get_uint8 () != 0x0A) |
| 3130 |
{ |
| 3131 |
return false; |
| 3132 |
} |
| 3133 |
|
| 3134 |
#if qDNGValidate |
| 3135 |
|
| 3136 |
if (gVerbose) |
| 3137 |
{ |
| 3138 |
printf ("\nJXL (basic) file found\n"); |
| 3139 |
} |
| 3140 |
|
| 3141 |
#endif |
| 3142 |
|
| 3143 |
// Reset stream position. |
| 3144 |
|
| 3145 |
stream.SetReadPosition (pos); |
| 3146 |
|
| 3147 |
return DoParseJXL (host, stream, info); |
| 3148 |
|
| 3149 |
} |
| 3150 |
|
| 3151 |
/*****************************************************************************/ |
| 3152 |
|
| 3153 |
static bool ParseJXL_Container (dng_host &host, |
| 3154 |
dng_stream &stream, |
| 3155 |
dng_info &info) |
| 3156 |
{ |
| 3157 |
|
| 3158 |
// Look for start of BMFF-based JXL container. |
| 3159 |
|
| 3160 |
if (stream.Length () < 12) |
| 3161 |
{ |
| 3162 |
return false; |
| 3163 |
} |
| 3164 |
|
| 3165 |
const auto pos = stream.Position (); |
| 3166 |
|
| 3167 |
uint8 buffer [12]; |
| 3168 |
|
| 3169 |
stream.Get (buffer, 12); |
| 3170 |
|
| 3171 |
uint8 jxlBuffer [] = |
| 3172 |
{ |
| 3173 |
0x00, 0x00, 0x00, 0x0C, |
| 3174 |
0x4A, 0x58, 0x4C, 0x20, |
| 3175 |
0x0D, 0x0A, 0x87, 0x0A |
| 3176 |
}; |
| 3177 |
|
| 3178 |
if (memcmp (buffer, jxlBuffer, 12) != 0) |
| 3179 |
{ |
| 3180 |
return false; |
| 3181 |
} |
| 3182 |
|
| 3183 |
#if qDNGValidate |
| 3184 |
|
| 3185 |
if (gVerbose) |
| 3186 |
{ |
| 3187 |
printf ("\nJXL (container) file found\n"); |
| 3188 |
} |
| 3189 |
|
| 3190 |
#endif |
| 3191 |
|
| 3192 |
// Reset stream position. |
| 3193 |
|
| 3194 |
stream.SetReadPosition (pos); |
| 3195 |
|
| 3196 |
return DoParseJXL (host, stream, info); |
| 3197 |
|
| 3198 |
} |
| 3199 |
|
| 3200 |
/*****************************************************************************/ |
| 3201 |
|
| 3202 |
bool ParseJXL (dng_host &host, |
| 3203 |
dng_stream &stream, |
| 3204 |
dng_info &info, |
| 3205 |
bool supportBasicCodeStream, |
| 3206 |
bool supportContainer) |
| 3207 |
{ |
| 3208 |
|
| 3209 |
const uint64 startPosition = stream.Position (); |
| 3210 |
|
| 3211 |
if (supportBasicCodeStream) |
| 3212 |
{ |
| 3213 |
|
| 3214 |
if (ParseJXL_Basic (host, stream, info)) |
| 3215 |
return true; |
| 3216 |
|
| 3217 |
stream.SetReadPosition (startPosition); |
| 3218 |
|
| 3219 |
} |
| 3220 |
|
| 3221 |
if (supportContainer) |
| 3222 |
{ |
| 3223 |
|
| 3224 |
if (ParseJXL_Container (host, stream, info)) |
| 3225 |
return true; |
| 3226 |
|
| 3227 |
} |
| 3228 |
|
| 3229 |
return false; |
| 3230 |
|
| 3231 |
} |
| 3232 |
|
| 3233 |
/*****************************************************************************/ |
| 3234 |
|
| 3235 |
void EncodeJXL_Tile (dng_host &host, |
| 3236 |
dng_stream &stream, |
| 3237 |
const dng_image &image, |
| 3238 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 3239 |
const dng_jxl_encode_settings &settings) |
| 3240 |
{ |
| 3241 |
|
| 3242 |
EncodeJXL (host, |
| 3243 |
stream, |
| 3244 |
image, |
| 3245 |
settings, |
| 3246 |
false, // use container |
| 3247 |
colorSpaceInfo, |
| 3248 |
nullptr, // meta |
| 3249 |
false, |
| 3250 |
false, |
| 3251 |
false, |
| 3252 |
nullptr); // gain map meta |
| 3253 |
|
| 3254 |
} |
| 3255 |
|
| 3256 |
/*****************************************************************************/ |
| 3257 |
|
| 3258 |
void EncodeJXL_Tile (dng_host &host, |
| 3259 |
dng_stream &stream, |
| 3260 |
const dng_pixel_buffer &buffer, |
| 3261 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 3262 |
const dng_jxl_encode_settings &settings) |
| 3263 |
{ |
| 3264 |
|
| 3265 |
EncodeJXL (host, |
| 3266 |
stream, |
| 3267 |
buffer, |
| 3268 |
settings, |
| 3269 |
false, // use container |
| 3270 |
colorSpaceInfo, |
| 3271 |
nullptr, // meta |
| 3272 |
false, |
| 3273 |
false, |
| 3274 |
false, |
| 3275 |
nullptr); // gain map meta |
| 3276 |
|
| 3277 |
} |
| 3278 |
|
| 3279 |
/*****************************************************************************/ |
| 3280 |
|
| 3281 |
void EncodeJXL_Container (dng_host &host, |
| 3282 |
dng_stream &stream, |
| 3283 |
const dng_image &image, |
| 3284 |
const dng_jxl_encode_settings &settings, |
| 3285 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 3286 |
const dng_metadata *metadata, |
| 3287 |
const bool includeExif, |
| 3288 |
const bool includeXMP, |
| 3289 |
const bool includeIPTC, |
| 3290 |
const dng_bmff_box_list *additionalBoxes) |
| 3291 |
{ |
| 3292 |
|
| 3293 |
EncodeJXL (host, |
| 3294 |
stream, |
| 3295 |
image, |
| 3296 |
settings, |
| 3297 |
true, // use container |
| 3298 |
colorSpaceInfo, |
| 3299 |
metadata, |
| 3300 |
includeExif, |
| 3301 |
includeXMP, |
| 3302 |
includeIPTC, |
| 3303 |
additionalBoxes); |
| 3304 |
|
| 3305 |
} |
| 3306 |
|
| 3307 |
/*****************************************************************************/ |
| 3308 |
|
| 3309 |
void EncodeJXL_Container (dng_host &host, |
| 3310 |
dng_stream &stream, |
| 3311 |
const dng_pixel_buffer &buffer, |
| 3312 |
const dng_jxl_encode_settings &settings, |
| 3313 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 3314 |
const dng_metadata *metadata, |
| 3315 |
const bool includeExif, |
| 3316 |
const bool includeXMP, |
| 3317 |
const bool includeIPTC, |
| 3318 |
const dng_bmff_box_list *additionalBoxes) |
| 3319 |
{ |
| 3320 |
|
| 3321 |
EncodeJXL (host, |
| 3322 |
stream, |
| 3323 |
buffer, |
| 3324 |
settings, |
| 3325 |
true, // use container |
| 3326 |
colorSpaceInfo, |
| 3327 |
metadata, |
| 3328 |
includeExif, |
| 3329 |
includeXMP, |
| 3330 |
includeIPTC, |
| 3331 |
additionalBoxes); |
| 3332 |
|
| 3333 |
} |
| 3334 |
|
| 3335 |
/*****************************************************************************/ |
| 3336 |
|
| 3337 |
real32 JXLQualityToDistance (uint32 quality) |
| 3338 |
{ |
| 3339 |
|
| 3340 |
quality = Pin_uint32 (kMinJXLCompressionQuality, |
| 3341 |
quality, |
| 3342 |
kMaxJXLCompressionQuality); |
| 3343 |
|
| 3344 |
// 1 to 13 scale. |
| 3345 |
|
| 3346 |
static const real32 kTable [] = |
| 3347 |
{ |
| 3348 |
0.0f, // 0 (unused) |
| 3349 |
8.0f, // 1 |
| 3350 |
6.0f, // 2 |
| 3351 |
5.0f, // 3 |
| 3352 |
4.0f, // 4 |
| 3353 |
3.0f, // 5 |
| 3354 |
2.0f, // 6 |
| 3355 |
1.6f, // 7 |
| 3356 |
1.3f, // 8 |
| 3357 |
1.0f, // 9 |
| 3358 |
0.7f, // 10 |
| 3359 |
0.4f, // 11 |
| 3360 |
0.1f, // 12 |
| 3361 |
0.0f, // 13 (lossless) |
| 3362 |
}; |
| 3363 |
|
| 3364 |
return kTable [quality]; |
| 3365 |
|
| 3366 |
} |
| 3367 |
|
| 3368 |
/*****************************************************************************/ |
| 3369 |
|
| 3370 |
dng_jxl_encode_settings * JXLQualityToSettings (uint32 quality) |
| 3371 |
{ |
| 3372 |
|
| 3373 |
AutoPtr<dng_jxl_encode_settings> settings (new dng_jxl_encode_settings); |
| 3374 |
|
| 3375 |
real32 d = JXLQualityToDistance (quality); |
| 3376 |
|
| 3377 |
settings->SetDistance (d); |
| 3378 |
|
| 3379 |
if (d <= 0.0f) |
| 3380 |
settings->SetFastest (); // Use fast lossless path |
| 3381 |
|
| 3382 |
return settings.Release (); |
| 3383 |
|
| 3384 |
} |
| 3385 |
|
| 3386 |
/*****************************************************************************/ |
| 3387 |
|
| 3388 |
void PreviewColorSpaceToJXLEncoding (const PreviewColorSpaceEnum colorSpace, |
| 3389 |
const uint32 planes, |
| 3390 |
dng_jxl_color_space_info &info) |
| 3391 |
{ |
| 3392 |
|
| 3393 |
#if qLogJXL |
| 3394 |
printf ("jxl preview info color space enum: %d\n", |
| 3395 |
int (colorSpace)); |
| 3396 |
#endif |
| 3397 |
|
| 3398 |
info.fJxlColorEncoding.Reset (new JxlColorEncoding); |
| 3399 |
|
| 3400 |
auto &encoding = *info.fJxlColorEncoding; |
| 3401 |
|
| 3402 |
memset (&encoding, 0, sizeof (encoding)); |
| 3403 |
|
| 3404 |
switch (colorSpace) |
| 3405 |
{ |
| 3406 |
|
| 3407 |
case previewColorSpace_GrayGamma22: |
| 3408 |
{ |
| 3409 |
encoding.color_space = JXL_COLOR_SPACE_GRAY; |
| 3410 |
encoding.white_point = JXL_WHITE_POINT_D65; // unused |
| 3411 |
encoding.primaries = JXL_PRIMARIES_SRGB; // unused |
| 3412 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA; |
| 3413 |
encoding.gamma = 1.0 / 2.2; |
| 3414 |
break; |
| 3415 |
} |
| 3416 |
|
| 3417 |
case previewColorSpace_sRGB: |
| 3418 |
{ |
| 3419 |
encoding.color_space = JXL_COLOR_SPACE_RGB; |
| 3420 |
encoding.white_point = JXL_WHITE_POINT_D65; |
| 3421 |
encoding.primaries = JXL_PRIMARIES_SRGB; |
| 3422 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_SRGB; |
| 3423 |
break; |
| 3424 |
} |
| 3425 |
|
| 3426 |
case previewColorSpace_AdobeRGB: |
| 3427 |
{ |
| 3428 |
|
| 3429 |
encoding.color_space = JXL_COLOR_SPACE_RGB; |
| 3430 |
encoding.white_point = JXL_WHITE_POINT_D65; |
| 3431 |
encoding.primaries = JXL_PRIMARIES_CUSTOM; |
| 3432 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA; |
| 3433 |
encoding.gamma = 1.0 / 2.2; |
| 3434 |
|
| 3435 |
encoding.primaries_red_xy [0] = 0.6400; |
| 3436 |
encoding.primaries_red_xy [1] = 0.3300; |
| 3437 |
|
| 3438 |
encoding.primaries_green_xy [0] = 0.2100; |
| 3439 |
encoding.primaries_green_xy [1] = 0.7100; |
| 3440 |
|
| 3441 |
encoding.primaries_blue_xy [0] = 0.1500; |
| 3442 |
encoding.primaries_blue_xy [1] = 0.0600; |
| 3443 |
|
| 3444 |
break; |
| 3445 |
|
| 3446 |
} |
| 3447 |
|
| 3448 |
case previewColorSpace_ProPhotoRGB: |
| 3449 |
{ |
| 3450 |
|
| 3451 |
encoding.color_space = JXL_COLOR_SPACE_RGB; |
| 3452 |
encoding.white_point = JXL_WHITE_POINT_CUSTOM; |
| 3453 |
encoding.primaries = JXL_PRIMARIES_CUSTOM; |
| 3454 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA; |
| 3455 |
encoding.gamma = 1.0 / 1.8; |
| 3456 |
|
| 3457 |
encoding.primaries_red_xy [0] = 0.734699; |
| 3458 |
encoding.primaries_red_xy [1] = 0.265301; |
| 3459 |
|
| 3460 |
encoding.primaries_green_xy [0] = 0.159597; |
| 3461 |
encoding.primaries_green_xy [1] = 0.840403; |
| 3462 |
|
| 3463 |
encoding.primaries_blue_xy [0] = 0.036598; |
| 3464 |
encoding.primaries_blue_xy [1] = 0.000105; |
| 3465 |
|
| 3466 |
dng_xy_coord white = D50_xy_coord (); |
| 3467 |
|
| 3468 |
encoding.white_point_xy [0] = white.x; |
| 3469 |
encoding.white_point_xy [1] = white.y; |
| 3470 |
|
| 3471 |
break; |
| 3472 |
|
| 3473 |
} |
| 3474 |
|
| 3475 |
case previewColorSpace_Unknown: |
| 3476 |
default: |
| 3477 |
{ |
| 3478 |
|
| 3479 |
// Assume everything else is raw. |
| 3480 |
|
| 3481 |
// Raw data is linear and usually not white balanced and still in |
| 3482 |
// native/camera RGB space. Transfer curve should be linear but |
| 3483 |
// unclear how to indicate the primaries. For now, just pretend |
| 3484 |
// data is linear BT 2100 / Rec 2020. |
| 3485 |
|
| 3486 |
if (planes == 1) |
| 3487 |
{ |
| 3488 |
encoding.color_space = JXL_COLOR_SPACE_GRAY; |
| 3489 |
encoding.white_point = JXL_WHITE_POINT_D65; // unused |
| 3490 |
encoding.primaries = JXL_PRIMARIES_2100; // unused |
| 3491 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR; |
| 3492 |
} |
| 3493 |
|
| 3494 |
else |
| 3495 |
{ |
| 3496 |
encoding.color_space = JXL_COLOR_SPACE_RGB; |
| 3497 |
encoding.white_point = JXL_WHITE_POINT_D65; |
| 3498 |
encoding.primaries = JXL_PRIMARIES_2100; |
| 3499 |
encoding.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR; |
| 3500 |
} |
| 3501 |
|
| 3502 |
break; |
| 3503 |
|
| 3504 |
} |
| 3505 |
|
| 3506 |
} |
| 3507 |
|
| 3508 |
} |
| 3509 |
|
| 3510 |
/*****************************************************************************/ |
| 3511 |
|
| 3512 |
bool SupportsJXL (const dng_image &image) |
| 3513 |
{ |
| 3514 |
|
| 3515 |
// TODO(erichan): We should add an image size check. But currently JXL's |
| 3516 |
// limits exceed our DNG SDK limits. |
| 3517 |
|
| 3518 |
// Currently ignore alpha; assume that it's a separate image. |
| 3519 |
|
| 3520 |
uint32 planes = image.Planes (); |
| 3521 |
uint32 pixelType = image.PixelType (); |
| 3522 |
|
| 3523 |
return ((planes == 1 || // gray |
| 3524 |
planes == 3) // RGB |
| 3525 |
|
| 3526 |
&& |
| 3527 |
|
| 3528 |
(pixelType == ttByte || |
| 3529 |
pixelType == ttShort || |
| 3530 |
pixelType == ttHalfFloat || |
| 3531 |
pixelType == ttFloat)); |
| 3532 |
|
| 3533 |
} |
| 3534 |
|
| 3535 |
/*****************************************************************************/ |