summaryrefslogtreecommitdiff
path: root/src/modules/evas/image_loaders/webp/evas_image_load_webp.c
blob: 8026e0c88006b1070c54f36df22553ff17bf7b60 (plain)
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
340
341
342
343
344
345
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <webp/decode.h>
#include <webp/demux.h>

#include "evas_common_private.h"
#include "evas_private.h"

typedef struct _Loader_Info
{
   Eina_File *f;
   Evas_Image_Load_Opts *opts;
   Evas_Image_Animated *animated;
   WebPAnimDecoder *dec;
   void *map;
   Eina_Array *frames;
}Loader_Info;

// WebP Frame Information
typedef struct _Image_Frame
{
   int index;
   int timestamp;
   double delay;
   uint8_t *data;
}Image_Frame;

static Eina_Bool
evas_image_load_file_check(Eina_File *f, void *map,
			   unsigned int *w, unsigned int *h, Eina_Bool *alpha,
			   int *error)
{
   WebPDecoderConfig config;

   if (eina_file_size_get(f) < 30) return EINA_FALSE;

   if (!WebPInitDecoderConfig(&config))
   {
      *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
      return EINA_FALSE;
   }
   if (WebPGetFeatures(map, 30, &config.input) != VP8_STATUS_OK)
   {
      *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
      return EINA_FALSE;
   }

   *w = config.input.width;
   *h = config.input.height;
   *alpha = config.input.has_alpha;

   return EINA_TRUE;
}

static void *
evas_image_load_file_open_webp(Eina_File *f, Eina_Stringshare *key EINA_UNUSED,
			       Evas_Image_Load_Opts *opts,
			       Evas_Image_Animated *animated,
			       int *error)
{
   Loader_Info *loader = calloc(1, sizeof (Loader_Info));
   if (!loader)
     {
        *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
        return NULL;
     }
   loader->f = eina_file_dup(f);
   loader->opts = opts;
   loader->animated = animated;
   return loader;
}

static void
_free_all_frame(Loader_Info *loader)
{
   Image_Frame *frame;

   if (!loader->frames) return;

   for (unsigned int i = 0; i < eina_array_count(loader->frames); ++i)
     {
        frame = eina_array_data_get(loader->frames, i);
        if (frame->data)
          {
             free(frame->data);
             frame->data = NULL;
          }
        free(frame);
     }
}


static void
evas_image_load_file_close_webp(void *loader_data)
{
   // Free Allocated Data
   Loader_Info *loader = loader_data;
   _free_all_frame(loader);
   eina_array_free(loader->frames);
   if (loader->dec) WebPAnimDecoderDelete(loader->dec);
   if ((loader->map) && (loader->f))
     eina_file_map_free(loader->f, loader->map);
   if (loader->f) eina_file_close(loader->f);
   free(loader);
}


static void
_new_frame(Loader_Info *loader, uint8_t *data, int width, int height, int index,
           int pre_timestamp, int cur_timestamp)
{
   // Allocate Frame Data
   Image_Frame *frame;

   frame = calloc(1, sizeof(Image_Frame));
   if (!frame) return;

   frame->data = calloc(width * height * 4, sizeof(uint8_t));
   if (!frame->data)
     {
        free(frame);
        return;
     }

   frame->index = index;
   frame->timestamp = cur_timestamp;
   frame->delay = ((double)(cur_timestamp - pre_timestamp)/1000.0);
   memcpy(frame->data, data, width * height * 4);

   eina_array_push(loader->frames, frame);
}

static Image_Frame *
_find_frame(Loader_Info *loader, int index)
{
   // Find Frame
   Image_Frame *frame;

   if (!loader->frames) return NULL;

   frame = eina_array_data_get(loader->frames, index - 1);
   if (frame->index == index)
     return frame;

   return NULL;
}

