diff --git a/api/current.txt b/api/current.txt index 019ae848f8186..9a4436674b5e0 100644 --- a/api/current.txt +++ b/api/current.txt @@ -13773,7 +13773,9 @@ package android.graphics { public enum Bitmap.CompressFormat { enum_constant public static final android.graphics.Bitmap.CompressFormat JPEG; enum_constant public static final android.graphics.Bitmap.CompressFormat PNG; - enum_constant public static final android.graphics.Bitmap.CompressFormat WEBP; + enum_constant @Deprecated public static final android.graphics.Bitmap.CompressFormat WEBP; + enum_constant public static final android.graphics.Bitmap.CompressFormat WEBP_LOSSLESS; + enum_constant public static final android.graphics.Bitmap.CompressFormat WEBP_LOSSY; } public enum Bitmap.Config { diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp index 89c12f88594d3..0487e139d264e 100755 --- a/core/jni/android/graphics/Bitmap.cpp +++ b/core/jni/android/graphics/Bitmap.cpp @@ -9,6 +9,7 @@ #include "SkColorSpace.h" #include "GraphicsJNI.h" #include "SkStream.h" +#include "SkWebpEncoder.h" #include "android_os_Parcel.h" #include "android_util_Binder.h" @@ -526,27 +527,14 @@ static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle, enum JavaEncodeFormat { kJPEG_JavaEncodeFormat = 0, kPNG_JavaEncodeFormat = 1, - kWEBP_JavaEncodeFormat = 2 + kWEBP_JavaEncodeFormat = 2, + kWEBP_LOSSY_JavaEncodeFormat = 3, + kWEBP_LOSSLESS_JavaEncodeFormat = 4, }; static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, jint format, jint quality, jobject jstream, jbyteArray jstorage) { - SkEncodedImageFormat fm; - switch (format) { - case kJPEG_JavaEncodeFormat: - fm = SkEncodedImageFormat::kJPEG; - break; - case kPNG_JavaEncodeFormat: - fm = SkEncodedImageFormat::kPNG; - break; - case kWEBP_JavaEncodeFormat: - fm = SkEncodedImageFormat::kWEBP; - break; - default: - return JNI_FALSE; - } - LocalScopedBitmap bitmap(bitmapHandle); if (!bitmap.valid()) { return JNI_FALSE; @@ -577,6 +565,30 @@ static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, } skbitmap = p3; } + SkEncodedImageFormat fm; + switch (format) { + case kJPEG_JavaEncodeFormat: + fm = SkEncodedImageFormat::kJPEG; + break; + case kPNG_JavaEncodeFormat: + fm = SkEncodedImageFormat::kPNG; + break; + case kWEBP_JavaEncodeFormat: + fm = SkEncodedImageFormat::kWEBP; + break; + case kWEBP_LOSSY_JavaEncodeFormat: + case kWEBP_LOSSLESS_JavaEncodeFormat: { + SkWebpEncoder::Options options; + options.fQuality = quality; + options.fCompression = format == kWEBP_LOSSY_JavaEncodeFormat ? + SkWebpEncoder::Compression::kLossy : SkWebpEncoder::Compression::kLossless; + return SkWebpEncoder::Encode(strm.get(), skbitmap.pixmap(), options) ? + JNI_TRUE : JNI_FALSE; + } + default: + return JNI_FALSE; + } + return SkEncodeImage(strm.get(), skbitmap, fm, quality) ? JNI_TRUE : JNI_FALSE; } diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index 44710178da5e2..d900a42b1e664 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -1359,9 +1359,44 @@ public final class Bitmap implements Parcelable { * Specifies the known formats a bitmap can be compressed into */ public enum CompressFormat { - JPEG (0), - PNG (1), - WEBP (2); + /** + * Compress to the JPEG format. {@code quality} of {@code 0} means + * compress for the smallest size. {@code 100} means compress for max + * visual quality. + */ + JPEG (0), + /** + * Compress to the PNG format. PNG is lossless, so {@code quality} is + * ignored. + */ + PNG (1), + /** + * Compress to the WEBP format. {@code quality} of {@code 0} means + * compress for the smallest size. {@code 100} means compress for max + * visual quality. As of {@link android.os.Build.VERSION_CODES#Q}, a + * value of {@code 100} results in a file in the lossless WEBP format. + * Otherwise the file will be in the lossy WEBP format. + * + * @deprecated in favor of the more explicit + * {@link CompressFormat#WEBP_LOSSY} and + * {@link CompressFormat#WEBP_LOSSLESS}. + */ + @Deprecated + WEBP (2), + /** + * Compress to the WEBP lossy format. {@code quality} of {@code 0} means + * compress for the smallest size. {@code 100} means compress for max + * visual quality. + */ + WEBP_LOSSY (3), + /** + * Compress to the WEBP lossless format. {@code quality} refers to how + * much effort to put into compression. A value of {@code 0} means to + * compress quickly, resulting in a relatively large file size. + * {@code 100} means to spend more time compressing, resulting in a + * smaller file. + */ + WEBP_LOSSLESS (4); CompressFormat(int nativeInt) { this.nativeInt = nativeInt; @@ -1385,10 +1420,8 @@ public final class Bitmap implements Parcelable { * pixels). * * @param format The format of the compressed image - * @param quality Hint to the compressor, 0-100. 0 meaning compress for - * small size, 100 meaning compress for max quality. Some - * formats, like PNG which is lossless, will ignore the - * quality setting + * @param quality Hint to the compressor, 0-100. The value is interpreted + * differently depending on the {@link CompressFormat}. * @param stream The outputstream to write the compressed data. * @return true if successfully compressed to the specified stream. */