2005-04-04 09:47:12 +00:00
|
|
|
/*
|
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
|
|
#define WIDTH 2
|
|
|
|
|
#define HEIGHT 2
|
|
|
|
|
|
2007-09-25 23:35:25 +01:00
|
|
|
static cairo_status_t
|
|
|
|
|
no_memory_error (void *closure, unsigned char *data, unsigned int size)
|
|
|
|
|
{
|
|
|
|
|
return CAIRO_STATUS_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static cairo_status_t
|
|
|
|
|
read_error (void *closure, unsigned char *data, unsigned int size)
|
|
|
|
|
{
|
|
|
|
|
return CAIRO_STATUS_READ_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-04 09:47:12 +00:00
|
|
|
static cairo_test_status_t
|
|
|
|
|
draw (cairo_t *cr, int width, int height)
|
|
|
|
|
{
|
2008-08-11 21:12:45 +01:00
|
|
|
const cairo_test_context_t *ctx = cairo_test_get_context (cr);
|
2005-04-04 09:47:12 +00:00
|
|
|
char *filename;
|
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/reference/%s",
|
|
|
|
|
ctx->srcdir, "create-from-png.ref.png");
|
2005-04-25 20:42:54 +00:00
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
2005-07-28 10:41:08 +00:00
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
cairo_test_status_t result;
|
|
|
|
|
|
|
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-27 09:23:29 +00:00
|
|
|
free (filename);
|
2009-03-30 13:41:00 +01:00
|
|
|
return result;
|
2005-04-04 09:47:12 +00:00
|
|
|
}
|
2005-07-27 09:23:29 +00:00
|
|
|
|
2011-10-08 13:37:20 +02:00
|
|
|
/* Pretend we modify the surface data (which detaches the PNG mime data) */
|
|
|
|
|
cairo_surface_flush (surface);
|
|
|
|
|
cairo_surface_mark_dirty (surface);
|
|
|
|
|
|
2005-05-03 08:33:32 +00:00
|
|
|
cairo_set_source_surface (cr, surface, 0, 0);
|
2008-04-05 13:32:51 +10:30
|
|
|
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
|
2005-05-03 08:33:32 +00:00
|
|
|
cairo_paint (cr);
|
2005-04-04 09:47:12 +00:00
|
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
2009-03-30 13:41:00 +01:00
|
|
|
free (filename);
|
2005-04-04 09:47:12 +00:00
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 16:38:03 +01:00
|
|
|
static cairo_test_status_t
|
|
|
|
|
preamble (cairo_test_context_t *ctx)
|
2005-04-04 09:47:12 +00:00
|
|
|
{
|
2007-09-25 23:35:25 +01:00
|
|
|
char *filename;
|
2011-09-23 13:20:14 +01:00
|
|
|
char *path;
|
2007-09-25 23:35:25 +01:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
cairo_status_t status;
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_status_t result = CAIRO_TEST_SUCCESS;
|
|
|
|
|
|
2007-09-25 23:35:25 +01:00
|
|
|
surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
|
|
|
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: expected \"file not found\", but got: %s\n",
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2007-09-25 23:35:25 +01:00
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2007-09-25 23:35:25 +01:00
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
|
|
|
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2007-09-25 23:35:25 +01:00
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2007-09-25 23:35:25 +01:00
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
|
|
|
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_READ_ERROR) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2007-09-25 23:35:25 +01:00
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2007-09-25 23:35:25 +01:00
|
|
|
|
|
|
|
|
/* cheekily test error propagation from the user write funcs as well ... */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&path, "%s/reference", ctx->srcdir);
|
|
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
|
2007-09-25 23:35:25 +01:00
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else {
|
|
|
|
|
status = cairo_surface_write_to_png_stream (surface,
|
|
|
|
|
(cairo_write_func_t) no_memory_error,
|
|
|
|
|
NULL);
|
|
|
|
|
if (status != CAIRO_STATUS_NO_MEMORY) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx, status);
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
|
|
|
|
|
cairo_status_to_string (status));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
}
|
2007-09-25 23:35:25 +01:00
|
|
|
|
2008-08-11 21:12:45 +01:00
|
|
|
status = cairo_surface_write_to_png_stream (surface,
|
|
|
|
|
(cairo_write_func_t) read_error,
|
|
|
|
|
NULL);
|
|
|
|
|
if (status != CAIRO_STATUS_READ_ERROR) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx, status);
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
|
|
|
|
|
cairo_status_to_string (status));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* and check that error has not propagated to the surface */
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error: user write error propagated to surface: %s",
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
}
|
2007-09-25 23:35:25 +01:00
|
|
|
}
|
|
|
|
|
cairo_surface_destroy (surface);
|
2008-08-11 21:12:45 +01:00
|
|
|
free (filename);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2007-09-25 23:35:25 +01:00
|
|
|
|
2008-04-21 19:44:11 +01:00
|
|
|
/* check that loading alpha/opaque PNGs generate the correct surfaces */
|
2013-07-03 22:37:41 +00:00
|
|
|
/* TODO: Avoid using target-specific references as sample images */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.alpha.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2008-04-21 19:44:11 +01:00
|
|
|
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2008-04-21 19:44:11 +01:00
|
|
|
|
|
|
|
|
/* check paletted PNGs */
|
2013-07-03 22:37:41 +00:00
|
|
|
/* TODO: Avoid using target-specific references as sample images */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.indexed-alpha.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2008-04-21 19:44:11 +01:00
|
|
|
|
2013-07-03 22:37:41 +00:00
|
|
|
/* TODO: Avoid using target-specific references as sample images */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.indexed.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2008-04-21 19:44:11 +01:00
|
|
|
|
|
|
|
|
/* check grayscale PNGs */
|
2013-07-03 22:37:41 +00:00
|
|
|
/* TODO: Avoid using target-specific references as sample images */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.gray-alpha.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
2009-03-30 13:41:00 +01:00
|
|
|
if (result != CAIRO_TEST_SUCCESS)
|
|
|
|
|
return result;
|
2008-04-21 19:44:11 +01:00
|
|
|
|
2013-07-03 22:37:41 +00:00
|
|
|
/* TODO: Avoid using target-specific references as sample images */
|
2011-09-23 13:20:14 +01:00
|
|
|
xasprintf (&filename, "%s/%s", path, "create-from-png.gray.ref.png");
|
2008-04-21 19:44:11 +01:00
|
|
|
surface = cairo_image_surface_create_from_png (filename);
|
|
|
|
|
if (cairo_surface_status (surface)) {
|
2009-03-30 13:41:00 +01:00
|
|
|
result = cairo_test_status_from_status (ctx,
|
|
|
|
|
cairo_surface_status (surface));
|
|
|
|
|
if (result == CAIRO_TEST_FAILURE) {
|
|
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
|
|
|
|
|
filename,
|
|
|
|
|
cairo_status_to_string (cairo_surface_status (surface)));
|
|
|
|
|
}
|
2008-08-11 21:12:45 +01:00
|
|
|
} else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
|
2008-09-03 16:38:03 +01:00
|
|
|
cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
|
2008-08-11 21:12:45 +01:00
|
|
|
filename);
|
|
|
|
|
result = CAIRO_TEST_FAILURE;
|
2008-04-21 19:44:11 +01:00
|
|
|
}
|
|
|
|
|
free (filename);
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
2011-09-23 13:20:14 +01:00
|
|
|
free (path);
|
|
|
|
|
|
2008-09-03 16:38:03 +01:00
|
|
|
return result;
|
2005-04-04 09:47:12 +00:00
|
|
|
}
|
2008-09-03 16:38:03 +01:00
|
|
|
|
|
|
|
|
CAIRO_TEST (create_from_png,
|
|
|
|
|
"Tests the creation of an image surface from a PNG file",
|
|
|
|
|
"png", /* keywords */
|
|
|
|
|
NULL, /* requirements */
|
|
|
|
|
WIDTH, HEIGHT,
|
|
|
|
|
preamble, draw)
|