Exemplo n.º 1
0
        public static BitmapSource ApplyMedianCut(BitmapSource bitmap, object parameter)
        {
            var paletteSize = (int)parameter;

            Vector <byte>[] palette;
            return(bitmap.Create(MedianCut.Build(paletteSize, bitmap.GetColors(), out palette)));
        }
        /// <summary>
        /// Find the quantizer selected in the combobox
        /// </summary>
        /// <returns>the requested quantizer</returns>
        private Quantizer GetQuantizer()
        {
            Quantizer quantizer;

            if (ComboBoxQuantizerSelection.SelectedIndex == 0)
            {
                quantizer = new SimpleQuantizer();
            }
            else if (ComboBoxQuantizerSelection.SelectedIndex == 1)
            {
                quantizer = new HSLQuantizer();
            }
            else if (ComboBoxQuantizerSelection.SelectedIndex == 2)
            {
                quantizer = new BWQuantizer();
            }
            else if (ComboBoxQuantizerSelection.SelectedIndex == 3)
            {
                quantizer = new MedianCut();
            }
            else
            {
                throw new Exception("Invalid Quantizer option!");
            }
            return(quantizer);
        }
Exemplo n.º 3
0
        // Token: 0x06000041 RID: 65 RVA: 0x00003D64 File Offset: 0x00002964
        public void SetImage(Bitmap bitmap)
        {
            TextureBlock            textureBlock = this.TextureBlocks.FirstOrDefault <TextureBlock>();
            PaletteBlock            paletteBlock = this.PaletteBlocks.FirstOrDefault <PaletteBlock>();
            Bitmap                  bitmap2      = MedianCut.Quantize(bitmap, paletteBlock.Colors.Count);
            List <Color>            allColors    = MedianCut.GetAllColors(bitmap2);
            Dictionary <Color, int> dictionary   = new Dictionary <Color, int>();

            foreach (Color key in allColors)
            {
                if (!dictionary.ContainsKey(key))
                {
                    dictionary.Add(key, dictionary.Count);
                }
            }
            paletteBlock.Colors = dictionary.Keys.ToList <Color>();
            textureBlock.ApplyBitmapData(bitmap2, dictionary);
        }
        private void medianCutClick(object sender, RoutedEventArgs e)
        {
            if (imageHandler != null)
            {
                // TODO Background worker
                new Thread(() =>
                {
                    int k = 2;
                    MedianCut medianCut = new MedianCut(imageHandler.getOriginal());
                    medianCut.Compute(k);

                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
                    {
                        filteredImage.Source = BitmapLoader.loadBitmap(medianCut.Image);
                    }));
                    Console.Write("Median Cut finished \n");
                }).Start();
            }
        }
Exemplo n.º 5
0
        //public static unsafe ColorPalette GeneratePalette(this Bitmap bmp, QuantizationAlgorithm mode, int numColors)
        //{
        //    ColorInformation info = bmp.GetColorInformation();

        //    ColorPalette pal = ColorPaletteExtension.CreatePalette(ColorPaletteFlags.None, numColors);
        //    if (info.UniqueColors.Length <= numColors)
        //    {
        //        //Use original colors
        //        for (int i = 0; i < info.UniqueColors.Length; i++)
        //            pal.Entries[i] = (Color)info.UniqueColors[i];
        //    }
        //    else
        //    {
        //        switch (mode)
        //        {
        //            case QuantizationAlgorithm.WeightedAverage:
        //                {
        //                    pal = WeightedAverage.Process(bmp, numColors);
        //                    break;
        //                }
        //            //case QuantizationAlgorithm.MedianCut:
        //            //    {
        //            //        MedianCut.Quantize(bmp, numColors);
        //            //        break;
        //            //    }
        //        }
        //    }
        //    return pal;
        //}

        public static Bitmap Quantize(this Bitmap bmp, QuantizationAlgorithm algorithm, int numColors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
        {
            return(MedianCut.Quantize(bmp, numColors, texFormat, palFormat, progress));
        }
Exemplo n.º 6
0
        static Palette QuantizeColors2(ArrayList alsColors, Palette palFixed, int cPalEntries, int cPalEntriesFixed)
        {
            // If no quantization needed (4 bit grayscale), return

            if (cPalEntriesFixed >= cPalEntries)
                return palFixed;

            MedianCut mcut = new MedianCut(alsColors);
            mcut.convert(cPalEntries - cPalEntriesFixed);
            Palette palUpper = mcut.GetPalette();
            palUpper.Pad(cPalEntries, Color.FromArgb(255, 0, 255));

            Color[] aclr = new Color[cPalEntries];
            for (int iclr = 0; iclr < cPalEntriesFixed; iclr++)
                aclr[iclr] = palFixed[iclr];
            for (int iclr = cPalEntriesFixed; iclr < cPalEntries; iclr++) {
                Color clr = palUpper[iclr - cPalEntriesFixed];
                Color clrT = Color.FromArgb(clr.R & 0xfc, clr.G & 0xfc, clr.B & 0xfc);
                aclr[iclr] = clrT;
            }
            return new Palette(aclr);
        }