2 * Copyright 2021 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 package org.skia.androidkit;
10 import android.support.annotation.NonNull;
11 import android.support.annotation.Nullable;
13 public class ImageFilter {
14 private long mNativeInstance;
16 private ImageFilter(long native_instance) {
17 mNativeInstance = native_instance;
21 * Create a filter that calculates the diffuse illumination from a distant light source,
22 * interpreting the alpha channel of the input as the height profile of the surface (to
23 * approximate normal vectors).
24 * @param x, y, z The direction to the distance light.
25 * @param c The color of the diffuse light source.
26 * @param surfaceScale Scale factor to transform from alpha values to physical height.
27 * @param kd Diffuse reflectance coefficient.
28 * @param input The input filter that defines surface normals (as alpha), or uses the
29 * source bitmap when null.
31 public static ImageFilter distantLitDiffuse(float x, float y, float z, Color c,
32 float surfaceScale, float kd, @Nullable ImageFilter input) {
35 nativeInput = input.getNativeInstance();
37 return new ImageFilter(nDistantLitDiffuse(x, y, z, c.r(), c.g(), c.b(),
38 surfaceScale, kd, nativeInput));
42 * Create a filter that calculates the diffuse illumination from a distant light source,
43 * interpreting the alpha channel of the input as the height profile of the surface (to
44 * approximate normal vectors).
45 * @param sigmaX The Gaussian sigma value for blurring along the X axis.
46 * @param sigmaY The Gaussian sigma value for blurring along the Y axis.
47 * @param tileMode The tile mode applied at edges
48 * @param input The input filter that is blurred, uses source bitmap if this is null.
50 public static ImageFilter blur(float sigmaX, float sigmaY, TileMode tileMode, @Nullable ImageFilter input) {
53 nativeInput = input.getNativeInstance();
55 return new ImageFilter(nBlur(sigmaX, sigmaY, tileMode.nativeInt, nativeInput));
59 * Create a filter that draws a drop shadow under the input content. This filter produces an
60 * image that includes the inputs' content.
61 * @param dx The X offset of the shadow.
62 * @param dy The Y offset of the shadow.
63 * @param sigmaX The blur radius for the shadow, along the X axis.
64 * @param sigmaY The blur radius for the shadow, along the Y axis.
65 * @param c The color of the drop shadow.
66 * @param input The input filter, or will use the source bitmap if this is null.
68 public static ImageFilter dropShadow(float dx, float dy,
69 float sigmaX, float sigmaY,
70 Color c, @Nullable ImageFilter input) {
73 nativeInput = input.getNativeInstance();
75 return new ImageFilter(nDropShadow(dx, dy, sigmaX, sigmaY, c.r(), c.g(), c.b(), nativeInput));
78 public static ImageFilter blend(BlendMode blendMode, @Nullable ImageFilter background,
79 @Nullable ImageFilter foreground) {
80 long nativeBackground = 0;
81 long nativeForeground = 0;
82 if (background != null) {
83 nativeBackground = background.getNativeInstance();
85 if (foreground != null) {
86 nativeForeground = foreground.getNativeInstance();
88 return new ImageFilter(nBlend(blendMode.nativeInt, nativeBackground, nativeForeground));
91 public static ImageFilter image(@NonNull Image image) {
92 return new ImageFilter(nImage(image.getNativeInstance()));
96 * Releases any resources associated with this Shader.
98 public void release() {
99 nRelease(mNativeInstance);
104 protected void finalize() throws Throwable {
109 long getNativeInstance() { return mNativeInstance; }
110 private static native void nRelease(long nativeInstance);
111 private static native long nDistantLitDiffuse(float x, float y, float z,
112 float r, float g, float b,
113 float surfaceScale, float kd,
115 private static native long nBlur(float sigmaX, float sigmaY, int tileMode, long native_input);
116 private static native long nDropShadow(float dx, float dy, float sigmaX, float sigmaY,
117 float r, float g, float b, long native_input);
118 private static native long nBlend(int blendMode, long native_background, long native_foreground);
119 private static native long nImage(long native_image);