context: using System;
  using QRCodeDecoder = HazTech.Util.QR4bit.Codec.QRCodeDecoder;
  using HazTech.Util.QR4bit.Codec.Reader;
  using FinderPatternNotFoundException = HazTech.Util.QR4bit.ExceptionHandler.FinderPatternNotFoundException;
  using InvalidVersionInfoException = HazTech.Util.QR4bit.ExceptionHandler.InvalidVersionInfoException;
  using InvalidVersionException = HazTech.Util.QR4bit.ExceptionHandler.InvalidVersionException;
  using VersionInformationException = HazTech.Util.QR4bit.ExceptionHandler.VersionInformationException;
  using HazTech.Util.QR4bit.Geom;
  using HazTech.Util.QR4bit.Codec.Util;
  
  namespace HazTech.Util.QR4bit.Codec.Reader.Pattern
  {
  	
  	public class FinderPattern
  	{
          public const int UL = 0;
          public const int UR = 1;
          public const int DL = 2;
  
          internal static readonly int[] VersionInfoBit = new int[] { 0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6, 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78, 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683, 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB, 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250, 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B, 0x2542E, 0x26A64, 0x27541, 0x28C69 };
  
          internal static DebugCanvas canvas;
          internal Point[] center;
          internal int version;
          internal int[] sincos;
          internal int[] width;
          internal int[] moduleSize;
  
  		virtual public int Version
  		{
  			get
  			{
  				return version;
  			}
  			
  		}
  		virtual public int SqrtNumModules
  		{
  			get
  			{
  				return 17 + 4 * version;
  			}
  			
  		}
  		
  		public static FinderPattern findFinderPattern(bool[][] image)
  		{
  			Line[] lineAcross = findLineAcross(image);
  			Line[] lineCross = findLineCross(lineAcross);
  			Point[] center = null;
  			try
  			{
  				center = getCenter(lineCross);
  			}
  			catch (FinderPatternNotFoundException e)
  			{
  				throw e;
  			}
  			int[] sincos = getAngle(center);
  			center = sort(center, sincos);
  			int[] width = getWidth(image, center, sincos);
  			// moduleSize for version recognition
  			int[] moduleSize = new int[]{(width[UL] << QRCodeImageReader.DECIMAL_POINT) / 7, (width[UR] << QRCodeImageReader.DECIMAL_POINT) / 7, (width[DL] << QRCodeImageReader.DECIMAL_POINT) / 7};
  			int version = calcRoughVersion(center, width);
  			if (version > 6)
  			{
  				try
  				{
  					version = calcExactVersion(center, sincos, moduleSize, image);
  				}
  				catch (VersionInformationException e)
  				{
                      new Log().Write("VersionInformationException :" + e.ToString());
  					//use rough version data
  					// throw e;
  				}
  			}
  			return new FinderPattern(center, version, sincos, width, moduleSize);
  		}
  		
  		internal FinderPattern(Point[] center, int version, int[] sincos, int[] width, int[] moduleSize)
  		{
  			this.center = center;
  			this.version = version;
  			this.sincos = sincos;
  			this.width = width;
  			this.moduleSize = moduleSize;
  		}
  		
  		public virtual Point[] getCenter()
  		{
  			return center;
  		}
  		
  		public virtual Point getCenter(int position)
  		{
  			if (position >= UL && position <= DL)
  				return center[position];
  			else
  				return null;
  		}
  		
  		public virtual int getWidth(int position)
  		{
  			return width[position];
  		}
  		
  		public virtual int[] getAngle()
  		{
  			return sincos;
  		}
  		
  		public virtual int getModuleSize()
  		{
  			return moduleSize[UL];
  		}
  		public virtual int getModuleSize(int place)
  		{
  			return moduleSize[place];
  		}
  				
  		internal static Line[] findLineAcross(bool[][] image)
  		{
  			int READ_HORIZONTAL = 0;
  			int READ_VERTICAL = 1;
  			
  			int imageWidth = image.Length;
  			int imageHeight = image[0].Length;
  			
  			//int currentX = 0, currentY = 0;
  			Point current = new Point();
  			System.Collections.ArrayList lineAcross = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
  			
  			//buffer contains recent length of modules which has same brightness
  			int[] lengthBuffer = new int[5];
  			int bufferPointer = 0;
  			
  			int direction = READ_HORIZONTAL; //start to read horizontally
  			bool lastElement = QRCodeImageReader.POINT_LIGHT;
  			
  			while (true)
  			{
  				//check points in image
  				bool currentElement = image[current.X][current.Y];
  				if (currentElement == lastElement)
  				{
  					//target point has same brightness with last point
  					lengthBuffer[bufferPointer]++;
  				}
  				else
  				{
  					//target point has different brightness with last point
  					if (currentElement == QRCodeImageReader.POINT_LIGHT)
  					{
  						if (checkPattern(lengthBuffer, bufferPointer))
  						{
  							//detected pattern
  							int x1, y1, x2, y2;
  							if (direction == READ_HORIZONTAL)
  							{
  								//obtain X coordinates of both side of the detected horizontal pattern
  								x1 = current.X;
  								for (int j = 0; j < 5; j++)
  								{
  									x1 -= lengthBuffer[j];
  								}
  								x2 = current.X - 1; //right side is last X coordinate
  								y1 = y2 = current.Y;
  							}
  							else
  							{
  								x1 = x2 = current.X;
  								//obtain Y coordinates of both side of the detected vertical pattern
  								// upper side is sum of length of buffer
  								y1 = current.Y;
  								for (int j = 0; j < 5; j++)
  								{
  									y1 -= lengthBuffer[j];
  								}
  								y2 = current.Y - 1; // bottom side is last Y coordinate
  							}
  							lineAcross.Add(new Line(x1, y1, x2, y2));
  						}
  					}
  					bufferPointer = (bufferPointer + 1) % 5;
  					lengthBuffer[bufferPointer] = 1;
  					lastElement = !lastElement;
  				}
  				
  				// determine if read next, change read direction or terminate this loop
  				if (direction == READ_HORIZONTAL)
  				{
  					if (current.X < imageWidth - 1)
  					{
  						current.translate(1, 0);
  					}
  					else if (current.Y < imageHeight - 1)
  					{
  						current.set_Renamed(0, current.Y + 1);
  						lengthBuffer = new int[5];
  					}
  					else
  					{
  						current.set_Renamed(0, 0); //reset target point
  						lengthBuffer = new int[5];
  						direction = READ_VERTICAL; //start to read vertically
  					}
  				}
  				else
  				{
  					//reading vertically
  					if (current.Y < imageHeight - 1)
  						current.translate(0, 1);
  					else if (current.X < imageWidth - 1)
  					{
  						current.set_Renamed(current.X + 1, 0);
  						lengthBuffer = new int[5];
  					}
  					else
  					{
  						break;
  					}
  				}
  			}
  			
  			Line[] foundLines = new Line[lineAcross.Count];
  			
  			for (int i = 0; i < foundLines.Length; i++)
  
gt: 				foundLines[i] = (Line) lineAcross[i];