Struct pixtra::pixels::PixelBuilder
source · pub struct PixelBuilder { /* private fields */ }
Expand description
The PixelBuilder is a construct which allows for values bigger than u8 and smaller than 0. This is used if you want to add a lot of values together and divide them later or want to use both positive and negative values.
Implementations§
source§impl PixelBuilder
impl PixelBuilder
sourcepub fn new() -> PixelBuilder
pub fn new() -> PixelBuilder
Creates a new PixelBuilder
with initial values (r, g, b, a) = (0f32, 0f32, 0f32, 0f32)
sourcepub fn from(red: f32, green: f32, blue: f32, alpha: f32) -> PixelBuilder
pub fn from(red: f32, green: f32, blue: f32, alpha: f32) -> PixelBuilder
Creates a new PixelBuilder
with initial values (r, g, b, a) = (red, green, blue, alpha)
.
Examples found in repository?
examples/advanced-filters.rs (lines 86-91)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
fn apply_filter(
canvas: &Canvas,
center: (u32, u32),
kernel: &Vec<f32>,
coords: &Vec<(i32, i32)>,
) -> Pixel {
let scales = kernel
.iter()
.zip(coords.iter())
.fold(Pixel::builder(), |acc, (scale, (x, y))| {
let pixel =
canvas.get_pixel((center.0 as i32 + *x) as u32, (center.1 as i32 + *y) as u32);
acc + PixelBuilder::from(
pixel.r as f32 * scale,
pixel.g as f32 * scale,
pixel.b as f32 * scale,
pixel.a as f32,
)
});
scales.build()
}
fn lap_edge_detection_filter(canvas: &Canvas, x: u32, y: u32) -> Pixel {
// NOTE: The kernel (this vector) needs to sum to 1.0. Below 1.0 your image will appear darker
// and above 1.0 it will appear lighter.
let kernel = vec![0.5, 1.0, 0.5, 1.0, -6.0, 1.0, 0.5, 1.0, 0.5];
let coords = vec![
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(0, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
];
// For simplicity, we will leave out the edges of the picture.
let canvas_size = canvas.dimensions();
if x > 0 && y > 0 && x < canvas_size.width - 1 && y < canvas_size.height - 1 {
let pixel = apply_filter(canvas, (x, y), &kernel, &coords);
return pixel;
}
canvas.get_pixel(x, y)
}
fn prewitt_edge_detection_filter(canvas: &Canvas, x: u32, y: u32) -> Pixel {
let kernel_one: Vec<f32> = vec![1f32, 0f32, -1f32, 1f32, 0f32, -1f32, 1f32, 0f32, -1f32];
let kernel_two: Vec<f32> = vec![1f32, 1f32, 1f32, 0f32, 0f32, 0f32, -1f32, -1f32, -1f32];
let coords = vec![
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(0, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
];
let canvas_size = canvas.dimensions();
if x > 0 && y > 0 && x < canvas_size.width - 1 && y < canvas_size.height - 1 {
let pixel_one = apply_filter(canvas, (x, y), &kernel_one, &coords);
let pixel_two = apply_filter(canvas, (x, y), &kernel_two, &coords);
let pixel = PixelBuilder::from(
((pixel_one.r as u32 * pixel_one.r as u32 + pixel_two.r as u32 * pixel_two.r as u32)
as f64)
.sqrt() as f32,
((pixel_one.g as u32 * pixel_one.g as u32 + pixel_two.g as u32 * pixel_two.g as u32)
as f64)
.sqrt() as f32,
((pixel_one.b as u32 * pixel_one.b as u32 + pixel_two.b as u32 * pixel_two.b as u32)
as f64)
.sqrt() as f32,
255f32,
);
return pixel.build();
}
canvas.get_pixel(x, y)
}
sourcepub fn build(&self) -> Pixel
pub fn build(&self) -> Pixel
Builds a Pixel
from this PixelBuilder
Examples found in repository?
examples/advanced-filters.rs (line 93)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
fn apply_filter(
canvas: &Canvas,
center: (u32, u32),
kernel: &Vec<f32>,
coords: &Vec<(i32, i32)>,
) -> Pixel {
let scales = kernel
.iter()
.zip(coords.iter())
.fold(Pixel::builder(), |acc, (scale, (x, y))| {
let pixel =
canvas.get_pixel((center.0 as i32 + *x) as u32, (center.1 as i32 + *y) as u32);
acc + PixelBuilder::from(
pixel.r as f32 * scale,
pixel.g as f32 * scale,
pixel.b as f32 * scale,
pixel.a as f32,
)
});
scales.build()
}
fn lap_edge_detection_filter(canvas: &Canvas, x: u32, y: u32) -> Pixel {
// NOTE: The kernel (this vector) needs to sum to 1.0. Below 1.0 your image will appear darker
// and above 1.0 it will appear lighter.
let kernel = vec![0.5, 1.0, 0.5, 1.0, -6.0, 1.0, 0.5, 1.0, 0.5];
let coords = vec![
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(0, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
];
// For simplicity, we will leave out the edges of the picture.
let canvas_size = canvas.dimensions();
if x > 0 && y > 0 && x < canvas_size.width - 1 && y < canvas_size.height - 1 {
let pixel = apply_filter(canvas, (x, y), &kernel, &coords);
return pixel;
}
canvas.get_pixel(x, y)
}
fn prewitt_edge_detection_filter(canvas: &Canvas, x: u32, y: u32) -> Pixel {
let kernel_one: Vec<f32> = vec![1f32, 0f32, -1f32, 1f32, 0f32, -1f32, 1f32, 0f32, -1f32];
let kernel_two: Vec<f32> = vec![1f32, 1f32, 1f32, 0f32, 0f32, 0f32, -1f32, -1f32, -1f32];
let coords = vec![
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(0, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
];
let canvas_size = canvas.dimensions();
if x > 0 && y > 0 && x < canvas_size.width - 1 && y < canvas_size.height - 1 {
let pixel_one = apply_filter(canvas, (x, y), &kernel_one, &coords);
let pixel_two = apply_filter(canvas, (x, y), &kernel_two, &coords);
let pixel = PixelBuilder::from(
((pixel_one.r as u32 * pixel_one.r as u32 + pixel_two.r as u32 * pixel_two.r as u32)
as f64)
.sqrt() as f32,
((pixel_one.g as u32 * pixel_one.g as u32 + pixel_two.g as u32 * pixel_two.g as u32)
as f64)
.sqrt() as f32,
((pixel_one.b as u32 * pixel_one.b as u32 + pixel_two.b as u32 * pixel_two.b as u32)
as f64)
.sqrt() as f32,
255f32,
);
return pixel.build();
}
canvas.get_pixel(x, y)
}
sourcepub fn reset(&self) -> PixelBuilder
pub fn reset(&self) -> PixelBuilder
Resets all channels to 0
Trait Implementations§
source§impl Add<Pixel> for PixelBuilder
impl Add<Pixel> for PixelBuilder
§type Output = PixelBuilder
type Output = PixelBuilder
The resulting type after applying the
+
operator.source§impl Add<PixelBuilder> for Pixel
impl Add<PixelBuilder> for Pixel
§type Output = PixelBuilder
type Output = PixelBuilder
The resulting type after applying the
+
operator.source§fn add(self, other: PixelBuilder) -> PixelBuilder
fn add(self, other: PixelBuilder) -> PixelBuilder
Performs the
+
operation. Read moresource§impl Add<PixelBuilder> for PixelBuilder
impl Add<PixelBuilder> for PixelBuilder
§type Output = PixelBuilder
type Output = PixelBuilder
The resulting type after applying the
+
operator.source§fn add(self, other: PixelBuilder) -> PixelBuilder
fn add(self, other: PixelBuilder) -> PixelBuilder
Performs the
+
operation. Read moresource§impl Clone for PixelBuilder
impl Clone for PixelBuilder
source§fn clone(&self) -> PixelBuilder
fn clone(&self) -> PixelBuilder
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl RefUnwindSafe for PixelBuilder
impl Send for PixelBuilder
impl Sync for PixelBuilder
impl Unpin for PixelBuilder
impl UnwindSafe for PixelBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more