-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMG_avif.c
339 lines (288 loc) · 8.89 KB
/
IMG_avif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* This is a AVIF image file loading framework */
#include "SDL_image.h"
#ifdef LOAD_AVIF
#include <avif/avif.h>
#include <limits.h> /* for INT_MAX */
static struct {
int loaded;
void *handle;
avifBool (*avifPeekCompatibleFileType)(const avifROData * input);
avifDecoder * (*avifDecoderCreate)(void);
void (*avifDecoderDestroy)(avifDecoder * decoder);
void (*avifDecoderSetIO)(avifDecoder * decoder, avifIO * io);
avifResult (*avifDecoderParse)(avifDecoder * decoder);
avifResult (*avifDecoderNextImage)(avifDecoder * decoder);
avifResult (*avifImageYUVToRGB)(const avifImage * image, avifRGBImage * rgb);
} lib;
#ifdef LOAD_AVIF_DYNAMIC
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = FUNC; \
if (lib.FUNC == NULL) { IMG_SetError("Missing avif.framework"); return -1; }
#endif
int IMG_InitAVIF()
{
if ( lib.loaded == 0 ) {
#ifdef LOAD_AVIF_DYNAMIC
lib.handle = SDL_LoadObject(LOAD_AVIF_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
#endif
FUNCTION_LOADER(avifPeekCompatibleFileType, avifBool (*)(const avifROData * input))
FUNCTION_LOADER(avifDecoderCreate, avifDecoder *(*)(void))
FUNCTION_LOADER(avifDecoderDestroy, void (*)(avifDecoder * decoder))
FUNCTION_LOADER(avifDecoderSetIO, void (*)(avifDecoder * decoder, avifIO * io))
FUNCTION_LOADER(avifDecoderParse, avifResult (*)(avifDecoder * decoder))
FUNCTION_LOADER(avifDecoderNextImage, avifResult (*)(avifDecoder * decoder))
FUNCTION_LOADER(avifImageYUVToRGB, avifResult (*)(const avifImage * image, avifRGBImage * rgb))
}
++lib.loaded;
return 0;
}
void IMG_QuitAVIF()
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
#ifdef LOAD_AVIF_DYNAMIC
SDL_UnloadObject(lib.handle);
#endif
}
--lib.loaded;
}
static SDL_bool ReadAVIFHeader(SDL_RWops *src, Uint8 **header_data, size_t *header_size)
{
Uint8 magic[16];
Uint64 size;
size_t read = 0;
Uint8 *data;
*header_data = NULL;
*header_size = 0;
if (!SDL_RWread(src, magic, 8, 1)) {
return SDL_FALSE;
}
read += 8;
if (SDL_memcmp(&magic[4], "ftyp", 4) != 0) {
return SDL_FALSE;
}
size = (((size_t)magic[0] << 24) |
((size_t)magic[1] << 16) |
((size_t)magic[2] << 8) |
((size_t)magic[3] << 0));
if (size == 1) {
/* 64-bit header size */
if (!SDL_RWread(src, &magic[8], 8, 1)) {
return SDL_FALSE;
}
read += 8;
size = (((Uint64)magic[8] << 56) |
((Uint64)magic[9] << 48) |
((Uint64)magic[10] << 40) |
((Uint64)magic[11] << 32) |
((Uint64)magic[12] << 24) |
((Uint64)magic[13] << 16) |
((Uint64)magic[14] << 8) |
((Uint64)magic[15] << 0));
}
if (size > INT_MAX) {
return SDL_FALSE;
}
if (size <= read) {
return SDL_FALSE;
}
/* Read in the header */
data = (Uint8 *)SDL_malloc(size);
if (!data) {
return SDL_FALSE;
}
SDL_memcpy(data, magic, read);
if (!SDL_RWread(src, &data[read], (size - read), 1)) {
SDL_free(data);
return SDL_FALSE;
}
*header_data = data;
*header_size = (size_t)size;
return SDL_TRUE;
}
/* See if an image is contained in a data source */
int IMG_isAVIF(SDL_RWops *src)
{
Sint64 start;
int is_AVIF;
Uint8 *data;
size_t size;
if ( !src )
return 0;
start = SDL_RWtell(src);
is_AVIF = 0;
if (ReadAVIFHeader(src, &data, &size)) {
/* This might be AVIF, do more thorough checks */
if ((IMG_Init(IMG_INIT_AVIF) & IMG_INIT_AVIF) != 0) {
avifROData header;
header.data = data;
header.size = size;
is_AVIF = lib.avifPeekCompatibleFileType(&header);
}
SDL_free(data);
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_AVIF);
}
/* Context for AFIF I/O operations */
typedef struct
{
SDL_RWops *src;
Uint64 start;
uint8_t *data;
size_t size;
} avifIOContext;
static avifResult ReadAVIFIO(struct avifIO * io, uint32_t readFlags, uint64_t offset, size_t size, avifROData * out)
{
avifIOContext *context = (avifIOContext *)io->data;
/* The AVIF reader bounces all over, so always seek to the correct offset */
if (SDL_RWseek(context->src, context->start + offset, RW_SEEK_SET) < 0) {
return AVIF_RESULT_IO_ERROR;
}
if (size > context->size) {
uint8_t *data = (uint8_t *)SDL_realloc(context->data, size);
if (!data) {
return AVIF_RESULT_IO_ERROR;
}
context->data = data;
context->size = size;
}
out->data = context->data;
out->size = SDL_RWread(context->src, context->data, 1, size);
if (out->size == 0) {
return AVIF_RESULT_IO_ERROR;
}
return AVIF_RESULT_OK;
}
static void DestroyAVIFIO(struct avifIO * io)
{
avifIOContext *context = (avifIOContext *)io->data;
if (context->data) {
SDL_free(context->data);
context->data = NULL;
}
}
/* Load a AVIF type image from an SDL datasource */
SDL_Surface *IMG_LoadAVIF_RW(SDL_RWops *src)
{
Sint64 start;
avifDecoder *decoder = NULL;
avifIO io;
avifIOContext context;
avifRGBImage rgb;
avifResult result;
SDL_Surface *surface = NULL;
if (!src) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
if ((IMG_Init(IMG_INIT_AVIF) & IMG_INIT_AVIF) == 0) {
return NULL;
}
SDL_zero(context);
SDL_zero(io);
SDL_zero(rgb);
decoder = lib.avifDecoderCreate();
if (!decoder) {
IMG_SetError("Couldn't create AVIF decoder");
goto done;
}
/* Be permissive so we can load as many images as possible */
decoder->strictFlags = AVIF_STRICT_DISABLED;
context.src = src;
context.start = start;
io.destroy = DestroyAVIFIO;
io.read = ReadAVIFIO;
io.data = &context;
lib.avifDecoderSetIO(decoder, &io);
result = lib.avifDecoderParse(decoder);
if (result != AVIF_RESULT_OK) {
IMG_SetError("Couldn't parse AVIF image: %d", result);
goto done;
}
result = lib.avifDecoderNextImage(decoder);
if (result != AVIF_RESULT_OK) {
IMG_SetError("Couldn't get AVIF image: %d", result);
goto done;
}
surface = SDL_CreateRGBSurfaceWithFormat(0, decoder->image->width, decoder->image->height, 0, SDL_PIXELFORMAT_ARGB8888);
if (!surface) {
goto done;
}
/* Convert the YUV image to RGB */
rgb.width = surface->w;
rgb.height = surface->h;
rgb.depth = 8;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
rgb.format = AVIF_RGB_FORMAT_BGRA;
#else
rgb.format = AVIF_RGB_FORMAT_ARGB;
#endif
rgb.pixels = (uint8_t *)surface->pixels;
rgb.rowBytes = (uint32_t)surface->pitch;
result = lib.avifImageYUVToRGB(decoder->image, &rgb);
if (result != AVIF_RESULT_OK) {
IMG_SetError("Couldn't convert AVIF image to RGB: %d", result);
SDL_FreeSurface(surface);
surface = NULL;
goto done;
}
done:
if (decoder) {
lib.avifDecoderDestroy(decoder);
}
if (!surface) {
SDL_RWseek(src, start, RW_SEEK_SET);
}
return surface;
}
#else
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif
int IMG_InitAVIF()
{
IMG_SetError("AVIF images are not supported");
return(-1);
}
void IMG_QuitAVIF()
{
}
/* See if an image is contained in a data source */
int IMG_isAVIF(SDL_RWops *src)
{
return(0);
}
/* Load a AVIF type image from an SDL datasource */
SDL_Surface *IMG_LoadAVIF_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_AVIF */