Ejemplo n.º 1
0
        public override void generate(RGBA_Bytes[] span, int spanIndex, int x, int y, int len)
        {
            ImageBuffer SourceRenderingBuffer = (ImageBuffer)GetImageBufferAccessor().SourceImage;

            if (SourceRenderingBuffer.BitDepth != 24)
            {
                throw new NotSupportedException("The source is expected to be 32 bit.");
            }
            ISpanInterpolator spanInterpolator = interpolator();

            spanInterpolator.begin(x + filter_dx_dbl(), y + filter_dy_dbl(), len);
            int offset;

            byte[] fg_ptr = SourceRenderingBuffer.GetBuffer(out offset);
            do
            {
                int x_hr;
                int y_hr;
                spanInterpolator.coordinates(out x_hr, out y_hr);
                int x_lr = x_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
                int y_lr = y_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
                int bufferIndex;
                bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);
                RGBA_Bytes color;
                color.blue      = fg_ptr[bufferIndex++];
                color.green     = fg_ptr[bufferIndex++];
                color.red       = fg_ptr[bufferIndex++];
                color.alpha     = 255;
                span[spanIndex] = color;
                spanIndex++;
                spanInterpolator.Next();
            } while (--len != 0);
        }
Ejemplo n.º 2
0
        public void Update(double NumSecondsPassed)
        {
            foreach (SequenceEntity aSequenceEntity in sequenceEntityList)
            {
                aSequenceEntity.Update(NumSecondsPassed);
            }

            foreach (Player aPlayer in playerList)
            {
                aPlayer.Update(NumSecondsPassed);
                {
                    int      offset;
                    byte[]   buffer      = levelMap.GetBuffer(out offset);
                    Vector2D newPosition = aPlayer.Position;

                    int xOnMap = ((int)(newPosition.x + .5)) / 16;
                    int yOnMap = ((int)(newPosition.y + .5)) / 16;
                    offset = levelMap.GetBufferOffsetXY(xOnMap, yOnMap);
                    if (aPlayer.hasKey && buffer[offset] == 43)
                    {
                        aPlayer.m_Score++;
                        aPlayer.hasKey        = false;
                        aPlayer.entityHolding = null;
                        key.Position          = keyStart;
                    }
                }
            }

            RemoveDeadStuff(sequenceEntityList);
        }
Ejemplo n.º 3
0
        public static Bitmap ConvertImageToBitmap(ImageBuffer sourceImage)
        {
            var bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            int destIndex = 0;

            unsafe
            {
                byte[] sourceBuffer    = sourceImage.GetBuffer();
                byte * pDestBuffer     = (byte *)bitmapData.Scan0;
                int    scanlinePadding = bitmapData.Stride - bitmapData.Width * 4;
                for (int y = 0; y < sourceImage.Height; y++)
                {
                    int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                    for (int x = 0; x < sourceImage.Width; x++)
                    {
                        pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                        pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                        pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                        pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                    }

                    destIndex += scanlinePadding;
                }
            }

            bitmap.UnlockBits(bitmapData);

            return(bitmap);
        }
Ejemplo n.º 4
0
        void PlaceObjectsOnLevel()
        {
            ImageBuffer levelMap = LevelMap;
            int         offset;

            byte[] buffer = levelMap.GetBuffer(out offset);

            for (int y = 0; y < levelMap.Height(); y++)
            {
                for (int x = 0; x < levelMap.Width(); x++)
                {
                    offset = levelMap.GetBufferOffsetXY(x, y);
                    switch (buffer[offset])
                    {
                    case 220:
                        // this is the sword.
                        sword          = new Sword();
                        sword.Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    case 170:
                        // this is the key.
                        key          = new Key();
                        key.Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        keyStart     = key.Position;
                        break;

                    case 2:
                        // this is the red player.
                        playerList[0].Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    case 35:
                        // this is the green player.
                        playerList[1].Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    case 251:
                        // this is the blue player.
                        playerList[2].Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    case 5:
                        // this is the yellow player.
                        playerList[3].Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    case 248:
                        // this is the shield.
                        shield          = new Shield();
                        shield.Position = new Vector2D(x * 16 + 8, y * 16 + 8);
                        break;

                    default:
                        break;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public static void DoErode3x3MinValue(ImageBuffer source, ImageBuffer dest)
        {
            if (source.BitDepth != 32 || dest.BitDepth != 32)
            {
                throw new NotImplementedException("We only work with 32 bit at the moment.");
            }

            if (source.Width != dest.Width || source.Height != dest.Height)
            {
                throw new NotImplementedException("Source and Dest have to be the same size");
            }

            int height = source.Height;
            int width  = source.Width;
            int sourceStrideInBytes = source.StrideInBytes();
            int destStrideInBytes   = dest.StrideInBytes();

            byte[] sourceBuffer = source.GetBuffer();
            byte[] destBuffer   = dest.GetBuffer();

            // This can be made much faster by holding the buffer pointer and offsets better // LBB 2013 06 09
            for (int testY = 1; testY < height - 1; testY++)
            {
                for (int testX = 1; testX < width - 1; testX++)
                {
                    Color minColor     = Color.White;
                    int   sourceOffset = source.GetBufferOffsetXY(testX, testY - 1);

                    // x-1, y-1
                    //minColor = MinColor(sourceBuffer, minColor, sourceOffset - 4);
                    // x0, y-1
                    minColor = MinColor(sourceBuffer, minColor, sourceOffset + 0);
                    // x1, y-1
                    //minColor = MinColor(sourceBuffer, minColor, sourceOffset + 4);

                    // x-1, y0
                    minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes - 4);
                    // x0, y0
                    minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes + 0);
                    // x+1, y0
                    minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes + 4);

                    // x-1, y+1
                    //minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes * 2 - 4);
                    // x0, y+1
                    minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes * 2 + 0);
                    // x+1, y+1
                    //minColor = MinColor(sourceBuffer, minColor, sourceOffset + sourceStrideInBytes * 2 + 4);

                    int destOffset = dest.GetBufferOffsetXY(testX, testY);
                    destBuffer[destOffset + 2] = minColor.red;
                    destBuffer[destOffset + 1] = minColor.green;
                    destBuffer[destOffset + 0] = minColor.blue;
                    destBuffer[destOffset + 3] = 255;
                }
            }
        }
