void master_alpha(int style, double alpha)
 {
     if (style >= 0)
     {
         while ((int)m_master_alpha.size() <= style)
         {
             m_master_alpha.add(aa_mask);
         }
         m_master_alpha.Array[style] = agg_basics.uround(alpha * aa_mask);
     }
 }
            public void rotate_colors()
            {
                int i;

                for (i = 1; i < m_vertices.size(); i++)
                {
                    RGBA_Bytes c  = m_vertices[i].color;
                    RGBA_Bytes dc = m_vertices[i].dc;
                    int        r  = (int)c.Red0To255 + (dc.Red0To255 != 0 ? 5 : -5);
                    int        g  = (int)c.Green0To255 + (dc.Green0To255 != 0 ? 5 : -5);
                    int        b  = (int)c.Blue0To255 + (dc.Blue0To255 != 0 ? 5 : -5);
                    if (r < 0)
                    {
                        r = 0; dc.Red0To255 ^= 1;
                    }
                    if (r > 255)
                    {
                        r = 255; dc.Red0To255 ^= 1;
                    }
                    if (g < 0)
                    {
                        g = 0; dc.Green0To255 ^= 1;
                    }
                    if (g > 255)
                    {
                        g = 255; dc.Green0To255 ^= 1;
                    }
                    if (b < 0)
                    {
                        b = 0; dc.Blue0To255 ^= 1;
                    }
                    if (b > 255)
                    {
                        b = 255; dc.Blue0To255 ^= 1;
                    }
                    c.Red0To255   = (int)r;
                    c.Green0To255 = (int)g;
                    c.Blue0To255  = (int)b;
                }
            }
        // Returns the number of styles
        public int sweep_styles()
        {
            for (;;)
            {
                if (m_scan_y > m_Rasterizer.max_y())
                {
                    return(0);
                }
                int       num_cells = (int)m_Rasterizer.scanline_num_cells(m_scan_y);
                cell_aa[] cells;
                int       cellOffset = 0;
                int       curCellOffset;
                m_Rasterizer.scanline_cells(m_scan_y, out cells, out cellOffset);
                int num_styles = (int)(m_max_style - m_min_style + 2);
                int style_id;
                int styleOffset = 0;

                m_cells.Allocate((int)num_cells * 2, 256); // Each cell can have two styles
                m_ast.Capacity(num_styles, 64);
                m_asm.Allocate((num_styles + 7) >> 3, 8);
                m_asm.zero();

                if (num_cells > 0)
                {
                    // Pre-add zero (for no-fill style, that is, -1).
                    // We need that to ensure that the "-1 style" would go first.
                    m_asm.Array[0] |= 1;
                    m_ast.add(0);
                    m_styles.Array[styleOffset].start_cell = 0;
                    m_styles.Array[styleOffset].num_cells  = 0;
                    m_styles.Array[styleOffset].last_x     = -0x7FFFFFFF;

                    m_sl_start = cells[0].x;
                    m_sl_len   = (int)(cells[num_cells - 1].x - m_sl_start + 1);
                    while (num_cells-- != 0)
                    {
                        curCellOffset = (int)cellOffset++;
                        add_style(cells[curCellOffset].left);
                        add_style(cells[curCellOffset].right);
                    }

                    // Convert the Y-histogram into the array of starting indexes
                    int          i;
                    int          start_cell  = 0;
                    style_info[] stylesArray = m_styles.Array;
                    for (i = 0; i < m_ast.size(); i++)
                    {
                        int IndexToModify = (int)m_ast[i];
                        int v             = stylesArray[IndexToModify].start_cell;
                        stylesArray[IndexToModify].start_cell = start_cell;
                        start_cell += v;
                    }

                    num_cells = (int)m_Rasterizer.scanline_num_cells(m_scan_y);
                    m_Rasterizer.scanline_cells(m_scan_y, out cells, out cellOffset);

                    while (num_cells-- > 0)
                    {
                        curCellOffset = (int)cellOffset++;
                        style_id      = (int)((cells[curCellOffset].left < 0) ? 0 :
                                              cells[curCellOffset].left - m_min_style + 1);

                        styleOffset = (int)style_id;
                        if (cells[curCellOffset].x == stylesArray[styleOffset].last_x)
                        {
                            cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells - 1;
                            unchecked
                            {
                                cells[cellOffset].area  += cells[curCellOffset].area;
                                cells[cellOffset].cover += cells[curCellOffset].cover;
                            }
                        }
                        else
                        {
                            cellOffset                      = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells;
                            cells[cellOffset].x             = cells[curCellOffset].x;
                            cells[cellOffset].area          = cells[curCellOffset].area;
                            cells[cellOffset].cover         = cells[curCellOffset].cover;
                            stylesArray[styleOffset].last_x = cells[curCellOffset].x;
                            stylesArray[styleOffset].num_cells++;
                        }

                        style_id = (int)((cells[curCellOffset].right < 0) ? 0 :
                                         cells[curCellOffset].right - m_min_style + 1);

                        styleOffset = (int)style_id;
                        if (cells[curCellOffset].x == stylesArray[styleOffset].last_x)
                        {
                            cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells - 1;
                            unchecked
                            {
                                cells[cellOffset].area  -= cells[curCellOffset].area;
                                cells[cellOffset].cover -= cells[curCellOffset].cover;
                            }
                        }
                        else
                        {
                            cellOffset                      = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells;
                            cells[cellOffset].x             = cells[curCellOffset].x;
                            cells[cellOffset].area          = -cells[curCellOffset].area;
                            cells[cellOffset].cover         = -cells[curCellOffset].cover;
                            stylesArray[styleOffset].last_x = cells[curCellOffset].x;
                            stylesArray[styleOffset].num_cells++;
                        }
                    }
                }
                if (m_ast.size() > 1)
                {
                    break;
                }
                ++m_scan_y;
            }
            ++m_scan_y;

            if (m_layer_order != layer_order_e.layer_unsorted)
            {
                VectorPOD_RangeAdaptor ra = new VectorPOD_RangeAdaptor(m_ast, 1, m_ast.size() - 1);
                if (m_layer_order == layer_order_e.layer_direct)
                {
                    QuickSort_range_adaptor_uint m_QSorter = new QuickSort_range_adaptor_uint();
                    m_QSorter.Sort(ra);
                    //quick_sort(ra, uint_greater);
                }
                else
                {
                    throw new System.NotImplementedException();
                    //QuickSort_range_adaptor_uint m_QSorter = new QuickSort_range_adaptor_uint();
                    //m_QSorter.Sort(ra);
                    //quick_sort(ra, uint_less);
                }
            }

            return(m_ast.size() - 1);
        }
 public int num_edges()
 {
     return(m_edges.size());
 }
 public int num_triangles()
 {
     return(m_triangles.size());
 }
        public void sort_cells()
        {
            if (m_sorted)
            {
                return;                       //Perform sort only the first time.
            }
            add_curr_cell();
            m_curr_cell.x     = 0x7FFFFFFF;
            m_curr_cell.y     = 0x7FFFFFFF;
            m_curr_cell.cover = 0;
            m_curr_cell.area  = 0;

            if (m_num_used_cells == 0)
            {
                return;
            }

            // Allocate the array of cell pointers
            m_sorted_cells.Allocate(m_num_used_cells);

            // Allocate and zero the Y array
            m_sorted_y.Allocate((int)(m_max_y - m_min_y + 1));
            m_sorted_y.zero();
            cell_aa[]  cells           = m_cells.Array;
            sorted_y[] sortedYData     = m_sorted_y.Array;
            cell_aa[]  sortedCellsData = m_sorted_cells.Array;

            // Create the Y-histogram (count the numbers of cells for each Y)
            for (int i = 0; i < m_num_used_cells; i++)
            {
                int Index = cells[i].y - m_min_y;
                sortedYData[Index].start++;
            }

            // Convert the Y-histogram into the array of starting indexes
            int start       = 0;
            int SortedYSize = m_sorted_y.size();

            for (int i = 0; i < SortedYSize; i++)
            {
                int v = sortedYData[i].start;
                sortedYData[i].start = start;
                start += v;
            }

            // Fill the cell pointer array sorted by Y
            for (int i = 0; i < m_num_used_cells; i++)
            {
                int SortedIndex  = cells[i].y - m_min_y;
                int curr_y_start = sortedYData[SortedIndex].start;
                int curr_y_num   = sortedYData[SortedIndex].num;
                sortedCellsData[curr_y_start + curr_y_num] = cells[i];
                ++sortedYData[SortedIndex].num;
            }

            // Finally arrange the X-arrays
            for (int i = 0; i < SortedYSize; i++)
            {
                if (sortedYData[i].num != 0)
                {
                    m_QSorter.Sort(sortedCellsData, sortedYData[i].start, sortedYData[i].start + sortedYData[i].num - 1);
                }
            }
            m_sorted = true;
        }