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

source

pub fn new() -> PixelBuilder

Creates a new PixelBuilder with initial values (r, g, b, a) = (0f32, 0f32, 0f32, 0f32)

source

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)
}
source

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)
}
source

pub fn reset(&self) -> PixelBuilder

Resets all channels to 0

Trait Implementations§

source§

impl Add<Pixel> for PixelBuilder

§

type Output = PixelBuilder

The resulting type after applying the + operator.
source§

fn add(self, other: Pixel) -> PixelBuilder

Performs the + operation. Read more
source§

impl Add<PixelBuilder> for Pixel

§

type Output = PixelBuilder

The resulting type after applying the + operator.
source§

fn add(self, other: PixelBuilder) -> PixelBuilder

Performs the + operation. Read more
source§

impl Add<PixelBuilder> for PixelBuilder

§

type Output = PixelBuilder

The resulting type after applying the + operator.
source§

fn add(self, other: PixelBuilder) -> PixelBuilder

Performs the + operation. Read more
source§

impl Clone for PixelBuilder

source§

fn clone(&self) -> PixelBuilder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PixelBuilder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V