/src/libwebp/tests/fuzzer/enc_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2024 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 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include <cstddef> |
18 | | #include <cstdint> |
19 | | #include <cstdlib> |
20 | | #include <iostream> |
21 | | #include <string> |
22 | | #include <string_view> |
23 | | |
24 | | #include "imageio/image_dec.h" |
25 | | #include "src/dsp/cpu.h" |
26 | | #include "src/webp/decode.h" |
27 | | #include "src/webp/encode.h" |
28 | | #include "src/webp/types.h" |
29 | | #include "tests/fuzzer/fuzz_utils.h" |
30 | | |
31 | | namespace { |
32 | | |
33 | | const VP8CPUInfo default_VP8GetCPUInfo = fuzz_utils::VP8GetCPUInfo; |
34 | | |
35 | | void EncTest(std::string_view file, uint32_t optimization_index, bool use_argb, |
36 | | WebPConfig config, |
37 | 9.14k | const fuzz_utils::CropOrScaleParams& crop_or_scale_params) { |
38 | 9.14k | fuzz_utils::SetOptimization(default_VP8GetCPUInfo, optimization_index); |
39 | | |
40 | | // Init the source picture. |
41 | 9.14k | WebPPicture pic; |
42 | 9.14k | if (!WebPPictureInit(&pic)) { |
43 | 0 | std::cerr << "WebPPictureInit failed.\n"; |
44 | 0 | std::abort(); |
45 | 0 | } |
46 | 9.14k | pic.use_argb = use_argb; |
47 | | |
48 | 9.14k | const uint8_t* const file_data = |
49 | 9.14k | reinterpret_cast<const uint8_t*>(file.data()); |
50 | 9.14k | if (fuzz_utils::IsImageTooBig(file_data, file.size())) return; |
51 | 9.13k | WebPImageReader reader = WebPGuessImageReader(file_data, file.size()); |
52 | 9.13k | if (!reader(file_data, file.size(), &pic, 1, NULL)) return; |
53 | | |
54 | | // Crop and scale. |
55 | 8.32k | if (!CropOrScale(&pic, crop_or_scale_params)) { |
56 | 0 | const WebPEncodingError error_code = pic.error_code; |
57 | 0 | WebPPictureFree(&pic); |
58 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return; |
59 | 0 | std::cerr << "CropOrScale failed. Error code: " << error_code << "\n"; |
60 | 0 | std::abort(); |
61 | 0 | } |
62 | | |
63 | | // Skip the cruncher except on small images, it's likely to timeout. |
64 | 8.32k | if (config.lossless && config.quality == 100. && config.method == 6 && |
65 | 1.71k | pic.width * pic.height >= 16384) { |
66 | 276 | config.lossless = 0; |
67 | 276 | } |
68 | | |
69 | | // Encode. |
70 | 8.32k | WebPMemoryWriter memory_writer; |
71 | 8.32k | WebPMemoryWriterInit(&memory_writer); |
72 | 8.32k | pic.writer = WebPMemoryWrite; |
73 | 8.32k | pic.custom_ptr = &memory_writer; |
74 | 8.32k | if (!WebPEncode(&config, &pic)) { |
75 | 0 | const WebPEncodingError error_code = pic.error_code; |
76 | 0 | WebPMemoryWriterClear(&memory_writer); |
77 | 0 | WebPPictureFree(&pic); |
78 | 0 | if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return; |
79 | 0 | std::cerr << "WebPEncode failed. Error code: " << error_code |
80 | 0 | << " \nFile starts with: " << file.substr(0, 20) << "\n"; |
81 | 0 | std::abort(); |
82 | 0 | } |
83 | | |
84 | | // Try decoding the result. |
85 | 8.32k | int w, h; |
86 | 8.32k | const uint8_t* const out_data = memory_writer.mem; |
87 | 8.32k | const size_t out_size = memory_writer.size; |
88 | 8.32k | uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h); |
89 | 8.32k | if (rgba == nullptr || w != pic.width || h != pic.height) { |
90 | 0 | std::cerr << "WebPDecodeBGRA failed.\nFile starts with: " |
91 | 0 | << file.substr(0, 20) << "\n"; |
92 | 0 | WebPFree(rgba); |
93 | 0 | WebPMemoryWriterClear(&memory_writer); |
94 | 0 | WebPPictureFree(&pic); |
95 | 0 | std::abort(); |
96 | 0 | } |
97 | | |
98 | | // Compare the results if exact encoding. |
99 | 8.32k | if (pic.use_argb && config.lossless && config.near_lossless == 100) { |
100 | 686 | const uint32_t* src1 = (const uint32_t*)rgba; |
101 | 686 | const uint32_t* src2 = pic.argb; |
102 | 133k | for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) { |
103 | 6.84M | for (int x = 0; x < w; ++x) { |
104 | 6.71M | uint32_t v1 = src1[x], v2 = src2[x]; |
105 | 6.71M | if (!config.exact) { |
106 | 2.35M | if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) { |
107 | | // Only keep alpha for comparison of fully transparent area. |
108 | 212k | v1 &= 0xff000000u; |
109 | 212k | v2 &= 0xff000000u; |
110 | 212k | } |
111 | 2.35M | } |
112 | 6.71M | if (v1 != v2) { |
113 | 0 | std::cerr |
114 | 0 | << "Lossless compression failed pixel-exactness.\nFile starts " |
115 | 0 | "with: " |
116 | 0 | << file.substr(0, 20) << "\n"; |
117 | 0 | WebPFree(rgba); |
118 | 0 | WebPMemoryWriterClear(&memory_writer); |
119 | 0 | WebPPictureFree(&pic); |
120 | 0 | std::abort(); |
121 | 0 | } |
122 | 6.71M | } |
123 | 132k | } |
124 | 686 | } |
125 | | |
126 | 8.32k | WebPFree(rgba); |
127 | 8.32k | WebPMemoryWriterClear(&memory_writer); |
128 | 8.32k | WebPPictureFree(&pic); |
129 | 8.32k | } |
130 | | |
131 | | } // namespace |
132 | | |
133 | | FUZZ_TEST(Enc, EncTest) |
134 | | .WithDomains(fuzztest::Arbitrary<std::string>(), |
135 | | /*optimization_index=*/ |
136 | | fuzztest::InRange<uint32_t>(0, |
137 | | fuzz_utils::kMaxOptimizationIndex), |
138 | | /*use_argb=*/fuzztest::Arbitrary<bool>(), |
139 | | fuzz_utils::ArbitraryWebPConfig(), |
140 | | fuzz_utils::ArbitraryCropOrScaleParams()); |