internal virtual void InvalidateImage()
 {
     if (!lockBackBuffer)
     {
         backBuffer = null;
     }
 }
示例#2
0
        private static Bitmap TrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect   = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                rect = TrimTransparentFindX(unsafeBitmap, rect);
                rect = TrimTransparentFindY(unsafeBitmap, rect);
                rect = TrimTransparentFindWidth(unsafeBitmap, rect);
                rect = TrimTransparentFindHeight(unsafeBitmap, rect);
            }

            if (source != rect)
            {
                Bitmap croppedBitmap = ImageHelpers.CropBitmap(bitmap, rect);

                if (croppedBitmap != null)
                {
                    bitmap.Dispose();
                    return(croppedBitmap);
                }
            }

            return(bitmap);
        }
示例#3
0
        public override Bitmap Apply(Bitmap bmp)
        {
            Bitmap bmpResult = bmp.CreateEmptyBitmap();

            using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
                {
                    for (int y = 0; y < dest.Height; y++)
                    {
                        for (int x = 0; x < dest.Width; x++)
                        {
                            ColorBgra colorR = source.GetPixel(MathHelpers.Clamp(x + OffsetRed.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetRed.Y, 0, dest.Height - 1));
                            ColorBgra colorG = source.GetPixel(MathHelpers.Clamp(x + OffsetGreen.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetGreen.Y, 0, dest.Height - 1));
                            ColorBgra colorB = source.GetPixel(MathHelpers.Clamp(x + OffsetBlue.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetBlue.Y, 0, dest.Height - 1));

                            byte colorR_alpha = colorR.Alpha;
                            byte colorG_alpha = colorG.Alpha;
                            byte colorB_alpha = colorB.Alpha;
                            byte colorA       = (byte)(colorR_alpha / 3 + colorG_alpha / 3 + colorB_alpha / 3);

                            ColorBgra shiftedColor = new ColorBgra((byte)(colorB.Blue * colorB_alpha / 255), (byte)(colorG.Green * colorG_alpha / 255),
                                                                   (byte)(colorR.Red * colorR_alpha / 255), colorA);
                            dest.SetPixel(x, y, shiftedColor);
                        }
                    }
                }

            return(bmpResult);
        }
示例#4
0
        public override Bitmap Apply(Bitmap bmp)
        {
            Bitmap bmpResult = bmp.CreateEmptyBitmap();

            using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
                {
                    int right  = source.Width - 1;
                    int bottom = source.Height - 1;

                    for (int y = 0; y < source.Height; y++)
                    {
                        for (int x = 0; x < source.Width; x++)
                        {
                            ColorBgra colorR       = source.GetPixel(MathHelpers.Clamp(x - OffsetRed.X, 0, right), MathHelpers.Clamp(y - OffsetRed.Y, 0, bottom));
                            ColorBgra colorG       = source.GetPixel(MathHelpers.Clamp(x - OffsetGreen.X, 0, right), MathHelpers.Clamp(y - OffsetGreen.Y, 0, bottom));
                            ColorBgra colorB       = source.GetPixel(MathHelpers.Clamp(x - OffsetBlue.X, 0, right), MathHelpers.Clamp(y - OffsetBlue.Y, 0, bottom));
                            ColorBgra shiftedColor = new ColorBgra((byte)(colorB.Blue * colorB.Alpha / 255), (byte)(colorG.Green * colorG.Alpha / 255),
                                                                   (byte)(colorR.Red * colorR.Alpha / 255), (byte)((colorR.Alpha + colorG.Alpha + colorB.Alpha) / 3));
                            dest.SetPixel(x, y, shiftedColor);
                        }
                    }
                }

            return(bmpResult);
        }
