Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <android/bitmap.h> |
Leon Scroggins III | 9010e8b | 2019-08-20 11:27:17 -0400 | [diff] [blame^] | 18 | #include <android/data_space.h> |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 19 | #include <android/graphics/bitmap.h> |
Leon Scroggins III | 1994fcb | 2020-01-10 13:40:07 -0500 | [diff] [blame] | 20 | #include <android/data_space.h> |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 21 | |
| 22 | int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap, |
| 23 | AndroidBitmapInfo* info) { |
| 24 | if (NULL == env || NULL == jbitmap) { |
| 25 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 26 | } |
| 27 | |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 28 | if (info) { |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 29 | *info = ABitmap_getInfoFromJava(env, jbitmap); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 30 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 31 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Leon Scroggins III | 1994fcb | 2020-01-10 13:40:07 -0500 | [diff] [blame] | 34 | int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) { |
| 35 | if (NULL == env || NULL == jbitmap) { |
| 36 | return ADATASPACE_UNKNOWN; // Or return a real error? |
| 37 | } |
| 38 | |
| 39 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 40 | return bitmap.getDataSpace(); |
| 41 | } |
| 42 | |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 43 | int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) { |
| 44 | if (NULL == env || NULL == jbitmap) { |
| 45 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 46 | } |
| 47 | |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 48 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 49 | void* addr = bitmap.isValid() ? bitmap.getPixels() : nullptr; |
| 50 | |
John Reck | 00799f7 | 2017-03-01 18:05:41 -0800 | [diff] [blame] | 51 | if (!addr) { |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 52 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 53 | } |
| 54 | |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 55 | ABitmap_acquireRef(bitmap.get()); |
| 56 | |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 57 | if (addrPtr) { |
| 58 | *addrPtr = addr; |
| 59 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 60 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) { |
| 64 | if (NULL == env || NULL == jbitmap) { |
| 65 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 66 | } |
| 67 | |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 68 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 69 | |
| 70 | if (!bitmap.isValid()) { |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 71 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 72 | } |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 73 | |
| 74 | bitmap.notifyPixelsChanged(); |
| 75 | ABitmap_releaseRef(bitmap.get()); |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 76 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 77 | } |
Leon Scroggins III | 9010e8b | 2019-08-20 11:27:17 -0400 | [diff] [blame^] | 78 | |
| 79 | int AndroidBitmap_compress(const AndroidBitmapInfo* info, |
| 80 | int32_t dataSpace, |
| 81 | const void* pixels, |
| 82 | int32_t format, int32_t quality, |
| 83 | void* userContext, |
| 84 | AndroidBitmap_compress_write_fn fn) { |
| 85 | if (NULL == info || NULL == pixels || NULL == fn) { |
| 86 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 87 | } |
| 88 | if (quality < 0 || quality > 100) { |
| 89 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 90 | } |
| 91 | |
| 92 | return ABitmap_compress(info, (ADataSpace) dataSpace, pixels, |
| 93 | (AndroidBitmapCompressFormat) format, quality, userContext, fn); |
| 94 | } |