Advertisement
honey_the_codewitch

rasterizer routine (C#)

Dec 11th, 2023
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. void _RasterizeSortedEdges(float tx, float ty, float scale, SvgRasterizerCachedPaint cache, SvgFillRule fillRule)
  2. {
  3.     SvgRasterizerActiveEdge active = null;
  4.     int y, s;
  5.     int e = 0;
  6.     int maxWeight = (255 / SVG_SUBSAMPLES);  // weight per vertical scanline
  7.     int xmin, xmax;
  8.  
  9.     for (y = 0; y < height; y++)
  10.     {
  11.         Array.Clear(scanline, 0, width);
  12.         xmin = width;
  13.         xmax = 0;
  14.         for (s = 0; s < SVG_SUBSAMPLES; ++s)
  15.         {
  16.             // find center of pixel for this scanline
  17.             float scany = (float)(y * SVG_SUBSAMPLES + s) + 0.5f;
  18.             SvgRasterizerActiveEdge step = active;
  19.  
  20.             // update all active edges;
  21.             // remove all active edges that terminate before the center of this scanline
  22.             while (step!=null)
  23.             {
  24.                 SvgRasterizerActiveEdge z = step;
  25.                 if (z.ey <= scany)
  26.                 {
  27.                     step = z.next;  // delete from list
  28.                                         //                  NSVG__assert(z->valid);
  29.                     _FreeActive(z);
  30.                 }
  31.                 else
  32.                 {
  33.                     z.x += z.dx;            // advance to position for current scanline
  34.                     step = step.next;  // advance through list
  35.                 }
  36.             }
  37.  
  38.             // resort the list if needed
  39.             for (; ; )
  40.             {
  41.                 bool changed = false;
  42.                 step = active;
  43.                 while (step!=null && step.next!=null)
  44.                 {
  45.                     if (step.x > step.next.x)
  46.                     {
  47.                         SvgRasterizerActiveEdge te = step;
  48.                         SvgRasterizerActiveEdge q = te.next;
  49.                         te.next = q.next;
  50.                         q.next = te;
  51.                         step = q;
  52.                         changed = true;
  53.                     }
  54.                     step = step.next;
  55.                 }
  56.                 if (!changed) break;
  57.             }
  58.  
  59.             // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
  60.             while (e < edges.Count && edges[e].y0 <= scany)
  61.             {
  62.                 if (edges[e].y1 > scany)
  63.                 {
  64.                     SvgRasterizerEdge edge = edges[e];
  65.                     SvgRasterizerActiveEdge z = _AddActive(ref edge, scany);
  66.                     edges[e] = edge;
  67.                     if (z == null) break;
  68.                     // find insertion point
  69.                     if (active == null)
  70.                     {
  71.                         active = z;
  72.                     }
  73.                     else if (z.x < active.x)
  74.                     {
  75.                         // insert at front
  76.                         z.next = active;
  77.                         active = z;
  78.                     }
  79.                     else
  80.                     {
  81.                         // find thing to insert AFTER
  82.                         SvgRasterizerActiveEdge p = active;
  83.                         while (p.next!=null && p.next.x < z.x)
  84.                             p = p.next;
  85.                         // at this point, p->next->x is NOT < z->x
  86.                         z.next = p.next;
  87.                         p.next = z;
  88.                     }
  89.                 }
  90.                 ++e;
  91.             }
  92.  
  93.             // now process all active edges in non-zero fashion
  94.             if (active != null)
  95.                 _FillActiveEdges(scanline, width, active, maxWeight, ref xmin, ref xmax, fillRule);
  96.         }
  97.         // Blit
  98.         if (xmin < 0) xmin = 0;
  99.         if (xmax > width - 1) xmax = width - 1;
  100.         if (xmin <= xmax)
  101.         {
  102.             _ScanlineSolid(null,0, readCallback, writeCallback, xmax - xmin + 1, scanline,xmin, xmin, y, tx, ty, scale, cache);
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement