Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add `png' command to save the result of a yaml file #942

Merged
merged 1 commit into from Mar 3, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add `png' command to save the result of a yaml file

  • Loading branch information
jrmuizel committed Mar 2, 2017
commit b74dbc081ad712e78a364fe7565257955ceaab0e
@@ -52,6 +52,13 @@ args:
help: Enable vsync for OpenGL window

subcommands:
- png:
about: render frame described by YAML and save it to a png file
args:
- INPUT:
help: The input YAML file
required: true
index: 1
- show:
about: show frame(s) described by YAML
args:
@@ -30,6 +30,7 @@ extern crate yaml_rust;
mod binary_frame_reader;
mod json_frame_writer;
mod parse_function;
mod png;
mod reftest;
mod scene;
mod wrench;
@@ -313,6 +314,10 @@ fn main() {
Box::new(YamlFrameReader::new_from_args(subargs)) as Box<WrenchThing>
} else if let Some(subargs) = args.subcommand_matches("replay") {
Box::new(BinaryFrameReader::new_from_args(subargs)) as Box<WrenchThing>
} else if let Some(subargs) = args.subcommand_matches("png") {
let reader = YamlFrameReader::new_from_args(subargs);
png::png(&mut wrench, &mut window, reader);
return;
} else if let Some(subargs) = args.subcommand_matches("reftest") {
let harness = ReftestHarness::new(&mut wrench, &mut window);
let base_manifest = Path::new("reftests/reftest.list");
@@ -0,0 +1,62 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use WindowWrapper;
use gleam::gl;
use image::ColorType;
use image::png::PNGEncoder;
use std::fs::File;
use std::sync::mpsc::{channel, Sender};
use webrender_traits::*;
use wrench::{Wrench, WrenchThing};
use yaml_frame_reader::YamlFrameReader;

pub fn png(wrench: &mut Wrench,
window: &mut WindowWrapper,
mut reader: YamlFrameReader)
{
// setup a notifier so we can wait for frames to be finished
struct Notifier {
tx: Sender<()>,
};
impl RenderNotifier for Notifier {
fn new_frame_ready(&mut self) {
self.tx.send(()).unwrap();
}
fn new_scroll_frame_ready(&mut self, _composite_needed: bool) {}
}
let (tx, rx) = channel();
wrench.renderer.set_render_notifier(Box::new(Notifier { tx: tx }));
reader.do_frame(wrench);

// wait for the frame
rx.recv().unwrap();
wrench.render();

let size = window.get_inner_size_pixels();
let mut data = gl::read_pixels(0,
0,
size.0 as gl::GLsizei,
size.1 as gl::GLsizei,
gl::RGBA,
gl::UNSIGNED_BYTE);
let width = size.0;
let height = size.1;

// flip image vertically (texture is upside down)

This comment has been minimized.

@glennw

glennw Mar 1, 2017

Member

We should probably put this into a function (since it's used elsewhere).

let orig_pixels = data.clone();
let stride = width as usize * 4;
for y in 0..height as usize {
let dst_start = y * stride;
let src_start = (height as usize - y - 1) * stride;
let src_slice = &orig_pixels[src_start .. src_start + stride];
(&mut data[dst_start .. dst_start + stride]).clone_from_slice(&src_slice[..stride]);
}

let encoder = PNGEncoder::new(File::create("out.png").unwrap());

This comment has been minimized.

@glennw

glennw Mar 1, 2017

Member

We should probably allow specifying a filename on the command line.

encoder.encode(&data[..],
width,
height,
ColorType::RGBA(8)).expect("Unable to encode PNG!");
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.