示例#5
0
        /// <summary>
        /// Creates a Transparent Bitmap from a combination of a Bitmap on a White Background and another on a Black Background
        /// </summary>
        internal static unsafe Bitmap DifferentiateAlpha(Bitmap WhiteBitmap, Bitmap BlackBitmap)
        {
            if (WhiteBitmap == null || BlackBitmap == null ||
                WhiteBitmap.Width != BlackBitmap.Width ||
                WhiteBitmap.Height != BlackBitmap.Height)
            {
                return(null);
            }

            int sizeX = WhiteBitmap.Width,
                sizeY = WhiteBitmap.Height;

            var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);

            var empty = true;

            using (var a = new UnsafeBitmap(WhiteBitmap))
            {
                using (var b = new UnsafeBitmap(BlackBitmap))
                {
                    using (var f = new UnsafeBitmap(final))
                    {
                        for (int x = 0, y = 0; x < sizeX && y < sizeY;)
                        {
                            PixelData *pixelA = a[x, y],
                                      pixelB  = b[x, y],
                                      pixelF  = f[x, y];

                            pixelF->Alpha = ToByte((pixelB->Red - pixelA->Red + 255 + pixelB->Green -
                                                    pixelA->Green + 255 + pixelB->Blue - pixelA->Blue +
                                                    255) / 3);

                            if (pixelF->Alpha > 0)
                            {
                                // Following math creates an image optimized to be displayed on a black background
                                pixelF->Red   = ToByte(255 * pixelB->Red / pixelF->Alpha);
                                pixelF->Green = ToByte(255 * pixelB->Green / pixelF->Alpha);
                                pixelF->Blue  = ToByte(255 * pixelB->Blue / pixelF->Alpha);
                            }

                            if (empty && pixelF->Alpha > 0)
                            {
                                empty = false;
                            }

                            if (x == sizeX - 1)
                            {
                                y++;
                                x = 0;
                                continue;
                            }
                            x++;
                        }
                    }
                }
            }

            return(empty ? null : final);
        }
示例#6
0
        private static Bitmap QuickTrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect   = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                int middleX = rect.Width / 2;
                int middleY = rect.Height / 2;

                // Find X
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.X = x;
                        break;
                    }
                }

                // Find Y
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Y = y;
                        break;
                    }
                }

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X; x--)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        break;
                    }
                }

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y; y--)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        break;
                    }
                }
            }

            if (source != rect)
            {
                return(ImageHelpers.CropBitmap(bitmap, rect));
            }

            return(bitmap);
        }
        public List <bloc> analyse(List <bloc> liste_bloc1, List <bloc> liste_bloc2)
        {
            a = new UnsafeBitmap(image1);
            b = new UnsafeBitmap(image2);
            bloc        rst      = new bloc();
            List <bloc> resultat = new List <bloc>();
            //   List<bloc> liste_bloc1= new List<bloc>();
            //   List<bloc> liste_bloc2= new List<bloc>();
            //   liste_bloc1= decoupage(image1);
            //  liste_bloc2= decoupage(image2);
            DateTime start = DateTime.Now;

            //  MessageBox.Show(liste_bloc1.Count.ToString());
            a.LockBitmap();
            b.LockBitmap();
            object o = new object();

            zmp(ref liste_bloc1, ref liste_bloc2);

            //   MessageBox.Show(liste_bloc1.Count.ToString());
            AForge.Parallel.For(0, liste_bloc1.Count, delegate(int i)
                                //   for(int i=0;i<liste_bloc1.Count;i++)
            {
                rst = diamond_search(liste_bloc1[i]);

                if (lst_contain(liste_bloc1, rst) == true)
                {
                    lock (o)
                    {
                        if ((rst.x != liste_bloc1[i].x) || (rst.y != liste_bloc1[i].y)) //MessageBox.Show("kifkif");
                        {
                            resultat.Add(rst);
                        }
                        //            sad_moyen += SAD(liste_bloc1[i], rst);
                        //          compteur_sad++;
                        // MessageBox.Show(SAD(liste_bloc1[i],rst).ToString());
                        //sad_moyen.Add(SAD(liste_bloc1[i], rst));
                    }
                }
            });


            //  sad_moyen = sad_moyen / compteur_sad;
            //);
            a.UnlockBitmap();
            b.UnlockBitmap();
            //fic_ecr.Close();
            //sady.Close();
            //sadt.Close();
            // MessageBox.Show(sad_moyen.Average().ToString());
            TimeSpan dur = DateTime.Now - start;

            //  MessageBox.Show(dur.ToString());
            return(resultat);
        }
