forked from rmtheis/tess-two
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreadfile.cpp
187 lines (142 loc) · 5.56 KB
/
readfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2011, Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <allheaders.h>
#include <android/bitmap.h>
#include <android/log.h>
#include <common.h>
#include <environ.h>
#include <jni.h>
#include <pix.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/************
* ReadFile *
************/
jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadMem(JNIEnv *env, jclass clazz,
jbyteArray image, jint length) {
jbyte *image_buffer = env->GetByteArrayElements(image, NULL);
int buffer_length = env->GetArrayLength(image);
PIX *pix = pixReadMem((const l_uint8 *) image_buffer, buffer_length);
env->ReleaseByteArrayElements(image, image_buffer, JNI_ABORT);
return (jlong) pix;
}
jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadBytes8(JNIEnv *env, jclass clazz,
jbyteArray data, jint w,
jint h) {
PIX *pix = pixCreateNoInit((l_int32) w, (l_int32) h, 8);
l_uint8 **lineptrs = pixSetupByteProcessing(pix, NULL, NULL);
jbyte *data_buffer = env->GetByteArrayElements(data, NULL);
l_uint8 *byte_buffer = (l_uint8 *) data_buffer;
for (int i = 0; i < h; i++) {
memcpy(lineptrs[i], (byte_buffer + (i * w)), w);
}
env->ReleaseByteArrayElements(data, data_buffer, JNI_ABORT);
pixCleanupByteProcessing(pix, lineptrs);
l_int32 d;
pixGetDimensions(pix, &w, &h, &d);
LOGE("Created image width w=%d, h=%d, d=%d", w, h, d);
return (jlong) pix;
}
jboolean Java_com_googlecode_leptonica_android_ReadFile_nativeReplaceBytes8(JNIEnv *env,
jclass clazz,
jlong nativePix,
jbyteArray data,
jint srcw, jint srch) {
PIX *pix = (PIX *) nativePix;
l_int32 w, h, d;
pixGetDimensions(pix, &w, &h, &d);
if (d != 8 || (l_int32) srcw != w || (l_int32) srch != h) {
LOGE("Failed to replace bytes at w=%d, h=%d, d=%d with w=%d, h=%d", w, h, d, srcw, srch);
return JNI_FALSE;
}
l_uint8 **lineptrs = pixSetupByteProcessing(pix, NULL, NULL);
jbyte *data_buffer = env->GetByteArrayElements(data, NULL);
l_uint8 *byte_buffer = (l_uint8 *) data_buffer;
for (int i = 0; i < h; i++) {
memcpy(lineptrs[i], (byte_buffer + (i * w)), w);
}
env->ReleaseByteArrayElements(data, data_buffer, JNI_ABORT);
pixCleanupByteProcessing(pix, lineptrs);
return JNI_TRUE;
}
jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadFiles(JNIEnv *env, jclass clazz,
jstring dirName, jstring prefix) {
PIXA *pixad = NULL;
const char *c_dirName = env->GetStringUTFChars(dirName, NULL);
if (c_dirName == NULL) {
LOGE("could not extract dirName string!");
return JNI_FALSE;
}
const char *c_prefix = env->GetStringUTFChars(prefix, NULL);
if (c_prefix == NULL) {
LOGE("could not extract prefix string!");
return JNI_FALSE;
}
pixad = pixaReadFiles(c_dirName, c_prefix);
env->ReleaseStringUTFChars(dirName, c_dirName);
env->ReleaseStringUTFChars(prefix, c_prefix);
return (jlong) pixad;
}
jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadFile(JNIEnv *env, jclass clazz,
jstring fileName) {
PIX *pixd = NULL;
const char *c_fileName = env->GetStringUTFChars(fileName, NULL);
if (c_fileName == NULL) {
LOGE("could not extract fileName string!");
return JNI_FALSE;
}
pixd = pixRead(c_fileName);
env->ReleaseStringUTFChars(fileName, c_fileName);
return (jlong) pixd;
}
jlong Java_com_googlecode_leptonica_android_ReadFile_nativeReadBitmap(JNIEnv *env, jclass clazz,
jobject bitmap) {
LOGV(__FUNCTION__);
l_int32 w, h, d;
AndroidBitmapInfo info;
void* pixels;
int ret;
if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return JNI_FALSE;
}
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return JNI_FALSE;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return JNI_FALSE;
}
PIX *pixd = pixCreate(info.width, info.height, 32);
l_uint8 *src = (l_uint8 *) pixels;
l_uint8 *dst = (l_uint8 *) pixGetData(pixd);
l_int32 srcBpl = (info.stride);
l_int32 dstBpl = pixGetWpl(pixd)*4;
for (int dy = 0; dy < info.height; dy++) {
memcpy(dst, src, 4 * info.width);
dst += dstBpl;
src += srcBpl;
}
pixEndianByteSwap(pixd);
AndroidBitmap_unlockPixels(env, bitmap);
return (jlong) pixd;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */