Exemplo n.º 1
0
        /// <summary>Resizes the bitmap to the specified size.</summary>
        /// <remarks>This should be overloaded to speed up the resize.</remarks>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="mode">The resize mode.</param>
        /// <returns></returns>
        public virtual Bitmap32 Resize(int width, int height, ResizeMode mode = 0)
        {
            var   result = new Bitmap32(width, height);
            float w      = width;
            float h      = height;

            if (mode != ResizeMode.None)
            {
                float fw = w / (float)Width;
                float fh = h / (float)Height;
                float f;
                if (mode.HasFlag(ResizeMode.TouchFromInside))
                {
                    f = Math.Min(fw, fh);
                }
                else
                {
                    f = Math.Max(fw, fh);
                }
                w = Width * f;
                h = Height * f;
            }
            float x = (width - w) / 2;
            float y = (height - h) / 2;

            result.Draw(this, x, y, w, h);
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a 32x32 fingerprint for the specified bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <returns>Returns a fingerprint with 6 bits per pixel (32 px = 6144 bit = 768 byte = 1024 base32 chars).</returns>
        public static FingerPrint Create(IBitmap32 bitmap)
        {
            using (Bitmap32 thumb = bitmap.Resize(32, 32, ResizeMode.TouchFromInside))
            {
                ARGBImageData data = thumb.Data;
                using (var ms = new MemoryStream())
                {
                    // calculate fingerprint and distance matrix
                    var     writer         = new BitStreamWriter(ms);
                    float[] distanceMatrix = new float[16];
                    {
                        int  x = 0, y = 0;
                        ARGB last = 0;
                        foreach (ARGB pixel in data.Data)
                        {
                            if (++x > 15)
                            {
                                x = 0;
                                ++y;
                            }

                            int r = pixel.Red >> 6;
                            int g = pixel.Green >> 6;
                            int b = pixel.Blue >> 6;
                            writer.WriteBits(r, 2);
                            writer.WriteBits(g, 2);
                            writer.WriteBits(b, 2);

                            unchecked
                            {
                                int   i        = ((y << 1) & 0xC) + (x >> 2);
                                float distance = Math.Abs(pixel.GetDistance(last));
                                distanceMatrix[i] += distance;
                                last = pixel;
                            }
                        }
                    }

                    // normalize matrix
                    float maxDistance = distanceMatrix.Max();
                    for (int i = 0; i < distanceMatrix.Length; i++)
                    {
                        distanceMatrix[i] /= maxDistance;
                    }

                    // calculate blocks
                    uint[] blocks = new uint[4];
                    int[]  index  = new int[] { 0, 2, 8, 10 };
                    for (int i = 0; i < 4; i++)
                    {
                        int  idx        = index[i];
                        uint blockValue = (uint)(255 * distanceMatrix[idx]) << 24;
                        blockValue |= (uint)(255 * distanceMatrix[idx + 1]) << 16;
                        blockValue |= (uint)(255 * distanceMatrix[idx + 4]) << 8;
                        blockValue |= (uint)(255 * distanceMatrix[idx + 5]);
                        blocks[i]   = blockValue;
                    }

                    /*
                     * uint b1 = (uint)(uint.MaxValue * (distanceMatrix[0] + distanceMatrix[1]  + distanceMatrix[4] + distanceMatrix[5]) /4);
                     * uint b2 = (uint)(uint.MaxValue * (distanceMatrix[3] + distanceMatrix[2]  + distanceMatrix[7] + distanceMatrix[6]) / 4);
                     * uint b3 = (uint)(uint.MaxValue * (distanceMatrix[12] + distanceMatrix[13]  + distanceMatrix[8]  + distanceMatrix[9]) / 4);
                     * uint b4 = (uint)(uint.MaxValue * (distanceMatrix[15] + distanceMatrix[14]  + distanceMatrix[11] + distanceMatrix[10]) / 4);
                     */
                    return(new FingerPrint(32, blocks, ms.ToArray()));
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="translation">The translation.</param>
 public virtual void Draw(Bitmap32 other, int x, int y, Translation?translation = null)
 {
     bitmap.Draw(other, x, y, other.Width, other.Height, translation);
 }
Exemplo n.º 4
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public virtual void Draw(Bitmap32 other, float x, float y, float width, float height, Translation?translation = null)
 {
     bitmap.Draw(other, x, y, width, height, translation);
 }
Exemplo n.º 5
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public override void Draw(Bitmap32 other, float x, float y, float width, float height, Translation?translation = null)
 {
     Draw(Convert(other), x, y, width, height, translation);
 }
Exemplo n.º 6
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="translation">The translation.</param>
 public override void Draw(Bitmap32 other, int x, int y, Translation?translation = null)
 {
     Draw(other, x, y, other.Width, other.Height, translation);
 }
Exemplo n.º 7
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public override void Draw(Bitmap32 other, float x, float y, float width, float height, Translation?translation = null)
 {
     Draw(GdiBitmap32Extensions.ToGdiBitmap(other), x, y, width, height, translation);
 }
Exemplo n.º 8
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public override void Draw(Bitmap32 other, int x, int y, int width, int height, Translation?translation = null) => Draw(GdiBitmap32Extensions.ToGdiBitmap(other), x, y, width, height, translation);
Exemplo n.º 9
0
 /// <summary>Draws the specified image ontop of this one.</summary>
 /// <param name="other">The image to draw.</param>
 /// <param name="x">The x position.</param>
 /// <param name="y">The y position.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="translation">The translation.</param>
 public virtual void Draw(Bitmap32 other, int x, int y, int width, int height, Translation?translation = null) => bitmap.Draw(other, x, y, width, height, translation);