Table of Contents

libavif

Examples

 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;
 }
 ```

 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;
 }
 ```

 # 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")
 ```

Summary