Various minor changes
[profile/ivi/pixman.git] / pixman / pixman-image.c
index dca04cd..3a0d0f8 100644 (file)
 /*
+ * Copyright © 2000 SuSE, Inc.
  * Copyright © 2007 Red Hat, Inc.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
  * the above copyright notice appear in all copies and that both that
  * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of Red Hat not be used in advertising or
+ * documentation, and that the name of SuSE not be used in advertising or
  * publicity pertaining to distribution of the software without specific,
- * written prior permission.  Red Hat makes no representations about the
+ * written prior permission.  SuSE makes no representations about the
  * suitability of this software for any purpose.  It is provided "as is"
  * without express or implied warranty.
  *
- * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
+ * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Author:  Soren Sandmann, Red Hat, Inc.
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
 
-#include "pixman.h"
 #include "pixman-private.h"
 
+#define Alpha(x) ((x) >> 24)
+
+pixman_bool_t
+_pixman_init_gradient (gradient_t     *gradient,
+                      const pixman_gradient_stop_t *stops,
+                      int             n_stops)
+{
+    return_val_if_fail (n_stops > 0, FALSE);
+
+    gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
+    if (!gradient->stops)
+       return FALSE;
+
+    memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
+
+    gradient->n_stops = n_stops;
+
+    gradient->stop_range = 0xffff;
+    gradient->color_table = NULL;
+    gradient->color_table_size = 0;
+    gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
+
+    return TRUE;
+}
+
+/*
+ * By default, just evaluate the image at 32bpp and expand.  Individual image
+ * types can plug in a better scanline getter if they want to. For example
+ * we  could produce smoother gradients by evaluating them at higher color depth, but
+ * that's a project for the future.
+ */
 void
-pixman_image_init_bits (pixman_image_t         *image,
-                       pixman_format_code_t    format,
-                       int                     width,
-                       int                     height,
-                       uint8_t                *bits,
-                       int                     rowstride)
+_pixman_image_get_scanline_64_generic (pixman_image_t * pict, int x, int y, int width,
+                                      uint64_t *buffer, uint64_t *mask, uint32_t maskBits)
 {
-    image_t *img = (image_t *)image;
-
-    img->type = BITS;
-    img->bits.format = format;
-    img->bits.width = width;
-    img->bits.height = height;
-    img->bits.bits = bits;
-    img->bits.rowstride = rowstride;
+    uint32_t *mask8 = NULL;
+
+    // Contract the mask image, if one exists, so that the 32-bit fetch function
+    // can use it.
+    if (mask) {
+        mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
+       if (!mask8)
+           return;
+       
+        pixman_contract(mask8, mask, width);
+    }
+
+    // Fetch the source image into the first half of buffer.
+    _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
+                                  maskBits);
+
+    // Expand from 32bpp to 64bpp in place.
+    pixman_expand(buffer, (uint32_t*)buffer, PIXMAN_a8r8g8b8, width);
+
+    free(mask8);
+}
+
+pixman_image_t *
+_pixman_image_allocate (void)
+{
+    pixman_image_t *image = malloc (sizeof (pixman_image_t));
+
+    if (image)
+    {
+       image_common_t *common = &image->common;
+
+       pixman_region32_init (&common->clip_region);
+
+       common->have_clip_region = FALSE;
+       common->clip_sources = FALSE;
+       common->transform = NULL;
+       common->repeat = PIXMAN_REPEAT_NONE;
+       common->filter = PIXMAN_FILTER_NEAREST;
+       common->filter_params = NULL;
+       common->n_filter_params = 0;
+       common->alpha_map = NULL;
+       common->component_alpha = FALSE;
+       common->ref_count = 1;
+       common->read_func = NULL;
+       common->write_func = NULL;
+       common->classify = NULL;
+       common->client_clip = FALSE;
+       common->destroy_func = NULL;
+       common->destroy_data = NULL;
+    }
+
+    return image;
+}
+
+source_pict_class_t
+_pixman_image_classify (pixman_image_t *image,
+                       int             x,
+                       int             y,
+                       int             width,
+                       int             height)
+{
+    if (image->common.classify)
+       return image->common.classify (image, x, y, width, height);
+    else
+       return SOURCE_IMAGE_CLASS_UNKNOWN;
 }
 
 void
