Example #1
0
 /// <summary>
 /// Increment the pixel count and add to the color information
 /// </summary>
 public void Increment(Color32 *pixel)
 {
     this._pixelCount++;
     this._red   += pixel->Red;
     this._green += pixel->Green;
     this._blue  += pixel->Blue;
 }
Example #2
0
            public unsafe void Execute(int index)
            {
                Color32 *outputColor = (Color32 *)output[index];

                for (int i = flattenIndex[index].start; i <= flattenIndex[index].end; ++i)
                {
                    if (original[i] == IntPtr.Zero)
                    {
                        continue;
                    }
                    Color32 *originalColor = (Color32 *)original[i];
                    int      bufferWidth   = width[i];
                    int      bufferHeight  = height[i];
                    for (int h = 0; h < bufferHeight; ++h)
                    {
                        int originalYOffset = h * bufferWidth;
                        int outputYOffset   = (bufferHeight - h - 1) * bufferWidth;
                        for (int w = 0; w < bufferWidth; ++w)
                        {
                            var   outColor = outputColor[w + outputYOffset];
                            var   inColor  = originalColor[w + originalYOffset];
                            float alpha    = outColor.a / 255.0f;
                            outColor.r = (byte)(alpha * (float)(outColor.r) + (float)((1.0f - alpha) * (float)inColor.r));
                            outColor.g = (byte)(alpha * (float)(outColor.g) + (float)((1.0f - alpha) * (float)inColor.g));
                            outColor.b = (byte)(alpha * (float)(outColor.b) + (float)((1.0f - alpha) * (float)inColor.b));
                            outColor.a = (byte)(alpha * (float)(outColor.a) + (float)((1.0f - alpha) * (float)inColor.a));
                            outputColor[w + outputYOffset] = outColor;
                        }
                    }
                }
            }
Example #3
0
 private static void CopyFullRow(Color32 *dst, Color32 *src)
 {
     dst[0] = src[0];
     dst[1] = src[1];
     dst[2] = src[2];
     dst[3] = src[3];
 }
Example #4
0
        public static void LoadFont(string fontPath)
        {
            var surf = SDL_image.IMG_Load(fontPath);

            if (surf == IntPtr.Zero)
            {
                Console.WriteLine("Font loading failed, things about to break");
                Console.WriteLine(SDL_image.IMG_GetError());
                return;
            }
            var fontSurface = Marshal.PtrToStructure <SDL.SDL_Surface>(surf);

            fontWidth  = fontSurface.w;
            fontHeight = fontSurface.h;

            fontBuffer = new bool[fontWidth, fontHeight];
            unsafe {
                Color32 *colors = (Color32 *)fontSurface.pixels;

                for (int i = 0; i < fontWidth; i++)
                {
                    for (int j = 0; j < fontHeight; j++)
                    {
                        int fontColorBufferIndex = (j * fontWidth) + i;
                        fontBuffer[i, fontHeight - j - 1] = colors[fontColorBufferIndex].r > 0;
                    }
                }
            }

            fontWidth  = fontSurface.w / 16;
            fontHeight = fontSurface.h / 8;
        }
Example #5
0
 public unsafe static extern void CalibrateCamera(
     Color32 *textureData, int width, int height,
     IntPtr image_points,
     IntPtr object_points,
     IntPtr cameraMatrix,
     IntPtr distortionCoefficients
     );
Example #6
0
 /// <summary> Increment the pixel count and add to the color information. </summary>
 public void Increment(Color32 *pixel)
 {
     pixelCount++;
     red   += pixel->Red;
     green += pixel->Green;
     blue  += pixel->Blue;
 }
