Ephoto: Add auto histogram equalization filter. Clean up some code.

v-1.6.0
Stephen Houston 7 years ago
parent 76cfa1d52c
commit fba9a17b48
  1. 1
      src/bin/ephoto.h
  2. 140
      src/bin/ephoto_bcg.c
  3. 139
      src/bin/ephoto_filters.c
  4. 128
      src/bin/ephoto_hsv.c
  5. 25
      src/bin/ephoto_single_browser.c

@ -78,6 +78,7 @@ void ephoto_filter_blur(Evas_Object *main, Evas_Object *image);
void ephoto_filter_sharpen(Evas_Object *main, Evas_Object *image);
void ephoto_filter_black_and_white(Evas_Object *main, Evas_Object *image);
void ephoto_filter_old_photo(Evas_Object *main, Evas_Object *image);
void ephoto_filter_histogram_eq(Evas_Object *main, Evas_Object *image);
enum _Ephoto_State
{

@ -17,12 +17,41 @@ struct _Ephoto_BCG
unsigned int *original_im_data;
};
static int
_normalize_color(int color)
{
if (color < 0)
return 0;
else if (color > 255)
return 255;
else
return color;
}
static int
_mul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return (color * (255 / alpha));
else
return color;
}
static int
_demul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return ((color * alpha) / 255);
else
return color;
}
unsigned int *
_ephoto_bcg_adjust_brightness(Ephoto_BCG *ebcg, int brightness, unsigned int *image_data)
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
im_data = malloc(sizeof(unsigned int) * ebcg->w * ebcg->h);
if (image_data)
@ -43,28 +72,19 @@ _ephoto_bcg_adjust_brightness(Ephoto_BCG *ebcg, int brightness, unsigned int *im
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
nb = (int)b+ebcg->brightness;
ng = (int)g+ebcg->brightness;
nr = (int)r+ebcg->brightness;
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
bb = (int)b+ebcg->brightness;
gg = (int)g+ebcg->brightness;
rr = (int)r+ebcg->brightness;
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}
@ -79,7 +99,7 @@ _ephoto_bcg_adjust_contrast(Ephoto_BCG *ebcg, int contrast, unsigned int *image_
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h, top, bottom;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
float factor;
im_data = malloc(sizeof(unsigned int) * ebcg->w * ebcg->h);
@ -104,28 +124,19 @@ _ephoto_bcg_adjust_contrast(Ephoto_BCG *ebcg, int contrast, unsigned int *image_
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
nb = (int)((factor * (b - 128)) + 128);
ng = (int)((factor * (g - 128)) + 128);
nr = (int)((factor * (r - 128)) + 128);
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
bb = (int)((factor * (b - 128)) + 128);
gg = (int)((factor * (g - 128)) + 128);
rr = (int)((factor * (r - 128)) + 128);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}
@ -140,7 +151,7 @@ _ephoto_bcg_adjust_gamma(Ephoto_BCG *ebcg, double gamma, unsigned int *image_dat
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
im_data = malloc(sizeof(unsigned int) * ebcg->w * ebcg->h);
if (image_data)
@ -161,28 +172,19 @@ _ephoto_bcg_adjust_gamma(Ephoto_BCG *ebcg, double gamma, unsigned int *image_dat
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
nb = (int)(pow(((double)b/255), ebcg->gamma) * 255);
ng = (int)(pow(((double)g/255), ebcg->gamma) * 255);
nr = (int)(pow(((double)r/255), ebcg->gamma) * 255);
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
bb = (int)(pow(((double)b/255), ebcg->gamma) * 255);
gg = (int)(pow(((double)g/255), ebcg->gamma) * 255);
rr = (int)(pow(((double)r/255), ebcg->gamma) * 255);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}

@ -1,5 +1,34 @@
#include "ephoto.h"
static int
_normalize_color(int color)
{
if (color < 0)
return 0;
else if (color > 255)
return 255;
else
return color;
}
static int
_mul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return (color * (255 / alpha));
else
return color;
}
static int
_demul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return ((color * alpha) / 255);
else
return color;
}
void
ephoto_filter_blur(Evas_Object *main, Evas_Object *image)
{
@ -168,12 +197,9 @@ ephoto_filter_black_and_white(Evas_Object *main, Evas_Object *image)
g = (int)((im_data[i] >> 8) & 0xff);
r = (int)((im_data[i] >> 16) & 0xff);
a = (int)((im_data[i] >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
gray = (int)((0.3 * r) + (0.59 * g) + (0.11 * b));
if (a >= 0 && a < 255)
gray = (gray * a) / 255;
@ -197,34 +223,89 @@ ephoto_filter_old_photo(Evas_Object *main, Evas_Object *image)
g = (int)((im_data[i] >> 8) & 0xff);
r = (int)((im_data[i] >> 16) & 0xff);
a = (int)((im_data[i] >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
rr = (int)((r* .393) + (g*.769) + (b*.189));
if (rr < 0)
rr = 0;
if (rr > 255)
rr = 255;
rr = _normalize_color(rr);
gg = (int)((r* .349) + (g*.686) + (b*.168));
if (gg < 0)
gg = 0;
if (gg > 255)
gg = 255;
gg = _normalize_color(gg);
bb = (int)((r* .272) + (g*.534) + (b*.131));
if (bb < 0)
bb = 0;
if (bb > 255)
bb = 255;
if (a >= 0 && a < 255)
bb = _normalize_color(bb);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
im_data_new[i] = (a << 24) | (rr << 16) | (gg << 8) | bb;
}
ephoto_single_browser_image_data_update(main, image, EINA_TRUE, im_data_new, w, h);
}
void
ephoto_filter_histogram_eq(Evas_Object *main, Evas_Object *image)
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h, i, hist[256], cdf[256];
int a, r, g, b, bb, gg, rr, norm, total;
float hh, s, v, nv, sum;
im_data = evas_object_image_data_get(elm_image_object_get(image), EINA_FALSE);
evas_object_image_size_get(elm_image_object_get(image), &w, &h);
im_data_new = malloc(sizeof(unsigned int) * w * h);
total = w * h;
for (i = 0; i < 256; i++)
hist[i] = 0;
for (y = 0; y < h; y++)
{
p1 = im_data + (y * w);
for (x = 0; x < w; x++)
{
rr = (rr * a) / 255;
gg = (gg * a) / 255;
bb = (bb * a) / 255;
b = (int)((*p1) & 0xff);
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
evas_color_rgb_to_hsv(r, g, b, &hh, &s, &v);
norm = (int)round((double)v * (double)255);
hist[norm] += 1;
p1++;
}
}
sum = 0;
for (i = 0; i < 256; i++)
{
sum += ((double)hist[i] / (double)total);
cdf[i] = (int)round(sum * 255);
}
for (y = 0; y < h; y++)
{
p1 = im_data + (y * w);
p2 = im_data_new + (y * w);
for (x = 0; x < w; x++)
{
b = (int)((*p1) & 0xff);
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
evas_color_rgb_to_hsv(r, g, b, &hh, &s, &v);
norm = (int)round((double)v * (double)255);
nv = (float)cdf[norm] / (float)255;
evas_color_hsv_to_rgb(hh, s, nv, &rr, &gg, &bb);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}
im_data_new[i] = (a << 24) | (rr << 16) | (gg << 8) | bb;
}
ephoto_single_browser_image_data_update(main, image, EINA_TRUE, im_data_new, w, h);
}

@ -17,12 +17,41 @@ struct _Ephoto_HSV
unsigned int *original_im_data;
};
static int
_normalize_color(int color)
{
if (color < 0)
return 0;
else if (color > 255)
return 255;
else
return color;
}
static int
_mul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return (color * (255 / alpha));
else
return color;
}
static int
_demul_color_alpha(int color, int alpha)
{
if (alpha > 0 && alpha < 255)
return ((color * alpha) / 255);
else
return color;
}
unsigned int *
_ephoto_hsv_adjust_hue(Ephoto_HSV *ehsv, double hue, unsigned int *image_data)
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
float hh, s, v;
im_data = malloc(sizeof(unsigned int) * ehsv->w * ehsv->h);
@ -43,32 +72,23 @@ _ephoto_hsv_adjust_hue(Ephoto_HSV *ehsv, double hue, unsigned int *image_data)
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
evas_color_rgb_to_hsv(r, g, b, &hh, &s, &v);
hh += hue;
if (hh < 0)
hh += 360;
if (hh > 360)
hh -= 360;
evas_color_hsv_to_rgb(hh, s, v, &nr, &ng, &nb);
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
evas_color_hsv_to_rgb(hh, s, v, &rr, &gg, &bb);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}
@ -84,7 +104,7 @@ _ephoto_hsv_adjust_saturation(Ephoto_HSV *ehsv, double saturation, unsigned int
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
float hh, s, v;
im_data = malloc(sizeof(unsigned int) * ehsv->w * ehsv->h);
@ -105,32 +125,23 @@ _ephoto_hsv_adjust_saturation(Ephoto_HSV *ehsv, double saturation, unsigned int
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
evas_color_rgb_to_hsv(r, g, b, &hh, &s, &v);
s += s*((float)saturation / 100);
if (s < 0)
s = 0;
if (s > 1)
s = 1;
evas_color_hsv_to_rgb(hh, s, v, &nr, &ng, &nb);
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
evas_color_hsv_to_rgb(hh, s, v, &rr, &gg, &bb);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}
@ -146,7 +157,7 @@ _ephoto_hsv_adjust_value(Ephoto_HSV *ehsv, double value, unsigned int *image_dat
{
unsigned int *im_data, *im_data_new, *p1, *p2;
int x, y, w, h;
int a, r, g, b, nb, ng, nr;
int a, r, g, b, bb, gg, rr;
float hh, s, v;
im_data = malloc(sizeof(unsigned int) * ehsv->w * ehsv->h);
@ -167,32 +178,23 @@ _ephoto_hsv_adjust_value(Ephoto_HSV *ehsv, double value, unsigned int *image_dat
g = (int)((*p1 >> 8) & 0xff);
r = (int)((*p1 >> 16) & 0xff);
a = (int)((*p1 >> 24) & 0xff);
if (a > 0 && a < 255)
{
b = b * (255 / a);
g = g * (255 / a);
r = r * (255 / a);
}
b = _mul_color_alpha(b, a);
g = _mul_color_alpha(g, a);
r = _mul_color_alpha(r, a);
evas_color_rgb_to_hsv(r, g, b, &hh, &s, &v);
v += (v*((float)value/100));
if (v < 0)
v = 0;
if (v > 1)
v = 1;
evas_color_hsv_to_rgb(hh, s, v, &nr, &ng, &nb);
if (nb < 0) nb = 0;
if (nb > 255) nb = 255;
if (ng < 0) ng = 0;
if (ng > 255) ng = 255;
if (nr < 0) nr = 0;
if (nr > 255) nr = 255;
if (a > 0 && a < 255)
{
nb = (nb * a) / 255;
ng = (ng * a) / 255;
nr = (nr * a) / 255;
}
*p2 = (a << 24) | (nr << 16) | (ng << 8) | nb;
evas_color_hsv_to_rgb(hh, s, v, &rr, &gg, &bb);
bb = _normalize_color(bb);
gg = _normalize_color(gg);
rr = _normalize_color(rr);
bb = _demul_color_alpha(bb, a);
gg = _demul_color_alpha(gg, a);
rr = _demul_color_alpha(rr, a);
*p2 = (a << 24) | (rr << 16) | (gg << 8) | bb;
p2++;
p1++;
}

@ -1209,6 +1209,20 @@ _go_hsv(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
}
}
static void
_go_auto_eq(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
if (sb->viewer)
{
sb->editing = EINA_TRUE;
elm_object_disabled_set(sb->bar, EINA_TRUE);
evas_object_freeze_events_set(sb->bar, EINA_TRUE);
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
ephoto_filter_histogram_eq(sb->main, v->image);
}
}
static void
_go_blur(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
@ -1464,6 +1478,7 @@ ephoto_single_browser_add(Ephoto *ephoto, Evas_Object *parent)
elm_menu_item_add(menu, menu_it, "insert-image", _("Brightness/Contrast/Gamma"), _go_bcg, sb);
elm_menu_item_add(menu, menu_it, "insert-image", _("Hue/Saturation/Value"), _go_hsv, sb);
menu_it = elm_menu_item_add(menu, NULL, "document-properties", _("Filters"), NULL, NULL);
elm_menu_item_add(menu, menu_it, "insert-image", _("Auto Equalize"), _go_auto_eq, sb);
elm_menu_item_add(menu, menu_it, "insert-image", _("Black and White"), _go_black_and_white, sb);
elm_menu_item_add(menu, menu_it, "insert-image", _("Old Photo"), _go_old_photo, sb);
/*FIXME: Use separators once they don't mess up homogeneous toolbar*/
@ -1536,9 +1551,13 @@ ephoto_single_browser_entry_set(Evas_Object *obj, Ephoto_Entry *entry)
ephoto_entry_free_listener_add(entry, _entry_free, sb);
_ephoto_single_browser_recalc(sb);
sb->edited_image_data = NULL;
sb->ew = 0;
sb->eh = 0;
if (sb->edited_image_data)
{
free(sb->edited_image_data);
sb->edited_image_data = NULL;
sb->ew = 0;
sb->eh = 0;
}
if (sb->viewer)
_zoom_fit(sb);
}

Loading…
Cancel
Save