blob: ea8a521c9d5f45cd6c40866ada6ed7f90c04c0f4 [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
79int 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}