rusto-rs 0.2.2

RustO! - Pure Rust OCR library based on RapidOCR with PaddleOCR engine
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// FFI bindings for C/C++/C#
use crate::{DetectTextResult, ImageSource, OcrRunOptions, OutputGranularity, RustO, InitializeConfig};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_float, c_int};
use std::slice;

const ROCR_INVALID_OPTIONS: c_int = -4;

unsafe fn parse_run_options(options_json: *const c_char) -> Result<OcrRunOptions, c_int> {
    if options_json.is_null() {
        return Err(-1);
    }
    let options = CStr::from_ptr(options_json)
        .to_str()
        .ok()
        .and_then(|json| serde_json::from_str::<OcrRunOptions>(json).ok())
        .ok_or(ROCR_INVALID_OPTIONS)?;
    if options
        .line_y_threshold
        .is_some_and(|value| !value.is_finite() || value < 0.0)
        || options
            .word_x_threshold
            .is_some_and(|value| !value.is_finite() || value < 0.0)
        || options
            .text_score
            .is_some_and(|value| !value.is_finite() || !(0.0..=1.0).contains(&value))
    {
        return Err(ROCR_INVALID_OPTIONS);
    }
    Ok(options)
}

/// Opaque handle to RustO instance
pub struct ROCRHandle {
    inner: RustO,
}

/// C-compatible text result structure
#[repr(C)]
pub struct CTextResult {
    pub text: *mut c_char,
    pub score: c_float,
    pub box_x1: c_float,
    pub box_y1: c_float,
    pub box_x2: c_float,
    pub box_y2: c_float,
    pub box_x3: c_float,
    pub box_y3: c_float,
    pub box_x4: c_float,
    pub box_y4: c_float,
    pub frame_width: c_float,
    pub frame_height: c_float,
    pub frame_top: c_float,
    pub frame_left: c_float,
}

/// Create a new RustO instance with JSON configuration
///
/// # Safety
/// config_json must be a valid null-terminated UTF-8 JSON string
#[no_mangle]
pub unsafe extern "C" fn rocr_initialize(config_json: *const c_char) -> *mut ROCRHandle {
    if config_json.is_null() {
        return std::ptr::null_mut();
    }

    let json_str = match CStr::from_ptr(config_json).to_str() {
        Ok(s) => s,
        Err(_) => return std::ptr::null_mut(),
    };

    let config = match InitializeConfig::from_json(json_str) {
        Ok(c) => c,
        Err(_) => return std::ptr::null_mut(),
    };

    match RustO::initialize(config) {
        Ok(ocr) => Box::into_raw(Box::new(ROCRHandle { inner: ocr })),
        Err(_) => std::ptr::null_mut(),
    }
}

/// Run OCR on an image file with runtime output options.
#[no_mangle]
pub unsafe extern "C" fn rocr_detect_text_file(
    handle: *mut ROCRHandle,
    image_path: *const c_char,
    options_json: *const c_char,
    results_out: *mut *mut CTextResult,
    count_out: *mut usize,
) -> c_int {
    if handle.is_null()
        || image_path.is_null()
        || options_json.is_null()
        || results_out.is_null()
        || count_out.is_null()
    {
        return -1;
    }
    let path = match CStr::from_ptr(image_path).to_str() {
        Ok(value) => value,
        Err(_) => return -2,
    };
    let options = match parse_run_options(options_json) {
        Ok(value) => value,
        Err(code) => return code,
    };
    if options.output == OutputGranularity::Spatial {
        return -5;
    }
    let detected = match (*handle)
        .inner
        .detect_text(&ImageSource::Path(path.into()), &options)
    {
        Ok(value) => value,
        Err(_) => return -3,
    };
    let DetectTextResult::Structured(projected) = detected else {
        return -5;
    };
    let c_results = text_results_to_c(&projected);
    *count_out = c_results.len();
    *results_out = c_results.as_ptr() as *mut CTextResult;
    std::mem::forget(c_results);
    0
}

