Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/jni/android/androiddrawdevice.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 // Copyright (C) 2004-2021 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 /* AndroidDrawDevice interface */ | |
| 24 | |
| 25 static jlong | |
| 26 newNativeAndroidDrawDevice(JNIEnv *env, jobject self, fz_context *ctx, jobject obj, jint width, jint height, NativeDeviceLockFn *lock, NativeDeviceUnlockFn *unlock, jint xOrigin, jint yOrigin, jint patchX0, jint patchY0, jint patchX1, jint patchY1, jboolean clear) | |
| 27 { | |
| 28 fz_device *device = NULL; | |
| 29 fz_pixmap *pixmap = NULL; | |
| 30 unsigned char dummy; | |
| 31 NativeDeviceInfo *ninfo = NULL; | |
| 32 NativeDeviceInfo *info; | |
| 33 fz_irect bbox; | |
| 34 int err = 0; | |
| 35 | |
| 36 if (!ctx) return 0; | |
| 37 | |
| 38 /* Ensure patch fits inside bitmap. */ | |
| 39 if (patchX0 < 0) patchX0 = 0; | |
| 40 if (patchY0 < 0) patchY0 = 0; | |
| 41 if (patchX1 > width) patchX1 = width; | |
| 42 if (patchY1 > height) patchY1 = height; | |
| 43 | |
| 44 bbox.x0 = xOrigin + patchX0; | |
| 45 bbox.y0 = yOrigin + patchY0; | |
| 46 bbox.x1 = xOrigin + patchX1; | |
| 47 bbox.y1 = yOrigin + patchY1; | |
| 48 | |
| 49 fz_var(pixmap); | |
| 50 fz_var(ninfo); | |
| 51 | |
| 52 fz_try(ctx) | |
| 53 { | |
| 54 pixmap = fz_new_pixmap_with_bbox_and_data(ctx, fz_device_rgb(ctx), bbox, NULL, 1, &dummy); | |
| 55 pixmap->stride = width * sizeof(int32_t); | |
| 56 ninfo = Memento_label(fz_malloc(ctx, sizeof(*ninfo)), "newNativeAndroidDrawDevice"); | |
| 57 ninfo->pixmap = pixmap; | |
| 58 ninfo->lock = lock; | |
| 59 ninfo->unlock = unlock; | |
| 60 ninfo->xOffset = patchX0; | |
| 61 ninfo->yOffset = patchY0; | |
| 62 ninfo->width = width; | |
| 63 ninfo->height = height; | |
| 64 ninfo->object = obj; | |
| 65 (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, jlong_cast(ninfo)); | |
| 66 (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, obj); | |
| 67 if (clear) | |
| 68 { | |
| 69 info = lockNativeDevice(env,self,&err); | |
| 70 if (!err) | |
| 71 { | |
| 72 fz_clear_pixmap_with_value(ctx, pixmap, 0xff); | |
| 73 unlockNativeDevice(env,ninfo); | |
| 74 } | |
| 75 } | |
| 76 if (!err) | |
| 77 device = fz_new_draw_device(ctx, fz_identity, pixmap); | |
| 78 } | |
| 79 fz_catch(ctx) | |
| 80 { | |
| 81 (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, 0); | |
| 82 (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, NULL); | |
| 83 fz_drop_pixmap(ctx, pixmap); | |
| 84 fz_free(ctx, ninfo); | |
| 85 jni_rethrow(env, ctx); | |
| 86 } | |
| 87 | |
| 88 /* lockNativeDevice will already have raised a JNI error if there was one. */ | |
| 89 if (err) | |
| 90 { | |
| 91 jthrowable t = (*env)->ExceptionOccurred(env); | |
| 92 (*env)->ExceptionClear(env); | |
| 93 (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, 0); | |
| 94 (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, NULL); | |
| 95 fz_drop_pixmap(ctx, pixmap); | |
| 96 fz_free(ctx, ninfo); | |
| 97 if ((*env)->Throw(env, t) < 0) | |
| 98 (*env)->ThrowNew(env, cls_RuntimeException, "could not rethrow exception after cleanup when locking failed"); | |
| 99 return 0; | |
| 100 } | |
| 101 | |
| 102 return jlong_cast(device); | |
| 103 } | |
| 104 | |
| 105 static int androidDrawDevice_lock(JNIEnv *env, NativeDeviceInfo *info) | |
| 106 { | |
| 107 uint8_t *pixels; | |
| 108 int ret; | |
| 109 int phase = 0; | |
| 110 fz_context *ctx = get_context(env); | |
| 111 size_t size = info->width * info->height * 4; | |
| 112 | |
| 113 if (!ctx) | |
| 114 { | |
| 115 jni_throw_run_imp(env, "no context in DrawDevice call"); | |
| 116 return 1; | |
| 117 } | |
| 118 | |
| 119 assert(info); | |
| 120 assert(info->object); | |
| 121 | |
| 122 while (1) | |
| 123 { | |
| 124 ret = AndroidBitmap_lockPixels(env, info->object, (void **)&pixels); | |
| 125 if (ret == ANDROID_BITMAP_RESULT_SUCCESS) | |
| 126 break; | |
| 127 if (!fz_store_scavenge_external(ctx, size, &phase)) | |
| 128 break; /* Failed to free any */ | |
| 129 } | |
| 130 if (ret != ANDROID_BITMAP_RESULT_SUCCESS) | |
| 131 { | |
| 132 info->pixmap->samples = NULL; | |
| 133 jni_throw_run_imp(env, "bitmap lock failed in DrawDevice call"); | |
| 134 return 1; | |
| 135 } | |
| 136 | |
| 137 /* Now offset pixels to allow for the page offsets */ | |
| 138 pixels += sizeof(int32_t) * (info->xOffset + info->width * info->yOffset); | |
| 139 | |
| 140 info->pixmap->samples = pixels; | |
| 141 | |
| 142 return 0; | |
| 143 } | |
| 144 | |
| 145 static void androidDrawDevice_unlock(JNIEnv *env, NativeDeviceInfo *info) | |
| 146 { | |
| 147 assert(info); | |
| 148 assert(info->object); | |
| 149 | |
| 150 info->pixmap->samples = NULL; | |
| 151 if (AndroidBitmap_unlockPixels(env, info->object) != ANDROID_BITMAP_RESULT_SUCCESS) | |
| 152 jni_throw_run_void(env, "bitmap unlock failed in DrawDevice call"); | |
| 153 } | |
| 154 | |
| 155 JNIEXPORT void JNICALL | |
| 156 FUN(android_AndroidDrawDevice_invertLuminance)(JNIEnv *env, jobject self) | |
| 157 { | |
| 158 fz_context *ctx = get_context(env); | |
| 159 fz_device *dev = from_Device(env, self); | |
| 160 NativeDeviceInfo *info; | |
| 161 int err; | |
| 162 | |
| 163 if (!ctx || !dev) return; | |
| 164 | |
| 165 info = lockNativeDevice(env, self, &err); | |
| 166 if (err) | |
| 167 return; | |
| 168 | |
| 169 fz_try(ctx) | |
| 170 fz_invert_pixmap_luminance(ctx, info->pixmap); | |
| 171 fz_always(ctx) | |
| 172 unlockNativeDevice(env, info); | |
| 173 fz_catch(ctx) | |
| 174 jni_rethrow_void(env, ctx); | |
| 175 } | |
| 176 | |
| 177 JNIEXPORT jlong JNICALL | |
| 178 FUN(android_AndroidDrawDevice_newNative)(JNIEnv *env, jclass self, jobject jbitmap, jint xOrigin, jint yOrigin, jint pX0, jint pY0, jint pX1, jint pY1, jboolean clear) | |
| 179 { | |
| 180 fz_context *ctx = get_context(env); | |
| 181 AndroidBitmapInfo info; | |
| 182 jlong device = 0; | |
| 183 int ret; | |
| 184 | |
| 185 if (!ctx) return 0; | |
| 186 if (!jbitmap) jni_throw_arg(env, "bitmap must not be null"); | |
| 187 | |
| 188 if ((ret = AndroidBitmap_getInfo(env, jbitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS) | |
| 189 jni_throw_run(env, "new DrawDevice failed to get bitmap info"); | |
| 190 if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) | |
| 191 jni_throw_run(env, "new DrawDevice failed as bitmap format is not RGBA_8888"); | |
| 192 if (info.stride != info.width * 4) | |
| 193 jni_throw_run(env, "new DrawDevice failed as bitmap width != stride"); | |
| 194 | |
| 195 fz_try(ctx) | |
| 196 device = newNativeAndroidDrawDevice(env, self, ctx, jbitmap, info.width, info.height, androidDrawDevice_lock, androidDrawDevice_unlock, xOrigin, yOrigin, pX0, pY0, pX1, pY1, clear); | |
| 197 fz_catch(ctx) | |
| 198 jni_rethrow(env, ctx); | |
| 199 | |
| 200 return device; | |
| 201 } |
