From f29fe4c75650529add4ba0f3141395e6441add24 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 20 Apr 2015 22:32:22 -0400 Subject: [PATCH] Playing with Rust and Python --- core_pe/rustmodule/Cargo.lock | 66 +++++++++++++++++++++++++++++++++ core_pe/rustmodule/Cargo.toml | 12 ++++++ core_pe/rustmodule/foo.py | 8 ++++ core_pe/rustmodule/src/block.rs | 27 ++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 core_pe/rustmodule/Cargo.lock create mode 100644 core_pe/rustmodule/Cargo.toml create mode 100644 core_pe/rustmodule/foo.py create mode 100644 core_pe/rustmodule/src/block.rs diff --git a/core_pe/rustmodule/Cargo.lock b/core_pe/rustmodule/Cargo.lock new file mode 100644 index 00000000..b2c11c95 --- /dev/null +++ b/core_pe/rustmodule/Cargo.lock @@ -0,0 +1,66 @@ +[root] +name = "block" +version = "0.0.1" +dependencies = [ + "image 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "byteorder" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "enum_primitive" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "image" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "enum_primitive 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/core_pe/rustmodule/Cargo.toml b/core_pe/rustmodule/Cargo.toml new file mode 100644 index 00000000..60f9fb7b --- /dev/null +++ b/core_pe/rustmodule/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "block" +version = "0.0.1" +authors = ["Virgil Dupras "] + +[lib] +name = "block" +crate-type = ["dylib"] + +[dependencies] +image = "*" + diff --git a/core_pe/rustmodule/foo.py b/core_pe/rustmodule/foo.py new file mode 100644 index 00000000..9815c481 --- /dev/null +++ b/core_pe/rustmodule/foo.py @@ -0,0 +1,8 @@ +import ctypes + +p = 'target/release/libblock-b7b66d53f276d597.so' +imgp = b'/home/hsoft/src/dupeguru/images/dgme_logo_128.png' +block = ctypes.CDLL(p) +s = ctypes.create_string_buffer(imgp) +print(repr(block.block(imgp))) + diff --git a/core_pe/rustmodule/src/block.rs b/core_pe/rustmodule/src/block.rs new file mode 100644 index 00000000..36e9f368 --- /dev/null +++ b/core_pe/rustmodule/src/block.rs @@ -0,0 +1,27 @@ +#![feature(libc)] +extern crate libc; +extern crate image; +use libc::c_char; +use std::ffi::CStr; +use std::str; +use std::path::Path; +use image::GenericImage; + +#[no_mangle] +pub extern "C" fn block(value: *const c_char) -> u32 { + let slice = unsafe { CStr::from_ptr(value) }; + let path = &Path::new(str::from_utf8(slice.to_bytes()).unwrap()); + let image = image::open(path).unwrap().to_rgb(); + let mut totr: u32 = 0; + let mut totg: u32 = 0; + let mut totb: u32 = 0; + for pixel in image.pixels() { + let data = pixel.data; + totr += data[0] as u32; + totg += data[1] as u32; + totb += data[2] as u32; + } + println!("foo! {} {} {}", totr, totg, totb); + 42 +} +