Example #7
0
        /// <summary>
        /// Override this to process the pixel in the second pass of the algorithm
        /// </summary>
        /// <param name="pixel">The pixel to quantize</param>
        /// <param name="destinationPixel"></param>
        /// <returns>The quantized value</returns>
        protected override void QuantizePixel(Color32 *pixel, Color32 *destinationPixel)
        {
            int maxColor = pixel->Red;

            if (maxColor < pixel->Green)
            {
                maxColor = pixel->Green;
            }
            if (maxColor < pixel->Blue)
            {
                maxColor = pixel->Blue;
            }

            int minColor = pixel->Red;

            if (minColor > pixel->Green)
            {
                minColor = pixel->Green;
            }
            if (minColor > pixel->Blue)
            {
                minColor = pixel->Blue;
            }

            var luminance = (byte)((minColor + maxColor) / 2.00f);

            destinationPixel->Red   = luminance;
            destinationPixel->Green = luminance;
            destinationPixel->Blue  = luminance;
            destinationPixel->Alpha = pixel->Alpha;
        }
Example #8
0
 protected override unsafe void Map(Color32 *pixels, int len)
 {
     for (int ii = 0; ii < len; ++ii)
     {
         Color32 pixel = pixels[ii];
         if (pixel.a > zero)
         {
             float value = pixel.r / 255f;
             if (value > 0.5f)
             {
                 value   = value * 2 - 1f;
                 pixel.r = (byte)(mGray.r + value * (mWhite.r - mGray.r));
                 pixel.g = (byte)(mGray.g + value * (mWhite.g - mGray.g));
                 pixel.b = (byte)(mGray.b + value * (mWhite.b - mGray.b));
             }
             else
             {
                 value   = value * 2;
                 pixel.r = (byte)(mBlack.r + value * (mGray.r - mBlack.r));
                 pixel.g = (byte)(mBlack.g + value * (mGray.g - mBlack.g));
                 pixel.b = (byte)(mBlack.b + value * (mGray.b - mBlack.b));
             }
             pixels[ii] = pixel;
         }
     }
 }
Example #9
0
 /// <summary>
 /// Override this to process the pixel in the second pass of the algorithm
 /// </summary>
 /// <param name="pixel">The pixel to quantize</param>
 /// <param name="destinationPixel"></param>
 /// <returns>The quantized value</returns>
 protected override void QuantizePixel(Color32 *pixel, Color32 *destinationPixel)
 {
     destinationPixel->Red   = pixel->Alpha;
     destinationPixel->Blue  = pixel->Alpha;
     destinationPixel->Green = pixel->Alpha;
     destinationPixel->Alpha = 255;
 }
Example #10
0
 /// <summary>
 /// Increment the pixel count and add to the color information
 /// </summary>
 /// <param name="pixel">
 /// The pixel to add.
 /// </param>
 public void Increment(Color32 *pixel)
 {
     this.pixelCount++;
     this.red   += pixel->R;
     this.green += pixel->G;
     this.blue  += pixel->B;
 }
Example #11
0
 public void Increment(Color32 *pixel)
 {
     Red   += pixel->Red;
     Green += pixel->Green;
     Blue  += pixel->Blue;
     PixelCount++;
 }
Example #12
0
        private static Color32[] FromSRGBOrSRGBA(IntPtr ptr, ImageFormat format, int width, int height, int widthStep)
        {
            var colors  = new Color32[width * height];
            var padding = format == ImageFormat.SRGB ? (widthStep - 3 * width) : (widthStep - 4 * width);

            unsafe
            {
                fixed(Color32 *dest = colors)
                {
                    byte *   pSrc  = (byte *)ptr.ToPointer();
                    Color32 *pDest = dest;

                    for (var i = 0; i < height; i++)
                    {
                        for (var j = 0; j < width; j++)
                        {
                            byte r       = *pSrc++;
                            byte g       = *pSrc++;
                            byte b       = *pSrc++;
                            byte a       = format == ImageFormat.SRGB ? (byte)255 : (*pSrc++);
                            *    pDest++ = new Color32(r, g, b, a);
                        }

                        pSrc += padding;
                    }
                }
            }

            return(colors);
        }