示例#8
0
        private int GenerateStrokesWithRawScaling(Bitmap input, ColorImage area, ArrayList list, int S, int factor, int level)
        {
            int x, y;

            System.Console.WriteLine("Generating stroke positions image.");

            /*
             * Bitmap areascaled = new Bitmap(area.Width / factor, area.Height / factor);
             * Graphics g = Graphics.FromImage(areascaled);
             * g.ScaleTransform(1.0f / (float)factor, 1.0f / (float)factor);
             * g.DrawImage(area, 0, 0, area.Width, area.Height);
             * areascaled.Save("areascaled-" + factor.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
             * ColorImage img = new ColorImage(areascaled);
             */

            ColorImage scaledWidth = ColorImage.ScaleWidth(area, area.Width / factor);
            ColorImage img         = ColorImage.ScaleHeight(scaledWidth, area.Height / factor);

            this.dither = new FloydSteinbergDither(4 * S / System.Math.Sqrt(level), 2.0f / level);

            UnsafeBitmap dithered = new UnsafeBitmap(this.dither.Dither(img));

            //StatusReporter.Instance.SubmitWorkingImage(ImageTools.GenWorkingImage(dithered));
            //dithered.Save("dithered-" + factor.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

            Bitmap   inscaled = new Bitmap(input.Width / factor, input.Height / factor);
            Graphics g2       = Graphics.FromImage(inscaled);

            g2.ScaleTransform(1.0f / (float)factor, 1.0f / (float)factor);
            g2.DrawImage(input, 0, 0, input.Width, input.Height);
            //inscaled.Save("inputscaled-" + factor.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            System.Console.WriteLine("Collecting strokes.");

            int maxY = dithered.Height;
            int maxX = dithered.Width;

            ColorImage inScaled = new ColorImage(new UnsafeBitmap(inscaled));              // should be &inputscaled

            for (y = 0; y < maxY; y++)
            {
                for (x = 0; x < maxX; x++)
                {
                    if (dithered.GetPixel(x, y).R == 0)
                    {
                        //StatusReporter.Instance.SubmitWorkingImage(ImageTools.GenWorkingImage(dithered, x, y, S, S));
                        Stroke s = GenerateStroke(inScaled, x, y, S, factor, level);
                        list.Add(s);
                    }
                }
            }

            return(list.Count);
        }
示例#9
0
        public void Iterate()
        {
            _colourClusterAllocation    = new Hashtable(); //for keeping track of colour<->cluster allocation
            _pixelDataClusterAllocation = new Hashtable();
            _clusterColours             = new Hashtable();

            UnsafeBitmap fastBitmap = new UnsafeBitmap(_image);

            fastBitmap.LockBitmap();
            Point size = fastBitmap.Size;
            BGRA *pPixel;

            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    PixelData pd = Colour.GetPixelData(pPixel, _model);
                    AllocateToCluster(pd);

                    //increment the pointer
                    pPixel++;
                }
            }
            fastBitmap.UnlockBitmap();

            CalculateClusterCentroids();

            _processedImage = (Bitmap)_image.Clone();

            //segment the image based on the cluster
            fastBitmap = new UnsafeBitmap(_processedImage);
            fastBitmap.LockBitmap();
            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    PixelData pd     = Colour.GetPixelData(pPixel, _model);
                    Color     newClr = (Color)_clusterColours[pd.Name];

                    pPixel->red   = newClr.R;
                    pPixel->green = newClr.G;
                    pPixel->blue  = newClr.B;

                    //increment the pointer
                    pPixel++;
                }
            }

            fastBitmap.UnlockBitmap();

            CheckConvergence();
        }
