Home | History | Annotate | Download | only in ewk
      1 /*
      2     Copyright (C) 2009-2010 ProFUSION embedded systems
      3     Copyright (C) 2009-2010 Samsung Electronics
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Library General Public
      7     License as published by the Free Software Foundation; either
      8     version 2 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Library General Public License for more details.
     14 
     15     You should have received a copy of the GNU Library General Public License
     16     along with this library; see the file COPYING.LIB.  If not, write to
     17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18     Boston, MA 02110-1301, USA.
     19 */
     20 
     21 #include "config.h"
     22 #include "ewk_util.h"
     23 
     24 #include "ewk_private.h"
     25 #include <eina_safety_checks.h>
     26 
     27 /**
     28  * Converts an image from cairo_surface to the Evas_Object.
     29  *
     30  * @param canvas display canvas
     31  * @param surface cairo representation of an image
     32  * @return converted cairo_surface object to the Evas_Object
     33  */
     34 Evas_Object* ewk_util_image_from_cairo_surface_add(Evas* canvas, cairo_surface_t* surface)
     35 {
     36     cairo_status_t status;
     37     cairo_surface_type_t type;
     38     cairo_format_t format;
     39     int w, h, stride;
     40     Evas_Object* image;
     41     const void* src;
     42     void* dst;
     43 
     44     EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
     45     EINA_SAFETY_ON_NULL_RETURN_VAL(surface, 0);
     46 
     47     status = cairo_surface_status(surface);
     48     if (status != CAIRO_STATUS_SUCCESS) {
     49         ERR("cairo surface is invalid: %s", cairo_status_to_string(status));
     50         return 0;
     51     }
     52 
     53     type = cairo_surface_get_type(surface);
     54     if (type != CAIRO_SURFACE_TYPE_IMAGE) {
     55         ERR("unknown surface type %d, required %d (CAIRO_SURFACE_TYPE_IMAGE).",
     56             type, CAIRO_SURFACE_TYPE_IMAGE);
     57         return 0;
     58     }
     59 
     60     format = cairo_image_surface_get_format(surface);
     61     if (format != CAIRO_FORMAT_ARGB32 && format != CAIRO_FORMAT_RGB24) {
     62         ERR("unknown surface format %d, expected %d or %d.",
     63             format, CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_RGB24);
     64         return 0;
     65     }
     66 
     67     w = cairo_image_surface_get_width(surface);
     68     h = cairo_image_surface_get_height(surface);
     69     stride = cairo_image_surface_get_stride(surface);
     70     if (w <= 0 || h <= 0 || stride <= 0) {
     71         ERR("invalid image size %dx%d, stride=%d", w, h, stride);
     72         return 0;
     73     }
     74 
     75     src = cairo_image_surface_get_data(surface);
     76     if (!src) {
     77         ERR("could not get source data.");
     78         return 0;
     79     }
     80 
     81     image = evas_object_image_filled_add(canvas);
     82     if (!image) {
     83         ERR("could not add image to canvas.");
     84         return 0;
     85     }
     86 
     87     evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
     88     evas_object_image_size_set(image, w, h);
     89     evas_object_image_alpha_set(image, format == CAIRO_FORMAT_ARGB32);
     90 
     91     if (evas_object_image_stride_get(image) != stride) {
     92         ERR("evas' stride %d diverges from cairo's %d.",
     93             evas_object_image_stride_get(image), stride);
     94         evas_object_del(image);
     95         return 0;
     96     }
     97 
     98     dst = evas_object_image_data_get(image, EINA_TRUE);
     99     memcpy(dst, src, h * stride);
    100     evas_object_image_data_set(image, dst);
    101 
    102     evas_object_resize(image, w, h); // helpful but not really required
    103 
    104     return image;
    105 }
    106