John Hunter
2005-05-18 14:32:37 UTC
I would like to overlay dvipng rasters over an arbitrary background,
not just a solid color. My first thought was that one could extract
an alpha mask from the dvipng output by having dvipng blend black and
white and working out what the alpha at each pixel must have been
using the formula (I'm using the red channel but values are the same
for blue and green). First I run dvipng with
dvipng -bg Transparent -fg 'rgb 0.0 0.0 0.0' -D 100 -T tight -o outfile.png somefile.dvi
red = alpha*red_foreground + (1-alpha)*red_background
Since the foreground is black (0) and the background is white (1) this
reduces to red = 1-alpha or
alpha = 1-red
This gives be an MxN arrays of an alpha mask, and I then blend the
dvipng raster over an arbitrary background.
The problem is that the small rasters blended this way don't come out
as nicely as the ones rendered by dvipng assuming a constant
background. So I must be making some error in my logic. At first I
thought I might not be handling gamma properly, but I'm using gamma=1
so I don't have any special correction for that AFAIK. I also
wondered if I'm using the wrong blending formula -- eg is dvipng using
premultiplied alpha or some other formula to blend.
So my questions are
* would it be possible to modify dvipng to have an option to simply
return an alpha mask so we could blend dvi rasters over arbitrary
backgrounds?
* is there a way to calculate this mask from the output of dvipng as
it stands?
Thanks!
JDH
not just a solid color. My first thought was that one could extract
an alpha mask from the dvipng output by having dvipng blend black and
white and working out what the alpha at each pixel must have been
using the formula (I'm using the red channel but values are the same
for blue and green). First I run dvipng with
dvipng -bg Transparent -fg 'rgb 0.0 0.0 0.0' -D 100 -T tight -o outfile.png somefile.dvi
red = alpha*red_foreground + (1-alpha)*red_background
Since the foreground is black (0) and the background is white (1) this
reduces to red = 1-alpha or
alpha = 1-red
This gives be an MxN arrays of an alpha mask, and I then blend the
dvipng raster over an arbitrary background.
The problem is that the small rasters blended this way don't come out
as nicely as the ones rendered by dvipng assuming a constant
background. So I must be making some error in my logic. At first I
thought I might not be handling gamma properly, but I'm using gamma=1
so I don't have any special correction for that AFAIK. I also
wondered if I'm using the wrong blending formula -- eg is dvipng using
premultiplied alpha or some other formula to blend.
So my questions are
* would it be possible to modify dvipng to have an option to simply
return an alpha mask so we could blend dvi rasters over arbitrary
backgrounds?
* is there a way to calculate this mask from the output of dvipng as
it stands?
Thanks!
JDH