AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Python raster scan image algorithm11/29/2023 ![]() Most open-source programs use rasterization code derived from one of the following projects:įreeType - a font rasterizer which contains both mono and anti-aliasing rasterizer modules that are relatively easy to use stand-alone - that is, for any shape, not just for fonts. ![]() The above is a great over-simplification but gives the general idea. The drawing module can using the coverage as an alpha value applied to the current drawing colour, or as a mask applied to a texture. Send the runs to your drawing module: for example, FreeType emits runs consisting of a Y coordinate, start and end X coordinates, and coverage from 0 to 255. Accumulate runs of pixels in the same state. In simple monochrome rasterization this number, written in the previous stage, will be +1 when crossing an edge into the shape and -1 when coming out of the shape. Maintain a state which indicates whether the current position is inside the shape or not by (for example) adding the number stored in the bitmap to a stored number. ![]() (iv) (scan) Traverse the temporary bitmap, row by row. This is where algorithms differ in the details, and where winding rules ( non-zero versus even-odd) are distinguished. When drawing, mark pixels in some way to indicate (a) whether they are starts or ends of runs - downward lines are starts, and upward lines are ends (b) the coverage - the fraction of the pixel that's inside the shape. Use Bresenham's algorithm for straight lines for curves, bisect until the line is no further than (say) 1/8 of a pixel from the real curve, then use a straight line from start to end. (iii) (mark the run limits) Draw each segment into a temporary bitmap. The usual method is to bisect repeatedly until monotonicity is achieved. This stage is needed only for curved segments, and is relatively easy when using Bézier splines. (ii) (make monotonic in Y) Where necessary, split each segment of the polygon into smaller segments that run continuously upward or downward. The resolution need not be the same, and for anti-aliased images is often greater: for example, FreeType uses 64ths of pixels. (i) (projection) Convert the polygon to the same coordinate system as the destination bitmap. These closed curves are defined so that the inside is always on the left, as the curve is traversed so ordinary shapes run anti-clockwise and holes run clockwise.) (A polygon is defined as one or more closed curves made from straight line segments and parametric splines - in normal practice these are 2nd-order (conic alias quadratic) and 3rd-order (cubic) Bézier splines. The general rasterization algorithm is this, for each polygon in the image.
0 Comments
Read More
Leave a Reply. |