/// Run OCR on an image file and return spatial text with runtime options.
#[no_mangle]
pub unsafe extern "C" fn rocr_detect_text_file_spatial(
    handle: *mut ROCRHandle,
    image_path: *const c_char,
    options_json: *const c_char,
) -> *mut c_char {
    if handle.is_null() || image_path.is_null() || options_json.is_null() {
        return std::ptr::null_mut();
    }
    let path = match CStr::from_ptr(image_path).to_str() {
        Ok(value) => value,
        Err(_) => return std::ptr::null_mut(),
    };
    let options = match parse_run_options(options_json) {
        Ok(value) => value,
        Err(_) => return std::ptr::null_mut(),
    };
    if options.output != OutputGranularity::Spatial {
        return std::ptr::null_mut();
    }
    let detected = match (*handle)
        .inner
        .detect_text(&ImageSource::Path(path.into()), &options)
    {
        Ok(value) => value,
        Err(_) => return std::ptr::null_mut(),
    };
    let DetectTextResult::Spatial(text) = detected else {
        return std::ptr::null_mut();
    };
    CString::new(text).map_or(std::ptr::null_mut(), CString::into_raw)
}

/// Run OCR on image data with runtime output options.
#[no_mangle]
pub unsafe extern "C" fn rocr_detect_text_data(
    handle: *mut ROCRHandle,
    image_data: *const u8,
    image_len: usize,
    options_json: *const c_char,
    results_out: *mut *mut CTextResult,
    count_out: *mut usize,
) -> c_int {
    if handle.is_null()
        || image_data.is_null()
        || options_json.is_null()
        || results_out.is_null()
        || count_out.is_null()
    {
        return -1;
    }
    let options = match parse_run_options(options_json) {
        Ok(value) => value,
        Err(code) => return code,
    };
    if options.output == OutputGranularity::Spatial {
        return -5;
    }
    let detected = match (*handle).inner.detect_text(
        &ImageSource::Bytes(slice::from_raw_parts(image_data, image_len).to_vec()),
        &options,
    ) {
        Ok(value) => value,
        Err(_) => return -3,
    };
    let DetectTextResult::Structured(results) = detected else {
        return -5;
    };
    let c_results = text_results_to_c(&results);
    *count_out = c_results.len();
    *results_out = c_results.as_ptr() as *mut CTextResult;
    std::mem::forget(c_results);
    0
}

/// Run OCR on image data and return spatial text with runtime options.
#[no_mangle]
pub unsafe extern "C" fn rocr_detect_text_data_spatial(
    handle: *mut ROCRHandle,
    image_data: *const u8,
    image_len: usize,
    options_json: *const c_char,
) -> *mut c_char {
    if handle.is_null() || image_data.is_null() || options_json.is_null() {
        return std::ptr::null_mut();
    }
    let options = match parse_run_options(options_json) {
        Ok(value) => value,
        Err(_) => return std::ptr::null_mut(),
    };
    if options.output != OutputGranularity::Spatial {
        return std::ptr::null_mut();
    }
    let detected = match (*handle).inner.detect_text(
        &ImageSource::Bytes(slice::from_raw_parts(image_data, image_len).to_vec()),
        &options,
    ) {
        Ok(value) => value,
        Err(_) => return std::ptr::null_mut(),
    };
    let DetectTextResult::Spatial(text) = detected else {
        return std::ptr::null_mut();
    };
    CString::new(text).map_or(std::ptr::null_mut(), CString::into_raw)
}

/// Free string returned from export functions
///
/// # Safety
/// - s must be a pointer returned from rocr_detect_text_*_spatial
#[no_mangle]
pub unsafe extern "C" fn rocr_free_string(s: *mut c_char) {
    if !s.is_null() {
        drop(CString::from_raw(s));
    }
}

/// Free results returned from structured text detection.
///
/// # Safety
/// - results must be a pointer returned from rocr_detect_text_file or rocr_detect_text_data
/// - count must match the count returned by text detection
#[no_mangle]
pub unsafe extern "C" fn rocr_free_results(results: *mut CTextResult, count: usize) {
    if results.is_null() {
        return;
    }

    let results_vec = Vec::from_raw_parts(results, count, count);
    for result in results_vec {
        if !result.text.is_null() {
            drop(CString::from_raw(result.text));
        }
    }
}

/// Free a RustO instance
///
/// # Safety
/// handle must be a valid pointer returned from rocr_initialize
#[no_mangle]
pub unsafe extern "C" fn rocr_free(handle: *mut ROCRHandle) {
    if !handle.is_null() {
        drop(Box::from_raw(handle));
    }
}

