| 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 |
/** \file |
| 10 |
* Basic support for JPEG XL. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef __dng_jxl__ |
| 16 |
#define __dng_jxl__ |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
#include "dng_flags.h" |
| 21 |
|
| 22 |
/*****************************************************************************/ |
| 23 |
|
| 24 |
#include "dng_auto_ptr.h" |
| 25 |
#include "dng_bmff.h" |
| 26 |
#include "dng_classes.h" |
| 27 |
#include "dng_orientation.h" |
| 28 |
#include "dng_point.h" |
| 29 |
#include "dng_tag_values.h" |
| 30 |
#include "dng_types.h" |
| 31 |
#include "dng_utils.h" |
| 32 |
|
| 33 |
#include <vector> |
| 34 |
#include <unordered_set> |
| 35 |
|
| 36 |
#include "jxl/color_encoding.h" |
| 37 |
|
| 38 |
/*****************************************************************************/ |
| 39 |
|
| 40 |
// jxl requires exif data be prepended with a 4-byte TIFF offset which can be |
| 41 |
// all zero as long as the exif data comes immediately after. |
| 42 |
|
| 43 |
static const uint32 kNumLeadingZeroBytesForEXIF = 4; |
| 44 |
|
| 45 |
/*****************************************************************************/ |
| 46 |
|
| 47 |
class dng_jxl_encode_settings |
| 48 |
{ |
| 49 |
|
| 50 |
private: |
| 51 |
|
| 52 |
real32 fDistance = 1.0f; |
| 53 |
|
| 54 |
// Effort is 1 to 9 where |
| 55 |
// 1: least effort -> faster, bigger files |
| 56 |
// 9: most effort -> slower, smaller files |
| 57 |
|
| 58 |
// Empirically, use 5 to optimize for smaller size and 3 to optimize |
| 59 |
// for speed. |
| 60 |
|
| 61 |
uint32 fEffort = 7; |
| 62 |
|
| 63 |
// Options are 0 to 4. |
| 64 |
|
| 65 |
uint32 fDecodeSpeed = 4; |
| 66 |
|
| 67 |
/* Notes from libjxl 0.7.0 |
| 68 |
|
| 69 |
decode speed (ds) goes from level 0 thru 4 |
| 70 |
0 means smaller files, but longer encode & decode times |
| 71 |
4 means larger files, but shorter encode & decode times |
| 72 |
|
| 73 |
ds0: |
| 74 |
e1 is 1.868 sec at 5.83 MB |
| 75 |
e3 is 1.933 sec at 5.12 MB |
| 76 |
e5 is 3.072 sec at 4.90 MB |
| 77 |
e7 is 6.173 sec at 5.05 MB |
| 78 |
decode: 649 ms |
| 79 |
|
| 80 |
ds4: |
| 81 |
e1 is 1.647 sec at 5.92 MB |
| 82 |
e3 is 1.636 sec at 5.39 MB |
| 83 |
e5 is 2.3 sec at 5.09 MB |
| 84 |
e7 is 5.4 sec at 5.17 MB |
| 85 |
decode: 459 ms |
| 86 |
|
| 87 |
summary: |
| 88 |
|
| 89 |
- at all effort levels: |
| 90 |
- ds0 is smaller than ds4 |
| 91 |
- ds0 is slower to encode than ds4 |
| 92 |
- ds0 is slower to decode than ds4 |
| 93 |
|
| 94 |
- ds4 is 41% faster to decode than ds0 |
| 95 |
|
| 96 |
- ds4 is 13% faster to encode than ds0 at e1 |
| 97 |
- ds4 is 18% faster to encode than ds0 at e3 |
| 98 |
- ds4 is 33% faster to encode than ds0 at e5 |
| 99 |
- ds4 is 14% faster to encode than ds0 at e7 |
| 100 |
|
| 101 |
- ds0 is 1.5% smaller than ds4 at e1 |
| 102 |
- ds0 is 5.3% smaller than ds4 at e3 |
| 103 |
- ds0 is 3.9% smaller than ds4 at e5 |
| 104 |
- ds0 is 2.4% smaller than ds4 at e7 |
| 105 |
|
| 106 |
conclusions 2022-02: |
| 107 |
|
| 108 |
smallest files are at e5 |
| 109 |
slowest times are at e7 |
| 110 |
e1 and e3 are similar times |
| 111 |
e5 much slower than e3 |
| 112 |
e7 much slower than e5 |
| 113 |
|
| 114 |
----> e3 + ds4 is sweet spot <---- |
| 115 |
|
| 116 |
e1 is a little faster (up to 3%) but files are about 10% bigger |
| 117 |
e5 is a little smaller (up to 6%) but 40% to 60% slower to encode |
| 118 |
ds0 is a little smaller (up to 5%) but 40% slower to decode |
| 119 |
|
| 120 |
smallest files: use e5 + ds0 |
| 121 |
pros: 10% smaller (than e3 + ds4) |
| 122 |
cons: 87% slower to encode, 40% slower to decode (than e3 + ds4) |
| 123 |
|
| 124 |
fastest encoding: use e3 + ds4 |
| 125 |
|
| 126 |
smallest files: use e7 + ds4 |
| 127 |
|
| 128 |
*/ |
| 129 |
|
| 130 |
bool fUseOriginalColorEncoding = false; |
| 131 |
|
| 132 |
bool fUseSingleThread = false; |
| 133 |
|
| 134 |
public: |
| 135 |
|
| 136 |
// Low-level methods. |
| 137 |
|
| 138 |
void SetDistance (real32 distance) |
| 139 |
{ |
| 140 |
fDistance = distance; |
| 141 |
} |
| 142 |
|
| 143 |
real32 Distance () const |
| 144 |
{ |
| 145 |
return fDistance; |
| 146 |
} |
| 147 |
|
| 148 |
void SetEffort (uint32 effort) |
| 149 |
{ |
| 150 |
fEffort = Pin_uint32 (1, effort, 9); |
| 151 |
} |
| 152 |
|
| 153 |
uint32 Effort () const |
| 154 |
{ |
| 155 |
return fEffort; |
| 156 |
} |
| 157 |
|
| 158 |
void SetDecodeSpeed (uint32 speed04) |
| 159 |
{ |
| 160 |
fDecodeSpeed = Pin_uint32 (0, speed04, 4); |
| 161 |
} |
| 162 |
|
| 163 |
uint32 DecodeSpeed () const |
| 164 |
{ |
| 165 |
return fDecodeSpeed; |
| 166 |
} |
| 167 |
|
| 168 |
// Convenience method to set encoder settings to optimize for smallest |
| 169 |
// files. |
| 170 |
|
| 171 |
void SetSmallest () |
| 172 |
{ |
| 173 |
fDecodeSpeed = 0; |
| 174 |
fEffort = 5; |
| 175 |
} |
| 176 |
|
| 177 |
// Convenience method to set encoder settings to optimize for fastest |
| 178 |
// encode & decode. |
| 179 |
|
| 180 |
void SetFastest () |
| 181 |
{ |
| 182 |
fDecodeSpeed = 4; |
| 183 |
fEffort = 3; |
| 184 |
} |
| 185 |
|
| 186 |
bool UseSingleThread () const |
| 187 |
{ |
| 188 |
return fUseSingleThread; |
| 189 |
} |
| 190 |
|
| 191 |
void SetSingleThread (bool flag) |
| 192 |
{ |
| 193 |
fUseSingleThread = flag; |
| 194 |
} |
| 195 |
|
| 196 |
bool UseOriginalColorEncoding () const |
| 197 |
{ |
| 198 |
return fUseOriginalColorEncoding; |
| 199 |
} |
| 200 |
|
| 201 |
void SetUseOriginalColorEncoding (bool flag) |
| 202 |
{ |
| 203 |
fUseOriginalColorEncoding = flag; |
| 204 |
} |
| 205 |
|
| 206 |
// If desired, add other JPEG XL encoding options here in the future. |
| 207 |
|
| 208 |
// ... |
| 209 |
|
| 210 |
}; |
| 211 |
|
| 212 |
/*****************************************************************************/ |
| 213 |
|
| 214 |
bool ParseJXL (dng_host &host, |
| 215 |
dng_stream &stream, |
| 216 |
dng_info &info, |
| 217 |
bool supportBasicCodeStream, |
| 218 |
bool supportContainer); |
| 219 |
|
| 220 |
/*****************************************************************************/ |
| 221 |
|
| 222 |
class dng_jxl_color_space_info |
| 223 |
{ |
| 224 |
|
| 225 |
public: |
| 226 |
|
| 227 |
AutoPtr<JxlColorEncoding> fJxlColorEncoding; |
| 228 |
|
| 229 |
AutoPtr<dng_memory_block> fICCProfile; |
| 230 |
|
| 231 |
real32 fIntensityTargetNits = 0.0f; |
| 232 |
|
| 233 |
}; |
| 234 |
|
| 235 |
/*****************************************************************************/ |
| 236 |
|
| 237 |
// Basic JPEG XL decoder. |
| 238 |
|
| 239 |
class dng_jxl_decoder |
| 240 |
{ |
| 241 |
|
| 242 |
friend class dng_jxl_box_reader; |
| 243 |
|
| 244 |
public: |
| 245 |
|
| 246 |
// Input params. |
| 247 |
|
| 248 |
bool fNeedBoxMeta = true; |
| 249 |
|
| 250 |
bool fNeedImage = true; |
| 251 |
|
| 252 |
bool fUseSingleThread = false; |
| 253 |
|
| 254 |
bool fUsePixelBuffer = false; |
| 255 |
|
| 256 |
// Basic info. |
| 257 |
|
| 258 |
bool fUsesOriginalProfile = false; |
| 259 |
|
| 260 |
dng_point fMainImageSize; |
| 261 |
|
| 262 |
uint32 fMainImagePlanes = 3; |
| 263 |
|
| 264 |
uint32 fBitsPerSample = 8; |
| 265 |
|
| 266 |
uint32 fExponentBitsPerSample = 0; |
| 267 |
|
| 268 |
uint32 fNumExtraChannels = 0; |
| 269 |
|
| 270 |
bool fHasPreview = false; |
| 271 |
|
| 272 |
dng_info *fInfo = nullptr; |
| 273 |
|
| 274 |
dng_orientation fOrientation; |
| 275 |
|
| 276 |
dng_jxl_color_space_info fColorSpaceInfo; |
| 277 |
|
| 278 |
// Main image. |
| 279 |
|
| 280 |
AutoPtr<dng_image> fMainImage; |
| 281 |
|
| 282 |
AutoPtr<dng_memory_block> fMainBlock; |
| 283 |
|
| 284 |
AutoPtr<dng_pixel_buffer> fMainPixelBuffer; |
| 285 |
|
| 286 |
// Transparency. |
| 287 |
|
| 288 |
AutoPtr<dng_image> fAlphaMask; |
| 289 |
|
| 290 |
bool fAlphaPremultiplied = false; |
| 291 |
|
| 292 |
// Other stuff we might want to decode. |
| 293 |
|
| 294 |
//const_dng_image_sptr fPreviewImage; |
| 295 |
|
| 296 |
//const_dng_image_sptr fThumb; |
| 297 |
|
| 298 |
public: |
| 299 |
|
| 300 |
virtual ~dng_jxl_decoder (); |
| 301 |
|
| 302 |
virtual void Decode (dng_host &host, |
| 303 |
dng_stream &stream); |
| 304 |
|
| 305 |
protected: |
| 306 |
|
| 307 |
virtual void ProcessExifBox (dng_host &host, |
| 308 |
const std::vector<uint8> &data); |
| 309 |
|
| 310 |
virtual void ProcessXMPBox (dng_host &host, |
| 311 |
const std::vector<uint8> &data); |
| 312 |
|
| 313 |
virtual void ProcessBox (dng_host &host, |
| 314 |
const dng_string &name, |
| 315 |
const std::vector<uint8> &data); |
| 316 |
|
| 317 |
}; |
| 318 |
|
| 319 |
/*****************************************************************************/ |
| 320 |
|
| 321 |
// Write a "raw JXL" codestream (no container, no metadata boxes) for a single |
| 322 |
// image. |
| 323 |
|
| 324 |
void EncodeJXL_Tile (dng_host &host, |
| 325 |
dng_stream &stream, |
| 326 |
const dng_pixel_buffer &buffer, |
| 327 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 328 |
const dng_jxl_encode_settings &settings); |
| 329 |
|
| 330 |
void EncodeJXL_Tile (dng_host &host, |
| 331 |
dng_stream &stream, |
| 332 |
const dng_image &image, |
| 333 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 334 |
const dng_jxl_encode_settings &settings); |
| 335 |
|
| 336 |
/*****************************************************************************/ |
| 337 |
|
| 338 |
// Write a JXL container to the stream with a single image with optional |
| 339 |
// metadata and preview. |
| 340 |
|
| 341 |
void EncodeJXL_Container (dng_host &host, |
| 342 |
dng_stream &stream, |
| 343 |
const dng_image &image, |
| 344 |
const dng_jxl_encode_settings &settings, |
| 345 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 346 |
const dng_metadata *metadata, |
| 347 |
const bool includeExif, |
| 348 |
const bool includeXMP, |
| 349 |
const bool includeIPTC, |
| 350 |
const dng_bmff_box_list *additionalBoxes); |
| 351 |
|
| 352 |
void EncodeJXL_Container (dng_host &host, |
| 353 |
dng_stream &stream, |
| 354 |
const dng_pixel_buffer &buffer, |
| 355 |
const dng_jxl_encode_settings &settings, |
| 356 |
const dng_jxl_color_space_info &colorSpaceInfo, |
| 357 |
const dng_metadata *metadata, |
| 358 |
const bool includeExif, |
| 359 |
const bool includeXMP, |
| 360 |
const bool includeIPTC, |
| 361 |
const dng_bmff_box_list *additionalBoxes); |
| 362 |
|
| 363 |
/*****************************************************************************/ |
| 364 |
|
| 365 |
// Quality is 1 to 13. |
| 366 |
// |
| 367 |
// Level 13 is lossless. |
| 368 |
// Level 12 maps to distance 0.1. |
| 369 |
// Level 9 maps to distance 1. |
| 370 |
// Level 6 maps to distance 2. |
| 371 |
// Level 3 maps to distance 3. |
| 372 |
|
| 373 |
real32 JXLQualityToDistance (uint32 quality); |
| 374 |
|
| 375 |
dng_jxl_encode_settings * JXLQualityToSettings (uint32 quality); |
| 376 |
|
| 377 |
static constexpr uint32 kMinJXLCompressionQuality = 1; |
| 378 |
static constexpr uint32 kMaxJXLCompressionQuality = 13; |
| 379 |
|
| 380 |
static constexpr uint32 kDefaultJXLCompressionQuality = 9; |
| 381 |
|
| 382 |
/*****************************************************************************/ |
| 383 |
|
| 384 |
void PreviewColorSpaceToJXLEncoding (const PreviewColorSpaceEnum colorSpace, |
| 385 |
const uint32 planes, |
| 386 |
dng_jxl_color_space_info &info); |
| 387 |
|
| 388 |
/*****************************************************************************/ |
| 389 |
|
| 390 |
bool SupportsJXL (const dng_image &image); |
| 391 |
|
| 392 |
/*****************************************************************************/ |
| 393 |
|
| 394 |
#endif // __dng_jxl__ |
| 395 |
|
| 396 |
/*****************************************************************************/ |