sondrele / rust-bmp Goto Github PK
View Code? Open in Web Editor NEWSmall library for reading and writing BMP images in Rust
License: MIT License
Small library for reading and writing BMP images in Rust
License: MIT License
I was writing some multithreaded code using channels, and was getting the values for each pixel in a non-deterministic order. When I attempted to just use the set_pixel
function to just write each pixel's value to the image, I got very strange results. I eventually realized that set_pixel
uses the Vec::insert
method, which shifts all elements after the one inserted. This works fine for sequentially assigning the pixels, but breaks when assigning pixels out of order.
Library uses u32 for image size and image coords.
Most libraries use usize as index and size.
So there is a lot of boilerplase cast operators while working with this crate.
Could u change coordinate and size to usize, please?
You may have forgotten to push the new version (0.0.6) to crates.io.
A lot of the time you'll be working on images entirely in-memory and don't want to write to the file system. Is it possible to expose an interface which reads from an io::Read
er and writes to an io::Write
? You probably already work with something like this internally, so it shouldn't require much extra work.
i.e. Something like this:
use std::io::{Read, Write};
fn from_reader<R: Read>(source: R) -> Result<Image> { /* ... */ }
fn to_writer<W: Write>(writer: W, image: Image) -> Result<()> { /* ... */ }
Hi, I noticed that this library doesn't support encoding 8bpp BMPs. ( http://sondrele.github.io/rust-bmp/bmp/index.html )
Is this intentional, or just unimplemented? I'd like to save a 8bpp greyscale image, and BMP seems like an ideal format because of its simplicity.
You should think about copying or merging code from this repository into PistonDevelopers/image. It attempts to be the end-all image library, and it could use BMP support.
Hi Sondre,
It's my first try on Rust, after doing an "Hello world" I've try to use your lib: I'm really a noob, it may be a problem on my side, not yours.
Having "rustc 1.0.0-beta.3", I've done as on your example. I've got this error:
> cargo build
Compiling bmp v0.1.2 (https://github.com/sondrele/rust-bmp#060fb24d)
C:\Users\Yop\.cargo\git\checkouts\rust-bmp-3a7f890ae607b0ba\master\src\lib.rs:3:1: 3:25 error: unstable feature
C:\Users\Yop\.cargo\git\checkouts\rust-bmp-3a7f890ae607b0ba\master\src\lib.rs:3 #![feature(collections)]
^~~~~~~~~~~~~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
error: aborting due to previous error
Could not compile `bmp`.
To learn more, run the command again with --verbose.
When creating a BMP from a normal image buffer, the image ends up rotated 90 degrees. This is due to how BMP is defined, but rust-bmp makes no reference to this.
rust-bmp should state clearly the coordinate convention its API follows.
It's inappropriate to panic on a failure to write a file, since the consumer of this library may want to handle that error. Why not just return the underlying IoResult?
For some reason I'm not sure why, binaries generated with --release parameter produce corrupt bmp headers. I suspect this could be a Rust compiler/LLVM bug, but I haven't had time to debug. I've later found out that https://github.com/pistondevelopers/image also corrupts the whole PNG file with release builds. Debug builds are again OK.
Sample code:
extern crate bmp;
fn main() {
let mut img = bmp::Image::new(100, 100);
for y in 0..100 {
for x in 0..100 {
img.set_pixel(x, y, bmp::Pixel {r: 0, g: 206, b: 209});
}
}
img.save("test.bmp").unwrap();
}
Output:
$ cargo run && xxd test.bmp | head
Running `target/debug/bug`
0000000: 424d 6675 0000 0000 0000 3600 0000 2800 BMfu......6...(.
0000010: 0000 6400 0000 6400 0000 0100 1800 0000 ..d...d.........
0000020: 0000 3075 0000 e803 0000 e803 0000 0000 ..0u............
0000030: 0000 0000 0000 d1ce 00d1 ce00 d1ce 00d1 ................
0000040: ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce ................
0000050: 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 ................
0000060: d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ................
0000070: ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce ................
0000080: 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 ................
0000090: d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ................
$ cargo run --release && xxd test.bmp | head
Running `target/release/bug`
0000000: 424d 0200 0000 0000 0000 0200 0000 0400 BM..............
0000010: 0000 0400 0000 0400 0000 0000 0000 0200 ................
0000020: 0000 0400 0000 0400 0000 0400 0000 0400 ................
0000030: 0000 0400 0000 d1ce 00d1 ce00 d1ce 00d1 ................
0000040: ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce ................
0000050: 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 ................
0000060: d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ................
0000070: ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce ................
0000080: 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ce00 ................
0000090: d1ce 00d1 ce00 d1ce 00d1 ce00 d1ce 00d1 ................
hi
Cannot compile on 1.60.0-nightly..
It'd be great to have basic transformations such as rotate (90° steps) and flip (horizontal/vertical).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.