Ejemplo n.º 6
0
        public static void DoDilate3x3MaxValue(ImageBuffer source, ImageBuffer dest)
        {
            if (source.BitDepth != 32 || dest.BitDepth != 32)
            {
                throw new NotImplementedException("We only work with 32 bit at the moment.");
            }

            if (source.Width != dest.Width || source.Height != dest.Height)
            {
                throw new NotImplementedException("Source and Dest have to be the same size");
            }

            int height = source.Height;
            int width  = source.Width;
            int sourceStrideInBytes = source.StrideInBytes();
            int destStrideInBytes   = dest.StrideInBytes();

            byte[] sourceBuffer = source.GetBuffer();
            byte[] destBuffer   = dest.GetBuffer();

            for (int testY = 1; testY < height - 1; testY++)
            {
                for (int testX = 1; testX < width - 1; testX++)
                {
                    RGBA_Bytes maxColor     = RGBA_Bytes.Black;
                    int        sourceOffset = source.GetBufferOffsetXY(testX, testY - 1);

                    // x-1, y-1
                    //maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset - 4);
                    // x0, y-1
                    maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + 0);
                    // x1, y-1
                    //maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + 4);

                    // x-1, y0
                    maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes - 4);
                    // x0, y0
                    maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes + 0);
                    // x+1, y0
                    maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes + 4);

                    // x-1, y+1
                    //maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes * 2 - 4);
                    // x0, y+1
                    maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes * 2 + 0);
                    // x+1, y+1
                    //maxColor = MaxColor(sourceBuffer, maxColor, sourceOffset + sourceStrideInBytes * 2 + 4);

                    int destOffset = dest.GetBufferOffsetXY(testX, testY);
                    destBuffer[destOffset + 2] = maxColor.red;
                    destBuffer[destOffset + 1] = maxColor.green;
                    destBuffer[destOffset + 0] = maxColor.blue;
                    destBuffer[destOffset + 3] = 255;
                }
            }
        }
Ejemplo n.º 7
0
        void LinearFill(int x, int y)
        {
            int bytesPerPixel = destImage.GetBytesBetweenPixelsInclusive();
            int imageWidth    = destImage.Width;

            int leftFillX    = x;
            int bufferOffset = destImage.GetBufferOffsetXY(x, y);
            int pixelOffset  = (imageWidth * y) + x;

            while (true)
            {
                fillRule.SetPixel(destBuffer, bufferOffset);
                pixelsChecked[pixelOffset] = true;
                leftFillX--;
                pixelOffset--;
                bufferOffset -= bytesPerPixel;
                if (leftFillX <= 0 || (pixelsChecked[pixelOffset]) || !fillRule.CheckPixel(destBuffer, bufferOffset))
                {
                    break;
                }
            }
            leftFillX++;

            int rightFillX = x;

            bufferOffset = destImage.GetBufferOffsetXY(x, y);
            pixelOffset  = (imageWidth * y) + x;
            while (true)
            {
                fillRule.SetPixel(destBuffer, bufferOffset);
                pixelsChecked[pixelOffset] = true;
                rightFillX++;
                pixelOffset++;
                bufferOffset += bytesPerPixel;
                if (rightFillX >= imageWidth || pixelsChecked[pixelOffset] || !fillRule.CheckPixel(destBuffer, bufferOffset))
                {
                    break;
                }
            }
            rightFillX--;

            ranges.Enqueue(new Range(leftFillX, rightFillX, y));
        }
