diff --git a/gb/src/joypad_menu/joypad_gfx_menu.rs b/gb/src/joypad_menu/joypad_gfx_menu.rs index 4611674b..5022b2d1 100644 --- a/gb/src/joypad_menu/joypad_gfx_menu.rs +++ b/gb/src/joypad_menu/joypad_gfx_menu.rs @@ -43,7 +43,7 @@ impl<'a, GFX: GfxDevice, T, S:AsRef> MenuRenderer for GfxDeviceMenuRe } impl<'a, GFX:GfxDevice> GfxDeviceMenuRenderer<'a, GFX>{ - fn render_string>(prompt: S, frame_buffer: &mut [u32; 23040], frame_buffer_height_index: usize, color:Color, bg:Color) { + fn render_string>(prompt: S, frame_buffer: &mut [Pixel; SCREEN_HEIGHT * SCREEN_WIDTH], frame_buffer_height_index: usize, color:Color, bg:Color) { let mut width_index = 0; for char in prompt.as_ref().as_bytes(){ let glyph = FONT_LUT[(char - FONT_ASCII_START_INDEX) as usize]; diff --git a/gb/src/rpi_gpio/ili9341_controller.rs b/gb/src/rpi_gpio/ili9341_controller.rs index 9bcbc7ff..a2245793 100644 --- a/gb/src/rpi_gpio/ili9341_controller.rs +++ b/gb/src/rpi_gpio/ili9341_controller.rs @@ -100,7 +100,7 @@ impl Ili9341Contoller{ spi.write(Ili9341Commands::VcomControl2, &[0x86]); // Configuring the screen - spi.write(Ili9341Commands::MemoryAccessControl, &[0x20]); // This command tlit the screen 90 degree + spi.write(Ili9341Commands::MemoryAccessControl, &[0x28]); // This command tlit the screen 90 degree and set pixel BGR order spi.write(Ili9341Commands::PixelFormatSet, &[0x55]); // set pixel format to 16 bit per pixel; spi.write(Ili9341Commands::FrameRateControl, &[0x0, 0x10 /*According to the docs this is 119 hrz, setting this option in order to avoid screen tearing on rpi zero2 */]); spi.write(Ili9341Commands::DisplayFunctionControl, &[0x8, 0x82, 0x27]); @@ -142,7 +142,7 @@ impl Ili9341Contoller{ pub fn write_frame_buffer(&mut self, buffer:&[u16;SCREEN_HEIGHT*SCREEN_WIDTH]){ let mut scaled_buffer: [u8;TARGET_SCREEN_HEIGHT * TARGET_SCREEN_WIDTH * 2] = [0;TARGET_SCREEN_HEIGHT * TARGET_SCREEN_WIDTH * 2]; - unsafe{image_inter::scale_biliniear_c::(buffer.as_ptr(), scaled_buffer.as_mut_ptr())}; + unsafe{image_inter::scale_nearest::(buffer.as_ptr(), scaled_buffer.as_mut_ptr())}; let end_x_index = TARGET_SCREEN_WIDTH + FRAME_BUFFER_X_OFFSET - 1; self.spi.write(Ili9341Commands::ColumnAddressSet, &[ diff --git a/image_inter/benches/inter_bench.rs b/image_inter/benches/inter_bench.rs index 857ebe16..5a822e65 100644 --- a/image_inter/benches/inter_bench.rs +++ b/image_inter/benches/inter_bench.rs @@ -22,7 +22,7 @@ pub fn neighbor_rust_inter(c: &mut Criterion){ let input_buffer = [0_u16; 160*144]; let mut output_buffer = [0_u8; 240*266*2]; c.bench_function("bench rust neighbor", |b|b.iter(||{ - unsafe{scale_nearest::<160, 144, 266, 240>(input_buffer.as_ptr(), output_buffer.as_mut_ptr(), 5.0/3.0)}; + unsafe{scale_nearest::<160, 144, 266, 240>(input_buffer.as_ptr(), output_buffer.as_mut_ptr())}; })); } diff --git a/image_inter/src/lib.rs b/image_inter/src/lib.rs index 2eb177b5..c8c7498b 100644 --- a/image_inter/src/lib.rs +++ b/image_inter/src/lib.rs @@ -57,12 +57,14 @@ pub unsafe fn scale_bilinear(input_buffer: *const u16, output_buffer: *mut u8, scale:f32){ +// implemented based on this article - https://towardsdatascience.com/image-processing-image-scaling-algorithms-ae29aaa6b36c +pub unsafe fn scale_nearest(input_buffer: *const u16, output_buffer: *mut u8){ + let scale_x = OUTPUT_WIDTH as f32 / INPUT_WIDTH as f32; + let scale_y = OUTPUT_HEIGHT as f32 / INPUT_HEIGHT as f32; for y in 0..OUTPUT_HEIGHT{ for x in 0..OUTPUT_WIDTH{ - let proj_x = ((1.0 / scale) * x as f32) as usize; - let proj_y = ((1.0 / scale) * y as f32) as usize; + let proj_x = (x as f32 / scale_x).round() as usize; + let proj_y = (y as f32 / scale_y).round() as usize; let pixel = *input_buffer.add((proj_y * INPUT_WIDTH) + proj_x); let output_index = (y * OUTPUT_WIDTH) + x; *output_buffer.add(output_index * 2) = (pixel >> 8) as u8;