pixa.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef __cplusplus
  18. extern "C" {
  19. #endif /* __cplusplus */
  20. jlong Java_com_googlecode_leptonica_android_Pixa_nativeCreate(JNIEnv *env, jclass clazz, jint size) {
  21. PIXA *pixa = pixaCreate((l_int32) size);
  22. return (jlong) pixa;
  23. }
  24. jlong Java_com_googlecode_leptonica_android_Pixa_nativeCopy(JNIEnv *env, jclass clazz,
  25. jlong nativePixa) {
  26. PIXA *pixas = (PIXA *) nativePixa;
  27. PIXA *pixad = pixaCopy(pixas, L_CLONE);
  28. return (jlong) pixad;
  29. }
  30. jlong Java_com_googlecode_leptonica_android_Pixa_nativeSort(JNIEnv *env, jclass clazz,
  31. jlong nativePixa, jint field, jint order) {
  32. PIXA *pixas = (PIXA *) nativePixa;
  33. PIXA *pixad = pixaSort(pixas, field, order, NULL, L_CLONE);
  34. return (jlong) pixad;
  35. }
  36. void Java_com_googlecode_leptonica_android_Pixa_nativeDestroy(JNIEnv *env, jclass clazz,
  37. jlong nativePixa) {
  38. PIXA *pixa = (PIXA *) nativePixa;
  39. pixaDestroy(&pixa);
  40. }
  41. jboolean Java_com_googlecode_leptonica_android_Pixa_nativeJoin(JNIEnv *env, jclass clazz,
  42. jlong nativePixa, jlong otherPixa) {
  43. PIXA *pixa = (PIXA *) nativePixa;
  44. PIXA *pixas = (PIXA *) otherPixa;
  45. if (pixaJoin(pixa, pixas, 0, 0)) {
  46. return JNI_FALSE;
  47. }
  48. return JNI_TRUE;
  49. }
  50. jint Java_com_googlecode_leptonica_android_Pixa_nativeGetCount(JNIEnv *env, jclass clazz,
  51. jlong nativePixa) {
  52. PIXA *pixa = (PIXA *) nativePixa;
  53. return (jint) pixaGetCount(pixa);
  54. }
  55. void Java_com_googlecode_leptonica_android_Pixa_nativeAddPix(JNIEnv *env, jclass clazz,
  56. jlong nativePixa, jlong nativePix,
  57. jint mode) {
  58. PIXA *pixa = (PIXA *) nativePixa;
  59. PIX *pix = (PIX *) nativePix;
  60. pixaAddPix(pixa, pix, mode);
  61. }
  62. void Java_com_googlecode_leptonica_android_Pixa_nativeAddBox(JNIEnv *env, jclass clazz,
  63. jlong nativePixa, jlong nativeBox,
  64. jint mode) {
  65. PIXA *pixa = (PIXA *) nativePixa;
  66. BOX *box = (BOX *) nativeBox;
  67. pixaAddBox(pixa, box, mode);
  68. }
  69. void Java_com_googlecode_leptonica_android_Pixa_nativeAdd(JNIEnv *env, jclass clazz,
  70. jlong nativePixa, jlong nativePix,
  71. jlong nativeBox, jint mode) {
  72. PIXA *pixa = (PIXA *) nativePixa;
  73. PIX *pix = (PIX *) nativePix;
  74. BOX *box = (BOX *) nativeBox;
  75. pixaAddPix(pixa, pix, mode);
  76. pixaAddBox(pixa, box, mode);
  77. }
  78. void Java_com_googlecode_leptonica_android_Pixa_nativeReplacePix(JNIEnv *env, jclass clazz,
  79. jlong nativePixa, jint index,
  80. jlong nativePix, jlong nativeBox) {
  81. PIXA *pixa = (PIXA *) nativePixa;
  82. PIX *pix = (PIX *) nativePix;
  83. BOX *box = (BOX *) nativeBox;
  84. pixaReplacePix(pixa, index, pix, box);
  85. }
  86. void Java_com_googlecode_leptonica_android_Pixa_nativeMergeAndReplacePix(JNIEnv *env, jclass clazz,
  87. jlong nativePixa,
  88. jint indexA, jint indexB) {
  89. PIXA *pixa = (PIXA *) nativePixa;
  90. l_int32 op;
  91. l_int32 depth;
  92. l_int32 x, y, w, h;
  93. l_int32 dx, dy, dw, dh;
  94. PIX *pixA, *pixB, *pixd;
  95. BOX *boxA, *boxB, *boxd;
  96. pixA = pixaGetPix(pixa, indexA, L_CLONE);
  97. pixB = pixaGetPix(pixa, indexB, L_CLONE);
  98. boxA = pixaGetBox(pixa, indexA, L_CLONE);
  99. boxB = pixaGetBox(pixa, indexB, L_CLONE);
  100. boxd = boxBoundingRegion(boxA, boxB);
  101. depth = pixGetDepth(pixA);
  102. boxGetGeometry(boxd, &x, &y, &w, &h);
  103. pixd = pixCreate(w, h, depth);
  104. op = PIX_SRC | PIX_DST;
  105. boxGetGeometry(boxA, &dx, &dy, &dw, &dh);
  106. pixRasterop(pixd, dx - x, dy - y, dw, dh, op, pixA, 0, 0);
  107. pixDestroy(&pixA);
  108. boxDestroy(&boxA);
  109. boxGetGeometry(boxB, &dx, &dy, &dw, &dh);
  110. pixRasterop(pixd, dx - x, dy - y, dw, dh, op, pixB, 0, 0);
  111. pixDestroy(&pixB);
  112. boxDestroy(&boxB);
  113. pixaReplacePix(pixa, indexA, pixd, boxd);
  114. pixaRemovePix(pixa, indexB);
  115. }
  116. jboolean Java_com_googlecode_leptonica_android_Pixa_nativeWriteToFileRandomCmap(JNIEnv *env,
  117. jclass clazz,
  118. jlong nativePixa,
  119. jstring fileName,
  120. jint width,
  121. jint height) {
  122. PIX *pixtemp;
  123. PIXA *pixa = (PIXA *) nativePixa;
  124. const char *c_fileName = env->GetStringUTFChars(fileName, NULL);
  125. if (c_fileName == NULL) {
  126. LOGE("could not extract fileName string!");
  127. return JNI_FALSE;
  128. }
  129. if (pixaGetCount(pixa) > 0) {
  130. pixtemp = pixaDisplayRandomCmap(pixa, (l_int32) width, (l_int32) height);
  131. } else {
  132. pixtemp = pixCreate((l_int32) width, (l_int32) height, 1);
  133. }
  134. pixWrite(c_fileName, pixtemp, IFF_BMP);
  135. pixDestroy(&pixtemp);
  136. env->ReleaseStringUTFChars(fileName, c_fileName);
  137. return JNI_TRUE;
  138. }
  139. jlong Java_com_googlecode_leptonica_android_Pixa_nativeGetPix(JNIEnv *env, jclass clazz,
  140. jlong nativePixa, jint index) {
  141. PIXA *pixa = (PIXA *) nativePixa;
  142. PIX *pix = pixaGetPix(pixa, (l_int32) index, L_CLONE);
  143. return (jlong) pix;
  144. }
  145. jlong Java_com_googlecode_leptonica_android_Pixa_nativeGetBox(JNIEnv *env, jclass clazz,
  146. jlong nativePixa, jint index) {
  147. PIXA *pixa = (PIXA *) nativePixa;
  148. BOX *box = pixaGetBox(pixa, (l_int32) index, L_CLONE);
  149. return (jlong) box;
  150. }
  151. jboolean Java_com_googlecode_leptonica_android_Pixa_nativeGetBoxGeometry(JNIEnv *env, jclass clazz,
  152. jlong nativePixa,
  153. jint index,
  154. jintArray dimensions) {
  155. PIXA *pixa = (PIXA *) nativePixa;
  156. jint *dimensionArray = env->GetIntArrayElements(dimensions, NULL);
  157. l_int32 x, y, w, h;
  158. if (pixaGetBoxGeometry(pixa, (l_int32) index, &x, &y, &w, &h)) {
  159. return JNI_FALSE;
  160. }
  161. dimensionArray[0] = x;
  162. dimensionArray[1] = y;
  163. dimensionArray[2] = w;
  164. dimensionArray[3] = h;
  165. env->ReleaseIntArrayElements(dimensions, dimensionArray, 0);
  166. return JNI_TRUE;
  167. }
  168. #ifdef __cplusplus
  169. }
  170. #endif /* __cplusplus */