tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

encode.h (70052B)


      1 /* Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 *
      3 * Use of this source code is governed by a BSD-style
      4 * license that can be found in the LICENSE file.
      5 */
      6 
      7 /** @addtogroup libjxl_encoder
      8 * @{
      9 * @file encode.h
     10 * @brief Encoding API for JPEG XL.
     11 */
     12 
     13 #ifndef JXL_ENCODE_H_
     14 #define JXL_ENCODE_H_
     15 
     16 #include <jxl/cms_interface.h>
     17 #include <jxl/codestream_header.h>
     18 #include <jxl/color_encoding.h>
     19 #include <jxl/jxl_export.h>
     20 #include <jxl/memory_manager.h>
     21 #include <jxl/parallel_runner.h>
     22 #include <jxl/stats.h>
     23 #include <jxl/types.h>
     24 #include <jxl/version.h>  // TODO(eustas): remove before v1.0
     25 #include <stddef.h>
     26 #include <stdint.h>
     27 
     28 #ifdef __cplusplus
     29 extern "C" {
     30 #endif
     31 
     32 /**
     33 * Encoder library version.
     34 *
     35 * @return the encoder library version as an integer:
     36 * MAJOR_VERSION * 1000000 + MINOR_VERSION * 1000 + PATCH_VERSION. For example,
     37 * version 1.2.3 would return 1002003.
     38 */
     39 JXL_EXPORT uint32_t JxlEncoderVersion(void);
     40 
     41 /**
     42 * Opaque structure that holds the JPEG XL encoder.
     43 *
     44 * Allocated and initialized with @ref JxlEncoderCreate().
     45 * Cleaned up and deallocated with @ref JxlEncoderDestroy().
     46 */
     47 typedef struct JxlEncoderStruct JxlEncoder;
     48 
     49 /**
     50 * Settings and metadata for a single image frame. This includes encoder options
     51 * for a frame such as compression quality and speed.
     52 *
     53 * Allocated and initialized with @ref JxlEncoderFrameSettingsCreate().
     54 * Cleaned up and deallocated when the encoder is destroyed with
     55 * @ref JxlEncoderDestroy().
     56 */
     57 typedef struct JxlEncoderFrameSettingsStruct JxlEncoderFrameSettings;
     58 
     59 /**
     60 * Return value for multiple encoder functions.
     61 */
     62 typedef enum {
     63  /** Function call finished successfully, or encoding is finished and there is
     64   * nothing more to be done.
     65   */
     66  JXL_ENC_SUCCESS = 0,
     67 
     68  /** An error occurred, for example out of memory.
     69   */
     70  JXL_ENC_ERROR = 1,
     71 
     72  /** The encoder needs more output buffer to continue encoding.
     73   */
     74  JXL_ENC_NEED_MORE_OUTPUT = 2,
     75 
     76 } JxlEncoderStatus;
     77 
     78 /**
     79 * Error conditions:
     80 * API usage errors have the 0x80 bit set to 1
     81 * Other errors have the 0x80 bit set to 0
     82 */
     83 typedef enum {
     84  /** No error
     85   */
     86  JXL_ENC_ERR_OK = 0,
     87 
     88  /** Generic encoder error due to unspecified cause
     89   */
     90  JXL_ENC_ERR_GENERIC = 1,
     91 
     92  /** Out of memory
     93   *  TODO(jon): actually catch this and return this error
     94   */
     95  JXL_ENC_ERR_OOM = 2,
     96 
     97  /** JPEG bitstream reconstruction data could not be
     98   *  represented (e.g. too much tail data)
     99   */
    100  JXL_ENC_ERR_JBRD = 3,
    101 
    102  /** Input is invalid (e.g. corrupt JPEG file or ICC profile)
    103   */
    104  JXL_ENC_ERR_BAD_INPUT = 4,
    105 
    106  /** The encoder doesn't (yet) support this. Either no version of libjxl
    107   * supports this, and the API is used incorrectly, or the libjxl version
    108   * should have been checked before trying to do this.
    109   */
    110  JXL_ENC_ERR_NOT_SUPPORTED = 0x80,
    111 
    112  /** The encoder API is used in an incorrect way.
    113   *  In this case, a debug build of libjxl should output a specific error
    114   * message. (if not, please open an issue about it)
    115   */
    116  JXL_ENC_ERR_API_USAGE = 0x81,
    117 
    118 } JxlEncoderError;
    119 
    120 /**
    121 * Id of encoder options for a frame. This includes options such as setting
    122 * encoding effort/speed or overriding the use of certain coding tools, for this
    123 * frame. This does not include non-frame related encoder options such as for
    124 * boxes.
    125 */
    126 typedef enum {
    127  /** Sets encoder effort/speed level without affecting decoding speed. Valid
    128   * values are, from faster to slower speed: 1:lightning 2:thunder 3:falcon
    129   * 4:cheetah 5:hare 6:wombat 7:squirrel 8:kitten 9:tortoise 10:glacier.
    130   * Default: squirrel (7).
    131   */
    132  JXL_ENC_FRAME_SETTING_EFFORT = 0,
    133 
    134  /** Sets the decoding speed tier for the provided options. Minimum is 0
    135   * (slowest to decode, best quality/density), and maximum is 4 (fastest to
    136   * decode, at the cost of some quality/density). Default is 0.
    137   */
    138  JXL_ENC_FRAME_SETTING_DECODING_SPEED = 1,
    139 
    140  /** Sets resampling option. If enabled, the image is downsampled before
    141   * compression, and upsampled to original size in the decoder. Integer option,
    142   * use -1 for the default behavior (resampling only applied for low quality),
    143   * 1 for no downsampling (1x1), 2 for 2x2 downsampling, 4 for 4x4
    144   * downsampling, 8 for 8x8 downsampling.
    145   */
    146  JXL_ENC_FRAME_SETTING_RESAMPLING = 2,
    147 
    148  /** Similar to ::JXL_ENC_FRAME_SETTING_RESAMPLING, but for extra channels.
    149   * Integer option, use -1 for the default behavior (depends on encoder
    150   * implementation), 1 for no downsampling (1x1), 2 for 2x2 downsampling, 4 for
    151   * 4x4 downsampling, 8 for 8x8 downsampling.
    152   */
    153  JXL_ENC_FRAME_SETTING_EXTRA_CHANNEL_RESAMPLING = 3,
    154 
    155  /** Indicates the frame added with @ref JxlEncoderAddImageFrame is already
    156   * downsampled by the downsampling factor set with @ref
    157   * JXL_ENC_FRAME_SETTING_RESAMPLING. The input frame must then be given in the
    158   * downsampled resolution, not the full image resolution. The downsampled
    159   * resolution is given by ceil(xsize / resampling), ceil(ysize / resampling)
    160   * with xsize and ysize the dimensions given in the basic info, and resampling
    161   * the factor set with ::JXL_ENC_FRAME_SETTING_RESAMPLING.
    162   * Use 0 to disable, 1 to enable. Default value is 0.
    163   */
    164  JXL_ENC_FRAME_SETTING_ALREADY_DOWNSAMPLED = 4,
    165 
    166  /** Adds noise to the image emulating photographic film noise, the higher the
    167   * given number, the grainier the image will be. As an example, a value of 100
    168   * gives low noise whereas a value of 3200 gives a lot of noise. The default
    169   * value is 0.
    170   */
    171  JXL_ENC_FRAME_SETTING_PHOTON_NOISE = 5,
    172 
    173  /** Enables adaptive noise generation. This setting is not recommended for
    174   * use, please use ::JXL_ENC_FRAME_SETTING_PHOTON_NOISE instead. Use -1 for
    175   * the default (encoder chooses), 0 to disable, 1 to enable.
    176   */
    177  JXL_ENC_FRAME_SETTING_NOISE = 6,
    178 
    179  /** Enables or disables dots generation. Use -1 for the default (encoder
    180   * chooses), 0 to disable, 1 to enable.
    181   */
    182  JXL_ENC_FRAME_SETTING_DOTS = 7,
    183 
    184  /** Enables or disables patches generation. Use -1 for the default (encoder
    185   * chooses), 0 to disable, 1 to enable.
    186   */
    187  JXL_ENC_FRAME_SETTING_PATCHES = 8,
    188 
    189  /** Edge preserving filter level, -1 to 3. Use -1 for the default (encoder
    190   * chooses), 0 to 3 to set a strength.
    191   */
    192  JXL_ENC_FRAME_SETTING_EPF = 9,
    193 
    194  /** Enables or disables the gaborish filter. Use -1 for the default (encoder
    195   * chooses), 0 to disable, 1 to enable.
    196   */
    197  JXL_ENC_FRAME_SETTING_GABORISH = 10,
    198 
    199  /** Enables modular encoding. Use -1 for default (encoder
    200   * chooses), 0 to enforce VarDCT mode (e.g. for photographic images), 1 to
    201   * enforce modular mode (e.g. for lossless images).
    202   */
    203  JXL_ENC_FRAME_SETTING_MODULAR = 11,
    204 
    205  /** Enables or disables preserving color of invisible pixels. Use -1 for the
    206   * default (1 if lossless, 0 if lossy), 0 to disable, 1 to enable.
    207   */
    208  JXL_ENC_FRAME_SETTING_KEEP_INVISIBLE = 12,
    209 
    210  /** Determines the order in which 256x256 regions are stored in the codestream
    211   * for progressive rendering. Use -1 for the encoder
    212   * default, 0 for scanline order, 1 for center-first order.
    213   */
    214  JXL_ENC_FRAME_SETTING_GROUP_ORDER = 13,
    215 
    216  /** Determines the horizontal position of center for the center-first group
    217   * order. Use -1 to automatically use the middle of the image, 0..xsize to
    218   * specifically set it.
    219   */
    220  JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_X = 14,
    221 
    222  /** Determines the center for the center-first group order. Use -1 to
    223   * automatically use the middle of the image, 0..ysize to specifically set it.
    224   */
    225  JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_Y = 15,
    226 
    227  /** Enables or disables progressive encoding for modular mode. Use -1 for the
    228   * encoder default, 0 to disable, 1 to enable.
    229   */
    230  JXL_ENC_FRAME_SETTING_RESPONSIVE = 16,
    231 
    232  /** Set the progressive mode for the AC coefficients of VarDCT, using spectral
    233   * progression from the DCT coefficients. Use -1 for the encoder default, 0 to
    234   * disable, 1 to enable.
    235   */
    236  JXL_ENC_FRAME_SETTING_PROGRESSIVE_AC = 17,
    237 
    238  /** Set the progressive mode for the AC coefficients of VarDCT, using
    239   * quantization of the least significant bits. Use -1 for the encoder default,
    240   * 0 to disable, 1 to enable.
    241   */
    242  JXL_ENC_FRAME_SETTING_QPROGRESSIVE_AC = 18,
    243 
    244  /** Set the progressive mode using lower-resolution DC images for VarDCT. Use
    245   * -1 for the encoder default, 0 to disable, 1 to have an extra 64x64 lower
    246   * resolution pass, 2 to have a 512x512 and 64x64 lower resolution pass.
    247   */
    248  JXL_ENC_FRAME_SETTING_PROGRESSIVE_DC = 19,
    249 
    250  /** Use Global channel palette if the amount of colors is smaller than this
    251   * percentage of range. Use 0-100 to set an explicit percentage, -1 to use the
    252   * encoder default. Used for modular encoding.
    253   */
    254  JXL_ENC_FRAME_SETTING_CHANNEL_COLORS_GLOBAL_PERCENT = 20,
    255 
    256  /** Use Local (per-group) channel palette if the amount of colors is smaller
    257   * than this percentage of range. Use 0-100 to set an explicit percentage, -1
    258   * to use the encoder default. Used for modular encoding.
    259   */
    260  JXL_ENC_FRAME_SETTING_CHANNEL_COLORS_GROUP_PERCENT = 21,
    261 
    262  /** Use color palette if amount of colors is smaller than or equal to this
    263   * amount, or -1 to use the encoder default. Used for modular encoding.
    264   */
    265  JXL_ENC_FRAME_SETTING_PALETTE_COLORS = 22,
    266 
    267  /** Enables or disables delta palette. Use -1 for the default (encoder
    268   * chooses), 0 to disable, 1 to enable. Used in modular mode.
    269   */
    270  JXL_ENC_FRAME_SETTING_LOSSY_PALETTE = 23,
    271 
    272  /** Color transform for internal encoding: -1 = default, 0=XYB, 1=none (RGB),
    273   * 2=YCbCr. The XYB setting performs the forward XYB transform. None and
    274   * YCbCr both perform no transform, but YCbCr is used to indicate that the
    275   * encoded data losslessly represents YCbCr values.
    276   */
    277  JXL_ENC_FRAME_SETTING_COLOR_TRANSFORM = 24,
    278 
    279  /** Reversible color transform for modular encoding: -1=default, 0-41=RCT
    280   * index, e.g. index 0 = none, index 6 = YCoCg.
    281   * If this option is set to a non-default value, the RCT will be globally
    282   * applied to the whole frame.
    283   * The default behavior is to try several RCTs locally per modular group,
    284   * depending on the speed and distance setting.
    285   */
    286  JXL_ENC_FRAME_SETTING_MODULAR_COLOR_SPACE = 25,
    287 
    288  /** Group size for modular encoding: -1=default, 0=128, 1=256, 2=512, 3=1024.
    289   */
    290  JXL_ENC_FRAME_SETTING_MODULAR_GROUP_SIZE = 26,
    291 
    292  /** Predictor for modular encoding. -1 = default, 0=zero, 1=left, 2=top,
    293   * 3=avg0, 4=select, 5=gradient, 6=weighted, 7=topright, 8=topleft,
    294   * 9=leftleft, 10=avg1, 11=avg2, 12=avg3, 13=toptop predictive average 14=mix
    295   * 5 and 6, 15=mix everything.
    296   */
    297  JXL_ENC_FRAME_SETTING_MODULAR_PREDICTOR = 27,
    298 
    299  /** Fraction of pixels used to learn MA trees as a percentage. -1 = default,
    300   * 0 = no MA and fast decode, 50 = default value, 100 = all, values above
    301   * 100 are also permitted. Higher values use more encoder memory.
    302   */
    303  JXL_ENC_FRAME_SETTING_MODULAR_MA_TREE_LEARNING_PERCENT = 28,
    304 
    305  /** Number of extra (previous-channel) MA tree properties to use. -1 =
    306   * default, 0-11 = valid values. Recommended values are in the range 0 to 3,
    307   * or 0 to amount of channels minus 1 (including all extra channels, and
    308   * excluding color channels when using VarDCT mode). Higher value gives slower
    309   * encoding and slower decoding.
    310   */
    311  JXL_ENC_FRAME_SETTING_MODULAR_NB_PREV_CHANNELS = 29,
    312 
    313  /** Enable or disable CFL (chroma-from-luma) for lossless JPEG recompression.
    314   * -1 = default, 0 = disable CFL, 1 = enable CFL.
    315   */
    316  JXL_ENC_FRAME_SETTING_JPEG_RECON_CFL = 30,
    317 
    318  /** Prepare the frame for indexing in the frame index box.
    319   * 0 = ignore this frame (same as not setting a value),
    320   * 1 = index this frame within the Frame Index Box.
    321   * If any frames are indexed, the first frame needs to
    322   * be indexed, too. If the first frame is not indexed, and
    323   * a later frame is attempted to be indexed, ::JXL_ENC_ERROR will occur.
    324   * If non-keyframes, i.e., frames with cropping, blending or patches are
    325   * attempted to be indexed, ::JXL_ENC_ERROR will occur.
    326   */
    327  JXL_ENC_FRAME_INDEX_BOX = 31,
    328 
    329  /** Sets brotli encode effort for use in JPEG recompression and
    330   * compressed metadata boxes (brob). Can be -1 (default) or 0 (fastest) to 11
    331   * (slowest). Default is based on the general encode effort in case of JPEG
    332   * recompression, and 4 for brob boxes.
    333   */
    334  JXL_ENC_FRAME_SETTING_BROTLI_EFFORT = 32,
    335 
    336  /** Enables or disables brotli compression of metadata boxes derived from
    337   * a JPEG frame when using @ref JxlEncoderAddJPEGFrame. This has no effect on
    338   * boxes added using @ref JxlEncoderAddBox. -1 = default, 0 = disable
    339   * compression, 1 = enable compression.
    340   */
    341  JXL_ENC_FRAME_SETTING_JPEG_COMPRESS_BOXES = 33,
    342 
    343  /** Control what kind of buffering is used, when using chunked image frames.
    344   * -1 = default (let the encoder decide)
    345   * 0 = buffers everything, basically the same as non-streamed code path
    346   (mainly for testing)
    347   * 1 = buffers everything for images that are smaller than 2048 x 2048, and
    348   *     uses streaming input and output for larger images
    349   * 2 = uses streaming input and output for all images that are larger than
    350   *     one group, i.e. 256 x 256 pixels by default
    351   * 3 = currently same as 2
    352   *
    353   * When using streaming input and output the encoder minimizes memory usage at
    354   * the cost of compression density. Also note that images produced with
    355   * streaming mode might not be progressively decodeable.
    356   */
    357  JXL_ENC_FRAME_SETTING_BUFFERING = 34,
    358 
    359  /** Keep or discard Exif metadata boxes derived from a JPEG frame when using
    360   * @ref JxlEncoderAddJPEGFrame. This has no effect on boxes added using
    361   * @ref JxlEncoderAddBox. When @ref JxlEncoderStoreJPEGMetadata is set to 1,
    362   * this option cannot be set to 0. Even when Exif metadata is discarded, the
    363   * orientation will still be applied. 0 = discard Exif metadata, 1 = keep Exif
    364   * metadata (default).
    365   */
    366  JXL_ENC_FRAME_SETTING_JPEG_KEEP_EXIF = 35,
    367 
    368  /** Keep or discard XMP metadata boxes derived from a JPEG frame when using
    369   * @ref JxlEncoderAddJPEGFrame. This has no effect on boxes added using
    370   * @ref JxlEncoderAddBox. When @ref JxlEncoderStoreJPEGMetadata is set to 1,
    371   * this option cannot be set to 0. 0 = discard XMP metadata, 1 = keep XMP
    372   * metadata (default).
    373   */
    374  JXL_ENC_FRAME_SETTING_JPEG_KEEP_XMP = 36,
    375 
    376  /** Keep or discard JUMBF metadata boxes derived from a JPEG frame when using
    377   * @ref JxlEncoderAddJPEGFrame. This has no effect on boxes added using
    378   * @ref JxlEncoderAddBox. 0 = discard JUMBF metadata, 1 = keep JUMBF metadata
    379   * (default).
    380   */
    381  JXL_ENC_FRAME_SETTING_JPEG_KEEP_JUMBF = 37,
    382 
    383  /** If this mode is disabled, the encoder will not make any image quality
    384   * decisions that are computed based on the full image, but stored only once
    385   * (e.g. the X quant multiplier in the frame header). Used mainly for testing
    386   * equivalence of streaming and non-streaming code.
    387   * 0 = disabled, 1 = enabled (default)
    388   */
    389  JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS = 38,
    390 
    391  /** Disable perceptual optimizations. 0 = optimizations enabled (default), 1 =
    392   * optimizations disabled.
    393   */
    394  JXL_ENC_FRAME_SETTING_DISABLE_PERCEPTUAL_HEURISTICS = 39,
    395 
    396  /** Enum value not to be used as an option. This value is added to force the
    397   * C compiler to have the enum to take a known size.
    398   */
    399  JXL_ENC_FRAME_SETTING_FILL_ENUM = 65535,
    400 
    401 } JxlEncoderFrameSettingId;
    402 
    403 /**
    404 * Creates an instance of @ref JxlEncoder and initializes it.
    405 *
    406 * @p memory_manager will be used for all the library dynamic allocations made
    407 * from this instance. The parameter may be NULL, in which case the default
    408 * allocator will be used. See jpegxl/memory_manager.h for details.
    409 *
    410 * @param memory_manager custom allocator function. It may be NULL. The memory
    411 *        manager will be copied internally.
    412 * @return @c NULL if the instance can not be allocated or initialized
    413 * @return pointer to initialized @ref JxlEncoder otherwise
    414 */
    415 JXL_EXPORT JxlEncoder* JxlEncoderCreate(const JxlMemoryManager* memory_manager);
    416 
    417 /**
    418 * Re-initializes a @ref JxlEncoder instance, so it can be re-used for encoding
    419 * another image. All state and settings are reset as if the object was
    420 * newly created with @ref JxlEncoderCreate, but the memory manager is kept.
    421 *
    422 * @param enc instance to be re-initialized.
    423 */
    424 JXL_EXPORT void JxlEncoderReset(JxlEncoder* enc);
    425 
    426 /**
    427 * Deinitializes and frees a @ref JxlEncoder instance.
    428 *
    429 * @param enc instance to be cleaned up and deallocated.
    430 */
    431 JXL_EXPORT void JxlEncoderDestroy(JxlEncoder* enc);
    432 
    433 /**
    434 * Sets the color management system (CMS) that will be used for color conversion
    435 * (if applicable) during encoding. May only be set before starting encoding. If
    436 * left unset, the default CMS implementation will be used.
    437 *
    438 * @param enc encoder object.
    439 * @param cms structure representing a CMS implementation. See @ref
    440 * JxlCmsInterface for more details.
    441 */
    442 JXL_EXPORT void JxlEncoderSetCms(JxlEncoder* enc, JxlCmsInterface cms);
    443 
    444 /**
    445 * Set the parallel runner for multithreading. May only be set before starting
    446 * encoding.
    447 *
    448 * @param enc encoder object.
    449 * @param parallel_runner function pointer to runner for multithreading. It may
    450 *        be NULL to use the default, single-threaded, runner. A multithreaded
    451 *        runner should be set to reach fast performance.
    452 * @param parallel_runner_opaque opaque pointer for parallel_runner.
    453 * @return ::JXL_ENC_SUCCESS if the runner was set, ::JXL_ENC_ERROR
    454 * otherwise (the previous runner remains set).
    455 */
    456 JXL_EXPORT JxlEncoderStatus
    457 JxlEncoderSetParallelRunner(JxlEncoder* enc, JxlParallelRunner parallel_runner,
    458                            void* parallel_runner_opaque);
    459 
    460 /**
    461 * Get the (last) error code in case ::JXL_ENC_ERROR was returned.
    462 *
    463 * @param enc encoder object.
    464 * @return the @ref JxlEncoderError that caused the (last) ::JXL_ENC_ERROR to
    465 * be returned.
    466 */
    467 JXL_EXPORT JxlEncoderError JxlEncoderGetError(JxlEncoder* enc);
    468 
    469 /**
    470 * Encodes a JPEG XL file using the available bytes. @p *avail_out indicates how
    471 * many output bytes are available, and @p *next_out points to the input bytes.
    472 * *avail_out will be decremented by the amount of bytes that have been
    473 * processed by the encoder and *next_out will be incremented by the same
    474 * amount, so *next_out will now point at the amount of *avail_out unprocessed
    475 * bytes.
    476 *
    477 * The returned status indicates whether the encoder needs more output bytes.
    478 * When the return value is not ::JXL_ENC_ERROR or ::JXL_ENC_SUCCESS, the
    479 * encoding requires more @ref JxlEncoderProcessOutput calls to continue.
    480 *
    481 * The caller must guarantee that *avail_out >= 32 when calling
    482 * @ref JxlEncoderProcessOutput; otherwise, ::JXL_ENC_NEED_MORE_OUTPUT will
    483 * be returned. It is guaranteed that, if *avail_out >= 32, at least one byte of
    484 * output will be written.
    485 *
    486 * This encodes the frames and/or boxes added so far. If the last frame or last
    487 * box has been added, @ref JxlEncoderCloseInput, @ref JxlEncoderCloseFrames
    488 * and/or @ref JxlEncoderCloseBoxes must be called before the next
    489 * @ref JxlEncoderProcessOutput call, or the codestream won't be encoded
    490 * correctly.
    491 *
    492 * @param enc encoder object.
    493 * @param next_out pointer to next bytes to write to.
    494 * @param avail_out amount of bytes available starting from *next_out.
    495 * @return ::JXL_ENC_SUCCESS when encoding finished and all events handled.
    496 * @return ::JXL_ENC_ERROR when encoding failed, e.g. invalid input.
    497 * @return ::JXL_ENC_NEED_MORE_OUTPUT more output buffer is necessary.
    498 */
    499 JXL_EXPORT JxlEncoderStatus JxlEncoderProcessOutput(JxlEncoder* enc,
    500                                                    uint8_t** next_out,
    501                                                    size_t* avail_out);
    502 
    503 /**
    504 * Sets the frame information for this frame to the encoder. This includes
    505 * animation information such as frame duration to store in the frame header.
    506 * The frame header fields represent the frame as passed to the encoder, but not
    507 * necessarily the exact values as they will be encoded file format: the encoder
    508 * could change crop and blending options of a frame for more efficient encoding
    509 * or introduce additional internal frames. Animation duration and time code
    510 * information is not altered since those are immutable metadata of the frame.
    511 *
    512 * It is not required to use this function, however if have_animation is set
    513 * to true in the basic info, then this function should be used to set the
    514 * time duration of this individual frame. By default individual frames have a
    515 * time duration of 0, making them form a composite still. See @ref
    516 * JxlFrameHeader for more information.
    517 *
    518 * This information is stored in the @ref JxlEncoderFrameSettings and so is used
    519 * for any frame encoded with these @ref JxlEncoderFrameSettings. It is ok to
    520 * change between @ref JxlEncoderAddImageFrame calls, each added image frame
    521 * will have the frame header that was set in the options at the time of calling
    522 * @ref JxlEncoderAddImageFrame.
    523 *
    524 * The is_last and name_length fields of the @ref JxlFrameHeader are ignored,
    525 * use
    526 * @ref JxlEncoderCloseFrames to indicate last frame, and @ref
    527 * JxlEncoderSetFrameName to indicate the name and its length instead.
    528 * Calling this function will clear any name that was previously set with @ref
    529 * JxlEncoderSetFrameName.
    530 *
    531 * @param frame_settings set of options and metadata for this frame. Also
    532 * includes reference to the encoder object.
    533 * @param frame_header frame header data to set. Object owned by the caller and
    534 * does not need to be kept in memory, its information is copied internally.
    535 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    536 */
    537 JXL_EXPORT JxlEncoderStatus
    538 JxlEncoderSetFrameHeader(JxlEncoderFrameSettings* frame_settings,
    539                         const JxlFrameHeader* frame_header);
    540 
    541 /**
    542 * Sets blend info of an extra channel. The blend info of extra channels is set
    543 * separately from that of the color channels, the color channels are set with
    544 * @ref JxlEncoderSetFrameHeader.
    545 *
    546 * @param frame_settings set of options and metadata for this frame. Also
    547 * includes reference to the encoder object.
    548 * @param index index of the extra channel to use.
    549 * @param blend_info blend info to set for the extra channel
    550 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    551 */
    552 JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelBlendInfo(
    553    JxlEncoderFrameSettings* frame_settings, size_t index,
    554    const JxlBlendInfo* blend_info);
    555 
    556 /**
    557 * Sets the name of the animation frame. This function is optional, frames are
    558 * not required to have a name. This setting is a part of the frame header, and
    559 * the same principles as for @ref JxlEncoderSetFrameHeader apply. The
    560 * name_length field of @ref JxlFrameHeader is ignored by the encoder, this
    561 * function determines the name length instead as the length in bytes of the C
    562 * string.
    563 *
    564 * The maximum possible name length is 1071 bytes (excluding terminating null
    565 * character).
    566 *
    567 * Calling @ref JxlEncoderSetFrameHeader clears any name that was
    568 * previously set.
    569 *
    570 * @param frame_settings set of options and metadata for this frame. Also
    571 * includes reference to the encoder object.
    572 * @param frame_name name of the next frame to be encoded, as a UTF-8 encoded C
    573 * string (zero terminated). Owned by the caller, and copied internally.
    574 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    575 */
    576 JXL_EXPORT JxlEncoderStatus JxlEncoderSetFrameName(
    577    JxlEncoderFrameSettings* frame_settings, const char* frame_name);
    578 
    579 /**
    580 * Sets the bit depth of the input buffer.
    581 *
    582 * For float pixel formats, only the default @ref
    583 JXL_BIT_DEPTH_FROM_PIXEL_FORMAT
    584 * setting is allowed, while for unsigned pixel formats,
    585 * ::JXL_BIT_DEPTH_FROM_CODESTREAM setting is also allowed. See the comment
    586 on
    587 * @ref JxlEncoderAddImageFrame for the effects of the bit depth setting.
    588 
    589 * @param frame_settings set of options and metadata for this frame. Also
    590 * includes reference to the encoder object.
    591 * @param bit_depth the bit depth setting of the pixel input
    592 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    593 */
    594 JXL_EXPORT JxlEncoderStatus JxlEncoderSetFrameBitDepth(
    595    JxlEncoderFrameSettings* frame_settings, const JxlBitDepth* bit_depth);
    596 
    597 /**
    598 * Sets the buffer to read JPEG encoded bytes from for the next frame to encode.
    599 *
    600 * If @ref JxlEncoderSetBasicInfo has not yet been called, calling
    601 * @ref JxlEncoderAddJPEGFrame will implicitly call it with the parameters of
    602 * the added JPEG frame.
    603 *
    604 * If @ref JxlEncoderSetColorEncoding or @ref JxlEncoderSetICCProfile has not
    605 * yet been called, calling @ref JxlEncoderAddJPEGFrame will implicitly call it
    606 * with the parameters of the added JPEG frame.
    607 *
    608 * If the encoder is set to store JPEG reconstruction metadata using @ref
    609 * JxlEncoderStoreJPEGMetadata and a single JPEG frame is added, it will be
    610 * possible to losslessly reconstruct the JPEG codestream.
    611 *
    612 * If this is the last frame, @ref JxlEncoderCloseInput or @ref
    613 * JxlEncoderCloseFrames must be called before the next
    614 * @ref JxlEncoderProcessOutput call.
    615 *
    616 * Note, this can only be used to add JPEG frames for lossless compression. To
    617 * encode with lossy compression, the JPEG must be decoded manually and a pixel
    618 * buffer added using JxlEncoderAddImageFrame.
    619 *
    620 * @param frame_settings set of options and metadata for this frame. Also
    621 * includes reference to the encoder object.
    622 * @param buffer bytes to read JPEG from. Owned by the caller and its contents
    623 * are copied internally.
    624 * @param size size of buffer in bytes.
    625 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    626 */
    627 JXL_EXPORT JxlEncoderStatus
    628 JxlEncoderAddJPEGFrame(const JxlEncoderFrameSettings* frame_settings,
    629                       const uint8_t* buffer, size_t size);
    630 
    631 /**
    632 * Sets the buffer to read pixels from for the next image to encode. Must call
    633 * @ref JxlEncoderSetBasicInfo before @ref JxlEncoderAddImageFrame.
    634 *
    635 * Currently only some data types for pixel formats are supported:
    636 * - ::JXL_TYPE_UINT8, with range 0..255
    637 * - ::JXL_TYPE_UINT16, with range 0..65535
    638 * - ::JXL_TYPE_FLOAT16, with nominal range 0..1
    639 * - ::JXL_TYPE_FLOAT, with nominal range 0..1
    640 *
    641 * Note: the sample data type in pixel_format is allowed to be different from
    642 * what is described in the @ref JxlBasicInfo. The type in pixel_format,
    643 * together with an optional @ref JxlBitDepth parameter set by @ref
    644 * JxlEncoderSetFrameBitDepth describes the format of the uncompressed pixel
    645 * buffer. The bits_per_sample and exponent_bits_per_sample in the @ref
    646 * JxlBasicInfo describes what will actually be encoded in the JPEG XL
    647 * codestream. For example, to encode a 12-bit image, you would set
    648 * bits_per_sample to 12, while the input frame buffer can be in the following
    649 * formats:
    650 *  - if pixel format is in ::JXL_TYPE_UINT16 with default bit depth setting
    651 *    (i.e. ::JXL_BIT_DEPTH_FROM_PIXEL_FORMAT), input sample values are
    652 * rescaled to 16-bit, i.e. multiplied by 65535/4095;
    653 *  - if pixel format is in ::JXL_TYPE_UINT16 with @ref
    654 * JXL_BIT_DEPTH_FROM_CODESTREAM bit depth setting, input sample values are
    655 * provided unscaled;
    656 *  - if pixel format is in ::JXL_TYPE_FLOAT, input sample values are
    657 * rescaled to 0..1, i.e.  multiplied by 1.f/4095.f. While it is allowed, it is
    658 * obviously not recommended to use a pixel_format with lower precision than
    659 * what is specified in the @ref JxlBasicInfo.
    660 *
    661 * We support interleaved channels as described by the @ref JxlPixelFormat
    662 * "JxlPixelFormat":
    663 * - single-channel data, e.g. grayscale
    664 * - single-channel + alpha
    665 * - trichromatic, e.g. RGB
    666 * - trichromatic + alpha
    667 *
    668 * Extra channels not handled here need to be set by @ref
    669 * JxlEncoderSetExtraChannelBuffer.
    670 * If the image has alpha, and alpha is not passed here, it will implicitly be
    671 * set to all-opaque (an alpha value of 1.0 everywhere).
    672 *
    673 * The pixels are assumed to be encoded in the original profile that is set with
    674 * @ref JxlEncoderSetColorEncoding or @ref JxlEncoderSetICCProfile. If none of
    675 * these functions were used, the pixels are assumed to be nonlinear sRGB for
    676 * integer data types (::JXL_TYPE_UINT8, ::JXL_TYPE_UINT16), and linear
    677 * sRGB for floating point data types (::JXL_TYPE_FLOAT16, @ref
    678 * JXL_TYPE_FLOAT).
    679 *
    680 * Sample values in floating-point pixel formats are allowed to be outside the
    681 * nominal range, e.g. to represent out-of-sRGB-gamut colors in the
    682 * uses_original_profile=false case. They are however not allowed to be NaN or
    683 * +-infinity.
    684 *
    685 * If this is the last frame, @ref JxlEncoderCloseInput or @ref
    686 * JxlEncoderCloseFrames must be called before the next
    687 * @ref JxlEncoderProcessOutput call.
    688 *
    689 * @param frame_settings set of options and metadata for this frame. Also
    690 * includes reference to the encoder object.
    691 * @param pixel_format format for pixels. Object owned by the caller and its
    692 * contents are copied internally.
    693 * @param buffer buffer type to input the pixel data from. Owned by the caller
    694 * and its contents are copied internally.
    695 * @param size size of buffer in bytes. This size should match what is implied
    696 * by the frame dimensions and the pixel format.
    697 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    698 */
    699 JXL_EXPORT JxlEncoderStatus JxlEncoderAddImageFrame(
    700    const JxlEncoderFrameSettings* frame_settings,
    701    const JxlPixelFormat* pixel_format, const void* buffer, size_t size);
    702 
    703 /**
    704 * The @ref JxlEncoderOutputProcessor structure provides an interface for the
    705 * encoder's output processing. Users of the library, who want to do streaming
    706 * encoding, should implement the required callbacks for buffering, writing,
    707 * seeking (if supported), and setting a finalized position during the encoding
    708 * process.
    709 *
    710 * At a high level, the processor can be in one of two states:
    711 * - With an active buffer: This indicates that a buffer has been acquired using
    712 *   `get_buffer` and encoded data can be written to it.
    713 * - Without an active buffer: In this state, no data can be written. A new
    714 * buffer must be acquired after releasing any previously active buffer.
    715 *
    716 * The library will not acquire more than one buffer at a given time.
    717 *
    718 * The state of the processor includes `position` and `finalized position`,
    719 * which have the following meaning.
    720 *
    721 * - position: Represents the current position, in bytes, within the output
    722 * stream where the encoded data will be written next. This position moves
    723 * forward with each `release_buffer` call as data is written, and can also be
    724 * adjusted through the optional seek callback, if provided. At this position
    725 * the next write will occur.
    726 *
    727 * - finalized position:  A position in the output stream that ensures all bytes
    728 * before this point are finalized and won't be changed by later writes.
    729 *
    730 * All fields but `seek` are required, `seek` is optional and can be NULL.
    731 */
    732 struct JxlEncoderOutputProcessor {
    733  /**
    734   * Required.
    735   * An opaque pointer that the client can use to store custom data.
    736   * This data will be passed to the associated callback functions.
    737   */
    738  void* opaque;
    739 
    740  /**
    741   * Required.
    742   * Acquires a buffer at the current position into which the library will write
    743   * the output data.
    744   *
    745   * If the `size` argument points to 0 and the returned value is NULL, this
    746   * will be interpreted as asking the output writing to stop. In such a case,
    747   * the library will return an error. The client is expected to set the size of
    748   * the returned buffer based on the suggested `size` when this function is
    749   * called.
    750   *
    751   * @param opaque user supplied parameters to the callback
    752   * @param size points to a suggested buffer size when called; must be set to
    753   * the size of the returned buffer once the function returns.
    754   * @return a pointer to the acquired buffer or NULL to indicate a stop
    755   * condition.
    756   */
    757  void* (*get_buffer)(void* opaque, size_t* size);
    758 
    759  /**
    760   * Required.
    761   * Notifies the user of library that the current buffer's data has been
    762   * written and can be released. This function should advance the current
    763   * position of the buffer by `written_bytes` number of bytes.
    764   *
    765   * @param opaque user supplied parameters to the callback
    766   * @param written_bytes the number of bytes written to the buffer.
    767   */
    768  void (*release_buffer)(void* opaque, size_t written_bytes);
    769 
    770  /**
    771   * Optional, can be NULL
    772   * Seeks to a specific position in the output. This function is optional and
    773   * can be set to NULL if the output doesn't support seeking. Can only be done
    774   * when there is no buffer. Cannot be used to seek before the finalized
    775   * position.
    776   *
    777   * @param opaque user supplied parameters to the callback
    778   * @param position the position to seek to, in bytes.
    779   */
    780  void (*seek)(void* opaque, uint64_t position);
    781 
    782  /**
    783   * Required.
    784   * Sets a finalized position on the output data, at a specific position.
    785   * Seeking will never request a position before the finalized position.
    786   *
    787   * Will only be called if there is no active buffer.
    788   *
    789   * @param opaque user supplied parameters to the callback
    790   * @param finalized_position the position, in bytes, where the finalized
    791   * position should be set.
    792   */
    793  void (*set_finalized_position)(void* opaque, uint64_t finalized_position);
    794 };
    795 
    796 /**
    797 * Sets the output processor for the encoder. This processor determines how the
    798 * encoder will handle buffering, writing, seeking (if supported), and
    799 * setting a finalized position during the encoding process.
    800 *
    801 * This should not be used when using @ref JxlEncoderProcessOutput.
    802 *
    803 * @param enc encoder object.
    804 * @param output_processor the struct containing the callbacks for managing
    805 * output.
    806 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error.
    807 */
    808 JXL_EXPORT JxlEncoderStatus JxlEncoderSetOutputProcessor(
    809    JxlEncoder* enc, struct JxlEncoderOutputProcessor output_processor);
    810 
    811 /**
    812 * Flushes any buffered input in the encoder, ensuring that all available input
    813 * data has been processed and written to the output.
    814 *
    815 * This function can only be used after @ref JxlEncoderSetOutputProcessor.
    816 * Before making the last call to @ref JxlEncoderFlushInput, users should call
    817 * @ref JxlEncoderCloseInput to signal the end of input data.
    818 *
    819 * This should not be used when using @ref JxlEncoderProcessOutput.
    820 *
    821 * @param enc encoder object.
    822 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error.
    823 */
    824 JXL_EXPORT JxlEncoderStatus JxlEncoderFlushInput(JxlEncoder* enc);
    825 
    826 /**
    827 * This struct provides callback functions to pass pixel data in a streaming
    828 * manner instead of requiring the entire frame data in memory at once.
    829 */
    830 struct JxlChunkedFrameInputSource {
    831  /**
    832   * A pointer to any user-defined data or state. This can be used to pass
    833   * information to the callback functions.
    834   */
    835  void* opaque;
    836 
    837  /**
    838   * Get the pixel format that color channel data will be provided in.
    839   * When called, `pixel_format` points to a suggested pixel format; if
    840   * color channel data can be given in this pixel format, processing might
    841   * be more efficient.
    842   *
    843   * This function will be called exactly once, before any call to
    844   * get_color_channel_at.
    845   *
    846   * @param opaque user supplied parameters to the callback
    847   * @param pixel_format format for pixels
    848   */
    849  void (*get_color_channels_pixel_format)(void* opaque,
    850                                          JxlPixelFormat* pixel_format);
    851 
    852  /**
    853   * Callback to retrieve a rectangle of color channel data at a specific
    854   * location. It is guaranteed that xpos and ypos are multiples of 8. xsize,
    855   * ysize will be multiples of 8, unless the resulting rectangle would be out
    856   * of image bounds. Moreover, xsize and ysize will be at most 2048. The
    857   * returned data will be assumed to be in the format returned by the
    858   * (preceding) call to get_color_channels_pixel_format, except the `align`
    859   * parameter of the pixel format will be ignored. Instead, the `i`-th row will
    860   * be assumed to start at position `return_value + i * *row_offset`, with the
    861   * value of `*row_offset` decided by the callee.
    862   *
    863   * Note that multiple calls to `get_color_channel_data_at` may happen before a
    864   * call to `release_buffer`.
    865   *
    866   * @param opaque user supplied parameters to the callback
    867   * @param xpos horizontal position for the data.
    868   * @param ypos vertical position for the data.
    869   * @param xsize horizontal size of the requested rectangle of data.
    870   * @param ysize vertical size of the requested rectangle of data.
    871   * @param row_offset pointer to a the byte offset between consecutive rows of
    872   * the retrieved pixel data.
    873   * @return pointer to the retrieved pixel data.
    874   */
    875  const void* (*get_color_channel_data_at)(void* opaque, size_t xpos,
    876                                           size_t ypos, size_t xsize,
    877                                           size_t ysize, size_t* row_offset);
    878 
    879  /**
    880   * Get the pixel format that extra channel data will be provided in.
    881   * When called, `pixel_format` points to a suggested pixel format; if
    882   * extra channel data can be given in this pixel format, processing might
    883   * be more efficient.
    884   *
    885   * This function will be called exactly once per index, before any call to
    886   * get_extra_channel_data_at with that given index.
    887   *
    888   * @param opaque user supplied parameters to the callback
    889   * @param ec_index zero-indexed index of the extra channel
    890   * @param pixel_format format for extra channel data
    891   */
    892  void (*get_extra_channel_pixel_format)(void* opaque, size_t ec_index,
    893                                         JxlPixelFormat* pixel_format);
    894 
    895  /**
    896   * Callback to retrieve a rectangle of extra channel `ec_index` data at a
    897   * specific location. It is guaranteed that xpos and ypos are multiples of
    898   * 8. xsize, ysize will be multiples of 8, unless the resulting rectangle
    899   * would be out of image bounds. Moreover, xsize and ysize will be at most
    900   * 2048. The returned data will be assumed to be in the format returned by the
    901   * (preceding) call to get_extra_channels_pixel_format_at with the
    902   * corresponding extra channel index `ec_index`, except the `align` parameter
    903   * of the pixel format will be ignored. Instead, the `i`-th row will be
    904   * assumed to start at position `return_value + i * *row_offset`, with the
    905   * value of `*row_offset` decided by the callee.
    906   *
    907   * Note that multiple calls to `get_extra_channel_data_at` may happen before a
    908   * call to `release_buffer`.
    909   *
    910   * @param opaque user supplied parameters to the callback
    911   * @param xpos horizontal position for the data.
    912   * @param ypos vertical position for the data.
    913   * @param xsize horizontal size of the requested rectangle of data.
    914   * @param ysize vertical size of the requested rectangle of data.
    915   * @param row_offset pointer to a the byte offset between consecutive rows of
    916   * the retrieved pixel data.
    917   * @return pointer to the retrieved pixel data.
    918   */
    919  const void* (*get_extra_channel_data_at)(void* opaque, size_t ec_index,
    920                                           size_t xpos, size_t ypos,
    921                                           size_t xsize, size_t ysize,
    922                                           size_t* row_offset);
    923 
    924  /**
    925   * Releases the buffer `buf` (obtained through a call to
    926   * `get_color_channel_data_at` or `get_extra_channel_data_at`). This function
    927   * will be called exactly once per call to `get_color_channel_data_at` or
    928   * `get_extra_channel_data_at`.
    929   *
    930   * @param opaque user supplied parameters to the callback
    931   * @param buf pointer returned by `get_color_channel_data_at` or
    932   * `get_extra_channel_data_at`
    933   */
    934  void (*release_buffer)(void* opaque, const void* buf);
    935 };
    936 
    937 /**
    938 * @brief Adds a frame to the encoder using a chunked input source.
    939 *
    940 * This function gives a way to encode a frame by providing pixel data in a
    941 * chunked or streaming manner, which can be especially useful when dealing with
    942 * large images that may not fit entirely in memory or when trying to optimize
    943 * memory usage. The input data is provided through callbacks defined in the
    944 * @ref JxlChunkedFrameInputSource struct. Once the frame data has been
    945 * completely retrieved, this function will flush the input and close it if it
    946 * is the last frame.
    947 *
    948 * @param frame_settings set of options and metadata for this frame. Also
    949 * includes reference to the encoder object.
    950 * @param is_last_frame indicates if this is the last frame.
    951 * @param chunked_frame_input struct providing callback methods for retrieving
    952 * pixel data in chunks.
    953 *
    954 * @return Returns a status indicating the success or failure of adding the
    955 * frame.
    956 */
    957 JXL_EXPORT JxlEncoderStatus JxlEncoderAddChunkedFrame(
    958    const JxlEncoderFrameSettings* frame_settings, JXL_BOOL is_last_frame,
    959    struct JxlChunkedFrameInputSource chunked_frame_input);
    960 
    961 /**
    962 * Sets the buffer to read pixels from for an extra channel at a given index.
    963 * The index must be smaller than the num_extra_channels in the associated
    964 * @ref JxlBasicInfo. Must call @ref JxlEncoderSetExtraChannelInfo before @ref
    965 * JxlEncoderSetExtraChannelBuffer.
    966 *
    967 * TODO(firsching): mention what data types in pixel formats are supported.
    968 *
    969 * It is required to call this function for every extra channel, except for the
    970 * alpha channel if that was already set through @ref JxlEncoderAddImageFrame.
    971 *
    972 * @param frame_settings set of options and metadata for this frame. Also
    973 * includes reference to the encoder object.
    974 * @param pixel_format format for pixels. Object owned by the caller and its
    975 * contents are copied internally. The num_channels value is ignored, since the
    976 * number of channels for an extra channel is always assumed to be one.
    977 * @param buffer buffer type to input the pixel data from. Owned by the caller
    978 * and its contents are copied internally.
    979 * @param size size of buffer in bytes. This size should match what is implied
    980 * by the frame dimensions and the pixel format.
    981 * @param index index of the extra channel to use.
    982 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
    983 */
    984 JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelBuffer(
    985    const JxlEncoderFrameSettings* frame_settings,
    986    const JxlPixelFormat* pixel_format, const void* buffer, size_t size,
    987    uint32_t index);
    988 
    989 /** Adds a metadata box to the file format. @ref JxlEncoderProcessOutput must be
    990 * used to effectively write the box to the output. @ref JxlEncoderUseBoxes must
    991 * be enabled before using this function.
    992 *
    993 * Boxes allow inserting application-specific data and metadata (Exif, XML/XMP,
    994 * JUMBF and user defined boxes).
    995 *
    996 * The box format follows ISO BMFF and shares features and box types with other
    997 * image and video formats, including the Exif, XML and JUMBF boxes. The box
    998 * format for JPEG XL is specified in ISO/IEC 18181-2.
    999 *
   1000 * Boxes in general don't contain other boxes inside, except a JUMBF superbox.
   1001 * Boxes follow each other sequentially and are byte-aligned. If the container
   1002 * format is used, the JXL stream consists of concatenated boxes.
   1003 * It is also possible to use a direct codestream without boxes, but in that
   1004 * case metadata cannot be added.
   1005 *
   1006 * Each box generally has the following byte structure in the file:
   1007 * - 4 bytes: box size including box header (Big endian. If set to 0, an
   1008 *   8-byte 64-bit size follows instead).
   1009 * - 4 bytes: type, e.g. "JXL " for the signature box, "jxlc" for a codestream
   1010 *   box.
   1011 * - N bytes: box contents.
   1012 *
   1013 * Only the box contents are provided to the contents argument of this function,
   1014 * the encoder encodes the size header itself. Most boxes are written
   1015 * automatically by the encoder as needed ("JXL ", "ftyp", "jxll", "jxlc",
   1016 * "jxlp", "jxli", "jbrd"), and this function only needs to be called to add
   1017 * optional metadata when encoding from pixels (using @ref
   1018 * JxlEncoderAddImageFrame). When recompressing JPEG files (using @ref
   1019 * JxlEncoderAddJPEGFrame), if the input JPEG contains EXIF, XMP or JUMBF
   1020 * metadata, the corresponding boxes are already added automatically.
   1021 *
   1022 * Box types are given by 4 characters. The following boxes can be added with
   1023 * this function:
   1024 * - "Exif": a box with EXIF metadata, can be added by libjxl users, or is
   1025 *   automatically added when needed for JPEG reconstruction. The contents of
   1026 *   this box must be prepended by a 4-byte tiff header offset, which may
   1027 *   be 4 zero bytes in case the tiff header follows immediately.
   1028 *   The EXIF metadata must be in sync with what is encoded in the JPEG XL
   1029 *   codestream, specifically the image orientation. While this is not
   1030 *   recommended in practice, in case of conflicting metadata, the JPEG XL
   1031 *   codestream takes precedence.
   1032 * - "xml ": a box with XML data, in particular XMP metadata, can be added by
   1033 *   libjxl users, or is automatically added when needed for JPEG reconstruction
   1034 * - "jumb": a JUMBF superbox, which can contain boxes with different types of
   1035 *   metadata inside. This box type can be added by the encoder transparently,
   1036 *   and other libraries to create and handle JUMBF content exist.
   1037 * - Application-specific boxes. Their typename should not begin with "jxl" or
   1038 *   "JXL" or conflict with other existing typenames, and they should be
   1039 *   registered with MP4RA (mp4ra.org).
   1040 *
   1041 * These boxes can be stored uncompressed or Brotli-compressed (using a "brob"
   1042 * box), depending on the compress_box parameter.
   1043 *
   1044 * @param enc encoder object.
   1045 * @param type the box type, e.g. "Exif" for EXIF metadata, "xml " for XMP or
   1046 * IPTC metadata, "jumb" for JUMBF metadata.
   1047 * @param contents the full contents of the box, for example EXIF
   1048 * data. ISO BMFF box header must not be included, only the contents. Owned by
   1049 * the caller and its contents are copied internally.
   1050 * @param size size of the box contents.
   1051 * @param compress_box Whether to compress this box as a "brob" box. Requires
   1052 * Brotli support.
   1053 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error, such as
   1054 * when using this function without @ref JxlEncoderUseContainer, or adding a box
   1055 * type that would result in an invalid file format.
   1056 */
   1057 JXL_EXPORT JxlEncoderStatus JxlEncoderAddBox(JxlEncoder* enc,
   1058                                             const JxlBoxType type,
   1059                                             const uint8_t* contents,
   1060                                             size_t size,
   1061                                             JXL_BOOL compress_box);
   1062 
   1063 /**
   1064 * Indicates the intention to add metadata boxes. This allows @ref
   1065 * JxlEncoderAddBox to be used. When using this function, then it is required
   1066 * to use @ref JxlEncoderCloseBoxes at the end.
   1067 *
   1068 * By default the encoder assumes no metadata boxes will be added.
   1069 *
   1070 * This setting can only be set at the beginning, before encoding starts.
   1071 *
   1072 * @param enc encoder object.
   1073 */
   1074 JXL_EXPORT JxlEncoderStatus JxlEncoderUseBoxes(JxlEncoder* enc);
   1075 
   1076 /**
   1077 * Declares that no further boxes will be added with @ref JxlEncoderAddBox.
   1078 * This function must be called after the last box is added so the encoder knows
   1079 * the stream will be finished. It is not necessary to use this function if
   1080 * @ref JxlEncoderUseBoxes is not used. Further frames may still be added.
   1081 *
   1082 * Must be called between @ref JxlEncoderAddBox of the last box
   1083 * and the next call to @ref JxlEncoderProcessOutput, or @ref
   1084 * JxlEncoderProcessOutput won't output the last box correctly.
   1085 *
   1086 * NOTE: if you don't need to close frames and boxes at separate times, you can
   1087 * use @ref JxlEncoderCloseInput instead to close both at once.
   1088 *
   1089 * @param enc encoder object.
   1090 */
   1091 JXL_EXPORT void JxlEncoderCloseBoxes(JxlEncoder* enc);
   1092 
   1093 /**
   1094 * Declares that no frames will be added and @ref JxlEncoderAddImageFrame and
   1095 * @ref JxlEncoderAddJPEGFrame won't be called anymore. Further metadata boxes
   1096 * may still be added. This function or @ref JxlEncoderCloseInput must be called
   1097 * after adding the last frame and the next call to
   1098 * @ref JxlEncoderProcessOutput, or the frame won't be properly marked as last.
   1099 *
   1100 * NOTE: if you don't need to close frames and boxes at separate times, you can
   1101 * use @ref JxlEncoderCloseInput instead to close both at once.
   1102 *
   1103 * @param enc encoder object.
   1104 */
   1105 JXL_EXPORT void JxlEncoderCloseFrames(JxlEncoder* enc);
   1106 
   1107 /**
   1108 * Closes any input to the encoder, equivalent to calling @ref
   1109 * JxlEncoderCloseFrames as well as calling @ref JxlEncoderCloseBoxes if needed.
   1110 * No further input of any kind may be given to the encoder, but further @ref
   1111 * JxlEncoderProcessOutput calls should be done to create the final output.
   1112 *
   1113 * The requirements of both @ref JxlEncoderCloseFrames and @ref
   1114 * JxlEncoderCloseBoxes apply to this function. Either this function or the
   1115 * other two must be called after the final frame and/or box, and the next
   1116 * @ref JxlEncoderProcessOutput call, or the codestream won't be encoded
   1117 * correctly.
   1118 *
   1119 * @param enc encoder object.
   1120 */
   1121 JXL_EXPORT void JxlEncoderCloseInput(JxlEncoder* enc);
   1122 
   1123 /**
   1124 * Sets the original color encoding of the image encoded by this encoder. This
   1125 * is an alternative to @ref JxlEncoderSetICCProfile and only one of these two
   1126 * must be used. This one sets the color encoding as a @ref JxlColorEncoding,
   1127 * while the other sets it as ICC binary data. Must be called after @ref
   1128 * JxlEncoderSetBasicInfo.
   1129 *
   1130 * @param enc encoder object.
   1131 * @param color color encoding. Object owned by the caller and its contents are
   1132 * copied internally.
   1133 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1134 * JXL_ENC_ERROR otherwise
   1135 */
   1136 JXL_EXPORT JxlEncoderStatus
   1137 JxlEncoderSetColorEncoding(JxlEncoder* enc, const JxlColorEncoding* color);
   1138 
   1139 /**
   1140 * Sets the original color encoding of the image encoded by this encoder as an
   1141 * ICC color profile. This is an alternative to @ref JxlEncoderSetColorEncoding
   1142 * and only one of these two must be used. This one sets the color encoding as
   1143 * ICC binary data, while the other defines it as a @ref JxlColorEncoding. Must
   1144 * be called after @ref JxlEncoderSetBasicInfo.
   1145 *
   1146 * @param enc encoder object.
   1147 * @param icc_profile bytes of the original ICC profile
   1148 * @param size size of the icc_profile buffer in bytes
   1149 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1150 * JXL_ENC_ERROR otherwise
   1151 */
   1152 JXL_EXPORT JxlEncoderStatus JxlEncoderSetICCProfile(JxlEncoder* enc,
   1153                                                    const uint8_t* icc_profile,
   1154                                                    size_t size);
   1155 
   1156 /**
   1157 * Initializes a @ref JxlBasicInfo struct to default values.
   1158 * For forwards-compatibility, this function has to be called before values
   1159 * are assigned to the struct fields.
   1160 * The default values correspond to an 8-bit RGB image, no alpha or any
   1161 * other extra channels.
   1162 *
   1163 * @param info global image metadata. Object owned by the caller.
   1164 */
   1165 JXL_EXPORT void JxlEncoderInitBasicInfo(JxlBasicInfo* info);
   1166 
   1167 /**
   1168 * Initializes a @ref JxlFrameHeader struct to default values.
   1169 * For forwards-compatibility, this function has to be called before values
   1170 * are assigned to the struct fields.
   1171 * The default values correspond to a frame with no animation duration and the
   1172 * 'replace' blend mode. After using this function, For animation duration must
   1173 * be set, for composite still blend settings must be set.
   1174 *
   1175 * @param frame_header frame metadata. Object owned by the caller.
   1176 */
   1177 JXL_EXPORT void JxlEncoderInitFrameHeader(JxlFrameHeader* frame_header);
   1178 
   1179 /**
   1180 * Initializes a @ref JxlBlendInfo struct to default values.
   1181 * For forwards-compatibility, this function has to be called before values
   1182 * are assigned to the struct fields.
   1183 *
   1184 * @param blend_info blending info. Object owned by the caller.
   1185 */
   1186 JXL_EXPORT void JxlEncoderInitBlendInfo(JxlBlendInfo* blend_info);
   1187 
   1188 /**
   1189 * Sets the global metadata of the image encoded by this encoder.
   1190 *
   1191 * If the @ref JxlBasicInfo contains information of extra channels beyond an
   1192 * alpha channel, then @ref JxlEncoderSetExtraChannelInfo must be called between
   1193 * @ref JxlEncoderSetBasicInfo and @ref JxlEncoderAddImageFrame. In order to
   1194 * indicate extra channels, the value of `info.num_extra_channels` should be set
   1195 * to the number of extra channels, also counting the alpha channel if present.
   1196 *
   1197 * @param enc encoder object.
   1198 * @param info global image metadata. Object owned by the caller and its
   1199 * contents are copied internally.
   1200 * @return ::JXL_ENC_SUCCESS if the operation was successful,
   1201 * ::JXL_ENC_ERROR otherwise
   1202 */
   1203 JXL_EXPORT JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc,
   1204                                                   const JxlBasicInfo* info);
   1205 
   1206 /**
   1207 * Sets the upsampling method the decoder will use in case there are frames
   1208 * with ::JXL_ENC_FRAME_SETTING_RESAMPLING set. This is useful in combination
   1209 * with the ::JXL_ENC_FRAME_SETTING_ALREADY_DOWNSAMPLED option, to control
   1210 * the type of upsampling that will be used.
   1211 *
   1212 * @param enc encoder object.
   1213 * @param factor upsampling factor to configure (1, 2, 4 or 8; for 1 this
   1214 * function has no effect at all)
   1215 * @param mode upsampling mode to use for this upsampling:
   1216 * -1: default (good for photographic images, no signaling overhead)
   1217 * 0: nearest neighbor (good for pixel art)
   1218 * 1: 'pixel dots' (same as NN for 2x, diamond-shaped 'pixel dots' for 4x/8x)
   1219 * @return ::JXL_ENC_SUCCESS if the operation was successful,
   1220 * ::JXL_ENC_ERROR otherwise
   1221 */
   1222 JXL_EXPORT JxlEncoderStatus JxlEncoderSetUpsamplingMode(JxlEncoder* enc,
   1223                                                        int64_t factor,
   1224                                                        int64_t mode);
   1225 
   1226 /**
   1227 * Initializes a @ref JxlExtraChannelInfo struct to default values.
   1228 * For forwards-compatibility, this function has to be called before values
   1229 * are assigned to the struct fields.
   1230 * The default values correspond to an 8-bit channel of the provided type.
   1231 *
   1232 * @param type type of the extra channel.
   1233 * @param info global extra channel metadata. Object owned by the caller and its
   1234 * contents are copied internally.
   1235 */
   1236 JXL_EXPORT void JxlEncoderInitExtraChannelInfo(JxlExtraChannelType type,
   1237                                               JxlExtraChannelInfo* info);
   1238 
   1239 /**
   1240 * Sets information for the extra channel at the given index. The index
   1241 * must be smaller than num_extra_channels in the associated @ref JxlBasicInfo.
   1242 *
   1243 * @param enc encoder object
   1244 * @param index index of the extra channel to set.
   1245 * @param info global extra channel metadata. Object owned by the caller and its
   1246 * contents are copied internally.
   1247 * @return ::JXL_ENC_SUCCESS on success, ::JXL_ENC_ERROR on error
   1248 */
   1249 JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelInfo(
   1250    JxlEncoder* enc, size_t index, const JxlExtraChannelInfo* info);
   1251 
   1252 /**
   1253 * Sets the name for the extra channel at the given index in UTF-8. The index
   1254 * must be smaller than the num_extra_channels in the associated @ref
   1255 * JxlBasicInfo.
   1256 *
   1257 * TODO(lode): remove size parameter for consistency with
   1258 * @ref JxlEncoderSetFrameName
   1259 *
   1260 * @param enc encoder object
   1261 * @param index index of the extra channel to set.
   1262 * @param name buffer with the name of the extra channel.
   1263 * @param size size of the name buffer in bytes, not counting the terminating
   1264 * character.
   1265 * @return JXL_ENC_SUCCESS on success, JXL_ENC_ERROR on error
   1266 */
   1267 JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelName(JxlEncoder* enc,
   1268                                                          size_t index,
   1269                                                          const char* name,
   1270                                                          size_t size);
   1271 
   1272 /**
   1273 * Sets a frame-specific option of integer type to the encoder options.
   1274 * The @ref JxlEncoderFrameSettingId argument determines which option is set.
   1275 *
   1276 * @param frame_settings set of options and metadata for this frame. Also
   1277 * includes reference to the encoder object.
   1278 * @param option ID of the option to set.
   1279 * @param value Integer value to set for this option.
   1280 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1281 * JXL_ENC_ERROR in case of an error, such as invalid or unknown option id, or
   1282 * invalid integer value for the given option. If an error is returned, the
   1283 * state of the
   1284 * @ref JxlEncoderFrameSettings object is still valid and is the same as before
   1285 * this function was called.
   1286 */
   1287 JXL_EXPORT JxlEncoderStatus JxlEncoderFrameSettingsSetOption(
   1288    JxlEncoderFrameSettings* frame_settings, JxlEncoderFrameSettingId option,
   1289    int64_t value);
   1290 
   1291 /**
   1292 * Sets a frame-specific option of float type to the encoder options.
   1293 * The @ref JxlEncoderFrameSettingId argument determines which option is set.
   1294 *
   1295 * @param frame_settings set of options and metadata for this frame. Also
   1296 * includes reference to the encoder object.
   1297 * @param option ID of the option to set.
   1298 * @param value Float value to set for this option.
   1299 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1300 * JXL_ENC_ERROR in case of an error, such as invalid or unknown option id, or
   1301 * invalid integer value for the given option. If an error is returned, the
   1302 * state of the
   1303 * @ref JxlEncoderFrameSettings object is still valid and is the same as before
   1304 * this function was called.
   1305 */
   1306 JXL_EXPORT JxlEncoderStatus JxlEncoderFrameSettingsSetFloatOption(
   1307    JxlEncoderFrameSettings* frame_settings, JxlEncoderFrameSettingId option,
   1308    float value);
   1309 
   1310 /** Forces the encoder to use the box-based container format (BMFF) even
   1311 * when not necessary.
   1312 *
   1313 * When using @ref JxlEncoderUseBoxes, @ref JxlEncoderStoreJPEGMetadata or @ref
   1314 * JxlEncoderSetCodestreamLevel with level 10, the encoder will automatically
   1315 * also use the container format, it is not necessary to use
   1316 * @ref JxlEncoderUseContainer for those use cases.
   1317 *
   1318 * By default this setting is disabled.
   1319 *
   1320 * This setting can only be set at the beginning, before encoding starts.
   1321 *
   1322 * @param enc encoder object.
   1323 * @param use_container true if the encoder should always output the JPEG XL
   1324 * container format, false to only output it when necessary.
   1325 * @return JXL_ENC_SUCCESS if the operation was successful, JXL_ENC_ERROR
   1326 * otherwise.
   1327 */
   1328 JXL_EXPORT JxlEncoderStatus JxlEncoderUseContainer(JxlEncoder* enc,
   1329                                                   JXL_BOOL use_container);
   1330 
   1331 /**
   1332 * Configure the encoder to store JPEG reconstruction metadata in the JPEG XL
   1333 * container.
   1334 *
   1335 * If this is set to true and a single JPEG frame is added, it will be
   1336 * possible to losslessly reconstruct the JPEG codestream.
   1337 *
   1338 * This setting can only be set at the beginning, before encoding starts.
   1339 *
   1340 * @param enc encoder object.
   1341 * @param store_jpeg_metadata true if the encoder should store JPEG metadata.
   1342 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1343 * JXL_ENC_ERROR otherwise.
   1344 */
   1345 JXL_EXPORT JxlEncoderStatus
   1346 JxlEncoderStoreJPEGMetadata(JxlEncoder* enc, JXL_BOOL store_jpeg_metadata);
   1347 
   1348 /** Sets the feature level of the JPEG XL codestream. Valid values are 5 and
   1349 * 10, or -1 (to choose automatically). Using the minimum required level, or
   1350 * level 5 in most cases, is recommended for compatibility with all decoders.
   1351 *
   1352 * Level 5: for end-user image delivery, this level is the most widely
   1353 * supported level by image decoders and the recommended level to use unless a
   1354 * level 10 feature is absolutely necessary. Supports a maximum resolution
   1355 * 268435456 pixels total with a maximum width or height of 262144 pixels,
   1356 * maximum 16-bit color channel depth, maximum 120 frames per second for
   1357 * animation, maximum ICC color profile size of 4 MiB, it allows all color
   1358 * models and extra channel types except CMYK and the JXL_CHANNEL_BLACK
   1359 * extra channel, and a maximum of 4 extra channels in addition to the 3 color
   1360 * channels. It also sets boundaries to certain internally used coding tools.
   1361 *
   1362 * Level 10: this level removes or increases the bounds of most of the level
   1363 * 5 limitations, allows CMYK color and up to 32 bits per color channel, but
   1364 * may be less widely supported.
   1365 *
   1366 * The default value is -1. This means the encoder will automatically choose
   1367 * between level 5 and level 10 based on what information is inside the @ref
   1368 * JxlBasicInfo structure. Do note that some level 10 features, particularly
   1369 * those used by animated JPEG XL codestreams, might require level 10, even
   1370 * though the @ref JxlBasicInfo only suggests level 5. In this case, the level
   1371 * must be explicitly set to 10, otherwise the encoder will return an error.
   1372 * The encoder will restrict internal encoding choices to those compatible with
   1373 * the level setting.
   1374 *
   1375 * This setting can only be set at the beginning, before encoding starts.
   1376 *
   1377 * @param enc encoder object.
   1378 * @param level the level value to set, must be -1, 5, or 10.
   1379 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1380 * JXL_ENC_ERROR otherwise.
   1381 */
   1382 JXL_EXPORT JxlEncoderStatus JxlEncoderSetCodestreamLevel(JxlEncoder* enc,
   1383                                                         int level);
   1384 
   1385 /** Returns the codestream level required to support the currently configured
   1386 * settings and basic info. This function can only be used at the beginning,
   1387 * before encoding starts, but after setting basic info.
   1388 *
   1389 * This does not support per-frame settings, only global configuration, such as
   1390 * the image dimensions, that are known at the time of writing the header of
   1391 * the JPEG XL file.
   1392 *
   1393 * If this returns 5, nothing needs to be done and the codestream can be
   1394 * compatible with any decoder. If this returns 10, @ref
   1395 * JxlEncoderSetCodestreamLevel has to be used to set the codestream level to
   1396 * 10, or the encoder can be configured differently to allow using the more
   1397 * compatible level 5.
   1398 *
   1399 * @param enc encoder object.
   1400 * @return -1 if no level can support the configuration (e.g. image dimensions
   1401 * larger than even level 10 supports), 5 if level 5 is supported, 10 if setting
   1402 * the codestream level to 10 is required.
   1403 *
   1404 */
   1405 JXL_EXPORT int JxlEncoderGetRequiredCodestreamLevel(const JxlEncoder* enc);
   1406 
   1407 /**
   1408 * Enables lossless encoding.
   1409 *
   1410 * This is not an option like the others on itself, but rather while enabled it
   1411 * overrides a set of existing options (such as distance, modular mode and
   1412 * color transform) that enables bit-for-bit lossless encoding.
   1413 *
   1414 * When disabled, those options are not overridden, but since those options
   1415 * could still have been manually set to a combination that operates losslessly,
   1416 * using this function with lossless set to ::JXL_FALSE does not
   1417 * guarantee lossy encoding, though the default set of options is lossy.
   1418 *
   1419 * @param frame_settings set of options and metadata for this frame. Also
   1420 * includes reference to the encoder object.
   1421 * @param lossless whether to override options for lossless mode
   1422 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1423 * JXL_ENC_ERROR otherwise.
   1424 */
   1425 JXL_EXPORT JxlEncoderStatus JxlEncoderSetFrameLossless(
   1426    JxlEncoderFrameSettings* frame_settings, JXL_BOOL lossless);
   1427 
   1428 /**
   1429 * Sets the distance level for lossy compression: target max butteraugli
   1430 * distance, lower = higher quality. Range: 0 .. 25.
   1431 * 0.0 = mathematically lossless (however, use @ref JxlEncoderSetFrameLossless
   1432 * instead to use true lossless, as setting distance to 0 alone is not the only
   1433 * requirement). 1.0 = visually lossless. Recommended range: 0.5 .. 3.0. Default
   1434 * value: 1.0.
   1435 *
   1436 * @param frame_settings set of options and metadata for this frame. Also
   1437 * includes reference to the encoder object.
   1438 * @param distance the distance value to set.
   1439 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1440 * JXL_ENC_ERROR otherwise.
   1441 */
   1442 JXL_EXPORT JxlEncoderStatus JxlEncoderSetFrameDistance(
   1443    JxlEncoderFrameSettings* frame_settings, float distance);
   1444 
   1445 /**
   1446 * Sets the distance level for lossy compression of extra channels.
   1447 * The distance is as in @ref JxlEncoderSetFrameDistance (lower = higher
   1448 * quality). If not set, or if set to the special value -1, the distance that
   1449 * was set with
   1450 * @ref JxlEncoderSetFrameDistance will be used.
   1451 *
   1452 * @param frame_settings set of options and metadata for this frame. Also
   1453 * includes reference to the encoder object.
   1454 * @param index index of the extra channel to set a distance value for.
   1455 * @param distance the distance value to set.
   1456 * @return ::JXL_ENC_SUCCESS if the operation was successful, @ref
   1457 * JXL_ENC_ERROR otherwise.
   1458 */
   1459 JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelDistance(
   1460    JxlEncoderFrameSettings* frame_settings, size_t index, float distance);
   1461 
   1462 /**
   1463 * Maps JPEG-style quality factor to distance.
   1464 *
   1465 * This function takes in input a JPEG-style quality factor `quality` and
   1466 * produces as output a `distance` value suitable to be used with @ref
   1467 * JxlEncoderSetFrameDistance and @ref JxlEncoderSetExtraChannelDistance.
   1468 *
   1469 * The `distance` value influences the level of compression, with lower values
   1470 * indicating higher quality:
   1471 * - 0.0 implies lossless compression (however, note that calling @ref
   1472 * JxlEncoderSetFrameLossless is required).
   1473 * - 1.0 represents a visually lossy compression, which is also the default
   1474 * setting.
   1475 *
   1476 * The `quality` parameter, ranging up to 100, is inversely related to
   1477 * 'distance':
   1478 * - A `quality` of 100.0 maps to a `distance` of 0.0 (lossless).
   1479 * - A `quality` of 90.0 corresponds to a `distance` of 1.0.
   1480 *
   1481 * Recommended Range:
   1482 * - `distance`: 0.5 to 3.0.
   1483 * - corresponding `quality`: approximately 96 to 68.
   1484 *
   1485 * Allowed Range:
   1486 * - `distance`: 0.0 to 25.0.
   1487 * - corresponding `quality`: 100.0 to 0.0.
   1488 *
   1489 * Note: the `quality` parameter has no consistent psychovisual meaning
   1490 * across different codecs and libraries. Using the mapping defined by @ref
   1491 * JxlEncoderDistanceFromQuality will result in a visual quality roughly
   1492 * equivalent to what would be obtained with `libjpeg-turbo` with the same
   1493 * `quality` parameter, but that is by no means guaranteed; do not assume that
   1494 * the same quality value will result in similar file sizes and image quality
   1495 * across different codecs.
   1496 */
   1497 JXL_EXPORT float JxlEncoderDistanceFromQuality(float quality);
   1498 
   1499 /**
   1500 * Create a new set of encoder options, with all values initially copied from
   1501 * the @p source options, or set to default if @p source is NULL.
   1502 *
   1503 * The returned pointer is an opaque struct tied to the encoder and it will be
   1504 * deallocated by the encoder when @ref JxlEncoderDestroy() is called. For
   1505 * functions taking both a @ref JxlEncoder and a @ref JxlEncoderFrameSettings,
   1506 * only @ref JxlEncoderFrameSettings created with this function for the same
   1507 * encoder instance can be used.
   1508 *
   1509 * @param enc encoder object.
   1510 * @param source source options to copy initial values from, or NULL to get
   1511 * defaults initialized to defaults.
   1512 * @return the opaque struct pointer identifying a new set of encoder options.
   1513 */
   1514 JXL_EXPORT JxlEncoderFrameSettings* JxlEncoderFrameSettingsCreate(
   1515    JxlEncoder* enc, const JxlEncoderFrameSettings* source);
   1516 
   1517 /**
   1518 * Sets a color encoding to be sRGB.
   1519 *
   1520 * @param color_encoding color encoding instance.
   1521 * @param is_gray whether the color encoding should be gray scale or color.
   1522 */
   1523 JXL_EXPORT void JxlColorEncodingSetToSRGB(JxlColorEncoding* color_encoding,
   1524                                          JXL_BOOL is_gray);
   1525 
   1526 /**
   1527 * Sets a color encoding to be linear sRGB.
   1528 *
   1529 * @param color_encoding color encoding instance.
   1530 * @param is_gray whether the color encoding should be gray scale or color.
   1531 */
   1532 JXL_EXPORT void JxlColorEncodingSetToLinearSRGB(
   1533    JxlColorEncoding* color_encoding, JXL_BOOL is_gray);
   1534 
   1535 /**
   1536 * Enables usage of expert options.
   1537 *
   1538 * At the moment, the only expert option is setting an effort value of 11,
   1539 * which gives the best compression for pixel-lossless modes but is very slow.
   1540 *
   1541 * @param enc encoder object.
   1542 */
   1543 JXL_EXPORT void JxlEncoderAllowExpertOptions(JxlEncoder* enc);
   1544 
   1545 /**
   1546 * Function type for @ref JxlEncoderSetDebugImageCallback.
   1547 *
   1548 * The callback may be called simultaneously by different threads when using a
   1549 * threaded parallel runner, on different debug images.
   1550 *
   1551 * @param opaque optional user data, as given to @ref
   1552 *   JxlEncoderSetDebugImageCallback.
   1553 * @param label label of debug image, can be used in filenames
   1554 * @param xsize width of debug image
   1555 * @param ysize height of debug image
   1556 * @param color color encoding of debug image
   1557 * @param pixels pixel data of debug image as big-endian 16-bit unsigned
   1558 *   samples. The memory is not owned by the user, and is only valid during the
   1559 *   time the callback is running.
   1560 */
   1561 typedef void (*JxlDebugImageCallback)(void* opaque, const char* label,
   1562                                      size_t xsize, size_t ysize,
   1563                                      const JxlColorEncoding* color,
   1564                                      const uint16_t* pixels);
   1565 
   1566 /**
   1567 * Sets the given debug image callback that will be used by the encoder to
   1568 * output various debug images during encoding.
   1569 *
   1570 * This only has any effect if the encoder was compiled with the appropriate
   1571 * debug build flags.
   1572 *
   1573 * @param frame_settings set of options and metadata for this frame. Also
   1574 * includes reference to the encoder object.
   1575 * @param callback used to return the debug image
   1576 * @param opaque user supplied parameter to the image callback
   1577 */
   1578 JXL_EXPORT void JxlEncoderSetDebugImageCallback(
   1579    JxlEncoderFrameSettings* frame_settings, JxlDebugImageCallback callback,
   1580    void* opaque);
   1581 
   1582 /**
   1583 * Sets the given stats object for gathering various statistics during encoding.
   1584 *
   1585 * This only has any effect if the encoder was compiled with the appropriate
   1586 * debug build flags.
   1587 *
   1588 * @param frame_settings set of options and metadata for this frame. Also
   1589 * includes reference to the encoder object.
   1590 * @param stats object that can be used to query the gathered stats (created
   1591 *   by @ref JxlEncoderStatsCreate)
   1592 */
   1593 JXL_EXPORT void JxlEncoderCollectStats(JxlEncoderFrameSettings* frame_settings,
   1594                                       JxlEncoderStats* stats);
   1595 
   1596 #ifdef __cplusplus
   1597 }
   1598 #endif
   1599 
   1600 #endif /* JXL_ENCODE_H_ */
   1601 
   1602 /** @}*/