Example #13
0
        public void CaptureFrame(Color32[] pixelBuffer)
        {
            if (preview == null)
            {
                return;
            }

#if OPENCV_USE_UNSAFE_CODE && UNITY_2018_2_OR_NEWER
            unsafe
            {
                NativeArray <Color32> rawTextureData = preview.GetRawTextureData <Color32>();
                int      size    = UnsafeUtility.SizeOf <Color32>() * rawTextureData.Length;
                Color32 *srcAddr = (Color32 *)NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(rawTextureData);

                fixed(Color32 *dstAddr = pixelBuffer)
                {
                    UnsafeUtility.MemCpy(dstAddr, srcAddr, size);
                }
            }
#else
            byte[]   rawTextureData = preview.GetRawTextureData();
            GCHandle pin            = GCHandle.Alloc(pixelBuffer, GCHandleType.Pinned);
            Marshal.Copy(rawTextureData, 0, pin.AddrOfPinnedObject(), rawTextureData.Length);
            pin.Free();
#endif
        }
        public void Draw(Texture2D texture, ImageFrame mask, Color color, bool isFlipped = false, float threshold = 0.9f)
        {
            var maskPixels = mask.GetColor32s(isFlipped);
            var maskWidth  = mask.Width();
            var maskHeight = mask.Height();
            var minValue   = 255 * threshold;

            unsafe
            {
                fixed(Color32 *maskPtr = maskPixels)
                {
                    Color32 *pixel = maskPtr;

                    for (var i = 0; i < maskHeight; i++)
                    {
                        for (var j = 0; j < maskWidth; j++)
                        {
                            if (pixel->r > minValue)
                            {
                                SetMask(texture, j, i, maskWidth, maskHeight, (float)(pixel->r) / 255, color);
                            }

                            pixel++;
                        }
                    }
                }
            }
        }
Example #15
0
 /// <summary>
 /// Increment the pixel count and add to the color information
 /// </summary>
 /// <param name="pixel">
 /// The pixel to add.
 /// </param>
 public void Increment(Color32 *pixel)
 {
     pixelCount++;
     red   += pixel->R;
     green += pixel->G;
     blue  += pixel->B;
 }
Example #16
0
 public unsafe void Increment(Color32 *pixel)
 {
     _pixelCount++;
     _red   += pixel->Red;
     _green += pixel->Green;
     _blue  += pixel->Blue;
 }
Example #17
0
        public static void stb_compress_dxt_block(byte *dest, Color32 *src, int alpha, int mode)
        {
            var data = stackalloc Color32[16];

            if (init != 0)
            {
                stb__InitDXT();
                init = 0;
            }

            if (alpha != 0)
            {
                var i = 0;
                stb__CompressAlphaBlock(dest, src);
                dest += 8;
                for (i = 0; i < 16; ++i)
                {
                    data[i]   = src[i];
                    data[i].a = 255;
                }
                src = data;
            }

            stb__CompressColorBlock(dest, src, mode);
        }
Example #18
0
                /// <summary> Add a color into the tree. </summary>
                /// <param name="pixel"> The color. </param>
                /// <param name="colorBits"> The number of significant color bits. </param>
                /// <param name="level"> The level in the tree. </param>
                /// <param name="octree"> The tree to which this node belongs. </param>
                public void AddColor(Color32 *pixel, int colorBits, int level, Octree octree)
                {
                    // Update the color information if this is a leaf
                    if (leaf)
                    {
                        Increment(pixel);
                        // Setup the previous node
                        octree.TrackPrevious(this);
                    }
                    else
                    {
                        // Go to the next level down in the tree
                        int shift = 7 - level;
                        int index = ((pixel->Red & Mask[level]) >> (shift - 2)) |
                                    ((pixel->Green & Mask[level]) >> (shift - 1)) |
                                    ((pixel->Blue & Mask[level]) >> (shift));

                        OctreeNode child = children[index];

                        if (null == child)
                        {
                            // Create a new child node & store in the array
                            child           = new OctreeNode(level + 1, colorBits, octree);
                            children[index] = child;
                        }

                        // Add the color to the child node
                        child.AddColor(pixel, colorBits, level + 1, octree);
                    }
                }
