public void Enqueue(ref DetectionRange range)
 {
     if (LastIndex == array.Length)
     {
         Array.Resize<DetectionRange>(ref array, 2 * array.Length);
         firstIndex = 0;
     }
     array[LastIndex] = range;
     size++;
 }
        static void LinearDetect(ref int x, ref int y)
        {
            byte[] bitmapBits = result.data;

            //Go left
            int currentX = x;
            int currentIndex = result.GetDataIndex(x, y);
            int height = result.Height;
            int width = result.Width;
            while (true)
            {
                if (!pixelsChecked[currentIndex])
                    currentCluster.Add(currentIndex);
                pixelsChecked[currentIndex] = true;
                currentX--;
                currentIndex -= height;
                if (currentX <= 0 || (pixelsChecked[currentIndex]) || !PixelIsWithinColorRange(ref currentIndex))
                    break;

            }

            int xLeft = currentX + 1;

            //Go right
            currentX = x;
            currentIndex = result.GetDataIndex(x, y);
            while (true)
            {
                if (!pixelsChecked[currentIndex])
                    currentCluster.Add(currentIndex);
                pixelsChecked[currentIndex] = true;

                currentX++;
                currentIndex += height;

                if (currentX >= width || pixelsChecked[currentIndex] || !PixelIsWithinColorRange(ref currentIndex))
                    break;

            }

            int xRight = currentX - 1;

            DetectionRange r = new DetectionRange(xLeft, xRight, y);
            ranges.Enqueue(ref r);
        }
        public DetectionRange Dequeue()
        {
            DetectionRange range = new DetectionRange();
            if (size > 0)
            {
                range = array[firstIndex];
                array[firstIndex] = new DetectionRange();

                firstIndex++;
                size--;
            }
            return range;
        }