Skip to content
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

Support kCMPixelFormat_32BGRA and add github action for running tests #1

Open
wants to merge 15 commits into
base: 0.10
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md
#
# While our "example" application has the platform-specific code,
# for simplicity we are compiling and testing everything on the Ubuntu environment only.
# For multi-OS testing see the `cross.yml` workflow.

on:
pull_request: {}


name: Compile and test Nokhwa Core

jobs:
run_core_tests:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true

- name: Cargo Test
run: |
cd nokhwa-core
cargo test
2 changes: 1 addition & 1 deletion examples/threaded-capture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {

let format = RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestFrameRate);

let first_camera = cameras.first().unwrap();
let first_camera = cameras.get(1).unwrap();

let mut threaded = CallbackCamera::new(first_camera.index().clone(), format, |buffer| {
let image = buffer.decode_image::<RgbAFormat>().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions nokhwa-bindings-linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ mod internal {
FrameFormat::GRAY => FourCC::new(b"GRAY"),
FrameFormat::RAWRGB => FourCC::new(b"RGB3"),
FrameFormat::NV12 => FourCC::new(b"NV12"),
FrameFormat::BGRA => FourCC::new(b"BGRA"),
};

let format = Format::new(new_fmt.width(), new_fmt.height(), v4l_fcc);
Expand Down Expand Up @@ -913,6 +914,7 @@ mod internal {
"GRAY" => Some(FrameFormat::GRAY),
"RGB3" => Some(FrameFormat::RAWRGB),
"NV12" => Some(FrameFormat::NV12),
"BGRA" => Some(FrameFormat::BGRA),
_ => None,
}
}
Expand All @@ -924,6 +926,7 @@ mod internal {
FrameFormat::GRAY => FourCC::new(b"GRAY"),
FrameFormat::RAWRGB => FourCC::new(b"RGB3"),
FrameFormat::NV12 => FourCC::new(b"NV12"),
FrameFormat::BGRA => FourCC::new(b"BGRA"),
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions nokhwa-bindings-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ mod internal {
foundation::{NSArray, NSDictionary, NSInteger, NSString, NSUInteger},
};
use core_media_sys::{
kCMPixelFormat_24RGB, kCMPixelFormat_422YpCbCr8_yuvs,
kCMPixelFormat_24RGB, kCMPixelFormat_32BGRA, kCMPixelFormat_422YpCbCr8_yuvs,
kCMPixelFormat_8IndexedGray_WhiteIsZero, kCMVideoCodecType_422YpCbCr8,
kCMVideoCodecType_JPEG, kCMVideoCodecType_JPEG_OpenDML, CMFormatDescriptionGetMediaSubType,
CMFormatDescriptionRef, CMSampleBufferRef, CMTime, CMVideoDimensions,
Expand Down Expand Up @@ -373,6 +373,7 @@ mod internal {
| kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
| 875704438 => Some(FrameFormat::NV12),
kCMPixelFormat_24RGB => Some(FrameFormat::RAWRGB),
kCMPixelFormat_32BGRA => Some(FrameFormat::BGRA),
_ => None,
}
}
Expand Down Expand Up @@ -511,14 +512,15 @@ mod internal {

// fuck it, use deprecated APIs
pub fn query_avfoundation() -> Result<Vec<CameraInfo>, NokhwaError> {
Ok(AVCaptureDeviceDiscoverySession::new(vec![
let devices = AVCaptureDeviceDiscoverySession::new(vec![
AVCaptureDeviceType::UltraWide,
AVCaptureDeviceType::WideAngle,
AVCaptureDeviceType::Telephoto,
AVCaptureDeviceType::TrueDepth,
AVCaptureDeviceType::External,
])?
.devices())
.devices();
Ok(devices)
}

pub fn get_raw_device_info(index: CameraIndex, device: *mut Object) -> CameraInfo {
Expand Down Expand Up @@ -985,7 +987,6 @@ mod internal {
let format_desc_ref: CMFormatDescriptionRef =
unsafe { msg_send![format.internal, performSelector: format_description_sel] };
let dimensions = unsafe { CMVideoFormatDescriptionGetDimensions(format_desc_ref) };

if dimensions.height == descriptor.resolution().height() as i32
&& dimensions.width == descriptor.resolution().width() as i32
{
Expand Down Expand Up @@ -2284,6 +2285,7 @@ mod internal {
FrameFormat::GRAY => kCMPixelFormat_8IndexedGray_WhiteIsZero,
FrameFormat::NV12 => kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange,
FrameFormat::RAWRGB => kCMPixelFormat_24RGB,
FrameFormat::BGRA => kCMPixelFormat_32BGRA,
};
let obj = CFNumber::from(cmpixelfmt as i32);
let obj = obj.as_CFTypeRef() as *mut Object;
Expand Down
51 changes: 35 additions & 16 deletions nokhwa-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ impl Buffer {
/// Most notably, the `data` **must** stay in scope for the duration of the [`Mat`](https://docs.rs/opencv/latest/opencv/core/struct.Mat.html) or bad, ***bad*** things happen.
#[cfg(feature = "opencv-mat")]
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "opencv-mat")))]
pub fn decode_opencv_mat<F: FormatDecoder>(
&mut self,
) -> Result<BoxedRef<Mat>, NokhwaError> {
pub fn decode_opencv_mat<F: FormatDecoder>(&mut self) -> Result<BoxedRef<Mat>, NokhwaError> {
use crate::buffer::channel_defs::make_mat;

make_mat::<F>(self.resolution, self.buffer())
Expand All @@ -137,11 +135,11 @@ impl Buffer {
&mut self,
dst: &mut Mat,
) -> Result<(), NokhwaError> {
use bytes::Buf;
use image::Pixel;
use opencv::core::{
Mat, MatTraitConst, MatTraitManual, Scalar, CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4,
};
use bytes::Buf;

let array_type = match F::Output::CHANNEL_COUNT {
1 => CV_8UC1,
Expand Down Expand Up @@ -221,23 +219,45 @@ impl Buffer {
/// You (probably) shouldn't use this.
#[cfg(feature = "opencv-mat")]
pub mod channel_defs {
use bytemuck::{cast_slice, Pod, Zeroable};
use image::Pixel;
use crate::error::NokhwaError;
use crate::pixel_format::FormatDecoder;
use crate::types::{FrameFormat, Resolution};
use bytemuck::{cast_slice, Pod, Zeroable};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more cargo fmt

use image::Pixel;

#[cfg(feature = "opencv-mat")]
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "opencv-mat")))]
pub(crate) fn make_mat<F>(resolution: Resolution, data: &[u8]) -> Result<opencv::boxed_ref::BoxedRef<opencv::core::Mat>, NokhwaError> where F: FormatDecoder {
pub(crate) fn make_mat<F>(
resolution: Resolution,
data: &[u8],
) -> Result<opencv::boxed_ref::BoxedRef<opencv::core::Mat>, NokhwaError>
where
F: FormatDecoder,
{
use crate::buffer::channel_defs::*;
use opencv::core::Mat;

let mat = match F::Output::CHANNEL_COUNT {
1 => Mat::new_rows_cols_with_data::<G8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
2 => Mat::new_rows_cols_with_data::<GA8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
3 => Mat::new_rows_cols_with_data::<RGB8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
4 => Mat::new_rows_cols_with_data::<RGBA8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
1 => Mat::new_rows_cols_with_data::<G8>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just cargo fmt

resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
2 => Mat::new_rows_cols_with_data::<GA8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
3 => Mat::new_rows_cols_with_data::<RGB8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
4 => Mat::new_rows_cols_with_data::<RGBA8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
_ => {
return Err(NokhwaError::ProcessFrameError {
src: FrameFormat::RAWRGB,
Expand All @@ -253,16 +273,15 @@ pub mod channel_defs {
src: FrameFormat::RAWRGB,
destination: "OpenCV Mat".to_string(),
error: why.to_string(),
})
}),
}
}


/// Three u8
#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct RGB8 {
pub data: [u8; 3]
pub data: [u8; 3],
}

unsafe impl opencv::core::DataType for RGB8 {
Expand Down Expand Up @@ -324,7 +343,7 @@ pub mod channel_defs {
#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct RGBA8 {
pub data: [u8; 4]
pub data: [u8; 4],
}

unsafe impl opencv::core::DataType for RGBA8 {
Expand All @@ -340,4 +359,4 @@ pub mod channel_defs {
unsafe impl Zeroable for RGBA8 {}

unsafe impl Pod for RGBA8 {}
}
}
Loading
Loading