private void AddColorEntity(ColorEntry c) { var colorEntity = Instantiate <ColorEntity>(_colorEntityPrefab, _stage); colorEntity.SetData(c); // colorEntity.transform.Translate(new Vector3(1200 * (float) _random.NextDouble() - 600, 500 * (float) _random.NextDouble() - 200, 0.0f)); }
private void Clear() { bool dirtyClear = false; for (int i = 0; i < Pixels.Count; i++) { if (CanPaintCharacters) { if (Pixels[i].Character != ' ') { dirtyClear = true; Pixels[i].Character = ' '; } } if (Pixels[i].Color.ConsoleColor != ConsoleColor.Black) { dirtyClear = true; Pixels[i].Color = ColorEntry.FromConsoleColor(ConsoleColor.Black); } } if (dirtyClear) { AddHistoryState("Clear"); IsDirty = true; } }
private static void SetColorPalette(Dataset ds, Bitmap bitmap) { List <OSGeo.GDAL.ColorInterp> interps = GetRasterColorInterps(ds); int paletteIndex = interps.IndexOf(OSGeo.GDAL.ColorInterp.GCI_PaletteIndex); int grayIndex = interps.IndexOf(OSGeo.GDAL.ColorInterp.GCI_GrayIndex); if (paletteIndex > -1) { ColorTable ct = ds.GetRasterBand(paletteIndex + 1).GetRasterColorTable(); int iCol = ct.GetCount(); ColorPalette pal = bitmap.Palette; for (int i = 0; i < iCol; i++) { ColorEntry ce = ct.GetColorEntry(i); pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3); } bitmap.Palette = pal; } else if (grayIndex > -1) { ColorPalette pal = bitmap.Palette; for (int i = 0; i < 256; i++) { pal.Entries[i] = Color.FromArgb(255, i, i, i); } bitmap.Palette = pal; } }
/// <summary> /// This should update the palette cached and in the file. /// </summary> /// <param name="value">The color palette.</param> public override void SetColorPalette(IEnumerable <Color> value) { var colorPalette = value as IList <Color> ?? value.ToList(); ColorPalette = colorPalette; _dataset = Gdal.Open(Filename, Access.GA_Update); ColorTable ct = new ColorTable(PaletteInterp.GPI_RGB); int index = 0; foreach (var c in colorPalette) { ColorEntry ce = new ColorEntry { c4 = c.A, c3 = c.B, c2 = c.G, c1 = c.R }; ct.SetColorEntry(index, ce); index++; } using (Band first = _dataset.GetRasterBand(1)) { first.SetRasterColorTable(ct); } }
public override System.Drawing.Color[] CategoryColors() { System.Drawing.Color[] Colors = null; ColorTable table = GetColorTable(); if (table != null) { int ColorCount = table.GetCount(); if (ColorCount > 0) { Colors = new System.Drawing.Color[ColorCount]; for (int ColorIndex = 0; ColorIndex < ColorCount; ColorIndex += 1) { Colors[ColorIndex] = System.Drawing.Color.DimGray; ColorEntry entry = table.GetColorEntry(ColorIndex); switch (table.GetPaletteInterpretation()) { case PaletteInterp.GPI_RGB: Colors[ColorIndex] = System.Drawing.Color.FromArgb(entry.c4, entry.c1, entry.c2, entry.c3); break; case PaletteInterp.GPI_Gray: Colors[ColorIndex] = System.Drawing.Color.FromArgb(255, entry.c1, entry.c1, entry.c1); break; //TODO: do any files use these types? //case PaletteInterp.GPI_HLS //case PaletteInterp.GPI_CMYK } } } } return(Colors); }
public static void AddColor(Dictionary <string, Color32> namedLookup, ColorEntry entry) { Color32 color = (Color32)entry; Log.Spam($"Adding color {entry.id}: {color}"); namedLookup[entry.id] = color; }
public void Initialize() { _rootEntry = ColorEntry.Create(); _rootEntry->_prev = _rootEntry->_next = _rootEntry; _rootEntry->_box = Address; _colors = 0; }
public static string ToEntryId(this ColorEntry entry) { switch (entry) { case ColorEntry.Background: return("341d370a-effa-40c9-90c8-ab56c7f03dde"); case ColorEntry.MainText: return("48d15dc5-7597-44a0-8fe5-88f6fc67dcaa"); case ColorEntry.KeyColor1: return("4d4ede3c-e38a-45f4-8dd2-db084f5ac4a6"); case ColorEntry.KeyColor2: return("5adfd18e-5fc5-4bc8-958a-4be3f6ec59b6"); case ColorEntry.ButtonContents: return("ace06488-c485-4f64-a359-64ab534f0454"); case ColorEntry.SubButtonBackground: return("813656bc-9c9a-432d-a465-98344fd8b8b1"); case ColorEntry.OptionButtonBackground: return("f0b6741e-8aab-4614-901d-d938f23a1ed8"); case ColorEntry.OptionButtonContents: return("3de325d8-9dc3-4304-aef6-df868625e6f2"); default: throw new ArgumentOutOfRangeException(nameof(entry), entry, null); } }
private bool SelectColors(int targetColors) { int splitAxis; ColorBox * splitBox, boxPtr = _boxes; ARGBPixel * sPtr = _srcPixels; ColorEntry **gPtr = _groupTable; ColorEntry * entry; ushort id; //Create initial box boxPtr->Initialize(); _boxCount = 1; //Iterate through colors using ID generator for (int i = 0; i < _size; i++) { _idFunc(*sPtr++, &id); gPtr = _groupTable + id; if ((entry = *gPtr) == null) { *gPtr = entry = ColorEntry.Create(); _idConv(&id, out entry->_color); entry->_weight = 1; boxPtr->_rootEntry->InsertPrev(entry); } else { entry->_weight++; } } //If no quantization is necessary, then leave. if (boxPtr->_colors <= targetColors) { return(false); } //Update initial box boxPtr->Update(targetColors - 1); //Split until we reach desired colors while (_boxCount < targetColors) { //Find split candidate splitBox = ColorBox.FindSplit(_boxes, _boxCount, targetColors, out splitAxis); //Create new box (++boxPtr)->Initialize(); //Move colors from one box to another using split axis splitBox->Split(boxPtr, splitAxis); //Update boxes _boxCount++; splitBox->Update(targetColors - _boxCount); boxPtr->Update(targetColors - _boxCount); } return(true); }
private void ReadPaletteBuffered() { ColorTable ct = _red.GetRasterColorTable(); if (ct == null) { throw new GdalException("Image was stored with a palette interpretation but has no color table."); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { throw new GdalException("Only RGB palette interpreation is currently supported by this plugin, " + ct.GetPaletteInterpretation() + " is not supported."); } int tw = TileCollection.TileWidth; int th = TileCollection.TileHeight; for (int row = 0; row < TileCollection.NumTilesTall(); row++) { for (int col = 0; col < TileCollection.NumTilesWide(); col++) { // takes into account that right and bottom tiles might be smaller. int width = TileCollection.GetTileWidth(col); int height = TileCollection.GetTileHeight(row); ImageData id = new ImageData(); byte[] red = new byte[width * height]; _red.ReadRaster(col * tw, row * th, width, height, red, width, height, 0, 0); Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); Stride = bData.Stride; image.UnlockBits(bData); const int Bpp = 4; int stride = Stride; byte[] vals = new byte[width * height * 4]; byte[][] colorTable = new byte[256][]; for (int i = 0; i < 255; i++) { ColorEntry ce = ct.GetColorEntry(i); colorTable[i] = new[] { (byte)ce.c3, (byte)ce.c2, (byte)ce.c1, (byte)ce.c4 }; } for (int r = 0; r < height; r++) { for (int c = 0; c < width; c++) { Array.Copy(colorTable[red[c + (r * width)]], 0, vals, (row * stride) + (col * Bpp), 4); } } id.Values = vals; id.CopyValuesToBitmap(); TileCollection.Tiles[row, col] = id; } } SetTileBounds(Bounds.AffineCoefficients); }
private static void SaveBitmapPaletteBuffered(Dataset ds, string filename, int iOverview) { // Get the GDAL Band objects from the Dataset Band band = ds.GetRasterBand(1); if (iOverview >= 0 && band.GetOverviewCount() > iOverview) { band = band.GetOverview(iOverview); } ColorTable ct = band.GetRasterColorTable(); if (ct == null) { Console.WriteLine(" Band has no color table!"); return; } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { Console.WriteLine(" Only RGB palette interp is supported by this sample!"); return; } // Get the width and height of the Dataset int width = band.XSize; int height = band.YSize; // Create a Bitmap to store the GDAL image in Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb); DateTime start = DateTime.Now; byte[] r = new byte[width * height]; band.ReadRaster(0, 0, width, height, r, width, height, 0, 0); TimeSpan renderTime = DateTime.Now - start; int i, j; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { ColorEntry entry = ct.GetColorEntry(r[i + j * width]); Color newColor = Color.FromArgb(Convert.ToInt32(entry.c1), Convert.ToInt32(entry.c2), Convert.ToInt32(entry.c3)); bitmap.SetPixel(i, j, newColor); } } bitmap.Save(filename + ".bmp"); float scale = bitmap.Width / 1024; Bitmap map = new Bitmap(bitmap, new Size(1024, (int)(bitmap.Height / scale))); map.Save(filename + "_icon.bmp"); }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread #endregion #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread public ThemeColorsQuickStart() { Instance = this; CategoryGuid = new Guid(CategoryGuidString); ColorEntries = new ColorEntry[] { new TitleEntry(this), new TextEntry(this), new HyperlinkEntry(this) }; }
private void _setRow(DataGridViewRow row, ColorEntry color) { row.HeaderCell.Value = row.Index.ToString(); row.Cells["Id"].Value = string.Format("{0}:{1}", color.Id, color.Name); row.Cells["Cloth"].Style.BackColor = color.Cloth.RGB; row.Cells["Cloth"].ToolTipText = string.Format(RGBStringFormat, color.Cloth.RGB.ToArgb()); row.Cells["Leather"].Style.BackColor = color.Leather.RGB; row.Cells["Leather"].ToolTipText = string.Format(RGBStringFormat, color.Leather.RGB.ToArgb()); row.Cells["Metal"].Style.BackColor = color.Metal.RGB; row.Cells["Metal"].ToolTipText = string.Format(RGBStringFormat, color.Metal.RGB.ToArgb()); }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread #endregion #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread public FontAndColorDefaultsResultsText() { Instance = this; CategoryGuid = new Guid(CategoryGuidString); CategoryName = "Regex Editor Results Text"; Font = CreateFontInfo("Consolas", 9, 1); ColorEntries = new ColorEntry[] { new PlainTextEntry(this), new SelectedTextEntry(this), }; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ColorEntryList colorEntries = App.Current.Resources["ColorEntries"] as ColorEntryList; ColorEntry entry = colorEntries.FirstOrDefault(ce => ce.Color.ToString() == value.ToString()); if (entry == null) { System.Diagnostics.Debug.WriteLine("TODO: CLean this up, Jaime added as temporary workaround, since it was crashing and preventing me from doing other work"); entry = new ColorEntry(); entry.Color = (string) value; entry.Name = "UNKNOWN"; } return entry; }
private void ReadPaletteBuffered() { ColorTable ct = _red.GetRasterColorTable(); if (ct == null) { throw new GdalException("Image was stored with a palette interpretation but has no color table."); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { throw new GdalException(String.Format("Only RGB palette interpretation is currently supported by this plug-in, {0} is not supported.", ct.GetPaletteInterpretation())); } int width = Width; int height = Height; byte[] r = new byte[width * height]; _red.ReadRaster(0, 0, width, height, r, width, height, 0, 0); _image = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bData = _image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); Stride = bData.Stride; _image.UnlockBits(bData); const int bpp = 4; int stride = Stride; byte[] vals = new byte[width * height * 4]; byte[][] colorTable = new byte[ct.GetCount()][]; for (int i = 0; i < ct.GetCount(); i++) { ColorEntry ce = ct.GetColorEntry(i); colorTable[i] = new[] { (byte)ce.c3, (byte)ce.c2, (byte)ce.c1, (byte)ce.c4 }; } for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Array.Copy(colorTable[r[col + row * width]], 0, vals, row * stride + col * bpp, 4); } } Values = vals; CopyValuesToBitmap(); }
private static Bitmap getBitmapPaletteBuffered(Dataset ds) { Band band = ds.GetRasterBand(1); ColorTable ct = band.GetRasterColorTable(); if (ct == null) { return(null); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { //MessageBox.Show(" Only RGB palette interp is supported by this sample!"); return(null); } int width = band.XSize; int height = band.YSize; DateTime start = DateTime.Now; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb); byte[] r = new byte[width * height]; band.ReadRaster(0, 0, width, height, r, width, height, 0, 0); int i, j; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { // entry c1 c2 c3 ColorEntry entry = ct.GetColorEntry(r[i + j * width]); int rColor = Convert.ToInt32(entry.c1); int gColor = Convert.ToInt32(entry.c2); int bColor = Convert.ToInt32(entry.c3); Color newColor = Color.FromArgb(rColor, gColor, bColor); bitmap.SetPixel(i, j, newColor); } } TimeSpan renderTime = DateTime.Now - start; double milliSeconds = renderTime.TotalMilliseconds; // ms //MessageBox.Show("GCI_PaletteIndex"); return(bitmap); }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread #endregion #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread public FontAndColorDefaultsResultsGrid() { Instance = this; CategoryGuid = new Guid(CategoryGuidString); CategoryName = "Regex Editor Results Grid"; Font = CreateFontInfo("Consolas", 9, 1); ColorEntries = new ColorEntry[] { new NormalCellEntry(this), new SelectedCellEntry(this), new InactiveSelectedCellEntry(this), new HeaderCellEntry(this), new FailureMarkerEntry(this), new GridLinesEntry(this), }; }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread #endregion #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread public FontAndColorDefaultsQuickRef() { Instance = this; CategoryGuid = new Guid(CategoryGuidString); CategoryName = "Regex Quick Reference Window"; Font = CreateFontInfo("Consolas", 9, 1); ColorEntries = new ColorEntry[] { new DocumentEntry(this), new HyperlinkEntry(this), new HeadRowEntry(this), new EvenRowEntry(this), new EvenRowArgEntry(this), new OddRowEntry(this), new OddRowArgEntry(this), }; }
public void Destroy() { ColorEntry *current, next; if (_rootEntry != null) { current = _rootEntry; do { next = current->_next; ColorEntry.Destroy(current); current = next; } while (current != _rootEntry); _rootEntry = null; } }
private void HandleCommmitCommand() { ColorEntry newColorEntry = new ColorEntry(editionColorItem.Token, editionColorItem.Name, editionColorItem.R, editionColorItem.G, editionColorItem.B); s52ResourceFileAnalyst.UpdateColorEntry(selectedColorSchema, newColorEntry); for (int i = 0; i < colorTables[selectedColorSchema].Count; i++) { ColorEntry item = colorTables[selectedColorSchema][i]; if (item.Token == editionColorItem.Token) { colorTables[selectedColorSchema][i] = newColorEntry; break; } } LoadCurrentSchemaColorItems(); MessageBox.Show("Commit succeed.", string.Empty, MessageBoxButton.OK, MessageBoxImage.Information); }
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread #endregion #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread public FontAndColorDefaultsResultsTree() { Instance = this; CategoryGuid = new Guid(CategoryGuidString); CategoryName = "Regex Editor Results Tree"; Font = CreateFontInfo("Consolas", 9, 1); ColorEntries = new ColorEntry[] { new DocumentEntry(this), new ActiveSelectionEntry(this), new InactiveSelectionEntry(this), new LineNodeHeaderEntry(this), new MatchNodeHeaderEntry(this), new GroupNodeHeaderEntry(this), new CaptureNodeHeaderEntry(this), new EmptyMarkerEntry(this), new FailureMarkerEntry(this), }; }
private static void LoadBitmapDirect(Dataset ds, Bitmap bitmap, int xOff, int yOff, int width, int height, int imageWidth, int imageHeight, int iOverview) { if (isIndexed) { // setting up the color table if (ct != null) { int iCol = ct.GetCount(); ColorPalette pal = bitmap.Palette; for (int i = 0; i < iCol; i++) { ColorEntry ce = ct.GetColorEntry(i); pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3); } bitmap.Palette = pal; } else { // grayscale ColorPalette pal = bitmap.Palette; for (int i = 0; i < 256; i++) { pal.Entries[i] = Color.FromArgb(255, i, i, i); } bitmap.Palette = pal; } } // Use GDAL raster reading methods to read the image data directly into the Bitmap BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; ds.ReadRaster(xOff, yOff, width, height, buf, imageWidth, imageHeight, dataType, channelCount, bandMap, pixelSpace, stride, 1); } finally { bitmap.UnlockBits(bitmapData); } }
/// <summary> /// Returns ARGB 32 bpp regardless of the fact that the original is palette buffered. /// </summary> /// <param name="startRow">The start row.</param> /// <param name="startColumn">The start column.</param> /// <param name="numRows">The number of rows.</param> /// <param name="numColumns">The number of columns.</param> /// <param name="overview">The overview.</param> /// <returns>The data that was read.</returns> private byte[] ReadPaletteBuffered(int startRow, int startColumn, int numRows, int numColumns, int overview) { ColorTable ct = _red.GetRasterColorTable(); if (ct == null) { throw new GdalException("Image was stored with a palette interpretation but has no color table."); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { throw new GdalException("Only RGB palette interpreation is currently supported by this plugin, " + ct.GetPaletteInterpretation() + " is not supported."); } int width = numRows; int height = numColumns; byte[] r = new byte[width * height]; _red.ReadRaster(startColumn, startRow, numColumns, numRows, r, width, height, 0, 0); if (overview > 0) { _red = _red.GetOverview(overview - 1); } const int Bpp = 4; byte[] vals = new byte[width * height * 4]; byte[][] colorTable = new byte[256][]; for (int i = 0; i < 255; i++) { ColorEntry ce = ct.GetColorEntry(i); colorTable[i] = new[] { (byte)ce.c3, (byte)ce.c2, (byte)ce.c1, (byte)ce.c4 }; } for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Array.Copy(colorTable[r[col + (row * width)]], 0, vals, (row * width) + (col * Bpp), 4); } } return(vals); }
/// <summary> /// This is only used in the palette indexed band type. /// </summary> public override IEnumerable <Color> GetColorPalette() { if (ColorPalette == null) { _dataset = Helpers.Open(Filename); Band first = _dataset.GetRasterBand(1); ColorTable ct = first.GetRasterColorTable(); int count = ct.GetCount(); List <Color> result = new List <Color>(); for (int i = 0; i < count; i++) { ColorEntry ce = ct.GetColorEntry(i); result.Add(Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3)); } ColorPalette = result; } return(ColorPalette); }
public void CmdAskForColor(int color, PlayerBehaviour player) { int entryIndex = ColorSelected.FindIndex(x => x.PlayerAssigned == player); if (entryIndex > -1) { ColorSelected.RemoveAt(entryIndex); } ColorEntry entry = new ColorEntry { PlayerAssigned = player, ColorIndex = color }; ColorSelected.Add(entry); player.SetColor(GameColors[color]); }
private void ImportArt() { if (string.IsNullOrEmpty(ImportedArt)) { return; } var lines = ImportedArt.Split('\n'); GridHeight = lines.Length; var ordered = lines.OrderByDescending(x => x.Length); int leftPad = lines.Length <= 1 ? 0 : -1; GridWidth = ordered.First().Length + leftPad; OnGridResized(); for (int y = 0; y < lines.Length; y++) { var line = lines[y]; for (int x = 0; x < GridWidth; x++) { if (x >= line.Length) { continue; } var pixel = Pixels[y * GridWidth + x]; if (charLookup.ContainsKey(line[x])) { pixel.Character = line[x]; pixel.Color = SelectedColor; } else { pixel.Character = InvalidCharacter; pixel.Color = ColorEntry.FromConsoleColor(ConsoleColor.Black); } } } ImportedArt = string.Empty; OnPropertyChanged("GridHeight"); OnPropertyChanged("GridWidth"); OnPropertyChanged("PixelWidth"); AddHistoryState("Import Art"); IsDirty = true; }
private static void WritePaletteBuffered(Bitmap value, int xOffset, int yOffset, Band first) { ColorTable ct = first.GetRasterColorTable(); if (ct == null) { throw new GdalException("Image was stored with a palette interpretation but has no color table."); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { throw new GdalException("Only RGB palette interpretation is currently supported by this " + " plug-in, " + ct.GetPaletteInterpretation() + " is not supported."); } int width = value.Width; int height = value.Height; BitmapData bData = value.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int stride = Math.Abs(bData.Stride); byte[] r = new byte[stride * height]; Marshal.Copy(bData.Scan0, r, 0, r.Length); value.UnlockBits(bData); byte[] vals = new byte[width * height]; byte[][] colorTable = new byte[ct.GetCount()][]; for (int i = 0; i < ct.GetCount(); i++) { ColorEntry ce = ct.GetColorEntry(i); colorTable[i] = new[] { (byte)ce.c3, (byte)ce.c2, (byte)ce.c1, (byte)ce.c4 }; } for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { vals[row * width + col] = MatchColor(r, row * stride + col * 4, colorTable); } } first.WriteRaster(xOffset, yOffset, width, height, vals, width, height, 0, 0); }
private void ApplySprite(ConsoleSprite sprite) { gridWidth = sprite.Width; gridHeight = sprite.Height; var pixels = new List <PixelEntry>(gridWidth * gridHeight); for (int i = 0; i < gridWidth * gridHeight; i++) { pixels.Add(new PixelEntry()); var e = Encoding.GetEncoding("437"); var s = e.GetString(new byte[] { sprite.Characters[i] }); pixels[i].Character = s[0]; pixels[i].Color = ColorEntry.FromConsoleColor((ConsoleColor)sprite.Colors[i]); } Pixels.Reset(pixels); SupportsTransparency = sprite.IsTransparent; OnPropertyChanged("GridHeight"); OnPropertyChanged("GridWidth"); OnPropertyChanged("PixelWidth"); }
/// <summary> /// This should update the palette cached and in the file. /// </summary> /// <param name="value"></param> public override void SetColorPalette(IEnumerable <Color> value) { ColorPalette = value; _dataset = Gdal.Open(Filename, Access.GA_Update); ColorTable ct = new ColorTable(PaletteInterp.GPI_RGB); int index = 0; foreach (Color c in value) { ColorEntry ce = new ColorEntry(); ce.c4 = c.A; ce.c3 = c.B; ce.c2 = c.G; ce.c1 = c.R; ct.SetColorEntry(index, ce); index++; } using (Band first = _dataset.GetRasterBand(1)) { first.SetRasterColorTable(ct); } }
public void InsertPrev(ColorEntry* entry) { entry->_prev = _prev; entry->_next = _prev->_next; entry->_box = _box; _box->_colors++; _prev->_next = entry; _prev = entry; }
public static void Destroy(ColorEntry* e) { Marshal.FreeHGlobal((IntPtr)e); }
private unsafe ColorEntry[] enumerateColors(BitmapData srcBmpData, Rectangle targetArea) { const int ColorHashSize = 4096; const int InitialChainLength = 32; #if DEBUG var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); #endif // HashSetの準備 ColorEntry[][] chainArray = new ColorEntry[ColorHashSize][]; int[] chainSizeArray = new int[ColorHashSize]; for (int i = 0; i < ColorHashSize; i++) { chainArray[i] = new ColorEntry[InitialChainLength]; chainSizeArray[i] = 0; } ArgbColor* scan0 = (ArgbColor*)srcBmpData.Scan0; int lineStride = srcBmpData.Stride / 4; for (int srcY = 0; srcY < targetArea.Height; srcY++) { ArgbColor* pixelPtr = scan0 + ((srcY + targetArea.Y) * lineStride) + targetArea.X ; for (int srcX = 0; srcX < targetArea.Width; srcX++) { ArgbColor pixel = *(pixelPtr++); // 不透明度が満たないピクセルは無視 if (pixel.A <TransparentThresh) continue; // ハッシュ値 uint hashTemp = pixel.AsUInt32 ^ ((pixel.AsUInt32 & 0x00f0f0f0u) >> 4); uint hash = ((hashTemp & 0x0000000fu) >> 0) | ((hashTemp & 0x00000f00u) >> 4) | ((hashTemp & 0x000f0000u) >> 8); // ハッシュ値に対応した配列を引いてくる ColorEntry[] chain = chainArray[hash]; int rowSize = chainSizeArray[hash]; // 完全一致する色を探す bool found = false; for (int i = 0; i < rowSize; i++) { if (chain[i].Color.AsUInt32 == pixel.AsUInt32) { found = true; chain[i].Count++; // 見つけたらちょっと前方に動かす var temp = chain[i]; chain[i] = chain[i / 2]; chain[i / 2] = temp; break; } } // 無ければ追加する if (found == false) { if (rowSize >= chain.Length) { Array.Resize(ref chain, chain.Length * 2); chainArray[hash] = chain; } ColorEntry newColor = new ColorEntry(); newColor.Color = pixel; newColor.Count = 1; chain[rowSize] = newColor; chainSizeArray[hash]++; } } } // 一本の配列に連結 int colorCount = chainSizeArray.Sum(); ColorEntry[] entries = new ColorEntry[colorCount]; int wptr = 0; int maxSize = 0; for (int hash = 0; hash < ColorHashSize; hash++) { int stored = chainSizeArray[hash]; if (stored == 0) continue; Array.Copy(chainArray[hash], 0, entries, wptr, stored); wptr += stored; if (stored > maxSize) { maxSize = stored; } } #if DEBUG Console.WriteLine("{0} colors are detected ({1} msec, size.max={2}, size.ave={3}).", colorCount, stopWatch.ElapsedMilliseconds, maxSize, colorCount / ColorHashSize); //Console.WriteLine(" R={0}-{1}, G={2}-{3}, B={4}-{5}", // firstColorBox.MinR, firstColorBox.MaxR, firstColorBox.MinG, firstColorBox.MaxG, firstColorBox.MinB, firstColorBox.MaxB); #endif return entries; }
public static void Main(string[] args) { if (args.Length != 2) { usage(); } string file = args[0]; string file_out = args[1]; try { /* -------------------------------------------------------------------- */ /* Register driver(s). */ /* -------------------------------------------------------------------- */ Gdal.AllRegister(); Driver dv = null; Dataset ds = null, ds_out = null; Band ba = null, ba_out = null; ColorTable ct = null, ct_out = null; byte [] buffer; /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ ds = Gdal.Open(file, Access.GA_ReadOnly); ba = ds.GetRasterBand(1); ct = ba.GetRasterColorTable(); if (ct != null) { Console.WriteLine("Band has a color table with " + ct.GetCount() + " entries."); } else { Console.WriteLine("Data source has no color table"); return; } buffer = new byte [ds.RasterXSize * ds.RasterYSize]; ba.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buffer, ds.RasterXSize, ds.RasterYSize, 0, 0); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ dv = Gdal.GetDriverByName("GTiff"); ds_out = dv.Create(file_out, ds.RasterXSize, ds.RasterYSize, ds.RasterCount, ba.DataType, new string [] {}); ba_out = ds_out.GetRasterBand(1); ct_out = new ColorTable(PaletteInterp.GPI_RGB); ba_out.WriteRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buffer, ds.RasterXSize, ds.RasterYSize, 0, 0); /* -------------------------------------------------------------------- */ /* Copying the colortable */ /* -------------------------------------------------------------------- */ for (int i = 0; i < ct.GetCount(); i++) { ColorEntry ce = null, ce_out = null; ce = ct.GetColorEntry(i); ce_out = new ColorEntry(); ce_out.c1 = ce.c1; ce_out.c2 = ce.c2; ce_out.c3 = ce.c3; ce_out.c4 = ce.c4; ct_out.SetColorEntry(i, ce_out); ce.Dispose(); ce_out.Dispose(); } ba_out.SetRasterColorTable(ct_out); } catch (Exception e) { Console.WriteLine("Application error: " + e.Message); } }
// メディアンカット private unsafe ColorBox[] medianCut(ColorEntry[] colors, ArgbColor[] importantColors, int paletteSize) { #if DEBUG var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); #endif ColorBox[] boxes = new ColorBox[paletteSize]; int boxCount = 0; boxes[boxCount++] = new ColorBox(0, colors.Length - 1) ; boxes[0].CalcWeight(colors, importantColors); fixed (ColorEntry* table = colors) while (boxCount < paletteSize) { // 分散が最も大きなBoxを探す int divIndex = -1; int maxWeight = int.MinValue; for (int i = 0; i < boxCount; i++) { if (boxes[i].DivWeight >= maxWeight) { maxWeight = boxes[i].DivWeight; divIndex = i; } } int minIdx = boxes[divIndex].ColorIndexMin; int maxIdx = boxes[divIndex].ColorIndexMax; int ch = boxes[divIndex].DivChannel; // 中央値を決めて分割 int median = boxes[divIndex].MedianValue; int l = minIdx; int r = maxIdx; do { while (l + 1 < r && table[l].Color.Channels[ch] <= median) l++; while (l < r - 1 && table[r].Color.Channels[ch] >= median) r--; if (table[l].Color.Channels[ch] > table[r].Color.Channels[ch]) { ColorEntry temp = table[l]; table[l] = table[r]; table[r] = temp; } } while (l + 1 < r); ColorBox leftBox = new ColorBox( minIdx, l); leftBox.CalcWeight(colors, importantColors); boxes[divIndex] = leftBox; ColorBox rightBox = new ColorBox( r, maxIdx); rightBox.CalcWeight(colors, importantColors); boxes[boxCount++] = rightBox; } #if DEBUG Console.WriteLine("median cut ({0} msec).", stopWatch.ElapsedMilliseconds); #endif return boxes; }
// 代表色の選択 private ArgbPalette generatePalette(Size size, ColorEntry[] colors, ColorBox[] boxes, ArgbColor[] importantColors, int paletteSize) { #if DEBUG var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); #endif int totalPixelCount = size.Width * size.Height; ArgbColor center = ArgbColor.FromArgb(128, 128, 128); ArgbPalette palette = new ArgbPalette(paletteSize); for (int i = 0; i < paletteSize; i++) { uint sumR = 0; uint sumG = 0; uint sumB = 0; uint sumCount = 0; int indexMin = boxes[i].ColorIndexMin; int indexMax = boxes[i].ColorIndexMax; int length = (indexMax - indexMin + 1); int maxDist = 0; ColorEntry maxE = colors[indexMin]; for (int j = indexMin; j <= indexMax; j++) { ArgbColor thisColor = colors[j].Color; int penalty = 0; foreach (var c in importantColors) { int dist2 = (int)thisColor.GetEuclidDistanceTo(c); if (dist2 > 64) dist2 = 0; penalty += dist2 *10; } if (importantColors.Length > 0) { penalty /= importantColors.Length; } sumR += thisColor.R * colors[j].Count; sumG += thisColor.G * colors[j].Count; sumB += thisColor.B * colors[j].Count; int dist = (int)center.GetEuclidDistanceTo(thisColor) + (1000 * (int)colors[j].Count / totalPixelCount) - penalty; if (dist > maxDist) { maxDist = dist; maxE = colors[j]; } sumCount += colors[j].Count; } byte r = (byte)Math.Round((float)sumR / (float)sumCount); byte g = (byte)Math.Round((float)sumG / (float)sumCount); byte b = (byte)Math.Round((float)sumB / (float)sumCount); palette.Set(i, r, g, b, 1); } #if DEBUG Console.WriteLine("palette generation ({0} msec).", stopWatch.ElapsedMilliseconds); #endif return palette; }
public void CalcWeight(ColorEntry[] colorArray,ArgbColor[] importantColors) { byte minR = 255, maxR = 0; byte minG = 255, maxG = 0; byte minB = 255, maxB = 0; int varianceR = 0; int varianceG = 0; int varianceB = 0; int length = ColorIndexMax - ColorIndexMin + 1; // 最大/最小と分散の算出 for (int i = ColorIndexMin; i <= ColorIndexMax; i++) { ColorEntry color = colorArray[i]; if (color.Color.R < minR) minR = color.Color.R; if (color.Color.R > maxR) maxR = color.Color.R; if (color.Color.G < minG) minG = color.Color.G; if (color.Color.G > maxG) maxG = color.Color.G; if (color.Color.B < minB) minB = color.Color.B; if (color.Color.B > maxB) maxB = color.Color.B; } int medianR = (maxR + minR) / 2; int medianG = (maxG + minG) / 2; int medianB = (maxB + minB) / 2; // 重要色からの距離 int penalty = 0; foreach (var e in importantColors) { ArgbColor medianColor = ArgbColor.FromArgb(medianR, medianG, medianB); int dist = (int)medianColor.GetEuclidDistanceTo(e); if (dist > 0) { medianR += (medianR - e.R) * 100 / (dist * dist); medianG += (medianG - e.R) * 100 / (dist * dist); medianB += (medianB - e.R) * 100 / (dist * dist); } penalty += dist; } if (importantColors.Length > 0) { const int a = 4, b = 1, ab = a + b; if (medianR < (minR * a + maxR * b) / ab) medianR = (minR * a + maxR * b) / ab; if (medianR > (minR * b + maxR * a) / ab) medianR = (minR * b + maxR * a) / ab; if (medianG < (minG * a + maxG * b) / ab) medianG = (minG * a + maxG * b) / ab; if (medianG > (minG * b + maxG * a) / ab) medianG = (minG * b + maxG * a) / ab; if (medianB < (minB * a + maxB * b) / ab) medianB = (minB * a + maxB * b) / ab; if (medianB > (minB * b + maxB * a) / ab) medianB = (minB * b + maxB * a) / ab; penalty /= (importantColors.Length); } // 分散の算出 for (int i = ColorIndexMin; i <= ColorIndexMax; i++) { ColorEntry color = colorArray[i]; // RGB各軸方向の分散 int diffR = ((int)color.Color.R - medianR); int diffG = ((int)color.Color.G - medianG); int diffB = ((int)color.Color.B - medianB); varianceR += diffR * diffR; varianceG += diffG * diffG; varianceB += diffB * diffB; } int weightR = varianceR / (penalty + 1); int weightG = varianceG / (penalty + 1); int weightB = varianceB / (penalty + 1); // 次回のメディアンカットに備えて分散の値を記録 if (weightR > weightG && weightR > weightB) { DivWeight = weightR; DivChannel = ArgbColor.ChannelR; MedianValue = medianR; } else if (weightG > weightB) { DivWeight = weightG; DivChannel = ArgbColor.ChannelG; MedianValue = medianG; } else { DivWeight = weightB; DivChannel = ArgbColor.ChannelB; MedianValue = medianB; } }