• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google Inc.
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 package org.skia.androidkit;
9 
10 import android.support.annotation.Nullable;
11 import java.io.InputStream;
12 import org.skia.androidkit.Matrix;
13 import org.skia.androidkit.SamplingOptions;
14 import org.skia.androidkit.Shader;
15 import org.skia.androidkit.TileMode;
16 
17 public class Image {
18     private long mNativeInstance;
19 
20     /**
21      * Construct an Image from encoded (PNG, GIF, etc) data.
22      *
23      * Returns null for unsupported formats or invalid data.
24      */
fromEncoded(byte[] encodedData)25     public static Image fromEncoded(byte[] encodedData) {
26         long nativeImage = nCreate(encodedData);
27         return nativeImage != 0
28             ? new Image(nativeImage)
29             : null;
30     }
31 
32     /**
33      * Construct an Image from an encoded data stream.
34      *
35      * Returns null for unsupported formats or invalid stream.
36      */
fromStream(InputStream encodedStream)37     public static Image fromStream(InputStream encodedStream) throws java.io.IOException {
38         byte[] encodedData = new byte[encodedStream.available()];
39         encodedStream.read(encodedData);
40 
41         return fromEncoded(encodedData);
42     }
43 
getWidth()44     public int getWidth() {
45         return nGetWidth(mNativeInstance);
46     }
47 
getHeight()48     public int getHeight() {
49         return nGetHeight(mNativeInstance);
50     }
51 
makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling)52     public Shader makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling) {
53         return makeShader(tmx, tmy, sampling, null);
54     }
55 
makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling, @Nullable Matrix localMatrix)56     public Shader makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling,
57                              @Nullable Matrix localMatrix) {
58         long nativeMatrix = localMatrix != null ? localMatrix.getNativeInstance() : 0;
59         return new Shader(nMakeShader(mNativeInstance, tmx.nativeInt, tmy.nativeInt,
60                                       sampling.getNativeDesc(),
61                                       sampling.getCubicCoeffB(), sampling.getCubicCoeffC(),
62                                       nativeMatrix));
63     }
64 
65     /**
66      * Releases any resources associated with this Paint.
67      */
release()68     public void release() {
69         nRelease(mNativeInstance);
70         mNativeInstance = 0;
71     }
72 
73     @Override
finalize()74     protected void finalize() throws Throwable {
75         release();
76     }
77 
78     // package private
Image(long nativeInstance)79     Image(long nativeInstance) {
80         mNativeInstance = nativeInstance;
81     }
82 
83     // package private
getNativeInstance()84     long getNativeInstance() { return mNativeInstance; }
85 
nCreate(byte[] encodedData)86     private static native long nCreate(byte[] encodedData);
nRelease(long nativeInstance)87     private static native void nRelease(long nativeInstance);
88 
nGetWidth(long nativeInstance)89     private static native int nGetWidth(long nativeInstance);
nGetHeight(long nativeInstance)90     private static native int nGetHeight(long nativeInstance);
91 
nMakeShader(long nativeInstance, int tmx, int tmy, int samplingDesc, float samplingCoeffB, float samplingCoeffC, long nativeMatrix)92     private static native long nMakeShader(long nativeInstance, int tmx, int tmy, int samplingDesc,
93                                            float samplingCoeffB, float samplingCoeffC,
94                                            long nativeMatrix);
95 }
96