comparison mupdf-source/platform/java/jni/image.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2023 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 /* Image interface */
24
25 JNIEXPORT void JNICALL
26 FUN(Image_finalize)(JNIEnv *env, jobject self)
27 {
28 fz_context *ctx = get_context(env);
29 fz_image *image = from_Image_safe(env, self);
30 if (!ctx || !image) return;
31 (*env)->SetLongField(env, self, fid_Image_pointer, 0);
32 fz_drop_image(ctx, image);
33 }
34
35 JNIEXPORT jlong JNICALL
36 FUN(Image_newNativeFromPixmap)(JNIEnv *env, jobject self, jobject jpixmap)
37 {
38 fz_context *ctx = get_context(env);
39 fz_pixmap *pixmap = from_Pixmap(env, jpixmap);
40 fz_image *image = NULL;
41
42 if (!ctx) return 0;
43 if (!pixmap) jni_throw_arg(env, "pixmap must not be null");
44
45 fz_try(ctx)
46 image = fz_new_image_from_pixmap(ctx, pixmap, NULL);
47 fz_catch(ctx)
48 jni_rethrow(env, ctx);
49
50 return jlong_cast(image);
51 }
52
53 JNIEXPORT jlong JNICALL
54 FUN(Image_newNativeFromFile)(JNIEnv *env, jobject self, jstring jfilename)
55 {
56 fz_context *ctx = get_context(env);
57 const char *filename = "null";
58 fz_image *image = NULL;
59
60 if (!ctx) return 0;
61 if (!jfilename) jni_throw_arg(env, "filename must not be null");
62
63 filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
64 if (!filename) return 0;
65
66 fz_try(ctx)
67 image = fz_new_image_from_file(ctx, filename);
68 fz_always(ctx)
69 (*env)->ReleaseStringUTFChars(env, jfilename, filename);
70 fz_catch(ctx)
71 jni_rethrow(env, ctx);
72
73 return jlong_cast(image);
74 }
75
76 JNIEXPORT jlong JNICALL
77 FUN(Image_newNativeFromBytes)(JNIEnv *env, jobject self, jbyteArray jByteArray)
78 {
79 fz_context *ctx = get_context(env);
80 fz_image *image = NULL;
81 jbyte *bytes = NULL;
82 fz_buffer *buffer = NULL;
83 int count;
84
85 if (!ctx) return 0;
86 if (!jByteArray) jni_throw_arg(env, "jByteArray must not be null");
87
88 count = (*env)->GetArrayLength(env, jByteArray);
89 bytes = (*env)->GetByteArrayElements(env, jByteArray, NULL);
90 if (!bytes)
91 jni_throw_run(env, "cannot get buffer");
92
93 fz_var(buffer);
94 fz_try(ctx) {
95 buffer = fz_new_buffer_from_copied_data(ctx, (unsigned char *) bytes, count);
96 image = fz_new_image_from_buffer(ctx, buffer);
97 }
98 fz_always(ctx) {
99 fz_drop_buffer(ctx, buffer);
100 if (bytes) (*env)->ReleaseByteArrayElements(env, jByteArray, bytes, 0);
101 }
102 fz_catch(ctx) {
103 jni_rethrow(env, ctx);
104 }
105
106 return jlong_cast(image);
107 }
108
109 JNIEXPORT jlong JNICALL
110 FUN(Image_newNativeFromBuffer)(JNIEnv *env, jobject self, jobject jbuffer)
111 {
112 fz_context *ctx = get_context(env);
113 fz_image *image = NULL;
114 fz_buffer *buffer = from_Buffer_safe(env, jbuffer);
115
116 if (!ctx) return 0;
117 if (!jbuffer) jni_throw_arg(env, "buffer must not be null");
118
119 fz_try(ctx)
120 image = fz_new_image_from_buffer(ctx, buffer);
121 fz_catch(ctx)
122 jni_rethrow(env, ctx);
123
124 return jlong_cast(image);
125 }
126
127 JNIEXPORT jint JNICALL
128 FUN(Image_getWidth)(JNIEnv *env, jobject self)
129 {
130 fz_image *image = from_Image(env, self);
131 return image ? image->w : 0;
132 }
133
134 JNIEXPORT jint JNICALL
135 FUN(Image_getHeight)(JNIEnv *env, jobject self)
136 {
137 fz_image *image = from_Image(env, self);
138 return image ? image->h : 0;
139 }
140
141 JNIEXPORT jint JNICALL
142 FUN(Image_getNumberOfComponents)(JNIEnv *env, jobject self)
143 {
144 fz_image *image = from_Image(env, self);
145 return image ? image->n : 0;
146 }
147
148 JNIEXPORT jobject JNICALL
149 FUN(Image_getColorSpace)(JNIEnv *env, jobject self)
150 {
151 fz_context *ctx = get_context(env);
152 fz_image *image = from_Image(env, self);
153 if (!ctx || !image) return NULL;
154 return to_ColorSpace_safe(ctx, env, image->colorspace);
155 }
156
157 JNIEXPORT jint JNICALL
158 FUN(Image_getBitsPerComponent)(JNIEnv *env, jobject self)
159 {
160 fz_image *image = from_Image(env, self);
161 return image ? image->bpc : 0;
162 }
163
164 JNIEXPORT jint JNICALL
165 FUN(Image_getXResolution)(JNIEnv *env, jobject self)
166 {
167 fz_image *image = from_Image(env, self);
168 int xres = 0;
169 fz_image_resolution(image, &xres, NULL);
170 return xres;
171 }
172
173 JNIEXPORT jint JNICALL
174 FUN(Image_getYResolution)(JNIEnv *env, jobject self)
175 {
176 fz_image *image = from_Image(env, self);
177 int yres = 0;
178 fz_image_resolution(image, NULL, &yres);
179 return yres;
180 }
181
182 JNIEXPORT jboolean JNICALL
183 FUN(Image_getImageMask)(JNIEnv *env, jobject self)
184 {
185 fz_image *image = from_Image(env, self);
186 return image && image->imagemask ? JNI_TRUE : JNI_FALSE;
187 }
188
189 JNIEXPORT jboolean JNICALL
190 FUN(Image_getInterpolate)(JNIEnv *env, jobject self)
191 {
192 fz_image *image = from_Image(env, self);
193 return image && image->interpolate ? JNI_TRUE : JNI_FALSE;
194 }
195
196 JNIEXPORT jint JNICALL
197 FUN(Image_getOrientation)(JNIEnv *env, jobject self)
198 {
199 fz_context *ctx = get_context(env);
200 fz_image *image = from_Image(env, self);
201 return fz_image_orientation(ctx, image);
202 }
203
204 JNIEXPORT jobject JNICALL
205 FUN(Image_getMask)(JNIEnv *env, jobject self)
206 {
207 fz_context *ctx = get_context(env);
208 fz_image *img = from_Image(env, self);
209
210 if (!ctx || !img) return NULL;
211
212 return to_Image_safe(ctx, env, img->mask);
213 }
214
215 JNIEXPORT jobject JNICALL
216 FUN(Image_toPixmap)(JNIEnv *env, jobject self)
217 {
218 fz_context *ctx = get_context(env);
219 fz_image *img = from_Image(env, self);
220 fz_pixmap *pixmap = NULL;
221
222 if (!ctx || !img) return NULL;
223
224 fz_try(ctx)
225 pixmap = fz_get_pixmap_from_image(ctx, img, NULL, NULL, NULL, NULL);
226 fz_catch(ctx)
227 jni_rethrow(env, ctx);
228
229 return to_Pixmap_safe_own(ctx, env, pixmap);
230 }
231
232 JNIEXPORT jintArray JNICALL
233 FUN(Image_getColorKey)(JNIEnv *env, jobject self)
234 {
235 fz_context *ctx = get_context(env);
236 fz_image *img = from_Image(env, self);
237 int colorkey[FZ_MAX_COLORS * 2];
238
239 if (!ctx || !img) return NULL;
240
241 if (!img->use_colorkey)
242 return NULL;
243
244 memcpy(colorkey, img->colorkey, 2 * img->n * sizeof(int));
245 return to_intArray(ctx, env, colorkey, 2 * img->n);
246 }
247
248 JNIEXPORT void JNICALL
249 FUN(Image_setOrientation)(JNIEnv *env, jobject self, jint orientation)
250 {
251 fz_image *img = from_Image(env, self);
252 if (!img) return;
253 if (orientation < 0 || orientation > 8) jni_throw_oob_void(env, "orientation out of range");
254 img->orientation = orientation;
255 }
256
257 JNIEXPORT jfloatArray JNICALL
258 FUN(Image_getDecode)(JNIEnv *env, jobject self)
259 {
260 fz_context *ctx = get_context(env);
261 fz_image *img = from_Image(env, self);
262 float decode[FZ_MAX_COLORS * 2];
263
264 if (!ctx || !img) return NULL;
265
266 if (!img->use_decode)
267 return NULL;
268
269 memcpy(decode, img->decode, 2 * img->n * sizeof(float));
270 return to_floatArray(ctx, env, decode, 2 * img->n);
271 }