From a6d9b6a671faf6cc726af12d4f4e706262c2bd6b Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 4 Apr 2005 09:47:12 +0000 Subject: [PATCH] Change type of data parameter from char* to unsigned char*. Propagate the unsigned char* change down the stack. Add cast since XImage uses char* rather than unsigned char*. Fix memory leak of image data. Switch to use cairo_surface_write_png rather than a custom write_png_argb32. Add test to exercise the cairo_image_surface_create_for_png function. --- ChangeLog | 35 ++++++++++++++++- src/cairo-ft-font.c | 2 +- src/cairo-image-surface.c | 2 +- src/cairo-pattern.c | 8 ++-- src/cairo-png.c | 9 ++++- src/cairo-xlib-surface.c | 2 +- src/cairo.c | 2 +- src/cairo.h | 4 +- test/.cvsignore | 1 + test/Makefile.am | 3 ++ test/buffer-diff.c | 6 ++- test/buffer-diff.h | 4 +- test/cairo-test.c | 14 ++++--- test/cairo-test.h | 2 + test/create-for-png-ref.png | Bin 0 -> 100 bytes test/create-for-png.c | 74 +++++++++++++++++++++++++++++++++++ test/create-from-png-ref.png | Bin 0 -> 100 bytes test/create-from-png.c | 74 +++++++++++++++++++++++++++++++++++ test/write-png.c | 2 +- test/write-png.h | 2 +- 20 files changed, 221 insertions(+), 25 deletions(-) create mode 100644 test/create-for-png-ref.png create mode 100644 test/create-for-png.c create mode 100644 test/create-from-png-ref.png create mode 100644 test/create-from-png.c diff --git a/ChangeLog b/ChangeLog index 58804d785..c50932bce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,36 @@ +2005-04-04 Carl Worth + + * src/cairo.h (cairo_set_target_image, + cairo_image_surface_create_for_data): + * src/cairo.c: (cairo_set_target_image): Change type of data + parameter from char* to unsigned char*. + + * src/cairo-ft-font.c: (_cairo_ft_font_create_glyph): + * src/cairo-image-surface.c: (cairo_image_surface_create_for_data): + * src/cairo-pattern.c: + (_cairo_pattern_acquire_surface_for_gradient): + * test/buffer-diff.c: (buffer_diff): + * test/buffer-diff.h: + * test/write-png.c: (write_png_argb32): + * test/write-png.h: Propagate the unsigned char* change down the + stack. + + * src/cairo-xlib-surface.c: (_get_image_surface): Add cast since + XImage uses char* rather than unsigned char*. + + * src/cairo-png.c: (cairo_image_surface_create_for_png): Fix + memory leak of image data. + + * test/cairo-test.c: (cairo_test), (cairo_test_create_png_pattern): + * test/cairo-test.h: Switch to use cairo_surface_write_png rather + than a custom write_png_argb32. + + * test/.cvsignore: + * test/Makefile.am: + * test/create-for-png-ref.png: + * test/create-for-png.c: (draw), (main): Add test to exercise the + cairo_image_surface_create_for_png function. + 2005-04-04 Carl Worth * TODO: Remove items for PNG backend removal and trapezoid @@ -26,7 +59,6 @@ * src/cairoint.h: Drop pixels_per_inch function from surface backend interface as it is no longer needed. ->>>>>>> 1.469 2005-04-02 Carl Worth * src/cairo-gstate.c: (_cairo_gstate_show_surface): Use the @@ -92,7 +124,6 @@ * src/cairo.c (cairo_set_target_png): Remove this function now that the PNG backend is gone. ->>>>>>> 1.468 2005-03-30 Carl Worth * configure.in: Fix typo I had introduced into Jason's patch that diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c index 644afbe13..a37374185 100644 --- a/src/cairo-ft-font.c +++ b/src/cairo-ft-font.c @@ -1273,7 +1273,7 @@ _cairo_ft_font_create_glyph (cairo_image_glyph_cache_entry_t *val) } val->image = (cairo_image_surface_t *) - cairo_image_surface_create_for_data ((char *) bitmap.buffer, + cairo_image_surface_create_for_data (bitmap.buffer, CAIRO_FORMAT_A8, width, height, stride); if (val->image == NULL) { diff --git a/src/cairo-image-surface.c b/src/cairo-image-surface.c index c0bfc3e20..51404aa84 100644 --- a/src/cairo-image-surface.c +++ b/src/cairo-image-surface.c @@ -195,7 +195,7 @@ cairo_image_surface_create (cairo_format_t format, * be created because of lack of memory **/ cairo_surface_t * -cairo_image_surface_create_for_data (char *data, +cairo_image_surface_create_for_data (unsigned char *data, cairo_format_t format, int width, int height, diff --git a/src/cairo-pattern.c b/src/cairo-pattern.c index 283c36dbd..f0e615afb 100644 --- a/src/cairo-pattern.c +++ b/src/cairo-pattern.c @@ -889,9 +889,9 @@ _cairo_pattern_acquire_surface_for_gradient (cairo_gradient_pattern_t *pattern, cairo_surface_attributes_t *attr) { cairo_image_surface_t *image; - cairo_status_t status; - uint32_t *data; - cairo_bool_t repeat = FALSE; + cairo_status_t status; + uint32_t *data; + cairo_bool_t repeat = FALSE; if (pattern->base.type == CAIRO_PATTERN_LINEAR) { cairo_bool_t is_horizontal; @@ -935,7 +935,7 @@ _cairo_pattern_acquire_surface_for_gradient (cairo_gradient_pattern_t *pattern, } image = (cairo_image_surface_t *) - cairo_image_surface_create_for_data ((char *) data, + cairo_image_surface_create_for_data ((unsigned char *) data, CAIRO_FORMAT_ARGB32, width, height, width * 4); diff --git a/src/cairo-png.c b/src/cairo-png.c index 1a4fc76ff..f6e61ce37 100644 --- a/src/cairo-png.c +++ b/src/cairo-png.c @@ -228,6 +228,7 @@ premultiply_data (png_structp png, cairo_surface_t * cairo_image_surface_create_for_png (FILE *file, int *width, int *height) { + cairo_surface_t *surface; png_byte *data; int i; static const int PNG_SIG_SIZE = 8; @@ -322,8 +323,12 @@ cairo_image_surface_create_for_png (FILE *file, int *width, int *height) if (height != NULL) *height = png_height; - return cairo_image_surface_create_for_data ((char *)data, CAIRO_FORMAT_ARGB32, - png_width, png_height, stride); + surface = cairo_image_surface_create_for_data (data, + CAIRO_FORMAT_ARGB32, + png_width, png_height, stride); + _cairo_image_surface_assume_ownership_of_data ((cairo_image_surface_t*)surface); + + return surface; BAIL3: free (data); diff --git a/src/cairo-xlib-surface.c b/src/cairo-xlib-surface.c index e1508002f..0686c5f61 100644 --- a/src/cairo-xlib-surface.c +++ b/src/cairo-xlib-surface.c @@ -282,7 +282,7 @@ _get_image_surface (cairo_xlib_surface_t *surface, ximage->bytes_per_line); } else { image = (cairo_image_surface_t *) - cairo_image_surface_create_for_data (ximage->data, + cairo_image_surface_create_for_data ((unsigned char*) ximage->data, surface->format, ximage->width, ximage->height, diff --git a/src/cairo.c b/src/cairo.c index 1f41e8cce..d6ff0ac4e 100644 --- a/src/cairo.c +++ b/src/cairo.c @@ -330,7 +330,7 @@ slim_hidden_def(cairo_set_target_surface); **/ void cairo_set_target_image (cairo_t *cr, - char *data, + unsigned char *data, cairo_format_t format, int width, int height, diff --git a/src/cairo.h b/src/cairo.h index 7cd4de779..7fbda3e3a 100644 --- a/src/cairo.h +++ b/src/cairo.h @@ -179,7 +179,7 @@ typedef enum cairo_format { */ void cairo_set_target_image (cairo_t *cr, - char *data, + unsigned char *data, cairo_format_t format, int width, int height, @@ -914,7 +914,7 @@ cairo_image_surface_create (cairo_format_t format, int height); cairo_surface_t * -cairo_image_surface_create_for_data (char *data, +cairo_image_surface_create_for_data (unsigned char *data, cairo_format_t format, int width, int height, diff --git a/test/.cvsignore b/test/.cvsignore index 2eb691234..4a9186ccd 100644 --- a/test/.cvsignore +++ b/test/.cvsignore @@ -4,6 +4,7 @@ Makefile Makefile.in clip-twice coverage +create-for-png fill-rule get-and-set imagediff diff --git a/test/Makefile.am b/test/Makefile.am index 93d9c9df8..e520152cb 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,6 +2,7 @@ TESTS = \ clip-twice \ coverage \ +create-for-png \ fill-rule \ get-and-set \ leaky-polygon \ @@ -18,6 +19,7 @@ user-data # this list. Anyone know a good way to avoid it? Can I use a wildcard # here? EXTRA_DIST = \ +create-for-png-ref.png \ fill-rule-ref.png \ leaky-polygon-ref.png \ line-width-ref.png \ @@ -73,6 +75,7 @@ LDADDS = libcairotest.la $(top_builddir)/src/libcairo.la # from autogen.sh. My, but this is painful... clip_twice_LDADD = $(LDADDS) coverage_LDADD = $(LDADDS) +create_for_png_LDADD = $(LDADDS) fill_rule_LDADD = $(LDADDS) get_and_set_LDADD = $(LDADDS) leaky_polygon_LDADD = $(LDADDS) diff --git a/test/buffer-diff.c b/test/buffer-diff.c index 486aff1d6..98b914d34 100644 --- a/test/buffer-diff.c +++ b/test/buffer-diff.c @@ -31,8 +31,10 @@ * images differ. */ int -buffer_diff (char *buf_a, char *buf_b, char *buf_diff, - int width, int height, int stride) +buffer_diff (unsigned char *buf_a, + unsigned char *buf_b, + unsigned char *buf_diff, + int width, int height, int stride) { int x, y; int total_pixels_changed = 0; diff --git a/test/buffer-diff.h b/test/buffer-diff.h index 9ee51c3cf..ab5d3bd97 100644 --- a/test/buffer-diff.h +++ b/test/buffer-diff.h @@ -32,7 +32,9 @@ * images differ. */ int -buffer_diff (char *buf_a, char *buf_b, char *buf_diff, +buffer_diff (unsigned char *buf_a, + unsigned char *buf_b, + unsigned char *buf_diff, int width, int height, int stride); #endif diff --git a/test/cairo-test.c b/test/cairo-test.c index 835bf38a0..e2f781d17 100644 --- a/test/cairo-test.c +++ b/test/cairo-test.c @@ -32,6 +32,8 @@ #include "cairo-test.h" +#include + #include "buffer-diff.h" #include "read-png.h" #include "write-png.h" @@ -42,7 +44,7 @@ #define CAIRO_TEST_REF_SUFFIX "-ref.png" #define CAIRO_TEST_DIFF_SUFFIX "-diff.png" -static void +void xasprintf (char **strp, const char *fmt, ...) { #ifdef HAVE_VASPRINTF @@ -105,7 +107,7 @@ cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw) char *log_name, *png_name, *ref_name, *diff_name; char *srcdir; int pixels_changed; - int ref_width, ref_height, ref_stride; + unsigned int ref_width, ref_height, ref_stride; read_png_status_t png_status; cairo_test_status_t ret; FILE *png_file; @@ -150,8 +152,6 @@ cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw) return CAIRO_TEST_FAILURE; } - cairo_destroy (cr); - /* Skip image check for tests with no image (width,height == 0,0) */ if (test->width == 0 || test->height == 0) { free (png_buf); @@ -160,9 +160,11 @@ cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw) } png_file = fopen (png_name, "wb"); - write_png_argb32 (png_buf, png_file, test->width, test->height, stride); + cairo_surface_write_png (cairo_get_target_surface (cr), png_file); fclose (png_file); + cairo_destroy (cr); + ref_buf = NULL; png_status = (read_png_argb32 (ref_name, &ref_buf, &ref_width, &ref_height, &ref_stride)); if (png_status) { @@ -236,7 +238,7 @@ cairo_test_create_png_pattern (cairo_t *cr, const char *filename) cairo_surface_t *image; cairo_pattern_t *pattern; unsigned char *buffer; - int w, h, stride; + unsigned int w, h, stride; read_png_status_t status; char *srcdir = getenv ("srcdir"); diff --git a/test/cairo-test.h b/test/cairo-test.h index cc18a7931..6bf9111d3 100644 --- a/test/cairo-test.h +++ b/test/cairo-test.h @@ -50,6 +50,8 @@ cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw); cairo_pattern_t * cairo_test_create_png_pattern (cairo_t *cr, const char *filename); +void +xasprintf (char **strp, const char *fmt, ...); #endif diff --git a/test/create-for-png-ref.png b/test/create-for-png-ref.png new file mode 100644 index 0000000000000000000000000000000000000000..765adc4a48835d273043c0d18be61eda17d88088 GIT binary patch literal 100 zcmeAS@N?(olHy`uVBq!ia0vp^Od!m`1|*BN@u~nRwj^(N7l!{JxM1({$v_cFPZ!6K uiaD$Y8F?8Pcn&K}yR-kPT!^c=vs~C)c7~6?-pvInV(@hJb6Mw<&;$Tgh#N!z literal 0 HcmV?d00001 diff --git a/test/create-for-png.c b/test/create-for-png.c new file mode 100644 index 000000000..41223f728 --- /dev/null +++ b/test/create-for-png.c @@ -0,0 +1,74 @@ +/* + * Copyright © 2005 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, Inc. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Red Hat, Inc. makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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: Carl Worth + */ + +#include "cairo-test.h" + +#include +#include + +#define WIDTH 2 +#define HEIGHT 2 + +cairo_test_t test = { + "create-for-png", + "Tests the creation of an image surface from a PNG file", + WIDTH, HEIGHT +}; + +static cairo_test_status_t +draw (cairo_t *cr, int width, int height) +{ + char *srcdir = getenv ("srcdir"); + char *filename; + FILE *file; + cairo_surface_t *surface; + int surface_width, surface_height; + + xasprintf (&filename, "%s/%s", srcdir ? srcdir : ".", + "create-for-png-ref.png"); + file = fopen (filename, "r"); + if (file == NULL) { + fprintf (stderr, "Error: failed to open file %s\n", filename); + free (filename); + return CAIRO_TEST_FAILURE; + } + free (filename); + + surface = cairo_image_surface_create_for_png (file, + &surface_width, + &surface_height); + + cairo_show_surface (cr, surface, surface_width, surface_height); + + cairo_surface_destroy (surface); + + return CAIRO_TEST_SUCCESS; +} + +int +main (void) +{ + return cairo_test (&test, draw); +} diff --git a/test/create-from-png-ref.png b/test/create-from-png-ref.png new file mode 100644 index 0000000000000000000000000000000000000000..765adc4a48835d273043c0d18be61eda17d88088 GIT binary patch literal 100 zcmeAS@N?(olHy`uVBq!ia0vp^Od!m`1|*BN@u~nRwj^(N7l!{JxM1({$v_cFPZ!6K uiaD$Y8F?8Pcn&K}yR-kPT!^c=vs~C)c7~6?-pvInV(@hJb6Mw<&;$Tgh#N!z literal 0 HcmV?d00001 diff --git a/test/create-from-png.c b/test/create-from-png.c new file mode 100644 index 000000000..41223f728 --- /dev/null +++ b/test/create-from-png.c @@ -0,0 +1,74 @@ +/* + * Copyright © 2005 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, Inc. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Red Hat, Inc. makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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: Carl Worth + */ + +#include "cairo-test.h" + +#include +#include + +#define WIDTH 2 +#define HEIGHT 2 + +cairo_test_t test = { + "create-for-png", + "Tests the creation of an image surface from a PNG file", + WIDTH, HEIGHT +}; + +static cairo_test_status_t +draw (cairo_t *cr, int width, int height) +{ + char *srcdir = getenv ("srcdir"); + char *filename; + FILE *file; + cairo_surface_t *surface; + int surface_width, surface_height; + + xasprintf (&filename, "%s/%s", srcdir ? srcdir : ".", + "create-for-png-ref.png"); + file = fopen (filename, "r"); + if (file == NULL) { + fprintf (stderr, "Error: failed to open file %s\n", filename); + free (filename); + return CAIRO_TEST_FAILURE; + } + free (filename); + + surface = cairo_image_surface_create_for_png (file, + &surface_width, + &surface_height); + + cairo_show_surface (cr, surface, surface_width, surface_height); + + cairo_surface_destroy (surface); + + return CAIRO_TEST_SUCCESS; +} + +int +main (void) +{ + return cairo_test (&test, draw); +} diff --git a/test/write-png.c b/test/write-png.c index a5494b461..f5a274738 100644 --- a/test/write-png.c +++ b/test/write-png.c @@ -55,7 +55,7 @@ unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data) } void -write_png_argb32 (char *buffer, FILE *file, +write_png_argb32 (unsigned char *buffer, FILE *file, int width, int height, int stride) { int i; diff --git a/test/write-png.h b/test/write-png.h index fe0e92b20..8074666a3 100644 --- a/test/write-png.h +++ b/test/write-png.h @@ -29,7 +29,7 @@ #define WRITE_PNG_H void -write_png_argb32 (char *buffer, FILE * file, +write_png_argb32 (unsigned char *buffer, FILE * file, int width, int height, int stride); #endif