static void Main(string[] args) { List <spPalette> pals = new List <spPalette>(); List <spPixels> pics = new List <spPixels>(); Console.WriteLine("png2c1"); Console.WriteLine("{0}", args.Length); byte[] scbs = new byte[200]; // 200 scanlines byte scb_index = 0; foreach (string arg in args) { String pngPath = arg; String palPath = Path.ChangeExtension(pngPath, ".pal"); //-------------------------------------------------------------- // Read in the palette file (thanks Pro Motion) Console.WriteLine($"Loading {palPath}"); spPalette pal = new spPalette(); using (FileStream palStream = new FileStream(palPath, FileMode.Open, FileAccess.Read)) { for (int idx = 0; idx < 16; ++idx) { int r = palStream.ReadByte(); int g = palStream.ReadByte(); int b = palStream.ReadByte(); pal.colors.Add(Color.FromArgb(255, r, g, b)); } } // Put it in the list pals.Add(pal); //-------------------------------------------------------------- // Read in the image file Console.WriteLine($"Loading {pngPath}"); using (var image = new Bitmap(System.Drawing.Image.FromFile(pngPath))) { //Bitmap image = new Bitmap(pngStream); Console.WriteLine("{0} width={1}, height={2}", pngPath, image.Width, image.Height); List <byte> pixels = new List <byte>(); for (int y = 0; y < image.Height; ++y) { Color p = image.GetPixel(0, y); if (255 == p.A) { scbs[y] = scb_index; } for (int x = 0; x < image.Width; x += 2) { Color p0 = image.GetPixel(x, y); Color p1 = image.GetPixel(x + 1, y); int idx0 = GetIndex(ref pal.colors, p0); int idx1 = GetIndex(ref pal.colors, p1); byte pb = (byte)((idx0 << 4) | (idx1)); pixels.Add(pb); } } spPixels pic = new spPixels(image.Width, image.Height, pixels); pics.Add(pic); } scb_index++; } //foreach (byte scb in scbs) //{ // Console.WriteLine("{0:X2}", scb); //} String outPath = Path.ChangeExtension(args[0], ".c1"); Console.WriteLine("Saving {0}", outPath); using (BinaryWriter b = new BinaryWriter( File.Open(outPath, FileMode.Create))) { for (int y = 0; y < scbs.Length; ++y) { int imageIdx = scbs[y]; spPixels pix = pics[imageIdx]; int pixIndex = 160 * y; // Write out the scanline of pix for (int idx = 0; idx < 160; ++idx) { b.Write((byte)pix.m_pixels[pixIndex + idx]); } } // Save out the scbs b.Write(scbs); // Pad out the page for (int idx = 0; idx < 56; ++idx) { b.Write((byte)0x00); } // Save out the palettes for (int palIdx = 0; palIdx < 16; ++palIdx) { if (palIdx < pals.Count) { spPalette pal = pals[palIdx]; for (int idx = 0; idx < 16; ++idx) { UInt16 rgb = ToGSColor(pal.colors[idx]); b.Write(rgb); } } else { for (int idx = 0; idx < 16; ++idx) { b.Write((UInt16)0); } } } } }
static void Main(string[] args) { List <spPalette> pals = new List <spPalette>(); List <spPixels> pics = new List <spPixels>(); Console.WriteLine("png2tiles"); Console.WriteLine("{0}", args.Length); foreach (string arg in args) { String pngPath = arg; String palPath = Path.ChangeExtension(pngPath, ".pal"); //-------------------------------------------------------------- // Read in the palette file (thanks Pro Motion) Console.WriteLine($"Loading {palPath}"); spPalette pal = new spPalette(); using (FileStream palStream = new FileStream(palPath, FileMode.Open, FileAccess.Read)) { for (int idx = 0; idx < 16; ++idx) { int r = palStream.ReadByte(); int g = palStream.ReadByte(); int b = palStream.ReadByte(); pal.colors.Add(Color.FromArgb(255, r, g, b)); } } // Put it in the list pals.Add(pal); //-------------------------------------------------------------- // Read in the image file Console.WriteLine($"Loading {pngPath}"); using (var image = new Bitmap(System.Drawing.Image.FromFile(pngPath))) { //Bitmap image = new Bitmap(pngStream); Console.WriteLine("{0} width={1}, height={2}", pngPath, image.Width, image.Height); List <byte> pixels = new List <byte>(); for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; x += 2) { Color p0 = image.GetPixel(x, y); Color p1 = image.GetPixel(x + 1, y); int idx0 = GetIndex(ref pal.colors, p0); int idx1 = GetIndex(ref pal.colors, p1); byte pb = (byte)((idx0 << 4) | (idx1)); pixels.Add(pb); } } spPixels pic = new spPixels(image.Width, image.Height, pixels); pics.Add(pic); } } String outPath = Path.ChangeExtension(args[0], ".gs"); Console.WriteLine("Saving {0}", outPath); using (BinaryWriter b = new BinaryWriter( File.Open(outPath, FileMode.Create))) { spPixels pix = pics[0]; for (int y = 0; y < pix.m_height; y += 8) { for (int x = 0; x < pix.m_width; x += 8) { int offset = y * (pix.m_width >> 1) + (x >> 1); for (int idy = 0; idy < 8; ++idy) { int boffset = offset + (idy * (pix.m_width >> 1)); for (int idx = 0; idx < 4; ++idx) { b.Write((byte)pix.m_pixels[boffset + idx]); } } } } } }