-pixman_set_clip_region (pixman_image_t    *image,
-                       pixman_region16_t *region)
+_pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
+                              uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
 {
-    
+    image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
 }
 
-enum
+void
+_pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
+                              uint32_t *buffer, uint32_t *unused, uint32_t unused2)
 {
-    PIXMAN_BAD_VALUE,
-    PIXMAN_BAD_ALLOC
-};
+    image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
+}
 
-static void
-init_source_image (source_image_t *image)
+/* Even thought the type of buffer is uint32_t *, the function actually expects
+ * a uint64_t *buffer.
+ */
+
+scanFetchProc
+_pixman_image_get_fetcher (pixman_image_t *image,
+                          int             wide)
 {
-    image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
+    assert (image->common.get_scanline_64);
+    assert (image->common.get_scanline_32);
+    
+    if (wide)
+       return image->common.get_scanline_64;
+    else
+       return image->common.get_scanline_32;
 }
 
 static void
-init_gradient (gradient_t     *gradient,
-              int             stop_count,
-              pixman_fixed_t *stop_points,
-              pixman_color_t *stop_colors,
-              int *error)
+image_property_changed (pixman_image_t *image)
 {
-    int i;
-    pixman_fixed_t dpos;
+    image->common.property_changed (image);
+}
 
-    if (stop_count <= 0)
-    {
-        *error = PIXMAN_BAD_VALUE;
-        return;
-    }
+/* Ref Counting */
+PIXMAN_EXPORT pixman_image_t *
+pixman_image_ref (pixman_image_t *image)
+{
+    image->common.ref_count++;
 
-    init_source_image (&gradient->common);
-    
-    dpos = -1;
-    for (i = 0; i < stop_count; ++i)
+    return image;
+}
+
+/* returns TRUE when the image is freed */
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_unref (pixman_image_t *image)
+{
+    image_common_t *common = (image_common_t *)image;
+
+    common->ref_count--;
+
+    if (common->ref_count == 0)
     {
-        if (stop_points[i] < dpos || stop_points[i] > (1<<16))
+       if (image->common.destroy_func)
+           image->common.destroy_func (image, image->common.destroy_data);
+       
+       pixman_region32_fini (&common->clip_region);
+
+       if (common->transform)
+           free (common->transform);
+
+       if (common->filter_params)
+           free (common->filter_params);
+
+       if (common->alpha_map)
+           pixman_image_unref ((pixman_image_t *)common->alpha_map);
+
+#if 0
+       if (image->type == BITS && image->bits.indexed)
+           free (image->bits.indexed);
+#endif
+
+#if 0
+       memset (image, 0xaa, sizeof (pixman_image_t));
+#endif
+       if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
        {
-            *error = PIXMAN_BAD_VALUE;
-            return;
-        }
-        dpos = stop_points[i];
-    }
+           if (image->gradient.stops)
+               free (image->gradient.stops);
+       }
 
-    gradient->stops = malloc (stop_count * sizeof (gradient_stop_t));
-    if (!gradient->stops)
-    {
-        *error = PIXMAN_BAD_ALLOC;
-        return;
-    }
 
-    gradient->n_stops = stop_count;
+       if (image->type == BITS && image->bits.free_me)
+           free (image->bits.free_me);
 
-    for (i = 0; i < stop_count; ++i)
-    {
-        gradient->stops[i].x = stop_points[i];
-        gradient->stops[i].color = stop_colors[i];
+       free (image);
+
+       return TRUE;
     }
 
-    gradient->stop_range = 0xffff;
-    gradient->color_table = NULL;
-    gradient->color_table_size = 0;
+    return FALSE;
 }
 
-static uint32_t
-color_to_uint32 (const pixman_color_t *color)
+PIXMAN_EXPORT void
+pixman_image_set_destroy_function (pixman_image_t *image,
+                                  pixman_image_destroy_func_t func,
+                                  void *data)
 {
-    return
-       (color->alpha >> 8 << 24) |
-       (color->red >> 8 << 16) |
-        (color->green & 0xff00) |
-       (color->blue >> 8);
+    image->common.destroy_func = func;
+    image->common.destroy_data = data;
 }
+                              
+
+/* Constructors */
 
 void
