image.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* Image interface */
  23. JNIEXPORT void JNICALL
  24. FUN(Image_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_image *image = from_Image_safe(env, self);
  28. if (!ctx || !image) return;
  29. (*env)->SetLongField(env, self, fid_Image_pointer, 0);
  30. fz_drop_image(ctx, image);
  31. }
  32. JNIEXPORT jlong JNICALL
  33. FUN(Image_newNativeFromPixmap)(JNIEnv *env, jobject self, jobject jpixmap)
  34. {
  35. fz_context *ctx = get_context(env);
  36. fz_pixmap *pixmap = from_Pixmap(env, jpixmap);
  37. fz_image *image = NULL;
  38. if (!ctx) return 0;
  39. if (!pixmap) jni_throw_arg(env, "pixmap must not be null");
  40. fz_try(ctx)
  41. image = fz_new_image_from_pixmap(ctx, pixmap, NULL);
  42. fz_catch(ctx)
  43. jni_rethrow(env, ctx);
  44. return jlong_cast(image);
  45. }
  46. JNIEXPORT jlong JNICALL
  47. FUN(Image_newNativeFromFile)(JNIEnv *env, jobject self, jstring jfilename)
  48. {
  49. fz_context *ctx = get_context(env);
  50. const char *filename = "null";
  51. fz_image *image = NULL;
  52. if (!ctx) return 0;
  53. if (!jfilename) jni_throw_arg(env, "filename must not be null");
  54. filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
  55. if (!filename) return 0;
  56. fz_try(ctx)
  57. image = fz_new_image_from_file(ctx, filename);
  58. fz_always(ctx)
  59. (*env)->ReleaseStringUTFChars(env, jfilename, filename);
  60. fz_catch(ctx)
  61. jni_rethrow(env, ctx);
  62. return jlong_cast(image);
  63. }
  64. JNIEXPORT jlong JNICALL
  65. FUN(Image_newNativeFromBytes)(JNIEnv *env, jobject self, jbyteArray jByteArray)
  66. {
  67. fz_context *ctx = get_context(env);
  68. fz_image *image = NULL;
  69. jbyte *bytes = NULL;
  70. fz_buffer *buffer = NULL;
  71. int count;
  72. if (!ctx) return 0;
  73. if (!jByteArray) jni_throw_arg(env, "jByteArray must not be null");
  74. count = (*env)->GetArrayLength(env, jByteArray);
  75. bytes = (*env)->GetByteArrayElements(env, jByteArray, NULL);
  76. if (!bytes)
  77. jni_throw_run(env, "cannot get buffer");
  78. fz_var(buffer);
  79. fz_try(ctx) {
  80. buffer = fz_new_buffer_from_copied_data(ctx, (unsigned char *) bytes, count);
  81. image = fz_new_image_from_buffer(ctx, buffer);
  82. }
  83. fz_always(ctx) {
  84. fz_drop_buffer(ctx, buffer);
  85. if (bytes) (*env)->ReleaseByteArrayElements(env, jByteArray, bytes, 0);
  86. }
  87. fz_catch(ctx) {
  88. jni_rethrow(env, ctx);
  89. }
  90. return jlong_cast(image);
  91. }
  92. JNIEXPORT jlong JNICALL
  93. FUN(Image_newNativeFromBuffer)(JNIEnv *env, jobject self, jobject jbuffer)
  94. {
  95. fz_context *ctx = get_context(env);
  96. fz_image *image = NULL;
  97. fz_buffer *buffer = from_Buffer_safe(env, jbuffer);
  98. if (!ctx) return 0;
  99. if (!jbuffer) jni_throw_arg(env, "buffer must not be null");
  100. fz_try(ctx)
  101. image = fz_new_image_from_buffer(ctx, buffer);
  102. fz_catch(ctx)
  103. jni_rethrow(env, ctx);
  104. return jlong_cast(image);
  105. }
  106. JNIEXPORT jint JNICALL
  107. FUN(Image_getWidth)(JNIEnv *env, jobject self)
  108. {
  109. fz_image *image = from_Image(env, self);
  110. return image ? image->w : 0;
  111. }
  112. JNIEXPORT jint JNICALL
  113. FUN(Image_getHeight)(JNIEnv *env, jobject self)
  114. {
  115. fz_image *image = from_Image(env, self);
  116. return image ? image->h : 0;
  117. }
  118. JNIEXPORT jint JNICALL
  119. FUN(Image_getNumberOfComponents)(JNIEnv *env, jobject self)
  120. {
  121. fz_image *image = from_Image(env, self);
  122. return image ? image->n : 0;
  123. }
  124. JNIEXPORT jobject JNICALL
  125. FUN(Image_getColorSpace)(JNIEnv *env, jobject self)
  126. {
  127. fz_context *ctx = get_context(env);
  128. fz_image *image = from_Image(env, self);
  129. if (!ctx || !image) return NULL;
  130. return to_ColorSpace_safe(ctx, env, image->colorspace);
  131. }
  132. JNIEXPORT jint JNICALL
  133. FUN(Image_getBitsPerComponent)(JNIEnv *env, jobject self)
  134. {
  135. fz_image *image = from_Image(env, self);
  136. return image ? image->bpc : 0;
  137. }
  138. JNIEXPORT jint JNICALL
  139. FUN(Image_getXResolution)(JNIEnv *env, jobject self)
  140. {
  141. fz_image *image = from_Image(env, self);
  142. int xres = 0;
  143. fz_image_resolution(image, &xres, NULL);
  144. return xres;
  145. }
  146. JNIEXPORT jint JNICALL
  147. FUN(Image_getYResolution)(JNIEnv *env, jobject self)
  148. {
  149. fz_image *image = from_Image(env, self);
  150. int yres = 0;
  151. fz_image_resolution(image, NULL, &yres);
  152. return yres;
  153. }
  154. JNIEXPORT jboolean JNICALL
  155. FUN(Image_getImageMask)(JNIEnv *env, jobject self)
  156. {
  157. fz_image *image = from_Image(env, self);
  158. return image && image->imagemask ? JNI_TRUE : JNI_FALSE;
  159. }
  160. JNIEXPORT jboolean JNICALL
  161. FUN(Image_getInterpolate)(JNIEnv *env, jobject self)
  162. {
  163. fz_image *image = from_Image(env, self);
  164. return image && image->interpolate ? JNI_TRUE : JNI_FALSE;
  165. }
  166. JNIEXPORT jint JNICALL
  167. FUN(Image_getOrientation)(JNIEnv *env, jobject self)
  168. {
  169. fz_context *ctx = get_context(env);
  170. fz_image *image = from_Image(env, self);
  171. return fz_image_orientation(ctx, image);
  172. }
  173. JNIEXPORT jobject JNICALL
  174. FUN(Image_getMask)(JNIEnv *env, jobject self)
  175. {
  176. fz_context *ctx = get_context(env);
  177. fz_image *img = from_Image(env, self);
  178. if (!ctx || !img) return NULL;
  179. return to_Image_safe(ctx, env, img->mask);
  180. }
  181. JNIEXPORT jobject JNICALL
  182. FUN(Image_toPixmap)(JNIEnv *env, jobject self)
  183. {
  184. fz_context *ctx = get_context(env);
  185. fz_image *img = from_Image(env, self);
  186. fz_pixmap *pixmap = NULL;
  187. if (!ctx || !img) return NULL;
  188. fz_try(ctx)
  189. pixmap = fz_get_pixmap_from_image(ctx, img, NULL, NULL, NULL, NULL);
  190. fz_catch(ctx)
  191. jni_rethrow(env, ctx);
  192. return to_Pixmap_safe_own(ctx, env, pixmap);
  193. }
  194. JNIEXPORT jintArray JNICALL
  195. FUN(Image_getColorKey)(JNIEnv *env, jobject self)
  196. {
  197. fz_context *ctx = get_context(env);
  198. fz_image *img = from_Image(env, self);
  199. int colorkey[FZ_MAX_COLORS * 2];
  200. if (!ctx || !img) return NULL;
  201. if (!img->use_colorkey)
  202. return NULL;
  203. memcpy(colorkey, img->colorkey, 2 * img->n * sizeof(int));
  204. return to_intArray(ctx, env, colorkey, 2 * img->n);
  205. }
  206. JNIEXPORT void JNICALL
  207. FUN(Image_setOrientation)(JNIEnv *env, jobject self, jint orientation)
  208. {
  209. fz_image *img = from_Image(env, self);
  210. if (!img) return;
  211. if (orientation < 0 || orientation > 8) jni_throw_oob_void(env, "orientation out of range");
  212. img->orientation = orientation;
  213. }
  214. JNIEXPORT jfloatArray JNICALL
  215. FUN(Image_getDecode)(JNIEnv *env, jobject self)
  216. {
  217. fz_context *ctx = get_context(env);
  218. fz_image *img = from_Image(env, self);
  219. float decode[FZ_MAX_COLORS * 2];
  220. if (!ctx || !img) return NULL;
  221. if (!img->use_decode)
  222. return NULL;
  223. memcpy(decode, img->decode, 2 * img->n * sizeof(float));
  224. return to_floatArray(ctx, env, decode, 2 * img->n);
  225. }