static SkImageFilter* make_image_filter(bool canBeNull) {
    SkImageFilter* filter = 0;

    // Add a 1 in 3 chance to get a nullptr input
    if (canBeNull && (R(3) == 1)) { return filter; }

    enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER,
           DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
           DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
           MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };

    switch (R(NUM_FILTERS)) {
    case ALPHA_THRESHOLD:
        filter = SkAlphaThresholdFilter::Create(make_region(), make_scalar(), make_scalar());
        break;
    case MERGE:
        filter = SkMergeImageFilter::Create(make_image_filter(), make_image_filter(), make_xfermode());
        break;
    case COLOR:
    {
        SkAutoTUnref<SkColorFilter> cf(make_color_filter());
        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
    }
        break;
    case LUT3D:
    {
        int cubeDimension;
        SkAutoDataUnref lut3D(make_3Dlut(&cubeDimension, (R(2) == 1), (R(2) == 1), (R(2) == 1)));
        SkAutoTUnref<SkColorFilter> cf(SkColorCubeFilter::Create(lut3D, cubeDimension));
        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
    }
        break;
    case BLUR:
        filter = SkBlurImageFilter::Create(make_scalar(true), make_scalar(true), make_image_filter());
        break;
    case MAGNIFIER:
        filter = SkMagnifierImageFilter::Create(make_rect(), make_scalar(true));
        break;
    case DOWN_SAMPLE:
        filter = SkDownSampleImageFilter::Create(make_scalar());
        break;
    case XFERMODE:
    {
        SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
        filter = SkXfermodeImageFilter::Create(mode, make_image_filter(), make_image_filter());
    }
        break;
    case OFFSET:
        filter = SkOffsetImageFilter::Create(make_scalar(), make_scalar(), make_image_filter());
        break;
    case MATRIX:
        filter = SkImageFilter::CreateMatrixFilter(make_matrix(),
                                                   (SkFilterQuality)R(4),
                                                   make_image_filter());
        break;
    case MATRIX_CONVOLUTION:
    {
        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
                                                     SkIntToScalar(kBitmapSize)));
        SkISize size = SkISize::Make(R(10)+1, R(10)+1);
        int arraySize = size.width() * size.height();
        SkTArray<SkScalar> kernel(arraySize);
        for (int i = 0; i < arraySize; ++i) {
            kernel.push_back() = make_scalar();
        }
        SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
                                               R(SkIntToScalar(size.height())));
        filter = SkMatrixConvolutionImageFilter::Create(size,
                                                        kernel.begin(),
                                                        make_scalar(),
                                                        make_scalar(),
                                                        kernelOffset,
                                                        (SkMatrixConvolutionImageFilter::TileMode)R(3),
                                                        R(2) == 1,
                                                        make_image_filter(),
                                                        &cropR);
    }
        break;
    case COMPOSE:
        filter = SkComposeImageFilter::Create(make_image_filter(), make_image_filter());
        break;
    case DISTANT_LIGHT:
        filter = (R(2) == 1) ?
                 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(),
                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
                 SkLightingImageFilter::CreateDistantLitSpecular(make_point(),
                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
                 make_image_filter());
        break;
    case POINT_LIGHT:
        filter = (R(2) == 1) ?
                 SkLightingImageFilter::CreatePointLitDiffuse(make_point(),
                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
                 SkLightingImageFilter::CreatePointLitSpecular(make_point(),
                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
                 make_image_filter());
        break;
    case SPOT_LIGHT:
        filter = (R(2) == 1) ?
                 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
                 make_point(), make_scalar(), make_scalar(), make_color(),
                 make_scalar(), make_scalar(), make_image_filter()) :
                 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3::Make(0, 0, 0),
                 make_point(), make_scalar(), make_scalar(), make_color(),
                 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_filter());
        break;
    case NOISE:
    {
        SkAutoTUnref<SkShader> shader((R(2) == 1) ?
            SkPerlinNoiseShader::CreateFractalNoise(
                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) :
            SkPerlinNoiseShader::CreateTurbulence(
                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()));
        SkPaint paint;
        paint.setShader(shader);
        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
                                                     SkIntToScalar(kBitmapSize)));
        filter = SkPaintImageFilter::Create(paint, &cropR);
    }
        break;
    case DROP_SHADOW:
        filter = SkDropShadowImageFilter::Create(make_scalar(), make_scalar(), make_scalar(true),
                    make_scalar(true), make_color(), make_shadow_mode(), make_image_filter(),
                    nullptr);
        break;
    case MORPHOLOGY:
        if (R(2) == 1) {
            filter = SkDilateImageFilter::Create(R(static_cast<float>(kBitmapSize)),
                R(static_cast<float>(kBitmapSize)), make_image_filter());
        } else {
            filter = SkErodeImageFilter::Create(R(static_cast<float>(kBitmapSize)),
                R(static_cast<float>(kBitmapSize)), make_image_filter());
        }
        break;
    case BITMAP:
    {
        SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(make_bitmap()));
        if (R(2) == 1) {
            filter = SkImageSource::Create(image, make_rect(), make_rect(), kHigh_SkFilterQuality);
        } else {
            filter = SkImageSource::Create(image);
        }
    }
        break;
    case DISPLACE:
        filter = SkDisplacementMapEffect::Create(make_channel_selector_type(),
                                                 make_channel_selector_type(), make_scalar(),
                                                 make_image_filter(false), make_image_filter());
        break;
    case TILE:
        filter = SkTileImageFilter::Create(make_rect(), make_rect(), make_image_filter(false));
        break;
    case PICTURE:
    {
        SkRTreeFactory factory;
        SkPictureRecorder recorder;
        SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize), 
                                                            SkIntToScalar(kBitmapSize), 
                                                            &factory, 0);
        drawSomething(recordingCanvas);
        SkAutoTUnref<SkPicture> pict(recorder.endRecording());
        filter = SkPictureImageFilter::Create(pict.get(), make_rect());
    }
        break;
    case PAINT:
    {
        SkImageFilter::CropRect cropR(make_rect());
        filter = SkPaintImageFilter::Create(make_paint(), &cropR);
    }
    default:
        break;
    }
    return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
}
Esempio n. 2
0
static sk_sp<SkImageFilter> make_image_filter(bool canBeNull) {
    sk_sp<SkImageFilter> filter;

    // Add a 1 in 3 chance to get a nullptr input
    uint8_t i;
    fuzz->nextRange(&i, 0, 2);
    if (fuzz->exhausted() || (canBeNull && i == 1)) {
        return filter;
    }

    enum { ALPHA_THRESHOLD, MERGE, COLOR, BLUR, MAGNIFIER,
           BLENDMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
           DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
           MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS };

    uint8_t s;
    fuzz->nextRange(&s, 0, NUM_FILTERS - 1);
    switch (s) {
    case ALPHA_THRESHOLD: {
        SkRegion reg = make_region();
        SkScalar innerMin, outerMax;
        fuzz->next(&innerMin, &outerMax);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkAlphaThresholdFilter::Make(reg, innerMin, outerMax, fil);
        break;
    }
    case MERGE: {
        sk_sp<SkImageFilter> filA = make_image_filter();
        sk_sp<SkImageFilter> filB = make_image_filter();
        filter = SkMergeImageFilter::Make(filA, filB);
        break;
    }
    case COLOR: {
        sk_sp<SkColorFilter> cf(make_color_filter());
        filter = cf ? SkColorFilterImageFilter::Make(std::move(cf), make_image_filter())
                    : nullptr;
        break;
    }
    case BLUR: {
        SkScalar sX = make_number(true);
        SkScalar sY = make_number(true);
        sk_sp<SkImageFilter> fil = make_image_filter();

        filter = SkBlurImageFilter::Make(sX, sY, fil);
        break;
    }
    case MAGNIFIER: {
        SkRect rect = make_rect();
        SkScalar inset = make_number(true);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkMagnifierImageFilter::Make(rect, inset, fil);
        break;
    }
    case BLENDMODE: {
        SkBlendMode mode = make_blendmode();
        sk_sp<SkImageFilter> filA = make_image_filter();
        sk_sp<SkImageFilter> filB = make_image_filter();
        filter = SkXfermodeImageFilter::Make(mode, filA, filB, nullptr);
        break;
    }
    case OFFSET: {
        SkScalar dx, dy;
        fuzz->next(&dx, &dy);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkOffsetImageFilter::Make(dx, dy, fil);
        break;
    }
    case MATRIX: {
        SkMatrix m;
        init_matrix(&m);
        int qual;
        fuzz->nextRange(&qual, 0, SkFilterQuality::kLast_SkFilterQuality - 1);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkImageFilter::MakeMatrixFilter(m, (SkFilterQuality)qual, fil);
        break;
    }
    case MATRIX_CONVOLUTION: {
        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
                                                     SkIntToScalar(kBitmapSize)));
        int w, h;
        fuzz->nextRange(&w, 1, 10);
        fuzz->nextRange(&h, 1, 10);
        SkISize size = SkISize::Make(w, h);
        int arraySize = size.width() * size.height();
        SkTArray<SkScalar> kernel(arraySize);
        for (int i = 0; i < arraySize; ++i) {
            kernel.push_back() = make_number(false);
        }
        fuzz->nextRange(&w, 0, size.width()  - 1);
        fuzz->nextRange(&h, 0, size.height() - 1);
        SkIPoint kernelOffset = SkIPoint::Make(w, h);
        int mode;
        fuzz->nextRange(&mode, 0, SkMatrixConvolutionImageFilter::kMax_TileMode - 1);
        bool convolveAlpha = make_bool();
        SkScalar gain, bias;
        fuzz->next(&gain, &bias);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkMatrixConvolutionImageFilter::Make(size,
                                                      kernel.begin(),
                                                      gain,
                                                      bias,
                                                      kernelOffset,
                                                      (SkMatrixConvolutionImageFilter::TileMode)mode,
                                                      convolveAlpha,
                                                      fil,
                                                      &cropR);
        break;
    }
    case COMPOSE: {
        sk_sp<SkImageFilter> filA = make_image_filter();
        sk_sp<SkImageFilter> filB = make_image_filter();
        filter = SkComposeImageFilter::Make(filA, filB);
        break;
    }
    case DISTANT_LIGHT: {
        SkPoint3 p = make_point();
        SkColor c = make_color();
        SkScalar ss, kd;
        fuzz->next(&ss, &kd);
        int shininess;
        fuzz->nextRange(&shininess, 0, 9);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = make_bool()
                 ? SkLightingImageFilter::MakeDistantLitDiffuse(p, c, ss, kd, fil)
                 : SkLightingImageFilter::MakeDistantLitSpecular(p, c, ss, kd, shininess, fil);
        break;
    }
    case POINT_LIGHT: {
        SkPoint3 p = make_point();
        SkColor c = make_color();
        SkScalar ss, kd;
        fuzz->next(&ss, &kd);
        int shininess;
        fuzz->nextRange(&shininess, 0, 9);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = make_bool()
                 ? SkLightingImageFilter::MakePointLitDiffuse(p, c, ss, kd, fil)
                 : SkLightingImageFilter::MakePointLitSpecular(p, c, ss, kd, shininess, fil);
        break;
    }
    case SPOT_LIGHT: {
        SkPoint3 p = make_point();
        SkColor c = make_color();
        SkScalar se, ca, ss, kd;
        fuzz->next(&se, &ca, &ss, &kd);
        int shininess;
        fuzz->nextRange(&shininess, 0, 9);
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = make_bool()
                 ? SkLightingImageFilter::MakeSpotLitDiffuse(SkPoint3::Make(0, 0, 0),
                                                             p, se, ca, c, ss, kd, fil)
                 : SkLightingImageFilter::MakeSpotLitSpecular(SkPoint3::Make(0, 0, 0),
                                                              p, se, ca, c, ss, kd,
                                                              shininess, fil);
        break;
    }
    case NOISE: {
        SkScalar bfx = make_number(true);
        SkScalar bfy = make_number(true);
        SkScalar seed = make_number(false);
        int octaves;
        fuzz->nextRange(&octaves, 0, 9);
        sk_sp<SkShader> shader(make_bool()
                ? SkPerlinNoiseShader::MakeFractalNoise(bfx, bfy, octaves, seed)
                : SkPerlinNoiseShader::MakeTurbulence(bfx, bfy, octaves, seed));
        SkPaint paint;
        paint.setShader(shader);
        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
                                                     SkIntToScalar(kBitmapSize)));
        filter = SkPaintImageFilter::Make(paint, &cropR);
        break;
    }
    case DROP_SHADOW: {
        SkScalar dx, dy, sx, sy;
        fuzz->next(&dx, &dy);
        sx = make_number(true);
        sy = make_number(true);
        SkColor c = make_color();
        SkDropShadowImageFilter::ShadowMode mode = make_shadow_mode();
        sk_sp<SkImageFilter> fil = make_image_filter();
        filter = SkDropShadowImageFilter::Make(dx, dy, sx, sy, c, mode, fil, nullptr);
        break;
    }
    case MORPHOLOGY: {
        int rx, ry;
        fuzz->nextRange(&rx, 0, kBitmapSize);
        fuzz->nextRange(&ry, 0, kBitmapSize);
        sk_sp<SkImageFilter> fil = make_image_filter();
        if (make_bool()) {
            filter = SkDilateImageFilter::Make(rx, ry, fil);
        } else {
            filter = SkErodeImageFilter::Make(rx, ry, fil);
        }
        break;
    }
    case BITMAP: {
        sk_sp<SkImage> image(SkImage::MakeFromBitmap(make_bitmap()));
        if (make_bool()) {
            filter = SkImageSource::Make(std::move(image),
                                         make_rect(),
                                         make_rect(),
                                         kHigh_SkFilterQuality);
        } else {
            filter = SkImageSource::Make(std::move(image));
        }
        break;
    }
    case DISPLACE: {
        SkDisplacementMapEffect::ChannelSelectorType x = make_channel_selector_type();
        SkDisplacementMapEffect::ChannelSelectorType y = make_channel_selector_type();
        SkScalar scale = make_number(false);
        sk_sp<SkImageFilter> filA = make_image_filter(false);
        sk_sp<SkImageFilter> filB = make_image_filter();

        filter = SkDisplacementMapEffect::Make(x, y, scale, filA, filB);
        break;
    }
    case TILE: {
        SkRect src = make_rect();
        SkRect dest = make_rect();
        sk_sp<SkImageFilter> fil = make_image_filter(false);
        filter = SkTileImageFilter::Make(src, dest, fil);
        break;
    }
    case PICTURE: {
        SkRTreeFactory factory;
        SkPictureRecorder recorder;
        SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
                                                            SkIntToScalar(kBitmapSize),
                                                            &factory, 0);
        drawSomething(recordingCanvas);
        sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
        filter = SkPictureImageFilter::Make(pict, make_rect());
        break;
    }
    case PAINT: {
        SkImageFilter::CropRect cropR(make_rect());
        filter = SkPaintImageFilter::Make(make_paint(), &cropR);
        break;
    }
    default:
        break;
    }
    return filter;
}