mirror of
https://github.com/arsenetar/dupeguru.git
synced 2024-10-29 21:05:57 +00:00
Playing with Rust and Python
This commit is contained in:
parent
942e5a3c31
commit
f29fe4c756
66
core_pe/rustmodule/Cargo.lock
generated
Normal file
66
core_pe/rustmodule/Cargo.lock
generated
Normal 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"
|
||||
|
12
core_pe/rustmodule/Cargo.toml
Normal file
12
core_pe/rustmodule/Cargo.toml
Normal 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 = "*"
|
||||
|
8
core_pe/rustmodule/foo.py
Normal file
8
core_pe/rustmodule/foo.py
Normal 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)))
|
||||
|
27
core_pe/rustmodule/src/block.rs
Normal file
27
core_pe/rustmodule/src/block.rs
Normal 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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user