1/*
2 * Copyright (C) 2010 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 "utils.h"
18
19#include <android/bitmap.h>
20
21namespace android {
22namespace apps {
23namespace photoeditor {
24namespace utils {
25
26int ExtractInfoFromBitmap(JNIEnv *env,
27    AndroidBitmapInfo *src_info, AndroidBitmapInfo *dst_info,
28    jobject src_bitmap, jobject dst_bitmap) {
29  int ret;
30  /*
31   * The info->format of both bitmap should be ANDROID_BITMAP_FORMAT_RGBA_8888.
32   * Both bitmaps shall also be in the same dimension.
33   */
34  if ((ret = AndroidBitmap_getInfo(env, src_bitmap, src_info)) < 0) {
35    LOGE("AndroidBitmap_getInfo(src_bitmap) failed, error=%d", ret);
36    return ret;
37  }
38  if ((ret = AndroidBitmap_getInfo(env, dst_bitmap, dst_info)) < 0) {
39    LOGE("AndroidBitmap_getInfo(dst_bitmap) failed, error=%d", ret);
40    return ret;
41  }
42  return 0;
43}
44
45int LockBitmaps(JNIEnv* env, jobject src_bitmap, jobject dst_bitmap,
46    AndroidBitmapInfo* src_info, AndroidBitmapInfo* dst_info,
47    void** src_pixels, void** dst_pixels) {
48  int ret;
49  if ((ret = ExtractInfoFromBitmap(env, src_info, dst_info, src_bitmap, dst_bitmap)) < 0) {
50    return ret;
51  }
52  if ((ret = AndroidBitmap_lockPixels(env, src_bitmap, src_pixels)) < 0) {
53    LOGE("AndroidBitmap_lockPixels(src_bitmap) failed, error=%d", ret);
54    return ret;
55  }
56  if ((ret = AndroidBitmap_lockPixels(env, dst_bitmap, dst_pixels)) < 0) {
57    LOGE("AndroidBitmap_lockPixels(dst_bitmap) failed, error=%d", ret);
58    AndroidBitmap_unlockPixels(env, src_bitmap);
59    return ret;
60  }
61  return 0;
62}
63
64void UnlockBitmaps(JNIEnv* env, jobject src_bitmap, jobject dst_bitmap) {
65  AndroidBitmap_unlockPixels(env, src_bitmap);
66  AndroidBitmap_unlockPixels(env, dst_bitmap);
67}
68
69}  // namespace utils
70}  // namespace photoeditor
71}  // namespace apps
72}  // namespace android
73
74