Skip to content

Commit

Permalink
lbmp support
Browse files Browse the repository at this point in the history
  • Loading branch information
dlunch committed Oct 19, 2024
1 parent b69d420 commit 760ace1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
35 changes: 35 additions & 0 deletions wie_backend/src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod lbmp;

use core::mem::size_of;

use ab_glyph::{Font, FontRef, ScaleFont};
Expand All @@ -7,6 +9,8 @@ use num_traits::{Num, Zero};

use wie_util::{Result, WieError};

use self::lbmp::decode_lbmp;

lazy_static::lazy_static! {
static ref FONT: FontRef<'static> = FontRef::try_from_slice(include_bytes!("../../fonts/neodgm.ttf")).unwrap();
}
Expand Down Expand Up @@ -56,6 +60,33 @@ pub trait PixelType: Send {
fn to_color(raw: Self::DataType) -> Color;
}

pub struct Rgb332Pixel;

impl PixelType for Rgb332Pixel {
type DataType = u8;

fn from_color(color: Color) -> Self::DataType {
let r = (color.r * 7 + 127) / 255;
let g = (color.g * 7 + 127) / 255;
let b = (color.b * 3 + 127) / 255;

(r << 5) | (g << 2) | b
}

fn to_color(raw: Self::DataType) -> Color {
let r = (raw >> 5) & 0x7;
let g = (raw >> 2) & 0x7;
let b = raw & 0x3;

Color {
a: 0xff,
r: r * 36,
g: g * 36,
b: b * 85,
}
}
}

pub struct Rgb565Pixel;

impl PixelType for Rgb565Pixel {
Expand Down Expand Up @@ -433,6 +464,10 @@ impl Clip {
pub fn decode_image(data: &[u8]) -> Result<Box<dyn Image>> {
use std::io::Cursor;

if data[0] == b'L' && data[1] == b'B' && data[2] == b'M' && data[3] == b'P' {
return decode_lbmp(data);
}

let image = ImageReader::new(Cursor::new(&data))
.with_guessed_format()
.map_err(|x| WieError::FatalError(x.to_string()))?
Expand Down
40 changes: 40 additions & 0 deletions wie_backend/src/canvas/lbmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use bytemuck::{cast_vec, from_bytes, Pod, Zeroable};

use wie_util::{Result, WieError};

use crate::canvas::{Image, Rgb332Pixel, Rgb565Pixel, VecImageBuffer};

// lcd bitmap file format for skvm

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct LbmpHeader {
descriptor: u32,
r#type: u32,
width: u32,
height: u32,
size: u32,
mask: u32,
}

pub fn decode_lbmp(data: &[u8]) -> Result<Box<dyn Image>> {
let header: &LbmpHeader = from_bytes(&data[0..24]);
let data = &data[24..];

if header.r#type == 2 || header.r#type == 3 {
// unsupported grayscale
return Err(WieError::Unimplemented(format!("Unsupported grayscale type {}", header.r#type)));
}

Ok(if header.r#type == 8 {
Box::new(VecImageBuffer::<Rgb332Pixel>::from_raw(header.width, header.height, data.to_vec()))
} else if header.r#type == 16 {
Box::new(VecImageBuffer::<Rgb565Pixel>::from_raw(
header.width,
header.height,
cast_vec(data.to_vec()),
))
} else {
return Err(WieError::Unimplemented(format!("Unsupported type {}", header.r#type)));
})
}
3 changes: 2 additions & 1 deletion wie_midp/src/classes/javax/microedition/lcdui/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use jvm::{
Array, ClassInstanceRef, Jvm, Result as JvmResult,
};

use wie_backend::canvas::{decode_image, ArgbPixel, Canvas, Image as BackendImage, ImageBufferCanvas, Rgb565Pixel, VecImageBuffer};
use wie_backend::canvas::{decode_image, ArgbPixel, Canvas, Image as BackendImage, ImageBufferCanvas, Rgb332Pixel, Rgb565Pixel, VecImageBuffer};
use wie_jvm_support::{WieJavaClassProto, WieJvmContext};

use crate::classes::javax::microedition::lcdui::Graphics;
Expand Down Expand Up @@ -199,6 +199,7 @@ impl Image {
let bytes_per_pixel = bpl / width;

Ok(match bytes_per_pixel {
1 => Box::new(VecImageBuffer::<Rgb332Pixel>::from_raw(width as _, height as _, pod_collect_to_vec(&buf))) as Box<_>,
2 => Box::new(VecImageBuffer::<Rgb565Pixel>::from_raw(width as _, height as _, pod_collect_to_vec(&buf))) as Box<_>,
4 => Box::new(VecImageBuffer::<ArgbPixel>::from_raw(width as _, height as _, pod_collect_to_vec(&buf))) as Box<_>,
_ => unimplemented!("Unsupported pixel format: {}", bytes_per_pixel),
Expand Down

0 comments on commit 760ace1

Please sign in to comment.