示例#10
0
        private void FindTopXColours(int numColours)
        {
            Dictionary <string, ColourCount> colours = new Dictionary <string, ColourCount>();
            UnsafeBitmap fastBitmap = new UnsafeBitmap(_image);

            fastBitmap.LockBitmap();
            Point size = fastBitmap.Size;
            BGRA *pPixel;

            for (int y = 0; y < size.Y; y++)
            {
                pPixel = fastBitmap[0, y];
                for (int x = 0; x < size.X; x++)
                {
                    //get the bin index for the current pixel colour
                    Color clr = Color.FromArgb(pPixel->red, pPixel->green, pPixel->blue);

                    if (colours.ContainsKey(clr.Name))
                    {
                        ((ColourCount)colours[clr.Name]).Count++;
                    }
                    else
                    {
                        colours.Add(clr.Name, new ColourCount(clr, 1));
                    }

                    //increment the pointer
                    pPixel++;
                }
            }

            fastBitmap.UnlockBitmap();

            //instantiate using actual colours found - which might be less than numColours
            if (colours.Count < numColours)
            {
                numColours = colours.Count;
            }

            _topColours = new Color[numColours];

            List <KeyValuePair <string, ColourCount> > summaryList = new List <KeyValuePair <string, ColourCount> >();

            summaryList.AddRange(colours);

            summaryList.Sort(delegate(KeyValuePair <string, ColourCount> kvp1, KeyValuePair <string, ColourCount> kvp2)
                             { return(Comparer <int> .Default.Compare(kvp2.Value.Count, kvp1.Value.Count)); });


            for (int i = 0; i < _topColours.Length; i++)
            {
                _topColours[i] = Color.FromArgb(summaryList[i].Value.Colour.R, summaryList[i].Value.Colour.G, summaryList[i].Value.Colour.B);
            }
        }
示例#11
0
 //constructeur
 public fctcommunes(Bitmap _im1, Bitmap _im2, int t_bloc, int d_fenetre, double _seuil)
 {
     image1      = _im1;
     image2      = _im2;
     a           = new UnsafeBitmap(_im1);
     b           = new UnsafeBitmap(_im2);
     taille_bloc = t_bloc;
     deplace_fen = d_fenetre;
     he          = image1.Height;
     wi          = image1.Width;
     seuil       = _seuil;
 }
示例#12
0
        public void DrawBitmap(Bitmap bmp)
        {
            UnsafeBitmap ubmp = new UnsafeBitmap(bmp);

            ubmp.LockBitmap();
            try
            {
                DrawBitmap(ubmp);
            }
            finally
            {
                ubmp.UnlockBitmap();
            }
        }
示例#13
0
        /// <summary>
        /// Creates a Transparent Bitmap from a combination of a Bitmap on a White Background and another on a Black Background
        /// </summary>
        internal static unsafe Bitmap DifferentiateAlpha(Bitmap WhiteBitmap, Bitmap BlackBitmap)
        {
            if (WhiteBitmap == null || BlackBitmap == null || WhiteBitmap.Size != BlackBitmap.Size)
            {
                return(null);
            }

            int sizeX = WhiteBitmap.Width,
                sizeY = WhiteBitmap.Height;

            var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);

            var empty = true;

            using (var a = new UnsafeBitmap(WhiteBitmap))
                using (var b = new UnsafeBitmap(BlackBitmap))
                    using (var f = new UnsafeBitmap(final))
                    {
                        byte ToByte(int I) => (byte)(I > 255 ? 255 : (I < 0 ? 0 : I));

                        for (var y = 0; y < sizeY; ++y)
                        {
                            for (var x = 0; x < sizeX; ++x)
                            {
                                PixelData *pixelA = a[x, y],
                                          pixelB  = b[x, y],
                                          pixelF  = f[x, y];

                                pixelF->Alpha = ToByte((pixelB->Red - pixelA->Red + 255 + pixelB->Green -
                                                        pixelA->Green + 255 + pixelB->Blue - pixelA->Blue +
                                                        255) / 3);

                                if (pixelF->Alpha > 0)
                                {
                                    // Following math creates an image optimized to be displayed on a black background
                                    pixelF->Red   = ToByte(255 * pixelB->Red / pixelF->Alpha);
                                    pixelF->Green = ToByte(255 * pixelB->Green / pixelF->Alpha);
                                    pixelF->Blue  = ToByte(255 * pixelB->Blue / pixelF->Alpha);

                                    if (empty)
                                    {
                                        empty = false;
                                    }
                                }
                            }
                        }
                    }

            return(empty ? null : final);
        }