Example #19
0
        private unsafe byte QuantizePixel(Color32 *pixel)
        {
            byte colorIndex = 0;
            int  colorHash  = pixel->ARGB;

            if (_colorMap.ContainsKey(colorHash))
            {
                colorIndex = (byte)_colorMap[colorHash];
            }
            else
            {
                if (0 == pixel->Alpha)
                {
                    for (int index = 0; index < _colors.Length; index++)
                    {
                        if (0 == _colors[index].A)
                        {
                            colorIndex = (byte)index;
                            break;
                        }
                    }
                }
                else
                {
                    int leastDistance = int.MaxValue;
                    int red           = pixel->Red;
                    int green         = pixel->Green;
                    int blue          = pixel->Blue;

                    for (int index = 0; index < _colors.Length; index++)
                    {
                        Color paletteColor = _colors[index];

                        int redDistance   = paletteColor.R - red;
                        int greenDistance = paletteColor.G - green;
                        int blueDistance  = paletteColor.B - blue;

                        int distance = (redDistance * redDistance) +
                                       (greenDistance * greenDistance) +
                                       (blueDistance * blueDistance);

                        if (distance < leastDistance)
                        {
                            colorIndex    = (byte)index;
                            leastDistance = distance;

                            if (0 == distance)
                            {
                                break;
                            }
                        }
                    }
                }

                _colorMap.Add(colorHash, colorIndex);
            }

            return(colorIndex);
        }
Example #20
0
        /// <summary>
        /// Override this to process the pixel in the second pass of the algorithm
        /// </summary>
        /// <param name="pixel">The pixel to quantize</param>
        /// <returns>The quantized value</returns>
        protected override byte QuantizePixel(Color32 *pixel)
        {
            byte colorIndex = 0;
            int  colorHash  = pixel->ARGB;

            // Check if the color is in the lookup table
            if (_colorMap.ContainsKey(colorHash))
            {
                colorIndex = (byte)_colorMap[colorHash];
            }
            else
            {
                // Not found - loop through the palette and find the nearest match.
                // Firstly check the alpha value - if < 128, set the transparent color
                if (pixel->Alpha < 128)
                {
                    colorIndex = 0; // color 0 is transparent for Freebox
                }
                else
                {
                    // Not transparent...
                    int leastDistance = int.MaxValue;
                    int red           = pixel->Red;
                    int green         = pixel->Green;
                    int blue          = pixel->Blue;

                    // Loop through the freebox palette visible colors, looking for the closest color match
                    for (int index = 1; index < 192; index++)
                    {
                        Color paletteColor = _colors[index];

                        int redDistance   = paletteColor.R - red;
                        int greenDistance = paletteColor.G - green;
                        int blueDistance  = paletteColor.B - blue;

                        int distance = (redDistance * redDistance) +
                                       (greenDistance * greenDistance) +
                                       (blueDistance * blueDistance);

                        if (distance < leastDistance)
                        {
                            colorIndex    = (byte)index;
                            leastDistance = distance;

                            // And if it's an exact match, exit the loop
                            if (0 == distance)
                            {
                                break;
                            }
                        }
                    }
                }

                // Now I have the color, pop it into the hashtable for next time
                _colorMap.Add(colorHash, colorIndex);
            }

            return(colorIndex);
        }
Example #21
0
            public int      width, height; // 2D size (product must be <= length)

            // no allocation; pointer comes from a []
            public ColorBuffer(Color32[] c, Color32 *pc, int width_, int height_)
            {
                width   = width_;
                height  = height_;
                length  = c.Length;
                dealloc = IntPtr.Zero;
                array   = pc;
            }
Example #22
0
 // allocate non-garbage-collected memory
 public ColorBuffer(int width_, int height_)
 {
     width   = width_;
     height  = height_;
     length  = width * height;
     dealloc = System.Runtime.InteropServices.Marshal.AllocHGlobal(length * sizeof(Color32));
     array   = (Color32 *)dealloc;
 }
Example #23
0
 public unsafe static extern void FindChessboardCorners(
     Color32 *textureData, int width, int height,
     int cornersW, int cornersH,
     float cornerLength, float cornerSeparation,
     IntPtr foundBoardMarkers,
     IntPtr image_points,
     IntPtr object_points
     );
