xref: /aosp_15_r20/external/skia/fuzz/oss_fuzz/FuzzColorspace.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "fuzz/Fuzz.h"
9 #include "include/core/SkColorSpace.h"
10 #include "include/core/SkData.h"
11 #include "modules/skcms/skcms.h"
12 
FuzzColorspace(const uint8_t * data,size_t size)13 void FuzzColorspace(const uint8_t *data, size_t size) {
14     sk_sp<SkColorSpace> space(SkColorSpace::Deserialize(data, size));
15     if (!space) {
16         return;
17     }
18     // Call some arbitrary methods on the colorspace, using a throw-away
19     // variable to prevent the compiler from optimizing things away.
20     int i = 0;
21     if (space->gammaCloseToSRGB()) {
22         i += 1;
23     }
24     if (space->gammaIsLinear()) {
25         i += 1;
26     }
27     if (space->isSRGB()) {
28         i += 1;
29     }
30     skcms_ICCProfile profile;
31     space->toProfile(&profile);
32     sk_sp<SkColorSpace> space2 = space->makeLinearGamma()->makeSRGBGamma()->makeColorSpin();
33     sk_sp<SkData> data1 = space->serialize();
34     if (SkColorSpace::Equals(space.get(), space2.get()) && i > 5) {
35         SkDebugf("Should never happen %d", (int)data1->size());
36         space2->writeToMemory(nullptr);
37     }
38 }
39 
40 #if defined(SK_BUILD_FOR_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
42     if (size > 4000) {
43         return 0;
44     }
45     FuzzColorspace(data, size);
46     return 0;
47 }
48 #endif
49