Ejemplo n.º 1
0
        public static T Generate <T>(ColorPaletteIndex index, int size) where T : ColorPaletteDisguise, new()
        {
            T t = new T();

            t.Initialize(index, size, t.RealObject.BytesPerColor);
            return(t);
        }
        ColorDictionaryToPalette <T, K>(List <ConcurrentDictionary <Int32, int> > pals, int palSize) where T : ColorPaletteDisguise, new()
            where K : ColorPaletteIndex, new()
        {
            T[]    Palettes = new T[pals.Count];
            byte[] cols;
            int    it  = 0;
            int    ind = 0;

            foreach (var p in pals)
            {
                Palettes[it] = ColorPaletteDisguise.Generate <T>(ColorPaletteIndex.Generate <K>(it, (palSize + 1) * it), (palSize + 1));

                cols = new byte[(palSize + 1) * 4];

                cols[0] = 0;
                cols[1] = 0;
                cols[2] = 0;
                cols[3] = 0;
                ind     = 4;
                foreach (var kvp in p)
                {
                    cols[ind]     = (byte)((kvp.Key >> 24) & 0x000000FF);
                    cols[ind + 1] = (byte)((kvp.Key >> 16) & 0x000000FF);
                    cols[ind + 2] = (byte)((kvp.Key >> 8) & 0x000000FF);
                    cols[ind + 3] = (byte)(kvp.Key & 0x000000FF);
                    ind          += 4;
                }

                Palettes[it].RealObject.Load(cols);

                it++;
            }

            return(Palettes);
        }
Ejemplo n.º 3
0
 public void Initialize(ColorPaletteIndex index, int size, BytesPerPixel bpp)
 {
     RealObject = DressUp(index, size, bpp);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ColorPaletteDisguise"/> class.
 /// </summary>
 /// <param name="args">The args.</param>
 protected ColorPaletteDisguise(ColorPaletteIndex index, int size, BytesPerPixel bpp) : base(index, size, bpp)
 {
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SNESColorPalette"/> class.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="size">The size.</param>
 public SNESColorPalette(ColorPaletteIndex index, int size) : base(index, size, BytesPerPixel.RGB555)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ColorPalette"/> class.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="size">The size.</param>
 public ColorPalette(ColorPaletteIndex index, int size, BytesPerPixel bpp) : base(index, size, bpp)
 {
 }
Ejemplo n.º 7
0
        public static T ReduceColorsFromBitmap <T, K>(int maxSize, Int32[,] bp) where T : ColorPaletteDisguise, new()
            where K : ColorPaletteIndex, new()
        {
            ConcurrentDictionary <int, int> cols = ExtractColors(bp);

            List <ColorGroup> clsgrs = new List <ColorGroup>();

            foreach (var kvp in cols)
            {
                clsgrs.Add(new ColorGroup(kvp.Key, kvp.Value));
            }

            clsgrs = HierarchicalClusteringSolver <ColorGroup, ColorReductionClusterNode> .Solve(clsgrs, 100);

            IntervalHeap <ColorGroup> ih = new IntervalHeap <ColorGroup>();

            ih.Add(clsgrs[0]);

            ColorGroup        aux = null;
            List <ColorGroup> ret = new List <ColorGroup>();

            while (ih.Count + ret.Count < maxSize)
            {
                aux = ih.DeleteMax();

                if (aux.LeftSon != null && aux.RightSon != null)
                {
                    ih.Add(aux.LeftSon);
                    ih.Add(aux.RightSon);
                }
                else
                {
                    ret.Add(aux);
                }
            }

            foreach (var cg in ih)
            {
                ret.Add(cg);
            }
            Int32[]  Bits       = new Int32[bp.GetLength(0) * bp.GetLength(1)];
            GCHandle BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
            Bitmap   bitm       = new Bitmap(bp.GetLength(0), bp.GetLength(1), bp.GetLength(0) * 4, PixelFormat.Format32bppArgb,
                                             BitsHandle.AddrOfPinnedObject());

            Parallel.For(0, bp.GetLength(0), x =>
            {
                Parallel.For(0, bp.GetLength(1), y =>
                {
                    ColorGroup cg     = new ColorGroup(bp[x, y], 1);
                    int mind          = int.MaxValue;
                    ColorGroup minval = null;
                    int curd          = 0;
                    foreach (var c in ret)
                    {
                        curd = c.Distance(cg);
                        if (curd < mind)
                        {
                            mind   = curd;
                            minval = c;
                        }
                    }

                    Bits[x + (y * bp.GetLength(1))] = (minval.A << 24) + (minval.R << 16) + (minval.G << 8) + (minval.B);
                });
            });

            bitm.Save("Try.png");

            T res = ColorPaletteDisguise.Generate <T>(ColorPaletteIndex.Generate <K>(0, 0), maxSize + 1);

            byte[] newcols = new byte[(maxSize + 1) * 4];

            int i = 4;

            foreach (var c in ret)
            {
                newcols[i]     = (byte)c.A;
                newcols[i + 1] = (byte)c.R;
                newcols[i + 2] = (byte)c.G;
                newcols[i + 3] = (byte)c.B;
                i += 4;
            }

            res.RealObject.Load(newcols);

            return(res);
        }