protected override void Run(Workbench workbench, ILogger logger) { string fmtName = FindUnnamedParameter(0).Values[0].ToString(); bool sloppy = FindNamedParameter("--sloppy").IsPresent; if (!(ColorFormat.GetFormat(fmtName) is ColorFormat fmt)) { logger?.Log("Unknown color format \"" + fmtName + "\".", LogLevel.Error); return; } BitmapData bmpData = workbench.Bitmap.LockBits( new Rectangle(0, 0, workbench.Bitmap.Width, workbench.Bitmap.Height), ImageLockMode.ReadWrite, workbench.Bitmap.PixelFormat ); byte[] buffer = new byte[bmpData.Stride * workbench.Bitmap.Height]; Marshal.Copy(bmpData.Scan0, buffer, 0, buffer.Length); int mayNeedSloppy = 0; for (int i = 0; i < buffer.Length; i += 4) { int cb = BitConverter.ToInt32(buffer, i); int ca = fmt.Convert(cb, workbench.BitmapFormat, sloppy); if (!sloppy && fmt.Bits < workbench.BitmapFormat.Bits) { // Convert sloppy and back. int csa = fmt.Convert(cb, workbench.BitmapFormat, true); int csb = workbench.BitmapFormat.Convert(csa, fmt, true); // Check if original color may be sloppy, and this would change the output. if (cb == csb && ca != csa) { mayNeedSloppy++; } } buffer[i + 0] = (byte)(ca >> 0); buffer[i + 1] = (byte)(ca >> 8); buffer[i + 2] = (byte)(ca >> 16); buffer[i + 3] = (byte)(ca >> 24); } Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length); workbench.Bitmap.UnlockBits(bmpData); workbench.BitmapFormat = fmt; if (mayNeedSloppy >= workbench.Bitmap.Width * workbench.Bitmap.Height) { logger?.Log("This bitmap appears to be improperly scaled. The --sloppy flag may be required.", LogLevel.Warning); } logger?.Log("Converted bitmap to color format " + fmtName + "."); }
protected override void Run(Workbench workbench, ILogger logger) { string fmtName = FindUnnamedParameter(0).Values[0].ToString(); bool sloppy = FindNamedParameter("--sloppy").IsPresent; if (!(ColorFormat.GetFormat(fmtName) is ColorFormat fmt)) { logger?.Log("Unknown color format \"" + fmtName + "\".", LogLevel.Error); return; } foreach (Palette pal in workbench.PaletteSet.Select(pe => pe.Palette)) { for (int i = 0; i < pal.Count; i++) { pal[i] = fmt.Convert(pal[i], pal.Format, sloppy); } pal.Format = fmt; } logger?.Log("Converted palettes to " + fmtName + ".", LogLevel.Information); }
protected override void Run(Workbench workbench, ILogger logger) { string fmtName = FindUnnamedParameter(0).Values[0].Value; int palNum = FindNamedParameter("--palette-number").Values[0].ToInt32(); int palSize = FindNamedParameter("--palette-size").Values[0].ToInt32(); int palCount = FindNamedParameter("--palette-count").Values[0].ToInt32(); long offset = FindNamedParameter("--offset").Values[0].ToInt64(); if (palNum < -1) { logger?.Log("Invalid palette number.", LogLevel.Error); return; } if (palSize == 0 || palSize < -1) { logger?.Log("Invalid palette size.", LogLevel.Error); return; } if (palCount < 0) { logger?.Log("Invalid palette count.", LogLevel.Error); return; } if (offset < 0) { logger?.Log("Invalid offset.", LogLevel.Error); return; } if (!(ColorFormat.GetFormat(fmtName) is ColorFormat fmt)) { logger?.Log("Unknown color format \"" + fmtName + "\".", LogLevel.Error); return; } workbench.Stream.Position = Math.Min(offset, workbench.Stream.Length); // Bytes per color. int bpc = (fmt.Bits + 7) / 8; int addedColors = 0; int addedPalettes = 0; byte[] buffer = new byte[4]; Palette pal = null; while (palCount > 0 && workbench.Stream.Read(buffer, 0, bpc) == bpc) { // Create new palette if needed. if (pal == null) { pal = new Palette(fmt, palSize); } // Add color to palette. int c = BitConverter.ToInt32(buffer, 0); pal.Add(c); addedColors++; // Add finished palette to palette set. if (palSize != -1 && pal.Count >= palSize) { if (palNum < 0) { workbench.PaletteSet.Add(pal); } else { workbench.PaletteSet.Add(pal, palNum++); } addedPalettes++; pal = null; if (--palCount <= 0) { break; } } } if (pal != null) { if (palNum < 0) { workbench.PaletteSet.Add(pal); } else { workbench.PaletteSet.Add(pal, palNum++); } addedPalettes++; } if (FindNamedParameter("--palette-size").IsPresent) { logger?.Log("Deserialized " + addedPalettes + " palettes with " + palSize + " colors each (" + addedColors + " colors total)."); } else { logger?.Log("Deserialized " + addedPalettes + " palettes with " + addedColors + " colors total."); } }