writefile.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * WriteFile *
  24. *************/
  25. jint Java_com_googlecode_leptonica_android_WriteFile_nativeWriteBytes8(JNIEnv *env, jclass clazz,
  26. jlong nativePix,
  27. jbyteArray data) {
  28. l_int32 w, h, d;
  29. PIX *pix = (PIX *) nativePix;
  30. pixGetDimensions(pix, &w, &h, &d);
  31. l_uint8 **lineptrs = pixSetupByteProcessing(pix, NULL, NULL);
  32. jbyte *data_buffer = env->GetByteArrayElements(data, NULL);
  33. l_uint8 *byte_buffer = (l_uint8 *) data_buffer;
  34. for (int i = 0; i < h; i++) {
  35. memcpy((byte_buffer + (i * w)), lineptrs[i], w);
  36. }
  37. env->ReleaseByteArrayElements(data, data_buffer, 0);
  38. pixCleanupByteProcessing(pix, lineptrs);
  39. return (jint)(w * h);
  40. }
  41. jboolean Java_com_googlecode_leptonica_android_WriteFile_nativeWriteImpliedFormat(JNIEnv *env,
  42. jclass clazz,
  43. jlong nativePix,
  44. jstring fileName) {
  45. PIX *pixs = (PIX *) nativePix;
  46. const char *c_fileName = env->GetStringUTFChars(fileName, NULL);
  47. if (c_fileName == NULL) {
  48. LOGE("could not extract fileName string!");
  49. return JNI_FALSE;
  50. }
  51. jboolean result = JNI_TRUE;
  52. if (pixWriteImpliedFormat(c_fileName, pixs, 0, JNI_FALSE)) {
  53. LOGE("could not write pix data to %s", c_fileName);
  54. result = JNI_FALSE;
  55. }
  56. env->ReleaseStringUTFChars(fileName, c_fileName);
  57. return result;
  58. }
  59. jboolean Java_com_googlecode_leptonica_android_WriteFile_nativeWriteBitmap(JNIEnv *env,
  60. jclass clazz,
  61. jlong nativePix,
  62. jobject bitmap) {
  63. PIX *pixs = (PIX *) nativePix;
  64. l_int32 w, h, d;
  65. AndroidBitmapInfo info;
  66. void* pixels;
  67. int ret;
  68. if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
  69. LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
  70. return JNI_FALSE;
  71. }
  72. if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
  73. LOGE("Bitmap format is not RGBA_8888 !");
  74. return JNI_FALSE;
  75. }
  76. pixGetDimensions(pixs, &w, &h, &d);
  77. if (w != info.width || h != info.height) {
  78. LOGE("Bitmap width and height do not match Pix dimensions!");
  79. return JNI_FALSE;
  80. }
  81. if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
  82. LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
  83. return JNI_FALSE;
  84. }
  85. pixEndianByteSwap(pixs);
  86. l_uint8 *dst = (l_uint8 *) pixels;
  87. l_uint8 *src = (l_uint8 *) pixGetData(pixs);
  88. l_int32 dstBpl = info.stride;
  89. l_int32 srcBpl = 4 * pixGetWpl(pixs);
  90. LOGI("Writing 32bpp RGBA bitmap (w=%d, h=%d, stride=%d) from %dbpp Pix (wpl=%d)", info.width,
  91. info.height, info.stride, d, pixGetWpl(pixs));
  92. for (int dy = 0; dy < info.height; dy++) {
  93. l_uint8 *dstx = dst;
  94. l_uint8 *srcx = src;
  95. if (d == 32) {
  96. memcpy(dst, src, 4 * info.width);
  97. } else if (d == 8) {
  98. for (int dw = 0; dw < info.width; dw++) {
  99. dstx[0] = dstx[1] = dstx[2] = srcx[0];
  100. dstx[3] = 0xFF;
  101. dstx += 4;
  102. srcx += 1;
  103. }
  104. } else if (d == 1) {
  105. for (int dw = 0; dw < info.width; dw++) {
  106. dstx[0] = dstx[1] = dstx[2] = (1 << (7 - (dw & 7)) & srcx[0]) ? 0x00 : 0xFF;
  107. dstx[3] = 0xFF;
  108. dstx += 4;
  109. srcx += ((dw % 8) == 7) ? 1 : 0;
  110. }
  111. }
  112. dst += dstBpl;
  113. src += srcBpl;
  114. }
  115. AndroidBitmap_unlockPixels(env, bitmap);
  116. return JNI_TRUE;
  117. }
  118. #ifdef __cplusplus
  119. }
  120. #endif /* __cplusplus */