Example #24
0
        public unsafe void SizeAndAlignmentTest()
        {
            Assert.AreEqual(4, sizeof(Color32));

            Color32 *p = stackalloc Color32[2];

            Assert.AreEqual(4, (byte *)&p[1] - (byte *)&p[0]);
        }
Example #25
0
        private unsafe void SetPixelCore(int x, int y, ref WinColor pixelColor)
        {
            Color32 *pixelPtr = this[x, y];

            pixelPtr->A = pixelColor.A;
            pixelPtr->R = pixelColor.R;
            pixelPtr->G = pixelColor.G;
            pixelPtr->B = pixelColor.B;
        }
Example #26
0
        public static void stb__CompressAlphaBlock(byte *dest, Color32 *src)
        {
            var i     = 0;
            var dist  = 0;
            var bias  = 0;
            var dist4 = 0;
            var dist2 = 0;
            var bits  = 0;
            var mask  = 0;
            var mn    = 0;
            var mx    = 0;

            mn = mx = src[0].a;
            for (i = 1; i < 16; i++)
            {
                if (src[i].a < mn)
                {
                    mn = src[i].a;
                }
                else if (src[i].a > mx)
                {
                    mx = src[i].a;
                }
            }
            dest[0] = (byte)mx;
            dest[1] = (byte)mn;
            dest   += 2;
            dist    = mx - mn;
            dist4   = dist * 4;
            dist2   = dist * 2;
            bias    = dist < 8 ? dist - 1 : dist / 2 + 2;
            bias   -= mn * 7;
            bits    = 0;
            mask    = 0;
            for (i = 0; i < 16; i++)
            {
                var a   = src[i].a * 7 + bias;
                var ind = 0;
                var t   = 0;
                t     = a >= dist4 ? -1 : 0;
                ind   = t & 4;
                a    -= dist4 & t;
                t     = a >= dist2 ? -1 : 0;
                ind  += t & 2;
                a    -= dist2 & t;
                ind  += a >= dist ? 1 : 0;
                ind   = -ind & 7;
                ind  ^= 2 > ind ? 1 : 0;
                mask |= ind << bits;
                if ((bits += 3) >= 8)
                {
                    *dest++ = (byte)mask;
                    mask >>= 8;
                    bits  -= 8;
                }
            }
        }
Example #27
0
        /// <summary>
        /// Override this to process the pixel in the second pass of the algorithm
        /// </summary>
        /// <param name="pixel">The pixel to quantize</param>
        /// <returns>The quantized value</returns>
        protected override byte QuantizePixel(Color32 *pixel)
        {
            pixel = Quantizer.ConvertAlpha(pixel);
            int redIndex   = pixel->Red >> 5;
            int greenIndex = pixel->Green >> 5;
            int blueIndex  = pixel->Blue >> 6;

            return((byte)((redIndex << 5) + (greenIndex << 2) + blueIndex));
        }
Example #28
0
 public void Deallocate()
 {
     if (dealloc != IntPtr.Zero)
     {
         System.Runtime.InteropServices.Marshal.FreeHGlobal(dealloc);
         dealloc = IntPtr.Zero;
         array   = null;
         length  = width = height = 0;
     }
 }
Example #29
0
        protected override unsafe byte QuantizePixel(Color32 *pixel)
        {
            var paletteIndex = (byte)_maxColors;

            if (pixel->Alpha > 254)
            {
                paletteIndex = (byte)_octree.GetPaletteIndex(pixel);
            }
            return(paletteIndex);
        }
Example #30
0
        /// <summary> Override this to process the pixel in the second pass of the algorithm. </summary>
        /// <param name="pixel"> The pixel to quantize. </param>
        /// <returns> The quantized value. </returns>
        protected override byte QuantizePixel(Color32 *pixel)
        {
            byte paletteIndex = (byte)maxColors; // The color at [_maxColors] is set to transparent

            // Get the palette index if this non-transparent
            if (pixel->Alpha > 0)
            {
                paletteIndex = (byte)octree.GetPaletteIndex(pixel);
            }

            return(paletteIndex);
        }