Inheritance: IDisposable
Example #1
0
        /// <summary>
        /// Converts this instance to a <see cref="Bitmap"/> using <see cref="ImageFormat.Bmp"/>.
        /// </summary>
        /// <returns>A <see cref="Bitmap"/> that has the format <see cref="ImageFormat.Bmp"/>.</returns>
        public Bitmap ToBitmap()
        {
            if (ColorSpace == ColorSpace.CMYK)
            {
                ColorSpace = ColorSpace.sRGB;
            }

            string      mapping = "BGR";
            PixelFormat format  = PixelFormat.Format24bppRgb;

            if (HasAlpha)
            {
                mapping = "BGRA";
                format  = PixelFormat.Format32bppArgb;
            }

            using (PixelCollection pixels = GetPixels())
            {
                Bitmap     bitmap      = new Bitmap(Width, Height, format);
                BitmapData data        = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, format);
                IntPtr     destination = data.Scan0;
                for (int y = 0; y < Height; y++)
                {
                    byte[] bytes = pixels.ToByteArray(0, y, Width, 1, mapping);
                    Marshal.Copy(bytes, 0, destination, bytes.Length);

                    destination = new IntPtr(destination.ToInt64() + data.Stride);
                }

                bitmap.UnlockBits(data);
                return(bitmap);
            }
        }
 public PixelCollectionEnumerator(PixelCollection collection, int width, int height)
 {
     _collection = collection;
     _width      = width;
     _height     = height;
     Reset();
 }
Example #3
0
        /// <summary>
        /// Converts this instance to a <see cref="BitmapSource"/>.
        /// </summary>
        /// <returns>A <see cref="BitmapSource"/>.</returns>
        public BitmapSource ToBitmapSource()
        {
            string           mapping = "RGB";
            MediaPixelFormat format  = MediaPixelFormats.Rgb24;

            if (HasAlpha)
            {
                mapping = "BGRA";
                format  = MediaPixelFormats.Bgra32;
            }

            if (ColorSpace == ColorSpace.CMYK)
            {
                mapping = "CMYK";
                format  = MediaPixelFormats.Cmyk32;
            }

            int step   = format.BitsPerPixel / 8;
            int stride = Width * step;

            using (PixelCollection pixels = GetPixels())
            {
                byte[] bytes = pixels.ToByteArray(mapping);
                return(BitmapSource.Create(Width, Height, 96, 96, format, null, bytes, stride));
            }
        }
 private static void Test_Set(PixelCollection pixels, QuantumType[] value)
 {
   ExceptionAssert.Throws<ArgumentException>(delegate ()
   {
     pixels.Set(value);
   });
 }
Example #5
0
        internal static Pixel Create(PixelCollection collection, int x, int y, QuantumType[] value)
        {
            Pixel pixel = new Pixel(collection);

            pixel.Initialize(x, y, value);
            return(pixel);
        }
Example #6
0
 internal static IntPtr GetInstance(PixelCollection instance)
 {
     if (instance == null)
     {
         return(IntPtr.Zero);
     }
     return(instance._NativeInstance.Instance);
 }
    private static void Test_PixelColor(PixelCollection pixels, int x, int y, MagickColor color)
    {
      var values = pixels.GetValue(x, y);
      Assert.AreEqual(3, values.Length);

      MagickColor magickColor = new MagickColor(values[0], values[1], values[2]);
      ColorAssert.AreEqual(color, magickColor);
    }
Example #8
0
        private Pixel(PixelCollection collection, int x, int y, QuantumType[] value)
        {
            _collection = collection;

            X      = x;
            Y      = y;
            _value = value;
        }
 private static void Test_PixelColor(PixelCollection pixels, MagickColor color)
 {
   Test_PixelColor(pixels, 0, 0, color);
 }
Example #10
0
 private Pixel(PixelCollection collection)
 {
     _collection = collection;
 }
