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

Upgrade to latest Rust. #21

Merged
merged 1 commit into from Jan 9, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -5,8 +5,7 @@ CXX ?= g++
CFLAGS += -fPIC -I$(VPATH)/../libpng
AR ?= ar
RUSTC ?= rustc
RUSTFLAGS ?=
RUSTFLAGS += -L ../libpng --opt-level=3
RUSTFLAGS := $(filter-out -O,$(RUSTFLAGS)) -L ../libpng --opt-level=3

.PHONY: all
all: librustpng.dummy
@@ -3,4 +3,4 @@
SRCDIR="$(cd $(dirname $0) && pwd)"
sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile

cp -ru ${SRCDIR}/test .
cp -R ${SRCDIR}/test .
3 ffi.rs
@@ -31,6 +31,9 @@ pub static INFO_tRNS: c_int = 0x0010;
pub type png_struct = c_void;
pub type png_info = c_void;

#[link(name = "png")]
#[link(name = "z")]
#[link(name = "shim")]
extern {
// libc routines needed
pub fn setjmp(env: *c_void) -> c_int;
38 lib.rs
@@ -7,9 +7,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[link(name = "png",
vers = "0.1")];
#[crate_type = "lib"];
#[crate_id = "github.com/mozilla-servo/rust-png#png:0.1"];

#[cfg(test)]
extern mod extra;
@@ -24,10 +22,6 @@ use std::libc::{c_int, size_t};

pub mod ffi;

#[nolink]
#[link_args="-L. -lpng -lz -lshim"]
extern {}

#[deriving(Eq)]
pub enum ColorType {
K1, K2, K4, K8, K16,
@@ -47,18 +41,15 @@ pub struct Image {
// This intermediate data structure is used to read
// an image data from 'offset' position, and store it
// to the data vector.
struct ImageData<'self> {
data: &'self [u8],
struct ImageData<'a> {
data: &'a [u8],
offset: uint,
}

#[fixed_stack_segment]
pub fn is_png(image: &[u8]) -> bool {
image.as_imm_buf(|bytes, _len| {
unsafe {
ffi::png_sig_cmp(bytes, 0, 8) == 0
}
})
unsafe {
ffi::png_sig_cmp(image.as_ptr(), 0, 8) == 0
}
}

pub extern fn read_data(png_ptr: *ffi::png_struct, data: *mut u8, length: size_t) {
@@ -68,9 +59,7 @@ pub extern fn read_data(png_ptr: *ffi::png_struct, data: *mut u8, length: size_t
let len = length as uint;
vec::raw::mut_buf_as_slice(data, len, |buf| {
let end_pos = std::num::min(image_data.data.len()-image_data.offset, len);
vec::raw::copy_memory(buf,
image_data.data.slice(image_data.offset, image_data.offset+end_pos),
end_pos);
buf.copy_memory(image_data.data.slice(image_data.offset, image_data.offset+end_pos));
image_data.offset += end_pos;
});
}
@@ -85,7 +74,6 @@ pub fn load_png(path: &Path) -> Result<Image,~str> {
load_png_from_memory(buf)
}

#[fixed_stack_segment]
pub fn load_png_from_memory(image: &[u8]) -> Result<Image,~str> {
unsafe {
let png_ptr = ffi::png_create_read_struct(ffi::png_get_header_ver(ptr::null()),
@@ -151,12 +139,12 @@ pub fn load_png_from_memory(image: &[u8]) -> Result<Image,~str> {
};

let mut image_data = vec::from_elem((width * height * pixel_width) as uint, 0u8);
let image_buf = vec::raw::to_mut_ptr(image_data);
let image_buf = image_data.as_mut_ptr();
let row_pointers: ~[*mut u8] = vec::from_fn(height as uint, |idx| {
ptr::mut_offset(image_buf, (((width * pixel_width) as uint) * idx) as int)
});

ffi::png_read_image(png_ptr, vec::raw::to_ptr(row_pointers));
ffi::png_read_image(png_ptr, row_pointers.as_ptr());

let png_ptr: *ffi::png_struct = &*png_ptr;
let info_ptr: *ffi::png_info = &*info_ptr;
@@ -189,7 +177,6 @@ pub extern fn flush_data(png_ptr: *ffi::png_struct) {
}
}

#[fixed_stack_segment]
pub fn store_png(img: &Image, path: &Path) -> Result<(),~str> {
let mut file = match File::open_mode(path, io::Open, io::Write) {
Some(file) => file,
@@ -236,11 +223,11 @@ pub fn store_png(img: &Image, path: &Path) -> Result<(),~str> {
ffi::png_set_IHDR(&*png_ptr, info_ptr, img.width, img.height, bit_depth, color_type,
ffi::INTERLACE_NONE, ffi::COMPRESSION_TYPE_DEFAULT, ffi::FILTER_NONE);

let image_buf = vec::raw::to_ptr(img.pixels);
let image_buf = img.pixels.as_ptr();
let row_pointers: ~[*u8] = vec::from_fn(img.height as uint, |idx| {
ptr::offset(image_buf, (((img.width * pixel_width) as uint) * idx) as int)
});
ffi::png_set_rows(&*png_ptr, info_ptr, vec::raw::to_ptr(row_pointers));
ffi::png_set_rows(&*png_ptr, info_ptr, row_pointers.as_ptr());

ffi::png_write_png(png_ptr, info_ptr, ffi::TRANSFORM_IDENTITY, ptr::null());

@@ -262,7 +249,6 @@ mod test {
use super::{ColorType, RGB8, RGBA8, KA8, Image};

#[test]
#[fixed_stack_segment]
fn test_valid_png() {
let file = "test/servo-screenshot.png";
let mut reader = match File::open_mode(&Path::new(file), io::Open, io::Read) {
@@ -274,7 +260,7 @@ mod test {
let count = reader.read(buf.mut_slice(0, 1024)).unwrap();
assert!(count >= 8);
unsafe {
let res = ffi::png_sig_cmp(vec::raw::to_ptr(buf), 0, 8);
let res = ffi::png_sig_cmp(buf.as_ptr(), 0, 8);
assert!(res == 0);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.