Repository navigation
Expand file tree
/
Copy pathffi.rs
More file actions
720 lines (667 loc) · 23 KB
/
Copy pathffi.rs
File metadata and controls
720 lines (667 loc) · 23 KB
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! # FFI Interface
//!
//! C-compatible FFI interface for PaddleOCR-rs, enabling integration with
//! Android (JNI), iOS (Swift/ObjC), and other languages via C bindings.
//!
//! ## Safety
//!
//! All FFI functions are `unsafe` and require careful memory management.
//! Use the provided `free` functions to release allocated memory.
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;
use crate::{AccelerationDevice, OcrEngine, OrderBy, DocOrientationClassifier};
use crate::error::PaddleOcrError;
/// Result of a single text recognition operation.
///
/// Contains the recognized text, confidence score, and bounding box coordinates.
/// The caller must free `text` using `paddle_ocr_free_string`.
#[repr(C)]
pub struct OcrResult {
/// Recognized text (UTF-8 encoded, null-terminated).
/// Caller must free with `paddle_ocr_free_string`.
pub text: *mut c_char,
/// Recognition confidence score in `[0.0, 1.0]`.
pub confidence: f32,
/// X coordinate of the top-left corner (pixels).
pub x: f32,
/// Y coordinate of the top-left corner (pixels).
pub y: f32,
/// Width of the bounding rectangle (pixels).
pub width: f32,
/// Height of the bounding rectangle (pixels).
pub height: f32,
}
/// Error code returned by FFI operations.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcrErrorCode {
/// Success.
Ok = 0,
/// Invalid argument.
InvalidArgument = 1,
/// Model loading or session creation failed.
ModelError = 2,
/// Inference failed.
InferenceError = 3,
/// Memory allocation failed.
AllocationError = 4,
/// Invalid UTF-8 data.
Utf8Error = 5,
/// General error.
GeneralError = 6,
}
/// Error information returned by FFI operations.
///
/// The caller must free `message` using `paddle_ocr_free_string`.
#[repr(C)]
pub struct OcrError {
/// Error code.
pub code: OcrErrorCode,
/// Human-readable error message (UTF-8, null-terminated).
/// Caller must free with `paddle_ocr_free_string`.
pub message: *mut c_char,
}
impl From<PaddleOcrError> for OcrError {
fn from(err: PaddleOcrError) -> Self {
let (code, msg) = match &err {
PaddleOcrError::Image { message } => (OcrErrorCode::InvalidArgument, message.clone()),
PaddleOcrError::Model(e) => (OcrErrorCode::ModelError, e.to_string()),
PaddleOcrError::Inference { message } => (OcrErrorCode::InferenceError, message.clone()),
PaddleOcrError::Preprocessing { message } => (OcrErrorCode::GeneralError, message.clone()),
PaddleOcrError::Decoding { message } => (OcrErrorCode::GeneralError, message.clone()),
PaddleOcrError::Utf8(e) => (OcrErrorCode::Utf8Error, e.to_string()),
PaddleOcrError::DegenerateRegion { reason } => (OcrErrorCode::InvalidArgument, reason.clone()),
PaddleOcrError::Projection { reason } => (OcrErrorCode::GeneralError, reason.clone()),
PaddleOcrError::General(s) => (OcrErrorCode::GeneralError, s.clone()),
};
let c_msg = CString::new(msg).unwrap_or_else(|_| CString::new("unknown error").unwrap());
OcrError {
code,
message: c_msg.into_raw(),
}
}
}
/// Opaque handle to an OCR engine instance.
///
/// This handle is thread-safe and can be shared across threads.
/// The caller must destroy it with `paddle_ocr_destroy`.
pub struct OcrEngineHandle {
engine: OcrEngine,
}
/// Opaque handle to a document orientation classifier.
///
/// The caller must destroy it with `paddle_ocr_classifier_destroy`.
pub struct OcrClassifierHandle {
classifier: DocOrientationClassifier,
}
// ============================================================================
// OCR Engine Functions
// ============================================================================
/// Create a new OCR engine with default (CPU) acceleration.
///
/// # Arguments
///
/// * `det_model` -- Raw bytes of the detection ONNX model.
/// * `det_model_len` -- Length of `det_model` in bytes.
/// * `rec_model` -- Raw bytes of the recognition ONNX model.
/// * `rec_model_len` -- Length of `rec_model` in bytes.
/// * `keys_data` -- UTF-8 bytes of the character dictionary file.
/// * `keys_data_len` -- Length of `keys_data` in bytes.
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// Pointer to a new `OcrEngineHandle` on success, or NULL on failure.
/// The caller must destroy the handle with `paddle_ocr_destroy`.
///
/// # Safety
///
/// The input pointers must be valid for the specified lengths.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_create(
det_model: *const u8,
det_model_len: usize,
rec_model: *const u8,
rec_model_len: usize,
keys_data: *const u8,
keys_data_len: usize,
error: *mut OcrError,
) -> *mut OcrEngineHandle {
paddle_ocr_create_with_device(
det_model, det_model_len,
rec_model, rec_model_len,
keys_data, keys_data_len,
0, // AccelerationDevice::Cpu
error,
)
}
/// Create a new OCR engine with a specific acceleration device.
///
/// # Arguments
///
/// * `det_model` -- Raw bytes of the detection ONNX model.
/// * `det_model_len` -- Length of `det_model` in bytes.
/// * `rec_model` -- Raw bytes of the recognition ONNX model.
/// * `rec_model_len` -- Length of `rec_model` in bytes.
/// * `keys_data` -- UTF-8 bytes of the character dictionary file.
/// * `keys_data_len` -- Length of `keys_data` in bytes.
/// * `device` -- Acceleration device (0=CPU, 1=DirectML, 2=CUDA, 3=OpenVINO, 4=NNAPI, 5=CoreML, 6=CANN).
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// Pointer to a new `OcrEngineHandle` on success, or NULL on failure.
///
/// # Safety
///
/// The input pointers must be valid for the specified lengths.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_create_with_device(
det_model: *const u8,
det_model_len: usize,
rec_model: *const u8,
rec_model_len: usize,
keys_data: *const u8,
keys_data_len: usize,
device: u32,
error: *mut OcrError,
) -> *mut OcrEngineHandle {
// Validate inputs
if det_model.is_null() || rec_model.is_null() || keys_data.is_null() {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("null pointer argument").unwrap().into_raw(),
};
}
return ptr::null_mut();
}
// Convert device code to AccelerationDevice
let accel_device = match device {
0 => AccelerationDevice::Cpu,
1 => AccelerationDevice::DirectML,
2 => AccelerationDevice::Cuda,
3 => AccelerationDevice::OpenVINO,
4 => AccelerationDevice::Nnapi,
5 => AccelerationDevice::Coreml,
6 => AccelerationDevice::Cann,
_ => {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("invalid device code").unwrap().into_raw(),
};
}
return ptr::null_mut();
}
};
// Create slices from pointers
let det_slice = std::slice::from_raw_parts(det_model, det_model_len);
let rec_slice = std::slice::from_raw_parts(rec_model, rec_model_len);
let keys_slice = std::slice::from_raw_parts(keys_data, keys_data_len);
// Create engine
match OcrEngine::new_with_device(det_slice, rec_slice, keys_slice, accel_device) {
Ok(engine) => {
let handle = Box::new(OcrEngineHandle { engine });
Box::into_raw(handle)
}
Err(e) => {
if !error.is_null() {
(*error) = e.into();
}
ptr::null_mut()
}
}
}
/// Destroy an OCR engine handle and release all associated resources.
///
/// # Safety
///
/// `handle` must have been created by `paddle_ocr_create` or `paddle_ocr_create_with_device`.
/// Passing a null pointer or a dangling pointer is undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_destroy(handle: *mut OcrEngineHandle) {
if !handle.is_null() {
unsafe { drop(Box::from_raw(handle)); }
}
}
/// Recognize all text in an image.
///
/// # Arguments
///
/// * `handle` -- Valid OCR engine handle.
/// * `image_data` -- Raw image bytes (PNG, JPEG, etc.).
/// * `image_len` -- Length of `image_data` in bytes.
/// * `order` -- Ordering mode (0=Horizontal, 1=Vertical, 2=Score).
/// * `results` -- Pointer to receive the results array (caller must free with `paddle_ocr_free_results`).
/// * `results_len` -- Pointer to receive the number of results.
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// 0 on success, non-zero error code on failure.
///
/// # Safety
///
/// The input pointers must be valid for the specified lengths.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_recognize(
handle: *const OcrEngineHandle,
image_data: *const u8,
image_len: usize,
order: u32,
results: *mut *mut OcrResult,
results_len: *mut usize,
error: *mut OcrError,
) -> i32 {
// Validate inputs
if handle.is_null() || image_data.is_null() || results.is_null() || results_len.is_null() {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("null pointer argument").unwrap().into_raw(),
};
}
return OcrErrorCode::InvalidArgument as i32;
}
// Convert order
let order_by = match order {
0 => OrderBy::Horizontal,
1 => OrderBy::Vertical,
2 => OrderBy::Score,
_ => OrderBy::Horizontal,
};
// Create image slice
let image_slice = std::slice::from_raw_parts(image_data, image_len);
// Load image
let image = match image::load_from_memory(image_slice) {
Ok(img) => img,
Err(e) => {
if !error.is_null() {
(*error) = PaddleOcrError::Image { message: e.to_string() }.into();
}
return OcrErrorCode::InvalidArgument as i32;
}
};
// Run OCR
let engine = &(*handle).engine;
match engine.recognize_all(&image, order_by) {
Ok(blocks) => {
let len = blocks.len();
let mut results_vec = Vec::with_capacity(len);
for block in blocks {
let text = CString::new(block.text).unwrap_or_else(|_| CString::new("").unwrap());
results_vec.push(OcrResult {
text: text.into_raw(),
confidence: block.confidence,
x: block.x,
y: block.y,
width: block.width,
height: block.height,
});
}
let boxed = results_vec.into_boxed_slice();
let ptr = Box::into_raw(boxed) as *mut OcrResult;
*results = ptr;
*results_len = len;
OcrErrorCode::Ok as i32
}
Err(e) => {
if !error.is_null() {
(*error) = e.into();
}
OcrErrorCode::GeneralError as i32
}
}
}
/// Free an array of OCR results.
///
/// # Safety
///
/// `results` must have been allocated by `paddle_ocr_recognize`.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_free_results(results: *mut OcrResult, len: usize) {
if results.is_null() || len == 0 {
return;
}
let slice = std::slice::from_raw_parts_mut(results, len);
for result in slice.iter() {
if !result.text.is_null() {
unsafe { drop(CString::from_raw(result.text)); }
}
}
unsafe { drop(Box::from_raw(slice as *mut [OcrResult])); }
}
/// Free a string allocated by the OCR engine.
///
/// # Safety
///
/// `s` must have been allocated by the OCR engine (e.g., from `OcrResult.text`).
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_free_string(s: *mut c_char) {
if !s.is_null() {
unsafe { drop(CString::from_raw(s)); }
}
}
/// Free error information.
///
/// # Safety
///
/// `error` must have been allocated by an OCR function.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_free_error(error: OcrError) {
if !error.message.is_null() {
unsafe { drop(CString::from_raw(error.message)); }
}
}
// ============================================================================
// Document Orientation Classifier Functions
// ============================================================================
/// Create a new document orientation classifier with default (CPU) acceleration.
///
/// # Arguments
///
/// * `model_data` -- Raw bytes of the orientation classification ONNX model.
/// * `model_len` -- Length of `model_data` in bytes.
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// Pointer to a new `OcrClassifierHandle` on success, or NULL on failure.
///
/// # Safety
///
/// The input pointer must be valid for the specified length.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_classifier_create(
model_data: *const u8,
model_len: usize,
error: *mut OcrError,
) -> *mut OcrClassifierHandle {
paddle_ocr_classifier_create_with_device(model_data, model_len, 0, error)
}
/// Create a new document orientation classifier with a specific acceleration device.
///
/// # Arguments
///
/// * `model_data` -- Raw bytes of the orientation classification ONNX model.
/// * `model_len` -- Length of `model_data` in bytes.
/// * `device` -- Acceleration device code (see `paddle_ocr_create_with_device`).
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// Pointer to a new `OcrClassifierHandle` on success, or NULL on failure.
///
/// # Safety
///
/// The input pointer must be valid for the specified length.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_classifier_create_with_device(
model_data: *const u8,
model_len: usize,
device: u32,
error: *mut OcrError,
) -> *mut OcrClassifierHandle {
if model_data.is_null() {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("null pointer argument").unwrap().into_raw(),
};
}
return ptr::null_mut();
}
let accel_device = match device {
0 => AccelerationDevice::Cpu,
1 => AccelerationDevice::DirectML,
2 => AccelerationDevice::Cuda,
3 => AccelerationDevice::OpenVINO,
4 => AccelerationDevice::Nnapi,
5 => AccelerationDevice::Coreml,
6 => AccelerationDevice::Cann,
_ => {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("invalid device code").unwrap().into_raw(),
};
}
return ptr::null_mut();
}
};
let model_slice = std::slice::from_raw_parts(model_data, model_len);
match DocOrientationClassifier::new_with_device(model_slice, accel_device) {
Ok(classifier) => {
let handle = Box::new(OcrClassifierHandle { classifier });
Box::into_raw(handle)
}
Err(e) => {
if !error.is_null() {
(*error) = e.into();
}
ptr::null_mut()
}
}
}
/// Destroy a classifier handle and release all associated resources.
///
/// # Safety
///
/// `handle` must have been created by `paddle_ocr_classifier_create` or
/// `paddle_ocr_classifier_create_with_device`.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_classifier_destroy(handle: *mut OcrClassifierHandle) {
if !handle.is_null() {
unsafe { drop(Box::from_raw(handle)); }
}
}
/// Classify the orientation of a document image.
///
/// # Arguments
///
/// * `handle` -- Valid classifier handle.
/// * `image_data` -- Raw image bytes (PNG, JPEG, etc.).
/// * `image_len` -- Length of `image_data` in bytes.
/// * `orientation` -- Pointer to receive the orientation code (0=Upright, 1=Rotate90, 2=Rotate180, 3=Rotate270).
/// * `confidence` -- Pointer to receive the confidence score.
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// 0 on success, non-zero error code on failure.
///
/// # Safety
///
/// The input pointers must be valid for the specified lengths.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_classifier_classify(
handle: *const OcrClassifierHandle,
image_data: *const u8,
image_len: usize,
orientation: *mut u32,
confidence: *mut f32,
error: *mut OcrError,
) -> i32 {
if handle.is_null() || image_data.is_null() || orientation.is_null() || confidence.is_null() {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("null pointer argument").unwrap().into_raw(),
};
}
return OcrErrorCode::InvalidArgument as i32;
}
let image_slice = std::slice::from_raw_parts(image_data, image_len);
let image = match image::load_from_memory(image_slice) {
Ok(img) => img,
Err(e) => {
if !error.is_null() {
(*error) = PaddleOcrError::Image { message: e.to_string() }.into();
}
return OcrErrorCode::InvalidArgument as i32;
}
};
let classifier = &(*handle).classifier;
match classifier.classify(&image) {
Ok(result) => {
*orientation = match result.orientation {
crate::DocOrientation::Upright => 0,
crate::DocOrientation::Rotate90 => 1,
crate::DocOrientation::Rotate180 => 2,
crate::DocOrientation::Rotate270 => 3,
};
*confidence = result.confidence;
OcrErrorCode::Ok as i32
}
Err(e) => {
if !error.is_null() {
(*error) = e.into();
}
OcrErrorCode::GeneralError as i32
}
}
}
/// Classify and correct the orientation of a document image.
///
/// # Arguments
///
/// * `handle` -- Valid classifier handle.
/// * `image_data` -- Raw image bytes (PNG, JPEG, etc.).
/// * `image_len` -- Length of `image_data` in bytes.
/// * `corrected_data` -- Pointer to receive the corrected image bytes (PNG format).
/// * `corrected_len` -- Pointer to receive the length of `corrected_data`.
/// * `orientation` -- Pointer to receive the orientation code (optional, can be NULL).
/// * `confidence` -- Pointer to receive the confidence score (optional, can be NULL).
/// * `error` -- Pointer to receive error information (optional, can be NULL).
///
/// # Returns
///
/// 0 on success, non-zero error code on failure.
/// The caller must free `corrected_data` with `paddle_ocr_free_buffer`.
///
/// # Safety
///
/// The input pointers must be valid for the specified lengths.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_classifier_correct(
handle: *const OcrClassifierHandle,
image_data: *const u8,
image_len: usize,
corrected_data: *mut *mut u8,
corrected_len: *mut usize,
orientation: *mut u32,
confidence: *mut f32,
error: *mut OcrError,
) -> i32 {
if handle.is_null() || image_data.is_null() || corrected_data.is_null() || corrected_len.is_null() {
if !error.is_null() {
(*error) = OcrError {
code: OcrErrorCode::InvalidArgument,
message: CString::new("null pointer argument").unwrap().into_raw(),
};
}
return OcrErrorCode::InvalidArgument as i32;
}
let image_slice = std::slice::from_raw_parts(image_data, image_len);
let image = match image::load_from_memory(image_slice) {
Ok(img) => img,
Err(e) => {
if !error.is_null() {
(*error) = PaddleOcrError::Image { message: e.to_string() }.into();
}
return OcrErrorCode::InvalidArgument as i32;
}
};
let classifier = &(*handle).classifier;
match classifier.correct_orientation(&image) {
Ok((corrected, result)) => {
if !orientation.is_null() {
*orientation = match result.orientation {
crate::DocOrientation::Upright => 0,
crate::DocOrientation::Rotate90 => 1,
crate::DocOrientation::Rotate180 => 2,
crate::DocOrientation::Rotate270 => 3,
};
}
if !confidence.is_null() {
*confidence = result.confidence;
}
// Encode corrected image as PNG
let mut buf = std::io::Cursor::new(Vec::new());
match corrected.write_to(&mut buf, image::ImageFormat::Png) {
Ok(()) => {
let bytes = buf.into_inner();
let len = bytes.len();
let boxed = bytes.into_boxed_slice();
*corrected_data = Box::into_raw(boxed) as *mut u8;
*corrected_len = len;
OcrErrorCode::Ok as i32
}
Err(e) => {
if !error.is_null() {
(*error) = PaddleOcrError::Image { message: e.to_string() }.into();
}
OcrErrorCode::GeneralError as i32
}
}
}
Err(e) => {
if !error.is_null() {
(*error) = e.into();
}
OcrErrorCode::GeneralError as i32
}
}
}
/// Free a buffer allocated by the OCR engine.
///
/// # Safety
///
/// `data` must have been allocated by the OCR engine.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_free_buffer(data: *mut u8, len: usize) {
if data.is_null() || len == 0 {
return;
}
unsafe { drop(Vec::from_raw_parts(data, len, len)); }
}
// ============================================================================
// Utility Functions
// ============================================================================
/// Get the version string of the library.
///
/// # Returns
///
/// Pointer to a null-terminated version string.
/// The caller must free with `paddle_ocr_free_string`.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_version() -> *mut c_char {
CString::new(env!("CARGO_PKG_VERSION"))
.unwrap_or_else(|_| CString::new("unknown").unwrap())
.into_raw()
}
/// Check if a specific acceleration device is available.
///
/// # Arguments
///
/// * `device` -- Device code (see `paddle_ocr_create_with_device`).
///
/// # Returns
///
/// 1 if available, 0 if not.
#[no_mangle]
pub unsafe extern "C" fn paddle_ocr_is_device_available(device: u32) -> i32 {
let accel_device = match device {
0 => AccelerationDevice::Cpu,
1 => AccelerationDevice::DirectML,
2 => AccelerationDevice::Cuda,
3 => AccelerationDevice::OpenVINO,
4 => AccelerationDevice::Nnapi,
5 => AccelerationDevice::Coreml,
6 => AccelerationDevice::Cann,
_ => return 0,
};
accel_device.is_available() as i32
}