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
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23 const int kMaxXResolution = 6000;
24 const int kMaxYResolution = 6000;
25 const int kMaxLineThickness = 10;
26 const double kMaxFontScale = 10.0;
27
28 FuzzedDataProvider fuzz_provider(data, size);
29
30 int fuzz_font_face = fuzz_provider.PickValueInArray(
31 {cv::FONT_HERSHEY_SIMPLEX, cv::FONT_HERSHEY_PLAIN,
32 cv::FONT_HERSHEY_DUPLEX, cv::FONT_HERSHEY_COMPLEX,
33 cv::FONT_HERSHEY_TRIPLEX, cv::FONT_HERSHEY_COMPLEX_SMALL,
34 cv::FONT_HERSHEY_SCRIPT_SIMPLEX, cv::FONT_HERSHEY_SCRIPT_COMPLEX});
35
36 int fuzz_linetype = fuzz_provider.PickValueInArray({
37 cv::LINE_8, // 8-connected line
38 cv::LINE_4, // 4-connected line
39 cv::LINE_AA // anti-aliased line
40 });
41
42 double fuzz_font_scale =
43 fuzz_provider.ConsumeFloatingPointInRange(0.0, kMaxFontScale);
44 int fuzz_width =
45 fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxXResolution);
46 int fuzz_height =
47 fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxYResolution);
48 int fuzz_x_pos =
49 fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxXResolution);
50 int fuzz_y_pos =
51 fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxYResolution);
52 int fuzz_thickness =
53 fuzz_provider.ConsumeIntegralInRange<int>(0, kMaxLineThickness);
54
55 std::vector<uint8_t> color_scalar_vals;
56 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 for (int i = 0; i < 3; i++) {
61 color_scalar_vals.insert(color_scalar_vals.begin(),
62 fuzz_provider.ConsumeIntegralInRange(0, 255));
63 canvas_fill_scalar_vals.insert(
64 canvas_fill_scalar_vals.begin(),
65 fuzz_provider.ConsumeIntegralInRange(0, 255));
66 }
67
68 cv::Scalar fuzz_color(color_scalar_vals[0], color_scalar_vals[1],
69 color_scalar_vals[2]);
70 cv::Scalar fuzz_canvas_fill(canvas_fill_scalar_vals[0],
71 canvas_fill_scalar_vals[1],
72 canvas_fill_scalar_vals[2]);
73
74 cv::Point fuzz_text_pos(fuzz_x_pos, fuzz_y_pos);
75 cv::Mat fuzz_canvas(fuzz_height, fuzz_width, CV_8UC3, fuzz_canvas_fill);
76
77 std::basic_string<char> fuzz_annotation =
78 fuzz_provider.ConsumeRemainingBytesAsString();
79
80 cv::putText(fuzz_canvas, fuzz_annotation, fuzz_text_pos, fuzz_font_face,
81 fuzz_font_scale, fuzz_color, fuzz_thickness, fuzz_linetype);
82 return 0;
83 }
84