-pixman_image_init_solid_fill (pixman_image_t *image,
-                             pixman_color_t *color,
-                             int            *error)
+_pixman_image_reset_clip_region (pixman_image_t *image)
 {
-    image_t *priv = (image_t *)image;
-    priv->type = SOLID;
-    priv->solid.color = color_to_uint32 (color);
+    image->common.have_clip_region = FALSE;
 }
 
-void
-pixman_image_init_linear_gradient (pixman_image_t       *image,
-                                  pixman_point_fixed_t *p1,
-                                  pixman_point_fixed_t *p2,
-                                  int                   n_stops,
-                                  pixman_fixed_t       *stops,
-                                  pixman_color_t       *colors,
-                                  int                  *error)
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_set_clip_region32 (pixman_image_t *image,
+                               pixman_region32_t *region)
 {
-    image_t *priv = (image_t *)image;
-    linear_gradient_t *linear = &priv->linear;
-    
-    if (n_stops < 2)
+    image_common_t *common = (image_common_t *)image;
+    pixman_bool_t result;
+
+    if (region)
     {
-        *error = PIXMAN_BAD_VALUE;
-        return;
+       if ((result = pixman_region32_copy (&common->clip_region, region)))
+           image->common.have_clip_region = TRUE;
     }
-    
-    init_gradient (&linear->common, n_stops, stops, colors, error);
+    else
+    {
+       _pixman_image_reset_clip_region (image);
 
-    linear->p1 = *p1;
-    linear->p2 = *p2;
+       result = TRUE;
+    }
+
+    image_property_changed (image);
 
-    priv->type = LINEAR;
+    return result;
 }
 
 
-void
-pixman_image_init_radial_gradient (pixman_image_t *image,
-                                  pixman_point_fixed_t *inner,
-                                  pixman_point_fixed_t *outer,
-                                  pixman_fixed_t inner_radius,
-                                  pixman_fixed_t outer_radius,
-                                  int             n_stops,
-                                  pixman_fixed_t *stops,
-                                  pixman_color_t *colors,
-                                  int            *error)
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_set_clip_region (pixman_image_t    *image,
+                             pixman_region16_t *region)
+{
+    image_common_t *common = (image_common_t *)image;
+    pixman_bool_t result;
+
+    if (region)
+    {
+       if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
+           image->common.have_clip_region = TRUE;
+    }
+    else
+    {
+       _pixman_image_reset_clip_region (image);
+
+       result = TRUE;
+    }
+
+    image_property_changed (image);
+
+    return result;
+}
+
+PIXMAN_EXPORT void
+pixman_image_set_has_client_clip (pixman_image_t *image,
+                                 pixman_bool_t   client_clip)
+{
+    image->common.client_clip = client_clip;
+}
+
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_set_transform (pixman_image_t           *image,
+                           const pixman_transform_t *transform)
 {
-    image_t *priv = (image_t *)image;
-    radial_gradient_t *radial = &priv->radial;
+    static const pixman_transform_t id =
+    {
+       { { pixman_fixed_1, 0, 0 },
+         { 0, pixman_fixed_1, 0 },
+         { 0, 0, pixman_fixed_1 }
+       }
+    };
 
-    if (n_stops < 2)
+    image_common_t *common = (image_common_t *)image;
+    pixman_bool_t result;
+
+    if (common->transform == transform)
+       return TRUE;
+
+    if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
     {
-        *error = PIXMAN_BAD_VALUE;
-       return;
+       free(common->transform);
+       common->transform = NULL;
+       result = TRUE;
+       goto out;
     }
 
-    init_gradient (&radial->common, n_stops, stops, colors, error);
+    if (common->transform == NULL)
+       common->transform = malloc (sizeof (pixman_transform_t));
 
-    priv->type = RADIAL;
+    if (common->transform == NULL)
+    {
+       result = FALSE;
+       goto out;
+    }
+
+    memcpy(common->transform, transform, sizeof(pixman_transform_t));
+
+out:
+    image_property_changed (image);
     
-    radial->c1.x = inner->x;
-    radial->c1.y = inner->y;
-    radial->c1.radius = inner_radius;
-    radial->c2.x = outer->x;
-    radial->c2.y = outer->y;
-    radial->c2.radius = outer_radius;
-    radial->cdx = (radial->c2.x - radial->c1.x) / 65536.;
-    radial->cdy = (radial->c2.y - radial->c1.y) / 65536.;
-    radial->dr = (radial->c2.radius - radial->c1.radius) / 65536.;
-    radial->A = (  radial->cdx * radial->cdx
-                  + radial->cdy * radial->cdy
-                  - radial->dr  * radial->dr);
+    return TRUE;
 }
 