示例#14
0
 public Bitmap CreatePainting(Bitmap originalImage)
 {
     using (UnsafeBitmap unsafeOriginal = new UnsafeBitmap(originalImage))
     {
         using (UnsafeBitmap unsafeBrush = new UnsafeBitmap(Properties.Resources.brush1))
         {
             using (UnsafeBitmap unsafePainting = StrokeAreaGenerator.GeneratePainting(unsafeOriginal, unsafeBrush, this.windowSize))
             {
                 unsafePainting.UnlockBitmap();
                 Bitmap finalPainting = new Bitmap(unsafePainting.Bitmap);
                 return(finalPainting);
             }
         }
     }
 }
示例#15
0
        protected override Bitmap CreatePainting()
        {
            // Transform the original images to the fast and unsafe bitmaps we require
            List <UnsafeBitmap> unsafeOriginals = new List <UnsafeBitmap>();

            foreach (Bitmap original in this.originals)
            {
                unsafeOriginals.Add(new UnsafeBitmap(new Bitmap(original)));
            }

            List <UnsafeBitmap> unsafeMovieIntroFrames = new List <UnsafeBitmap>();

            if (this.introMovieFrames != null)
            {
                foreach (Bitmap originalIntroFrame in this.introMovieFrames)
                {
                    unsafeMovieIntroFrames.Add(new UnsafeBitmap(new Bitmap(originalIntroFrame)));
                }
            }

            try
            {
                using (UnsafeBitmap unsafePainting = CompositeNativeMomentPainterWrapper.CreatePainting(width, height, unsafeOriginals.ToArray(), this.locations, BrushCollection.Instance.AvailableBrushes, this.windowSize, this.generateMovie, unsafeMovieIntroFrames.ToArray(), this.MovieFilename))
                {
                    unsafePainting.UnlockBitmap();
                    return(new Bitmap(unsafePainting.Bitmap));
                }
            }
            finally
            {
                // We're done with the unsafe originals, free them up.  This will also
                // dispose of the originals
                foreach (UnsafeBitmap unsafeOriginal in unsafeOriginals)
                {
                    unsafeOriginal.Dispose();
                }

                // Dispose of the unsafe movie title frame, if it was created
                foreach (UnsafeBitmap unsafeIntroFrame in unsafeMovieIntroFrames)
                {
                    unsafeIntroFrame.Dispose();
                }

                // The originals were disposed of when the unsafe wrappers were
                // disposed of.  So, simply null out this collection.
                this.originals = null;
            }
        }
示例#16
0
        private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Y; y < rect.Height; y++)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Y = y;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
示例#17
0
        private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Height - 1; y >= rect.Y; y--)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
        private BrushCollection()
        {
            brushes = new UnsafeBitmap[3];

            //brushes[0] = new UnsafeBitmap(new Bitmap(Properties.Resources.brush1));
            //brushes[1] = new UnsafeBitmap(new Bitmap(Properties.Resources.brush2));
            //brushes[2] = new UnsafeBitmap(new Bitmap(Properties.Resources.brush3));

            brushes[0] = new UnsafeBitmap(new Bitmap(Properties.Resources.fatbrush));
            brushes[1] = new UnsafeBitmap(new Bitmap(Properties.Resources.fatbrush));
            brushes[2] = new UnsafeBitmap(new Bitmap(Properties.Resources.fatbrush));

            //brushes[3] = new UnsafeBitmap(new Bitmap(Properties.Resources.b1));
            //brushes[4] = new UnsafeBitmap(new Bitmap(Properties.Resources.b2));
            //brushes[5] = new UnsafeBitmap(new Bitmap(Properties.Resources.b4));
        }
示例#19
0
        public PSO(Bitmap _im1, Bitmap _im2, int _tBloc, int _fen, int _nbrgen, int _nbpar, double _seuil) : base(_im1, _im2, _tBloc, _fen, _seuil)
        {
            im1         = _im1;
            im2         = _im2;
            taille_bloc = _tBloc;
            fenetre     = _fen * 2;
            nbgen       = _nbrgen;
            nbpar       = _nbpar;
            a           = new UnsafeBitmap(im1);
            b           = new UnsafeBitmap(im2);

            h     = im1.Height - 1;
            w     = im1.Width - 1;
            c1    = calcul_c1();
            c_max = calcule_cmax(c1);
        }
