| 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> |
| 18 | #include <android/data_space.h> |
| 19 | #include <android/graphics/bitmap.h> |
| 20 | #include <android/data_space.h> |
| 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 | |
| 28 | if (info) { |
| 29 | *info = ABitmap_getInfoFromJava(env, jbitmap); |
| 30 | } |
| 31 | return ANDROID_BITMAP_RESULT_SUCCESS; |
| 32 | } |
| 33 | |
| 34 | int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) { |
| 35 | if (NULL == env || NULL == jbitmap) { |
| 36 | return ADATASPACE_UNKNOWN; |
| 37 | } |
| 38 | |
| 39 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 40 | if (!bitmap.isValid()) { |
| 41 | return ADATASPACE_UNKNOWN; |
| 42 | } |
| 43 | |
| 44 | return bitmap.getDataSpace(); |
| 45 | } |
| 46 | |
| 47 | int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) { |
| 48 | if (NULL == env || NULL == jbitmap) { |
| 49 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 50 | } |
| 51 | |
| 52 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 53 | void* addr = bitmap.isValid() ? bitmap.getPixels() : nullptr; |
| 54 | |
| 55 | if (!addr) { |
| 56 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 57 | } |
| 58 | |
| 59 | ABitmap_acquireRef(bitmap.get()); |
| 60 | |
| 61 | if (addrPtr) { |
| 62 | *addrPtr = addr; |
| 63 | } |
| 64 | return ANDROID_BITMAP_RESULT_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) { |
| 68 | if (NULL == env || NULL == jbitmap) { |
| 69 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 70 | } |
| 71 | |
| 72 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 73 | |
| 74 | if (!bitmap.isValid()) { |
| 75 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 76 | } |
| 77 | |
| 78 | bitmap.notifyPixelsChanged(); |
| 79 | ABitmap_releaseRef(bitmap.get()); |
| 80 | return ANDROID_BITMAP_RESULT_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | int AndroidBitmap_getHardwareBuffer(JNIEnv* env, jobject jbitmap, AHardwareBuffer** outBuffer) { |
| 84 | if (NULL == env || NULL == jbitmap || NULL == outBuffer) { |
| 85 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 86 | } |
| 87 | |
| 88 | android::graphics::Bitmap bitmap(env, jbitmap); |
| 89 | |
| 90 | if (!bitmap.isValid()) { |
| 91 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 92 | } |
| 93 | |
| 94 | *outBuffer = bitmap.getHardwareBuffer(); |
| 95 | return *outBuffer == NULL ? ANDROID_BITMAP_RESULT_BAD_PARAMETER : ANDROID_BITMAP_RESULT_SUCCESS; |
| 96 | } |
| 97 | |
| 98 | int AndroidBitmap_compress(const AndroidBitmapInfo* info, |
| 99 | int32_t dataSpace, |
| 100 | const void* pixels, |
| 101 | int32_t format, int32_t quality, |
| 102 | void* userContext, |
| 103 | AndroidBitmap_CompressWriteFunc fn) { |
| 104 | if (NULL == info || NULL == pixels || NULL == fn) { |
| 105 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 106 | } |
| 107 | if (quality < 0 || quality > 100) { |
| 108 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 109 | } |
| 110 | |
| 111 | return ABitmap_compress(info, (ADataSpace) dataSpace, pixels, |
| 112 | (AndroidBitmapCompressFormat) format, quality, userContext, fn); |
| 113 | } |