1 | #include "config.h" |
2 | #include "gdk-pixbuf/gdk-pixbuf.h" |
3 | #include <glib.h> |
4 | |
5 | /* |
6 | * Reads count_bytes from the channel and write them to the loader. |
7 | * Returns number of bytes written. |
8 | * count_bytes = G_MAXSIZE means read as many bytes as possible. |
9 | */ |
10 | static gsize |
11 | loader_write_from_channel (GdkPixbufLoader *loader, |
12 | GIOChannel *channel, |
13 | gsize count_bytes) |
14 | { |
15 | guchar* buffer = NULL; |
16 | gsize bytes_read = 0; |
17 | GIOStatus read_status; |
18 | GError* error = NULL; |
19 | gboolean ret; |
20 | |
21 | if(count_bytes < G_MAXSIZE) { |
22 | //read no more than 'count_bytes' bytes |
23 | buffer = g_malloc (n_bytes: count_bytes); |
24 | read_status = g_io_channel_read_chars (channel, buf: (gchar*)buffer, count: count_bytes, bytes_read: &bytes_read, NULL); |
25 | } else { |
26 | //read up to end |
27 | read_status = g_io_channel_read_to_end (channel, str_return: (gchar**)&buffer, length: &bytes_read, NULL); |
28 | } |
29 | g_assert (read_status == G_IO_STATUS_NORMAL || read_status == G_IO_STATUS_EOF); |
30 | |
31 | ret = gdk_pixbuf_loader_write(loader, buf: buffer, count: bytes_read, error: &error); |
32 | g_assert_no_error (error); |
33 | g_assert (ret); |
34 | g_free(mem: buffer); |
35 | return bytes_read; |
36 | } |
37 | |
38 | static void |
39 | test_short_gif_write (void) |
40 | { |
41 | GdkPixbufLoader *loader; |
42 | GIOChannel* channel = g_io_channel_new_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-animation.gif" , NULL), mode: "r" , NULL); |
43 | |
44 | g_assert (channel != NULL); |
45 | g_io_channel_set_encoding (channel, NULL, NULL); |
46 | |
47 | loader = gdk_pixbuf_loader_new_with_type (image_type: "gif" , NULL); |
48 | g_assert (loader != NULL); |
49 | |
50 | loader_write_from_channel (loader, channel, count_bytes: 10); |
51 | loader_write_from_channel (loader, channel, G_MAXSIZE); |
52 | |
53 | g_io_channel_unref (channel); |
54 | |
55 | gdk_pixbuf_loader_close (loader, NULL); |
56 | g_object_unref (object: loader); |
57 | } |
58 | |
59 | static void |
60 | test_load_first_frame (void) |
61 | { |
62 | GIOChannel* channel; |
63 | gboolean has_frame = FALSE; |
64 | GError *error = NULL; |
65 | GdkPixbuf *pixbuf; |
66 | GdkPixbufLoader *loader; |
67 | |
68 | channel = g_io_channel_new_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "1_partyanimsm2.gif" , NULL), mode: "r" , NULL); |
69 | g_assert (channel != NULL); |
70 | g_io_channel_set_encoding (channel, NULL, NULL); |
71 | |
72 | loader = gdk_pixbuf_loader_new_with_type (image_type: "gif" , NULL); |
73 | g_assert (loader != NULL); |
74 | |
75 | while (!has_frame) { |
76 | GdkPixbufAnimation *animation; |
77 | |
78 | loader_write_from_channel (loader, channel, count_bytes: 4096); |
79 | animation = gdk_pixbuf_loader_get_animation (loader); |
80 | if (animation) { |
81 | GdkPixbufAnimationIter *iter = gdk_pixbuf_animation_get_iter (animation, NULL); |
82 | if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) |
83 | has_frame = TRUE; |
84 | g_object_unref (object: iter); |
85 | } |
86 | } |
87 | |
88 | g_io_channel_unref (channel); |
89 | |
90 | gdk_pixbuf_loader_close (loader, error: &error); |
91 | g_assert_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION); |
92 | g_clear_error (err: &error); |
93 | pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); |
94 | g_assert (pixbuf); |
95 | g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 660); |
96 | g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 666); |
97 | g_object_unref (object: loader); |
98 | } |
99 | |
100 | int main (int argc, char *argv[]) |
101 | { |
102 | g_test_init (argc: &argc, argv: &argv, NULL); |
103 | |
104 | g_test_add_func (testpath: "/animation/short_gif_write" , test_func: test_short_gif_write); |
105 | g_test_add_func (testpath: "/animation/load_first_frame" , test_func: test_load_first_frame); |
106 | |
107 | return g_test_run (); |
108 | } |
109 | |