blob: 3d0fd254f9a82dcdc95c28d6db8157f424c94b99 [file] [log] [blame]
Dima Zavin32276312010-02-04 12:15:09 -08001/*
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 III9010e8b2019-08-20 11:27:17 -040018#include <android/data_space.h>
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050019#include <android/graphics/bitmap.h>
Leon Scroggins III1994fcb2020-01-10 13:40:07 -050020#include <android/data_space.h>
Dima Zavin32276312010-02-04 12:15:09 -080021
22int 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 Zavin32276312010-02-04 12:15:09 -080028 if (info) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050029 *info = ABitmap_getInfoFromJava(env, jbitmap);
Dima Zavin32276312010-02-04 12:15:09 -080030 }
Andrew Hsieheba82542012-12-12 11:27:44 +080031 return ANDROID_BITMAP_RESULT_SUCCESS;
Dima Zavin32276312010-02-04 12:15:09 -080032}
33
Leon Scroggins III1994fcb2020-01-10 13:40:07 -050034int32_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 Zavin32276312010-02-04 12:15:09 -080043int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) {
44 if (NULL == env || NULL == jbitmap) {
45 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
46 }
47
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050048 android::graphics::Bitmap bitmap(env, jbitmap);
49 void* addr = bitmap.isValid() ? bitmap.getPixels() : nullptr;
50
John Reck00799f72017-03-01 18:05:41 -080051 if (!addr) {
Dima Zavin32276312010-02-04 12:15:09 -080052 return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
53 }
54
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050055 ABitmap_acquireRef(bitmap.get());
56
Dima Zavin32276312010-02-04 12:15:09 -080057 if (addrPtr) {
58 *addrPtr = addr;
59 }
Andrew Hsieheba82542012-12-12 11:27:44 +080060 return ANDROID_BITMAP_RESULT_SUCCESS;
Dima Zavin32276312010-02-04 12:15:09 -080061}
62
63int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) {
64 if (NULL == env || NULL == jbitmap) {
65 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
66 }
67
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050068 android::graphics::Bitmap bitmap(env, jbitmap);
69
70 if (!bitmap.isValid()) {
Dima Zavin32276312010-02-04 12:15:09 -080071 return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
72 }
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050073
74 bitmap.notifyPixelsChanged();
75 ABitmap_releaseRef(bitmap.get());
Andrew Hsieheba82542012-12-12 11:27:44 +080076 return ANDROID_BITMAP_RESULT_SUCCESS;
Dima Zavin32276312010-02-04 12:15:09 -080077}
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040078
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050079int AndroidBitmap_getHardwareBuffer(JNIEnv* env, jobject jbitmap, AHardwareBuffer** outBuffer) {
80 if (NULL == env || NULL == jbitmap || NULL == outBuffer) {
81 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
82 }
83
84 android::graphics::Bitmap bitmap(env, jbitmap);
85
86 if (!bitmap.isValid()) {
87 return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
88 }
89
90 *outBuffer = bitmap.getHardwareBuffer();
91 return *outBuffer == NULL ? ANDROID_BITMAP_RESULT_BAD_PARAMETER : ANDROID_BITMAP_RESULT_SUCCESS;
92}
93
Leon Scroggins III9010e8b2019-08-20 11:27:17 -040094int AndroidBitmap_compress(const AndroidBitmapInfo* info,
95 int32_t dataSpace,
96 const void* pixels,
97 int32_t format, int32_t quality,
98 void* userContext,
Leon Scroggins III7871f492020-01-22 11:57:19 -050099 AndroidBitmap_CompressWriteFunc fn) {
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400100 if (NULL == info || NULL == pixels || NULL == fn) {
101 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
102 }
103 if (quality < 0 || quality > 100) {
104 return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
105 }
106
107 return ABitmap_compress(info, (ADataSpace) dataSpace, pixels,
108 (AndroidBitmapCompressFormat) format, quality, userContext, fn);
109}