-void
-pixman_image_init_conical_gradient (pixman_image_t *image,
-                                   pixman_point_fixed_t *center,
-                                   pixman_fixed_t angle,
-                                   int n_stops,
-                                   pixman_fixed_t *stops,
-                                   pixman_color_t *colors,
-                                   int *error)
+PIXMAN_EXPORT void
+pixman_image_set_repeat (pixman_image_t  *image,
+                        pixman_repeat_t  repeat)
+{
+    image->common.repeat = repeat;
+
+    image_property_changed (image);
+}
+
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_set_filter (pixman_image_t       *image,
+                        pixman_filter_t       filter,
+                        const pixman_fixed_t *params,
+                        int                   n_params)
 {
-    image_t *priv = (image_t *)image;
-    conical_gradient_t *conical = &priv->conical;
+    image_common_t *common = (image_common_t *)image;
+    pixman_fixed_t *new_params;
 
-    if (n_stops < 2)
+    if (params == common->filter_params && filter == common->filter)
+       return TRUE;
+
+    new_params = NULL;
+    if (params)
     {
-       *error = PIXMAN_BAD_VALUE;
-       return;
+       new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
+       if (!new_params)
+           return FALSE;
+
+       memcpy (new_params,
+               params, n_params * sizeof (pixman_fixed_t));
     }
 
-    init_gradient (&conical->common, n_stops, stops, colors, error);
+    common->filter = filter;
+
+    if (common->filter_params)
+       free (common->filter_params);
 
-    priv->type = CONICAL;
-    conical->center = *center;
-    conical->angle = angle;
+    common->filter_params = new_params;
+    common->n_filter_params = n_params;
+
+    image_property_changed (image);
+    return TRUE;
 }
 
-#define SCANLINE_BUFFER_LENGTH 1024
+PIXMAN_EXPORT void
+pixman_image_set_source_clipping (pixman_image_t  *image,
+                                 pixman_bool_t    clip_sources)
+{
+    image->common.clip_sources = clip_sources;
+}
 
