Skip to content

Commit

Permalink
Add dithering support
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Jan 13, 2021
1 parent 2a67bc4 commit 0560b08
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 62 deletions.
2 changes: 2 additions & 0 deletions dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[ -d dist ] && rm -Rf dist

[ -d bin ] || ./download.sh 'bin/*'
[ -d resources ] || ./download.sh 'resources/*'
[ -d hyphenation-patterns ] || ./download.sh 'hyphenation-patterns/*'
[ -e target/arm-unknown-linux-gnueabihf/release/plato ] || ./build.sh

Expand All @@ -29,6 +30,7 @@ cp -R keyboard-layouts dist
cp -R bin dist
cp -R scripts dist
cp -R icons dist
cp -R resources dist
cp -R fonts dist
cp -R css dist
find dist/css -name '*-user.css' -delete
Expand Down
2 changes: 1 addition & 1 deletion doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The following swipe sequences are recognized:
- Top left/right corner: go to the previous/next bookmark.
- Top left/right multi-corner: go to the previous/next annotation or highlight.
- Bottom left corner: guess the frontlight if there's more than two frontlight presets defined, toggle the frontlight otherwise.
- Bottom right corner: toggle the bitonal mode.
- Bottom right corner: toggle the dithering mode.

Simultaneous swipe sequences:

Expand Down
40 changes: 27 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ struct HistoryItem {
view: Box<dyn View>,
rotation: i8,
monochrome: bool,
dithered: bool,
}

