plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.81k stars 276 forks source link

[BUG] Unintuitive handling of RGBA images in BitMapElement #194

Open kangalio opened 3 years ago

kangalio commented 3 years ago

Describe the bug I wanted to draw icons on a plot. BitMapElement seemed to be the way to do it, and there was no constructor for it so I used the From<(Coord, image::DynamicImage)> implementation instead. My icon was rather simple; a black cog with a transparent background. However, plotters actually rendered... a completely black square.

At this point I have understood why this happens, but it took me 150 minutes of frustrating debugging work to find the root cause of this issue: https://github.com/38/plotters/blob/master/src/element/image.rs#L171 The problem is that plotters converts images into full-opaque images internally, discarding the transparency information along the way. For that reason, my pretty icon got turned into a black square.

I can think of several possible ways to improve/fix the situation:

  1. instead of From<(Coord, DynamicImage)>, take From<(Coord, DynamicImage, Color)>. Then, don't discard transparency but replace it with the given color instead
    • (On that note, might as well make a proper constructor BitMapElement::new(Coord, DynamicImage, Color) instead of the From implementation. This kind of use is not what From/Into is meant for)
  2. have RGBA support right in BitMapElement itself

Version Information 0.3.0

kmod-midori commented 3 years ago

The library doesn't seem to know RGBA internally at all, since it is not implemented as a PixelFormat.

itohatweb commented 2 years ago

Any update on this?