Пример #1
0
        public static byte[] Convert(Bitmap image, PixelDataFormat destinationFormat)
        {
            image.ThrowIfNull("image");
            if (destinationFormat == PixelDataFormat.Link)
            {
                throw new ArgumentException("Code tried to convert an image to link format which doesn't make sense.");
            }

            int bytesPerPixel = FrameInfo.GetBytesPerPixel(destinationFormat);
            byte[] convertedPixelBytes = new byte[image.Width * image.Height * bytesPerPixel];

            switch (destinationFormat)
            {
                case PixelDataFormat.OneFiveFiveFive:
                    FillBufferOneFiveFiveFiveLe(image, convertedPixelBytes);
                    break;
                case PixelDataFormat.FourFourFourFour:
                    FillBufferFourFourFourFourLe(image, convertedPixelBytes);
                    break;
                case PixelDataFormat.EightEightEightEight:
                    FillBufferEightEightEightEightLe(image, convertedPixelBytes);
                    break;
                default:
                    throw new NotImplementedException("Oops, missed a pixel format to convert to: {0}".F(destinationFormat));
            }

            return convertedPixelBytes;
            //BitmapData bits = inputImage.LockBits(new Rectangle(0, 0, inputImage.Width, inputImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="Image">Input image</param>
 /// <param name="Seed">Randomization seed</param>
 /// <param name="NumberOfPoints">Number of points for the painting</param>
 public OilPainting(Bitmap Image, int Seed, int NumberOfPoints)
 {
     Image.ThrowIfNull("Image");
     _Image = new Bitmap(Image);
     _NumberOfPoints = NumberOfPoints;
     Map = new CellularMap(Seed, Image.Width, Image.Height, NumberOfPoints);
     SetupImage();
 }
 public virtual Bitmap Apply(Bitmap OriginalImage)
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
     {
         System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
         using (ImageAttributes Attributes = new ImageAttributes())
         {
             Attributes.SetColorMatrix(NewColorMatrix);
             NewGraphics.DrawImage(OriginalImage,
                 new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
                 0, 0, OriginalImage.Width, OriginalImage.Height,
                 GraphicsUnit.Pixel,
                 Attributes);
         }
     }
     return NewBitmap;
 }
