示例#1
0
        public void TestCropRect()
        {
            var srcImage = new IndexedImage {
                Size = new Size(5, 5)
            };

            int[] pixels;
            using (srcImage.LockPixels(out pixels))
            {
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = i;
                }
            }

            var dstImage = ImageCropper.Crop(srcImage, new Rectangle(2, 1, 2, 3), ImageCropper.CropKind.Rectangle);

            Assert.AreEqual(new Size(2, 3), dstImage.Size);
            using (dstImage.LockPixels(out pixels))
            {
                Assert.AreEqual(6, pixels.Length);
                Assert.AreEqual(7, pixels[0]);
                Assert.AreEqual(8, pixels[1]);
                Assert.AreEqual(12, pixels[2]);
                Assert.AreEqual(13, pixels[3]);
                Assert.AreEqual(17, pixels[4]);
                Assert.AreEqual(18, pixels[5]);
            }
        }
示例#2
0
        public void TestCropArc()
        {
            var srcImage = new IndexedImage {
                Size = new Size(10, 10)
            };

            int[] pixels;
            using (srcImage.LockPixels(out pixels))
            {
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = 100;
                }
            }

            var dstImage = ImageCropper.Crop(srcImage, new Rectangle(1, 1, 8, 8), ImageCropper.CropKind.Arc);

            dstImage.CompletePalette();

            Assert.AreEqual(new Size(8, 8), dstImage.Size);
            Assert.AreEqual(new Color(255, 255, 255), dstImage[0, 0]);
            Assert.AreEqual(new Color(255, 255, 255), dstImage[0, 7]);
            Assert.AreEqual(new Color(255, 255, 255), dstImage[7, 0]);
            Assert.AreEqual(new Color(255, 255, 255), dstImage[7, 7]);
            Assert.AreEqual(new Color(100), dstImage[4, 4], "Some point in the centre");
        }
示例#3
0
        public void TestNewColor()
        {
            CodedPalette palette =
                new CodedPalette
            {
                new Color(1),
                new CodedColor(2)
            };

            Assert.AreEqual(typeof(CodedColor), palette[1].GetType());
            Assert.AreEqual(typeof(CodedColor), palette[2].GetType());

            IndexedImage image = new IndexedImage {
                Size = new Size(2, 2)
            };

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                pixels[0] = 0;
                pixels[1] = 1;
                pixels[2] = 2;
                pixels[3] = 1;
            }

            image.Palette = new CodedPalette();
            image.CompletePalette();

            Assert.AreEqual(3, image.Palette.Count);
            foreach (var color in image.Palette)
            {
                Assert.AreEqual(typeof(CodedColor), color.GetType());
            }
        }
示例#4
0
        public void TestFlipHorizontallyInPlace()
        {
            var srcImage = new IndexedImage {
                Size = new Size(3, 2)
            };

            int[] pixels;
            using (srcImage.LockPixels(out pixels))
            {
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = i;
                }
            }

            var dstImage = ImageRotator.FlipHorizontallyInPlace(srcImage);

            Assert.AreSame(srcImage, dstImage);
            Assert.AreEqual(new Size(3, 2), dstImage.Size);
            using (dstImage.LockPixels(out pixels))
            {
                Assert.AreEqual(2, pixels[0]);
                Assert.AreEqual(1, pixels[1]);
                Assert.AreEqual(0, pixels[2]);
                Assert.AreEqual(5, pixels[3]);
                Assert.AreEqual(4, pixels[4]);
                Assert.AreEqual(3, pixels[5]);
            }
        }
示例#5
0
 public void PaintText(string text, IndexedImage image, Point startPoint, Rectangle clipRect = default(Rectangle), int spaceBetweenCharacters = 1, int fontRgb = 0, TextDirection direction = TextDirection.LeftToRight, bool multithread = false)
 {
     int[] imagePixels;
     using (image.LockPixels(out imagePixels))
     {
         PaintText(text, imagePixels, image.Size, startPoint, clipRect, spaceBetweenCharacters, fontRgb, direction, multithread);
     }
 }
示例#6
0
 public void PaintSymbol(char c, IndexedImage image, Point atPoint, Rectangle clipRect = default(Rectangle), int fontRgb = 0, int backgroundRgb = 0x00ffffff)
 {
     int[] imagePixels;
     using (image.LockPixels(out imagePixels))
     {
         PaintSymbol(c, imagePixels, image.Size, atPoint, clipRect, fontRgb, backgroundRgb);
     }
 }
示例#7
0
        void AssertLoadedImage(IndexedImage image, Size expectedSize, int expectedColor)
        {
            Assert.AreEqual(expectedSize.Width, image.Size.Width);
            Assert.AreEqual(expectedSize.Height, image.Size.Height);

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                foreach (var pixel in pixels)
                {
                    Assert.AreEqual(expectedColor, pixel);
                }
            }
        }
示例#8
0
        /// <summary>
        /// Updates pixels in specified <see cref="System.Drawing.Bitmap"/> from this <see cref="IndexedImage"/> instance.
        /// </summary>
        /// <param name="image">Source <see cref="Ravlyk.Drawing.IndexedImage"/> object.</param>
        /// <param name="bitmap">Target <see cref="System.Drawing.Bitmap"/> to update.</param>
        public static void UpdateBitmap(this IndexedImage image, Bitmap bitmap)
        {
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            IntPtr ptr = bmpData.Scan0;

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                Debug.Assert(pixels != null, "Cannot lock pixels on the image.");
                System.Runtime.InteropServices.Marshal.Copy(pixels, 0, ptr, pixels.Length);
            }
            bitmap.UnlockBits(bmpData);
        }