/// Get library version
#[no_mangle]
pub extern "C" fn rocr_version() -> *const c_char {
    static VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
    VERSION.as_ptr() as *const c_char
}

fn text_results_to_c(results: &[crate::TextResult]) -> Vec<CTextResult> {
    results
        .iter()
        .map(|result| CTextResult {
            text: CString::new(result.text.clone())
                .unwrap_or_default()
                .into_raw(),
            score: result.score,
            box_x1: result.box_points[0].0,
            box_y1: result.box_points[0].1,
            box_x2: result.box_points[1].0,
            box_y2: result.box_points[1].1,
            box_x3: result.box_points[2].0,
            box_y3: result.box_points[2].1,
            box_x4: result.box_points[3].0,
            box_y4: result.box_points[3].1,
            frame_width: result.frame.width,
            frame_height: result.frame.height,
            frame_top: result.frame.top,
            frame_left: result.frame.left,
        })
        .collect()
}

// ============================================================================
// Android JNI bindings (com.byrizki.rusto.RustO)
// ============================================================================

#[cfg(feature = "ffi")]
use jni::objects::{JByteArray, JClass, JFloatArray, JIntArray, JLongArray, JObjectArray, JString};
#[cfg(feature = "ffi")]
use jni::sys::{jfloat, jint, jlong, jstring};
#[cfg(feature = "ffi")]
use jni::JNIEnv;

#[cfg(feature = "ffi")]
unsafe fn write_jni_results(
    env: &mut JNIEnv,
    results_out: &JLongArray,
    count_out: &JIntArray,
    results: Vec<CTextResult>,
) -> jint {
    let count = results.len();
    let ptr = results.as_ptr() as jlong;
    if env.set_long_array_region(results_out, 0, &[ptr]).is_err()
        || env.set_int_array_region(count_out, 0, &[count as jint]).is_err()
    {
        // `results` still owns allocations here; normal Vec drop releases the
        // buffer but not individual CString allocations.
        for result in results {
            if !result.text.is_null() {
                drop(CString::from_raw(result.text));
            }
        }
        return -4;
    }
    std::mem::forget(results);
    0
}

