public static SegaSaturnTexture LoadFromBitmap(Bitmap bitmap, SegaSaturnColor transparentColor = null) { if (bitmap == null) { return(null); } SegaSaturnTexture toReturn = new SegaSaturnTexture(bitmap.Width, bitmap.Height); using (BmpPixelSnoop tmp = new BmpPixelSnoop(bitmap)) { for (int y = 0; y < tmp.Height; y++) { for (int x = 0; x < tmp.Width; x++) { Color c = tmp.GetPixel(x, y); if (c.A != 255 && transparentColor != null && transparentColor.A == 255) { c = transparentColor; } toReturn.SetPixel(x, y, c); } } } return(toReturn); }
public static void ReplaceColor(this Bitmap img, SegaSaturnColor src, SegaSaturnColor dest) { using (BmpPixelSnoop tmp = new BmpPixelSnoop(img)) { int srcColor = src.ToArgb(); for (int y = 0; y < tmp.Height; ++y) { for (int x = 0; x < tmp.Width; ++x) { if (tmp.GetPixel(x, y).ToArgb() == srcColor) { tmp.SetPixel(x, y, dest); } } } } }
public static bool ContainsColor(this Bitmap img, SegaSaturnColor toFind) { using (BmpPixelSnoop tmp = new BmpPixelSnoop(img)) { for (int y = 0; y < tmp.Height; ++y) { for (int x = 0; x < tmp.Width; ++x) { if (tmp.GetPixel(x, y) == toFind) { return(true); } } } return(true); } }
public static bool IsTransparent(this Bitmap img, SegaSaturnColor transparentColor) { using (BmpPixelSnoop tmp = new BmpPixelSnoop(img)) { int tc = transparentColor.ToArgb(); for (int y = 0; y < tmp.Height; ++y) { for (int x = 0; x < tmp.Width; ++x) { Color c = tmp.GetPixel(x, y); if (c.A > 0 || c.ToArgb() == tc) { return(false); } } } return(true); } }
public static void To24BitsTga(Bitmap bitmap, Stream output, SegaSaturnColor transparentColor = null) { using (BinaryWriter writer = new BinaryWriter(output)) { using (BmpPixelSnoop tmp = new BmpPixelSnoop(bitmap)) { writer.Write(new byte[] { 0, // ID length 0, // no color map 2, // uncompressed, true color 0, 0, 0, 0, 0, 0, 0, 0, 0, // x and y origin (byte)(tmp.Width & 0x00FF), (byte)((tmp.Width & 0xFF00) >> 8), (byte)(tmp.Height & 0x00FF), (byte)((tmp.Height & 0xFF00) >> 8), 24, // 24 bit bitmap 0 }); for (int y = 0; y < tmp.Height; y++) { for (int x = 0; x < tmp.Width; x++) { Color c = tmp.GetPixel(x, tmp.Height - y - 1); if (c.A != 255 && transparentColor != null && transparentColor.A == 255) { c = transparentColor; } writer.Write(new[] { c.B, c.G, c.R }); } } } } }
public static void ToBin(Bitmap bmp, Stream outputStream, bool header) { using (BinaryWriter writer = new BinaryWriter(outputStream)) { if (header) { writer.Write((ushort)bmp.Width); writer.Write((ushort)bmp.Height); } using (BmpPixelSnoop tmp = new BmpPixelSnoop(bmp)) { for (int y = 0; y < tmp.Height; ++y) { for (int x = 0; x < tmp.Width; ++x) { writer.Write(new SegaSaturnColor(tmp.GetPixel(x, y)).SaturnColor); } } } } }
public static Bitmap ToBitmap(SegaSaturnTexture texture, SegaSaturnColor transparentColor = null) { if (texture == null) { return(null); } Bitmap toReturn = new Bitmap(texture.Width, texture.Height, PixelFormat.Format32bppArgb); using (BmpPixelSnoop tmp = new BmpPixelSnoop(toReturn)) { for (int y = 0; y < tmp.Height; y++) { for (int x = 0; x < tmp.Width; x++) { SegaSaturnColor color = texture.GetPixel(x, y); toReturn.SetPixel(x, y, transparentColor != null && color == transparentColor ? Color.Transparent : (Color)color); } } } return(toReturn); }
public static Bitmap LoadBitmapFromFile(string path, SegaSaturnColor transparentColor = null) { if (Path.GetExtension(path).ToLowerInvariant() == ".bin") { using (Stream stream = File.Open(path, FileMode.Open)) { using (BinaryReader streamReader = new BinaryReader(stream)) { Bitmap bin = new Bitmap(streamReader.ReadUInt16(), streamReader.ReadUInt16(), PixelFormat.Format32bppArgb); using (BmpPixelSnoop tmp = new BmpPixelSnoop(bin)) { for (int y = 0; y < tmp.Height; ++y) { for (int x = 0; x < tmp.Width; ++x) { tmp.SetPixel(x, y, new SegaSaturnColor(streamReader.ReadUInt16())); } } } return(bin); } } } if (Path.GetExtension(path).ToLowerInvariant() == ".tga") { Bitmap tga = TargaImage.LoadTargaImage(path); if (transparentColor != null) { tga.ReplaceColor(transparentColor, Color.Transparent); } return(tga); } Bitmap bitmap = (Bitmap)Bitmap.FromFile(path); if (transparentColor != null) { bitmap.ReplaceColor(transparentColor, Color.Transparent); } return(bitmap); }