1
/*
2
 * Copyright © 2005 Red Hat, Inc.
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Red Hat, Inc. makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Carl Worth <cworth@cworth.org>
24
 */
25

            
26
#include "cairo-test.h"
27

            
28
#include <stdlib.h>
29
#include <stdio.h>
30
#include <errno.h>
31

            
32
#define WIDTH 2
33
#define HEIGHT 2
34

            
35
static cairo_status_t
36
36
read_png_from_file (void *closure, unsigned char *data, unsigned int length)
37
{
38
36
    FILE *file = closure;
39
    size_t bytes_read;
40

            
41
36
    bytes_read = fread (data, 1, length, file);
42
36
    if (bytes_read != length)
43
	return CAIRO_STATUS_READ_ERROR;
44

            
45
36
    return CAIRO_STATUS_SUCCESS;
46
}
47

            
48
static cairo_test_status_t
49
3
draw (cairo_t *cr, int width, int height)
50
{
51
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
52
    char *filename;
53
    FILE *file;
54
    cairo_surface_t *surface;
55
    cairo_status_t status;
56

            
57
3
    xasprintf (&filename, "%s/reference/%s", ctx->srcdir,
58
	       "create-from-png-stream.ref.png");
59

            
60
3
    file = fopen (filename, "rb");
61
3
    if (file == NULL) {
62
	cairo_test_status_t ret;
63

            
64
	ret = CAIRO_TEST_FAILURE;
65
	if (errno == ENOMEM)
66
	    ret = cairo_test_status_from_status (ctx, CAIRO_STATUS_NO_MEMORY);
67

            
68
	if (ret != CAIRO_TEST_NO_MEMORY)
69
	    cairo_test_log (ctx, "Error: failed to open file: %s\n", filename);
70

            
71
	free (filename);
72
	return ret;
73
    }
74

            
75
3
    surface = cairo_image_surface_create_from_png_stream (read_png_from_file,
76
							  file);
77

            
78
3
    fclose (file);
79

            
80
3
    status = cairo_surface_status (surface);
81
3
    if (status) {
82
	cairo_test_status_t ret;
83

            
84
	cairo_surface_destroy (surface);
85

            
86
	ret = cairo_test_status_from_status (ctx, status);
87
	if (ret != CAIRO_TEST_NO_MEMORY) {
88
	    cairo_test_log (ctx,
89
			    "Error: failed to create surface from PNG: %s - %s\n",
90
			    filename,
91
			    cairo_status_to_string (status));
92
	}
93

            
94
	free (filename);
95

            
96
	return ret;
97
    }
98

            
99
3
    free (filename);
100

            
101
    /* Pretend we modify the surface data (which detaches the PNG mime data) */
102
3
    cairo_surface_flush (surface);
103
3
    cairo_surface_mark_dirty (surface);
104

            
105
3
    cairo_set_source_surface (cr, surface, 0, 0);
106
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
107
3
    cairo_paint (cr);
108

            
109
3
    cairo_surface_destroy (surface);
110

            
111
3
    return CAIRO_TEST_SUCCESS;
112
}
113

            
114
1
CAIRO_TEST (create_from_png_stream,
115
	    "Tests the creation of an image surface from a PNG using a FILE *",
116
	    "png", /* keywords */
117
	    NULL, /* requirements */
118
	    WIDTH, HEIGHT,
119
	    NULL, draw)