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 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <string.h>
  18. #include <jni.h>
  19. #include <android/bitmap.h>
  20. namespace ncnn {
  21. static Mat get_continous_pixels(const unsigned char* data, int w, int h, int elempack, int stride)
  22. {
  23. if (stride == w * elempack)
  24. return Mat(w, h, (void*)data, (size_t)elempack, elempack);
  25. Mat m(w, h, (size_t)elempack, elempack);
  26. unsigned char* ptr = m;
  27. for (int y=0; y<h; y++)
  28. {
  29. memcpy(ptr, data, w * elempack);
  30. ptr += w * elempack;
  31. data += stride;
  32. }
  33. return m;
  34. }
  35. static void set_continous_pixels(const Mat& m, unsigned char* data, int stride)
  36. {
  37. int w = m.w;
  38. int h = m.h;
  39. int elempack = m.elempack;
  40. if (stride == w * elempack)
  41. {
  42. if (data != m.data)
  43. memcpy(data, m, w * h * elempack);
  44. return;
  45. }
  46. const unsigned char* ptr = m;
  47. for (int y=0; y<h; y++)
  48. {
  49. memcpy(data, ptr, w * elempack);
  50. ptr += w * elempack;
  51. data += stride;
  52. }
  53. }
  54. Mat Mat::from_android_bitmap(JNIEnv* env, jobject bitmap, int type_to, Allocator* allocator)
  55. {
  56. AndroidBitmapInfo info;
  57. AndroidBitmap_getInfo(env, bitmap, &info);
  58. int type_from;
  59. int elempack;
  60. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  61. {
  62. type_from = PIXEL_GRAY;
  63. elempack = 1;
  64. }
  65. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  66. {
  67. type_from = PIXEL_RGBA;
  68. elempack = 4;
  69. }
  70. else
  71. {
  72. // unsuppored android bitmap format
  73. return Mat();
  74. }
  75. // let PIXEL_RGBA2XXX become PIXEL_XXX
  76. type_to = (type_to & PIXEL_CONVERT_MASK) ? (type_to >> PIXEL_CONVERT_SHIFT) : (type_to & PIXEL_FORMAT_MASK);
  77. void* data;
  78. AndroidBitmap_lockPixels(env, bitmap, &data);
  79. Mat continous_pixels = get_continous_pixels((const unsigned char*)data, info.width, info.height, elempack, info.stride);
  80. int type = type_to == type_from ? type_from : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  81. Mat m = Mat::from_pixels(continous_pixels, type, info.width, info.height, allocator);
  82. AndroidBitmap_unlockPixels(env, bitmap);
  83. return m;
  84. }
  85. Mat Mat::from_android_bitmap_resize(JNIEnv* env, jobject bitmap, int type_to, int target_width, int target_height, Allocator* allocator)
  86. {
  87. AndroidBitmapInfo info;
  88. AndroidBitmap_getInfo(env, bitmap, &info);
  89. int type_from;
  90. int elempack;
  91. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  92. {
  93. type_from = PIXEL_GRAY;
  94. elempack = 1;
  95. }
  96. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  97. {
  98. type_from = PIXEL_RGBA;
  99. elempack = 4;
  100. }
  101. else
  102. {
  103. // unsuppored android bitmap format
  104. return Mat();
  105. }
  106. // let PIXEL_RGBA2XXX become PIXEL_XXX
  107. type_to = (type_to & PIXEL_CONVERT_MASK) ? (type_to >> PIXEL_CONVERT_SHIFT) : (type_to & PIXEL_FORMAT_MASK);
  108. void* data;
  109. AndroidBitmap_lockPixels(env, bitmap, &data);
  110. Mat continous_pixels = get_continous_pixels((const unsigned char*)data, info.width, info.height, elempack, info.stride);
  111. int type = type_to == type_from ? type_from : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  112. Mat m = Mat::from_pixels_resize(continous_pixels, type, info.width, info.height, target_width, target_height, allocator);
  113. AndroidBitmap_unlockPixels(env, bitmap);
  114. return m;
  115. }
  116. void Mat::to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const
  117. {
  118. AndroidBitmapInfo info;
  119. AndroidBitmap_getInfo(env, bitmap, &info);
  120. int type_to;
  121. int elempack;
  122. if (info.format == ANDROID_BITMAP_FORMAT_A_8)
  123. {
  124. type_to = PIXEL_GRAY;
  125. elempack = 1;
  126. }
  127. else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
  128. {
  129. type_to = PIXEL_RGBA;
  130. elempack = 4;
  131. }
  132. else
  133. {
  134. // unsuppored android bitmap format
  135. return;
  136. }
  137. // let PIXEL_XXX2RGBA become PIXEL_XXX
  138. type_from = (type_from & PIXEL_CONVERT_MASK) ? (type_from & PIXEL_FORMAT_MASK) : type_from;
  139. void* data;
  140. AndroidBitmap_lockPixels(env, bitmap, &data);
  141. Mat continous_pixels = get_continous_pixels((const unsigned char*)data, info.width, info.height, elempack, info.stride);
  142. int type = type_from == type_to ? type_to : (type_from | (type_to << PIXEL_CONVERT_SHIFT));
  143. to_pixels_resize(continous_pixels, type, info.width, info.height);
  144. set_continous_pixels(continous_pixels, (unsigned char*)data, info.stride);
  145. AndroidBitmap_unlockPixels(env, bitmap);
  146. }
  147. } // namespace ncnn
  148. #endif // __ANDROID_API__ >= 9
  149. #endif // NCNN_PIXEL