public void GenerateRasterImage() { if (!string.IsNullOrEmpty(originalFile)) { try { nQuant.WuQuantizer quantizer = new nQuant.WuQuantizer(); System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(originalFile); quantizer.QuantizeImage(bitmap); rectangle1.Background = new SolidColorBrush(Color.FromRgb(quantizer.Palette.Colors[0].R, quantizer.Palette.Colors[0].G, quantizer.Palette.Colors[0].B)); rectangle2.Background = new SolidColorBrush(Color.FromRgb(quantizer.Palette.Colors[1].R, quantizer.Palette.Colors[1].G, quantizer.Palette.Colors[1].B)); rectangle3.Background = new SolidColorBrush(Color.FromRgb(quantizer.Palette.Colors[2].R, quantizer.Palette.Colors[2].G, quantizer.Palette.Colors[2].B)); rectangle4.Background = new SolidColorBrush(Color.FromRgb(quantizer.Palette.Colors[3].R, quantizer.Palette.Colors[3].G, quantizer.Palette.Colors[3].B)); QuantizedSignedDistanceFieldGenerator generator = new QuantizedSignedDistanceFieldGenerator(quantizer.Palette, bitmap.Height, bitmap.Width); generator.Generate(0); generator.Generate(1); generator.Generate(2); generator.Generate(3); quantizedBitmap = generator.GetBitmap(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error during generation", MessageBoxButton.OK, MessageBoxImage.Error); } } }
private static void Quantize(ref Bitmap img) { if (img.PixelFormat != PixelFormat.Format32bppArgb) { ConvertTo32bppAndDisposeOriginal(ref img); } try { var quantizer = new nQuant.WuQuantizer(); Bitmap quantized = (Bitmap)quantizer.QuantizeImage(img); img.Dispose(); img = quantized; } catch (nQuant.QuantizationException q) { Trace.WriteLine(q.Message); } }
/// <summary><para>Performa a lossy quantize.</para><para>This is seperated from the main function so that when the DLL is missing, a straight quantize can still be attempted.</para></summary> /// <param name="input">Input <c>Bitmap</c>.</param> /// <param name="MaxColor">Max colros in output image.</param> /// <returns>New image.</returns> private static Bitmap lossyQuantize(Bitmap input, int MaxColor) { return(quantizer.QuantizeImage(input, 12, 1, MaxColor) as Bitmap); }
public void GenerateRasterImage(string originalFile) { if (!string.IsNullOrEmpty(originalFile)) { try { nQuant.WuQuantizer quantizer = new nQuant.WuQuantizer(); System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(originalFile); quantizer.QuantizeImage(bitmap); QuantizedSignedDistanceFieldGenerator generator = new QuantizedSignedDistanceFieldGenerator(quantizer.Palette, bitmap.Height, bitmap.Width); generator.Generate(0); generator.Generate(1); generator.Generate(2); generator.Generate(3); RasterImage RasterResult = RasterImage.CreateFromBitmap(generator.GetBitmap()); uint InstanceId = TGIRandomGenerator.GetNext(); DatabaseIndex newIndex = new DatabaseIndex(OriginalIndex.Owner); newIndex.TypeId = (uint)TypeIds.RasterFile; newIndex.InstanceId = InstanceId; newIndex.Flags = 1; newIndex.IsModified = true; newIndex.Compressed = false; newIndex.ModifiedData = new ModifiedRasterFile() { ImageFileData = RasterResult.ToIndexData() }; OriginalIndex.Owner.Indices.Add(newIndex); SimCityPak.PackageReader.DatabaseManager.Instance.Indices.Add(newIndex); UnitFileEntry.LotMask.InstanceId = InstanceId; //quantizer.Palette.Colors[0].Scr Color col = Color.FromArgb(quantizer.Palette.Colors[0].A, quantizer.Palette.Colors[0].R, quantizer.Palette.Colors[0].G, quantizer.Palette.Colors[0].B); UnitFileEntry.LotColor4.R = col.ScR; UnitFileEntry.LotColor4.G = col.ScG; UnitFileEntry.LotColor4.B = col.ScB; col = Color.FromArgb(quantizer.Palette.Colors[1].A, quantizer.Palette.Colors[1].R, quantizer.Palette.Colors[1].G, quantizer.Palette.Colors[1].B); UnitFileEntry.LotColor1.R = col.ScR; UnitFileEntry.LotColor1.G = col.ScG; UnitFileEntry.LotColor1.B = col.ScB; col = Color.FromArgb(quantizer.Palette.Colors[2].A, quantizer.Palette.Colors[2].R, quantizer.Palette.Colors[2].G, quantizer.Palette.Colors[2].B); UnitFileEntry.LotColor2.R = col.ScR; UnitFileEntry.LotColor2.G = col.ScG; UnitFileEntry.LotColor2.B = col.ScB; col = Color.FromArgb(quantizer.Palette.Colors[3].A, quantizer.Palette.Colors[3].R, quantizer.Palette.Colors[3].G, quantizer.Palette.Colors[3].B); UnitFileEntry.LotColor3.R = col.ScR; UnitFileEntry.LotColor3.G = col.ScG; UnitFileEntry.LotColor3.B = col.ScB; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error during generation", MessageBoxButton.OK, MessageBoxImage.Error); } } }
private byte[] EncodeFromImages() { List <Bitmap> bitmaps = null; if (images.Count == 1) // We can quantize a single palette image { Image img = images.First(); if (img.ColorsCount() > colors) { nQuant.WuQuantizerBase quantizer = new nQuant.WuQuantizer(colors); img = quantizer.QuantizeImage(new Bitmap(img)); } bitmaps = new List <Bitmap>(); bitmaps.Add(new Bitmap(img)); } else //for multi palette images, quantization may break the pixel structure of the images. We must trust the work of the graphics editor. { bitmaps = new List <Image>(images).ConvertAll(x => new Bitmap(x)); } var indexes = new int[width * height]; Palettes = new List <Color[]>(); for (int i = 0; i < bitmaps.Count; i++) { Palettes.Add(Enumerable.Repeat <Color>(Color.Black, colors).ToArray()); } for (int i = 0; i < bitmaps.Count; i++) { int count = 0; List <Color> palette = new List <Color>(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color pixel = bitmaps[i].GetPixel(x, y); if (!palette.Contains(pixel)) { if (count >= colors) { throw new ArgumentException("Too many colors! The maximum for this image is " + colors + "!"); } palette.Add(pixel); count++; } } } for (int c = 0; c < colors - count; c++) { palette.Add(Color.Black); } palette.Sort(pixelSorter); Palettes[i] = palette.ToArray(); } int k = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color pixel = bitmaps[0].GetPixel(x, y); int idx = Array.BinarySearch(Palettes[0], pixel, pixelSorter); indexes[k++] = idx; } } if (colorEncoder != null) { EncodedPalettes = new List <byte[]>(Palettes.Count); foreach (Color[] pal in Palettes) { EncodedPalettes.Add(colorEncoder.EncodeColors(paletteFilter == null ? pal : paletteFilter.ApplyFilter(pal))); } } return(imageFilter == null?codec.PackIndexes(indexes) : imageFilter.ApplyFilter(codec.PackIndexes(indexes))); }