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.
This commit is contained in:
Carl Worth 2005-04-04 09:47:12 +00:00
parent 770d4c55b4
commit a6d9b6a671
20 changed files with 221 additions and 25 deletions

View file

@ -1,3 +1,36 @@
2005-04-04 Carl Worth <cworth@cworth.org>
* 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 <cworth@cworth.org>
* 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 <cworth@cworth.org>
* 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 <cworth@cworth.org>
* configure.in: Fix typo I had introduced into Jason's patch that

View file

@ -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) {

View file

@ -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,

View file

@ -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);

View file

@ -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);

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -4,6 +4,7 @@ Makefile
Makefile.in
clip-twice
coverage
create-for-png
fill-rule
get-and-set
imagediff

View file

@ -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)

View file

@ -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;

View file

@ -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

View file

@ -32,6 +32,8 @@
#include "cairo-test.h"
#include <cairo-png.h>
#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");

View file

@ -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

BIN
test/create-for-png-ref.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 B

74
test/create-for-png.c Normal file
View file

@ -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 <cworth@cworth.org>
*/
#include "cairo-test.h"
#include <stdlib.h>
#include <cairo-png.h>
#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);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 B

74
test/create-from-png.c Normal file
View file

@ -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 <cworth@cworth.org>
*/
#include "cairo-test.h"
#include <stdlib.h>
#include <cairo-png.h>
#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);
}

View file

@ -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;

View file

@ -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