Blender V5.3
format_avif.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include <fmt/format.h>
12
14
15#include "MEM_guardedalloc.h"
16
18#include "IMB_filetype.hh"
19#include "IMB_imbuf.hh"
20#include "IMB_imbuf_types.hh"
21
22namespace blender {
23
24const char *imb_file_extensions_avif[] = {".avif", nullptr};
25
26OIIO_NAMESPACE_USING
27using namespace imbuf;
28
29bool imb_is_a_avif(const uchar *mem, size_t size)
30{
31 return imb_oiio_check(mem, size, "heif");
32}
33
35 size_t size,
36 ImBufFlags flags,
37 ImFileColorSpace &r_colorspace)
38{
39 ImageSpec config, spec;
40 config.attribute("oiio:UnassociatedAlpha", 1);
41
42 ReadContext ctx{.mem_start = mem,
43 .mem_size = size,
44 .file_format = "heif",
45 .file_type = IMB_FTYPE_AVIF,
46 .flags = flags};
47
48 ImBuf *ibuf = imb_oiio_read(ctx, config, r_colorspace, spec);
49 if (ibuf) {
50 const int bits_per_sample = spec.get_int_attribute("oiio:BitsPerSample", 8);
51 if (bits_per_sample == 10) {
52 ibuf->foptions.flag |= AVIF_10BIT;
53 }
54 else if (bits_per_sample == 12) {
55 ibuf->foptions.flag |= AVIF_12BIT;
56 }
57 }
58
59 /* Assume SDR by default, CICP will indicate if it's HDR and set a colorspace. */
60 r_colorspace.is_hdr_float = false;
61
62 return ibuf;
63}
64
66static constexpr int AVIF_BLOCK_SIZE = 64;
67
77 WriteContext &ctx,
78 const bool prefer_float)
79{
80 /* The bug only affects the 8-bit path (the 10/12-bit path copies per-pixel). */
81 const bool use_float = prefer_float && (ibuf->float_data() != nullptr);
82 if (use_float || (ibuf->x % AVIF_BLOCK_SIZE) == 0) {
83 return nullptr;
84 }
85
86 const size_t size_orig = size_t(ibuf->y) * (-ctx.mem_span.ystride());
87 const size_t size_pad = size_orig + (AVIF_BLOCK_SIZE * ctx.mem_span.xstride());
88 const uchar *src_base = ibuf->byte_data();
89
90 uchar *buf_padded = MEM_new_array_uninitialized<uchar>(size_pad, __func__);
91 memcpy(buf_padded, src_base, size_orig);
92
93 /* `mem_start` points to the last row (images are stored bottom-to-top). */
94 const size_t y_flip_offset = size_t(ibuf->y - 1) * (-ctx.mem_span.ystride());
95 ctx.mem_span = image_span<const std::byte>(
96 reinterpret_cast<const std::byte *>(buf_padded + y_flip_offset),
97 ctx.mem_span.nchannels(),
98 ctx.mem_span.width(),
99 ctx.mem_span.height(),
100 ctx.mem_span.depth(),
101 ctx.mem_span.chanstride(),
102 ctx.mem_span.xstride(),
103 ctx.mem_span.ystride(),
104 ctx.mem_span.zstride(),
105 ctx.mem_span.chansize());
106
107 return buf_padded;
108}
109
110static void imb_save_avif_padding_workaround_end(const uchar *buf_padded)
111{
112 /* May be null. */
113 MEM_delete(buf_padded);
114}
115
116static std::tuple<WriteContext, ImageSpec, bool> prepare_save_avif(ImBuf *ibuf, ImBufFlags flags)
117{
118 const int bits_per_sample = (ibuf->foptions.flag & AVIF_10BIT) ? 10 :
119 (ibuf->foptions.flag & AVIF_12BIT) ? 12 :
120 8;
121 const bool use_float = bits_per_sample > 8;
122 int file_channels = ibuf->color_mode_channels_get();
123 /* AVIF/HEIF does not support 2-channel (gray + alpha) writes; promote to RGBA. */
124 if (file_channels == 2) {
125 file_channels = 4;
126 }
127 const TypeDesc data_format = use_float ? TypeDesc::UINT16 : TypeDesc::UINT8;
128
129 WriteContext ctx = imb_create_write_context("heif", ibuf, flags, use_float);
130 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
131
132 /* Skip if the float buffer was managed already. */
133 if (use_float && (ibuf->float_buffer.colorspace || ibuf->colorspace_is_data())) {
134 file_spec.attribute("oiio:UnassociatedAlpha", 0);
135 }
136 else {
137 file_spec.attribute("oiio:UnassociatedAlpha", 1);
138 }
139
140 file_spec.attribute("Compression", fmt::format("avif:{}", int(ibuf->foptions.quality)));
141 file_spec.attribute("oiio:BitsPerSample", bits_per_sample);
142
143 return {ctx, file_spec, use_float};
144}
145
146bool imb_save_avif(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
147{
148 auto [ctx, file_spec, use_float] = prepare_save_avif(ibuf, flags);
149 const uchar *buf_padded = nullptr;
150 if (OIIO_VERSION_LESS(3, 1, 12)) {
151 buf_padded = imb_save_avif_padding_workaround_begin(ibuf, ctx, use_float);
152 }
153 bool result = imb_oiio_write(ctx, filepath, file_spec);
155 return result;
156}
157
159{
160 auto [ctx, file_spec, use_float] = prepare_save_avif(ibuf, flags);
161 const uchar *buf_padded = nullptr;
162 if (OIIO_VERSION_LESS(3, 1, 12)) {
163 buf_padded = imb_save_avif_padding_workaround_begin(ibuf, ctx, use_float);
164 }
167 return result;
168}
169
170}; // namespace blender
#define AVIF_12BIT
#define AVIF_10BIT
Read Blender memory allocation and freeing.
bool imb_oiio_write(const WriteContext &ctx, const char *filepath, const ImageSpec &file_spec)
ImBuf * imb_oiio_read(const ReadContext &ctx, const ImageSpec &config, ImFileColorSpace &r_colorspace, ImageSpec &r_newspec)
bool imb_oiio_check(const uchar *mem, size_t mem_size, const char *file_format)
ImageSpec imb_create_write_spec(const WriteContext &ctx, int file_channels, TypeDesc data_format)
WriteContext imb_create_write_context(const char *file_format, const ImBuf *ibuf, ImBufFlags flags, bool prefer_float)
Vector< uint8_t > imb_oiio_write_buffer(const WriteContext &ctx, const ImageSpec &file_spec)
BMesh Iterators.
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void imb_save_avif_padding_workaround_end(const uchar *buf_padded)
ImBuf * imb_load_avif(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
bool imb_is_a_avif(const uchar *mem, size_t size)
const char * imb_file_extensions_avif[]
unsigned char uchar
Definition BKE_text.h:58
static constexpr int AVIF_BLOCK_SIZE
bool imb_save_avif(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Vector< uint8_t > imb_save_buffer_avif(ImBuf *ibuf, ImBufFlags flags)
static uchar * imb_save_avif_padding_workaround_begin(const ImBuf *ibuf, WriteContext &ctx, const bool prefer_float)
static std::tuple< WriteContext, ImageSpec, bool > prepare_save_avif(ImBuf *ibuf, ImBufFlags flags)
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
const uint8_t * byte_data() const
bool colorspace_is_data() const
const float * float_data() const
int color_mode_channels_get() const
OIIO::image_span< const std::byte > mem_span