Skip to content

Commit 70e3b27

Browse files
committed
cargo fmt cargo clippy
1 parent 7f0faad commit 70e3b27

File tree

4 files changed

+72
-51
lines changed

4 files changed

+72
-51
lines changed

src/avif/encoding.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
//! Provides AV1 encoding for higher data efficiency via [`ravif`].
22
33
use image::GenericImageView;
4-
use ravif::{Encoder, Img, EncodedImage};
4+
use ravif::{EncodedImage, Encoder, Img};
55

6-
///
6+
///
77
/// Encodes a **formatted** image with the AV1 format.
8-
///
8+
///
99
/// **If you are looking to encode a [`crate::cam::Webcam::get_frame`]'s returned frame [`encode_raw_image`] is the one to use.**
10-
///
10+
///
1111
/// # Behavior
1212
/// The **formatted** image is re-encoded with the settings the user passes in with the [`Encoder`] argument.
1313
/// The encoded image ([`ravif::EncodedImage`]) is returned from the image.
14-
///
14+
///
1515
/// # Error
1616
/// Will return an error if the image format could not be guessed correctly or if the image had incorrect proerties (Eg.: Invalid size).
17-
///
17+
///
1818
pub fn encode_image(encoder: Encoder, image: &[u8]) -> anyhow::Result<EncodedImage> {
1919
//Parse image from bytes
2020
let parsed_image = image::load_from_memory(image)?;
2121

2222
//Iter over the pixels
23-
let colors = parsed_image.pixels().map(|(_, _, color)| {
24-
ravif::RGB8::new(color.0[0], color.0[1], color.0[2])
25-
}).collect::<Vec<ravif::RGB8>>();
23+
let colors = parsed_image
24+
.pixels()
25+
.map(|(_, _, color)| ravif::RGB8::new(color.0[0], color.0[1], color.0[2]))
26+
.collect::<Vec<ravif::RGB8>>();
2627

2728
//Encode image
28-
let encoded_image: EncodedImage = encoder.encode_rgb(Img::new(colors.as_slice(), parsed_image.width() as usize, parsed_image.height() as usize))?;
29-
29+
let encoded_image: EncodedImage = encoder.encode_rgb(Img::new(
30+
colors.as_slice(),
31+
parsed_image.width() as usize,
32+
parsed_image.height() as usize,
33+
))?;
34+
3035
Ok(encoded_image)
3136
}
3237