Example #11
0
 internal static IntPtr GetInstance(PixelCollection instance)
 {
   if (instance == null)
     return IntPtr.Zero;
   return instance._NativeInstance.Instance;
 }
Example #12
0
 static void Main(string[] args)
 {
     if (args.Length != 9) {
         Console.WriteLine("ImageMerge: Version 1.0");
         displaySyntax();
     } else {
         for (int i = 0; i < 50; i++)
             Placed[i] = false;
         canvasSizeX = Convert.ToInt32(args[0]);
         canvasSizeY = Convert.ToInt32(args[1]);
         int canvasStartX = Convert.ToInt32(args[2]);
         int canvasStartY = Convert.ToInt32(args[3]);
         string bgColor = "xc:" + args[4]; // canvas color name or hex color
         accuracy = Convert.ToInt32(args[5]);
         shortCut = Convert.ToInt32(args[6]);
         nearness = Convert.ToInt32(args[7]);
         double overlap = Convert.ToDouble(args[8]);
         Console.WriteLine("\nCanvas Size: " + canvasSizeX.ToString() + "x" + canvasSizeY.ToString());
         Console.WriteLine("Canvas Start: " + canvasStartX.ToString() + "x" + canvasStartY.ToString());
         Console.WriteLine("Background color: " + args[4]);
         Console.WriteLine("Accuracy: " + accuracy.ToString());
         Console.WriteLine("Nearness: " + nearness.ToString());
         Console.WriteLine("Overlap: " + overlap.ToString() + "\n");
         if (overlap != 0) // 0 means use default 20 which is 20% overlap for nearness=1
             overlapPercent = overlap;
         overlapPercent = overlapPercent / 100 / accuracy / accuracy;
         if (!((shortCut == 0) || (shortCut == 1))) {
             Console.WriteLine("shortCut value is " + shortCut.ToString() + ". It must be 0 or 1. Setting to 0.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             shortCut = 0;
         }
         if (nearness < 0) {
             Console.WriteLine("nearness value is " + nearness.ToString() + ". It must be greater than 0. Setting to 0.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             nearness = 0;
         }
         if ((nearness > 0) && (shortCut != 1)) {
             Console.WriteLine("If nearness equal 1 then shortcut must equal 1.  Setting shortcut = 1.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             shortCut = 1;
         }
         UsedPixel = new bool[canvasSizeX, canvasSizeY]; // remembers which pixels are used in the canvas
         canvasR = new ushort[canvasSizeX, canvasSizeY];
         canvasG = new ushort[canvasSizeX, canvasSizeY];
         canvasB = new ushort[canvasSizeX, canvasSizeY];
         for (int y = 0; y < canvasSizeY; y++)
             for (int x = 0; x < canvasSizeX; x++) {
                 UsedPixel[x, y] = false;
         }
         using (MagickImage canvas = new MagickImage(bgColor, canvasSizeX, canvasSizeY)) {
             string[] files = Directory.GetFiles(".", "*.png", SearchOption.TopDirectoryOnly);
             int maxWidth = 0, maxHeight = 0;
             int len = files.Length;
             if (len < 2) {
                 Console.WriteLine("Need at least 2 images to run.");
                 return;
             }
             for (int i = 0; i < len; i++) {
                 image[i] = new MagickImage(files[i]);
                 imagePC[i] = image[i].GetReadOnlyPixels();
                 if (image[i].Width > maxWidth)
                     maxWidth = image[i].Width;
                 if (image[i].Height > maxHeight)
                     maxHeight = image[i].Height;
             }
             // record RGB pixel values for each image to reduce calls to GetPixel() to improve performance
             imageR = new ushort[len, maxWidth, maxHeight];
             imageG = new ushort[len, maxWidth, maxHeight];
             imageB = new ushort[len, maxWidth, maxHeight];
             for (int i = 1; i < len; i++) {
                 for (int y = 0; y < image[i].Height; y++) {
                     for (int x = 0; x < image[i].Width; x++) {
                         Pixel p = imagePC[i].GetPixel(x, y); // image pixel
                         imageR[i, x, y] = p.GetChannel(0);
                         imageG[i, x, y] = p.GetChannel(1);
                         imageB[i, x, y] = p.GetChannel(2);
                     }
                 }
             }
             canvas.Composite(image[0], new MagickGeometry(canvasStartX, canvasStartY, image[0].Width, image[0].Height), CompositeOperator.Over);
             canvas.Write(@"output\output-1.png");
             canvasPC = canvas.GetReadOnlyPixels();
             getCanvasRGB();
             Placed[0] = true; // first image has been placed on canvas
             // top/left of active-canvas
             activeCanvasX1 = lastImageX1 = canvasStartX;
             activeCanvasY1 = lastImageY1 = canvasStartY;
             // bottom/right of active-canvas
             activeCanvasX2 = lastImageX2 = canvasStartX + image[0].Width - 1;
             activeCanvasY2 = lastImageY2 = canvasStartY + image[0].Height - 1;
             setUsedPixels(activeCanvasX1, activeCanvasY1, activeCanvasX2, activeCanvasY2);
             int outputImageNum = 2;
             bool imagePlaced = false;
             canvasX = canvasSizeX - 1; // right column of pixels
             canvasY = canvasSizeY - 1; // bottom row of pixels
             Console.WriteLine(files[0] + " -> output-1: " + canvasStartX.ToString() + "x" + canvasStartY.ToString());
             do {
                 imagePlaced = false;
                 for (int i = 1; i < len; i++) {
                     if (!Placed[i]) { // if image has not been placed on canvas
                         Console.Write(files[i]);
                         findCorrectOverlap(i);
                         if ((nearness == 0) && (overlapMax == 0))
                             Console.WriteLine(": NO MERGE");
                         else {
                             Console.WriteLine(" -> output-" + outputImageNum.ToString() + ": " + maxX1.ToString() + "x" + maxY1.ToString());
                             int w = image[i].Width;
                             int h = image[i].Height;
                             canvas.Composite(image[i], new MagickGeometry(maxX1, maxY1, w, h), CompositeOperator.Over);
                             canvas.Write(@"output\output-" + (outputImageNum++).ToString() + ".png");
                             canvasPC = canvas.GetReadOnlyPixels(); // update canvas pixels
                             getCanvasRGB();
                             // record canvas background for comparison later
                             canvasBackR = canvasR[0, 0];
                             canvasBackG = canvasG[0, 0];
                             canvasBackB = canvasB[0, 0];
                             Placed[i] = true; // image has bee placed on canvas
                             maxX2 = maxX1 + w - 1;
                             maxY2 = maxY1 + h - 1;
                             // prevent bottom right from going outside of canvas
                             if (maxX2 > canvasX)
                                 maxX2 = canvasX;
                             if (maxY2 > canvasY)
                                 maxY2 = canvasY;
                             setUsedPixels(maxX1, maxY1, maxX2, maxY2); // mark pixels used
                             // update Active Canvas
                             if (maxX1 < activeCanvasX1)
                                 activeCanvasX1 = maxX1;
                             if (maxY1 < activeCanvasY1)
                                 activeCanvasY1 = maxY1;
                             if (maxX2 > activeCanvasX2)
                                 activeCanvasX2 = maxX2;
                             if (maxY2 > activeCanvasY2)
                                 activeCanvasY2 = maxY2;
                             imagePlaced = true;
                             if (shortCut == 1) {
                                 lastImageX1 = maxX1;
                                 lastImageY1 = maxY1;
                                 lastImageX2 = maxX2;
                                 lastImageY2 = maxY2;
                             }
                         }
                     }
                 }
             } while (imagePlaced); // repeat until no images are placed
         }
     }
 }