Skip to content

Commit

Permalink
Finish up trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
Meziu committed Nov 19, 2023
1 parent 079f3bd commit 99814fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ctru-rs/examples/camera-image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() {
.expect("Failed to enable auto white balance");
// This line has no effect on the camera since the photos are already shot with `TopLCD` size.
camera
.set_trimming(Trimming::Centered(ViewSize::TopLCD))
.set_trimming(Trimming::new_centered_with_view(ViewSize::TopLCD))
.expect("Failed to enable trimming");
}

Expand Down
63 changes: 50 additions & 13 deletions ctru-rs/src/services/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,18 @@ pub enum ShutterSound {
}

/// Configuration to handle image trimming.
///
/// See [`Trimming::new_centered()`] and the other associated methods for controlled
/// ways of configuring trimming.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Trimming {

Check warning on line 239 in ctru-rs/src/services/cam.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/services/cam.rs
/// Trimming configuration relatively to the center of the image.
Centered(ViewSize),
#[allow(missing_docs)]
Centered{
width: i16,
height: i16,
},
/// Trimming disabled.
Off,
}
Expand Down Expand Up @@ -653,7 +660,7 @@ pub trait Camera: private::ConfigurableCamera {
/// ```

Check warning on line 660 in ctru-rs/src/services/cam.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/services/cam.rs
fn final_view_size(&self) -> (i16, i16) {
match self.trimming() {
Trimming::Centered(trim_size) => trim_size.into(),
Trimming::Centered{width, height} => (width, height),
Trimming::Off => self.view_size().into(),
}
}
Expand All @@ -667,22 +674,24 @@ pub trait Camera: private::ConfigurableCamera {
///
/// # Notes
///
/// Trimming the image view directly on the cameras can only work if the final image has the size of one of the supported image formats.
/// As such, even after trimming the image (which will result in a "zoomed in" view of the original image)
/// the resulting picture must still be of [`ViewSize`] dimensions.
/// Setting up a [`Trimming`] configurations that exceeds the bounds of the original
/// image's size will result in the trimming to be clamped to the maximum borders of the image.
/// Also, the final image size must have a pixel area multiple of 128,
/// otherwise the "status_changed" error may be returned.
#[doc(alias = "CAMU_SetTrimming")]

Check warning on line 681 in ctru-rs/src/services/cam.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/services/cam.rs
fn set_trimming(&mut self, trimming: Trimming) -> crate::Result<()> {
// Run checks for all trimming possibilities.
match trimming {
Trimming::Centered(trim_size) => unsafe {
Trimming::Centered{width, height} => unsafe {
let view_size: (i16, i16) = self.view_size().into();
let trim_size: (i16, i16) = trim_size.into();
let mut trim_size: (i16, i16) = (width, height);

if trim_size.0 > view_size.0 {
trim_size.0 = view_size.0;
};

// Trim sizes are within the view.
assert!(
trim_size.0 <= view_size.0 && trim_size.1 <= view_size.1,
"trimmed view is bigger than the camera view"
);
if trim_size.1 > view_size.1 {
trim_size.1 = view_size.1;
};

ResultCode(ctru_sys::CAMU_SetTrimming(self.port_as_raw(), true))?;

Expand Down Expand Up @@ -1090,6 +1099,34 @@ pub trait Camera: private::ConfigurableCamera {
}
}

impl Trimming {
/// Create a new [`Trimming`] configuration using width and height centered to the original image.

Check warning on line 1103 in ctru-rs/src/services/cam.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/services/cam.rs
///
/// # Panics
///
/// This function will panic if the pixel area of the new configuration (`width * height`)
/// is not a multiple of 128.
pub fn new_centered(width: i16, height: i16) -> Self {
// Pixel area must be a multiple of 128.

Check warning on line 1110 in ctru-rs/src/services/cam.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/services/cam.rs
assert!((width * height) % 128 == 0);

Self::Centered {
width,
height,
}
}

/// Create a new [`Trimming`] configuration using a standard view size centered to the original image.
pub fn new_centered_with_view(size: ViewSize) -> Self {
let size: (i16, i16) = size.into();

Self::Centered {
width: size.0,
height: size.1,
}
}
}

impl Cam {
/// Initialize a new service handle.
///
Expand Down

0 comments on commit 99814fd

Please sign in to comment.