/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 | 0 | static void do_drawing(cairo_t *cr, const uint8_t *data, size_t size) { |
23 | 0 | if (size < 4) return; |
24 | | |
25 | | // Use some bytes for color and basic params |
26 | 0 | double r = data[0] / 255.0; |
27 | 0 | double g = data[1] / 255.0; |
28 | 0 | double b = data[2] / 255.0; |
29 | 0 | double a = data[3] / 255.0; |
30 | |
|
31 | 0 | cairo_set_source_rgba(cr, r, g, b, a); |
32 | 0 | cairo_set_line_width(cr, (size > 4 ? data[4] : 10) / 10.0); |
33 | |
|
34 | 0 | size_t offset = 5; |
35 | 0 | while (offset + 16 <= size) { |
36 | 0 | uint8_t op = data[offset++] % 8; |
37 | 0 | double x1 = data[offset++]; |
38 | 0 | double y1 = data[offset++]; |
39 | 0 | double x2 = data[offset++]; |
40 | 0 | double y2 = data[offset++]; |
41 | 0 | double x3 = data[offset++]; |
42 | 0 | double y3 = data[offset++]; |
43 | | |
44 | 0 | switch (op) { |
45 | 0 | case 0: |
46 | 0 | cairo_move_to(cr, x1, y1); |
47 | 0 | break; |
48 | 0 | case 1: |
49 | 0 | cairo_line_to(cr, x1, y1); |
50 | 0 | break; |
51 | 0 | case 2: |
52 | 0 | cairo_curve_to(cr, x1, y1, x2, y2, x3, y3); |
53 | 0 | break; |
54 | 0 | case 3: |
55 | 0 | cairo_rectangle(cr, x1, y1, x2, y2); |
56 | 0 | break; |
57 | 0 | case 4: |
58 | 0 | cairo_arc(cr, x1, y1, x2 / 10.0, 0, 2 * 3.14159); |
59 | 0 | break; |
60 | 0 | case 5: |
61 | 0 | cairo_fill(cr); |
62 | 0 | cairo_set_source_rgba(cr, x1/255.0, y1/255.0, x2/255.0, y2/255.0); |
63 | 0 | break; |
64 | 0 | case 6: |
65 | 0 | cairo_stroke(cr); |
66 | 0 | break; |
67 | 0 | case 7: |
68 | 0 | cairo_clip(cr); |
69 | 0 | break; |
70 | 0 | } |
71 | 0 | offset += 10; // skip some bytes to use for next ops |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | | #endif |