Playing with Rust and Python

This commit is contained in:
Virgil Dupras 2015-04-20 22:32:22 -04:00
parent 942e5a3c31
commit f29fe4c756
4 changed files with 113 additions and 0 deletions

66
core_pe/rustmodule/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -0,0 +1,12 @@
[package]
name = "block"
version = "0.0.1"
authors = ["Virgil Dupras <hsoft@hardcoded.net>"]
[lib]
name = "block"
crate-type = ["dylib"]
[dependencies]
image = "*"

View File

@ -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)))

View File

@ -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
}