示例#20
0
        private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int x = rect.Width - 1; x >= rect.X; x--)
            {
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        return(rect);
                    }
                }
            }

            return(rect);
        }
示例#21
0
        public ColorImage(UnsafeBitmap img)
        {
            DateTime start = DateTime.Now;

            this.Width  = img.Width;
            this.Height = img.Height;

            this.singledata = new Color[this.Height * this.Width];

            for (int j = 0; j < this.Height; j++)
            {
                for (int i = 0; i < this.Width; i++)
                {
                    this.singledata[j * this.Width + i] = img.GetPixel(i, j);
                }
            }
        }
        public static double[] ExtractGreyValues(UnsafeBitmap bitmap)
        {
            double[] data = new double[bitmap.Height * bitmap.Width];

            int count = 0;

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color  c      = bitmap.GetPixel(x, y);
                    double dvalue = (0.299 * c.R) + (0.587 * c.G) + (0.114 * c.B);
                    data[count++] = dvalue;
                }
            }

            return(data);
        }
        protected override Bitmap CreatePainting()
        {
            /*
             * renderingInProgress = true;
             *
             * ThreadPool.QueueUserWorkItem(new WaitCallback(StatusUpdater));
             */

            // Transform the original images to the fast and unsafe bitmaps we require
            List <UnsafeBitmap> unsafeOriginals = new List <UnsafeBitmap>();

            foreach (Bitmap original in originals)
            {
                unsafeOriginals.Add(new UnsafeBitmap(new Bitmap(original)));
            }

            try
            {
                using (UnsafeBitmap unsafeBrush = new UnsafeBitmap(new Bitmap(Properties.Resources.brush1)))
                {
                    using (UnsafeBitmap unsafePainting = NativeMomentPainterWrapper.GenerateSegmentedCompositePainting(width, height, unsafeOriginals.ToArray(), BrushCollection.Instance.AvailableBrushes, this.windowSize))
                    {
                        unsafePainting.UnlockBitmap();
                        return(new Bitmap(unsafePainting.Bitmap));
                    }
                }
            }
            finally
            {
                //renderingInProgress = false;

                // We're done with the unsafe originals, free them up
                foreach (UnsafeBitmap unsafeOriginal in unsafeOriginals)
                {
                    unsafeOriginal.Dispose();
                }

                // The originals were disposed of when the unsafe wrappers were
                // disposed of.  So, simply null out this collection.
                this.originals = null;
            }
        }
示例#24
0
        private void CreateSecondMark(int scrollBarWid)
        {
            int          w = scrollBarWid - 1;
            int          h = scrollBarWid - 2;
            UnsafeBitmap b = new UnsafeBitmap(w, h);

            b.LockBitmap();
            FormUtil.FillShadedRectangle(b, w, h);
            UnsafeBitmap bh = b.Lighter();
            UnsafeBitmap bp = b.Darker();

            b.UnlockBitmap();
            bh.UnlockBitmap();
            bp.UnlockBitmap();
            secondMark = b.Bitmap;
            secondMark.MakeTransparent();
            secondMarkHighlight = bh.Bitmap;
            secondMarkHighlight.MakeTransparent();
            secondMarkPress = bp.Bitmap;
            secondMarkPress.MakeTransparent();
        }
示例#25
0
        private void CreateBar(int scrollBarWid, int width)
        {
            int          w = CalcBarSize(width);
            int          h = scrollBarWid - 2;
            UnsafeBitmap b = new UnsafeBitmap(w, h);

            b.LockBitmap();
            FormUtil.FillShadedRectangle(b, w, h);
            UnsafeBitmap bh = b.Lighter();
            UnsafeBitmap bp = b.Darker();

            b.UnlockBitmap();
            bh.UnlockBitmap();
            bp.UnlockBitmap();
            bar = b.Bitmap;
            bar.MakeTransparent();
            barHighlight = bh.Bitmap;
            barHighlight.MakeTransparent();
            barPress = bp.Bitmap;
            barPress.MakeTransparent();
        }
