/src/fuzz/common_drawing.h
Line | Count | Source |
1 | | // Copyright 2026 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 | | #ifndef COMMON_DRAWING_H |
16 | | #define COMMON_DRAWING_H |
17 | | |
18 | | #include <cairo.h> |
19 | | #include <stdint.h> |
20 | | #include <string.h> |
21 | | |
22 | 11.2k | static void do_drawing(cairo_t *cr, const uint8_t *data, size_t size) { |
23 | 11.2k | if (size < 4) return; |
24 | | |
25 | | // Use some bytes for color and basic params |
26 | 11.0k | double r = data[0] / 255.0; |
27 | 11.0k | double g = data[1] / 255.0; |
28 | 11.0k | double b = data[2] / 255.0; |
29 | 11.0k | double a = data[3] / 255.0; |
30 | | |
31 | 11.0k | cairo_set_source_rgba(cr, r, g, b, a); |
32 | 11.0k | cairo_set_line_width(cr, (size > 4 ? data[4] : 10) / 10.0); |
33 | | |
34 | 11.0k | size_t offset = 5; |
35 | 4.06M | while (offset + 16 <= size) { |
36 | 4.05M | uint8_t op = data[offset++] % 8; |
37 | 4.05M | double x1 = data[offset++]; |
38 | 4.05M | double y1 = data[offset++]; |
39 | 4.05M | double x2 = data[offset++]; |
40 | 4.05M | double y2 = data[offset++]; |
41 | 4.05M | double x3 = data[offset++]; |
42 | 4.05M | double y3 = data[offset++]; |
43 | | |
44 | 4.05M | switch (op) { |
45 | 345k | case 0: |
46 | 345k | cairo_move_to(cr, x1, y1); |
47 | 345k | break; |
48 | 137k | case 1: |
49 | 137k | cairo_line_to(cr, x1, y1); |
50 | 137k | break; |
51 | 359k | case 2: |
52 | 359k | cairo_curve_to(cr, x1, y1, x2, y2, x3, y3); |
53 | 359k | break; |
54 | 897k | case 3: |
55 | 897k | cairo_rectangle(cr, x1, y1, x2, y2); |
56 | 897k | break; |
57 | 1.69M | case 4: |
58 | 1.69M | cairo_arc(cr, x1, y1, x2 / 10.0, 0, 2 * 3.14159); |
59 | 1.69M | break; |
60 | 268k | case 5: |
61 | 268k | cairo_fill(cr); |
62 | 268k | cairo_set_source_rgba(cr, x1/255.0, y1/255.0, x2/255.0, y2/255.0); |
63 | 268k | break; |
64 | 103k | case 6: |
65 | 103k | cairo_stroke(cr); |
66 | 103k | break; |
67 | 247k | case 7: |
68 | 247k | cairo_clip(cr); |
69 | 247k | break; |
70 | 4.05M | } |
71 | 4.05M | offset += 10; // skip some bytes to use for next ops |
72 | 4.05M | } |
73 | 11.0k | } |
74 | | |
75 | | #endif |