aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/evas/image_loaders/webp/evas_image_load_webp.c
blob: e3f97bb77a29272d9883da6ccfd00ccdeb1a5624 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

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

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

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 EINA_UNUSED,
			       Evas_Image_Animated *animated EINA_UNUSED,
			       int *error EINA_UNUSED)
{
   return f;
}

static void
evas_image_load_file_close_webp(void *loader_data EINA_UNUSED)
{
}

static Eina_Bool
evas_image_load_file_head_webp(void *loader_data,
			       Evas_Image_Property *prop,
			       int *error)
{
   Eina_File *f = loader_data;
   Eina_Bool r;
   void *data;

   *error = EVAS_LOAD_ERROR_NONE;

   data = eina_file_map_all(f, EINA_FILE_RANDOM);

   r = evas_image_load_file_check(f, data,
				  &prop->w, &prop->h, &prop->alpha,
				  error);

   if (data) eina_file_map_free(f, data);
   return r;
}

static Eina_Bool
evas_image_load_file_data_webp(void *loader_data,
			       Evas_Image_Property *prop,
			       void *pixels,
			       int *error)
{
   Eina_File *f = loader_data;
   void *data = NULL;
   void *decoded = NULL;
   void *surface = NULL;
   int width, height;

   data = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);

   surface = pixels;

   decoded = WebPDecodeBGRA(data, eina_file_size_get(f), &width, &height);
   if (!decoded)
     {
        *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
        goto free_data;
     }
   *error = EVAS_LOAD_ERROR_NONE;

   if ((int) prop->w != width ||
       (int) prop->h != height)
     goto free_data;

   // XXX: this copy of the surface is inefficient
   memcpy(surface, decoded, width * height * 4);
   prop->premul = EINA_TRUE;

 free_data:
   if (data) eina_file_map_free(f, data);
   free(decoded);

   return EINA_TRUE;
}

static Evas_Image_Load_Func evas_image_load_webp_func =
{
  evas_image_load_file_open_webp,
  evas_image_load_file_close_webp,
  evas_image_load_file_head_webp,
  evas_image_load_file_data_webp,
  NULL,
  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