Пример #1
0
        public bool FindLeastSquaresMatch(ImageBuffer imageToFind, out Vector2 bestPosition, out double bestLeastSquares, double maxError = double.MaxValue)
        {
            bestPosition     = Vector2.Zero;
            bestLeastSquares = double.MaxValue;

            if (Width >= imageToFind.Width &&
                Height >= imageToFind.Height &&
                BitDepth == imageToFind.BitDepth)
            {
                int    bytesPerPixel          = BitDepth / 8;
                int    aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
                int    bDistanceBetweenPixels = imageToFind.GetBytesBetweenPixelsInclusive();
                byte[] thisBuffer             = GetBuffer();
                byte[] containedBuffer        = imageToFind.GetBuffer();
                for (int matchY = 0; matchY <= Height - imageToFind.Height; matchY++)
                {
                    for (int matchX = 0; matchX <= Width - imageToFind.Width; matchX++)
                    {
                        double currentLeastSquares = 0;

                        for (int imageToFindY = 0; imageToFindY < imageToFind.Height; imageToFindY++)
                        {
                            int thisBufferOffset        = GetBufferOffsetXY(matchX, matchY + imageToFindY);
                            int imageToFindBufferOffset = imageToFind.GetBufferOffsetY(imageToFindY);
                            for (int imageToFindX = 0; imageToFindX < imageToFind.Width; imageToFindX++)
                            {
                                for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
                                {
                                    byte aByte      = thisBuffer[thisBufferOffset + byteIndex];
                                    byte bByte      = containedBuffer[imageToFindBufferOffset + byteIndex];
                                    int  difference = (int)aByte - (int)bByte;
                                    currentLeastSquares += difference * difference;
                                }
                                thisBufferOffset        += aDistanceBetweenPixels;
                                imageToFindBufferOffset += bDistanceBetweenPixels;
                            }
                            if (currentLeastSquares > maxError)
                            {
                                // stop checking we have too much error.
                                imageToFindY = imageToFind.Height;
                            }
                        }
                        if (currentLeastSquares < bestLeastSquares)
                        {
                            bestPosition     = new Vector2(matchX, matchY);
                            bestLeastSquares = currentLeastSquares;
                        }
                    }
                }
            }

            return(bestLeastSquares <= maxError);
        }
		public void pixel_high_res(ImageBuffer sourceImage, RGBA_Bytes[] destBuffer, int destBufferOffset, int x, int y)
		{
			int r, g, b, a;
			r = g = b = a = LineAABasics.line_subpixel_scale * LineAABasics.line_subpixel_scale / 2;

			int weight;
			int x_lr = x >> LineAABasics.line_subpixel_shift;
			int y_lr = y >> LineAABasics.line_subpixel_shift;

			x &= LineAABasics.line_subpixel_mask;
			y &= LineAABasics.line_subpixel_mask;
			int sourceOffset;
			byte[] ptr = sourceImage.GetPixelPointerXY(x_lr, y_lr, out sourceOffset);

			weight = (LineAABasics.line_subpixel_scale - x) *
					 (LineAABasics.line_subpixel_scale - y);
			r += weight * ptr[sourceOffset + ImageBuffer.OrderR];
			g += weight * ptr[sourceOffset + ImageBuffer.OrderG];
			b += weight * ptr[sourceOffset + ImageBuffer.OrderB];
			a += weight * ptr[sourceOffset + ImageBuffer.OrderA];

			sourceOffset += sourceImage.GetBytesBetweenPixelsInclusive();

			weight = x * (LineAABasics.line_subpixel_scale - y);
			r += weight * ptr[sourceOffset + ImageBuffer.OrderR];
			g += weight * ptr[sourceOffset + ImageBuffer.OrderG];
			b += weight * ptr[sourceOffset + ImageBuffer.OrderB];
			a += weight * ptr[sourceOffset + ImageBuffer.OrderA];

			ptr = sourceImage.GetPixelPointerXY(x_lr, y_lr + 1, out sourceOffset);

			weight = (LineAABasics.line_subpixel_scale - x) * y;
			r += weight * ptr[sourceOffset + ImageBuffer.OrderR];
			g += weight * ptr[sourceOffset + ImageBuffer.OrderG];
			b += weight * ptr[sourceOffset + ImageBuffer.OrderB];
			a += weight * ptr[sourceOffset + ImageBuffer.OrderA];

			sourceOffset += sourceImage.GetBytesBetweenPixelsInclusive();

			weight = x * y;
			r += weight * ptr[sourceOffset + ImageBuffer.OrderR];
			g += weight * ptr[sourceOffset + ImageBuffer.OrderG];
			b += weight * ptr[sourceOffset + ImageBuffer.OrderB];
			a += weight * ptr[sourceOffset + ImageBuffer.OrderA];

			destBuffer[destBufferOffset].red = (byte)(r >> LineAABasics.line_subpixel_shift * 2);
			destBuffer[destBufferOffset].green = (byte)(g >> LineAABasics.line_subpixel_shift * 2);
			destBuffer[destBufferOffset].blue = (byte)(b >> LineAABasics.line_subpixel_shift * 2);
			destBuffer[destBufferOffset].alpha = (byte)(a >> LineAABasics.line_subpixel_shift * 2);
		}
