gd_avif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <limits.h>
  8. #include <math.h>
  9. #include "gd.h"
  10. #include "gd_errors.h"
  11. #include "gdhelpers.h"
  12. #include "gd_intern.h"
  13. #ifdef HAVE_LIBAVIF
  14. #include <avif/avif.h>
  15. /*
  16. Define defaults for encoding images:
  17. CHROMA_SUBSAMPLING_DEFAULT: 4:2:0 is commonly used for Chroma subsampling.
  18. CHROMA_SUBAMPLING_HIGH_QUALITY: Use 4:4:4, or no subsampling, when a sufficient high quality is requested.
  19. SUBAMPLING_HIGH_QUALITY_THRESHOLD: At or above this value, use CHROMA_SUBAMPLING_HIGH_QUALITY
  20. QUANTIZER_DEFAULT:
  21. We need more testing to really know what quantizer settings are optimal,
  22. but teams at Google have been using maximum=30 as a starting point.
  23. QUALITY_DEFAULT: following gd conventions, -1 indicates the default.
  24. SPEED_DEFAULT:
  25. AVIF_SPEED_DEFAULT is simply the default encoding speed of the AV1 codec.
  26. This could be as slow as 0. So we use 6, which is currently considered to be a fine default.
  27. */
  28. #define CHROMA_SUBSAMPLING_DEFAULT AVIF_PIXEL_FORMAT_YUV420
  29. #define CHROMA_SUBAMPLING_HIGH_QUALITY AVIF_PIXEL_FORMAT_YUV444
  30. #define HIGH_QUALITY_SUBSAMPLING_THRESHOLD 90
  31. #define QUANTIZER_DEFAULT 30
  32. #define QUALITY_DEFAULT -1
  33. #define SPEED_DEFAULT 6
  34. // This initial size for the gdIOCtx is standard among GD image conversion functions.
  35. #define NEW_DYNAMIC_CTX_SIZE 2048
  36. // Our quality param ranges from 0 to 100.
  37. // To calculate quality, we convert from AVIF's quantizer scale, which runs from 63 to 0.
  38. #define MAX_QUALITY 100
  39. // These constants are for computing the number of tiles and threads to use during encoding.
  40. // Maximum threads are from libavif/contrib/gkd-pixbuf/loader.c.
  41. #define MIN_TILE_AREA (512 * 512)
  42. #define MAX_TILES 8
  43. #define MAX_THREADS 64
  44. /*** Macros ***/
  45. /*
  46. From gd_png.c:
  47. convert the 7-bit alpha channel to an 8-bit alpha channel.
  48. We do a little bit-flipping magic, repeating the MSB
  49. as the LSB, to ensure that 0 maps to 0 and
  50. 127 maps to 255. We also have to invert to match
  51. PNG's convention in which 255 is opaque.
  52. */
  53. #define alpha7BitTo8Bit(alpha7Bit) \
  54. (alpha7Bit == 127 ? \
  55. 0 : \
  56. 255 - ((alpha7Bit << 1) + (alpha7Bit >> 6)))
  57. #define alpha8BitTo7Bit(alpha8Bit) (gdAlphaMax - (alpha8Bit >> 1))
  58. /*** Helper functions ***/
  59. /* Convert the quality param we expose to the quantity params used by libavif.
  60. The *Quantizer* params values can range from 0 to 63, with 0 = highest quality and 63 = worst.
  61. We make the scale 0-100, and we reverse this, so that 0 = worst quality and 100 = highest.
  62. Values below 0 are set to 0, and values below MAX_QUALITY are set to MAX_QUALITY.
  63. */
  64. static int quality2Quantizer(int quality) {
  65. int clampedQuality = CLAMP(quality, 0, MAX_QUALITY);
  66. float scaleFactor = (float) AVIF_QUANTIZER_WORST_QUALITY / (float) MAX_QUALITY;
  67. return round(scaleFactor * (MAX_QUALITY - clampedQuality));
  68. }
  69. /*
  70. As of February 2021, this algorithm reflects the latest research on how many tiles
  71. and threads to include for a given image size.
  72. This is subject to change as research continues.
  73. Returns false if there was an error, true if all was well.
  74. */
  75. static avifBool setEncoderTilesAndThreads(avifEncoder *encoder, avifRGBImage *rgb) {
  76. int imageArea, tiles, tilesLog2, encoderTiles;
  77. // _gdImageAvifCtx(), the calling function, checks this operation for overflow
  78. imageArea = rgb->width * rgb->height;
  79. tiles = (int) ceil((double) imageArea / MIN_TILE_AREA);
  80. tiles = MIN(tiles, MAX_TILES);
  81. tiles = MIN(tiles, MAX_THREADS);
  82. // The number of tiles in any dimension will always be a power of 2. We can only specify log(2)tiles.
  83. tilesLog2 = floor(log2(tiles));
  84. // If the image's width is greater than the height, use more tile columns
  85. // than tile rows to make the tile size close to a square.
  86. if (rgb->width >= rgb->height) {
  87. encoder->tileRowsLog2 = tilesLog2 / 2;
  88. encoder->tileColsLog2 = tilesLog2 - encoder->tileRowsLog2;
  89. } else {
  90. encoder->tileColsLog2 = tilesLog2 / 2;
  91. encoder->tileRowsLog2 = tilesLog2 - encoder->tileColsLog2;
  92. }
  93. // It's good to have one thread per tile.
  94. encoderTiles = (1 << encoder->tileRowsLog2) * (1 << encoder->tileColsLog2);
  95. encoder->maxThreads = encoderTiles;
  96. return AVIF_TRUE;
  97. }
  98. /*
  99. We can handle AVIF images whose color profile is sRGB, or whose color profile isn't set.
  100. */
  101. static avifBool isAvifSrgbImage(avifImage *avifIm) {
  102. return
  103. (avifIm->colorPrimaries == AVIF_COLOR_PRIMARIES_BT709 ||
  104. avifIm->colorPrimaries == AVIF_COLOR_PRIMARIES_UNSPECIFIED) &&
  105. (avifIm->transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SRGB ||
  106. avifIm->transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED)
  107. ;
  108. }
  109. /*
  110. Check the result from an Avif function to see if it's an error.
  111. If so, decode the error and output it, and return true.
  112. Otherwise, return false.
  113. */
  114. static avifBool isAvifError(avifResult result, const char *msg) {
  115. if (result != AVIF_RESULT_OK) {
  116. gd_error("avif error - %s: %s\n", msg, avifResultToString(result));
  117. return AVIF_TRUE;
  118. }
  119. return AVIF_FALSE;
  120. }
  121. typedef struct avifIOCtxReader {
  122. avifIO io; // this must be the first member for easy casting to avifIO*
  123. avifROData rodata;
  124. } avifIOCtxReader;
  125. /*
  126. <readfromCtx> implements the avifIOReadFunc interface by calling the relevant functions
  127. in the gdIOCtx. Our logic is inspired by avifIOMemoryReaderRead() and avifIOFileReaderRead().
  128. We don't know whether we're reading from a file or from memory. We don't have to know,
  129. since we rely on the helper functions in the gdIOCtx.
  130. We assume we've stashed the gdIOCtx in io->data, as we do in createAvifIOFromCtx().
  131. We ignore readFlags, just as the avifIO*ReaderRead() functions do.
  132. If there's a problem, this returns an avifResult error.
  133. If things go well, return AVIF_RESULT_OK.
  134. Of course these AVIF codes shouldn't be returned by any top-level GD function.
  135. */
  136. static avifResult readFromCtx(avifIO *io, uint32_t readFlags, uint64_t offset, size_t size, avifROData *out)
  137. {
  138. gdIOCtx *ctx = (gdIOCtx *) io->data;
  139. avifIOCtxReader *reader = (avifIOCtxReader *) io;
  140. // readFlags is unsupported
  141. if (readFlags != 0) {
  142. return AVIF_RESULT_IO_ERROR;
  143. }
  144. // TODO: if we set sizeHint, this will be more efficient.
  145. if (offset > INT_MAX || size > INT_MAX)
  146. return AVIF_RESULT_IO_ERROR;
  147. // Try to seek offset bytes forward. If we pass the end of the buffer, throw an error.
  148. if (!ctx->seek(ctx, (int) offset))
  149. return AVIF_RESULT_IO_ERROR;
  150. if (size > reader->rodata.size) {
  151. reader->rodata.data = gdRealloc((void *) reader->rodata.data, size);
  152. reader->rodata.size = size;
  153. }
  154. if (!reader->rodata.data) {
  155. gd_error("avif error - couldn't allocate memory");
  156. return AVIF_RESULT_UNKNOWN_ERROR;
  157. }
  158. // Read the number of bytes requested.
  159. // If getBuf() returns a negative value, that means there was an error.
  160. int charsRead = ctx->getBuf(ctx, (void *) reader->rodata.data, (int) size);
  161. if (charsRead < 0) {
  162. return AVIF_RESULT_IO_ERROR;
  163. }
  164. out->data = reader->rodata.data;
  165. out->size = charsRead;
  166. return AVIF_RESULT_OK;
  167. }
  168. // avif.h says this is optional, but it seemed easy to implement.
  169. static void destroyAvifIO(struct avifIO *io) {
  170. avifIOCtxReader *reader = (avifIOCtxReader *) io;
  171. if (reader->rodata.data != NULL) {
  172. gdFree((void *) reader->rodata.data);
  173. }
  174. gdFree(reader);
  175. }
  176. /* Set up an avifIO object.
  177. The functions in the gdIOCtx struct may point either to a file or a memory buffer.
  178. To us, that's immaterial.
  179. Our task is simply to assign avifIO functions to the proper functions from gdIOCtx.
  180. The destroy function needs to destroy the avifIO object and anything else it uses.
  181. Returns NULL if memory for the object can't be allocated.
  182. */
  183. // TODO: can we get sizeHint somehow?
  184. static avifIO *createAvifIOFromCtx(gdIOCtx *ctx) {
  185. struct avifIOCtxReader *reader;
  186. reader = gdMalloc(sizeof(*reader));
  187. if (reader == NULL)
  188. return NULL;
  189. // TODO: setting persistent=FALSE is safe, but it's less efficient. Is it necessary?
  190. reader->io.persistent = AVIF_FALSE;
  191. reader->io.read = readFromCtx;
  192. reader->io.write = NULL; // this function is currently unused; see avif.h
  193. reader->io.destroy = destroyAvifIO;
  194. reader->io.sizeHint = 0; // sadly, we don't get this information from the gdIOCtx.
  195. reader->io.data = ctx;
  196. reader->rodata.data = NULL;
  197. reader->rodata.size = 0;
  198. return (avifIO *) reader;
  199. }
  200. /*** Decoding functions ***/
  201. /*
  202. Function: gdImageCreateFromAvif
  203. <gdImageCreateFromAvif> is called to load truecolor images from
  204. AVIF format files. Invoke <gdImageCreateFromAvif> with an
  205. already opened pointer to a file containing the desired
  206. image. <gdImageCreateFromAvif> returns a <gdImagePtr> to the new
  207. truecolor image, or NULL if unable to load the image (most often
  208. because the file is corrupt or does not contain a AVIF
  209. image). <gdImageCreateFromAvif> does not close the file.
  210. This function creates a gdIOCtx struct from the file pointer it's passed.
  211. And then it relies on <gdImageCreateFromAvifCtx> to do the real decoding work.
  212. If the file contains an image sequence, we simply read the first one, discarding the rest.
  213. Variants:
  214. <gdImageCreateFromAvifPtr> creates an image from AVIF data
  215. already in memory.
  216. <gdImageCreateFromAvifCtx> reads data from the function
  217. pointers in a <gdIOCtx> structure.
  218. Parameters:
  219. infile - pointer to the input file
  220. Returns:
  221. A pointer to the new truecolor image. This will need to be
  222. destroyed with <gdImageDestroy> once it is no longer needed.
  223. On error, returns 0.
  224. */
  225. gdImagePtr gdImageCreateFromAvif(FILE *infile)
  226. {
  227. gdImagePtr im;
  228. gdIOCtx *ctx = gdNewFileCtx(infile);
  229. if (!ctx)
  230. return NULL;
  231. im = gdImageCreateFromAvifCtx(ctx);
  232. ctx->gd_free(ctx);
  233. return im;
  234. }
  235. /*
  236. Function: gdImageCreateFromAvifPtr
  237. See <gdImageCreateFromAvif>.
  238. Parameters:
  239. size - size of Avif data in bytes.
  240. data - pointer to Avif data.
  241. */
  242. gdImagePtr gdImageCreateFromAvifPtr(int size, void *data)
  243. {
  244. gdImagePtr im;
  245. gdIOCtx *ctx = gdNewDynamicCtxEx(size, data, 0);
  246. if (!ctx)
  247. return 0;
  248. im = gdImageCreateFromAvifCtx(ctx);
  249. ctx->gd_free(ctx);
  250. return im;
  251. }
  252. /*
  253. Function: gdImageCreateFromAvifCtx
  254. See <gdImageCreateFromAvif>.
  255. Additional details: the AVIF library comes with functions to create an IO object from
  256. a file and from a memory pointer. Of course, it doesn't have a way to create an IO object
  257. from a gdIOCtx. So, here, we use our own helper function, <createAvifIOfromCtx>.
  258. Otherwise, we create the image by calling AVIF library functions in order:
  259. * avifDecoderCreate(), to create the decoder
  260. * avifDecoderSetIO(), to tell libavif how to read from our data structure
  261. * avifDecoderParse(), to parse the image
  262. * avifDecoderNextImage(), to read the first image from the decoder
  263. * avifRGBImageSetDefaults(), to create the avifRGBImage
  264. * avifRGBImageAllocatePixels(), to allocate memory for the pixels
  265. * avifImageYUVToRGB(), to convert YUV to RGB
  266. Finally, we create a new gd image and copy over the pixel data.
  267. Parameters:
  268. ctx - a gdIOCtx struct
  269. */
  270. gdImagePtr gdImageCreateFromAvifCtx (gdIOCtx *ctx)
  271. {
  272. uint32_t x, y;
  273. gdImage *im = NULL;
  274. avifResult result;
  275. avifIO *io;
  276. avifDecoder *decoder;
  277. avifRGBImage rgb;
  278. // this lets us know that memory hasn't been allocated yet for the pixels
  279. rgb.pixels = NULL;
  280. decoder = avifDecoderCreate();
  281. // Check if libavif version is >= 0.9.1
  282. // If so, allow the PixelInformationProperty ('pixi') to be missing in AV1 image
  283. // items. libheif v1.11.0 or older does not add the 'pixi' item property to
  284. // AV1 image items. (This issue has been corrected in libheif v1.12.0.)
  285. #if AVIF_VERSION >= 90100
  286. decoder->strictFlags &= ~AVIF_STRICT_PIXI_REQUIRED;
  287. #endif
  288. io = createAvifIOFromCtx(ctx);
  289. if (!io) {
  290. gd_error("avif error - Could not allocate memory");
  291. goto cleanup;
  292. }
  293. avifDecoderSetIO(decoder, io);
  294. result = avifDecoderParse(decoder);
  295. if (isAvifError(result, "Could not parse image"))
  296. goto cleanup;
  297. // Note again that, for an image sequence, we read only the first image, ignoring the rest.
  298. result = avifDecoderNextImage(decoder);
  299. if (isAvifError(result, "Could not decode image"))
  300. goto cleanup;
  301. if (!isAvifSrgbImage(decoder->image))
  302. gd_error_ex(GD_NOTICE, "Image's color profile is not sRGB");
  303. // Set up the avifRGBImage, and convert it from YUV to an 8-bit RGB image.
  304. // (While AVIF image pixel depth can be 8, 10, or 12 bits, GD truecolor images are 8-bit.)
  305. avifRGBImageSetDefaults(&rgb, decoder->image);
  306. rgb.depth = 8;
  307. avifRGBImageAllocatePixels(&rgb);
  308. result = avifImageYUVToRGB(decoder->image, &rgb);
  309. if (isAvifError(result, "Conversion from YUV to RGB failed"))
  310. goto cleanup;
  311. im = gdImageCreateTrueColor(decoder->image->width, decoder->image->height);
  312. if (!im) {
  313. gd_error("avif error - Could not create GD truecolor image");
  314. goto cleanup;
  315. }
  316. im->saveAlphaFlag = 1;
  317. // Read the pixels from the AVIF image and copy them into the GD image.
  318. uint8_t *p = rgb.pixels;
  319. for (y = 0; y < decoder->image->height; y++) {
  320. for (x = 0; x < decoder->image->width; x++) {
  321. uint8_t r = *(p++);
  322. uint8_t g = *(p++);
  323. uint8_t b = *(p++);
  324. uint8_t a = alpha8BitTo7Bit(*(p++));
  325. im->tpixels[y][x] = gdTrueColorAlpha(r, g, b, a);
  326. }
  327. }
  328. cleanup:
  329. // if io has been allocated, this frees it
  330. avifDecoderDestroy(decoder);
  331. if (rgb.pixels)
  332. avifRGBImageFreePixels(&rgb);
  333. return im;
  334. }
  335. /*** Encoding functions ***/
  336. /*
  337. Function: gdImageAvifEx
  338. <gdImageAvifEx> outputs the specified image to the specified file in
  339. AVIF format. The file must be open for writing. Under MSDOS and
  340. all versions of Windows, it is important to use "wb" as opposed to
  341. simply "w" as the mode when opening the file, and under Unix there
  342. is no penalty for doing so. <gdImageAvifEx> does not close the file;
  343. your code must do so.
  344. Variants:
  345. <gdImageAvifEx> writes the image to a file, encoding with the default quality and speed.
  346. <gdImageAvifPtrEx> stores the image in RAM.
  347. <gdImageAvifPtr> stores the image in RAM, encoding with the default quality and speed.
  348. <gdImageAvifCtx> stores the image using a <gdIOCtx> struct.
  349. Parameters:
  350. im - The image to save.
  351. outFile - The FILE pointer to write to.
  352. quality - Compression quality (0-100). 0 is lowest-quality, 100 is highest.
  353. speed - The speed of compression (0-10). 0 is slowest, 10 is fastest.
  354. Notes on parameters:
  355. quality - If quality = -1, we use a default quality as defined in QUALITY_DEFAULT.
  356. For information on how we convert this quality to libavif's quantity param, see <quality2Quantizer>.
  357. speed - At slower speeds, encoding may be quite slow. Use judiciously.
  358. Qualities or speeds that are lower than the minimum value get clamped to the minimum value,
  359. and qualities or speeds that are lower than the maximum value get clamped to the maxmum value.
  360. Note that AVIF_SPEED_DEFAULT is -1. If we ever set SPEED_DEFAULT = AVIF_SPEED_DEFAULT,
  361. we'd want to add a conditional to ensure that value doesn't get clamped.
  362. Returns:
  363. * for <gdImageAvifEx>, <gdImageAvif>, and <gdImageAvifCtx>, nothing.
  364. * for <gdImageAvifPtrEx> and <gdImageAvifPtr>, a pointer to the image in memory.
  365. */
  366. /*
  367. If we're passed the QUALITY_DEFAULT of -1, set the quantizer params to QUANTIZER_DEFAULT.
  368. */
  369. void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed)
  370. {
  371. avifResult result;
  372. avifRGBImage rgb;
  373. avifRWData avifOutput = AVIF_DATA_EMPTY;
  374. avifBool lossless = quality == 100;
  375. avifEncoder *encoder = NULL;
  376. uint32_t val;
  377. uint8_t *p;
  378. uint32_t x, y;
  379. if (im == NULL)
  380. return;
  381. if (!gdImageTrueColor(im)) {
  382. gd_error("avif error - avif doesn't support palette images");
  383. return;
  384. }
  385. if (!gdImageSX(im) || !gdImageSY(im)) {
  386. gd_error("avif error - image dimensions must not be zero");
  387. return;
  388. }
  389. if (overflow2(gdImageSX(im), gdImageSY(im))) {
  390. gd_error("avif error - image dimensions are too large");
  391. return;
  392. }
  393. speed = CLAMP(speed, AVIF_SPEED_SLOWEST, AVIF_SPEED_FASTEST);
  394. avifPixelFormat subsampling = quality >= HIGH_QUALITY_SUBSAMPLING_THRESHOLD ?
  395. CHROMA_SUBAMPLING_HIGH_QUALITY : CHROMA_SUBSAMPLING_DEFAULT;
  396. // Create the AVIF image.
  397. // Set the ICC to sRGB, as that's what gd supports right now.
  398. // Note that MATRIX_COEFFICIENTS_IDENTITY enables lossless conversion from RGB to YUV.
  399. avifImage *avifIm = avifImageCreate(gdImageSX(im), gdImageSY(im), 8, subsampling);
  400. avifIm->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709;
  401. avifIm->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB;
  402. avifIm->matrixCoefficients = lossless ? AVIF_MATRIX_COEFFICIENTS_IDENTITY : AVIF_MATRIX_COEFFICIENTS_BT709;
  403. avifRGBImageSetDefaults(&rgb, avifIm);
  404. // this allocates memory, and sets rgb.rowBytes and rgb.pixels.
  405. avifRGBImageAllocatePixels(&rgb);
  406. // Parse RGB data from the GD image, and copy it into the AVIF RGB image.
  407. // Convert 7-bit GD alpha channel values to 8-bit AVIF values.
  408. p = rgb.pixels;
  409. for (y = 0; y < rgb.height; y++) {
  410. for (x = 0; x < rgb.width; x++) {
  411. val = im->tpixels[y][x];
  412. *(p++) = gdTrueColorGetRed(val);
  413. *(p++) = gdTrueColorGetGreen(val);
  414. *(p++) = gdTrueColorGetBlue(val);
  415. *(p++) = alpha7BitTo8Bit(gdTrueColorGetAlpha(val));
  416. }
  417. }
  418. // Convert the RGB image to YUV.
  419. result = avifImageRGBToYUV(avifIm, &rgb);
  420. if (isAvifError(result, "Could not convert image to YUV"))
  421. goto cleanup;
  422. // Encode the image in AVIF format.
  423. encoder = avifEncoderCreate();
  424. int quantizerQuality = quality == QUALITY_DEFAULT ?
  425. QUANTIZER_DEFAULT : quality2Quantizer(quality);
  426. encoder->minQuantizer = quantizerQuality;
  427. encoder->maxQuantizer = quantizerQuality;
  428. encoder->minQuantizerAlpha = quantizerQuality;
  429. encoder->maxQuantizerAlpha = quantizerQuality;
  430. encoder->speed = speed;
  431. if (!setEncoderTilesAndThreads(encoder, &rgb))
  432. goto cleanup;
  433. //TODO: is there a reason to use timeSscales != 1?
  434. result = avifEncoderAddImage(encoder, avifIm, 1, AVIF_ADD_IMAGE_FLAG_SINGLE);
  435. if (isAvifError(result, "Could not encode image"))
  436. goto cleanup;
  437. result = avifEncoderFinish(encoder, &avifOutput);
  438. if (isAvifError(result, "Could not finish encoding"))
  439. goto cleanup;
  440. // Write the AVIF image bytes to the GD ctx.
  441. gdPutBuf(avifOutput.data, avifOutput.size, outfile);
  442. cleanup:
  443. if (rgb.pixels)
  444. avifRGBImageFreePixels(&rgb);
  445. if (encoder)
  446. avifEncoderDestroy(encoder);
  447. if (avifOutput.data)
  448. avifRWDataFree(&avifOutput);
  449. if (avifIm)
  450. avifImageDestroy(avifIm);
  451. }
  452. void gdImageAvifEx(gdImagePtr im, FILE *outFile, int quality, int speed)
  453. {
  454. gdIOCtx *out = gdNewFileCtx(outFile);
  455. if (out != NULL) {
  456. gdImageAvifCtx(im, out, quality, speed);
  457. out->gd_free(out);
  458. }
  459. }
  460. void gdImageAvif(gdImagePtr im, FILE *outFile)
  461. {
  462. gdImageAvifEx(im, outFile, QUALITY_DEFAULT, SPEED_DEFAULT);
  463. }
  464. void * gdImageAvifPtrEx(gdImagePtr im, int *size, int quality, int speed)
  465. {
  466. void *rv;
  467. gdIOCtx *out = gdNewDynamicCtx(NEW_DYNAMIC_CTX_SIZE, NULL);
  468. if (out == NULL) {
  469. return NULL;
  470. }
  471. gdImageAvifCtx(im, out, quality, speed);
  472. rv = gdDPExtractData(out, size);
  473. out->gd_free(out);
  474. return rv;
  475. }
  476. void * gdImageAvifPtr(gdImagePtr im, int *size)
  477. {
  478. return gdImageAvifPtrEx(im, size, QUALITY_DEFAULT, AVIF_SPEED_DEFAULT);
  479. }
  480. #endif /* HAVE_LIBAVIF */