Compare commits

..

No commits in common. "a512e710fa1f5ced9316bb6d1ef3af2b08f601d0" and "2347158093b918f7737dddaf65bd13cf9df4e284" have entirely different histories.

View File

@ -11,8 +11,6 @@ struct Args {
temperature: bool, temperature: bool,
#[arg(short, long, default_value = "/dev/video0")] #[arg(short, long, default_value = "/dev/video0")]
device: String, device: String,
#[arg(short, long)]
red_cutoff: Option<f64>,
} }
fn pixel_to_celcius(x: u16) -> u16 { fn pixel_to_celcius(x: u16) -> u16 {
@ -50,25 +48,17 @@ fn main() -> anyhow::Result<()> {
const WIDTH: usize = 288; const WIDTH: usize = 288;
const HEIGHT: usize = 384; const HEIGHT: usize = 384;
let greyscale = !args.temperature || args.red_cutoff.is_none(); let fourcc_repr = [
let fourcc_repr = if greyscale {
[
b'Y', // | 0b10000000 b'Y', // | 0b10000000
b'1', b'6', b'1', b'6',
b' ', // Note: not using b' ' | 0x80, (V4L2_PIX_FMT_Y16_BE) b' ', // Note: not using b' ' | 0x80, (V4L2_PIX_FMT_Y16_BE)
// because VID_S_FMT ioctl returns EINVAL, so just swap the bytes here // because VID_S_FMT ioctl returns EINVAL, so just swap the bytes here
] ];
} else {
// RGB32 is 4 bytes R, G, B, A
[b'R', b'G', b'B', b'4']
};
let bytes_per_pixel = if greyscale { 2 } else { 4 };
let fourcc = v4l::format::FourCC { repr: fourcc_repr }; let fourcc = v4l::format::FourCC { repr: fourcc_repr };
let mut out = v4l::Device::with_path(output)?; let mut out = v4l::Device::with_path(output)?;
// To find the fourcc code, use v4l2-ctl --list-formats-out /dev/video0 // To find the fourcc code, use v4l2-ctl --list-formats-out /dev/video0
// (or read the source :) // (or read the source :)
// flip axes let format = v4l::Format::new(WIDTH as u32, HEIGHT as u32, fourcc);
let format = v4l::Format::new(HEIGHT as u32, WIDTH as u32, fourcc);
Output::set_format(&out, &format)?; Output::set_format(&out, &format)?;
// Setup Capture // Setup Capture
@ -80,13 +70,9 @@ fn main() -> anyhow::Result<()> {
// get a packet and print its bytes // get a packet and print its bytes
const PACKET_LEN: usize = 6972; const PACKET_LEN: usize = 6972;
// input is grayscale 16 bits per pixel
const FRAME_LEN: usize = WIDTH * HEIGHT * 2; const FRAME_LEN: usize = WIDTH * HEIGHT * 2;
let mut frame = [0u8; FRAME_LEN]; let mut frame = [0u8; FRAME_LEN];
let mut len = 0; let mut len = 0;
let output_frame_len = WIDTH * HEIGHT * bytes_per_pixel;
let mut swapped_vec = vec![0u8; output_frame_len];
let swapped = &mut swapped_vec;
while let Ok(p) = cap.next_packet() { while let Ok(p) = cap.next_packet() {
let data = p.data; let data = p.data;
if data.len() != PACKET_LEN { if data.len() != PACKET_LEN {
@ -105,30 +91,14 @@ fn main() -> anyhow::Result<()> {
if len == FRAME_LEN { if len == FRAME_LEN {
// swap the bytes, we are using LE, not BE, 16 bit grayscale // swap the bytes, we are using LE, not BE, 16 bit grayscale
// possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2 // possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2
let mut swapped = [0u8; FRAME_LEN];
for i in 0..FRAME_LEN / 2 { for i in 0..FRAME_LEN / 2 {
let x = i % WIDTH;
let y = (i / WIDTH) % HEIGHT;
let mut pixel = u16::from_be_bytes([frame[i * 2], frame[i * 2 + 1]]); let mut pixel = u16::from_be_bytes([frame[i * 2], frame[i * 2 + 1]]);
if greyscale {
if args.temperature { if args.temperature {
pixel = pixel_to_celcius(pixel); pixel = pixel_to_celcius(pixel);
} }
let pixel_swapped = pixel.to_le_bytes(); let pixel_swapped = pixel.to_le_bytes();
let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 2; swapped[i * 2..i * 2 + 2].copy_from_slice(&pixel_swapped);
swapped[out_i..out_i + 2].copy_from_slice(&pixel_swapped);
} else {
pixel = pixel_to_celcius(pixel);
let cutoff = args.red_cutoff.unwrap();
let r = if pixel > (256.0 * cutoff) as u16 {
255
} else {
0
};
let g = frame[i * 2];
let b = frame[i * 2 + 1];
let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 4;
swapped[out_i..out_i + 4].copy_from_slice(&[0, r, g, b]);
}
} }
out.write_all(&swapped[..])?; out.write_all(&swapped[..])?;
} }