aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/ephoto_color.c
blob: 36a0f20bedfaa82240f4d558f600eb537e46dbbe (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
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
#include "ephoto.h"

typedef struct _Ephoto_Color Ephoto_Color;
struct _Ephoto_Color
{
   Evas_Object  *main;
   Evas_Object  *parent;
   Evas_Object  *image;
   Evas_Object  *editor;
   Evas_Object  *bslider;
   Evas_Object  *gslider;
   Evas_Object  *rslider;
   Eina_List    *handlers;
   int           blue;
   int           green;
   int           red;
   Evas_Coord    w, h;
   unsigned int *original_im_data;
};

typedef enum _Ephoto_Color_Adjust Ephoto_Color_Adjust;
enum _Ephoto_Color_Adjust
{
   EPHOTO_COLOR_ADJUST_RED,
   EPHOTO_COLOR_ADJUST_GREEN,
   EPHOTO_COLOR_ADJUST_BLUE
};

unsigned int *
_ephoto_apply_color_adjustment(Ephoto_Color *eco, unsigned int *image_data, int adjust, Ephoto_Color_Adjust color)
{
   unsigned int *im_data, *im_data_new, *p1, *p2;
   Evas_Coord x, y;
   int a, r, g, b, cc;

   im_data = malloc(sizeof(unsigned int) * eco->w * eco->h);
   if (image_data)
     memcpy(im_data, image_data, sizeof(unsigned int) * eco->w * eco->h);
   else
     memcpy(im_data, eco->original_im_data,
            sizeof(unsigned int) * eco->w * eco->h);

   im_data_new = malloc(sizeof(unsigned int) * eco->w * eco->h);

   for (y = 0; y < eco->h; y++)
     {
        p1 = im_data + (y * eco->w);
        p2 = im_data_new + (y * eco->w);
        for (x = 0; x < eco->w; x++)
          {
             b = (int)((*p1) & 0xff);
             g = (int)((*p1 >> 8) & 0xff);
             r = (int)((*p1 >> 16) & 0xff);
             a = (int)((*p1 >> 24) & 0xff);
             b = ephoto_mul_color_alpha(b, a);
             g = ephoto_mul_color_alpha(g, a);
             r = ephoto_mul_color_alpha(r, a);
             switch (color)
               {
                case EPHOTO_COLOR_ADJUST_RED:
                  eco->red = adjust;
                  cc = (int)r + eco->red;
                  r = cc;
                  break;

                case EPHOTO_COLOR_ADJUST_BLUE:
                  eco->blue = adjust;
                  cc = (int)b + eco->blue;
                  b = cc;
                  break;

                case EPHOTO_COLOR_ADJUST_GREEN:
                  eco->green = adjust;
                  cc = (int)g + eco->green;
                  g = cc;
                  break;

                default:
                  break;
               }
             b = ephoto_normalize_color(b);
             g = ephoto_normalize_color(g);
             r = ephoto_normalize_color(r);
             b = ephoto_demul_color_alpha(b, a);
             g = ephoto_demul_color_alpha(g, a);
             r = ephoto_demul_color_alpha(r, a);
             *p2 = (a << 24) | (r << 16) | (g << 8) | b;
             p2++;
             p1++;
          }
     }
   ephoto_single_browser_image_data_update(eco->main, eco->image,
                                           im_data_new, eco->w, eco->h);
   free(im_data);
   return im_data_new;
}

static void
_red_slider_changed(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;
   int red;
   unsigned int *image_data, *image_data_two;

   red = elm_slider_value_get(obj);
   image_data = _ephoto_apply_color_adjustment(eco, NULL, red, EPHOTO_COLOR_ADJUST_RED);
   image_data_two = _ephoto_apply_color_adjustment(eco, image_data, eco->green, EPHOTO_COLOR_ADJUST_GREEN);
   _ephoto_apply_color_adjustment(eco, image_data_two, eco->blue, EPHOTO_COLOR_ADJUST_BLUE);
}

static void
_green_slider_changed(void *data, Evas_Object *obj,
                      void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;
   int green;
   unsigned int *image_data, *image_data_two;

   green = elm_slider_value_get(obj);
   image_data = _ephoto_apply_color_adjustment(eco, NULL, green, EPHOTO_COLOR_ADJUST_GREEN);
   image_data_two = _ephoto_apply_color_adjustment(eco, image_data, eco->red, EPHOTO_COLOR_ADJUST_RED);
   _ephoto_apply_color_adjustment(eco, image_data_two, eco->blue, EPHOTO_COLOR_ADJUST_BLUE);
}

static void
_blue_slider_changed(void *data, Evas_Object *obj,
                     void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;
   int blue;
   unsigned int *image_data, *image_data_two;

   blue = elm_slider_value_get(obj);
   image_data = _ephoto_apply_color_adjustment(eco, NULL, blue, EPHOTO_COLOR_ADJUST_BLUE);
   image_data_two = _ephoto_apply_color_adjustment(eco, image_data, eco->red, EPHOTO_COLOR_ADJUST_RED);
   _ephoto_apply_color_adjustment(eco, image_data_two, eco->green, EPHOTO_COLOR_ADJUST_GREEN);
}

static Eina_Bool
_color_reset(void *data, int type EINA_UNUSED,
             void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;

   elm_slider_value_set(eco->rslider, 0);
   elm_slider_value_set(eco->gslider, 0);
   elm_slider_value_set(eco->bslider, 0);
   eco->red = 0;
   eco->green = 0;
   eco->blue = 0;
   _red_slider_changed(eco, eco->rslider, NULL);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_color_apply(void *data, int type EINA_UNUSED,
             void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;
   unsigned int *image_data;
   Evas_Coord w, h;

   if (elm_slider_value_get(eco->rslider) == 0 &&
       elm_slider_value_get(eco->gslider) == 0 &&
       elm_slider_value_get(eco->bslider) == 0)
     {
        ephoto_single_browser_cancel_editing(eco->main);
     }
   else
     {
        image_data =
          evas_object_image_data_get(eco->image, EINA_FALSE);
        evas_object_image_size_get(eco->image, &w, &h);
        ephoto_single_browser_image_data_done(eco->main, image_data, w, h);
     }
   ephoto_editor_del(eco->editor, eco->parent);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_color_cancel(void *data, int type EINA_UNUSED,
              void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;

   elm_slider_value_set(eco->rslider, 0);
   elm_slider_value_set(eco->gslider, 0);
   elm_slider_value_set(eco->bslider, 0);
   eco->red = 0;
   eco->green = 0;
   eco->blue = 0;
   _red_slider_changed(eco, eco->rslider, NULL);
   ephoto_single_browser_cancel_editing(eco->main);
   ephoto_editor_del(eco->editor, eco->parent);

   return ECORE_CALLBACK_PASS_ON;
}

static void
_editor_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
            void *event_info EINA_UNUSED)
{
   Ephoto_Color *eco = data;
   Ecore_Event_Handler *handler;

   EINA_LIST_FREE(eco->handlers, handler)
     ecore_event_handler_del(handler);
   free(eco->original_im_data);
   free(eco);
}

void
ephoto_color_add(Ephoto *ephoto, Evas_Object *main, Evas_Object *parent, Evas_Object *image)
{
   Evas_Object *slider;
   Ephoto_Color *eco;
   unsigned int *im_data;

   EINA_SAFETY_ON_NULL_GOTO(image, error);

   eco = calloc(1, sizeof(Ephoto_Color));
   EINA_SAFETY_ON_NULL_GOTO(eco, error);

   eco->red = 0;
   eco->green = 0;
   eco->blue = 0;
   eco->main = main;
   eco->parent = parent;
   eco->image = image;
   im_data =
     evas_object_image_data_get(eco->image,
                                EINA_FALSE);
   evas_object_image_size_get(eco->image, &eco->w,
                              &eco->h);
   eco->original_im_data = malloc(sizeof(unsigned int) * eco->w * eco->h);
   memcpy(eco->original_im_data, im_data,
          sizeof(unsigned int) * eco->w * eco->h);

   eco->editor = ephoto_editor_add(ephoto, parent, _("Adjust Color Levels"),
                                   "eco", eco);
   evas_object_event_callback_add(eco->editor, EVAS_CALLBACK_DEL, _editor_del,
                                  eco);

   slider = elm_slider_add(eco->editor);
   elm_object_text_set(slider, _("Blue"));
   elm_slider_min_max_set(slider, -100, 100);
   elm_slider_step_set(slider, 1);
   elm_slider_value_set(slider, 0);
   elm_slider_unit_format_set(slider, "%1.0f");
   elm_slider_indicator_format_set(slider, "%1.0f");
   EPHOTO_WEIGHT(slider, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
   EPHOTO_ALIGN(slider, EVAS_HINT_FILL, 0.5);
   evas_object_smart_callback_add(slider, "delay,changed",
                                  _blue_slider_changed, eco);
   elm_box_pack_start(eco->editor, slider);
   evas_object_show(slider);
   eco->bslider = slider;

   slider = elm_slider_add(eco->editor);
   elm_object_text_set(slider, _("Green"));
   elm_slider_min_max_set(slider, -100, 100);
   elm_slider_step_set(slider, 1);
   elm_slider_value_set(slider, 0);
   elm_slider_unit_format_set(slider, "%1.0f");
   elm_slider_indicator_format_set(slider, "%1.0f");
   EPHOTO_WEIGHT(slider, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
   EPHOTO_ALIGN(slider, EVAS_HINT_FILL, 0.5);
   evas_object_smart_callback_add(slider, "delay,changed",
                                  _green_slider_changed, eco);
   elm_box_pack_start(eco->editor, slider);
   evas_object_show(slider);
   eco->gslider = slider;

   slider = elm_slider_add(eco->editor);
   elm_object_text_set(slider, _("Red"));
   elm_slider_min_max_set(slider, -100, 100);
   elm_slider_step_set(slider, 1);
   elm_slider_value_set(slider, 0);
   elm_slider_unit_format_set(slider, "%1.0f");
   elm_slider_indicator_format_set(slider, "%1.0f");
   EPHOTO_WEIGHT(slider, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
   EPHOTO_ALIGN(slider, EVAS_HINT_FILL, 0.5);
   evas_object_smart_callback_add(slider, "delay,changed",
                                  _red_slider_changed, eco);
   elm_box_pack_start(eco->editor, slider);
   evas_object_show(slider);
   eco->rslider = slider;

   eco->handlers =
     eina_list_append(eco->handlers,
                      ecore_event_handler_add(EPHOTO_EVENT_EDITOR_RESET,
                                              _color_reset, eco));
   eco->handlers =
     eina_list_append(eco->handlers,
                      ecore_event_handler_add(EPHOTO_EVENT_EDITOR_APPLY,
                                              _color_apply, eco));
   eco->handlers =
     eina_list_append(eco->handlers,
                      ecore_event_handler_add(EPHOTO_EVENT_EDITOR_CANCEL,
                                              _color_cancel, eco));

   return;

error:
   return;
}