Renamed 02_evas_colorspace_convert to 02_evas_colorspace_convert.diff
[maemo-efl] / trunk / evas / debian / patches / 02_evas_colorspace_convert.diff
1 diff --git a/src/lib/Evas.h b/src/lib/Evas.h
2 index 18864ce..a56fd92 100644
3 --- a/src/lib/Evas.h
4 +++ b/src/lib/Evas.h
5 @@ -478,6 +478,7 @@ extern "C" {
6     EAPI void              evas_object_image_size_get        (Evas_Object *obj, int *w, int *h);
7     EAPI int               evas_object_image_stride_get      (Evas_Object *obj);
8     EAPI int               evas_object_image_load_error_get  (Evas_Object *obj);
9 +   EAPI void             *evas_object_image_data_convert    (Evas_Object *obj, Evas_Colorspace to_cspace);
10     EAPI void              evas_object_image_data_set        (Evas_Object *obj, void *data);
11     EAPI void             *evas_object_image_data_get        (Evas_Object *obj, Evas_Bool for_writing);
12     EAPI void              evas_object_image_data_copy_set   (Evas_Object *obj, void *data);
13 diff --git a/src/lib/canvas/evas_object_image.c b/src/lib/canvas/evas_object_image.c
14 index 6a8df55..92b0651 100644
15 --- a/src/lib/canvas/evas_object_image.c
16 +++ b/src/lib/canvas/evas_object_image.c
17 @@ -69,6 +69,8 @@ static int evas_object_image_is_opaque(Evas_Object *obj);
18  static int evas_object_image_was_opaque(Evas_Object *obj);
19  static int evas_object_image_is_inside(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
20  
21 +static void *evas_object_image_data_convert_internal(Evas_Object_Image *o, void *data, Evas_Colorspace to_cspace);
22 +
23  static const Evas_Object_Func object_func =
24  {
25     /* methods (compulsory) */
26 @@ -708,6 +710,46 @@ evas_object_image_load_error_get(Evas_Object *obj)
27   */
28  
29  /**
30 + * Converts the raw image data of the given image object to the
31 + * specified colorspace.
32 + *
33 + * Note that this function does not modify the raw image data.
34 + * If the requested colorspace is the same as the image colorspace
35 + * nothing is done and NULL is returned. You should use
36 + * evas_object_image_colorspace_get() to check the current image
37 + * colorspace.
38 + *
39 + * See @ref evas_object_image_colorspace_get.
40 + *
41 + * @param obj The given image object.
42 + * @param to_cspace The colorspace to which the image raw data will be converted.
43 + * @return data A newly allocated data in the format specified by to_cspace.
44 + * @ingroup Evas_Object_Image_Data
45 + */
46 +EAPI void *
47 +evas_object_image_data_convert(Evas_Object *obj, Evas_Colorspace to_cspace)
48 +{
49 +   Evas_Object_Image *o;
50 +   DATA32 *data;
51 +
52 +   MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
53 +   return NULL;
54 +   MAGIC_CHECK_END();
55 +   o = (Evas_Object_Image *)(obj->object_data);
56 +   MAGIC_CHECK(o, Evas_Object_Image, MAGIC_OBJ_IMAGE);
57 +   return NULL;
58 +   MAGIC_CHECK_END();
59 +   if (!o->engine_data) return NULL;
60 +   if (!o->cur.cspace == to_cspace) return NULL;
61 +   data = NULL;
62 +   o->engine_data = obj->layer->evas->engine.func->image_data_get(obj->layer->evas->engine.data.output,
63 +                                                                 o->engine_data,
64 +                                                                 0,
65 +                                                                 &data);
66 +   return evas_object_image_data_convert_internal(o, data, to_cspace);
67 +}
68 +
69 +/**
70   * Sets the raw image data of the given image object.
71   *
72   * Note that the raw data must be of the same size and colorspace
73 @@ -1126,11 +1168,22 @@ evas_object_image_save(Evas_Object *obj, const char *file, const char *key, cons
74       {
75         if (o->cur.has_alpha) im->flags |= RGBA_IMAGE_HAS_ALPHA;
76  
77 -        im->image->data = data;
78 -        im->image->w = o->cur.image.w;
79 -        im->image->h = o->cur.image.h;
80 -        im->image->no_free = 1;
81 -        ok = evas_common_save_image_to_file(im, file, key, quality, compress);
82 +       if (o->cur.cspace == EVAS_COLORSPACE_ARGB8888)
83 +         im->image->data = data;
84 +       else
85 +         im->image->data = evas_object_image_data_convert_internal(o,
86 +                                                                   data,
87 +                                                                   EVAS_COLORSPACE_ARGB8888);
88 +       if (im->image->data)
89 +         {
90 +            im->image->w = o->cur.image.w;
91 +            im->image->h = o->cur.image.h;
92 +            im->image->no_free = 1;
93 +            ok = evas_common_save_image_to_file(im, file, key, quality, compress);
94 +
95 +            if (o->cur.cspace != EVAS_COLORSPACE_ARGB8888)
96 +              free(im->image->data);
97 +         }
98  
99         evas_cache_image_drop(im);
100       }
101 @@ -2376,3 +2429,36 @@ evas_object_image_is_inside(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
102  
103     return (a != 0);
104  }
105 +
106 +static void *
107 +evas_object_image_data_convert_internal(Evas_Object_Image *o, void *data, Evas_Colorspace to_cspace)
108 +{
109 +   void *out = NULL;
110 +
111 +   if (!data)
112 +     return NULL;
113 +
114 +   switch (o->cur.cspace)
115 +     {
116 +       case EVAS_COLORSPACE_ARGB8888:
117 +         out = evas_common_convert_argb8888_to(data,
118 +                                               o->cur.image.w,
119 +                                               o->cur.image.h,
120 +                                               o->cur.image.stride,
121 +                                               o->cur.has_alpha,
122 +                                               to_cspace);
123 +         break;
124 +       case EVAS_COLORSPACE_RGB565_A5P:
125 +         out = evas_common_convert_rgb565_a5p_to(data,
126 +                                                 o->cur.image.w,
127 +                                                 o->cur.image.h,
128 +                                                 o->cur.image.stride,
129 +                                                 o->cur.has_alpha,
130 +                                                 to_cspace);
131 +         break;
132 +       default:
133 +         break;
134 +     }
135 +
136 +   return out;
137 +}
138 diff --git a/src/lib/engines/common/Makefile.am b/src/lib/engines/common/Makefile.am
139 index c15675b..3d5fd7c 100644
140 --- a/src/lib/engines/common/Makefile.am
141 +++ b/src/lib/engines/common/Makefile.am
142 @@ -21,6 +21,7 @@ evas_op_mul_main_.c \
143  evas_blend_main.c \
144  evas_blit_main.c \
145  evas_convert_color.c \
146 +evas_convert_colorspace.c \
147  evas_convert_gry_1.c \
148  evas_convert_gry_4.c \
149  evas_convert_gry_8.c \
150 diff --git a/src/lib/engines/common/evas_convert_colorspace.c b/src/lib/engines/common/evas_convert_colorspace.c
151 new file mode 100644
152 index 0000000..287bd2e
153 --- /dev/null
154 +++ b/src/lib/engines/common/evas_convert_colorspace.c
155 @@ -0,0 +1,69 @@
156 +#include "evas_common.h"
157 +
158 +#define CONVERT_RGB_565_TO_RGB_888(s) \
159 +       (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) | \
160 +        ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) | \
161 +        ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)))
162 +
163 +#define CONVERT_A5P_TO_A8(s) \
164 +       ((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7))
165 +
166 +static inline void *
167 +evas_common_convert_argb8888_to_rgb565_a5p(void *data, int w, int h, int stride, Evas_Bool has_alpha)
168 +{
169 +}
170 +
171 +static inline void *
172 +evas_common_convert_rgb565_a5p_to_argb8888(void *data, int w, int h, int stride, Evas_Bool has_alpha)
173 +{
174 +   DATA16 *src, *end;
175 +   DATA32 *ret, *dst;
176 +   int r, g, b;
177 +
178 +   src = data;
179 +   end = src + (stride * h);
180 +   ret = malloc(w * h * sizeof(DATA32));
181 +
182 +   dst = ret;
183 +   if (has_alpha)
184 +     {
185 +       DATA8 *alpha;
186 +
187 +       alpha = end;
188 +       for (; src < end; src++, alpha++, dst++)
189 +         *dst = (CONVERT_A5P_TO_A8(*alpha) << 24) |
190 +                 CONVERT_RGB_565_TO_RGB_888(*src);
191 +     }
192 +   else
193 +     {
194 +       for (; src < end; src++, dst++)
195 +         *dst = CONVERT_RGB_565_TO_RGB_888(*src);
196 +     }
197 +   return ret;
198 +}
199 +
200 +EAPI void *
201 +evas_common_convert_argb8888_to(void *data, int w, int h, int stride, Evas_Bool has_alpha, Evas_Colorspace cspace)
202 +{
203 +   switch (cspace)
204 +     {
205 +       case EVAS_COLORSPACE_RGB565_A5P:
206 +         return evas_common_convert_argb8888_to_rgb565_a5p(data, w, h, stride, has_alpha);
207 +       default:
208 +         break;
209 +     }
210 +   return NULL;
211 +}
212 +
213 +EAPI void *
214 +evas_common_convert_rgb565_a5p_to(void *data, int w, int h, int stride, Evas_Bool has_alpha, Evas_Colorspace cspace)
215 +{
216 +   switch (cspace)
217 +     {
218 +       case EVAS_COLORSPACE_ARGB8888:
219 +         return evas_common_convert_rgb565_a5p_to_argb8888(data, w, h, stride, has_alpha);
220 +       default:
221 +         break;
222 +     }
223 +   return NULL;
224 +}
225 diff --git a/src/lib/include/evas_common.h b/src/lib/include/evas_common.h
226 index a61ed6d..4c724b1 100644
227 --- a/src/lib/include/evas_common.h
228 +++ b/src/lib/include/evas_common.h
229 @@ -998,6 +998,9 @@ EAPI void evas_common_convert_color_rgb_to_hsv                     (int r, int g
230  EAPI void evas_common_convert_color_hsv_to_rgb_int                 (int h, int s, int v, int *r, int *g, int *b);
231  EAPI void evas_common_convert_color_rgb_to_hsv_int                 (int r, int g, int b, int *h, int *s, int *v);
232  
233 +EAPI void *evas_common_convert_argb8888_to                         (void *data, int w, int h, int stride, Evas_Bool has_alpha, Evas_Colorspace cspace);
234 +EAPI void *evas_common_convert_rgb565_a5p_to                       (void *data, int w, int h, int stride, Evas_Bool has_alpha, Evas_Colorspace cspace);
235 +
236  /****/
237  EAPI void evas_common_scale_init                            (void);
238