Hi,
I was using epsilon to generate some thumbnails but I needed to create
them in a fixed width x height, but epsilon default behavior is to
keep the original image aspect (thus changing your specified width or
height). So I made a patch that enables epsilon to make my cropped
thumbnails.
Note that this patch doesn't add support for cropped thumbs from edje
images (I didn't understand some of the code to create these thumbs).
Can someone review (and maybe apply) it, please?
--
Rafael Antognolli
Index: src/lib/Epsilon.h
===================================================================
--- src/lib/Epsilon.h (revision 39447)
+++ src/lib/Epsilon.h (working copy)
@@ -44,6 +44,8 @@
int tw, th;
int tsize;
int format;
+ int aspect;
+ float crop_x, crop_y;
};
typedef struct _Epsilon Epsilon;
@@ -73,6 +75,15 @@
typedef enum _Epsilon_Thumb_Format Epsilon_Thumb_Format;
+enum _Epsilon_Thumb_Aspect
+{
+ EPSILON_THUMB_KEEP_ASPECT,
+ EPSILON_THUMB_IGNORE_ASPECT,
+ EPSILON_THUMB_CROP
+};
+
+typedef enum _Epsilon_Thumb_Aspect Epsilon_Thumb_Aspect;
+
EAPI int epsilon_init (void);
/* construct destruct */
@@ -85,6 +96,10 @@
EAPI void epsilon_resolution_set (Epsilon * e, int w, int h);
/* Set the thumbnail format */
EAPI void epsilon_format_set (Epsilon * e, Epsilon_Thumb_Format f);
+/* Set the thumbnail aspect ratio */
+EAPI void epsilon_aspect_set (Epsilon * e, Epsilon_Thumb_Aspect a);
+/* Set the crop alignment */
+EAPI void epsilon_crop_align_set (Epsilon * e, float x, float y);
/*
* the source filename
Index: src/lib/Epsilon.c
===================================================================
--- src/lib/Epsilon.c (revision 39447)
+++ src/lib/Epsilon.c (working copy)
@@ -84,6 +84,8 @@
result->tw = THUMB_SIZE_LARGE;
result->th = THUMB_SIZE_LARGE;
result->format = EPSILON_THUMB_FDO;
+ result->crop_x = 0.5;
+ result->crop_y = 0.5;
}
else
{
@@ -228,6 +230,25 @@
e->format = f;
}
+void
+epsilon_aspect_set (Epsilon * e, Epsilon_Thumb_Aspect a)
+{
+ if (e && (a == EPSILON_THUMB_KEEP_ASPECT ||
+ a == EPSILON_THUMB_IGNORE_ASPECT ||
+ a == EPSILON_THUMB_CROP))
+ e->aspect = a;
+}
+
+void
+epsilon_crop_align_set (Epsilon * e, float x, float y)
+{
+ if (e && (x >= 0 && x <= 1 && y >= 0 && y <= 1))
+ {
+ e->crop_x = x;
+ e->crop_y = y;
+ }
+}
+
const char *
epsilon_file_get (Epsilon * e)
{
@@ -577,6 +598,53 @@
return (EPSILON_FAIL);
}
+
+static void
+_epsilon_calculate_aspect(Epsilon * e, int iw, int ih, int * w, int * h, int * fx, int * fy, int * fw, int * fh)
+{
+ *w = e->tw;
+ *h = e->th;
+ *fw = e->tw;
+ *fh = e->th;
+ if ((iw > ih && e->tw > 0) || e->th <= 0)
+ {
+ if (e->aspect == EPSILON_THUMB_KEEP_ASPECT)
+ {
+ *fh = (e->tw * ih) / iw;
+ *h = *fh;
+ }
+ else if (e->aspect == EPSILON_THUMB_CROP)
+ {
+ *fw = (e->th * iw) / ih;
+ *w = e->tw;
+ }
+ }
+ else
+ {
+ if (e->aspect == EPSILON_THUMB_KEEP_ASPECT)
+ {
+ *fw = (e->th * iw) / ih;
+ *w = *fw;
+ }
+ else if (e->aspect == EPSILON_THUMB_CROP)
+ {
+ *fh = (e->tw * ih) / iw;
+ *h = e->th;
+ }
+ }
+
+ if (e->aspect == EPSILON_THUMB_CROP)
+ {
+ *fx = - e->crop_x * (*fw - *w);
+ *fy = - e->crop_y * (*fh - *h);
+ }
+ else
+ {
+ *fx = 0;
+ *fy = 0;
+ }
+}
+
int
epsilon_generate (Epsilon * e)
{
@@ -585,7 +653,7 @@
Ecore_Evas *ee = NULL, *ee_im = NULL;
Evas_Object *im = NULL, *edje = NULL;
int ret = EPSILON_FAIL;
- int iw, ih, alpha, ww, hh;
+ int iw, ih, alpha, ww, hh, fx, fy, fw, fh;
unsigned int *data = NULL;
struct stat filestatus;
time_t mtime = 0;
@@ -610,6 +678,10 @@
evas_font_cache_set(evas, 0);
ww = 0;
hh = 0;
+ fx = 0;
+ fy = 0;
+ fw = 0;
+ fh = 0;
alpha = 1;
ext = strrchr(e->src, '.');
@@ -632,17 +704,8 @@
evas_object_image_data_update_add(im, 0, 0, iw, ih);
if ((iw > 0) && (ih > 0))
{
- if ((iw > ih && e->tw > 0) || e->th <= 0)
- {
- ww = e->tw;
- hh = (e->tw * ih) / iw;
- }
- else
- {
- hh = e->th;
- ww = (e->th * iw) / ih;
- }
- evas_object_image_fill_set(im, 0, 0, ww, hh);
+ _epsilon_calculate_aspect(e, iw, ih, &ww, &hh, &fx, &fy, &fw, &fh);
+ evas_object_image_fill_set(im, fx, fy, fw, fh);
}
ret = EPSILON_OK;
}
@@ -681,19 +744,11 @@
alpha = evas_object_image_alpha_get(im);
if ((iw > 0) && (ih > 0))
{
- if ((iw > ih && e->tw > 0) || e->th <= 0)
- {
- ww = e->tw;
- hh = (e->tw * ih) / iw;
- }
- else
- {
- hh = e->th;
- ww = (e->th * iw) / ih;
- }
- evas_object_image_fill_set(im, 0, 0, ww, hh);
+ _epsilon_calculate_aspect(e, iw, ih, &ww, &hh, &fx, &fy, &fw, &fh);
+ evas_object_image_fill_set(im, fx, fy, fw, fh);
}
}
+
evas_object_move(im, 0, 0);
evas_object_resize(im, ww, hh);
ecore_evas_resize(ee, ww, hh);
Index: python-epsilon/include/epsilon/c_epsilon.pxd
===================================================================
--- python-epsilon/include/epsilon/c_epsilon.pxd (revision 39447)
+++ python-epsilon/include/epsilon/c_epsilon.pxd (working copy)
@@ -34,6 +34,9 @@
int tw
int th
int format
+ int aspect
+ float crop_x
+ float crop_y
ctypedef void Epsilon_Exif_Info
@@ -52,6 +55,8 @@
void epsilon_free(_Epsilon *e)
void epsilon_key_set(_Epsilon *e, char *key)
void epsilon_resolution_set(_Epsilon *e, int w, int h)
+ void epsilon_aspect_set (_Epsilon *e, int a)
+ void epsilon_crop_align_set (_Epsilon * e, float x, float y)
char *epsilon_file_get(_Epsilon *e)
char *epsilon_thumb_file_get(_Epsilon *e)
Index: python-epsilon/epsilon/epsilon.c_epsilon.pyx
===================================================================
--- python-epsilon/epsilon/epsilon.c_epsilon.pyx (revision 39447)
+++ python-epsilon/epsilon/epsilon.c_epsilon.pyx (working copy)
@@ -27,6 +27,9 @@
EPSILON_THUMB_LARGE = 1
EPSILON_THUMB_FDO = 0
EPSILON_THUMB_JPEG = 1
+EPSILON_THUMB_KEEP_ASPECT = 0
+EPSILON_THUMB_IGNORE_ASPECT = 1
+EPSILON_THUMB_CROP = 2
cdef class Epsilon:
"""Epsilon thumbnail generator.
@@ -185,6 +188,42 @@
def __get__(self):
return self.format_get()
+ def aspect_set(self, int a):
+ """Specify thumbnail aspect, either EPSILON_THUMB_KEEP_ASPECT,
+ EPSILON_THUMB_IGNORE_ASPECT or EPSILON_THUMB_CROP.
+ """
+ if a != EPSILON_THUMB_KEEP_ASPECT and \
+ a != EPSILON_THUMB_IGNORE_ASPECT and \
+ a != EPSILON_THUMB_CROP:
+ raise ValueError("value must be either EPSILON_THUMB_KEEP_ASPECT, "
+ "EPSILON_THUMB_IGNORE_ASPECT or "
+ "EPSILON_THUMB_CROP")
+ epsilon_aspect_set(self.obj, a)
+
+ def aspect_get(self):
+ "@rtype: int"
+ if self.obj:
+ if self.obj.aspect == EPSILON_THUMB_KEEP_ASPECT:
+ return EPSILON_THUMB_KEEP_ASPECT
+ elif self.obj.aspect == EPSILON_THUMB_IGNORE_ASPECT:
+ return EPSILON_THUMB_IGNORE_ASPECT
+ else:
+ return EPSILON_THUMB_CROP
+
+ property aspect:
+ def __set__(self, int a):
+ self.aspect_set(a)
+
+ def __get__(self):
+ return self.aspect_get()
+
+ def crop_align_set(self, float x, float y):
+ """Specify thumbnail crop alignment.
+ """
+ if x < 0 or x > 1 or y < 0 or y > 1:
+ raise ValueError("values must be between 0 and 1")
+ epsilon_crop_align_set(self.obj, x, y)
+
def thumb_custom_size_set(self, int w, int h, char *directory):
"""Specify a custom thumbnail size.
Index: python-epsilon/epsilon/__init__.py
===================================================================
--- python-epsilon/epsilon/__init__.py (revision 39447)
+++ python-epsilon/epsilon/__init__.py (working copy)
@@ -18,4 +18,6 @@
#!/usr/bin/env python
from c_epsilon import init, EPSILON_THUMB_NORMAL, EPSILON_THUMB_LARGE, \
- EPSILON_THUMB_FDO, EPSILON_THUMB_JPEG, Epsilon
+ EPSILON_THUMB_FDO, EPSILON_THUMB_JPEG, \
+ EPSILON_THUMB_KEEP_ASPECT, EPSILON_THUMB_IGNORE_ASPECT, \
+ EPSILON_THUMB_CROP, Epsilon
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
enlightenment-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel