From 1827f5f0d2074a7abf4a8c77f9b7ef43961e102f Mon Sep 17 00:00:00 2001 From: Sung-jae Park Date: Thu, 19 Jun 2014 18:47:51 +0900 Subject: [PATCH] Add new loading type fit-size over-size : enlarge the image if it is smaller than part size, and then crop it. in-size : resize the image to fit it into the part without copping fit-size : resize the image as possible as its size is similiar with part size, and then crop it. Change-Id: Ied36f85befd34cd4b21554698fd3a3f8902b8859 --- src/script_port.c | 237 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 138 insertions(+), 99 deletions(-) diff --git a/src/script_port.c b/src/script_port.c index 80d5d7e..cfbafb4 100644 --- a/src/script_port.c +++ b/src/script_port.c @@ -53,7 +53,8 @@ struct image_option { enum { FILL_DISABLE, FILL_IN_SIZE, - FILL_OVER_SIZE + FILL_OVER_SIZE, + FILL_FIT_SIZE } fill; struct shadow { @@ -395,6 +396,8 @@ static void parse_fill(struct image_option *img_opt, const char *value, int len) img_opt->fill = FILL_IN_SIZE; } else if (!strncasecmp(value, "over-size", len)) { img_opt->fill = FILL_OVER_SIZE; + } else if (!strncasecmp(value, "fit-size", len)) { + img_opt->fill = FILL_FIT_SIZE; } else { img_opt->fill = FILL_DISABLE; } @@ -691,6 +694,93 @@ static inline void apply_shadow_effect(struct image_option *img_opt, Evas_Object ea_image_effect_destroy(ea_effect); } +static Evas_Object *crop_image(Evas_Object *img, const char *path, int part_w, int part_h, int w, int h, struct image_option *img_opt) +{ + Ecore_Evas *ee; + Evas *e; + Evas_Object *src_img; + Evas_Coord rw, rh; + const void *data; + Evas_Load_Error err; + + ee = ecore_evas_buffer_new(part_w, part_h); + if (!ee) { + ErrPrint("Failed to create a EE\n"); + evas_object_del(img); + return NULL; + } + + ecore_evas_alpha_set(ee, EINA_TRUE); + + e = ecore_evas_get(ee); + if (!e) { + ErrPrint("Unable to get Evas\n"); + ecore_evas_free(ee); + evas_object_del(img); + return NULL; + } + + src_img = evas_object_image_filled_add(e); + if (!src_img) { + ErrPrint("Unable to add an image\n"); + ecore_evas_free(ee); + evas_object_del(img); + return NULL; + } + + evas_object_image_alpha_set(src_img, EINA_TRUE); + evas_object_image_colorspace_set(src_img, EVAS_COLORSPACE_ARGB8888); + evas_object_image_smooth_scale_set(src_img, EINA_TRUE); + evas_object_image_load_orientation_set(src_img, img_opt->orient); + evas_object_image_file_set(src_img, path, NULL); + err = evas_object_image_load_error_get(src_img); + if (err != EVAS_LOAD_ERROR_NONE) { + ErrPrint("Load error: %s\n", evas_load_error_str(err)); + evas_object_del(src_img); + ecore_evas_free(ee); + evas_object_del(img); + return NULL; + } + evas_object_image_size_get(src_img, &rw, &rh); + evas_object_image_fill_set(src_img, 0, 0, rw, rh); + evas_object_resize(src_img, w, h); + evas_object_move(src_img, -(w - part_w) / 2, -(h - part_h) / 2); + evas_object_show(src_img); + + data = ecore_evas_buffer_pixels_get(ee); + if (!data) { + ErrPrint("Unable to get pixels\n"); + evas_object_del(src_img); + ecore_evas_free(ee); + evas_object_del(img); + return NULL; + } + + e = evas_object_evas_get(img); + evas_object_del(img); + img = evas_object_image_filled_add(e); + if (!img) { + evas_object_del(src_img); + ecore_evas_free(ee); + return NULL; + } + + evas_object_image_colorspace_set(img, EVAS_COLORSPACE_ARGB8888); + evas_object_image_smooth_scale_set(img, EINA_TRUE); + evas_object_image_alpha_set(img, EINA_TRUE); + evas_object_image_data_set(img, NULL); + evas_object_image_size_set(img, part_w, part_h); + evas_object_resize(img, part_w, part_h); + evas_object_image_data_copy_set(img, (void *)data); + evas_object_image_fill_set(img, 0, 0, part_w, part_h); + evas_object_image_data_update_add(img, 0, 0, part_w, part_h); + + evas_object_del(src_img); + ecore_evas_free(ee); + + return img; +} + PUBLIC int script_update_image(void *_h, const char *id, const char *part, const char *path, const char *option) { struct info *handle = _h; @@ -790,96 +880,7 @@ PUBLIC int script_update_image(void *_h, const char *id, const char *part, const return LB_STATUS_ERROR_INVALID; } - if (evas_object_image_region_support_get(img)) { - evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h); - evas_object_image_filled_set(img, EINA_TRUE); - //evas_object_image_fill_set(img, 0, 0, part_w, part_h); - DbgPrint("Size: %dx%d (region: %dx%d - %dx%d)\n", w, h, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h); - } else { - Ecore_Evas *ee; - Evas *e; - Evas_Object *src_img; - Evas_Coord rw, rh; - const void *data; - - DbgPrint("Part loading is not supported\n"); - ee = ecore_evas_buffer_new(part_w, part_h); - if (!ee) { - ErrPrint("Failed to create a EE\n"); - evas_object_del(img); - return LB_STATUS_ERROR_FAULT; - } - - ecore_evas_alpha_set(ee, EINA_TRUE); - - e = ecore_evas_get(ee); - if (!e) { - ErrPrint("Unable to get Evas\n"); - ecore_evas_free(ee); - evas_object_del(img); - return LB_STATUS_ERROR_FAULT; - } - - src_img = evas_object_image_filled_add(e); - if (!src_img) { - ErrPrint("Unable to add an image\n"); - ecore_evas_free(ee); - evas_object_del(img); - return LB_STATUS_ERROR_FAULT; - } - - evas_object_image_alpha_set(src_img, EINA_TRUE); - evas_object_image_colorspace_set(src_img, EVAS_COLORSPACE_ARGB8888); - evas_object_image_smooth_scale_set(src_img, EINA_TRUE); - evas_object_image_load_orientation_set(src_img, img_opt.orient); - evas_object_image_file_set(src_img, path, NULL); - err = evas_object_image_load_error_get(src_img); - if (err != EVAS_LOAD_ERROR_NONE) { - ErrPrint("Load error: %s\n", evas_load_error_str(err)); - evas_object_del(src_img); - ecore_evas_free(ee); - evas_object_del(img); - return LB_STATUS_ERROR_IO; - } - evas_object_image_size_get(src_img, &rw, &rh); - evas_object_image_fill_set(src_img, 0, 0, rw, rh); - evas_object_resize(src_img, w, h); - evas_object_move(src_img, -(w - part_w) / 2, -(h - part_h) / 2); - evas_object_show(src_img); - - data = ecore_evas_buffer_pixels_get(ee); - if (!data) { - ErrPrint("Unable to get pixels\n"); - evas_object_del(src_img); - ecore_evas_free(ee); - evas_object_del(img); - return LB_STATUS_ERROR_IO; - } - - e = evas_object_evas_get(img); - evas_object_del(img); - img = evas_object_image_filled_add(e); - if (!img) { - evas_object_del(src_img); - ecore_evas_free(ee); - return LB_STATUS_ERROR_MEMORY; - } - - evas_object_image_colorspace_set(img, EVAS_COLORSPACE_ARGB8888); - evas_object_image_smooth_scale_set(img, EINA_TRUE); - evas_object_image_alpha_set(img, EINA_TRUE); - evas_object_image_data_set(img, NULL); - evas_object_image_size_set(img, part_w, part_h); - evas_object_resize(img, part_w, part_h); - evas_object_image_data_copy_set(img, (void *)data); - evas_object_image_fill_set(img, 0, 0, part_w, part_h); - evas_object_image_data_update_add(img, 0, 0, part_w, part_h); - - evas_object_del(src_img); - ecore_evas_free(ee); - - apply_shadow_effect(&img_opt, img); - } + img = crop_image(img, path, part_w, part_h, w, h, &img_opt); } else if (img_opt.fill == FILL_IN_SIZE) { Evas_Coord part_w; Evas_Coord part_h; @@ -894,7 +895,7 @@ PUBLIC int script_update_image(void *_h, const char *id, const char *part, const } DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h); - if (part_w > w || part_h > h) { + if (w > part_w || h > part_h) { double fw; double fh; @@ -902,22 +903,60 @@ PUBLIC int script_update_image(void *_h, const char *id, const char *part, const fh = (double)part_h / (double)h; if (fw > fh) { - w = part_w; - h = (double)h * fw; - } else { h = part_h; w = (double)w * fh; + } else { + w = part_w; + h = (double)h * fw; } } - DbgPrint("Size: %dx%d\n", w, h); - evas_object_image_fill_set(img, 0, 0, part_w, part_h); - evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL); - evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + + if (!part_w || !part_h || !w || !h) { + evas_object_del(img); + return LB_STATUS_ERROR_INVALID; + } + + img = crop_image(img, path, part_w, part_h, w, h, &img_opt); + } else if (img_opt.fill == FILL_FIT_SIZE) { + Evas_Coord part_w; + Evas_Coord part_h; + double fw; + double fh; + + if (img_opt.width >= 0 && img_opt.height >= 0) { + part_w = img_opt.width * elm_config_scale_get(); + part_h = img_opt.height * elm_config_scale_get(); + } else { + part_w = 0; + part_h = 0; + edje_object_part_geometry_get(elm_layout_edje_get(edje), part, NULL, NULL, &part_w, &part_h); + } + DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h); + + fw = (double)part_w / (double)w; + fh = (double)part_h / (double)h; + + if (fw < fh) { + h = part_h; + w = (double)w * fh; + } else { + w = part_w; + h = (double)h * fw; + } + + if (!part_w || !part_h || !w || !h) { + evas_object_del(img); + return LB_STATUS_ERROR_INVALID; + } + + img = crop_image(img, path, part_w, part_h, w, h, &img_opt); } else { evas_object_image_fill_set(img, 0, 0, w, h); evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL); evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h); } + + apply_shadow_effect(&img_opt, img); } else { if (img_opt.width >= 0 && img_opt.height >= 0) { w = img_opt.width; -- 2.7.4