Commit 3a8d835b authored by Paolo Bacchilega's avatar Paolo Bacchilega

cairo metadata: use a flag to check whether a value is valid

parent bb99603c
......@@ -283,7 +283,7 @@ _cairo_image_surface_create_from_jpeg (GInputStream *istream,
}
metadata = _cairo_image_surface_get_metadata (surface);
metadata->has_alpha = FALSE;
_cairo_metadata_set_has_alpha (metadata, FALSE);
surface_data = _cairo_image_surface_flush_and_get_data (surface);
surface_row = surface_data + line_start;
......@@ -554,8 +554,7 @@ _cairo_image_surface_create_from_jpeg (GInputStream *istream,
original_height = srcinfo.image_height;
}
metadata->original_width = original_width;
metadata->original_height = original_height;
_cairo_metadata_set_original_size (metadata, original_width, original_height);
if (original_width_p != NULL)
*original_width_p = original_width;
......
......@@ -204,9 +204,8 @@ _cairo_image_surface_create_from_png (GInputStream *istream,
}
metadata = _cairo_image_surface_get_metadata (cairo_png_data->surface);
metadata->has_alpha = (color_type & PNG_COLOR_MASK_ALPHA);
metadata->original_width = width;
metadata->original_height = height;
_cairo_metadata_set_has_alpha (metadata, (color_type & PNG_COLOR_MASK_ALPHA));
_cairo_metadata_set_original_size (metadata, width, height);
/* Set the data transformations */
......
......@@ -250,9 +250,8 @@ _cairo_image_surface_create_from_tiff (GInputStream *istream,
}
metadata = _cairo_image_surface_get_metadata (surface);
metadata->has_alpha = (extrasamples == 1) || (spp == 4);
metadata->original_width = max_width;
metadata->original_width = max_height;
_cairo_metadata_set_has_alpha (metadata, (extrasamples == 1) || (spp == 4));
_cairo_metadata_set_original_size (metadata, max_width, max_height);
raster = (uint32*) _TIFFmalloc (image_width * image_height * sizeof (uint32));
if (raster == NULL) {
......
......@@ -81,7 +81,7 @@ _cairo_image_surface_create_from_webp (GInputStream *istream,
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
metadata = _cairo_image_surface_get_metadata (surface);
metadata->has_alpha = (config.input.has_alpha);
_cairo_metadata_set_has_alpha (metadata, config.input.has_alpha);
config.options.no_fancy_upsampling = 1;
......
......@@ -533,10 +533,8 @@ _cairo_image_surface_scale (cairo_surface_t *image,
scaled_height);
_cairo_image_surface_copy_metadata (image, scaled);
metadata = _cairo_image_surface_get_metadata (scaled);
if (metadata->original_width <= 0) {
metadata->original_width = src_width;
metadata->original_height = src_height;
}
if (metadata->original_width <= 0)
_cairo_metadata_set_original_size (metadata, src_width, src_height);
if (scaled == NULL)
return NULL;
......
......@@ -99,6 +99,37 @@ _gdk_rgba_to_cairo_color_255 (GdkRGBA *g_color,
}
void
_cairo_metadata_set_has_alpha (cairo_surface_metadata_t *metadata,
gboolean has_alpha)
{
metadata->valid_data |= _CAIRO_METADATA_FLAG_HAS_ALPHA;
metadata->has_alpha = has_alpha;
}
void
_cairo_metadata_set_original_size (cairo_surface_metadata_t *metadata,
int width,
int height)
{
metadata->valid_data |= _CAIRO_METADATA_FLAG_ORIGINAL_SIZE;
metadata->original_width = width;
metadata->original_height = height;
}
void
_cairo_metadata_set_thumbnail_size (cairo_surface_metadata_t *metadata,
int width,
int height)
{
metadata->valid_data |= _CAIRO_METADATA_FLAG_THUMBNAIL_SIZE;
metadata->thumbnail.image_width = width;
metadata->thumbnail.image_height = height;
}
void
_cairo_clear_surface (cairo_surface_t **surface)
{
......@@ -123,6 +154,7 @@ _cairo_image_surface_flush_and_get_data (cairo_surface_t *surface)
static void
_cairo_surface_metadata_init (cairo_surface_metadata_t *metadata)
{
metadata->valid_data = _CAIRO_METADATA_FLAG_NONE;
metadata->has_alpha = FALSE;
metadata->original_width = 0;
metadata->original_height = 0;
......@@ -157,6 +189,7 @@ _cairo_image_surface_copy_metadata (cairo_surface_t *src,
src_metadata = _cairo_image_surface_get_metadata (src);
dest_metadata = _cairo_image_surface_get_metadata (dest);
dest_metadata->valid_data = src_metadata->valid_data;
dest_metadata->has_alpha = src_metadata->has_alpha;
dest_metadata->original_width = src_metadata->original_width;
dest_metadata->original_height = src_metadata->original_height;
......@@ -180,15 +213,40 @@ gboolean
_cairo_image_surface_get_has_alpha (cairo_surface_t *surface)
{
cairo_surface_metadata_t *metadata;
int width;
int height;
int row_stride;
guchar *row;
int h, w;
if (surface == NULL)
return FALSE;
metadata = cairo_surface_get_user_data (surface, &surface_metadata_key);
if (metadata != NULL)
if ((metadata != NULL) && (metadata->valid_data & _CAIRO_METADATA_FLAG_HAS_ALPHA))
return metadata->has_alpha;
return cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32;
if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32)
return FALSE;
/* search an alpha value lower than 255 */
width = cairo_image_surface_get_width (surface);
height = cairo_image_surface_get_height (surface);
row_stride = cairo_image_surface_get_stride (surface);
row = _cairo_image_surface_flush_and_get_data (surface);
for (h = 0; h < height; h++) {
guchar *pixel = row;
for (w = 0; w < width; w++) {
if (pixel[CAIRO_ALPHA] < 255)
return TRUE;
pixel += 4;
}
row += row_stride;
}
return FALSE;
}
......@@ -206,7 +264,7 @@ _cairo_image_surface_get_original_size (cairo_surface_t *surface,
if (metadata == NULL)
return FALSE;
if ((metadata->original_width <= 0) || (metadata->original_height <= 0))
if ((metadata->valid_data & _CAIRO_METADATA_FLAG_ORIGINAL_SIZE) == 0)
return FALSE;
if (original_width)
......@@ -339,7 +397,7 @@ _cairo_image_surface_create_from_pixbuf (GdkPixbuf *pixbuf)
s_pixels = _cairo_image_surface_flush_and_get_data (surface);
metadata = _cairo_image_surface_get_metadata (surface);
metadata->has_alpha = (p_n_channels == 4);
_cairo_metadata_set_has_alpha (metadata, (p_n_channels == 4));
if (p_n_channels == 4) {
guchar *s_iter;
......
......@@ -135,7 +135,15 @@ typedef struct {
int image_height;
} thumbnail_metadata_t;
typedef enum {
_CAIRO_METADATA_FLAG_NONE = 0,
_CAIRO_METADATA_FLAG_HAS_ALPHA = 1 << 0,
_CAIRO_METADATA_FLAG_ORIGINAL_SIZE = 1 << 1,
_CAIRO_METADATA_FLAG_THUMBNAIL_SIZE = 1 << 2
} cairo_metadata_flags_t;
typedef struct {
cairo_metadata_flags_t valid_data;
gboolean has_alpha;
int original_width;
int original_height;
......@@ -161,6 +169,17 @@ void _gdk_color_to_cairo_color_255 (GdkColor
void _gdk_rgba_to_cairo_color_255 (GdkRGBA *g_color,
cairo_color_255_t *c_color);
/* metadata */
void _cairo_metadata_set_has_alpha (cairo_surface_metadata_t *metadata,
gboolean has_alpha);
void _cairo_metadata_set_original_size (cairo_surface_metadata_t *metadata,
int width,
int height);
void _cairo_metadata_set_thumbnail_size (cairo_surface_metadata_t *metadata,
int width,
int height);
/* surface */
void _cairo_clear_surface (cairo_surface_t **surface);
......
......@@ -214,9 +214,8 @@ gth_pixbuf_new_from_file (GInputStream *istream,
surface = _cairo_image_surface_create_from_pixbuf (pixbuf);
metadata = _cairo_image_surface_get_metadata (surface);
metadata->original_width = scale_data.original_width;
metadata->original_height = scale_data.original_height;
metadata->has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
_cairo_metadata_set_has_alpha (metadata, gdk_pixbuf_get_has_alpha (pixbuf));
_cairo_metadata_set_original_size (metadata, scale_data.original_width, scale_data.original_height);
gth_image_set_cairo_surface (image, surface);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment