androidimage.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /* AndroidImage interface */
  23. JNIEXPORT jlong JNICALL
  24. FUN(AndroidImage_newImageFromBitmap)(JNIEnv *env, jobject self, jobject jbitmap, jlong jmask)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_image *mask = CAST(fz_image *, jmask);
  28. fz_image *image = NULL;
  29. fz_pixmap *pixmap = NULL;
  30. AndroidBitmapInfo info;
  31. void *pixels;
  32. int ret;
  33. if (!ctx) return 0;
  34. if (!jbitmap) jni_throw_arg(env, "bitmap must not be null");
  35. if (mask && mask->mask)
  36. jni_throw_run(env, "new Image failed as mask cannot be masked");
  37. if ((ret = AndroidBitmap_getInfo(env, jbitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS)
  38. jni_throw_run(env, "new Image failed to get bitmap info");
  39. if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
  40. jni_throw_run(env, "new Image failed as bitmap format is not RGBA_8888");
  41. if (info.stride != info.width)
  42. jni_throw_run(env, "new Image failed as bitmap width != stride");
  43. fz_var(pixmap);
  44. fz_try(ctx)
  45. {
  46. int ret;
  47. int phase = 0;
  48. size_t size = info.width * info.height * 4;
  49. pixmap = fz_new_pixmap(ctx, fz_device_rgb(ctx), info.width, info.height, NULL, 1);
  50. while (1)
  51. {
  52. ret = AndroidBitmap_lockPixels(env, jbitmap, (void **)&pixels);
  53. if (ret == ANDROID_BITMAP_RESULT_SUCCESS)
  54. break;
  55. if (!fz_store_scavenge_external(ctx, size, &phase))
  56. break; /* Failed to free any */
  57. }
  58. if (ret != ANDROID_BITMAP_RESULT_SUCCESS)
  59. fz_throw(ctx, FZ_ERROR_GENERIC, "bitmap lock failed in new Image");
  60. memcpy(pixmap->samples, pixels, info.width * info.height * 4);
  61. if (AndroidBitmap_unlockPixels(env, jbitmap) != ANDROID_BITMAP_RESULT_SUCCESS)
  62. fz_throw(ctx, FZ_ERROR_GENERIC, "Bitmap unlock failed in new Image");
  63. image = fz_new_image_from_pixmap(ctx, fz_keep_pixmap(ctx, pixmap), fz_keep_image(ctx, mask));
  64. }
  65. fz_always(ctx)
  66. fz_drop_pixmap(ctx, pixmap);
  67. fz_catch(ctx)
  68. jni_rethrow(env, ctx);
  69. return jlong_cast(image);
  70. }