示例#26
0
        private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                    using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                        using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                        {
                            int pixelCount = blackBitmap.PixelCount;

                            for (int i = 0; i < pixelCount; i++)
                            {
                                ColorBgra white = whiteBitmap.GetPixel(i);
                                ColorBgra black = blackBitmap.GetPixel(i);

                                double alpha = (black.Red - white.Red + 255) / 255.0;

                                if (alpha == 1)
                                {
                                    resultBitmap.SetPixel(i, white);
                                }
                                else if (alpha > 0)
                                {
                                    white.Blue  = (byte)(black.Blue / alpha);
                                    white.Green = (byte)(black.Green / alpha);
                                    white.Red   = (byte)(black.Red / alpha);
                                    white.Alpha = (byte)(255 * alpha);

                                    resultBitmap.SetPixel(i, white);
                                }
                            }
                        }

                return(result);
            }

            return(whiteBackground);
        }
        public Bitmap ForwardTransformWorks(UnsafeBitmap img, double[] datax, double[] low, double[] high)
        {
            double[] data  = ExtractGreyValues(img);
            double[] tdata = new double[data.Length];

            wfltr_convolve(true, data, 0, 1, data.Length, tdata, 0);

            int half = data.Length / 2;

            low  = new double[half];
            high = new double[half];
            for (int i = 0; i < data.Length / 2; i++)
            {
                low[i]  = tdata[i];
                high[i] = tdata[half + i];
            }

            Bitmap bitmap = null; //GenerateImage(low, img.Width / 2, img.Height / 2);

            /*
             * Bitmap bitmap = new Bitmap(width, height);
             * double tValue;
             * int count = 0;
             * int value;
             * for (int y = 0; y < height; y++)
             * {
             *  for (int x = 0; x < width; x++)
             *  {
             *      // Very simple modification to keep data within specified range.
             *      tValue = low[count++];
             *      //if (tValue < 0) tValue = Math.Abs(tValue);
             *      if (tValue > 255) value = 255;
             *      else value = (int)tValue;  // Throw away precision
             *      bitmap.SetPixel(x, y, Color.FromArgb(value, value, value));
             *  }
             * }
             */
            return(bitmap);
        }
示例#28
0
        // StreamWriter fich;

        public genetique(Bitmap _im1, Bitmap _im2, int t_bloc, int d_fenetre, int _nbchro, int _nbgen, double _seuil)
            : base(_im1, _im2, t_bloc, d_fenetre, _seuil)
        {
            // //MessageBox.Show(" cv");
            // fich = new StreamWriter(".\\fich.txt");
            image1 = _im1;
            image2 = _im2;
            ima1   = _im1;
            ima2   = _im2;
            h      = image1.Height;
            w      = image1.Width;
            a      = new UnsafeBitmap(image1);
            b      = new UnsafeBitmap(image2);
            //a.LockBitmap();
            //b.LockBitmap();
            taille_bloc = t_bloc;
            deplace_fen = d_fenetre;
            nbchro      = _nbchro;
            nbgen       = _nbgen;
            //  zm = _im1;
            liste_bloc1 = new List <bloc>();
            liste_bloc2 = new List <bloc>();
        }
