* '''Encoder''': Functions to encode images into the AVIF format. * '''Decoder''': Functions to decode AVIF images back into standard image formats. * '''Utilities''': Additional tools for manipulating and processing AVIF images.
* '''High Compression Efficiency''': Utilizes the AV1 codec to achieve high compression ratios while maintaining image quality. * '''Support for HDR''': Supports High Dynamic Range (HDR) imaging. * '''Lossy and Lossless Compression''': Offers both lossy and lossless compression options. * '''Metadata Handling''': Supports embedding and extraction of image metadata.
```c #include#include
int main() { avifRWData raw = AVIF_DATA_EMPTY; avifImage *image = avifImageCreate(800, 600, 8, AVIF_PIXEL_FORMAT_YUV444); // Populate the image with pixel data // ...
avifEncoder *encoder = avifEncoderCreate(); avifResult result = avifEncoderWrite(encoder, image, &raw); if (result == AVIF_RESULT_OK) { FILE *f = fopen("output.avif", "wb"); fwrite(raw.data, 1, raw.size, f); fclose(f); } else { fprintf(stderr, "Failed to encode image: %s\n", avifResultToString(result)); }
avifRWDataFree(&raw); avifEncoderDestroy(encoder); avifImageDestroy(image); return 0; } ```
```c #include#include
int main() { avifRWData raw = AVIF_DATA_EMPTY; FILE *f = fopen("input.avif", "rb"); fseek(f, 0, SEEK_END); raw.size = ftell(f); fseek(f, 0, SEEK_SET); raw.data = (uint8_t *)malloc(raw.size); fread(raw.data, 1, raw.size, f); fclose(f);
avifImage *image = avifImageCreateEmpty(); avifDecoder *decoder = avifDecoderCreate(); avifResult result = avifDecoderRead(decoder, image, &raw); if (result == AVIF_RESULT_OK) { printf("Image decoded successfully\n"); // Process the image } else { fprintf(stderr, "Failed to decode image: %s\n", avifResultToString(result)); }
avifRWDataFree(&raw); avifDecoderDestroy(decoder); avifImageDestroy(image); return 0; } ```
```python import libavif
# Load an image and encode it to AVIF image = libavif.Image(width=800, height=600, depth=8) # Populate the image with pixel data # ...
encoded_data = image.encode() with open("output.avif", "wb") as f: f.write(encoded_data)
# Decode an AVIF image with open("input.avif", "rb") as f: encoded_data = f.read()
decoded_image = libavif.Image.decode(encoded_data) print("Image decoded successfully") ```