From 06b375aee999220ce294c22fa50a3040c19d5492 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 29 Feb 2008 11:55:01 +0000 Subject: [PATCH] [cairo-png] Use cairo_format_stride_for_width() Use cairo_format_stride_for_width() instead of assuming the pixel size and manually calculating the row stride. This should make it easier to support loading multiple image formats in future. --- src/cairo-image-surface.c | 1 + src/cairo-png.c | 16 ++++++++++------ src/cairoint.h | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cairo-image-surface.c b/src/cairo-image-surface.c index 8247e444e..28e3ce223 100644 --- a/src/cairo-image-surface.c +++ b/src/cairo-image-surface.c @@ -440,6 +440,7 @@ cairo_format_stride_for_width (cairo_format_t format, return ((bpp*width+7)/8 + STRIDE_ALIGNMENT-1) & ~(STRIDE_ALIGNMENT-1); } +slim_hidden_def (cairo_format_stride_for_width); /** * cairo_image_surface_create_for_data: diff --git a/src/cairo-png.c b/src/cairo-png.c index 103061841..de09086ab 100644 --- a/src/cairo-png.c +++ b/src/cairo-png.c @@ -399,10 +399,9 @@ read_png (png_rw_ptr read_func, png_info *info; png_byte *data = NULL; png_byte **row_pointers = NULL; - png_uint_32 png_width, png_height, stride; - int depth, color_type, interlace; + png_uint_32 png_width, png_height; + int depth, color_type, interlace, stride; unsigned int i; - unsigned int pixel_size; cairo_status_t status; /* XXX: Perhaps we'll want some other error handlers? */ @@ -476,8 +475,13 @@ read_png (png_rw_ptr read_func, png_read_update_info (png, info); - pixel_size = 4; - data = _cairo_malloc_abc (png_height, png_width, pixel_size); + stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, png_width); + if (stride < 0) { + surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE)); + goto BAIL; + } + + data = _cairo_malloc_ab (png_height, stride); if (data == NULL) { surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY)); goto BAIL; @@ -490,7 +494,7 @@ read_png (png_rw_ptr read_func, } for (i = 0; i < png_height; i++) - row_pointers[i] = &data[i * png_width * pixel_size]; + row_pointers[i] = &data[i * stride]; png_read_image (png, row_pointers); png_read_end (png, info); diff --git a/src/cairoint.h b/src/cairoint.h index 773daf6d4..022033c20 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -2244,6 +2244,7 @@ slim_hidden_proto (cairo_image_surface_create); slim_hidden_proto (cairo_image_surface_create_for_data); slim_hidden_proto (cairo_image_surface_get_height); slim_hidden_proto (cairo_image_surface_get_width); +slim_hidden_proto (cairo_format_stride_for_width); slim_hidden_proto (cairo_line_to); slim_hidden_proto (cairo_mask); slim_hidden_proto (cairo_matrix_init);