static Eina_Bool
evas_image_load_file_head_webp(void *loader_data,
			       Emile_Image_Property *prop,
			       int *error)
{
   Loader_Info *loader = loader_data;
   Evas_Image_Animated *animated = loader->animated;
   Eina_File *f = loader->f;
   void *data;

   *error = EVAS_LOAD_ERROR_NONE;

   data = eina_file_map_all(f, EINA_FILE_RANDOM);
   loader->map = data;

   if (!evas_image_load_file_check(f, data,
				  &prop->w, &prop->h, &prop->alpha,
				  error))
     {
        ERR("Image File is Invalid");
        *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
        return EINA_FALSE;
     }

   // Init WebP Data
   WebPData webp_data;
   WebPDataInit(&webp_data);

   // Assign Data
   webp_data.bytes = data;
   webp_data.size = eina_file_size_get(f);

   // Set Decode Option
   WebPAnimDecoderOptions dec_options;
   WebPAnimDecoderOptionsInit(&dec_options);
   dec_options.color_mode = MODE_BGRA;

   // Create WebPAnimation Decoder
   WebPAnimDecoder *dec = WebPAnimDecoderNew(&webp_data, &dec_options);
   if (!dec)
     {
        ERR("WebP Decoder Creation is Failed");
        *error = EVAS_LOAD_ERROR_GENERIC;
        return EINA_FALSE;
     }
   loader->dec = dec;

   // Get WebP Animation Info
   WebPAnimInfo anim_info;
   if (!WebPAnimDecoderGetInfo(dec, &anim_info))
     {
        ERR("Getting WebP Information is Failed");
        *error = EVAS_LOAD_ERROR_GENERIC;
        return EINA_FALSE;
     }

   uint8_t* buf;
   int pre_timestamp = 0;
   int cur_timestamp = 0;
   int index = 1;

   // Set Frame Array
   loader->frames = eina_array_new(anim_info.frame_count);
   if (!loader->frames)
     {
        ERR("Frame Array Allocation is Faild");
        *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
        return EINA_FALSE;
     }

   // Decode Frames
   while (WebPAnimDecoderHasMoreFrames(dec))
     {
        if (!WebPAnimDecoderGetNext(dec, &buf, &cur_timestamp))
          {
             ERR("WebP Decoded Frame Get is Failed");
             *error = EVAS_LOAD_ERROR_GENERIC;
             return EINA_FALSE;
          }
        _new_frame(loader, buf, anim_info.canvas_width, anim_info.canvas_height, index,
                   pre_timestamp, cur_timestamp);
        pre_timestamp = cur_timestamp;
        index++;
     }

   // Set Animation Info
   if (anim_info.frame_count > 1)
     {
        animated->animated = 1;
        animated->loop_count = anim_info.loop_count;
        animated->loop_hint = EVAS_IMAGE_ANIMATED_HINT_LOOP;
        animated->frame_count = anim_info.frame_count;
     }

   return EINA_TRUE;
}

static Eina_Bool
evas_image_load_file_data_webp(void *loader_data,
			       Emile_Image_Property *prop,
			       void *pixels,
			       int *error)
{
   Loader_Info *loader = loader_data;
   Evas_Image_Animated *animated = loader->animated;

   *error = EVAS_LOAD_ERROR_NONE;

   void *surface = NULL;
   int width, height;
   int index = 0;

   index = animated->cur_frame;

   // Find Cur Frame
   if (index == 0)
     index = 1;
   Image_Frame *frame = _find_frame(loader, index);
   if (frame == NULL) return EINA_FALSE;

   WebPAnimInfo anim_info;
   WebPAnimDecoderGetInfo(loader->dec, &anim_info);
   width = anim_info.canvas_width;
   height = anim_info.canvas_height;

   // Render Frame
   surface = pixels;
   memcpy(surface, frame->data, width * height * 4);
   prop->premul = EINA_TRUE;

   return EINA_TRUE;
}

static double
evas_image_load_frame_duration_webp(void *loader_data,
                                    int start_frame,
                                    int frame_num)
{
   Loader_Info *loader = loader_data;
   Evas_Image_Animated *animated = loader->animated;

   if (!animated->animated) return -1.0;
   if (frame_num < 0) return -1.0;
   if (start_frame < 1) return -1.0;

   // Calculate Duration of Current Frame
   Image_Frame *frame = _find_frame(loader, start_frame);
   if (frame == NULL) return -1.0;

   return frame->delay;
}

static Evas_Image_Load_Func evas_image_load_webp_func =
{
  EVAS_IMAGE_LOAD_VERSION,
  evas_image_load_file_open_webp,
  evas_image_load_file_close_webp,
  (void*) evas_image_load_file_head_webp,
  NULL,
  (void*) evas_image_load_file_data_webp,
  evas_image_load_frame_duration_webp,
  EINA_TRUE,
  EINA_FALSE
};

static int
module_open(Evas_Module *em)
{
   if (!em) return 0;
   em->functions = (void *)(&evas_image_load_webp_func);
   return 1;
}

static void
module_close(Evas_Module *em EINA_UNUSED)
{
}

static Evas_Module_Api evas_modapi =
{
   EVAS_MODULE_API_VERSION,
   "webp",
   "none",
   {
     module_open,
     module_close
   }
};

EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, webp);

#ifndef EVAS_STATIC_BUILD_WEBP
EVAS_EINA_MODULE_DEFINE(image_loader, webp);
#endif