Wavelet rasterization is a method for analytically calculating an anti-aliased rasterization of arbitrary polygons or shape bounded by Bezier curves. For more details, please read the following paper:
Manson, Josiah, and Scott Schaefer. "Wavelet rasterization." Computer Graphics Forum. Vol. 30. No. 2. Blackwell Publishing Ltd, 2011.
This is a python implementation of the algorithm. Currently it supports three types of contours:
- Polygon
- Quadratic Bezier Contour
- Cubic Bezier Contour
Rasterizing these contours are very simple:
- create a specific
Contour
object (Line.Contour
,QuadraticBezier.Contour
, orCubicBezier.Contour
) - use this contour to construct a
Rasterizer
object - call the method
get()
of theRasterizer
object, you get an array of pixels, each of which has a value range from 0 to 1 that indicates the local transparent of the shape.
An example session could like:
import cv2, numpy as np # for image IO
from contour import *
from rasterizer import Rasterizer
## rasterize a polyline
contour = Line.Contour([(4,4), (30,6), (10,14)])
raster = Rasterizer(contour, 32, 32).get()
raster = np.array(np.asarray(raster)*255+0.5, np.uint8)
cv2.imwrite('var/Line.png', raster)
Assuming everything is working OK, the examples should generate the following image:
You can also rasterize a quadratic bezier contour:
contour = QuadraticBezier.Contour([(4,4), (28,4), (28,28), (4,28)])
raster = Rasterizer(contour, 32, 32).get()
raster = np.array(np.asarray(raster)*255+0.5, np.uint8)
cv2.imwrite('var/QuadraticBezier.png', raster)
or a cubic bezier contour:
## rasterize a cubic bezier contour
contour = CubicBezier.Contour([(4,4),(12,4),(28,12),(28,28),(12,28),(4,12)])
raster = Rasterizer(contour, 32, 32).get()
raster = np.array(np.asarray(raster)*255+0.5, np.uint8)
cv2.imwrite('var/CubicBezier.png', raster)