private IEnumerable <CfsBitmap> LoadCfsBitmapFromBlob(LoadedBlobFile blob) { var entries = blob.BlobFile.Entries .Where(e => e.Name.Trim().ToLower().EndsWith(".cfs")) .SelectMany(e => { using (var stream = new MemoryStream()) { blob.Stream.Seek(e.Offset, SeekOrigin.Begin); blob.Stream.CopyTo(stream, (int)e.Size); stream.Seek(0, SeekOrigin.Begin); var spriteFile = new SpriteFile(); spriteFile.Deserialize(stream); return(CfsBitmap.FromSpriteFile(spriteFile, blob.BlobName, e.Name)); } }).ToList(); blob.Stream.Close(); blob.Stream.Dispose(); return(entries); }
public static void Main(string[] args) { using (var input = File.OpenRead("AtlasFlying.cfs")) { var cfs = new SpriteFile(); cfs.Deserialize(input); } }
private static void Export(string inputPath, string outputPath) { var inputBytes = File.ReadAllBytes(inputPath); if (BitConverter.ToUInt32(inputBytes, 0) == 0x67687361) // 'ashg' { inputBytes = RLE.Decompress(inputBytes, 0, inputBytes.Length); } var file = new SpriteFile(); using (var input = new MemoryStream(inputBytes, false)) { file.Deserialize(input, Endian.Little); } var sprite = file.Sprite; if (sprite.Texture != null) { var texture = sprite.Texture.Value; if (sprite.Palettes.Length == 1) { var bitmapPath = Path.ChangeExtension(outputPath, ".png"); var data = ExportPalettized(texture, sprite.Palettes[0]); var bitmap = MakeBitmapPalettized( texture.TotalWidth, texture.TotalHeight, data, sprite.Palettes[0]); using (bitmap) { bitmap.Save(bitmapPath, ImageFormat.Png); } } else { int i = 0; foreach (var palette in sprite.Palettes) { var bitmapPath = Path.ChangeExtension($"{outputPath}_{i}", ".png"); var data = ExportPalettized(texture, palette); var bitmap = MakeBitmapPalettized( texture.TotalWidth, texture.TotalHeight, data, palette); using (bitmap) { bitmap.Save(bitmapPath, ImageFormat.Png); } i++; } } } }
public void InitializeWithEntryAndStream(BlobFile.Entry entry, Stream stream) { var dontFixSpecialColors = true; sprite = new SpriteFile(); sprite.Deserialize(stream); lblFileName.Text = entry.Name; if (!string.IsNullOrWhiteSpace(sprite.Category)) { lblFileName.Text = $"{entry.Name} ({sprite.Category})"; } pictureBoxPreview.Width = sprite.Width; pictureBoxPreview.Height = sprite.Height; bitmap = new Bitmap(sprite.Width, sprite.Height, PixelFormat.Format8bppIndexed); var palette = bitmap.Palette; var shadowIndex = 256 - sprite.ShadowCount; var lightIndex = shadowIndex - sprite.LightCount; for (int i = 0; i < 256; i++) { var color = sprite.Palette[i]; var r = (int)((color >> 16) & 0xFF); var g = (int)((color >> 8) & 0xFF); var b = (int)((color >> 0) & 0xFF); //var a = (int)((color >> 24) & 0xFF); int a; if (i == 0) { // transparent pixel a = 0; } else if (sprite.ShadowCount > 0 && i >= shadowIndex) { if (dontFixSpecialColors == false) { // make shadows black+alpha a = 64 + (((i - shadowIndex) + 1) * 16); r = g = b = 0; } else { a = 255; } } else if (sprite.LightCount > 0 && i >= lightIndex) { if (dontFixSpecialColors == false) { // make lights white+alpha a = 64 + (((i - lightIndex) + 1) * 4); r = g = b = 255; } else { a = 255; } } /*else if (i > sprite.MaxSolidIndex) * { * a = 0; * }*/ else { a = 255; } palette.Entries[i] = Color.FromArgb(a, r, g, b); } this.palette = palette; RenderFrameAtIndex(index); if (sprite.Frames.Count() == 1) { btnPlayStop.Enabled = false; } }
public static void Main(string[] args) { bool showHelp = false; bool dontFixSpecialColors = false; var options = new OptionSet() { { "ncf|no-color-fix", "don't fix special colors (such as shadows, lights)", v => dontFixSpecialColors = v != null }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (extras.Count < 1 || extras.Count > 2 || showHelp == true) { Console.WriteLine("Usage: {0} [OPTIONS]+ input_cfs [output_png]", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } string inputPath = extras[0]; string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, ".png"); SpriteFile sprite; using (var input = File.OpenRead(inputPath)) { sprite = new SpriteFile(); sprite.Deserialize(input); } var bitmap = new Bitmap( sprite.Width * sprite.ColumnCount, sprite.Height * sprite.RowCount, PixelFormat.Format8bppIndexed); var palette = bitmap.Palette; var shadowIndex = 256 - sprite.ShadowCount; var lightIndex = shadowIndex - sprite.LightCount; for (int i = 0; i < 256; i++) { var color = sprite.Palette[i]; var r = (int)((color >> 16) & 0xFF); var g = (int)((color >> 8) & 0xFF); var b = (int)((color >> 0) & 0xFF); //var a = (int)((color >> 24) & 0xFF); int a; if (i == 0) { // transparent pixel a = 0; } else if (sprite.ShadowCount > 0 && i >= shadowIndex) { if (dontFixSpecialColors == false) { // make shadows black+alpha a = 64 + (((i - shadowIndex) + 1) * 16); r = g = b = 0; } else { a = 255; } } else if (sprite.LightCount > 0 && i >= lightIndex) { if (dontFixSpecialColors == false) { // make lights white+alpha a = 64 + (((i - lightIndex) + 1) * 4); r = g = b = 255; } else { a = 255; } } /*else if (i > sprite.MaxSolidIndex) * { * a = 0; * }*/ else { a = 255; } palette.Entries[i] = Color.FromArgb(a, r, g, b); } bitmap.Palette = palette; for (int i = 0, y = 0; y < sprite.Height * sprite.RowCount; y += sprite.Height) { for (int x = 0; x < sprite.Width * sprite.ColumnCount; x += sprite.Width) { var frame = sprite.Frames[i++]; if (frame.Width == 0 || frame.Height == 0) { continue; } var area = new Rectangle( x + frame.X, y + frame.Y, frame.Width, frame.Height); var data = bitmap.LockBits(area, ImageLockMode.WriteOnly, bitmap.PixelFormat); var scan = data.Scan0; for (int o = 0; o < frame.Height * frame.Width; o += frame.Width) { Marshal.Copy(frame.Pixels, o, scan, frame.Width); scan += data.Stride; } bitmap.UnlockBits(data); } } bitmap.Save(outputPath, ImageFormat.Png); }
public static void GenerateCFSPreview(string inputFile, string outputFile, bool dontFixSpecialColors = false) { SpriteFile sprite; using (var input = File.OpenRead(inputFile)) { sprite = new SpriteFile(); sprite.Deserialize(input); } var bitmap = new Bitmap( sprite.Width, sprite.Height, PixelFormat.Format8bppIndexed); var palette = bitmap.Palette; var shadowIndex = 256 - sprite.ShadowCount; var lightIndex = shadowIndex - sprite.LightCount; for (int i = 0; i < 256; i++) { var color = sprite.Palette[i]; var r = (int)((color >> 16) & 0xFF); var g = (int)((color >> 8) & 0xFF); var b = (int)((color >> 0) & 0xFF); //var a = (int)((color >> 24) & 0xFF); int a; if (i == 0) { // transparent pixel a = 0; } else if (sprite.ShadowCount > 0 && i >= shadowIndex) { if (dontFixSpecialColors == false) { // make shadows black+alpha a = 64 + (((i - shadowIndex) + 1) * 16); r = g = b = 0; } else { a = 255; } } else if (sprite.LightCount > 0 && i >= lightIndex) { if (dontFixSpecialColors == false) { // make lights white+alpha a = 64 + (((i - lightIndex) + 1) * 4); r = g = b = 255; } else { a = 255; } } /*else if (i > sprite.MaxSolidIndex) * { * a = 0; * }*/ else { a = 255; } palette.Entries[i] = Color.FromArgb(a, r, g, b); } bitmap.Palette = palette; for (int i = 0, y = 0; y < sprite.Height; y += sprite.Height) { for (int x = 0; x < sprite.Width; x += sprite.Width) { try { var frame = sprite.Frames[0]; if (frame.Width == 0 || frame.Height == 0) { continue; } var area = new Rectangle( x + frame.X, y + frame.Y, frame.Width, frame.Height); var data = bitmap.LockBits(area, ImageLockMode.WriteOnly, bitmap.PixelFormat); var scan = data.Scan0; for (int o = 0; o < frame.Height * frame.Width; o += frame.Width) { Marshal.Copy(frame.Pixels, o, scan, frame.Width); scan += data.Stride; } bitmap.UnlockBits(data); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } bitmap.Save(outputFile, ImageFormat.Png); }
private static Bitmap GenerateBitmapFromCfs(Stream s, string fileName) { bool dontFixSpecialColors = true; var sprite = new SpriteFile(); sprite.Deserialize(s, fileName); var bitmap = new Bitmap(sprite.Width * sprite.ColumnCount, sprite.Height * sprite.RowCount, PixelFormat.Format8bppIndexed); var palette = bitmap.Palette; var shadowIndex = 256 - sprite.ShadowCount; var lightIndex = shadowIndex - sprite.LightCount; for (int i = 0; i < 256; i++) { var color = sprite.Palette[i]; var r = (int)((color >> 16) & 0xFF); var g = (int)((color >> 8) & 0xFF); var b = (int)((color >> 0) & 0xFF); //var a = (int)((color >> 24) & 0xFF); int a; if (i == 0) { // transparent pixel a = 0; } else if (sprite.ShadowCount > 0 && i >= shadowIndex) { if (dontFixSpecialColors == false) { // make shadows black+alpha a = 64 + (((i - shadowIndex) + 1) * 16); r = g = b = 0; } else { a = 255; } } else if (sprite.LightCount > 0 && i >= lightIndex) { if (dontFixSpecialColors == false) { // make lights white+alpha a = 64 + (((i - lightIndex) + 1) * 4); r = g = b = 255; } else { a = 255; } } /*else if (i > sprite.MaxSolidIndex) * { * a = 0; * }*/ else { a = 255; } palette.Entries[i] = Color.FromArgb(a, r, g, b); } bitmap.Palette = palette; for (int i = 0, y = 0; y < sprite.Height * sprite.RowCount; y += sprite.Height) { for (int x = 0; x < sprite.Width * sprite.ColumnCount; x += sprite.Width) { var frame = sprite.Frames[i++]; if (frame.Width == 0 || frame.Height == 0) { continue; } var area = new Rectangle( x + frame.X, y + frame.Y, frame.Width, frame.Height); var data = bitmap.LockBits(area, ImageLockMode.WriteOnly, bitmap.PixelFormat); var scan = data.Scan0; for (int o = 0; o < frame.Height * frame.Width; o += frame.Width) { Marshal.Copy(frame.Pixels, o, scan, frame.Width); scan += data.Stride; } bitmap.UnlockBits(data); } } return(bitmap); }