示例#29
0
        public Bitmap CreatePainting(int width, int height, Bitmap[] originals, Point[] locations)
        {
            renderingInProgress = true;

            ThreadPool.QueueUserWorkItem(new WaitCallback(StatusUpdater));

            // Transform the original images to the fast and unsafe bitmaps we require
            List <UnsafeBitmap> unsafeOriginals = new List <UnsafeBitmap>();

            foreach (Bitmap original in originals)
            {
                unsafeOriginals.Add(new UnsafeBitmap(original));
            }

            try
            {
                using (UnsafeBitmap unsafeBrush = new UnsafeBitmap(Properties.Resources.brush1))
                {
                    using (UnsafeBitmap unsafePainting = StrokeAreaGenerator.GenerateCompositePainting(width, height, unsafeOriginals.ToArray(), locations, BrushCollection.Instance.AvailableBrushes, this.windowSize))
                    {
                        unsafePainting.UnlockBitmap();
                        Bitmap finalPainting = new Bitmap(unsafePainting.Bitmap);
                        return(finalPainting);
                    }
                }
            }
            finally
            {
                renderingInProgress = false;

                // We're done with the unsafe originals, free them up
                foreach (UnsafeBitmap unsafeOriginal in unsafeOriginals)
                {
                    unsafeOriginal.Dispose();
                }
            }
        }
        //  object proteger_inc;
        //public static double sad_moyen = 0;
        //public static Binding s;
        //public static List<double> li_sad = new List<double>();

        public class_abeilles(int _m, int _e, int _nbe, int _nba, int _nbgen, int _n, Bitmap im1, Bitmap im2, int _fenetre, int _taille_bloc, double _seuil)
            : base(im1, im2, _taille_bloc, _fenetre, _seuil)
        {
            image1      = im1;
            image2      = im2;
            h           = image1.Height;
            w           = image1.Width;
            m           = _m;
            n           = _n;
            e           = _e;
            nbe         = _nbe;
            nba         = _nba;
            nbgen       = _nbgen;
            fenetre     = _fenetre * 2;
            taille_bloc = _taille_bloc;
            a           = new UnsafeBitmap(im1);
            b           = new UnsafeBitmap(im2);
            // seuil = 255;
            //verif = new List<bloc>();
            // zm = im1;
            proteger_abeille = new object();
            //  proteger_inc = new object();
            // s = new Binding("Text", sad_moyen, "abeilles_vacances.class_abeilles.sad_moyen");
        }
        private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int pixelCount = blackBitmap.PixelCount;

                    for (int i = 0; i < pixelCount; i++)
                    {
                        ColorBgra white = whiteBitmap.GetPixel(i);
                        ColorBgra black = blackBitmap.GetPixel(i);

                        double alpha = (black.Red - white.Red + 255) / 255.0;

                        if (alpha == 1)
                        {
                            resultBitmap.SetPixel(i, white);
                        }
                        else if (alpha > 0)
                        {
                            white.Blue = (byte)(black.Blue / alpha);
                            white.Green = (byte)(black.Green / alpha);
                            white.Red = (byte)(black.Red / alpha);
                            white.Alpha = (byte)(255 * alpha);

                            resultBitmap.SetPixel(i, white);
                        }
                    }
                }

                return result;
            }

            return whiteBackground;
        }
 private static bool IsImagesEqual(Bitmap bmp1, Bitmap bmp2)
 {
     using (UnsafeBitmap unsafeBitmap1 = new UnsafeBitmap(bmp1))
     using (UnsafeBitmap unsafeBitmap2 = new UnsafeBitmap(bmp2))
     {
         return unsafeBitmap1 == unsafeBitmap2;
     }
 }
        private static Bitmap QuickTrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                int middleX = rect.Width / 2;
                int middleY = rect.Height / 2;

                // Find X
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.X = x;
                        break;
                    }
                }

                // Find Y
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Y = y;
                        break;
                    }
                }

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X; x--)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        break;
                    }
                }

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y; y--)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        break;
                    }
                }
            }

            if (source != rect)
            {
                return CaptureHelpers.CropBitmap(bitmap, rect);
            }

            return bitmap;
        }
        private static void TrimShadow(Bitmap bitmap)
        {
            int sizeLimit = 10;
            int alphaLimit = 200;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadWrite))
            {
                for (int i = 0; i < sizeLimit; i++)
                {
                    int y = i;
                    int width = bitmap.Width;

                    // Left top
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right top
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    y = bitmap.Height - i - 1;

                    // Left bottom
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right bottom
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        private static Bitmap TrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                rect = TrimTransparentFindX(unsafeBitmap, rect);
                rect = TrimTransparentFindY(unsafeBitmap, rect);
                rect = TrimTransparentFindWidth(unsafeBitmap, rect);
                rect = TrimTransparentFindHeight(unsafeBitmap, rect);
            }

            if (source != rect)
            {
                return CaptureHelpers.CropBitmap(bitmap, rect);
            }

            return bitmap;
        }
        private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Height - 1; y >= rect.Y; y--)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
        private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int x = rect.Width - 1; x >= rect.X; x--)
            {
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
        private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Y; y < rect.Height; y++)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Y = y;
                        return rect;
                    }
                }
            }

            return rect;
        }