Ejemplo n.º 8
0
        public override void generate(RGBA_Bytes[] span, int spanIndex, int x, int y, int len)
        {
            ImageBuffer SourceRenderingBuffer       = (ImageBuffer)GetImageBufferAccessor().SourceImage;
            int         bytesBetweenPixelsInclusive = SourceRenderingBuffer.GetBytesBetweenPixelsInclusive();

            if (SourceRenderingBuffer.BitDepth != 8)
            {
                throw new NotSupportedException("The source is expected to be 32 bit.");
            }
            ISpanInterpolator spanInterpolator = interpolator();

            spanInterpolator.begin(x + filter_dx_dbl(), y + filter_dy_dbl(), len);
            int x_hr;
            int y_hr;

            spanInterpolator.coordinates(out x_hr, out y_hr);
            int x_lr = x_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
            int y_lr = y_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
            int bufferIndex;

            bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);

            byte[] fg_ptr = SourceRenderingBuffer.GetBuffer();
#if USE_UNSAFE_CODE
            unsafe
            {
                fixed(byte *pSource = fg_ptr)
                {
                    do
                    {
                        span[spanIndex].red   = pSource[bufferIndex];
                        span[spanIndex].green = pSource[bufferIndex];
                        span[spanIndex].blue  = pSource[bufferIndex];
                        span[spanIndex].alpha = 255;
                        spanIndex++;
                        bufferIndex += bytesBetweenPixelsInclusive;
                    } while (--len != 0);
                }
            }
#else
            do
            {
                throw new Exception("this code is for 32 bit");
                RGBA_Bytes color;
                color.blue        = fg_ptr[bufferIndex++];
                color.green       = fg_ptr[bufferIndex++];
                color.red         = fg_ptr[bufferIndex++];
                color.alpha       = fg_ptr[bufferIndex++];
                span[spanIndex++] = color;
            } while (--len != 0);
#endif
        }
Ejemplo n.º 9
0
        public override void generate(Color[] span, int spanIndex, int x, int y, int len)
        {
            ImageBuffer SourceRenderingBuffer = (ImageBuffer)GetImageBufferAccessor().SourceImage;

            if (SourceRenderingBuffer.BitDepth != 24)
            {
                throw new NotSupportedException("The source is expected to be 32 bit.");
            }
            ISpanInterpolator spanInterpolator = interpolator();

            spanInterpolator.begin(x + filter_dx_dbl(), y + filter_dy_dbl(), len);
            int x_hr;
            int y_hr;

            spanInterpolator.coordinates(out x_hr, out y_hr);
            int x_lr = x_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
            int y_lr = y_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
            int bufferIndex;

            bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);

            byte[] fg_ptr = SourceRenderingBuffer.GetBuffer();
#if USE_UNSAFE_CODE
            unsafe
            {
                fixed(byte *pSource = fg_ptr)
                {
                    do
                    {
                        span[spanIndex++] = *(RGBA_Bytes *)&(pSource[bufferIndex]);
                        bufferIndex      += 4;
                    } while (--len != 0);
                }
            }
#else
            Color color = Color.White;
            do
            {
                color.blue        = fg_ptr[bufferIndex++];
                color.green       = fg_ptr[bufferIndex++];
                color.red         = fg_ptr[bufferIndex++];
                span[spanIndex++] = color;
            } while (--len != 0);
#endif
        }