示例#9
0
        public void TestCompletePaletteFromImage()
        {
            IndexedImage image = new IndexedImage {
                Size = new Size(2, 2)
            };

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                pixels[0] = 0;
                pixels[1] = 255;
                pixels[2] = 1;
                pixels[3] = 255;
            }

            Palette palette = new Palette();

            Assert.AreEqual(0, palette.Count);
            palette.CompletePaletteFromImage(image);

            Assert.AreEqual(3, palette.Count);
            AssertColorInPalette(palette, 0, 0, 0);
            AssertColorInPalette(palette, 1, 1, 2);
            AssertColorInPalette(palette, 255, 255, 1, 3);

            using (image.LockPixels(out pixels))
            {
                pixels[0] = 100;
                pixels[2] = 100;
            }
            palette.CompletePaletteFromImage(image);

            Assert.AreEqual(2, palette.Count);
            AssertColorInPalette(palette, 100, 100, 0, 2);
            AssertColorInPalette(palette, 255, 255, 1, 3);
        }
示例#10
0
        static void SetPixelsFromString(IndexedImage image, string pixelsString)
        {
            if (image.Size.Width * image.Size.Height < pixelsString.Length / 8)
            {
                image.Size = new Size(1, pixelsString.Length / 8);
            }

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                for (int i = 0; i < pixelsString.Length / 8; i++)
                {
                    pixels[i] = int.Parse(pixelsString.Substring(i * 8, 8), NumberStyles.AllowHexSpecifier);
                }
            }
        }
示例#11
0
        public void TestFromIndexedImage()
        {
            var indexedImage = new IndexedImage {
                Size = new Size(2, 3)
            };
            var codedImage = CodedImage.FromIndexedImage(indexedImage);

            Assert.AreEqual(indexedImage.Size, codedImage.Size);

            int[] indexedPixels, codedPixels;
            using (indexedImage.LockPixels(out indexedPixels))
                using (codedImage.LockPixels(out codedPixels))
                {
                    Assert.AreSame(indexedPixels, codedPixels, "Should use same pixels array");
                }
        }
示例#12
0
        public static CodedImage FromIndexedImage(IndexedImage sourceImage)
        {
            var codedImage = sourceImage as CodedImage;

            if (codedImage == null)
            {
                codedImage = new CodedImage();
                int[] pixels;
                using (sourceImage.LockPixels(out pixels))
                {
                    codedImage.Pixels = pixels;
                }
                codedImage.Size = sourceImage.Size;
            }
            return(codedImage);
        }
示例#13
0
        /// <summary>
        /// Converts <see cref="System.Drawing.Bitmap"/> object to <see cref="Ravlyk.Drawing.IndexedImage"/> one with explicit operation.
        /// </summary>
        /// <param name="bitmap">The source <see cref="T:System.Drawing.Bitmap"/> object.</param>
        /// <returns>New <see cref="Ravlyk.Drawing.IndexedImage"/> object initialized from source bitmap.</returns>
        public static IndexedImage FromBitmap(Bitmap bitmap)
        {
            IndexedImage image = new IndexedImage {
                Size = new Ravlyk.Common.Size(bitmap.Width, bitmap.Height)
            };
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            IntPtr ptr = bmpData.Scan0;

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                Debug.Assert(pixels != null, "Cannot lock pixels on the image.");
                System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, pixels.Length);
            }
            bitmap.UnlockBits(bmpData);
            return(image);
        }
示例#14
0
 static void ColorCursorImage(IndexedImage image, int argb)
 {
     int[] pixels;
     using (image.LockPixels(out pixels))
     {
         Parallel.For(0, pixels.Length,
                      index =>
         {
             var pixelArgb = pixels[index];
             var a         = pixelArgb.Alpha();
             if (a != 0)                             // Skip transparent
             {
                 var r = pixelArgb.Red();
                 var g = pixelArgb.Green();
                 var b = pixelArgb.Blue();
                 if (Math.Abs(r - g) + Math.Abs(g - b) + Math.Abs(b - r) <= 12)                                 // Close to grey colors
                 {
                     pixels[index] = ColorBytes.ShadeColor(pixelArgb, argb);
                 }
             }
         });
     }
 }
示例#15
0
        public void TestLockPixels()
        {
            IndexedImage image = new IndexedImage {
                Size = new Size(3, 2)
            };

            int[] pixels;
            using (image.LockPixels(out pixels))
            {
                Assert.IsNotNull(pixels);

                pixels[0] = 1;
                pixels[1] = 2;
                pixels[2] = 3;
                pixels[3] = 4;
                pixels[4] = 5;
                pixels[5] = 6;
            }

            Assert.AreEqual(0, image.Palette.Count);
            image.CompletePalette();
            Assert.AreEqual(6, image.Palette.Count);

            Assert.AreEqual(1, image[0, 0].Argb);
            Assert.AreEqual(2, image[1, 0].Argb);
            Assert.AreEqual(3, image[2, 0].Argb);
            Assert.AreEqual(4, image[0, 1].Argb);
            Assert.AreEqual(5, image[1, 1].Argb);
            Assert.AreEqual(6, image[2, 1].Argb);

            PaletteTest.AssertColorInPalette(image.Palette, 1, 1, 0);
            PaletteTest.AssertColorInPalette(image.Palette, 2, 2, 1);
            PaletteTest.AssertColorInPalette(image.Palette, 3, 3, 2);
            PaletteTest.AssertColorInPalette(image.Palette, 4, 4, 3);
            PaletteTest.AssertColorInPalette(image.Palette, 5, 5, 4);
            PaletteTest.AssertColorInPalette(image.Palette, 6, 6, 5);
        }