1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdint.h>
16 #include <gdk-pixbuf/gdk-pixbuf.h>
17 
18 #define WIDTH 10
19 #define HEIGHT 20
20 #define ROWSTRIDE (WIDTH * 4)
21 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23     if (!(size >= WIDTH * HEIGHT * 4)) {
24         return 0;
25     }
26     const gchar *profile;
27     GdkPixbuf *pixbuf, *tmp;
28     GBytes *bytes;
29     bytes = g_bytes_new_static(data, size);
30     pixbuf = g_object_new(GDK_TYPE_PIXBUF,
31             "width", WIDTH,
32             "height", HEIGHT,
33             "rowstride", ROWSTRIDE,
34             "bits-per-sample", 8,"n-channels", 3,
35             "has-alpha", FALSE,
36             "pixel-bytes", bytes,
37             NULL);
38     if (pixbuf == NULL) {
39         return 0;
40     }
41     gdk_pixbuf_scale(pixbuf, pixbuf,
42             0, 0,
43             gdk_pixbuf_get_width(pixbuf) / 4,
44             gdk_pixbuf_get_height(pixbuf) / 4,
45             0, 0, 0.5, 0.5,
46             GDK_INTERP_NEAREST);
47     unsigned int rot_amount = ((unsigned int) data[0]) % 4;
48     tmp = gdk_pixbuf_rotate_simple(pixbuf, rot_amount * 90);
49     tmp = gdk_pixbuf_flip(pixbuf, TRUE);
50     tmp = gdk_pixbuf_composite_color_simple(pixbuf,
51             gdk_pixbuf_get_width(pixbuf) / 4,
52             gdk_pixbuf_get_height(pixbuf) / 4,
53             GDK_INTERP_NEAREST,
54             128,
55             8,
56             G_MAXUINT32,
57             G_MAXUINT32/2);
58 
59     char *buf = (char *) calloc(size + 1, sizeof(char));
60     memcpy(buf, data, size);
61     buf[size] = '\0';
62 
63     gdk_pixbuf_set_option(pixbuf, buf, buf);
64     profile = gdk_pixbuf_get_option(pixbuf, buf);
65     tmp = gdk_pixbuf_new_from_data(gdk_pixbuf_get_pixels(pixbuf),
66             GDK_COLORSPACE_RGB,
67             FALSE,
68             gdk_pixbuf_get_bits_per_sample(pixbuf),
69             gdk_pixbuf_get_width(pixbuf),
70             gdk_pixbuf_get_height(pixbuf),
71             gdk_pixbuf_get_rowstride(pixbuf),
72             NULL,
73             NULL);
74     tmp = gdk_pixbuf_flip(tmp, TRUE);
75 
76     free(buf);
77     g_object_unref(pixbuf);
78     g_object_unref(tmp);
79     return 0;
80 }
81