Skip to content

Fix colormaps #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use defines::{ColorMap, MarkerType};
use error::HANDLE_ERROR;
use self::libc::{c_int, c_uint, c_float, c_double, c_char};
use std::ffi::CString;
use std::ptr;

type MutWndHandle = *mut self::libc::c_ulonglong;
type WndHandle = self::libc::c_ulonglong;
Expand Down Expand Up @@ -73,7 +74,7 @@ extern {
pub struct Cell {
pub row: i32,
pub col: i32,
pub title: String,
pub title: *const c_char,
pub cmap: ColorMap,
}

Expand Down Expand Up @@ -148,12 +149,12 @@ impl Window {
pub fn new(width: i32, height: i32, title: String) -> Window {
unsafe {
let mut temp: u64 = 0;
let cstr_ret = CString::new(title.as_bytes());
let cstr_ret = CString::new(title);
match cstr_ret {
Ok(cstr) => {
let err_val = af_create_window(&mut temp as MutWndHandle,
width as c_int, height as c_int,
cstr.to_bytes_with_nul().as_ptr() as *const c_char);
cstr.as_ptr());
HANDLE_ERROR(AfError::from(err_val));
Window::from(temp)
},
Expand Down Expand Up @@ -182,11 +183,11 @@ impl Window {
/// - `title` is the string to be displayed on window title bar
pub fn set_title(&self, title: String) {
unsafe {
let cstr_ret = CString::new(title.as_bytes());
let cstr_ret = CString::new(title);
match cstr_ret {
Ok(cstr) => {
let err_val = af_set_title(self.handle as WndHandle,
cstr.to_bytes_with_nul().as_ptr() as *const c_char);
cstr.as_ptr());
HANDLE_ERROR(AfError::from(err_val));
},
Err(_) => HANDLE_ERROR(AfError::ERR_INTERNAL),
Expand Down Expand Up @@ -284,15 +285,15 @@ impl Window {
/// - `ylabel` is y axis title
/// - `zlabel` is z axis title
pub fn set_axes_titles(&mut self, xlabel: String, ylabel: String, zlabel: String) {
let cprops = &Cell {row: self.row, col: self.col, title: String::from(""), cmap: self.cmap};
let xstr = CString::new(xlabel.as_bytes()).unwrap();
let ystr = CString::new(ylabel.as_bytes()).unwrap();
let zstr = CString::new(zlabel.as_bytes()).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: ptr::null(), cmap: self.cmap};
let xstr = CString::new(xlabel).unwrap();
let ystr = CString::new(ylabel).unwrap();
let zstr = CString::new(zlabel).unwrap();
unsafe {
let err_val = af_set_axes_titles(self.handle as WndHandle,
xstr.to_bytes_with_nul().as_ptr() as *const c_char,
ystr.to_bytes_with_nul().as_ptr() as *const c_char,
zstr.to_bytes_with_nul().as_ptr() as *const c_char,
xstr.as_ptr(),
ystr.as_ptr(),
zstr.as_ptr(),
cprops as *const Cell as CellPtr);
HANDLE_ERROR(AfError::from(err_val));
}
Expand All @@ -314,7 +315,7 @@ impl Window {
/// to next power of 2 and the magnitude remains the same.
pub fn set_axes_limits_compute(&mut self, xrange: &Array, yrange: &Array,
zrange: Option<&Array>, exact: bool) {
let cprops = &Cell {row: self.row, col: self.col, title: String::from(""), cmap: self.cmap};
let cprops = &Cell {row: self.row, col: self.col, title: ptr::null(), cmap: self.cmap};
unsafe {
let err_val = af_set_axes_limits_compute(self.handle as WndHandle,
xrange.get() as AfArray,
Expand Down Expand Up @@ -343,7 +344,7 @@ impl Window {
/// are to extracted. If exact is false then the most significant digit is rounded up
/// to next power of 2 and the magnitude remains the same.
pub fn set_axes_limits_2d(&mut self, xmin: f32, xmax: f32, ymin: f32, ymax: f32, exact: bool) {
let cprops = &Cell {row: self.row, col: self.col, title: String::from(""), cmap: self.cmap};
let cprops = &Cell {row: self.row, col: self.col, title: ptr::null(), cmap: self.cmap};
unsafe {
let err_val = af_set_axes_limits_2d(self.handle as WndHandle, xmin as c_float,
xmax as c_float, ymin as c_float, ymax as c_float,
Expand All @@ -370,7 +371,7 @@ impl Window {
/// to next power of 2 and the magnitude remains the same.
pub fn set_axes_limits_3d(&mut self, xmin: f32, xmax: f32, ymin: f32, ymax: f32,
zmin: f32, zmax: f32, exact: bool) {
let cprops = &Cell {row: self.row, col: self.col, title: String::from(""), cmap: self.cmap};
let cprops = &Cell {row: self.row, col: self.col, title: ptr::null(), cmap: self.cmap};
unsafe {
let err_val = af_set_axes_limits_3d(self.handle as WndHandle, xmin as c_float,
xmax as c_float, ymin as c_float, ymax as c_float,
Expand All @@ -392,7 +393,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_image(self.handle as WndHandle, input.get() as AfArray,
cprops as *const Cell as CellPtr);
Expand All @@ -413,7 +415,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_plot_2d(self.handle as WndHandle,
x.get() as AfArray, y.get() as AfArray,
Expand All @@ -436,7 +439,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_plot_3d(self.handle as WndHandle,
x.get() as AfArray, y.get() as AfArray, z.get() as AfArray,
Expand All @@ -457,7 +461,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_plot_nd(self.handle as WndHandle, points.get() as AfArray,
cprops as *const Cell as CellPtr);
Expand All @@ -479,7 +484,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_hist(self.handle as WndHandle, hst.get() as AfArray,
minval as c_double, maxval as c_double,
Expand All @@ -502,7 +508,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_surface(self.handle as WndHandle,
xvals.get() as AfArray,
Expand All @@ -528,7 +535,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_scatter_2d(self.handle as WndHandle,
xvals.get() as AfArray, yvals.get() as AfArray,
Expand All @@ -553,7 +561,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_scatter_3d(self.handle as WndHandle, xvals.get() as AfArray,
yvals.get() as AfArray, zvals.get() as AfArray,
Expand All @@ -575,7 +584,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_scatter_nd(self.handle as WndHandle, vals.get() as AfArray,
marker as c_int, cprops as *const Cell as CellPtr);
Expand All @@ -599,7 +609,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_vector_field_2d(self.handle as WndHandle,
xpnts.get() as AfArray, ypnts.get() as AfArray,
Expand Down Expand Up @@ -628,7 +639,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_vector_field_3d(self.handle as WndHandle, xpnts.get() as AfArray,
ypnts.get() as AfArray, zpnts.get() as AfArray,
Expand All @@ -653,7 +665,8 @@ impl Window {
Some(s) => s,
None => format!("Cell({},{}))", self.col, self.row)
};
let cprops = &Cell {row: self.row, col: self.col, title: tstr.clone(), cmap: self.cmap};
let tstr = CString::new(tstr).unwrap();
let cprops = &Cell {row: self.row, col: self.col, title: tstr.as_ptr(), cmap: self.cmap};
unsafe {
let err_val = af_draw_vector_field_nd(self.handle as WndHandle,
points.get() as AfArray, directions.get() as AfArray,
Expand Down