-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split buffer apart, play around with textures
- Loading branch information
Showing
9 changed files
with
355 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,103 @@ | ||
use bytes::{BufMut, BytesMut}; | ||
use memchr::memmem; | ||
extern crate libusb1_sys as usbffi; | ||
use image::{ImageBuffer, Rgb}; | ||
use itertools::Itertools; | ||
|
||
pub fn parse_image_data(data: &Vec<u8>) { | ||
let mut found_frames = 0; | ||
|
||
let mut frame_iter = memmem::find_iter(&data, &[0x33, 0xCC, 0x00, 0x00]); | ||
while let Some(frame_start) = frame_iter.next() { | ||
//println!("{}", frame_start); | ||
let finder = memmem::Finder::new(&[0x33, 0xCC, 0x00, 0x00]); | ||
|
||
let iter = finder.find_iter(&data); | ||
|
||
let result = iter.tuple_windows().collect::<Vec<_>>(); | ||
|
||
for (start, end) in result { | ||
let mut out_buf = BytesMut::with_capacity(0x40000); | ||
out_buf.put(&data[start..end]); | ||
|
||
let (upper_buffer, lower_buffer, sound_buffer) = split_capture_buffer(&out_buf); | ||
|
||
//let mut buf = BytesMut::from(data.as_slice()); | ||
let thing = &data[frame_start + 3..]; | ||
|
||
|
||
|
||
// let mut it = memmem::find(&thing, &[0x33, 0xCC]); | ||
//if let Some(res) = it { | ||
// out_buf.put(&thing[res..res + (720 * 248 * 2)]); | ||
let mut it = memmem::find_iter(&thing, &[0x33, 0xCC]); | ||
|
||
while let Some(res) = it.next() { | ||
/*if (thing[res + 2] == 0x90) { | ||
break; | ||
}*/ | ||
|
||
// Next image is here | ||
if thing[res + 2] == 0x00 && thing[res + 3] == 0x00 { | ||
break; | ||
} | ||
//println!("{}", res); | ||
if res + (248 * 2) > thing.len() { | ||
break; | ||
} | ||
out_buf.put(&thing[res..res + (248 * 2)]) | ||
// print lower image | ||
let result = rgb565_to_rgb(240, 320, &lower_buffer); | ||
if let Some(image) = result { | ||
image.save(format!("./img_out/lower_{}.png", found_frames)); | ||
} | ||
|
||
let mut image_buffer = BytesMut::with_capacity(720 * 248 * 2); | ||
image_buffer.resize(out_buf.len() / 2 * 3, 0); | ||
|
||
let mut j = 0; | ||
for i in (0..out_buf.len()).step_by(2) { | ||
let high = (out_buf[i + 1] as u16) << 8; | ||
let c: u16 = out_buf[i] as u16 + high; | ||
let r = (((c & 0xF800) >> 11) << 3) as u8; | ||
let g = (((c & 0x7E0) >> 5) << 2) as u8; | ||
let b = ((c & 0x1F) << 3) as u8; | ||
|
||
image_buffer[j] = r; | ||
j += 1; | ||
image_buffer[j] = g; | ||
j += 1; | ||
image_buffer[j] = b; | ||
j += 1; | ||
// print upper image | ||
let result = rgb565_to_rgb(240, 400, &upper_buffer); | ||
if let Some(image) = result { | ||
image.save(format!("./img_out/upper_{}.png", found_frames)); | ||
} | ||
|
||
use image::{ImageBuffer, Rgb}; | ||
let buffer = ImageBuffer::<Rgb<u8>, _>::from_raw( | ||
248, | ||
(out_buf.len() / 2 / 248).try_into().unwrap(), | ||
image_buffer, | ||
) | ||
.unwrap(); | ||
// print audio for fun | ||
let result = rgb565_to_rgb(8, 801, &sound_buffer); | ||
if let Some(image) = result { | ||
image.save(format!("./img_out/audio{}.png", found_frames)); | ||
} | ||
|
||
buffer.save(format!("./test_{}.png", found_frames)); | ||
found_frames += 1; | ||
} | ||
} | ||
|
||
//let mut file = fs::File::create(format!("./test_{}.bin", found_frames)).expect("WHY NO FILE"); | ||
fn split_capture_buffer(data: &BytesMut) -> (BytesMut, BytesMut, BytesMut) { | ||
let mut upper_buffer = BytesMut::with_capacity(400 * 240 * 2); | ||
let mut lower_buffer = BytesMut::with_capacity(320 * 240 * 2); | ||
let mut sound_buffer = BytesMut::with_capacity(720 * 8 * 2); | ||
|
||
// split apart buffer into parts | ||
// todo: replace with a nicer byte stream reader | ||
let mut pos = 0; | ||
while pos < data.len() - 496 { | ||
if pos < 81 * 496 { | ||
// copy preamble audio | ||
sound_buffer.extend(&data[pos..pos + 16]); | ||
pos += 496; | ||
} else if pos >= 81 * 496 && pos < 400 * 496 { | ||
sound_buffer.extend(&data[pos..pos + 16]); | ||
pos += 16; | ||
lower_buffer.extend(&data[pos..pos + 480]); | ||
pos += 480; | ||
} else if pos == 400 * 496 { | ||
lower_buffer.extend(&data[pos..pos + 480]); | ||
pos += 480; | ||
sound_buffer.extend(&data[pos..pos + 16]); | ||
pos += 16; | ||
} else if pos > 400 * 496 { | ||
upper_buffer.extend(&data[pos..pos + 480]); | ||
pos += 480; | ||
sound_buffer.extend(&data[pos..pos + 16]); | ||
pos += 16; | ||
} | ||
} | ||
|
||
//file.write_all(&out_buf); | ||
return (upper_buffer, lower_buffer, sound_buffer); | ||
} | ||
|
||
found_frames += 1; | ||
// } | ||
|
||
fn rgb565_to_rgb( | ||
width: u32, | ||
height: u32, | ||
data: &BytesMut, | ||
) -> Option<ImageBuffer<Rgb<u8>, BytesMut>> { | ||
let mut image_buffer = BytesMut::new(); | ||
image_buffer.resize((width * height * 3).try_into().unwrap(), 0); | ||
|
||
let mut j = 0; | ||
for i in (0..data.len()).step_by(2) { | ||
let high = (data[i + 1] as u16) << 8; | ||
let c: u16 = data[i] as u16 + high; | ||
let r = (((c & 0xF800) >> 11) << 3) as u8; | ||
let g = (((c & 0x7E0) >> 5) << 2) as u8; | ||
let b = ((c & 0x1F) << 3) as u8; | ||
|
||
image_buffer[j] = r; | ||
j += 1; | ||
image_buffer[j] = g; | ||
j += 1; | ||
image_buffer[j] = b; | ||
j += 1; | ||
} | ||
|
||
ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, image_buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.