1use image::GenericImageView;
2use romy_core::output::*;
3
4pub fn decode_png(data: &[u8]) -> Image {
6 let image = image::load_from_memory(data).unwrap();
7 let rah = image.to_rgba().into_raw();
8 Image::from_data(
9 image.dimensions().0 as i32,
10 image.dimensions().1 as i32,
11 &rah,
12 )
13}
14
15pub trait ImageEngine {
16 fn is_inside(&self, x: i32, y: i32) -> bool;
17 fn blit(&mut self, x: i32, y: i32, image: &Image);
18}
19
20impl ImageEngine for Image {
21 fn is_inside(&self, x: i32, y: i32) -> bool {
22 x >= 0 && x < self.width() && y >= 0 && y < self.height()
23 }
24
25 fn blit(&mut self, x: i32, y: i32, image: &Image) {
26 for image_x in 0..image.width() {
27 for image_y in 0..image.height() {
28 let to_x = x + image_x;
29 let to_y = y + image_y;
30 if !self.is_inside(to_x, to_y) {
31 continue;
32 }
33 let color = image.get_pixel(image_x, image_y);
34 if color.alpha() <= 0.0 {
35 continue;
36 }
37 self.set_pixel(to_x, to_y, color);
38 }
39 }
40 }
41}
42
43pub fn decode_ogg(data: &[u8]) -> Vec<Sound> {
45 let cursor = std::io::Cursor::new(data);
46 let mut srr = lewton::inside_ogg::OggStreamReader::new(cursor).unwrap();
47 let channels = srr.ident_hdr.audio_channels;
48 let sample_rate = srr.ident_hdr.audio_sample_rate;
49
50 let mut samples = Vec::with_capacity(channels as usize);
51 for _ in 0..channels {
52 samples.push(Vec::new());
53 }
54
55 while let Some(pck_samples) = srr.read_dec_packet().unwrap() {
56 for (channel, element) in pck_samples.iter().enumerate() {
57 for element in element {
58 let frac = f32::from(*element) / f32::from(std::i16::MAX);
59 samples[channel].push(frac);
60 }
61 }
62 }
63
64 let mut sounds = Vec::with_capacity(channels as usize);
65 for s in samples {
66 sounds.push(Sound::from_data(sample_rate as i32, &s));
67 }
68
69 sounds
70}