33-
///
38+
///
3439
/// Encodes a raw image with the AV1 format
35-
///
40+
///
3641
/// # Behavior
3742
/// The image is encoded with the settings the user passes in with the [`Encoder`] argument.
3843
/// The encoded image ([`EncodedImage`]) is returned from the raw image.
39-
///
44+
///
4045
/// # Error
4146
/// If the image had incorrect proerties (Eg.: Invalid size).
42-
///
43-
pub fn encode_raw_image(encoder: Encoder, image: &[u8], width: usize, height: usize) -> anyhow::Result<EncodedImage> {
47+
///
48+
pub fn encode_raw_image(
49+
encoder: Encoder,
50+
image: &[u8],
51+
width: usize,
52+
height: usize,
53+
) -> anyhow::Result<EncodedImage> {
4454
//Iter over the raw pixels
45-
let colors = image.chunks_exact(3).map(|chunk| {
46-
ravif::RGB8::new(chunk[0], chunk[1], chunk[2])
47-
}).collect::<Vec<ravif::RGB8>>();
55+
let colors = image
56+
.chunks_exact(3)
57+
.map(|chunk| ravif::RGB8::new(chunk[0], chunk[1], chunk[2]))
58+
.collect::<Vec<ravif::RGB8>>();
4859

4960
//Encode image
50-
let encoded_image: EncodedImage = encoder.encode_rgb(Img::new(colors.as_slice(), width, height))?;
51-
61+
let encoded_image: EncodedImage =
62+
encoder.encode_rgb(Img::new(colors.as_slice(), width, height))?;
63+
5264
Ok(encoded_image)
5365
}

src/cam/mod.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ pub struct Webcam(VideoCapture);
1818
impl Webcam {
1919
///
2020
/// Create new ```Webcam``` instance with api preference and camera index.
21-
///
21+
///
2222
/// **If you want to use the default api_preference you should use ```new_def(i32)``` instead**
23-
///
23+
///
2424
/// # Behavior
2525
/// Creates a new webcam instance with a set index and an [api_preference](https://docs.rs/opencv/0.93.4/opencv/videoio/enum.VideoCaptureAPIs.html).
26-
///
26+
///
2727
/// # Error
2828
/// Returns an error if the inpt device could not be found based on the camera_idx, or if the api preference was invalid.
29-
///
29+
///
3030
/// # Information
3131
/// API preference consts are available at the [opencv documentation](https://docs.rs/opencv/latest/opencv/index.html). Some exmaples for this const are: ```CAP_MSMF```, ```CAP_V4L```.
32-
///
32+
///
3333
pub fn new(camera_idx: i32, api_preference: i32) -> anyhow::Result<Self> {
3434
let video_capture_handle = VideoCapture::new(camera_idx, api_preference)?;
3535

@@ -40,17 +40,17 @@ impl Webcam {
4040
Ok(Self(video_capture_handle))
4141
}
4242

43-
///
43+
///
4444
/// Create new ```Webcam``` instance with automatic camera detection.
45-
///
45+
///
4646
/// **If you have more than one camera you should use the [`Self::new_def`] function to define which camera you are wanting to use.**
47-
///
47+
///
4848
/// # Behavior
4949
/// Creates a new [`Webcam`] instance while automaticly detecting the camera input.
5050
///
5151
/// # Error
5252
/// This returns an error if it could not find the input device.
53-
///
53+
///
5454
pub fn new_def_auto_detect() -> anyhow::Result<Self> {
5555
let video_capture_handle = VideoCapture::new_def(CAP_ANY)?;
5656

@@ -63,10 +63,10 @@ impl Webcam {
6363

6464
///
6565
/// Create new ```Webcam``` instance with api preference and camera index.
66-
///
66+
///
6767
/// # Behavior
6868
/// Creates a new webcam instance with a set index and the default ```api_preference``` ([`CAP_ANY`]).
69-
///
69+
///
7070
/// # Error
7171
/// Returns an error if the inpt device could not be found based on the camera_idx.
7272
pub fn new_def(camera_idx: i32) -> anyhow::Result<Self> {
@@ -79,13 +79,13 @@ impl Webcam {
7979
Ok(Self(video_capture_handle))
8080
}
8181

82-
///
83-
/// Requests a frame from the [`Webcam`] instance.
84-
///
82+
///
83+
/// Requests a frame from the [`Webcam`] instance.
84+
///
8585
/// # Behavior
8686
/// Reads an image out of the ```VideoCapture``` buffer, this removes the bytes of the image from the buffer.
8787
/// Returns a tuple of the raw image bytes and the size of the image.
88-
///
88+
///
8989
/// # Information
9090
/// Please note the image's bytes returned by this function are automaticly converted from [BRG8](https://learn.microsoft.com/en-us/windows/win32/wic/-wic-codec-native-pixel-formats#rgbbgr-color-model) (Which is returned by opencv by default) to RGB8
9191
///
@@ -95,7 +95,7 @@ impl Webcam {
9595
/// * the color format conversion failed.
9696
/// * there was some kind of error when getting the image bytes / the size of the image from the [`Mat`].
9797
/// * the Webcam instance was invalid. (It got released before requesting this frame)
98-
///
98+
///
9999
pub fn get_frame(&mut self) -> anyhow::Result<(Vec<u8>, Size_<i32>)> {
100100
//Create frame which will be overwritten
101101
let mut frame = Mat::default();
@@ -116,15 +116,15 @@ impl Webcam {
116116
))
117117
}
118118

119-
///
119+
///
120120
/// Get the backend api's name.
121121
///
122122
/// # Behavior
123123
/// Gets the backend api's name.
124-
///
124+
///
125125
/// # Error
126126
/// Returns an error if it failed to get the backend api's name.
127-
///
127+
///
128128
pub fn get_backend_name(&self) -> anyhow::Result<String> {
129129
Ok(self.0.get_backend_name()?)
130130
}
@@ -134,14 +134,14 @@ impl Webcam {
134134
///
135135
/// # Behavior
136136
/// This function releases the [`Webcam`]'s underlying [`VideoCapture`] instance.
137-
///
137+
///
138138
/// # Information
139139
/// The underlying [`VideoCapture`] instance is invalidated, thus requesting frames on this [`Webcam`] instance will be unsuccessful.
140140
///
141141
/// # Error
142142
/// Returns an error if it could not invalidate the instance.
143-
///
143+
///
144144
pub fn release(&mut self) -> anyhow::Result<()> {
145145
Ok(self.0.release()?)
146146
}
147-
}
147+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ pub mod opus;
3434
pub mod cam;
3535

3636
#[cfg(feature = "av1")]
37-
pub mod avif;
37+
pub mod avif;

src/tests.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
44
#[cfg(test)]
55
mod tests {
6-
use std::{fs, thread::sleep, time::Duration};
6+
use std::{thread::sleep, time::Duration};
77

88
use cpal::traits::{DeviceTrait, StreamTrait};
99
use opus::Channels;
1010
use ravif::Encoder;
1111
use tokio::sync::oneshot;
1212

1313
use crate::{
14-
avif::encoding::{encode_image, encode_raw_image}, cam, io::{self, playback::stream_audio, record::record_audio_with_interrupt}, opus::{
14+
avif::encoding::encode_raw_image,
15+
cam,
16+
io::{self, playback::stream_audio, record::record_audio_with_interrupt},
17+
opus::{
1518
decode::{create_opus_decoder, decode_samples_opus},
1619
encode::{create_opus_encoder, encode_samples_opus},
17-
}
20+
},
1821
};
1922

2023
#[test]
@@ -23,9 +26,15 @@ mod tests {
2326
let (bytes, size) = webcam.get_frame().unwrap();
2427

2528
let encoder = Encoder::new().with_speed(3);
26-
let encoded = encode_raw_image(encoder.clone(), &bytes, size.width as usize, size.height as usize).unwrap();
27-
28-
fs::write("asdsad.avif", encoded.avif_file);
29+
let encoded = encode_raw_image(
30+
encoder.clone(),
31+
&bytes,
32+
size.width as usize,
33+
size.height as usize,
34+
)
35+
.unwrap();
36+
37+
assert_ne!(encoded.avif_file.len(), 0)
2938
}
3039

3140
#[test]
@@ -134,4 +143,4 @@ mod tests {
134143

135144
sleep(Duration::from_secs(3));
136145
}
137-
}
146+
}

0 commit comments

Comments
 (0)