jrmuizel / raqote

Rust 2D graphics library
BSD 3-Clause "New" or "Revised" License
1.03k stars 59 forks source link

Panic on using clear & arc #165

Closed l4l closed 3 years ago

l4l commented 3 years ago

The following code with crate raqote = "0.8.0" panics with the message "attempt to add with overflow". Quite interesting, that panic goes away if either clear(..) or fill(..) calls commented out.

use std::f32::consts::{FRAC_PI_2, PI};

use raqote::*;

fn main() {
    let mut dt = DrawTarget::new(400, 500);

    dt.clear(SolidSource {
        r: 0xff,
        g: 0xff,
        b: 0xff,
        a: 0x77,
    });

    let mut pb = PathBuilder::new();
    pb.arc(20., 20., 15.0, FRAC_PI_2, PI);
    pb.arc(380., 20., 15.0, 3.0 * FRAC_PI_2, PI);
    let path = pb.finish();

    dt.fill(
        &path,
        &Source::Solid(SolidSource {
            r: 0xcc,
            g: 0xcc,
            b: 0xcc,
            a: 0x90,
        }),
        &DrawOptions::new(),
    );
}

Backtrace:

   3: sw_composite::over_in
             at /home/kitsu/.cargo/registry/src/github.com-1ecc6299db9ec823/sw-composite-0.7.14/src/lib.rs:804
   4: <raqote::blitter::ShaderMaskBlitter as raqote::blitter::Blitter>::blit_span
             at /home/kitsu/.cargo/registry/src/github.com-1ecc6299db9ec823/raqote-0.8.0/src/blitter.rs:468
   5: raqote::draw_target::DrawTarget::composite
             at /home/kitsu/.cargo/registry/src/github.com-1ecc6299db9ec823/raqote-0.8.0/src/draw_target.rs:957
   6: raqote::draw_target::DrawTarget::fill
             at /home/kitsu/.cargo/registry/src/github.com-1ecc6299db9ec823/raqote-0.8.0/src/draw_target.rs:701
   7: raqote_bug::main
             at ./src/main.rs:20
jrmuizel commented 3 years ago

SolidSource is a premultiplied color i.e. r <= a. You can use the from_unpremultiplied_argb() to construct it from unpremultiplied values if that's what you have.

l4l commented 3 years ago

Ah, that's indeed works, thanks. Should color channel variables be private then and have an unsafe constructor instead?

jrmuizel commented 3 years ago

Yeah, maybe. This isn't the first time someone has been bitten by this problem.