Ejemplo n.º 1
0
        SolveResults Compute(string fileLoc, int numColors)
        {
            var rc = new SolveResults();
            Dictionary <Color, int> dictColors = new Dictionary <Color, int>();
            Dictionary <Color, List <GH_Point> > dictColorLocation = new Dictionary <Color, List <GH_Point> >();
            List <GH_Colour>        topCols     = new List <GH_Colour>();
            List <GH_Integer>       colCount    = new List <GH_Integer>();
            GH_Structure <GH_Point> colLocation = new GH_Structure <GH_Point>();


            try
            {
                using (Bitmap bitmap = new Bitmap(fileLoc))
                {
                    GH_Integer pixCount = new GH_Integer();
                    GH_Convert.ToGHInteger(bitmap.Height * bitmap.Width, 0, ref pixCount);
                    rc.PixCount = pixCount;

                    ///https://www.grasshopper3d.com/forum/topics/unsafe?page=1&commentId=2985220%3AComment%3A808291&x=1#2985220Comment808291
                    GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmap);

                    Color col = Color.Transparent;
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        for (int y = 0; y < bitmap.Height; y++)
                        {
                            ///GH_MemoryBitmap Sample is faster than GetPixel
                            //col = bitmap.GetPixel(x, y);
                            if (sampler.Sample(x, y, ref col))
                            {
                                if (!dictColors.ContainsKey(col))
                                {
                                    dictColors.Add(col, 1);
                                    //dictColorLocation.Add(col, new List<GH_Point> { new GH_Point(new Point3d(x,y,0)) });
                                }
                                else
                                {
                                    dictColors[col]++;
                                    //dictColorLocation[col].Add(new GH_Point(new Point3d(x,y,0)));
                                }
                            }
                        }
                    }

                    if (numColors > dictColors.Count || numColors <= 0)
                    {
                        numColors = dictColors.Count;
                    }

                    var sortedColorDict = (from entry in dictColors orderby entry.Value descending select entry)
                                          .Take(numColors)
                                          .ToDictionary(pair => pair.Key, pair => pair.Value);

                    //var sortedColorLocation = (from entry in dictColorLocation orderby entry.Value.Count descending select entry)
                    //    .Take(numColors)
                    //   .ToDictionary(pair => pair.Key, pair => pair.Value);

                    foreach (var clr in sortedColorDict)
                    {
                        GH_Colour gh_Col = new GH_Colour();
                        GH_Convert.ToGHColour(clr.Key, 0, ref gh_Col);
                        topCols.Add(gh_Col);

                        GH_Integer gh_Count = new GH_Integer();
                        GH_Convert.ToGHInteger(clr.Value, 0, ref gh_Count);
                        colCount.Add(gh_Count);
                    }

                    /*int i = 0;
                     * foreach (var clr in sortedColorLocation)
                     * {
                     *  GH_Path path = new GH_Path(i);
                     *  colLocation.AppendRange(clr.Value, path);
                     *  i++;
                     * }
                     */

                    sampler.Release(false);
                    bitmap.Dispose();
                }
            }

            catch
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not load image from file path: " + fileLoc);
            }

            rc.TopColors  = topCols;
            rc.ColorCount = colCount;
            //rc.ColorLocation = colLocation;

            return(rc);
        }
Ejemplo n.º 2
0
        /*******************************************/

        public static bool CastToGoo(object value, ref GH_Colour target)
        {
            return(GH_Convert.ToGHColour(value, GH_Conversion.Both, ref target));
        }