1 | /* GdkPixbuf library - test compositing |
2 | * |
3 | * Copyright (C) 2015 Red Hat, Inc. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
17 | * |
18 | * Author: Matthias Clasen |
19 | */ |
20 | |
21 | #include <gdk-pixbuf.h> |
22 | #include "test-common.h" |
23 | |
24 | static void |
25 | test_composite1 (void) |
26 | { |
27 | GdkPixbuf *red, *green, *out, *ref, *sub; |
28 | |
29 | red = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, FALSE, bits_per_sample: 8, width: 24, height: 24); |
30 | gdk_pixbuf_fill (pixbuf: red, pixel: 0xff000000); |
31 | |
32 | green = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, FALSE, bits_per_sample: 8, width: 12, height: 12); |
33 | gdk_pixbuf_fill (pixbuf: green, pixel: 0x00ff0000); |
34 | |
35 | out = gdk_pixbuf_copy (pixbuf: red); |
36 | |
37 | gdk_pixbuf_composite (src: green, dest: out, |
38 | dest_x: 0, dest_y: 0, dest_width: 12, dest_height: 12, |
39 | offset_x: 0, offset_y: 0, scale_x: 1.0, scale_y: 1.0, |
40 | interp_type: GDK_INTERP_NEAREST, |
41 | overall_alpha: 255); |
42 | |
43 | ref = gdk_pixbuf_copy (pixbuf: red); |
44 | sub = gdk_pixbuf_new_subpixbuf (src_pixbuf: ref, src_x: 0, src_y: 0, width: 12, height: 12); |
45 | |
46 | gdk_pixbuf_fill (pixbuf: sub, pixel: 0x00ff0000); |
47 | |
48 | g_assert (pixdata_equal (out, ref, NULL)); |
49 | |
50 | g_object_unref (object: red); |
51 | g_object_unref (object: green); |
52 | g_object_unref (object: out); |
53 | g_object_unref (object: ref); |
54 | g_object_unref (object: sub); |
55 | } |
56 | |
57 | /* |
58 | * Test for Bug 766842 |
59 | * https://bugzilla.gnome.org/show_bug.cgi?id=766842 |
60 | */ |
61 | static void |
62 | test_composite2 (void) |
63 | { |
64 | GdkPixbuf *src, *dest; |
65 | guchar *pixels, *p; |
66 | |
67 | char *filename = g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-image.png" , NULL); |
68 | if (!format_supported (filename)) |
69 | { |
70 | g_test_skip (msg: "PNG format not supported" ); |
71 | return; |
72 | } |
73 | |
74 | src = gdk_pixbuf_new_from_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-image.png" , NULL), NULL); |
75 | |
76 | { |
77 | GdkPixbuf *tmp = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, |
78 | TRUE, |
79 | bits_per_sample: gdk_pixbuf_get_bits_per_sample (pixbuf: src), |
80 | width: gdk_pixbuf_get_width (pixbuf: src), |
81 | height: gdk_pixbuf_get_height (pixbuf: src)); |
82 | gdk_pixbuf_fill (pixbuf: tmp, pixel: 0x00ccccff); |
83 | gdk_pixbuf_composite (src, dest: tmp, |
84 | dest_x: 0, dest_y: 0, dest_width: gdk_pixbuf_get_width (pixbuf: tmp), dest_height: gdk_pixbuf_get_height (pixbuf: tmp), |
85 | offset_x: 0., offset_y: 0., scale_x: 1., scale_y: 1., |
86 | interp_type: GDK_INTERP_NEAREST, overall_alpha: 255); |
87 | g_object_unref (object: src); |
88 | src = tmp; |
89 | } |
90 | |
91 | pixels = gdk_pixbuf_get_pixels (pixbuf: src); |
92 | p = pixels; |
93 | p[0] = 0xff; |
94 | p[1] = 0x00; |
95 | p[2] = 0xff; |
96 | p = pixels + (gsize)((gdk_pixbuf_get_height (pixbuf: src) - 1) * gdk_pixbuf_get_rowstride (pixbuf: src)) + (gsize)((gdk_pixbuf_get_width (pixbuf: src) - 1) * gdk_pixbuf_get_n_channels (pixbuf: src)); |
97 | p[0] = 0xff; |
98 | p[1] = 0xff; |
99 | p[2] = 0x00; |
100 | |
101 | dest = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, |
102 | TRUE, |
103 | bits_per_sample: gdk_pixbuf_get_bits_per_sample (pixbuf: src), |
104 | width: gdk_pixbuf_get_width (pixbuf: src) + 80, |
105 | height: gdk_pixbuf_get_height (pixbuf: src) + 80); |
106 | gdk_pixbuf_fill (pixbuf: dest, pixel: 0xffffffff); |
107 | gdk_pixbuf_composite (src, dest, |
108 | dest_x: 0, dest_y: 0, dest_width: gdk_pixbuf_get_width (pixbuf: dest), dest_height: gdk_pixbuf_get_height (pixbuf: dest), |
109 | offset_x: 10.0, offset_y: 10.0, scale_x: 1.0, scale_y: 1.0, |
110 | interp_type: GDK_INTERP_NEAREST, overall_alpha: 255); |
111 | |
112 | pixels = gdk_pixbuf_get_pixels (pixbuf: dest); |
113 | p = pixels; |
114 | g_assert_cmpint (p[0], ==, 0xff); |
115 | g_assert_cmpint (p[1], ==, 0x00); |
116 | g_assert_cmpint (p[2], ==, 0xff); |
117 | p = pixels + (gsize)((gdk_pixbuf_get_height (pixbuf: dest) - 1) * gdk_pixbuf_get_rowstride (pixbuf: dest)) + (gsize)((gdk_pixbuf_get_width (pixbuf: dest) - 1) * gdk_pixbuf_get_n_channels (pixbuf: dest)); |
118 | g_assert_cmpint (p[0], ==, 0xff); |
119 | g_assert_cmpint (p[1], ==, 0xff); |
120 | g_assert_cmpint (p[2], ==, 0x00); |
121 | |
122 | g_object_unref (object: dest); |
123 | |
124 | /* now try compositing into a pixbuf that is 1px less in width and height */ |
125 | dest = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, |
126 | TRUE, |
127 | bits_per_sample: gdk_pixbuf_get_bits_per_sample (pixbuf: src), |
128 | width: gdk_pixbuf_get_width (pixbuf: src) - 1, |
129 | height: gdk_pixbuf_get_height (pixbuf: src) - 1); |
130 | gdk_pixbuf_fill (pixbuf: dest, pixel: 0xffffffff); |
131 | gdk_pixbuf_composite (src, dest, |
132 | dest_x: 0, dest_y: 0, dest_width: gdk_pixbuf_get_width (pixbuf: dest), dest_height: gdk_pixbuf_get_height (pixbuf: dest), |
133 | offset_x: -1.0, offset_y: -2.0, scale_x: 1.0, scale_y: 1.0, |
134 | interp_type: GDK_INTERP_NEAREST, overall_alpha: 255); |
135 | |
136 | pixels = gdk_pixbuf_get_pixels (pixbuf: dest); |
137 | p = pixels + (gsize)((gdk_pixbuf_get_height (pixbuf: dest) - 2) * gdk_pixbuf_get_rowstride (pixbuf: dest)) + (gsize)((gdk_pixbuf_get_width (pixbuf: dest) - 1) * gdk_pixbuf_get_n_channels (pixbuf: dest)); |
138 | g_assert_cmpint (p[0], ==, 0xff); |
139 | g_assert_cmpint (p[1], ==, 0xff); |
140 | g_assert_cmpint (p[2], ==, 0x00); |
141 | p = pixels + (gsize)((gdk_pixbuf_get_height (pixbuf: dest) - 1) * gdk_pixbuf_get_rowstride (pixbuf: dest)) + (gsize)((gdk_pixbuf_get_width (pixbuf: dest) - 1) * gdk_pixbuf_get_n_channels (pixbuf: dest)); |
142 | g_assert_cmpint (p[0], ==, 0xff); |
143 | g_assert_cmpint (p[1], ==, 0xff); |
144 | g_assert_cmpint (p[2], ==, 0x00); |
145 | |
146 | g_object_unref (object: dest); |
147 | |
148 | g_object_unref (object: src); |
149 | } |
150 | |
151 | int |
152 | main (int argc, char *argv[]) |
153 | { |
154 | g_test_init (argc: &argc, argv: &argv, NULL); |
155 | |
156 | g_test_add_func (testpath: "/pixbuf/composite1" , test_func: test_composite1); |
157 | g_test_add_func (testpath: "/pixbuf/composite2" , test_func: test_composite2); |
158 | |
159 | return g_test_run (); |
160 | } |
161 | |