You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mat_pixel_android.cpp 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "mat.h"
  15. #if NCNN_PIXEL
  16. #if __ANDROID_API__ >= 9
  17. #include <android/bitmap.h>
  18. #include <jni.h>
  19. #endif // __ANDROID_API__ >= 9
  20. namespace ncnn {
  21. #if __ANDROID_API__ >= 9
  22. Mat Mat::from_android_bitmap(JNIEnv* env, jobject bitmap, int type_to, Allocator* allocator)
  23. {
  24. AndroidBitmapInfo info;
  25. AndroidBitmap_getInfo(env, bitmap, &info);
  26. int type_from;
  27. int elempack;
  28. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  29. {
  30. type_from = PIXEL_GRAY;
  31. elempack = 1;
  32. }
  33. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  34. {
  35. type_from = PIXEL_RGBA;
  36. elempack = 4;
  37. }
  38. else
  39. {
  40. // unsuppored android bitmap format
  41. return Mat();
  42. }
  43. // let PIXEL_RGBA2XXX become PIXEL_XXX
  44. type_to = (type_to & PIXEL_CONVERT_MASK) ? (type_to >> PIXEL_CONVERT_SHIFT) : (type_to & PIXEL_FORMAT_MASK);
  45. void* data;
  46. AndroidBitmap_lockPixels(env, bitmap, &data);
  47. int type = type_to == type_from ? type_from : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  48. Mat m = Mat::from_pixels((const unsigned char*)data, type, info.width, info.height, info.stride, allocator);
  49. AndroidBitmap_unlockPixels(env, bitmap);
  50. return m;
  51. }
  52. Mat Mat::from_android_bitmap_resize(JNIEnv* env, jobject bitmap, int type_to, int target_width, int target_height, Allocator* allocator)
  53. {
  54. AndroidBitmapInfo info;
  55. AndroidBitmap_getInfo(env, bitmap, &info);
  56. int type_from;
  57. int elempack;
  58. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  59. {
  60. type_from = PIXEL_GRAY;
  61. elempack = 1;
  62. }
  63. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  64. {
  65. type_from = PIXEL_RGBA;
  66. elempack = 4;
  67. }
  68. else
  69. {
  70. // unsuppored android bitmap format
  71. return Mat();
  72. }
  73. // let PIXEL_RGBA2XXX become PIXEL_XXX
  74. type_to = (type_to & PIXEL_CONVERT_MASK) ? (type_to >> PIXEL_CONVERT_SHIFT) : (type_to & PIXEL_FORMAT_MASK);
  75. void* data;
  76. AndroidBitmap_lockPixels(env, bitmap, &data);
  77. int type = type_to == type_from ? type_from : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  78. Mat m = Mat::from_pixels_resize((const unsigned char*)data, type, info.width, info.height, info.stride, target_width, target_height, allocator);
  79. AndroidBitmap_unlockPixels(env, bitmap);
  80. return m;
  81. }
  82. void Mat::to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const
  83. {
  84. AndroidBitmapInfo info;
  85. AndroidBitmap_getInfo(env, bitmap, &info);
  86. int type_to;
  87. int elempack;
  88. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  89. {
  90. type_to = PIXEL_GRAY;
  91. elempack = 1;
  92. }
  93. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  94. {
  95. type_to = PIXEL_RGBA;
  96. elempack = 4;
  97. }
  98. else
  99. {
  100. // unsuppored android bitmap format
  101. return;
  102. }
  103. // let PIXEL_XXX2RGBA become PIXEL_XXX
  104. type_from = (type_from & PIXEL_CONVERT_MASK) ? (type_from & PIXEL_FORMAT_MASK) : type_from;
  105. void* data;
  106. AndroidBitmap_lockPixels(env, bitmap, &data);
  107. int type = type_from == type_to ? type_to : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  108. to_pixels_resize((unsigned char*)data, type, info.width, info.height, info.stride);
  109. AndroidBitmap_unlockPixels(env, bitmap);
  110. }
  111. #endif // __ANDROID_API__ >= 9
  112. } // namespace ncnn
  113. #endif // NCNN_PIXEL