Ejemplo n.º 10
0
        private bool IsCollision(Vector2D newPosition)
        {
            int offset;

            byte[] buffer = levelMap.GetBuffer(out offset);

            int xOnMap = ((int)(newPosition.x + .5)) / 16;
            int yOnMap = ((int)(newPosition.y + .5)) / 16;

            offset = levelMap.GetBufferOffsetXY(xOnMap, yOnMap);
            if (buffer[offset] == 0 ||
                !hasKey && buffer[offset] == 1)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 11
0
        public static void DoDilate3x3Binary(ImageBuffer source, ImageBuffer dest, int threshold)
        {
            if (source.BitDepth != 32 || dest.BitDepth != 32)
            {
                throw new NotImplementedException("We only work with 32 bit at the moment.");
            }

            if (source.Width != dest.Width || source.Height != dest.Height)
            {
                throw new NotImplementedException("Source and Dest have to be the same size");
            }

            int height = source.Height;
            int width  = source.Width;
            int sourceStrideInBytes = source.StrideInBytes();
            int destStrideInBytes   = dest.StrideInBytes();

            byte[] sourceBuffer = source.GetBuffer();
            byte[] destBuffer   = dest.GetBuffer();

            for (int testY = 1; testY < height - 1; testY++)
            {
                for (int testX = 1; testX < width - 1; testX++)
                {
                    for (int sourceY = -1; sourceY <= 1; sourceY++)
                    {
                        for (int sourceX = -1; sourceX <= 1; sourceX++)
                        {
                            int sourceOffset = source.GetBufferOffsetXY(testX + sourceX, testY + sourceY);
                            if (sourceBuffer[sourceOffset] > threshold)
                            {
                                int destOffset = dest.GetBufferOffsetXY(testX, testY);
                                destBuffer[destOffset++] = 255;
                                destBuffer[destOffset++] = 255;
                                destBuffer[destOffset++] = 255;
                                destBuffer[destOffset++] = 255;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        // Create
        //--------------------------------------------------------------------
        public void create(IImageByte src)
        {
            // we are going to create a dilated image for filtering
            // we add m_dilation pixels to every side of the image and then copy the image in the x
            // direction into each end so that we can sample into this image to get filtering on x repeating
            // if the original image look like this
            //
            // 123456
            //
            // the new image would look like this
            //
            // 0000000000
            // 0000000000
            // 5612345612
            // 0000000000
            // 0000000000

            m_height          = (int)agg_basics.uceil(src.Height);
            m_width           = (int)agg_basics.uceil(src.Width);
            m_width_hr        = (int)agg_basics.uround(src.Width * LineAABasics.line_subpixel_scale);
            m_half_height_hr  = (int)agg_basics.uround(src.Height * LineAABasics.line_subpixel_scale / 2);
            m_offset_y_hr     = m_dilation_hr + m_half_height_hr - LineAABasics.line_subpixel_scale / 2;
            m_half_height_hr += LineAABasics.line_subpixel_scale / 2;

            int bufferWidth    = m_width + m_dilation * 2;
            int bufferHeight   = m_height + m_dilation * 2;
            int bytesPerPixel  = src.BitDepth / 8;
            int NewSizeInBytes = bufferWidth * bufferHeight * bytesPerPixel;

            if (m_DataSizeInBytes < NewSizeInBytes)
            {
                m_DataSizeInBytes = NewSizeInBytes;
                m_data            = new byte[m_DataSizeInBytes];
            }

            m_buf.AttachBuffer(m_data, 0, bufferWidth, bufferHeight, bufferWidth * bytesPerPixel, src.BitDepth, bytesPerPixel);
            byte[] destBuffer   = m_buf.GetBuffer();
            byte[] sourceBuffer = src.GetBuffer();

            // copy the image into the middle of the dest
            for (int y = 0; y < m_height; y++)
            {
                for (int x = 0; x < m_width; x++)
                {
                    int sourceOffset = src.GetBufferOffsetXY(x, y);
                    int destOffset   = m_buf.GetBufferOffsetXY(m_dilation, y + m_dilation);
                    for (int channel = 0; channel < bytesPerPixel; channel++)
                    {
                        destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
                    }
                }
            }

            // copy the first two pixels form the end into the beginning and from the beginning into the end
            for (int y = 0; y < m_height; y++)
            {
                int s1Offset = src.GetBufferOffsetXY(0, y);
                int d1Offset = m_buf.GetBufferOffsetXY(m_dilation + m_width, y);

                int s2Offset = src.GetBufferOffsetXY(m_width - m_dilation, y);
                int d2Offset = m_buf.GetBufferOffsetXY(0, y);

                for (int x = 0; x < m_dilation; x++)
                {
                    for (int channel = 0; channel < bytesPerPixel; channel++)
                    {
                        destBuffer[d1Offset++] = sourceBuffer[s1Offset++];
                        destBuffer[d2Offset++] = sourceBuffer[s2Offset++];
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public static bool ConvertBitmapToImage(ImageBuffer destImage, Bitmap bitmap)
        {
            if (bitmap != null)
            {
                switch (bitmap.PixelFormat)
                {
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                {
                    destImage.Allocate(bitmap.Width, bitmap.Height, bitmap.Width * 4, 32);
                    if (destImage.GetRecieveBlender() == null)
                    {
                        destImage.SetRecieveBlender(new BlenderBGRA());
                    }

                    BitmapData bitmapData  = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                    int        sourceIndex = 0;
                    int        destIndex   = 0;
                    unsafe
                    {
                        byte[] destBuffer    = destImage.GetBuffer(out int offset);
                        byte * pSourceBuffer = (byte *)bitmapData.Scan0;
                        for (int y = 0; y < destImage.Height; y++)
                        {
                            destIndex = destImage.GetBufferOffsetXY(0, destImage.Height - 1 - y);
                            for (int x = 0; x < destImage.Width; x++)
                            {
#if true
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
#else
                                Color notPreMultiplied = new Color(pSourceBuffer[sourceIndex + 0], pSourceBuffer[sourceIndex + 1], pSourceBuffer[sourceIndex + 2], pSourceBuffer[sourceIndex + 3]);
                                sourceIndex += 4;
                                Color preMultiplied = notPreMultiplied.ToColorF().premultiply().ToColor();
                                destBuffer[destIndex++] = preMultiplied.blue;
                                destBuffer[destIndex++] = preMultiplied.green;
                                destBuffer[destIndex++] = preMultiplied.red;
                                destBuffer[destIndex++] = preMultiplied.alpha;
#endif
                            }
                        }
                    }

                    bitmap.UnlockBits(bitmapData);

                    return(true);
                }

                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                {
                    destImage.Allocate(bitmap.Width, bitmap.Height, bitmap.Width * 4, 32);
                    if (destImage.GetRecieveBlender() == null)
                    {
                        destImage.SetRecieveBlender(new BlenderBGRA());
                    }

                    BitmapData bitmapData  = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                    int        sourceIndex = 0;
                    int        destIndex   = 0;
                    unsafe
                    {
                        byte[] destBuffer    = destImage.GetBuffer(out int offset);
                        byte * pSourceBuffer = (byte *)bitmapData.Scan0;
                        for (int y = 0; y < destImage.Height; y++)
                        {
                            sourceIndex = y * bitmapData.Stride;
                            destIndex   = destImage.GetBufferOffsetXY(0, destImage.Height - 1 - y);
                            for (int x = 0; x < destImage.Width; x++)
                            {
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = pSourceBuffer[sourceIndex++];
                                destBuffer[destIndex++] = 255;
                            }
                        }
                    }

                    bitmap.UnlockBits(bitmapData);
                    return(true);
                }

                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                {
                    Copy8BitDataToImage(destImage, bitmap);
                    return(true);
                }

                default:
                    // let this code fall through and return false
                    break;
                }
            }

            return(false);
        }
Ejemplo n.º 14
0
        void CalculateSumResponseAtAllPixels(ImageBuffer imageBuffer, int totalResponseThreshold)
        {
            validResponsesBotomToTopList.Clear();

            if (imageBuffer.GetBytesBetweenPixelsInclusive() != 1)
            {
                throw new NotImplementedException("We only process gray scale images that are packed");
            }

            int width  = imageBuffer.Width;
            int height = imageBuffer.Height;

            byte[] buffer = imageBuffer.GetBuffer();
            for (int y = trimPixels; y < height - trimPixels; y++)
            {
                int byteOffset = imageBuffer.GetBufferOffsetXY(trimPixels, y);
                ValidResponseData[] totalResponseRow = allResponsesGrid.GetRow(y);
                for (int x = trimPixels; x < width - trimPixels; x++)
                {
                    int sumResponse = 0;
                    for (int angleToCheckIndex = 0; angleToCheckIndex < 4; angleToCheckIndex++)
                    {
                        int sum =
                            (buffer[byteOffset + byteOffsetToPixel[angleToCheckIndex]] + buffer[byteOffset + byteOffsetToPixel[angleToCheckIndex + 8]])
                            -
                            (buffer[byteOffset + byteOffsetToPixel[angleToCheckIndex + 4]] + buffer[byteOffset + byteOffsetToPixel[angleToCheckIndex + 12]]);
                        int absSum = Math.Abs(sum);
                        sumResponse += absSum;
                    }
                    int neighborMeanTotal = 0;
                    int diffResponse      = 0;
                    for (int diffCheck = 0; diffCheck < 8; diffCheck++)
                    {
                        int testValue     = buffer[byteOffset + byteOffsetToPixel[diffCheck]];
                        int oppositeValue = buffer[byteOffset + byteOffsetToPixel[diffCheck + 8]];
                        diffResponse      += Math.Abs(testValue - oppositeValue);
                        neighborMeanTotal += testValue + oppositeValue;
                    }
                    int neighborMean = (neighborMeanTotal + 8) / 16;

                    int centerMeanTotal = buffer[byteOffset - 1] + buffer[byteOffset + 1] + buffer[byteOffset - width] + buffer[byteOffset - width];
                    int centerMean      = (centerMeanTotal + 2) / 4;
                    int absMeanResponse = Math.Abs(neighborMean - centerMean);

                    ValidResponseData newResponse = new ValidResponseData();
                    int totalResponse             = sumResponse - diffResponse - absMeanResponse;
                    if (totalResponse >= totalResponseThreshold)
                    {
                        newResponse.totalResponse = totalResponse;
                        newResponse.position      = new Vector2(x, y);
                        newResponse.originalIndex = validResponsesBotomToTopList.Count;
                        // we are scanning pixels bottom to top so they go in the list bottom to top
                        validResponsesBotomToTopList.Add(newResponse);
                    }

                    totalResponseRow[x] = newResponse;

                    byteOffset++;
                }
            }
        }
Ejemplo n.º 15
0
        public override void OnDraw(RendererBase rendererToDrawWith)
        {
            ImageBuffer levelMap = playfield.LevelMap;
            int         offset;

            byte[] buffer = levelMap.GetBuffer(out offset);

            if (!haveDrawnWalls)
            {
                RendererBase backgroundRenderer = BackgroundImage.NewRenderer();
                rect_i       boundsI            = BackgroundImage.GetBoundingRect();
                rect_d       bounds             = new rect_d(boundsI.Left, boundsI.Bottom, boundsI.Right, boundsI.Top);
                backgroundRenderer.SetClippingRect(bounds);
                ImageSequence wallTileSequence = (ImageSequence)DataAssetCache.Instance.GetAsset(typeof(ImageSequence), "WallTile");
                for (int y = 0; y < levelMap.Height(); y++)
                {
                    for (int x = 0; x < levelMap.Width(); x++)
                    {
                        if (buffer[levelMap.GetBufferOffsetXY(x, y)] == 0)
                        {
                            int index = 0;
                            // what type of wall
                            if (x < levelMap.Width() - 1 &&
                                buffer[levelMap.GetBufferOffsetXY(x + 1, y + 0)] == 0)
                            {
                                index |= 8;
                            }

                            if (y < levelMap.Height() - 1 &&
                                buffer[levelMap.GetBufferOffsetXY(x + 0, y + 1)] == 0)
                            {
                                index |= 4;
                            }

                            if (x > 0 &&
                                buffer[levelMap.GetBufferOffsetXY(x - 1, y + 0)] == 0)
                            {
                                index |= 2;
                            }

                            if (y > 0 &&
                                buffer[levelMap.GetBufferOffsetXY(x + 0, y - 1)] == 0)
                            {
                                index |= 1;
                            }

                            backgroundRenderer.Render(wallTileSequence.GetImageByIndex(index), x * 16, y * 16);
                        }
                    }
                }
                haveDrawnWalls = true;
            }

            //for (int i = 0; i < 1; i++)
            for (int i = 0; i < numPlayers; i++)
            {
                playerViews[i].SetRendererPreDraw(BackgroundImage, rendererToDrawWith, playfield.PlayerList[i]);

                rendererToDrawWith.Render(BackgroundImage, 0, 0);

                foreach (SequenceEntity aSequenceEntity in playfield.SequenceEntityList)
                {
                    aSequenceEntity.Draw(rendererToDrawWith);
                }

                foreach (Player aPlayer in playfield.PlayerList)
                {
                    aPlayer.Draw(rendererToDrawWith);
                }

                playfield.sword.Draw(rendererToDrawWith);
                playfield.key.Draw(rendererToDrawWith);
                playfield.shield.Draw(rendererToDrawWith);

                playerViews[i].SetRendererPostDraw(rendererToDrawWith);
            }

            ImageSequence hud = (ImageSequence)DataAssetCache.Instance.GetAsset(typeof(ImageSequence), (playfield.PlayerList.Count).ToString() + "PlayerHUD");

            rendererToDrawWith.Render(hud.GetImageByIndex(0), 400, 300);

            foreach (Player aPlayer in playfield.PlayerList)
            {
                aPlayer.DrawScore(rendererToDrawWith);
            }

            rendererToDrawWith.Line(0.5, 300.5, 800.5, 300.5, new RGBA_Bytes(255, 20, 20));
            rendererToDrawWith.Line(400.5, 0.5, 400.5, 600.5, new RGBA_Bytes(255, 20, 20));

            base.OnDraw(rendererToDrawWith);
        }
Ejemplo n.º 16
0
        public static void DoErode3x3Binary(ImageBuffer source, ImageBuffer dest, int threshold)
        {
            if (source.Width != dest.Width ||
                source.Height != dest.Height ||
                source.StrideInBytes() != dest.StrideInBytes())
            {
                throw new NotImplementedException("Source and Dest have to be the same size");
            }

            switch (source.BitDepth)
            {
            case 8:
            {
                int    height        = source.Height;
                int    width         = source.Width;
                int    strideInBytes = source.StrideInBytes();
                byte[] sourceBuffer  = source.GetBuffer();
                byte[] destBuffer    = dest.GetBuffer();

                System.Threading.Tasks.Parallel.For(1, height, y =>
                                                    //for (int y = 1; y < height - 1; y++)
                    {
                        int rowOffset = source.GetBufferOffsetY(y);
                        for (int x = 1; x < width - 1; x++)
                        {
                            int bufferOffset = rowOffset + x;
                            // make sure it is set to 1 if we don't change it to 0
                            destBuffer[bufferOffset] = 255;

                            // do the upper left
                            int checkOffset = bufferOffset - strideInBytes - 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }
                            // do the upper center
                            checkOffset = bufferOffset - strideInBytes + 0;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the upper right
                            checkOffset = bufferOffset - strideInBytes + 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the center left
                            checkOffset = bufferOffset + strideInBytes - 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the center center
                            checkOffset = bufferOffset + strideInBytes + 0;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the lower right
                            checkOffset = bufferOffset + strideInBytes + 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the lower left
                            checkOffset = bufferOffset + strideInBytes - 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the lower center
                            checkOffset = bufferOffset + strideInBytes + 0;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }

                            // do the lower right
                            checkOffset = bufferOffset + strideInBytes + 1;
                            if (sourceBuffer[checkOffset] < threshold)
                            {
                                destBuffer[bufferOffset] = 0;
                                continue;
                            }
                        }
                    });
            }
            break;

            case 32:
            {
                int    height = source.Height;
                int    width  = source.Width;
                int    sourceStrideInBytes = source.StrideInBytes();
                int    destStrideInBytes   = dest.StrideInBytes();
                byte[] sourceBuffer        = source.GetBuffer();
                byte[] destBuffer          = dest.GetBuffer();

                for (int destY = 1; destY < height - 1; destY++)
                {
                    for (int destX = 1; destX < width - 1; destX++)
                    {
                        for (int sourceY = -1; sourceY <= 1; sourceY++)
                        {
                            for (int sourceX = -1; sourceX <= 1; sourceX++)
                            {
                                int sourceOffset = source.GetBufferOffsetXY(destX + sourceX, destY + sourceY);
                                if (sourceBuffer[sourceOffset] < threshold)
                                {
                                    int destOffset = dest.GetBufferOffsetXY(destX, destY);
                                    destBuffer[destOffset++] = 0;
                                    destBuffer[destOffset++] = 0;
                                    destBuffer[destOffset++] = 0;
                                    destBuffer[destOffset++] = 255;
                                }
                            }
                        }
                    }
                }
            }
            break;

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 17
0
        public void Fill(ImageBuffer bufferToFillOn, int x, int y)
        {
            unchecked             // this way we can overflow the uint on negative and get a big number
            {
                if ((uint)x > bufferToFillOn.Width || (uint)y > bufferToFillOn.Height)
                {
                    return;
                }
            }

            destImage   = bufferToFillOn;
            imageStride = destImage.StrideInBytes();
            destBuffer  = destImage.GetBuffer();
            int imageWidth  = destImage.Width;
            int imageHeight = destImage.Height;

            pixelsChecked = new bool[destImage.Width * destImage.Height];

            int startColorBufferOffset = destImage.GetBufferOffsetXY(x, y);

            fillRule.SetStartColor(new Color(destImage.GetBuffer()[startColorBufferOffset + 2], destImage.GetBuffer()[startColorBufferOffset + 1], destImage.GetBuffer()[startColorBufferOffset]));

            LinearFill(x, y);

            while (ranges.Count > 0)
            {
                Range range = ranges.Dequeue();

                int downY           = range.y - 1;
                int upY             = range.y + 1;
                int downPixelOffset = (imageWidth * (range.y - 1)) + range.startX;
                int upPixelOffset   = (imageWidth * (range.y + 1)) + range.startX;
                for (int rangeX = range.startX; rangeX <= range.endX; rangeX++)
                {
                    if (range.y > 0)
                    {
                        if (!pixelsChecked[downPixelOffset])
                        {
                            int bufferOffset = destImage.GetBufferOffsetXY(rangeX, downY);
                            if (fillRule.CheckPixel(destBuffer, bufferOffset))
                            {
                                LinearFill(rangeX, downY);
                            }
                        }
                    }

                    if (range.y < (imageHeight - 1))
                    {
                        if (!pixelsChecked[upPixelOffset])
                        {
                            int bufferOffset = destImage.GetBufferOffsetXY(rangeX, upY);
                            if (fillRule.CheckPixel(destBuffer, bufferOffset))
                            {
                                LinearFill(rangeX, upY);
                            }
                        }
                    }
                    upPixelOffset++;
                    downPixelOffset++;
                }
            }
        }
Ejemplo n.º 18
0
        public override void generate(RGBA_Bytes[] span, int spanIndex, int x, int y, int len)
        {
            base.interpolator().begin(x + base.filter_dx_dbl(), y + base.filter_dy_dbl(), len);

            ImageBuffer       SourceRenderingBuffer = (ImageBuffer)base.GetImageBufferAccessor().SourceImage;
            ISpanInterpolator spanInterpolator      = base.interpolator();
            int bufferIndex;

            byte[] fg_ptr = SourceRenderingBuffer.GetBuffer(out bufferIndex);

            unchecked
            {
                do
                {
                    int tempR;
                    int tempG;
                    int tempB;

                    int x_hr;
                    int y_hr;

                    spanInterpolator.coordinates(out x_hr, out y_hr);

                    x_hr -= base.filter_dx_int();
                    y_hr -= base.filter_dy_int();

                    int x_lr = x_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
                    int y_lr = y_hr >> (int)image_subpixel_scale_e.image_subpixel_shift;
                    int weight;

                    tempR         =
                        tempG     =
                            tempB = (int)image_subpixel_scale_e.image_subpixel_scale * (int)image_subpixel_scale_e.image_subpixel_scale / 2;

                    x_hr &= (int)image_subpixel_scale_e.image_subpixel_mask;
                    y_hr &= (int)image_subpixel_scale_e.image_subpixel_mask;

                    bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);

                    weight = (((int)image_subpixel_scale_e.image_subpixel_scale - x_hr) *
                              ((int)image_subpixel_scale_e.image_subpixel_scale - y_hr));
                    tempR       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderR];
                    tempG       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderG];
                    tempB       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderB];
                    bufferIndex += 3;

                    weight = (x_hr * ((int)image_subpixel_scale_e.image_subpixel_scale - y_hr));
                    tempR += weight * fg_ptr[bufferIndex + ImageBuffer.OrderR];
                    tempG += weight * fg_ptr[bufferIndex + ImageBuffer.OrderG];
                    tempB += weight * fg_ptr[bufferIndex + ImageBuffer.OrderB];

                    y_lr++;
                    bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);

                    weight       = (((int)image_subpixel_scale_e.image_subpixel_scale - x_hr) * y_hr);
                    tempR       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderR];
                    tempG       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderG];
                    tempB       += weight * fg_ptr[bufferIndex + ImageBuffer.OrderB];
                    bufferIndex += 3;

                    weight = (x_hr * y_hr);
                    tempR += weight * fg_ptr[bufferIndex + ImageBuffer.OrderR];
                    tempG += weight * fg_ptr[bufferIndex + ImageBuffer.OrderG];
                    tempB += weight * fg_ptr[bufferIndex + ImageBuffer.OrderB];

                    tempR >>= (int)image_subpixel_scale_e.image_subpixel_shift * 2;
                    tempG >>= (int)image_subpixel_scale_e.image_subpixel_shift * 2;
                    tempB >>= (int)image_subpixel_scale_e.image_subpixel_shift * 2;

                    RGBA_Bytes color;
                    color.red       = (byte)tempR;
                    color.green     = (byte)tempG;
                    color.blue      = (byte)tempB;
                    color.alpha     = 255;
                    span[spanIndex] = color;
                    spanIndex++;
                    spanInterpolator.Next();
                } while (--len != 0);
            }
        }
Ejemplo n.º 19
0
        void CalculateOrientationAtAllFeatures(ImageBuffer imageBuffer)
        {
            byte[] buffer = imageBuffer.GetBuffer();
            foreach (ValidResponseData validResponse in validResponsesBotomToTopList)
            {
                if (validResponse.totalResponse <= 0)
                {
                    throw new Exception("You should not have added a response below the threashold.");
                }

                int maxAbsSum  = 0;
                int byteOffset = imageBuffer.GetBufferOffsetXY((int)validResponse.position.x, (int)validResponse.position.y);
                for (int angleToCheckIndex = 0; angleToCheckIndex < 4; angleToCheckIndex++)
                {
                    int sum = 0;
                    for (int extraAngleToCheckIndex = angleToCheckIndex - 1; extraAngleToCheckIndex <= angleToCheckIndex + 1; extraAngleToCheckIndex++)
                    {
                        int check0 = extraAngleToCheckIndex; if (check0 < 0)
                        {
                            check0 += 16;
                        }
                        if (check0 > 15)
                        {
                            check0 -= 16;
                        }
                        int check1 = extraAngleToCheckIndex + 8; if (check1 < 0)
                        {
                            check1 += 16;
                        }
                        if (check1 > 15)
                        {
                            check1 -= 16;
                        }
                        int check2 = extraAngleToCheckIndex + 4; if (check2 < 0)
                        {
                            check2 += 16;
                        }
                        if (check2 > 15)
                        {
                            check2 -= 16;
                        }
                        int check3 = extraAngleToCheckIndex + 12; if (check3 < 0)
                        {
                            check3 += 16;
                        }
                        if (check3 > 15)
                        {
                            check3 -= 16;
                        }
                        sum +=
                            (buffer[byteOffset + byteOffsetToPixel[check0]] + buffer[byteOffset + byteOffsetToPixel[check1]])
                            -
                            (buffer[byteOffset + byteOffsetToPixel[check2]] + buffer[byteOffset + byteOffsetToPixel[check3]]);
                    }
                    int absSum = Math.Abs(sum);
                    if (absSum > maxAbsSum)
                    {
                        maxAbsSum = absSum;
                        if (sum > 0)
                        {
                            validResponse.orientation = angleToCheckIndex;
                        }
                        else
                        {
                            validResponse.orientation = (4 + angleToCheckIndex);
                        }
                    }
                }
            }
        }