readfile.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * ReadFile *
  24. ************/
  25. jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadMem(JNIEnv *env, jclass clazz,
  26. jbyteArray image, jint length) {
  27. jbyte *image_buffer = env->GetByteArrayElements(image, NULL);
  28. int buffer_length = env->GetArrayLength(image);
  29. PIX *pix = pixReadMem((const l_uint8 *) image_buffer, buffer_length);
  30. env->ReleaseByteArrayElements(image, image_buffer, JNI_ABORT);
  31. return (jlong) pix;
  32. }
  33. jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadBytes8(JNIEnv *env, jclass clazz,
  34. jbyteArray data, jint w,
  35. jint h) {
  36. PIX *pix = pixCreateNoInit((l_int32) w, (l_int32) h, 8);
  37. l_uint8 **lineptrs = pixSetupByteProcessing(pix, NULL, NULL);
  38. jbyte *data_buffer = env->GetByteArrayElements(data, NULL);
  39. l_uint8 *byte_buffer = (l_uint8 *) data_buffer;
  40. for (int i = 0; i < h; i++) {
  41. memcpy(lineptrs[i], (byte_buffer + (i * w)), w);
  42. }
  43. env->ReleaseByteArrayElements(data, data_buffer, JNI_ABORT);
  44. pixCleanupByteProcessing(pix, lineptrs);
  45. l_int32 d;
  46. pixGetDimensions(pix, &w, &h, &d);
  47. LOGI("Created image with w=%d, h=%d, d=%d", w, h, d);
  48. return (jlong) pix;
  49. }
  50. jboolean Java_com_googlecode_leptonica_android_ReadFile_nativeReplaceBytes8(JNIEnv *env,
  51. jclass clazz,
  52. jlong nativePix,
  53. jbyteArray data,
  54. jint srcw, jint srch) {
  55. PIX *pix = (PIX *) nativePix;
  56. l_int32 w, h, d;
  57. pixGetDimensions(pix, &w, &h, &d);
  58. if (d != 8 || (l_int32) srcw != w || (l_int32) srch != h) {
  59. LOGE("Failed to replace bytes at w=%d, h=%d, d=%d with w=%d, h=%d", w, h, d, srcw, srch);
  60. return JNI_FALSE;
  61. }
  62. l_uint8 **lineptrs = pixSetupByteProcessing(pix, NULL, NULL);
  63. jbyte *data_buffer = env->GetByteArrayElements(data, NULL);
  64. l_uint8 *byte_buffer = (l_uint8 *) data_buffer;
  65. for (int i = 0; i < h; i++) {
  66. memcpy(lineptrs[i], (byte_buffer + (i * w)), w);
  67. }
  68. env->ReleaseByteArrayElements(data, data_buffer, JNI_ABORT);
  69. pixCleanupByteProcessing(pix, lineptrs);
  70. return JNI_TRUE;
  71. }
  72. jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadFile(JNIEnv *env, jclass clazz,
  73. jstring fileName) {
  74. PIX *pixd = NULL;
  75. const char *c_fileName = env->GetStringUTFChars(fileName, NULL);
  76. if (c_fileName == NULL) {
  77. LOGE("could not extract fileName string!");
  78. return (jlong) NULL;
  79. }
  80. pixd = pixRead(c_fileName);
  81. env->ReleaseStringUTFChars(fileName, c_fileName);
  82. return (jlong) pixd;
  83. }
  84. jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadBitmap(JNIEnv *env, jclass clazz,
  85. jobject bitmap) {
  86. l_int32 w, h, d;
  87. AndroidBitmapInfo info;
  88. void* pixels;
  89. int ret;
  90. if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
  91. LOGE("AndroidBitmap_getInfo() failed! error=%d", ret);
  92. return (jlong) NULL;
  93. }
  94. if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
  95. LOGE("Bitmap format is not RGBA_8888!");
  96. return (jlong) NULL;
  97. }
  98. if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
  99. LOGE("AndroidBitmap_lockPixels() failed! error=%d", ret);
  100. return (jlong) NULL;
  101. }
  102. PIX *pixd = pixCreate(info.width, info.height, 8);
  103. l_uint32 *src = (l_uint32 *) pixels;
  104. l_int32 srcWpl = (info.stride / 4);
  105. l_uint32 *dst = pixGetData(pixd);
  106. l_int32 dstWpl = pixGetWpl(pixd);
  107. l_uint8 a, r, g, b, pixel8;
  108. for (int y = 0; y < info.height; y++) {
  109. l_uint32 *dst_line = dst + (y * dstWpl);
  110. l_uint32 *src_line = src + (y * srcWpl);
  111. for (int x = 0; x < info.width; x++) {
  112. // Get pixel from RGBA_8888
  113. r = *src_line >> SK_R32_SHIFT;
  114. g = *src_line >> SK_G32_SHIFT;
  115. b = *src_line >> SK_B32_SHIFT;
  116. a = *src_line >> SK_A32_SHIFT;
  117. pixel8 = (l_uint8)((r + g + b) / 3);
  118. // Set pixel to LUMA_8
  119. SET_DATA_BYTE(dst_line, x, pixel8);
  120. // Move to the next pixel
  121. src_line++;
  122. }
  123. }
  124. AndroidBitmap_unlockPixels(env, bitmap);
  125. return (jlong) pixd;
  126. }
  127. #ifdef __cplusplus
  128. }
  129. #endif /* __cplusplus */