The Optris SDK from Rust
Two crates over the SDK's flat C ABI:
In Fahrenheit: with the SDK switched to Fahrenheit (optris_sdk::set_temperature_unit(), see Temperatures in Fahrenheit), every temperature on this page is read and written in Fahrenheit, and every difference in Fahrenheit degrees; the samples are unchanged.
| Crate | What it is |
|---|---|
optris-sdk |
The safe, idiomatic crate. Result errors, RAII handles, closures for callbacks, and pixel lifetimes the compiler checks. Use this one. |
optris-sdk-sys |
Raw FFI declarations, generated by bindgen and checked in. Everything is unsafe and no rule of the C API is enforced. Reach for it only if you need something the safe crate has not wrapped. |
optris-sdk wraps all 689 of the SDK's C entry points. That is not an estimate: it is what scripts/check_binding_parity.py counts, and the count is a gate rather than a note in a document. Rust is the one binding with no gap at all.
Adding it to a project
1. Put both crates into [dependencies] in your Cargo.toml. Both are published on crates.io:
[dependencies] optris-sdk = "12.0" optris-sdk-sys = "12.0"
"12.0" takes the newest 12.0 release. cargo add optris-sdk optris-sdk-sys adds the same two crates, with the newest version written out in full.
Why both, when your code uses only optris-sdk: listing optris-sdk-sys beside it keeps the two versions matched and leaves the raw FFI layer available if you need it, and a direct dependency on it is what hands your build script DEP_OTCSDK_LIB_DIR (see the build script).
2. Install the Optris SDK itself. The crates carry the bindings, not the SDK library. Download the platform package from download.optris.com and install it: the .deb for your distribution on Debian and Ubuntu (or sudo apt install otcsdk from apt.optris.com), the installer on Windows. Installation has both. The crates need SDK 12.0.2 or later.
3. Tell the build where the library is, where the platform needs it. optris-sdk-sys links against the library that package installed. It looks in the directory named by OPTRIS_SDK_LIB_DIR when that is set, and otherwise in the linker's default search path:
| SDK installed from | What to set |
|---|---|
the .deb or apt, on Linux |
Nothing. The library is /usr/lib/libotcsdk.so, where the linker and the loader look anyway. |
| the installer, on Windows | OPTRIS_SDK_LIB_DIR to the installation's lib folder, before cargo build; see On Windows. |
| anywhere else on Linux, with the library outside the system directories | OPTRIS_SDK_LIB_DIR to the folder holding libotcsdk.so, and the build script so your program loads the same copy. |
export OPTRIS_SDK_LIB_DIR=/path/to/sdk/lib cargo build
OPTRIS_SDK_LIB_DIR tells the linker where the library is. Your application also needs the loader to find the same copy at run time, and that is a separate thing.
On Windows
OPTRIS_SDK_LIB_DIR must name the directory holding otcsdk.lib, the import library MSVC links against. In an installed SDK that is <install>\lib, which is not where the DLL is: <install>\bin holds otcsdk-34.dll. The default <install> is C:\Program Files\Optris\otcsdk.
set OPTRIS_SDK_LIB_DIR=<install>\lib cargo build
Do not build this path from OTC_SDK_DIR. The installer adds <install> to that system variable, but the variable is a list: a second SDK installation, or other Optris software such as Optris Relay, adds an entry of its own, and OTC_SDK_DIR%\lib then names no folder at all. Name the installation, or let PowerShell pick the entry that holds the import library:
$env:OPTRIS_SDK_LIB_DIR = $env:OTC_SDK_DIR -split ';' | Where-Object { $_ -and (Test-Path (Join-Path $_ 'lib\otcsdk.lib')) } | Select-Object -First 1 | ForEach-Object { Join-Path $_ 'lib' } cargo build
Without it the link fails with LINK : fatal error LNK1181: cannot open input file 'otcsdk.lib, which names the file and not the variable.optris-sdk-sysemits a build-script warning saying exactly this, but cargo does not print warnings from a dependency, socargo build -vv` is what shows it.
At run time the loader needs <install>\bin on PATH, or otcsdk-34.dll beside your executable. Windows has no rpath, so the build script in the next section, LD_LIBRARY_PATH and --disable-new-dtags are Linux and macOS matters. The SDK installer adds <install>\bin to the system PATH, so a command prompt opened after the installation finds the library; one that was already open keeps its old PATH until you open a new one. If you copy otcsdk-34.dll beside your executable instead, copy the four Microsoft Visual C++ runtime files from <install>\bin with it.
Checking you got the right library
optris_sdk::check_abi_compatible()?;
Compares the ABI version the loaded library implements against the one this crate was built for, and fails with a readable message rather than a segfault.
Your application needs three lines of build script
Only if you set OPTRIS_SDK_LIB_DIR on Linux. With the SDK installed from the .deb or apt there is nothing to add, and on Windows the run-time question is PATH (see On Windows). When you do set it, this is the one piece of setup that is not obvious, and getting it wrong produces a confusing failure rather than a clear one.
OPTRIS_SDK_LIB_DIR satisfies the linker. At startup the dynamic loader searches its own path instead, so an older libotcsdk.so installed system-wide is loaded in preference to the one you linked against. The symptom is not "library not found". It is a successful build followed by
symbol lookup error: undefined symbol: optris_sdk_rule_add_inhibit
or, if the stale copy happens to have the symbol, a silent call into a different version of the SDK.
Add a build.rs to your application:
fn main() { println!("cargo:rerun-if-env-changed=OPTRIS_SDK_LIB_DIR"); let dir = std::env::var("DEP_OTCSDK_LIB_DIR") .or_else(|_| std::env::var("OPTRIS_SDK_LIB_DIR")); if let Ok(dir) = dir { println!("cargo:rustc-link-arg=-Wl,-rpath,{dir}"); println!("cargo:rustc-link-arg=-Wl,--disable-new-dtags"); } }
DEP_OTCSDK_LIB_DIR is handed to you by Cargo because optris-sdk-sys declares links = "otcsdk" and your [dependencies] lists it directly, as step 1 above does; Cargo passes DEP_ variables to the build scripts of immediate dependents and no further. In a project that lists only optris-sdk it is not set, which is why the script falls back to OPTRIS_SDK_LIB_DIR itself. --disable-new-dtags records DT_RPATH rather than DT_RUNPATH, so the directory you linked against beats the default search at run time too.
A crate cannot do this on your behalf, and that is why it is your three lines rather than ours: a build script may only set link arguments for its own package's targets, and your binary is not one of them. If you would rather not add a build script, set LD_LIBRARY_PATH when you run, or install the SDK from the .deb.
A first program
use optris_sdk::{Camera, SimulatedConfig}; fn main() -> optris_sdk::Result<()> { let config = SimulatedConfig::new(); let mut camera = Camera::simulated(&config)?; println!("{} ({})", camera.model()?, camera.serial()?); camera.start()?; // Frames from before the startup calibration has finished arrive // too, and are not measurements. while let Some(frame) = camera.acquire_frame(1000)? { let info = frame.info()?; if info.thermal_data_reliable { println!("{:.1} C at the hottest pixel", info.max_temp_c); break; } } camera.stop()?; Ok(()) }
That needs no hardware. For a real camera, name it: Camera::open() with no argument enumerates and takes the first camera it finds, which on a machine with more than one is whichever answered first, and on a shared machine possibly one somebody else is streaming from. Open a DeviceInfo from optris_sdk::enumerate(), or name the serial.
The API, area by area
The crate's modules follow the same areas as the topic chapters of this manual, so a reader moving between languages finds the same shape. cargo doc --open is the member-by-member reference; every public item carries documentation.
| Area | Module | What is in it |
|---|---|---|
| Device Enumeration | device, hotplug, metadata |
Finding cameras and what enumeration says about them; being told when one is plugged in or unplugged; what this machine records about a camera, keyed by its serial |
| Device Communication | camera_connection |
The status, the timeout and the reconnect |
| Operation Modes | camera_control |
What the sensor measures and how |
| Measurement Fields · Regions | region, camera_regions |
The areas of the frame you want numbers for, and the regions on a camera |
| Alarms | rule |
One signal, one condition, and what to do about it |
| Color Palettes | palette, camera_color |
The colours a picture is drawn with; reading back the colour scale and how it is derived |
| The Canvas | canvas |
Placing a camera's picture in view space, and asking what covers what |
| Recording and Playback | recording, frame_index, camera_continuous, session, video_export |
How a recording is written and what it did; what a seek will cost before paying it; a rolling history and interval capture; several cameras on one clock; writing a recording out as a video |
| Snapshot File Formats | snapshot |
What goes into a still image, and reading one back |
| Process Interface (PIF) | pif |
Industrial I/O on a camera |
| Virtual Imager | simulated, faults |
Simulated cameras in the device list; breaking a camera on purpose so your error handling can be tested |
| Error Handling | error |
The one error model this crate has |
| (none) | camera, camera_open, camera_media |
Opening one, streaming from it, and the media surface |
| (none) | frame, image |
Thermal frames and the lifetimes that keep their pixels honest; the non-thermal representations |
| (none) | enums, durations |
The C API's enumerations as Rust enums; a Duration as the whole units the ABI carries |
| (none) | log, sdkinfo |
The SDK's own log lines into your program's log; facts about the SDK, none of which needs a camera |
The safety contract
Almost everything here is safe, and the compiler enforces the parts that matter. Three exceptions:
Pixel data is borrowed, never copied
A Frame delivered to a callback is valid only until that callback returns; the SDK reuses the memory immediately afterwards. In C nothing stops you keeping the pointer; it stays valid-looking and quietly starts returning the next frame's data.
Here the frame carries a lifetime, so a slice taken from it cannot escape:
camera.on_thermal_frame(move |frame| { let pixels = frame.data()?; // fine, inside the callback escaped = Some(pixels); // does not compile })?;
To keep a frame, call frame.to_owned(). A frame from acquire_frame is already owned and lasts until you drop it.
Nothing is copied to achieve this. At 32 Hz on a 640×480 sensor, copying every frame would be 19 MB/s of pure overhead; these are the same borrowed pointers with a lifetime attached.
A camera may not be used from two threads at once
The C API allows distinct handles to be used concurrently but not one handle. So Camera is Send and deliberately not Sync: move it between threads freely, wrap it in a Mutex to share it. OwnedFrame, Region and Rule are the same. ProcessInterface is neither, because using it reaches through to the camera's controller.
Callbacks run on the SDK's own threads, so every callback closure must be Send. Closures are retained until the camera is dropped; replacing a callback does not release the old one, because the C API gives no signal that it has finished with the pointer.
Two functions are unsafe, and only two
Camera::register_external_buffertakes a pointer the SDK writes into from its own thread. It must stay valid and unmoved until the buffer is removed or the camera is dropped.Camera::configure_surfacetakes aWGPUSurfacebelonging to another library which, as the C header says, cannot be validated.
Everything else in the public API is safe. Panics inside callbacks are caught at the boundary and reported on stderr rather than unwinding into C, which would be undefined behaviour.
Working without a camera
Everything works against a simulated device, which needs no hardware:
use optris_sdk::{Camera, SimulatedConfig, TestPattern}; let config = SimulatedConfig::new() .pattern(TestPattern::HotSpot) .temperature_range(20.0, 120.0) .seed(42); let camera = Camera::simulated(&config)?;
That is what this crate's own tests use, and it is why they pass on a machine with nothing plugged in.
Examples
Twenty-seven examples across eleven areas, at examples/rust/<area>/, declared as examples of the optris-sdk crate. The same twenty-seven exist in C++, C, C# and Python, demonstrating the same things in the same order, so they can be read side by side, see Example Applications for the full table.
Run one from the crate directory with cargo run --example <area>_<name>:
cargo run --example frames_minimal cargo run --example measurement_region_stats
Every one of them is compiled on every build of the SDK, and each opens a simulated camera by default, so none needs hardware and none competes for a camera somebody else is using.