fn build_context(fb: Box<dyn Framebuffer>) -> Result<Context, Error> {
Expand Down Expand Up @@ -814,16 +815,21 @@ pub fn run() -> Result<(), Error> {
},
Event::Open(info) => {
let rotation = context.display.rotation;
if let Some(n) = info.reader.as_ref()
.and_then(|r| r.rotation.map(|n| CURRENT_DEVICE.from_canonical(n))) {
if CURRENT_DEVICE.orientation(n) != CURRENT_DEVICE.orientation(rotation) {
updating.retain(|tok, _| context.fb.wait(*tok).is_err());
if let Ok(dims) = context.fb.set_rotation(n) {
raw_sender.send(display_rotate_event(n)).ok();
context.display.rotation = n;
context.display.dims = dims;
let dithered = context.fb.dithered();
if let Some(reader_info) = info.reader.as_ref() {
if let Some(n) = reader_info.rotation.map(|n| CURRENT_DEVICE.from_canonical(n)) {
if CURRENT_DEVICE.orientation(n) != CURRENT_DEVICE.orientation(rotation) {
updating.retain(|tok, _| context.fb.wait(*tok).is_err());
if let Ok(dims) = context.fb.set_rotation(n) {
raw_sender.send(display_rotate_event(n)).ok();
context.display.rotation = n;
context.display.dims = dims;
}
}
}
context.fb.set_dithered(reader_info.dithered);
} else {
context.fb.set_dithered(context.settings.reader.dithered_kinds.contains(&info.file.kind));
}
let path = info.file.path.clone();
if let Some(r) = Reader::new(context.fb.rect(), *info, &tx, &mut context) {
Expand All @@ -832,7 +838,8 @@ pub fn run() -> Result<(), Error> {
history.push(HistoryItem {
view,
rotation,
monochrome: context.fb.monochrome()
monochrome: context.fb.monochrome(),
dithered,
});
view = next_view;
} else {
Expand All @@ -843,6 +850,7 @@ pub fn run() -> Result<(), Error> {
context.display.dims = dims;
}
}
context.fb.set_dithered(dithered);
handle_event(view.as_mut(), &Event::Invalid(path), &tx, &mut bus, &mut rq, &mut context);
}
},
Expand All @@ -864,6 +872,7 @@ pub fn run() -> Result<(), Error> {
view,
rotation: context.display.rotation,
monochrome: context.fb.monochrome(),
dithered: context.fb.dithered(),
});
view = next_view;
},
Expand All @@ -876,6 +885,7 @@ pub fn run() -> Result<(), Error> {
view,
rotation: context.display.rotation,
monochrome: context.fb.monochrome(),
dithered: context.fb.dithered(),
});
view = next_view;
},
Expand All @@ -901,7 +911,8 @@ pub fn run() -> Result<(), Error> {
history.push(HistoryItem {
view,
rotation: context.display.rotation,
monochrome
monochrome,
dithered: context.fb.dithered(),
});
view = next_view;
},
Expand All @@ -911,6 +922,9 @@ pub fn run() -> Result<(), Error> {
if item.monochrome != context.fb.monochrome() {
context.fb.set_monochrome(item.monochrome);
}
if item.dithered != context.fb.dithered() {
context.fb.set_dithered(item.dithered);
}
if CURRENT_DEVICE.orientation(item.rotation) != CURRENT_DEVICE.orientation(context.display.rotation) {
updating.retain(|tok, _| context.fb.wait(*tok).is_err());
if let Ok(dims) = context.fb.set_rotation(item.rotation) {
Expand Down Expand Up @@ -971,9 +985,9 @@ pub fn run() -> Result<(), Error> {
context.fb.toggle_inverted();
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Gui));
},
Event::Select(EntryId::ToggleMonochrome) => {
context.fb.toggle_monochrome();
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Gui));
Event::Select(EntryId::ToggleDithered) => {
context.fb.toggle_dithered();
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Full));
},
Event::Select(EntryId::ToggleIntermissionImage(ref kind, ref path)) => {
let full_path = context.library.home.join(path);
Expand Down
17 changes: 10 additions & 7 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,24 @@ impl Framebuffer for WindowCanvas {
Ok((width, height))
}

fn set_monochrome(&mut self, _enable: bool) {
}

fn set_dithered(&mut self, _enable: bool) {
}

fn set_inverted(&mut self, _enable: bool) {
}

fn set_monochrome(&mut self, _enable: bool) {
fn monochrome(&self) -> bool {
false
}

fn inverted(&self) -> bool {
fn dithered(&self) -> bool {
false
}

fn monochrome(&self) -> bool {
fn inverted(&self) -> bool {
false
}

Expand Down Expand Up @@ -482,10 +489,6 @@ fn main() -> Result<(), Error> {
context.fb.toggle_inverted();
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Gui));
},
Event::Select(EntryId::ToggleMonochrome) => {
context.fb.toggle_monochrome();
rq.add(RenderData::new(view.id(), context.fb.rect(), UpdateMode::Gui));
},
Event::Select(EntryId::TakeScreenshot) => {
let name = Local::now().format("screenshot-%Y%m%d_%H%M%S.png");
let msg = match context.fb.save(&name.to_string()) {
Expand Down
23 changes: 20 additions & 3 deletions src/framebuffer/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs::File;
use std::path::Path;
use anyhow::{Error, Context, format_err};
use super::{Framebuffer, UpdateMode};
use crate::color::WHITE;
Expand Down Expand Up @@ -28,6 +29,15 @@ impl Pixmap {
pub fn data_mut(&mut self) -> &mut [u8] {
&mut self.data
}

pub fn from_png<P: AsRef<Path>>(path: P) -> Result<Pixmap, Error> {
let file = File::open(path.as_ref())?;
let decoder = png::Decoder::new(file);
let (info, mut reader) = decoder.read_info()?;
let mut pixmap = Pixmap::new(info.width, info.height);
reader.next_frame(pixmap.data_mut())?;
Ok(pixmap)
}
}

impl Framebuffer for Pixmap {
Expand Down Expand Up @@ -95,17 +105,24 @@ impl Framebuffer for Pixmap {
Err(format_err!("Unsupported."))
}

fn set_monochrome(&mut self, _enable: bool) {
}

fn set_dithered(&mut self, _enable: bool) {
}

fn set_inverted(&mut self, _enable: bool) {
}

fn set_monochrome(&mut self, _enable: bool) {
fn monochrome(&self) -> bool {
false
}

fn inverted(&self) -> bool {
fn dithered(&self) -> bool {
false
}

fn monochrome(&self) -> bool {
fn inverted(&self) -> bool {
false
}

Expand Down
Loading

0 comments on commit 0560b08

Please sign in to comment.