-void
-pixman_composite (pixman_image_t       *src_img,
-                 pixman_image_t        *mask_img,
-                 pixman_image_t        *dest_img,
-                 int                    src_x,
-                 int                    src_y,
-                 int                    mask_x,
-                 int                    mask_y,
-                 int                    dest_x,
-                 int                    dset_y,
-                 int                    width,
-                 int                    height)
+/* Unlike all the other property setters, this function does not
+ * copy the content of indexed. Doing this copying is simply
+ * way, way too expensive.
+ */
+PIXMAN_EXPORT void
+pixman_image_set_indexed (pixman_image_t        *image,
+                         const pixman_indexed_t *indexed)
 {
-    image_t *src = (image_t *) src;
-    image_t *mask = (image_t *) mask;
-    image_t *dest = (image_t *) dest;
+    bits_image_t *bits = (bits_image_t *)image;
 
-    uint32_t _scanline_buffer[SCANLINE_BUFFER_LENGTH * 3];
-    uint32_t *scanline_buffer = _scanline_buffer;
+    bits->indexed = indexed;
 
-    if (width > SCANLINE_BUFFER_LENGTH)
-       scanline_buffer = (uint32_t *)malloc (width * 3 * sizeof (uint32_t));
+    image_property_changed (image);
+}
+
+PIXMAN_EXPORT void
+pixman_image_set_alpha_map (pixman_image_t *image,
+                           pixman_image_t *alpha_map,
+                           int16_t         x,
+                           int16_t         y)
+{
+    image_common_t *common = (image_common_t *)image;
 
-    if (!scanline_buffer)
-       return;
+    return_if_fail (!alpha_map || alpha_map->type == BITS);
 
+    if (common->alpha_map != (bits_image_t *)alpha_map)
+    {
+       if (common->alpha_map)
+           pixman_image_unref ((pixman_image_t *)common->alpha_map);
+
+       if (alpha_map)
+           common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
+       else
+           common->alpha_map = NULL;
+    }
+
+    common->alpha_origin_x = x;
+    common->alpha_origin_y = y;
+
+    image_property_changed (image);
+}
+
+PIXMAN_EXPORT void
+pixman_image_set_component_alpha   (pixman_image_t       *image,
+                                   pixman_bool_t         component_alpha)
+{
+    image->common.component_alpha = component_alpha;
+
+    image_property_changed (image);
+}
+
+
+PIXMAN_EXPORT void
+pixman_image_set_accessors (pixman_image_t             *image,
+                           pixman_read_memory_func_t   read_func,
+                           pixman_write_memory_func_t  write_func)
+{
+    return_if_fail (image != NULL);
+
+    image->common.read_func = read_func;
+    image->common.write_func = write_func;
+
+    image_property_changed (image);
+}
+
+PIXMAN_EXPORT uint32_t *
+pixman_image_get_data (pixman_image_t *image)
+{
+    if (image->type == BITS)
+       return image->bits.bits;
+
+    return NULL;
+}
+
+PIXMAN_EXPORT int
+pixman_image_get_width (pixman_image_t *image)
+{
+    if (image->type == BITS)
+       return image->bits.width;
+
+    return 0;
+}
+
+PIXMAN_EXPORT int
+pixman_image_get_height (pixman_image_t *image)
+{
+    if (image->type == BITS)
+       return image->bits.height;
+
+    return 0;
+}
+
+PIXMAN_EXPORT int
+pixman_image_get_stride (pixman_image_t *image)
+{
+    if (image->type == BITS)
+       return image->bits.rowstride * (int) sizeof (uint32_t);
+
+    return 0;
+}
+
+PIXMAN_EXPORT int
+pixman_image_get_depth (pixman_image_t *image)
+{
+    if (image->type == BITS)
+       return PIXMAN_FORMAT_DEPTH (image->bits.format);
+
+    return 0;
+}
+
+pixman_bool_t
+_pixman_image_is_solid (pixman_image_t *image)
+{
+    if (image->type == SOLID)
+       return TRUE;
+
+    if (image->type != BITS    ||
+       image->bits.width != 1  ||
+       image->bits.height != 1)
+    {
+       return FALSE;
+    }
+
+    if (image->common.repeat == PIXMAN_REPEAT_NONE)
+       return FALSE;
+
+    return TRUE;
+}
+
+uint32_t
+_pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
+{
+    uint32_t result;
+    
+    _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
     
+    /* If necessary, convert RGB <--> BGR. */
+    if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
+    {
+       result = (((result & 0xff000000) >>  0) |
+                 ((result & 0x00ff0000) >> 16) |
+                 ((result & 0x0000ff00) >>  0) |
+                 ((result & 0x000000ff) << 16));
+    }                                                                  
     
-    if (scanline_buffer != _scanline_buffer)
-       free (scanline_buffer);
+    return result;                                                     
+}
+
+pixman_bool_t
+_pixman_image_is_opaque (pixman_image_t *image)
+{
+    int i;
+
+    if (image->common.alpha_map)
+        return FALSE;
+
+    switch (image->type)
+    {
+    case BITS:
+       if (image->common.repeat == PIXMAN_REPEAT_NONE)
+           return FALSE;
+       
+        if (PIXMAN_FORMAT_A (image->bits.format))
+            return FALSE;
+        break;
+
+    case LINEAR:
+    case RADIAL:
+       if (image->common.repeat == PIXMAN_REPEAT_NONE)
+           return FALSE;
+       
+       for (i = 0; i < image->gradient.n_stops; ++i)
+       {
+            if (image->gradient.stops[i].color.alpha != 0xffff)
+                return FALSE;
+        }
+        break;
+
+    case CONICAL:
+       /* Conical gradients always have a transparent border */
+       return FALSE;
+       break;
+       
+    case SOLID:
+       if (Alpha (image->solid.color) != 0xff)
+            return FALSE;
+        break;
+    }
+
+    /* Convolution filters can introduce translucency if the sum of the
+     * weights is lower than 1.
+     */
+    if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
+         return FALSE;
+
+     return TRUE;
 }