/// <summary> /// Gets a custom palette from collection. If custom palette is not found, creates one, adds it to the collection and returns it. /// Search is done by comparing names of the palettes. /// </summary> /// <param name="paletteName">Name of the palette to find, without theater or .pal extension.</param> /// <returns>The correct custom palette.</returns> public Palette GetCustomPalette(string paletteName) { string fileName; // Necessary to distinguish between object and theater/animation palettes when recalculating values. bool objectPalette = false; if (paletteName.ToLower().EndsWith(".pal")) // full name already given { fileName = paletteName; } else { // filename = <paletteName><theaterExtension>.pal (e.g. lib<tem/sno/urb>.pal) fileName = paletteName + ModConfig.ActiveTheater.Extension.Substring(1) + ".pal"; objectPalette = true; } var pal = CustomPalettes.FirstOrDefault(p => p.Name == paletteName); if (pal == null) { // palette hasn't been loaded yet // If the original does not exist, it means the file it should use does not exist. It now returns a null in this case, which is // handled appropriately wherever this method is called to fall back to the default palette for that type of object. PalFile orig = _vfs.Open <PalFile>(fileName); if (orig == null) { return(null); } pal = new Palette(_vfs.Open <PalFile>(fileName), paletteName, objectPalette); CustomPalettes.Add(pal); } return(pal); }
public Merge(PalFile src) { InitializeComponent(); Misc.DeepCopy(src.Data, srcPal.Data); PreviewPanel.PalSource = new PalFile(); Misc.DeepCopy(src.Data, PreviewPanel.PalSource.Data); NudNewCount_ValueChanged(null, new EventArgs()); Misc.SetLanguage(this); }
public Palette(PalFile originalPalette, string name = "") { _originalPalette = originalPalette; if (!string.IsNullOrEmpty(name)) { Name = name; } else { Name = Path.GetFileNameWithoutExtension(originalPalette.FileName); } }
public unsafe static void GifToIndex(Image img, PalFile pal) { PropertyItem[] props = img.PropertyItems; HashSet <int> set = new HashSet <int>(); foreach (PropertyItem x in props) { if (x.Id == 0x5102) { for (int i = 0; i < x.Len; i += 3) { set.Add(Color.FromArgb(252, x.Value[i], x.Value[i + 1], x.Value[i + 2]).ToArgb()); } } } if (set.Count > 256) { Bitmap bmp = new Bitmap(1, set.Count); GetIndexedItem(bmp, pal, 255); } else { pal.Data = set.ToList(); while (pal.Data.Count < 256) { pal.Data.Add(Constant.Colors.PaletteBlack); } } //HashSet<int> set = new HashSet<int>(); //FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]); //for (int k = 0; k < framecount; k++) //{ // img.SelectActiveFrame(fd, k); // Bitmap src = new Bitmap(img); // Rectangle rect = new Rectangle(0, 0, src.Width, src.Height); // BitmapData bmpData = src.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // byte* ptr = (byte*)bmpData.Scan0; // for (int j = 0; j < src.Height; j++) // { // for (int i = 0; i < src.Width; i++) // { // if (ptr[3] != 0) set.Add(Color.FromArgb(ptr[3], ptr[2], ptr[1], ptr[0]).ToArgb()); // ptr += 4; // } // ptr += bmpData.Stride - bmpData.Width * 4; // } // src.UnlockBits(bmpData); // src.Dispose(); //} //pal.Data = set.ToList(); }
private void MainPanel_DragDrop(object sender, DragEventArgs e) { if (MainPanel.AllowDropOpen) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string path = files.Last(); try { PalFile palFile = new PalFile(path); if (MainPanel.PalSource != null) { switch (MyMessageBox.Show(Language.DICT["MainTitle"], Language.DICT["MsgInfoHintForSave"], MyMessageBoxButtons.YesNoCancel)) { case DialogResult.Yes: SaveToolStripMenuItem_Click(null, new EventArgs()); break; case DialogResult.No: break; default: return; } } SavePath = path; IsSaved = true; Undos.Clear(); Redos.Clear(); MainPanel.Selections.Clear(); MainPanel.PalSource = palFile; MainPanel.Refresh(); MainPanel_SelectedIndexChanged(null, new EventArgs()); MainPanel_BackColorChanged(null, new EventArgs()); UpdateTitle(Language.DICT["MainTitleEmptyName"]); CurrentStatusLabel.Text = Language.DICT["StslblOpenSucceed"]; } catch (Exception ex) { SavePath = ""; IsSaved = false; MainPanel.Close(); UpdateTitle(Language.DICT["MainTitleEmptyName"]); CurrentStatusLabel.Text = Language.DICT["StslblOpenFailed"]; MyMessageBox.Show(Language.DICT["MainTitle"], Language.DICT["MsgFatalOpen"] + ex.Message); } } } }
public Sort(PalFile pal, List <byte> selects) { InitializeComponent(); Misc.SetLanguage(this); OrigionPal = pal; OriginSelections = selects; PreviewPanel.PalSource = new PalFile(); for (int i = 0; i < 256; i++) { PreviewPanel.PalSource[(byte)i] = OrigionPal[(byte)i]; } PreviewPanel.Selections = OriginSelections; PreviewPanel.Refresh(); InitializeRadioButtons(); }
public Paste(PalFile palFile, List <Tuple <byte, int> > data, byte nowSelectOn) { InitializeComponent(); Misc.SetLanguage(this); OriginDatas = data; OriginPal = palFile; OriginSelecting = nowSelectOn; PreviewPanel.PalSource = new PalFile(); for (int i = 0; i < 256; i++) { PreviewPanel.PalSource[(byte)i] = OriginPal[(byte)i]; } PreviewPanel.Selections.Add(OriginSelecting); RbtnPasteTo_CheckedChanged(null, new EventArgs()); UpdatePreview(); }
public Palette(PalFile originalPalette, string name = "", bool objectPalette = false) { try { _originalPalette = originalPalette; _isObjectPalette = objectPalette; if (!string.IsNullOrEmpty(name)) { Name = name; } else { Name = Path.GetFileNameWithoutExtension(originalPalette.FileName); } } catch (Exception) { throw; } }
public static byte FindBestColor(Color color, PalFile pal) { byte bestIdx = 0; int minDistance = 195075; // 255* 255* 3 for (int i = 0; i < 256; ++i) { Color palColor = Color.FromArgb(pal[(byte)i]); int deltaR = palColor.R - color.R; int deltaG = palColor.G - color.G; int deltaB = palColor.B - color.B; int delta = deltaR * deltaR + deltaG * deltaG + deltaB * deltaB; if (delta < minDistance) { minDistance = delta; bestIdx = (byte)i; } } return(bestIdx); }
public unsafe static void GetIndexedItem(Image img, PalFile pal, int maxNum, int mode = 2) { List <int> myPalette = new List <int>(); if (img.PixelFormat == PixelFormat.Format8bppIndexed) { foreach (Color c in img.Palette.Entries) { myPalette.Add(c.ToArgb()); } } else { switch (mode) { case 0: // Even HashSet <int> _set = new HashSet <int>(); Bitmap pic = new Bitmap(img); BitmapData picData = pic.LockBits( new Rectangle(0, 0, pic.Width, pic.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb ); byte *__ptr = (byte *)picData.Scan0; for (int j = 0; j < pic.Height; ++j) { for (int i = 0; i < pic.Width; ++i) { int argb = Color.FromArgb(252, __ptr[2], __ptr[1], __ptr[0]).ToArgb(); _set.Add(argb); __ptr += 4; } __ptr += picData.Stride - picData.Width * 4; } int STEP = 1; while (_set.Count > 255) { var setarr = _set.ToArray(); foreach (int argb in setarr) { Color clr = Color.FromArgb(argb); int nR = clr.R / (int)Math.Pow(8, STEP) * (int)Math.Pow(8, STEP); int nG = clr.G / (int)Math.Pow(8, STEP) * (int)Math.Pow(8, STEP); int nB = clr.B / (int)Math.Pow(4, STEP) * (int)Math.Pow(4, STEP); clr = Color.FromArgb(252, nR, nG, nB); } _set.Clear(); foreach (int argb in setarr) { _set.Add(argb); } ++STEP; } myPalette = _set.ToList(); break; case 1: // Frequency { Dictionary <int, int> filt = new Dictionary <int, int>(); Bitmap tmp = new Bitmap(img); BitmapData tmpData = tmp.LockBits( new Rectangle(0, 0, tmp.Width, tmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb ); byte *_ptr = (byte *)tmpData.Scan0; for (int j = 0; j < tmp.Height; ++j) { for (int i = 0; i < tmp.Width; ++i) { int argb = Color.FromArgb(252, _ptr[2], _ptr[1], _ptr[0]).ToArgb(); if (filt.ContainsKey(argb)) { ++filt[argb]; } else { filt[argb] = 1; } _ptr += 4; } _ptr += tmpData.Stride - tmpData.Width * 4; } var retarr = filt.ToArray(); retarr.OrderByDescending(a => a.Value); int cnt = Math.Min(255, retarr.Count()); for (int i = 0; i <= cnt; ++i) { myPalette.Add(retarr[i].Key); } } break; case 2: // Mid Cut default: HashSet <int> set = new HashSet <int>(); ImageFactory factory = new ImageFactory(); factory.Load(img); ISupportedImageFormat format = new PngFormat { Quality = 100, IsIndexed = true, Quantizer = new OctreeQuantizer(maxNum, 8) }; factory.Format(format); MemoryStream stream = new MemoryStream(); factory.Save(stream); img = Image.FromStream(stream); stream.Dispose(); Bitmap src = new Bitmap(img); Rectangle rect = new Rectangle(0, 0, src.Width, src.Height); BitmapData bmpData = src.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); byte * ptr = (byte *)bmpData.Scan0; for (int j = 0; j < src.Height; j++) { for (int i = 0; i < src.Width; i++) { int argb = Color.FromArgb(252, ptr[2], ptr[1], ptr[0]).ToArgb(); set.Add(argb); ptr += 4; } ptr += bmpData.Stride - bmpData.Width * 4; } src.UnlockBits(bmpData); img = src; myPalette = set.ToList(); break; } } while (myPalette.Count < 256) { myPalette.Add(Constant.Colors.PaletteBlack); } pal.Data = myPalette; }