/src/generateusergallerycollage_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2020 Google Inc. |
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 <cstddef> |
16 | | #include <cstdint> |
17 | | #include <iosfwd> |
18 | | |
19 | | #include <opencv2/opencv.hpp> |
20 | | #include <fuzzer/FuzzedDataProvider.h> |
21 | | |
22 | 1.24k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
23 | 1.24k | const int kMaxXResolution = 6000; |
24 | 1.24k | const int kMaxYResolution = 6000; |
25 | 1.24k | const int kMaxLineThickness = 10; |
26 | 1.24k | const double kMaxFontScale = 10.0; |
27 | | |
28 | 1.24k | FuzzedDataProvider fuzz_provider(data, size); |
29 | | |
30 | 1.24k | int fuzz_font_face = fuzz_provider.PickValueInArray( |
31 | 1.24k | {cv::FONT_HERSHEY_SIMPLEX, cv::FONT_HERSHEY_PLAIN, |
32 | 1.24k | cv::FONT_HERSHEY_DUPLEX, cv::FONT_HERSHEY_COMPLEX, |
33 | 1.24k | cv::FONT_HERSHEY_TRIPLEX, cv::FONT_HERSHEY_COMPLEX_SMALL, |
34 | 1.24k | cv::FONT_HERSHEY_SCRIPT_SIMPLEX, cv::FONT_HERSHEY_SCRIPT_COMPLEX}); |
35 | | |
36 | 1.24k | int fuzz_linetype = fuzz_provider.PickValueInArray({ |
37 | 1.24k | cv::LINE_8, // 8-connected line |
38 | 1.24k | cv::LINE_4, // 4-connected line |
39 | 1.24k | cv::LINE_AA // anti-aliased line |
40 | 1.24k | }); |
41 | | |
42 | 1.24k | double fuzz_font_scale = |
43 | 1.24k | fuzz_provider.ConsumeFloatingPointInRange(0.0, kMaxFontScale); |
44 | 1.24k | int fuzz_width = |
45 | 1.24k | fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxXResolution); |
46 | 1.24k | int fuzz_height = |
47 | 1.24k | fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxYResolution); |
48 | 1.24k | int fuzz_x_pos = |
49 | 1.24k | fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxXResolution); |
50 | 1.24k | int fuzz_y_pos = |
51 | 1.24k | fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxYResolution); |
52 | 1.24k | int fuzz_thickness = |
53 | 1.24k | fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxLineThickness); |
54 | | |
55 | 1.24k | std::vector<uint8_t> color_scalar_vals; |
56 | 1.24k | std::vector<uint8_t> canvas_fill_scalar_vals; |
57 | | |
58 | | // Ensure that all 3D vectors are fully populated, |
59 | | // even if fuzz_provider is exhausted. |
60 | 4.98k | for (int i = 0; i < 3; i++) { |
61 | 3.74k | color_scalar_vals.insert(color_scalar_vals.begin(), |
62 | 3.74k | fuzz_provider.ConsumeIntegralInRange(0, 255)); |
63 | 3.74k | canvas_fill_scalar_vals.insert( |
64 | 3.74k | canvas_fill_scalar_vals.begin(), |
65 | 3.74k | fuzz_provider.ConsumeIntegralInRange(0, 255)); |
66 | 3.74k | } |
67 | | |
68 | 1.24k | cv::Scalar fuzz_color(color_scalar_vals[0], color_scalar_vals[1], |
69 | 1.24k | color_scalar_vals[2]); |
70 | 1.24k | cv::Scalar fuzz_canvas_fill(canvas_fill_scalar_vals[0], |
71 | 1.24k | canvas_fill_scalar_vals[1], |
72 | 1.24k | canvas_fill_scalar_vals[2]); |
73 | | |
74 | 1.24k | cv::Point fuzz_text_pos(fuzz_x_pos, fuzz_y_pos); |
75 | 1.24k | cv::Mat fuzz_canvas(fuzz_height, fuzz_width, CV_8UC3, fuzz_canvas_fill); |
76 | | |
77 | 1.24k | std::basic_string<char> fuzz_annotation = |
78 | 1.24k | fuzz_provider.ConsumeRemainingBytesAsString(); |
79 | | |
80 | 1.24k | cv::putText(fuzz_canvas, fuzz_annotation, fuzz_text_pos, fuzz_font_face, |
81 | 1.24k | fuzz_font_scale, fuzz_color, fuzz_thickness, fuzz_linetype); |
82 | 1.24k | return 0; |
83 | 1.24k | } |