Пример #4
0
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap Image)
 {
     Image.ThrowIfNull("Image");
     CreateFilter();
     using (Bitmap TempImage = EdgeDetectionFilter.ApplyFilter(Image))
     {
         return TempImage.BlackAndWhite();
     }
 }
 /// <summary>
 /// Applies the filter to the input image
 /// </summary>
 /// <param name="Input">input image</param>
 /// <returns>Returns a separate image with the filter applied</returns>
 public virtual Bitmap ApplyFilter(Bitmap Input)
 {
     Input.ThrowIfNull("Input");
     Bitmap NewBitmap = new Bitmap(Input.Width, Input.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = Input.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width2 = Input.Width;
     int Height2 = Input.Height;
     Parallel.For(0, Width2, x =>
     {
         for (int y = 0; y < Height2; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             int Weight = 0;
             int XCurrent = -Width / 2;
             for (int x2 = 0; x2 < Width; ++x2)
             {
                 if (XCurrent + x < Width2 && XCurrent + x >= 0)
                 {
                     int YCurrent = -Height / 2;
                     for (int y2 = 0; y2 < Height; ++y2)
                     {
                         if (YCurrent + y < Height2 && YCurrent + y >= 0)
                         {
                             Color Pixel = OldData.GetPixel(XCurrent + x, YCurrent + y, OldPixelSize);
                             RValue += MyFilter[x2, y2] * Pixel.R;
                             GValue += MyFilter[x2, y2] * Pixel.G;
                             BValue += MyFilter[x2, y2] * Pixel.B;
                             Weight += MyFilter[x2, y2];
                         }
                         ++YCurrent;
                     }
                 }
                 ++XCurrent;
             }
             Color MeanPixel = OldData.GetPixel(x, y, OldPixelSize);
             if (Weight == 0)
                 Weight = 1;
             if (Weight > 0)
             {
                 if (Absolute)
                 {
                     RValue = System.Math.Abs(RValue);
                     GValue = System.Math.Abs(GValue);
                     BValue = System.Math.Abs(BValue);
                 }
                 RValue = (RValue / Weight) + Offset;
                 RValue = RValue.Clamp(255, 0);
                 GValue = (GValue / Weight) + Offset;
                 GValue = GValue.Clamp(255, 0);
                 BValue = (BValue / Weight) + Offset;
                 BValue = BValue.Clamp(255, 0);
                 MeanPixel = Color.FromArgb(RValue, GValue, BValue);
             }
             NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     Input.UnlockImage(OldData);
     return NewBitmap;
 }
 /// <summary>
 /// Loads an image
 /// </summary>
 /// <param name="ImageUsing">Image to load</param>
 public virtual void LoadImage(Bitmap ImageUsing)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     BitmapData OldData = ImageUsing.LockImage();
     int PixelSize = OldData.GetPixelSize();
     Width = ImageUsing.Width;
     Height = ImageUsing.Height;
     R.Clear();
     G.Clear();
     B.Clear();
     for (int x = 0; x < Width; ++x)
     {
         for (int y = 0; y < Height; ++y)
         {
             Color TempColor = OldData.GetPixel(x, y, PixelSize);
             ++R[(int)TempColor.R];
             ++G[(int)TempColor.G];
             ++B[(int)TempColor.B];
         }
     }
     ImageUsing.UnlockImage(OldData);
 }
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap ImageUsing)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     CreateFilter();
     using (Bitmap TempImageX = FilterX.Create(ImageUsing))
     {
         using (Bitmap TempImageY = FilterY.Create(ImageUsing))
         {
             Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height);
             BitmapData TempImageXData = TempImageX.LockImage();
             BitmapData TempImageYData = TempImageY.LockImage();
             BitmapData ReturnImageData = ReturnImage.LockImage();
             int TempImageXPixelSize = TempImageXData.GetPixelSize();
             int TempImageYPixelSize = TempImageYData.GetPixelSize();
             int ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             int Width = TempImageX.Width;
             int Height = TempImageX.Height;
             Parallel.For(0, Height, y =>
             {
                 Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
                 for (int x = 0; x < Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                         Color.FromArgb((int)TempVector.X,
                             (int)TempVector.Y,
                             (int)TempVector.Z),
                         ReturnImagePixelSize);
                 }
             });
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return ReturnImage;
         }
     }
 }
 public static Bitmap And(this Bitmap Image1, Bitmap Image2, string FileName = "")
 {
     Image1.ThrowIfNull("Image1");
     Image2.ThrowIfNull("Image2");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(Image1.Width, Image1.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData1 = Image1.LockImage();
     BitmapData OldData2 = Image2.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize1 = OldData1.GetPixelSize();
     int OldPixelSize2 = OldData2.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color Pixel1 = OldData1.GetPixel(x, y, OldPixelSize1);
             Color Pixel2 = OldData2.GetPixel(x, y, OldPixelSize2);
             NewData.SetPixel(x, y,
                 Color.FromArgb(Pixel1.R & Pixel2.R,
                     Pixel1.G & Pixel2.G,
                     Pixel1.B & Pixel2.B),
                 NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     Image1.UnlockImage(OldData1);
     Image2.UnlockImage(OldData2);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
        public static Bitmap Watermark(this Bitmap Image, Bitmap WatermarkImage, float Opacity, int X, int Y, Color KeyColor, string FileName = "")
        {
            Image.ThrowIfNull("Image");
            WatermarkImage.ThrowIfNull("WatermarkImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(Image, Image.Width, Image.Height);
            using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
            {
                float[][] FloatColorMatrix ={
                            new float[] {1, 0, 0, 0, 0},
                            new float[] {0, 1, 0, 0, 0},
                            new float[] {0, 0, 1, 0, 0},
                            new float[] {0, 0, 0, Opacity, 0},
                            new float[] {0, 0, 0, 0, 1}
                        };

                System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
                using (ImageAttributes Attributes = new ImageAttributes())
                {
                    Attributes.SetColorMatrix(NewColorMatrix);
                    if (KeyColor != null)
                    {
                        Attributes.SetColorKey(KeyColor, KeyColor);
                    }
                    NewGraphics.DrawImage(WatermarkImage,
                        new System.Drawing.Rectangle(X, Y, WatermarkImage.Width, WatermarkImage.Height),
                        0, 0, WatermarkImage.Width, WatermarkImage.Height,
                        GraphicsUnit.Pixel,
                        Attributes);
                }
            }
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
 /// <summary>
 /// Runs a simplistic motion detection algorithm
 /// </summary>
 /// <param name="NewImage">The "new" frame</param>
 /// <param name="OldImage">The "old" frame</param>
 /// <param name="Threshold">The threshold used to detect changes in the image</param>
 /// <param name="DetectionColor">Color to display changes in the images as</param>
 /// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns>
 public static Bitmap MotionDetection(this Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor)
 {
     NewImage.ThrowIfNull("NewImage");
     OldImage.ThrowIfNull("OldImage");
     DetectionColor.ThrowIfNull("DetectionColor");
     using (Bitmap NewImage1 = NewImage.BlackAndWhite())
     {
         using (Bitmap OldImage1 = OldImage.BlackAndWhite())
         {
             using (Bitmap NewImage2 = NewImage1.SNNBlur(5))
             {
                 using (Bitmap OldImage2 = OldImage1.SNNBlur(5))
                 {
                     using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height))
                     {
                         using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height))
                         {
                             BitmapData NewImage2Data = NewImage2.LockImage();
                             int NewImage2PixelSize = NewImage2Data.GetPixelSize();
                             BitmapData OldImage2Data = OldImage2.LockImage();
                             int OldImage2PixelSize = OldImage2Data.GetPixelSize();
                             BitmapData OverlayData = Overlay.LockImage();
                             int OverlayPixelSize = OverlayData.GetPixelSize();
                             int Width = OutputImage.Width;
                             int Height = OutputImage.Height;
                             Parallel.For(0, Width, x =>
                             {
                                 for (int y = 0; y < Height; ++y)
                                 {
                                     Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize);
                                     Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize);
                                     if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold)
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize);
                                     }
                                     else
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize);
                                     }
                                 }
                             });
                             Overlay.UnlockImage(OverlayData);
                             NewImage2.UnlockImage(NewImage2Data);
                             OldImage2.UnlockImage(OldImage2Data);
                             using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor))
                             {
                                 BitmapData Overlay2Data = Overlay2.LockImage();
                                 int Overlay2PixelSize = Overlay2Data.GetPixelSize();
                                 Width = OutputImage.Width;
                                 Height = OutputImage.Height;
                                 Parallel.For(0, Width, x =>
                                 {
                                     for (int y = 0; y < Height; ++y)
                                     {
                                         Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize);
                                         if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B)
                                         {
                                             Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize);
                                         }
                                     }
                                 });
                                 Overlay2.UnlockImage(Overlay2Data);
                                 return OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200));
                             }
                         }
                     }
                 }
             }
         }
     }
 }