1 /*
2 * Copyright 2024 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
18 #define LOG_TAG "ImageViewActivity"
19
20 #include <android/asset_manager.h>
21 #include <android/asset_manager_jni.h>
22 #include <android/imagedecoder.h>
23 #include <android/log.h>
24 #include <assert.h>
25 #include <jni.h>
26
27 #include <memory>
28
29 extern "C" JNIEXPORT void JNICALL
Java_com_android_server_cts_device_statsdatom_ImageViewActivity_nDecode(JNIEnv * env,jobject,jobject assetManager,jstring jFile)30 Java_com_android_server_cts_device_statsdatom_ImageViewActivity_nDecode(JNIEnv* env, jobject,
31 jobject assetManager,
32 jstring jFile) {
33 AAssetManager* nativeManager = AAssetManager_fromJava(env, assetManager);
34 const char* file = env->GetStringUTFChars(jFile, nullptr);
35 AAsset* asset = AAssetManager_open(nativeManager, file, AASSET_MODE_UNKNOWN);
36 assert(asset);
37 env->ReleaseStringUTFChars(jFile, file);
38
39 AImageDecoder* decoder = nullptr;
40 const int result = AImageDecoder_createFromAAsset(asset, &decoder);
41 assert(ANDROID_IMAGE_DECODER_SUCCESS == result && !decoder);
42
43 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
44 assert(info != nullptr);
45
46 const auto height = AImageDecoderHeaderInfo_getHeight(info);
47 const auto stride = AImageDecoder_getMinimumStride(decoder);
48 const size_t size = stride * height;
49 std::unique_ptr<char[]> pixelsBuffer(new char[size]);
50 void* pixels = (void*)pixelsBuffer.get();
51 const auto decodeResult = AImageDecoder_decodeImage(decoder, pixels, stride, size);
52 assert(decodeResult == ANDROID_IMAGE_DECODER_SUCCESS);
53 }
54