/// <summary> /// creates a palette of colors from a bitmap /// </summary> public unsafe static Octree FromBitmap(Bitmap bmp, int maxcolors) { if (bmp == null || bmp.PixelFormat != PixelFormat.Format32bppArgb) { throw new ArgumentException("bmp not valid. use copyimage()"); } if (maxcolors < 2 || maxcolors > 256) { throw new ArgumentException("length of colors not valid", "value"); } //no exact mapping of colors Octree ret = new Octree(5, maxcolors); #region adding bitmap //lock bitmap BitmapData bdata = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int count = bmp.Width * bmp.Height; ColorBgra *pixels = (ColorBgra *)bdata.Scan0; //loop through pixels for (int i = 0; i < count; i++) { ret.AddColor(pixels[i]); while (ret._colors > ret._maxcolors) { ret.ReduceTree(); } } bmp.UnlockBits(bdata); #endregion //make palette ret._table = new ColorBgra[maxcolors]; int index = 0; ret._root.AddToTable(ret._table, ref index); //return octree return(ret); }
/// <summary> /// creates an adaptive palette based on the data in the /// specified bitmap /// </summary> /// <param name="bmp">a bitmap containing the colors. use only 32bppArgb images</param> public void MakePalette(Bitmap bmp) { _octree = Octree.FromBitmap(bmp, _maxcolors); }