encode.h 68 KB

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