Пример #3
0
        public bool Contains(ImageBuffer imageToFind, out int matchX, out int matchY, int maxError = 0)
        {
            matchX = 0;
            matchY = 0;
            if (Width >= imageToFind.Width &&
                Height >= imageToFind.Height &&
                BitDepth == imageToFind.BitDepth)
            {
                int    bytesPerPixel          = BitDepth / 8;
                int    aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
                int    bDistanceBetweenPixels = imageToFind.GetBytesBetweenPixelsInclusive();
                byte[] thisBuffer             = GetBuffer();
                byte[] containedBuffer        = imageToFind.GetBuffer();
                for (matchY = 0; matchY <= Height - imageToFind.Height; matchY++)
                {
                    for (matchX = 0; matchX <= Width - imageToFind.Width; matchX++)
                    {
                        bool foundBadMatch = false;
                        for (int imageToFindY = 0; imageToFindY < imageToFind.Height; imageToFindY++)
                        {
                            int thisBufferOffset        = GetBufferOffsetXY(matchX, matchY + imageToFindY);
                            int imageToFindBufferOffset = imageToFind.GetBufferOffsetY(imageToFindY);
                            for (int imageToFindX = 0; imageToFindX < imageToFind.Width; imageToFindX++)
                            {
                                for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
                                {
                                    byte aByte = thisBuffer[thisBufferOffset + byteIndex];
                                    byte bByte = containedBuffer[imageToFindBufferOffset + byteIndex];
                                    if (aByte < (bByte - maxError) || aByte > (bByte + maxError))
                                    {
                                        foundBadMatch = true;
                                        byteIndex     = bytesPerPixel;
                                        imageToFindX  = imageToFind.Width;
                                        imageToFindY  = imageToFind.Height;
                                    }
                                }
                                thisBufferOffset        += aDistanceBetweenPixels;
                                imageToFindBufferOffset += bDistanceBetweenPixels;
                            }
                        }
                        if (!foundBadMatch)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Пример #4
0
        public bool Equals(ImageBuffer b, int maxError = 0)
        {
            if (Width == b.Width &&
                Height == b.Height &&
                BitDepth == b.BitDepth &&
                StrideInBytes() == b.StrideInBytes() &&
                m_OriginOffset == b.m_OriginOffset)
            {
                int    bytesPerPixel          = BitDepth / 8;
                int    aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
                int    bDistanceBetweenPixels = b.GetBytesBetweenPixelsInclusive();
                byte[] aBuffer = GetBuffer();
                byte[] bBuffer = b.GetBuffer();
                for (int y = 0; y < Height; y++)
                {
                    int aBufferOffset = GetBufferOffsetY(y);
                    int bBufferOffset = b.GetBufferOffsetY(y);
                    for (int x = 0; x < Width; x++)
                    {
                        for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
                        {
                            byte aByte = aBuffer[aBufferOffset + byteIndex];
                            byte bByte = bBuffer[bBufferOffset + byteIndex];
                            if (aByte < (bByte - maxError) || aByte > (bByte + maxError))
                            {
                                return(false);
                            }
                        }
                        aBufferOffset += aDistanceBetweenPixels;
                        bBufferOffset += bDistanceBetweenPixels;
                    }
                }
                return(true);
            }

            return(false);
        }
Пример #5
0
        public ImageBuffer(ImageBuffer sourceImage)
        {
            SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), true);
            int offset = sourceImage.GetBufferOffsetXY(0, 0);

            byte[] buffer    = sourceImage.GetBuffer();
            byte[] newBuffer = new byte[buffer.Length];
            agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
            SetBuffer(newBuffer, offset);
            SetRecieveBlender(sourceImage.GetRecieveBlender());
        }
Пример #6
0
		public bool FindLeastSquaresMatch(ImageBuffer imageToFind, out Vector2 bestPosition, out double bestLeastSquares, double maxError = double.MaxValue)
		{
			bestPosition = Vector2.Zero;
			bestLeastSquares = double.MaxValue;

			if (Width >= imageToFind.Width
				&& Height >= imageToFind.Height
				&& BitDepth == imageToFind.BitDepth)
			{
				int bytesPerPixel = BitDepth / 8;
				int aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
				int bDistanceBetweenPixels = imageToFind.GetBytesBetweenPixelsInclusive();
				byte[] thisBuffer = GetBuffer();
				byte[] containedBuffer = imageToFind.GetBuffer();
				for (int matchY = 0; matchY <= Height - imageToFind.Height; matchY++)
				{
					for (int matchX = 0; matchX <= Width - imageToFind.Width; matchX++)
					{
						double currentLeastSquares = 0;

						for (int imageToFindY = 0; imageToFindY < imageToFind.Height; imageToFindY++)
						{
							int thisBufferOffset = GetBufferOffsetXY(matchX, matchY + imageToFindY);
							int imageToFindBufferOffset = imageToFind.GetBufferOffsetY(imageToFindY);
							for (int imageToFindX = 0; imageToFindX < imageToFind.Width; imageToFindX++)
							{
								for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
								{
									byte aByte = thisBuffer[thisBufferOffset + byteIndex];
									byte bByte = containedBuffer[imageToFindBufferOffset + byteIndex];
									int difference = (int)aByte - (int)bByte;
									currentLeastSquares += difference * difference;
								}
								thisBufferOffset += aDistanceBetweenPixels;
								imageToFindBufferOffset += bDistanceBetweenPixels;
								if (currentLeastSquares > maxError)
								{
									// stop checking we have too much error.
									imageToFindX = imageToFind.Width;
									imageToFindY = imageToFind.Height;
								}
							}
						}

						if (currentLeastSquares < bestLeastSquares)
						{
							bestPosition = new Vector2(matchX, matchY);
							bestLeastSquares = currentLeastSquares;
							if (maxError > currentLeastSquares)
							{
								maxError = currentLeastSquares;
								if (currentLeastSquares == 0)
								{
									return true;
								}
							}
						}
					}
				}
			}

			return bestLeastSquares <= maxError;
		}
Пример #7
0
		public bool Contains(ImageBuffer imageToFind, out int matchX, out int matchY, int maxError = 0)
		{
			matchX = 0;
			matchY = 0;
			if (Width >= imageToFind.Width
				&& Height >= imageToFind.Height
				&& BitDepth == imageToFind.BitDepth)
			{
				int bytesPerPixel = BitDepth / 8;
				int aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
				int bDistanceBetweenPixels = imageToFind.GetBytesBetweenPixelsInclusive();
				byte[] thisBuffer = GetBuffer();
				byte[] containedBuffer = imageToFind.GetBuffer();
				for (matchY = 0; matchY <= Height - imageToFind.Height; matchY++)
				{
					for (matchX = 0; matchX <= Width - imageToFind.Width; matchX++)
					{
						bool foundBadMatch = false;
						for (int imageToFindY = 0; imageToFindY < imageToFind.Height; imageToFindY++)
						{
							int thisBufferOffset = GetBufferOffsetXY(matchX, matchY + imageToFindY);
							int imageToFindBufferOffset = imageToFind.GetBufferOffsetY(imageToFindY);
							for (int imageToFindX = 0; imageToFindX < imageToFind.Width; imageToFindX++)
							{
								for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
								{
									byte aByte = thisBuffer[thisBufferOffset + byteIndex];
									byte bByte = containedBuffer[imageToFindBufferOffset + byteIndex];
									if (aByte < (bByte - maxError) || aByte > (bByte + maxError))
									{
										foundBadMatch = true;
										byteIndex = bytesPerPixel;
										imageToFindX = imageToFind.Width;
										imageToFindY = imageToFind.Height;
									}
								}
								thisBufferOffset += aDistanceBetweenPixels;
								imageToFindBufferOffset += bDistanceBetweenPixels;
							}
						}
						if (!foundBadMatch)
						{
							return true;
						}
					}
				}
			}

			return false;
		}
Пример #8
0
		public bool Equals(ImageBuffer b, int maxError = 0)
		{
			if (Width == b.Width
				&& Height == b.Height
				&& BitDepth == b.BitDepth
				&& StrideInBytes() == b.StrideInBytes()
				&& m_OriginOffset == b.m_OriginOffset)
			{
				int bytesPerPixel = BitDepth / 8;
				int aDistanceBetweenPixels = GetBytesBetweenPixelsInclusive();
				int bDistanceBetweenPixels = b.GetBytesBetweenPixelsInclusive();
				byte[] aBuffer = GetBuffer();
				byte[] bBuffer = b.GetBuffer();
				for (int y = 0; y < Height; y++)
				{
					int aBufferOffset = GetBufferOffsetY(y);
					int bBufferOffset = b.GetBufferOffsetY(y);
					for (int x = 0; x < Width; x++)
					{
						for (int byteIndex = 0; byteIndex < bytesPerPixel; byteIndex++)
						{
							byte aByte = aBuffer[aBufferOffset + byteIndex];
							byte bByte = bBuffer[bBufferOffset + byteIndex];
							if (aByte < (bByte - maxError) || aByte > (bByte + maxError))
							{
								return false;
							}
						}
						aBufferOffset += aDistanceBetweenPixels;
						bBufferOffset += bDistanceBetweenPixels;
					}
				}
				return true;
			}

			return false;
		}
Пример #9
0
		public ImageBuffer(ImageBuffer sourceImage)
		{
			SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), true);
			int offset = sourceImage.GetBufferOffsetXY(0, 0);
			byte[] buffer = sourceImage.GetBuffer();
			byte[] newBuffer = new byte[buffer.Length];
			agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
			SetBuffer(newBuffer, offset);
			SetRecieveBlender(sourceImage.GetRecieveBlender());
		}