# Poor quality Rasterize output when Background -> None

I am attempting to generate partially transparent images for PNG Export, but seem to run to the following issue. If I Rasterize a simple piece of Graphics with Background -> None (transparent) it looks worse:

rings = Image[
Rasterize[
Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}],
Background -> #, RasterSize -> 400, ImageSize -> 400]] & /@ {None, White}


First version appears jagged, as a circle drawn using a too simple polygon would do. I can verify this with ImageDifference:

ImageAdjust[ImageDifference @@ (RemoveAlphaChannel[#, White] & /@ rings)]


Jagged pattern is clearly visible.

How to use Rasterize with Background -> None (or anything similar generating an alpha-channel image) and achieve good output quality without resorting to generating primitives such as Disk using hand-crafted code?

Clarification:

I want alpha channel on the output to behave as it does on Rasterize[..., Background -> None]. That is, the image having conceptually three regions: transparent background, black outer disk and white (non-transparent) inner disk.

(These screenshots were taken on Mathematica 9.0.1.0 running on OS X 10.8.4.)

-
I could not reproduce your example in v7 on Windows, FWIW. –  Mr.Wizard Sep 9 '13 at 20:20
@Mr.Wizard I hope someone can try to reproduce this on v9. These things tend to be particularly dependent of graphics hardware and drivers, but it would really seem like Mma just uses a poor polygon... unless that's some sort of failed attempt to render a spline. –  kirma Sep 9 '13 at 20:30
I see it in v9 on Windows, but it works as expected if I set the Background option in Graphics instead of Rasterize. –  Michael Hale Sep 10 '13 at 4:56
@MichaelHale Thanks for confirming this issue! The sad part about Background -> None in Graphics is that it alone is not enough to produce an alpha-channel image, and if the same is done also in Rasterize, the issue creeps up again. –  kirma Sep 10 '13 at 5:24

Another easy idea: use difference between white and black backgrounds to set alpha channel

SetAlphaChannel[#2, ColorNegate@ImageSubtract[##]] & @@ (
Rasterize[
Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}],
Background -> #, RasterSize -> 400,
ImageSize -> 400] & /@ {Black, White})

-
If you don't mind my asking: what is your background with Mathematica? You are new to our site but quite clearly not new to Mathematica. Thanks for posting lots of good answers. –  Mr.Wizard Sep 10 '13 at 10:49
Heh. Considering confusing multitude of color specifications in Mathematica, this is probably easiest way to accomplish the task at hand, even if not the most conceptually puritan. –  kirma Sep 10 '13 at 12:23
Accepting this answer. Thanks! –  kirma Sep 10 '13 at 12:35
@Mr.Wizard I'm glad you enjoyed my answers. I started to use Mathematica intensively one year ago when I realized that it is suitable for big numerical calculations. My scientific interests are conductivity and thermal conductivity of disordered media, random matrix theory, and related topics (may be I intersect with the scientific area of Leonid Shifrin). –  ybeltukov Sep 10 '13 at 22:50
@Mr.Wizard For numerical analysis of eigenvalue distributions, Green functions, etc. I have implemented The Kernel Polynomial Method and have got a great experience in Mathematica. For example, this this method can very fast plot something like SmoothHistogram@Eigenvalues of large sparse matrices. If anybody wants I can tell more about it. –  ybeltukov Sep 10 '13 at 22:50

Oops. I was too tired to fully verify the expected output before I wrote my comment. Here is a simple hack to make the image you want though.

disk[c_, r_] :=
Polygon@Table[c + r {Cos@a, Sin@a}, {a, 0, 2 Pi, Pi/50}]
Image[Rasterize[
Graphics[{Black, disk[{0, 0}, 1], White,
disk[{1, 1}/78, 1 - Sqrt[2]/64]}], Background -> None,
RasterSize -> 400, ImageSize -> 400]]

-
Yes, this fixes the issue, but it's a trick I wanted to avoid and already referred on my original question as "without resorting to generating primitives such as Disk using hand-crafted code". What if similar issues come up with less obvious graphics primitives? That's the problem I'm pondering about. Nonetheless, +1. –  kirma Sep 10 '13 at 7:55

One easy idea is to set the alpha channel directly but whether this satisfy your needs really depends on what exactly you want to have. Btw, it is worth that you use ColorSeparate to see for yourself what exactly the output in the single channels of your jagged image is; you may be surprised.

SetAlphaChannel[#, ColorNegate[#]] &@
Rasterize[
Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}],
Background -> White, RasterSize -> 400, ImageSize -> 400]


-
Conceptually, I'd want area surrounding the outer circle to be transparent, but inner white circle not to be. Surely, this can be achieved by painting alpha channel separately and combining it to color channels. This far, it'd seem there is a bug, and the best that can be achieved is a workaround. –  kirma Sep 10 '13 at 8:01