public static PIXIE FromBitmap(PixelmapFormat format, Bitmap bitmap) { PIXIE pixie = new PIXIE { Format = format, Width = bitmap.Width, Height = bitmap.Height, HalfWidth = bitmap.Width / 2, HalfHeight = bitmap.Height / 2, RowSize = bitmap.Width }; if (pixie.Width % 2 != 0 || pixie.HalfWidth % 2 != 0) { pixie.RowSize = (pixie.HalfWidth + (pixie.HalfWidth % 2 == 0 ? 2 : 1)) * 2; } pixie.PixelCount = pixie.RowSize * pixie.Height; pixie.PixelSize = 1; byte[] data = new byte[pixie.RowSize * pixie.Height]; Dictionary <Colour, byte> lut = new Dictionary <Colour, byte>(); for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < pixie.Width; x++) { Colour c = Colour.FromColor(bitmap.GetPixel(x, y)); if (!lut.TryGetValue(c, out byte index)) { index = (byte)PIX.GamePalette.GetNearestPixelIndex(c); lut.Add(c, index); } data[y * pixie.RowSize + x] = index; } } pixie.SetData(data); return(pixie); }
public static PIX Load(string path) { Logger.LogToFile(Logger.LogLevel.Info, "{0}", path); FileInfo fi = new FileInfo(path); PIX pix = new PIX(); using (var br = new BEBinaryReader(fi.OpenRead(), Encoding.Default)) { if (br.ReadUInt32() != 0x12 || br.ReadUInt32() != 0x08 || br.ReadUInt32() != 0x02 || br.ReadUInt32() != 0x02) { Logger.LogToFile(Logger.LogLevel.Error, "{0} isn't a valid PIX file", path); return null; } PIXIE pixelmap = new PIXIE(); while (br.BaseStream.Position < br.BaseStream.Length) { int tag = (int)br.ReadUInt32(); int length = (int)br.ReadUInt32(); switch (tag) { case 0x03: pixelmap = new PIXIE(); pixelmap.Format = (PIXIE.PixelmapFormat)br.ReadByte(); pixelmap.RowSize = br.ReadUInt16(); pixelmap.Width = br.ReadUInt16(); pixelmap.Height = br.ReadUInt16(); pixelmap.HalfWidth = br.ReadUInt16(); pixelmap.HalfHeight = br.ReadUInt16(); pixelmap.Name = br.ReadString(); break; case 0x21: pixelmap.PixelCount = (int)br.ReadUInt32(); pixelmap.PixelSize = (int)br.ReadUInt32(); pixelmap.SetData(br.ReadBytes(pixelmap.DataLength)); break; case 0x00: pix.pixies.Add(pixelmap); break; case 0x3d: pixelmap = new PIXIE(); pixelmap.Format = (PIXIE.PixelmapFormat)br.ReadByte(); pixelmap.RowSize = br.ReadUInt16(); pixelmap.Width = br.ReadUInt16(); pixelmap.Height = br.ReadUInt16(); br.ReadBytes(6); pixelmap.Name = br.ReadString(); break; default: Logger.LogToFile(Logger.LogLevel.Error, "Unknown PIX tag: {0} ({1:x2})", tag, br.BaseStream.Position); return null; } } } return pix; }