#[cfg(feature = "ffi")]
unsafe fn parse_jni_options(env: &mut JNIEnv, options_json: JString) -> Result<OcrRunOptions, jint> {
    let json: String = env.get_string(&options_json).map_err(|_| -4)?.into();
    let c_json = CString::new(json).map_err(|_| ROCR_INVALID_OPTIONS)?;
    parse_run_options(c_json.as_ptr()).map_err(|code| code as jint)
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeInitialize(
    mut env: JNIEnv,
    _class: JClass,
    config_json: JString,
) -> jlong {
    let json: String = match env.get_string(&config_json) { Ok(value) => value.into(), Err(_) => return 0 };
    let config = match InitializeConfig::from_json(&json) { Ok(value) => value, Err(_) => return 0 };
    match RustO::initialize(config) {
        Ok(ocr) => Box::into_raw(Box::new(ROCRHandle { inner: ocr })) as jlong,
        Err(_) => 0,
    }
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeDetectTextFile(
    mut env: JNIEnv,
    _class: JClass,
    handle: jlong,
    image_path: JString,
    options_json: JString,
    results_out: JLongArray,
    count_out: JIntArray,
) -> jint {
    if handle == 0 { return -1; }
    let path: String = match env.get_string(&image_path) { Ok(value) => value.into(), Err(_) => return -2 };
    let options = match parse_jni_options(&mut env, options_json) { Ok(value) => value, Err(code) => return code };
    if options.output == OutputGranularity::Spatial { return -5; }
    let detected = match (&mut (*(handle as *mut ROCRHandle)).inner)
        .detect_text(&ImageSource::Path(path.into()), &options)
    { Ok(value) => value, Err(_) => return -3 };
    let DetectTextResult::Structured(results) = detected else { return -5; };
    write_jni_results(&mut env, &results_out, &count_out, text_results_to_c(&results))
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeDetectTextData(
    mut env: JNIEnv,
    _class: JClass,
    handle: jlong,
    image_data: JByteArray,
    options_json: JString,
    results_out: JLongArray,
    count_out: JIntArray,
) -> jint {
    if handle == 0 { return -1; }
    let bytes = match env.convert_byte_array(&image_data) { Ok(value) => value, Err(_) => return -2 };
    let options = match parse_jni_options(&mut env, options_json) { Ok(value) => value, Err(code) => return code };
    if options.output == OutputGranularity::Spatial { return -5; }
    let detected = match (&mut (*(handle as *mut ROCRHandle)).inner)
        .detect_text(&ImageSource::Bytes(bytes), &options)
    { Ok(value) => value, Err(_) => return -3 };
    let DetectTextResult::Structured(results) = detected else { return -5; };
    write_jni_results(&mut env, &results_out, &count_out, text_results_to_c(&results))
}

#[cfg(feature = "ffi")]
unsafe fn spatial_to_jstring(env: &mut JNIEnv, detected: DetectTextResult) -> jstring {
    let DetectTextResult::Spatial(text) = detected else { return std::ptr::null_mut(); };
    env.new_string(text).map_or(std::ptr::null_mut(), |value| value.into_raw())
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeDetectTextFileSpatial(
    mut env: JNIEnv,
    _class: JClass,
    handle: jlong,
    image_path: JString,
    options_json: JString,
) -> jstring {
    if handle == 0 { return std::ptr::null_mut(); }
    let path: String = match env.get_string(&image_path) { Ok(value) => value.into(), Err(_) => return std::ptr::null_mut() };
    let options = match parse_jni_options(&mut env, options_json) { Ok(value) => value, Err(_) => return std::ptr::null_mut() };
    if options.output != OutputGranularity::Spatial { return std::ptr::null_mut(); }
    let detected = match (&mut (*(handle as *mut ROCRHandle)).inner)
        .detect_text(&ImageSource::Path(path.into()), &options)
    { Ok(value) => value, Err(_) => return std::ptr::null_mut() };
    spatial_to_jstring(&mut env, detected)
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeDetectTextDataSpatial(
    mut env: JNIEnv,
    _class: JClass,
    handle: jlong,
    image_data: JByteArray,
    options_json: JString,
) -> jstring {
    if handle == 0 { return std::ptr::null_mut(); }
    let bytes = match env.convert_byte_array(&image_data) { Ok(value) => value, Err(_) => return std::ptr::null_mut() };
    let options = match parse_jni_options(&mut env, options_json) { Ok(value) => value, Err(_) => return std::ptr::null_mut() };
    if options.output != OutputGranularity::Spatial { return std::ptr::null_mut(); }
    let detected = match (&mut (*(handle as *mut ROCRHandle)).inner)
        .detect_text(&ImageSource::Bytes(bytes), &options)
    { Ok(value) => value, Err(_) => return std::ptr::null_mut() };
    spatial_to_jstring(&mut env, detected)
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeGetResult(
    env: JNIEnv,
    _class: JClass,
    results_ptr: jlong,
    index: jint,
    text_out: JObjectArray,
    score_out: JFloatArray,
    box_out: JFloatArray,
) {
    if results_ptr == 0 || index < 0 { return; }
    let item = &*((results_ptr as *const CTextResult).add(index as usize));
    if !item.text.is_null() {
        if let Ok(text) = env.new_string(CStr::from_ptr(item.text).to_string_lossy()) {
            let _ = env.set_object_array_element(&text_out, 0, text);
        }
    }
    let _ = env.set_float_array_region(&score_out, 0, &[item.score as jfloat]);
    let _ = env.set_float_array_region(&box_out, 0, &[
        item.box_x1 as jfloat, item.box_y1 as jfloat, item.box_x2 as jfloat, item.box_y2 as jfloat,
        item.box_x3 as jfloat, item.box_y3 as jfloat, item.box_x4 as jfloat, item.box_y4 as jfloat,
    ]);
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeFreeResults(
    _env: JNIEnv, _class: JClass, results_ptr: jlong, count: jint,
) {
    if results_ptr != 0 && count >= 0 { rocr_free_results(results_ptr as *mut CTextResult, count as usize); }
}

#[cfg(feature = "ffi")]
#[no_mangle]
pub unsafe extern "C" fn Java_com_byrizki_rusto_RustO_nativeFree(
    _env: JNIEnv, _class: JClass, handle: jlong,
) {
    if handle != 0 { rocr_free(handle as *mut ROCRHandle); }
}