From 3a05649c7137aab832dc2a1e7634f06030bc7b4b Mon Sep 17 00:00:00 2001 From: tarsin Date: Sun, 24 Jul 2022 22:34:19 +0800 Subject: [PATCH] Code clean --- native/src/main/jni/image/config.h | 37 ----------- native/src/main/jni/image/image.c | 65 ++++++++++-------- native/src/main/jni/image/image.h | 55 ++++++--------- native/src/main/jni/image/image_plain.c | 85 ++---------------------- native/src/main/jni/image/image_plain.h | 23 +------ native/src/main/jni/image/java_wrapper.c | 38 +++-------- 6 files changed, 70 insertions(+), 233 deletions(-) delete mode 100644 native/src/main/jni/image/config.h diff --git a/native/src/main/jni/image/config.h b/native/src/main/jni/image/config.h deleted file mode 100644 index 7268a3d4b..000000000 --- a/native/src/main/jni/image/config.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2015 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// -// Created by Hippo on 12/27/2015. -// - -#ifndef IMAGE_CONFIG_H -#define IMAGE_CONFIG_H - -#ifndef IMAGE_NOT_SUPPORT_PLAIN -#define IMAGE_SUPPORT_PLAIN -#endif -#ifndef IMAGE_NOT_SUPPORT_JPEG -#define IMAGE_SUPPORT_JPEG -#endif -#ifndef IMAGE_NOT_SUPPORT_PNG -#define IMAGE_SUPPORT_PNG -#endif -#ifndef IMAGE_NOT_SUPPORT_GIF -#define IMAGE_SUPPORT_GIF -#endif - -#endif //IMAGE_CONFIG_H diff --git a/native/src/main/jni/image/image.c b/native/src/main/jni/image/image.c index 1dce40bd3..9888c395b 100644 --- a/native/src/main/jni/image/image.c +++ b/native/src/main/jni/image/image.c @@ -24,6 +24,7 @@ #include "image.h" #include "image_plain.h" +#include "image_utils.h" #include "filter/clahe.h" #include "filter/gray.h" #include "log.h" @@ -36,81 +37,84 @@ void* decode(JNIEnv* env, int fd, bool partially, int* format) if (r) return NULL; *format = AImageDecoder_isAnimated(decoder); - AImageDecoderHeaderInfo *headerInfo = AImageDecoder_getHeaderInfo(decoder); + const AImageDecoderHeaderInfo *headerInfo = AImageDecoder_getHeaderInfo(decoder); + size_t stride = AImageDecoder_getMinimumStride(decoder); IMAGE *image = malloc(sizeof(IMAGE)); image->height = AImageDecoderHeaderInfo_getHeight(headerInfo); image->width = AImageDecoderHeaderInfo_getWidth(headerInfo); - unsigned long bufferLen = image->height * image->width * 4; - image->buffer = malloc(bufferLen); - AImageDecoder_decodeImage(decoder, image->buffer, AImageDecoder_getMinimumStride(decoder), bufferLen); - AImageDecoder_delete(decoder); + image->decoder = decoder; + image->bufferLen = image->height * stride; + image->buffer = malloc(image->bufferLen); + AImageDecoder_decodeImage(decoder, image->buffer, stride, image->bufferLen); return image; } -void* create(unsigned int width, unsigned int height, const void* data) +void* create(int32_t width, int32_t height, const void* data) { return PLAIN_create(width, height, data); } -bool complete(JNIEnv* env, void* image, int format) +bool complete(JNIEnv* env, IMAGE * image, int format) { return true; } -bool is_completed(void* image, int format) +bool is_completed(IMAGE * image, int format) { return true; } -int get_width(void* image, int format) +int get_width(IMAGE * image, int format) { - return ((IMAGE*)image)->width; + return image->width; } -int get_height(void* image, int format) +int get_height(IMAGE * image, int format) { - return ((IMAGE*)image)->height; + return image->height; } -int get_byte_count(void* image, int format) +int get_byte_count(IMAGE * image, int format) { - return ((IMAGE*)image)->width * ((IMAGE*)image)->height * 4; + return image->bufferLen; } -void render(void* image, int format, int src_x, int src_y, +void render(IMAGE * image, int format, int src_x, int src_y, void* dst, int dst_w, int dst_h, int dst_x, int dst_y, int width, int height, bool fill_blank, int default_color) { - PLAIN_render((IMAGE *) image, src_x, src_y, dst, dst_w, dst_h, dst_x, dst_y, width, height, fill_blank, default_color); + copy_pixels(image->buffer, image->width, image->height, src_x, src_y, dst, dst_w, dst_h, dst_x, dst_y, width, height, fill_blank, default_color); } -void advance(void* image, int format) +void advance(IMAGE * image, int format) { + if (format) { + AImageDecoder_advanceFrame(image->decoder); + } } -int get_delay(void* image, int format) +int get_delay(IMAGE * image, int format) { return 0; } -int get_frame_count(void* image, int format) +int get_frame_count(IMAGE * image, int format) { return 1; } -bool is_opaque(void* image, int format) +bool is_opaque(IMAGE * image, int format) { return false; } -static void get_image_data(void* image, int format, void** pixel, int* width, int* height) { - PLAIN* plain = (PLAIN*) image; - *pixel = PLAIN_get_pixels(plain); - *width = PLAIN_get_width(plain); - *height = PLAIN_get_height(plain); +static void get_image_data(IMAGE * image, int format, void** pixel, int* width, int* height) { + *pixel = image->buffer; + *width = image->width; + *height = image->height; } -bool is_gray(void* image, int format, int error) +bool is_gray(IMAGE * image, int format, int error) { void* pixel = NULL; int width = 0; @@ -125,7 +129,7 @@ bool is_gray(void* image, int format, int error) return IMAGE_is_gray(pixel, width, height, error); } -void clahe(void* image, int format, bool to_gray) +void clahe(IMAGE * image, int format, bool to_gray) { void* pixel = NULL; int width = 0; @@ -140,7 +144,10 @@ void clahe(void* image, int format, bool to_gray) IMAGE_clahe(pixel, width, height, to_gray); } -void recycle(JNIEnv *env, void* image, int format) +void recycle(JNIEnv *env, IMAGE* image, int format) { - PLAIN_recycle((PLAIN*) image); + free(image->buffer); + image->buffer = NULL; + AImageDecoder_delete(image->decoder); + free(image); } diff --git a/native/src/main/jni/image/image.h b/native/src/main/jni/image/image.h index f088e7ba8..788c037a6 100644 --- a/native/src/main/jni/image/image.h +++ b/native/src/main/jni/image/image.h @@ -21,52 +21,35 @@ #ifndef IMAGE_IMAGE_H #define IMAGE_IMAGE_H +#include #include #include -#include "config.h" - -#define IMAGE_FORMAT_UNKNOWN -1 -#ifdef IMAGE_SUPPORT_PLAIN -#define IMAGE_FORMAT_PLAIN 0 -#endif -#ifdef IMAGE_SUPPORT_JPEG -#define IMAGE_FORMAT_JPEG 1 -#endif -#ifdef IMAGE_SUPPORT_PNG -#define IMAGE_FORMAT_PNG 2 -#endif -#ifdef IMAGE_SUPPORT_GIF -#define IMAGE_FORMAT_GIF 3 -#endif - -#define IMAGE_MAX_SUPPORTED_FORMAT_COUNT 3 - typedef struct { - unsigned int width; - unsigned int height; + int32_t width; + int32_t height; void* buffer; + int bufferLen; + AImageDecoder* decoder; } IMAGE; void* decode(JNIEnv* env, int fd, bool partially, int* format); -void* create(unsigned int width, unsigned int height, const void* data); -bool complete(JNIEnv* env, void* image, int format); -bool is_completed(void* image, int format); -int get_width(void* image, int format); -int get_height(void* image, int format); -int get_byte_count(void* image, int format); -void render(void* image, int format, int src_x, int src_y, +void* create(int32_t width, int32_t height, const void* data); +bool complete(JNIEnv* env, IMAGE * image, int format); +bool is_completed(IMAGE * image, int format); +int get_width(IMAGE * image, int format); +int get_height(IMAGE * image, int format); +int get_byte_count(IMAGE * image, int format); +void render(IMAGE * image, int format, int src_x, int src_y, void* dst, int dst_w, int dst_h, int dst_x, int dst_y, int width, int height, bool fill_blank, int default_color); -void advance(void* image, int format); -int get_delay(void* image, int format); -int get_frame_count(void* image, int format); -bool is_opaque(void* image, int format); -bool is_gray(void* image, int format, int error); -void clahe(void* image, int format, bool to_gray); -void recycle(JNIEnv *env, void* image, int format); -int get_supported_formats(int *); -const char *get_decoder_description(int); +void advance(IMAGE * image, int format); +int get_delay(IMAGE * image, int format); +int get_frame_count(IMAGE * image, int format); +bool is_opaque(IMAGE * image, int format); +bool is_gray(IMAGE * image, int format, int error); +void clahe(IMAGE * image, int format, bool to_gray); +void recycle(JNIEnv *env, IMAGE * image, int format); #endif //IMAGE_IMAGE_H diff --git a/native/src/main/jni/image/image_plain.c b/native/src/main/jni/image/image_plain.c index 2480e8515..cea9c7e29 100644 --- a/native/src/main/jni/image/image_plain.c +++ b/native/src/main/jni/image/image_plain.c @@ -14,13 +14,6 @@ * limitations under the License. */ -// -// Created by Hippo on 5/3/2016. -// - -#include "config.h" -#ifdef IMAGE_SUPPORT_PLAIN - #include #include @@ -28,13 +21,13 @@ #include "image_utils.h" #include "log.h" -void* PLAIN_create(unsigned int width, unsigned int height, const void* data) +void* PLAIN_create(int32_t width, int32_t height, const void* data) { - PLAIN* plain = NULL; + IMAGE * plain = NULL; void* buffer = NULL; size_t length; - plain = (PLAIN*) malloc(sizeof(PLAIN)); + plain = malloc(sizeof(IMAGE)); if (plain == NULL) { WTF_OM; return NULL; @@ -54,76 +47,8 @@ void* PLAIN_create(unsigned int width, unsigned int height, const void* data) plain->width = width; plain->height = height; plain->buffer = buffer; + plain->bufferLen = length; + plain->decoder = NULL; return plain; } - -bool PLAIN_complete(PLAIN* plain) -{ - return true; -} - -bool PLAIN_is_completed(PLAIN* plain) -{ - return true; -} - -void* PLAIN_get_pixels(PLAIN* plain) -{ - return plain->buffer; -} - -int PLAIN_get_width(PLAIN* plain) -{ - return plain->width; -} - -int PLAIN_get_height(PLAIN* plain) -{ - return plain->height; -} - -int PLAIN_get_byte_count(PLAIN* plain) -{ - return plain->width * plain->height * 4; -} - -void PLAIN_render(PLAIN* plain, int src_x, int src_y, - void* dst, int dst_w, int dst_h, int dst_x, int dst_y, - int width, int height, bool fill_blank, int default_color) -{ - copy_pixels(plain->buffer, plain->width, plain->height, src_x, src_y, - dst, dst_w, dst_h, dst_x, dst_y, - width, height, fill_blank, default_color); -} - -void PLAIN_advance(PLAIN* plain) -{ - -} - -int PLAIN_get_delay(PLAIN* plain) -{ - return 0; -} - -int PLAIN_get_frame_count(PLAIN* plain) -{ - return 1; -} - -bool PLAIN_is_opaque(PLAIN* plain) -{ - // TODO Check plain all alpha - return false; -} - -void PLAIN_recycle(PLAIN* plain) -{ - free(plain->buffer); - plain->buffer = NULL; - - free(plain); -} - -#endif // IMAGE_SUPPORT_PLAIN diff --git a/native/src/main/jni/image/image_plain.h b/native/src/main/jni/image/image_plain.h index 2c7763d5d..665ef0f84 100644 --- a/native/src/main/jni/image/image_plain.h +++ b/native/src/main/jni/image/image_plain.h @@ -21,31 +21,10 @@ #ifndef IMAGE_IMAGE_PLAIN_H #define IMAGE_IMAGE_PLAIN_H -#include "config.h" -#ifdef IMAGE_SUPPORT_PLAIN - #include #include "image.h" -typedef IMAGE PLAIN; - -void* PLAIN_create(unsigned int width, unsigned int height, const void* data); -bool PLAIN_complete(PLAIN* plain); -bool PLAIN_is_completed(PLAIN* plain); -void* PLAIN_get_pixels(PLAIN* plain); -int PLAIN_get_width(PLAIN* plain); -int PLAIN_get_height(PLAIN* plain); -int PLAIN_get_byte_count(PLAIN* plain); -void PLAIN_render(PLAIN* plain, int src_x, int src_y, - void* dst, int dst_w, int dst_h, int dst_x, int dst_y, - int width, int height, bool fill_blank, int default_color); -void PLAIN_advance(PLAIN* plain); -int PLAIN_get_delay(PLAIN* plain); -int PLAIN_get_frame_count(PLAIN* plain); -bool PLAIN_is_opaque(PLAIN* plain); -void PLAIN_recycle(PLAIN* plain); - -#endif // IMAGE_SUPPORT_PLAIN +void* PLAIN_create(int32_t width, int32_t height, const void* data); #endif // IMAGE_IMAGE_PLAIN_H diff --git a/native/src/main/jni/image/java_wrapper.c b/native/src/main/jni/image/java_wrapper.c index ccf300c63..0f9edf048 100644 --- a/native/src/main/jni/image/java_wrapper.c +++ b/native/src/main/jni/image/java_wrapper.c @@ -14,10 +14,6 @@ * limitations under the License. */ -// -// Created by Hippo on 12/27/2015. -// - #include #include #include @@ -85,29 +81,6 @@ jobject create_image_object(JNIEnv* env, void* ptr, int format, int width, int h } } -JNIEXPORT jobject JNICALL -Java_com_hippo_image_Image_nativeDecode(JNIEnv* env, - jclass clazz, jobject fd, jboolean partially) -{ - int format; - void* image; - jobject image_object; - - image = decode(env, jniGetFDFromFileDescriptor(env, fd), partially, &format); - if (image == NULL) { - return NULL; - } - - image_object = create_image_object(env, image, format, - get_width(image, format), get_height(image, format)); - if (image_object == NULL) { - recycle(env, image, format); - return NULL; - } else { - return image_object; - } -} - JNIEXPORT jobject JNICALL Java_com_hippo_image_Image_nativeDecodeFdInt(JNIEnv* env, jclass clazz, jint fd, jboolean partially) @@ -131,6 +104,13 @@ Java_com_hippo_image_Image_nativeDecodeFdInt(JNIEnv* env, } } +JNIEXPORT jobject JNICALL +Java_com_hippo_image_Image_nativeDecode(JNIEnv* env, + jclass clazz, jobject fd, jboolean partially) +{ + return Java_com_hippo_image_Image_nativeDecodeFdInt(env, clazz, jniGetFDFromFileDescriptor(env, fd), partially); +} + JNIEXPORT jobject JNICALL Java_com_hippo_image_Image_nativeCreate(JNIEnv* env, jclass clazz, jobject bitmap) @@ -155,10 +135,10 @@ Java_com_hippo_image_Image_nativeCreate(JNIEnv* env, return NULL; } - image_object = create_image_object(env, image, IMAGE_FORMAT_PLAIN, + image_object = create_image_object(env, image, 0, info.width, info.height); if (image_object == NULL) { - recycle(env, image, IMAGE_FORMAT_PLAIN); + recycle(env, image, 0); return NULL; } else { return image_object;