utilities.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 2011, Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common.h"
  17. #include <string.h>
  18. #include <android/bitmap.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif /* __cplusplus */
  22. /***************
  23. * AdaptiveMap *
  24. ***************/
  25. jlong Java_com_googlecode_leptonica_android_AdaptiveMap_nativeBackgroundNormMorph(JNIEnv *env,
  26. jclass clazz,
  27. jlong nativePix,
  28. jint reduction,
  29. jint size,
  30. jint bgval) {
  31. // Normalizes the background of each element in pixa.
  32. PIX *pixs = (PIX *) nativePix;
  33. PIX *pixd = pixBackgroundNormMorph(pixs, NULL, (l_int32) reduction, (l_int32) size,
  34. (l_int32) bgval);
  35. return (jlong) pixd;
  36. }
  37. jlong Java_com_googlecode_leptonica_android_AdaptiveMap_nativePixContrastNorm(JNIEnv *env,
  38. jclass clazz,
  39. jlong nativePix,
  40. jint sizeX,
  41. jint sizeY,
  42. jint minDiff,
  43. jint smoothX,
  44. jint smoothY) {
  45. PIX *pixs = (PIX *) nativePix;
  46. PIX *pixd = pixContrastNorm(NULL, pixs, (l_int32) sizeX, (l_int32) sizeY,
  47. (l_int32) minDiff, (l_int32) smoothX, (l_int32) smoothY);
  48. return (jlong) pixd;
  49. }
  50. /************
  51. * Binarize *
  52. ************/
  53. jlong Java_com_googlecode_leptonica_android_Binarize_nativeOtsuAdaptiveThreshold(JNIEnv *env,
  54. jclass clazz,
  55. jlong nativePix,
  56. jint sizeX,
  57. jint sizeY,
  58. jint smoothX,
  59. jint smoothY,
  60. jfloat scoreFract) {
  61. PIX *pixs = (PIX *) nativePix;
  62. PIX *pixd;
  63. if (pixOtsuAdaptiveThreshold(pixs, (l_int32) sizeX, (l_int32) sizeY, (l_int32) smoothX,
  64. (l_int32) smoothY, (l_float32) scoreFract, NULL, &pixd)) {
  65. return (jlong) 0;
  66. }
  67. return (jlong) pixd;
  68. }
  69. jlong Java_com_googlecode_leptonica_android_Binarize_nativeSauvolaBinarizeTiled(JNIEnv *env,
  70. jclass clazz,
  71. jlong nativePix,
  72. jint whsize,
  73. jfloat factor,
  74. jint nx,
  75. jint ny) {
  76. PIX *pixs = (PIX *) nativePix;
  77. PIX *pixd;
  78. if (pixSauvolaBinarizeTiled(pixs, (l_int32) whsize, (l_float32) factor, (l_int32) nx,
  79. (l_int32) ny, NULL, &pixd)) {
  80. return (jlong) 0;
  81. }
  82. return (jlong) pixd;
  83. }
  84. /********
  85. * Clip *
  86. ********/
  87. jlong Java_com_googlecode_leptonica_android_Clip_nativeClipRectangle(JNIEnv *env, jclass clazz,
  88. jlong nativePix, jlong nativeBox) {
  89. PIX *pixs = (PIX *) nativePix;
  90. BOX *box = (BOX *) nativeBox;
  91. PIX *pixd;
  92. pixd = pixClipRectangle(pixs,box,NULL);
  93. return (jlong) pixd;
  94. }
  95. /***********
  96. * Convert *
  97. ***********/
  98. jlong Java_com_googlecode_leptonica_android_Convert_nativeConvertTo8(JNIEnv *env, jclass clazz,
  99. jlong nativePix) {
  100. PIX *pixs = (PIX *) nativePix;
  101. PIX *pixd = pixConvertTo8(pixs, FALSE);
  102. return (jlong) pixd;
  103. }
  104. /********
  105. * Edge *
  106. ********/
  107. jlong Java_com_googlecode_leptonica_android_Edge_nativePixSobelEdgeFilter(JNIEnv *env,
  108. jclass clazz,
  109. jlong nativePix,
  110. jint orientFlag) {
  111. PIX *pixs = (PIX *) nativePix;
  112. PIX *pixd = pixSobelEdgeFilter(pixs, (l_int32) orientFlag);
  113. return (jlong) pixd;
  114. }
  115. /***********
  116. * Enhance *
  117. ***********/
  118. jlong Java_com_googlecode_leptonica_android_Enhance_nativeUnsharpMasking(JNIEnv *env, jclass clazz,
  119. jlong nativePix,
  120. jint halfwidth,
  121. jfloat fract) {
  122. PIX *pixs = (PIX *) nativePix;
  123. PIX *pixd = pixUnsharpMasking(pixs, (l_int32) halfwidth, (l_float32) fract);
  124. return (jlong) pixd;
  125. }
  126. /*************
  127. * GrayQuant *
  128. *************/
  129. jlong Java_com_googlecode_leptonica_android_GrayQuant_nativePixThresholdToBinary(JNIEnv *env, jclass clazz,
  130. jlong nativePix, jint thresh) {
  131. PIX *pixs = (PIX *) nativePix;
  132. PIX *pixd = pixThresholdToBinary(pixs, (l_int32) thresh);
  133. return (jlong) pixd;
  134. }
  135. /**********
  136. * JpegIO *
  137. **********/
  138. jbyteArray Java_com_googlecode_leptonica_android_JpegIO_nativeCompressToJpeg(JNIEnv *env,
  139. jclass clazz,
  140. jlong nativePix,
  141. jint quality,
  142. jboolean progressive) {
  143. PIX *pix = (PIX *) nativePix;
  144. l_uint8 *data;
  145. size_t size;
  146. if (pixWriteMemJpeg(&data, &size, pix, (l_int32) quality, progressive == JNI_TRUE ? 1 : 0)) {
  147. LOGE("Failed to write JPEG data");
  148. return NULL;
  149. }
  150. // TODO Can we just use the byte array directly?
  151. jbyteArray array = env->NewByteArray(size);
  152. env->SetByteArrayRegion(array, 0, size, (jbyte *) data);
  153. free(data);
  154. return array;
  155. }
  156. /************
  157. * MorphApp *
  158. ************/
  159. jlong Java_com_googlecode_leptonica_android_MorphApp_nativePixTophat(JNIEnv *env, jclass clazz,
  160. jlong nativePix, jint hsize,
  161. jint vsize, jint type) {
  162. PIX *pixs = (PIX *) nativePix;
  163. PIX *pixd = pixTophat(pixs, (l_int32) hsize, (l_int32) vsize, (l_int32) type);
  164. return (jlong) pixd;
  165. }
  166. jlong Java_com_googlecode_leptonica_android_MorphApp_nativePixFastTophat(JNIEnv *env, jclass clazz,
  167. jlong nativePix, jint xsize,
  168. jint ysize, jint type) {
  169. PIX *pixs = (PIX *) nativePix;
  170. PIX *pixd = pixFastTophat(pixs, (l_int32) xsize, (l_int32) ysize, (l_int32) type);
  171. return (jlong) pixd;
  172. }
  173. /*********
  174. * Scale *
  175. *********/
  176. jlong Java_com_googlecode_leptonica_android_Scale_nativeScaleGeneral(JNIEnv *env, jclass clazz,
  177. jlong nativePix, jfloat scaleX,
  178. jfloat scaleY, jfloat sharpfract, jint sharpwidth) {
  179. PIX *pixs = (PIX *) nativePix;
  180. PIX *pixd = pixScaleGeneral(pixs, (l_float32) scaleX, (l_float32) scaleY,(l_float32) sharpfract, (l_int32) sharpwidth);
  181. return (jlong) pixd;
  182. }
  183. jlong Java_com_googlecode_leptonica_android_Scale_nativeScale(JNIEnv *env, jclass clazz,
  184. jlong nativePix, jfloat scaleX,
  185. jfloat scaleY) {
  186. PIX *pixs = (PIX *) nativePix;
  187. PIX *pixd = pixScale(pixs, (l_float32) scaleX, (l_float32) scaleY);
  188. return (jlong) pixd;
  189. }
  190. /********
  191. * Skew *
  192. ********/
  193. jfloat Java_com_googlecode_leptonica_android_Skew_nativeFindSkew(JNIEnv *env, jclass clazz,
  194. jlong nativePix, jfloat sweepRange,
  195. jfloat sweepDelta,
  196. jint sweepReduction,
  197. jint searchReduction,
  198. jfloat searchMinDelta) {
  199. PIX *pixs = (PIX *) nativePix;
  200. l_float32 angle, conf;
  201. if (!pixFindSkewSweepAndSearch(pixs, &angle, &conf, (l_int32) sweepReduction,
  202. (l_int32) searchReduction, (l_float32) sweepRange,
  203. (l_int32) sweepDelta, (l_float32) searchMinDelta)) {
  204. if (conf <= 0) {
  205. return (jfloat) 0;
  206. }
  207. return (jfloat) angle;
  208. }
  209. return (jfloat) 0;
  210. }
  211. /**********
  212. * Rotate *
  213. **********/
  214. jlong Java_com_googlecode_leptonica_android_Rotate_nativeRotate(JNIEnv *env, jclass clazz,
  215. jlong nativePix, jfloat degrees,
  216. jboolean quality, jboolean resize) {
  217. PIX *pixd;
  218. PIX *pixs = (PIX *) nativePix;
  219. l_float32 deg2rad = 3.1415926535 / 180.0;
  220. l_float32 radians = degrees * deg2rad;
  221. l_int32 w, h, bpp, type;
  222. pixGetDimensions(pixs, &w, &h, &bpp);
  223. if (bpp == 1 && quality == JNI_TRUE) {
  224. pixd = pixRotateBinaryNice(pixs, radians, L_BRING_IN_WHITE);
  225. } else {
  226. type = quality == JNI_TRUE ? L_ROTATE_AREA_MAP : L_ROTATE_SAMPLING;
  227. w = (resize == JNI_TRUE) ? w : 0;
  228. h = (resize == JNI_TRUE) ? h : 0;
  229. pixd = pixRotate(pixs, radians, type, L_BRING_IN_WHITE, w, h);
  230. }
  231. return (jlong) pixd;
  232. }
  233. jlong Java_com_googlecode_leptonica_android_Rotate_nativeRotateOrth(JNIEnv *env, jclass clazz,
  234. jlong nativePix, jint quads) {
  235. PIX *pixs = (PIX *) nativePix;
  236. PIX *pixd;
  237. pixd = pixRotateOrth(pixs,(int)quads);
  238. return (jlong) pixd;
  239. }
  240. #ifdef __cplusplus
  241. }
  242. #endif /* __cplusplus */