summaryrefslogtreecommitdiff
path: root/libs/cairo-1.16.0/test/png.c
blob: cd11fd0d1825ca337ef93634ae7da49cda2a496d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright © 2008 Chris Wilson
 *
 * 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
 * Chris Wilson not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Chris Wilson makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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: Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairo-test.h"

#include <assert.h>

/* Test the idempotency of write_png->read_png */

#define RGB_MASK 0x00ffffff
#define BASENAME "png.out"

static cairo_bool_t
image_surface_equals (cairo_surface_t *A, cairo_surface_t *B)
{
    if (cairo_image_surface_get_format (A) !=
	cairo_image_surface_get_format (B))
	return 0;

    if (cairo_image_surface_get_width (A) !=
	cairo_image_surface_get_width (B))
	return 0;

    if (cairo_image_surface_get_height (A) !=
	cairo_image_surface_get_height (B))
	return 0;

    return 1;
}

static const char *
format_to_string (cairo_format_t format)
{
    switch (format) {
    case CAIRO_FORMAT_A1:     return "a1";
    case CAIRO_FORMAT_A8:     return "a8";
    case CAIRO_FORMAT_RGB16_565:  return "rgb16";
    case CAIRO_FORMAT_RGB24:  return "rgb24";
    case CAIRO_FORMAT_RGB30:  return "rgb30";
    case CAIRO_FORMAT_ARGB32: return "argb32";
    case CAIRO_FORMAT_INVALID:
    default: return "???";
    }
}

static void
print_surface (const cairo_test_context_t *ctx, cairo_surface_t *surface)
{
    cairo_test_log (ctx,
		    "%s (%dx%d)\n",
		    format_to_string (cairo_image_surface_get_format (surface)),
		    cairo_image_surface_get_width (surface),
		    cairo_image_surface_get_height (surface));
}

static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    cairo_surface_t *surface0, *surface1;
    cairo_status_t status;
    uint32_t argb32 = 0xdeadbede;
    char *filename;
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";

    xasprintf (&filename, "%s/%s.png", path, BASENAME);
    surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
						    CAIRO_FORMAT_ARGB32,
						    1, 1, 4);
    status = cairo_surface_write_to_png (surface0, filename);
    if (status) {
	cairo_test_log (ctx, "Error writing '%s': %s\n",
			filename, cairo_status_to_string (status));

	cairo_surface_destroy (surface0);
	free (filename);
	return cairo_test_status_from_status (ctx, status);
    }
    surface1 = cairo_image_surface_create_from_png (filename);
    status = cairo_surface_status (surface1);
    if (status) {
	cairo_test_log (ctx, "Error reading '%s': %s\n",
			filename, cairo_status_to_string (status));

	cairo_surface_destroy (surface1);
	cairo_surface_destroy (surface0);
	free (filename);
	return cairo_test_status_from_status (ctx, status);
    }

    if (! image_surface_equals (surface0, surface1)) {
	cairo_test_log (ctx, "Error surface mismatch.\n");
	cairo_test_log (ctx, "to png: "); print_surface (ctx, surface0);
	cairo_test_log (ctx, "from png: "); print_surface (ctx, surface1);

	cairo_surface_destroy (surface0);
	cairo_surface_destroy (surface1);
	free (filename);
	return CAIRO_TEST_FAILURE;
    }
    assert (*(uint32_t *) cairo_image_surface_get_data (surface1) == argb32);

    cairo_surface_destroy (surface0);
    cairo_surface_destroy (surface1);

    surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
						    CAIRO_FORMAT_RGB24,
						    1, 1, 4);
    status = cairo_surface_write_to_png (surface0, filename);
    if (status) {
	cairo_test_log (ctx, "Error writing '%s': %s\n",
			filename, cairo_status_to_string (status));
	cairo_surface_destroy (surface0);
	return cairo_test_status_from_status (ctx, status);
    }
    surface1 = cairo_image_surface_create_from_png (filename);
    status = cairo_surface_status (surface1);
    if (status) {
	cairo_test_log (ctx, "Error reading '%s': %s\n",
			filename, cairo_status_to_string (status));
	free (filename);

	cairo_surface_destroy (surface1);
	cairo_surface_destroy (surface0);
	return cairo_test_status_from_status (ctx, status);
    }
    free (filename);

    if (! image_surface_equals (surface0, surface1)) {
	cairo_test_log (ctx, "Error surface mismatch.\n");
	cairo_test_log (ctx, "to png: "); print_surface (ctx, surface0);
	cairo_test_log (ctx, "from png: "); print_surface (ctx, surface1);

	cairo_surface_destroy (surface0);
	cairo_surface_destroy (surface1);
	return CAIRO_TEST_FAILURE;
    }
    assert ((*(uint32_t *) cairo_image_surface_get_data (surface1) & RGB_MASK)
	    == (argb32 & RGB_MASK));

    cairo_surface_destroy (surface0);
    cairo_surface_destroy (surface1);

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (png,
	    "Check that the png export/import is idempotent.",
	    "png, api", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    preamble, NULL)