public static void Encode(string[] args) { string outPath = Path.ChangeExtension(args[1], ".pvr"); string outPalettePath = Path.ChangeExtension(args[1], ".pvp"); PvrTextureEncoder texture = new PvrTextureEncoder(args[1], StringToPixelFormat(args[3]), StringToDataFormat(args[4])); // Was this texture initalized successfully if (!texture.Initalized) { Console.WriteLine("Error: Unable to encode using the specified texture."); return; } // Get arguments for (int i = 5; i < args.Length; i++) { string arg = args[i].ToLower(); // Change the output path if (arg == "-o" && args.Length > i + 1) { outPath = args[i + 1]; i++; } // Change the output path for the palette if (arg == "-op" && args.Length > i + 1) { outPalettePath = args[i + 1]; i++; } // No global index if (arg == "-nogbix") { texture.HasGlobalIndex = false; } // Set global index if (arg == "-gi" && args.Length > i + 1) { uint globalIndex; if (!uint.TryParse(args[i + 1], out globalIndex)) { globalIndex = 0; } texture.GlobalIndex = globalIndex; i++; } // Set compression format if (arg == "-cmp" && args.Length > i + 1) { texture.CompressionFormat = StringToCompressionFormat(args[i + 1]); i++; } } Console.WriteLine("Texture Information"); Console.WriteLine("--------------------"); Console.WriteLine("Format : PVR"); if (texture.HasGlobalIndex) { Console.WriteLine("Global Index : {0}", texture.GlobalIndex); } Console.WriteLine("Dimensions : {0}x{1}", texture.TextureWidth, texture.TextureHeight); Console.WriteLine("Pixel Format : {0}", PixelFormatToString(texture.PixelFormat)); Console.WriteLine("Data Format : {0}", DataFormatToString(texture.DataFormat)); if (texture.CompressionFormat != PvrCompressionFormat.None) { Console.WriteLine("Compression : {0}", CompressionFormatToString(texture.CompressionFormat)); } texture.Save(outPath); if (texture.NeedsExternalPalette) { texture.PaletteEncoder.Save(outPalettePath); } Console.WriteLine("\nTexture encoded successfully."); }
public override void Write(Stream source, Stream destination) { // Writing PVR textures is done through VrSharp, so just pass it to that PvrTextureEncoder texture = new PvrTextureEncoder(source, PixelFormat, DataFormat); if (!texture.Initalized) { throw new TextureNotInitalizedException("Unable to initalize texture."); } texture.CompressionFormat = compressionFormat; texture.HasGlobalIndex = HasGlobalIndex; if (texture.HasGlobalIndex) { texture.GlobalIndex = GlobalIndex; } // If we have an external palette file, save it if (texture.NeedsExternalPalette) { needsExternalPalette = true; PaletteStream = new MemoryStream(); texture.PaletteEncoder.Save(PaletteStream); } else { needsExternalPalette = false; } texture.Save(destination); }
private void SaveTextures() { byte[] data; using (MemoryStream str = new MemoryStream()) { PvmArchiveWriter writer = new PvmArchiveWriter(str); foreach (TextureInfo tex in textures) { if (tex.DataFormat != PvrDataFormat.Index4 && tex.DataFormat != PvrDataFormat.Index8) { System.Drawing.Imaging.BitmapData bmpd = tex.Image.LockBits(new Rectangle(Point.Empty, tex.Image.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int stride = bmpd.Stride; byte[] bits = new byte[Math.Abs(stride) * bmpd.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length); tex.Image.UnlockBits(bmpd); int tlevels = 0; for (int y = 0; y < tex.Image.Height; y++) { int srcaddr = y * Math.Abs(stride); for (int x = 0; x < tex.Image.Width; x++) { Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4))); if (c.A == 0) tlevels = 1; else if (c.A < 255) { tlevels = 2; break; } } if (tlevels == 2) break; } if (tlevels == 0) tex.PixelFormat = PvrPixelFormat.Rgb565; else if (tlevels == 1) tex.PixelFormat = PvrPixelFormat.Argb1555; else if (tlevels == 2) tex.PixelFormat = PvrPixelFormat.Argb4444; if (tex.Image.Width == tex.Image.Height) if (tex.Mipmap) tex.DataFormat = PvrDataFormat.SquareTwiddledMipmaps; else tex.DataFormat = PvrDataFormat.SquareTwiddled; else tex.DataFormat = PvrDataFormat.Rectangle; } PvrTextureEncoder encoder = new PvrTextureEncoder(tex.Image, tex.PixelFormat, tex.DataFormat); encoder.GlobalIndex = tex.GlobalIndex; MemoryStream pvr = new MemoryStream(); encoder.Save(pvr); pvr.Seek(0, SeekOrigin.Begin); writer.CreateEntry(pvr, tex.Name); } writer.Flush(); data = str.ToArray(); str.Close(); } if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase)) FraGag.Compression.Prs.Compress(data, filename); else File.WriteAllBytes(filename, data); }
static void Main(string[] args) { string directoryName; UInt32 startingGBIX = 0; List<String> textureNames = new List<String>(); List<PvrTexture> finalTextureList = new List<PvrTexture>(); if (args.Length == 0) { Console.WriteLine("Error - no texturelist provided. Provide a path to a texturelist as your first command line argument"); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); return; } String filePath = args[0]; directoryName = Path.GetDirectoryName(filePath); string archiveName = Path.GetFileNameWithoutExtension(filePath); if (File.Exists(filePath)) { StreamReader texlistStream = File.OpenText(filePath); startingGBIX = UInt32.Parse(texlistStream.ReadLine()); while (!texlistStream.EndOfStream) textureNames.Add(texlistStream.ReadLine()); PvmArchive pvmArchive = new PvmArchive(); Stream pvmStream = File.Open(Path.ChangeExtension(filePath, ".pvm"), FileMode.Create); PvmArchiveWriter pvmWriter = (PvmArchiveWriter)pvmArchive.Create(pvmStream); // Reading in textures for (uint imgIndx = 0; imgIndx < textureNames.Count; imgIndx++) { Bitmap tempTexture = new Bitmap(8, 8); string texturePath = Path.Combine(directoryName, Path.ChangeExtension(textureNames[(int)imgIndx], ".png")); if (File.Exists(texturePath)) { tempTexture = (Bitmap)Bitmap.FromFile(texturePath); tempTexture = tempTexture.Clone(new Rectangle(Point.Empty, tempTexture.Size), System.Drawing.Imaging.PixelFormat.Format32bppArgb); } else { Console.WriteLine(String.Concat("Texture ", textureNames[(int)imgIndx], " not found. Generating a placeholder. Check your files.")); } System.Drawing.Imaging.BitmapData bmpd = tempTexture.LockBits(new Rectangle(Point.Empty, tempTexture.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int stride = bmpd.Stride; byte[] bits = new byte[Math.Abs(stride) * bmpd.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length); tempTexture.UnlockBits(bmpd); int tlevels = 0; for (int y = 0; y < tempTexture.Height; y++) { int srcaddr = y * Math.Abs(stride); for (int x = 0; x < tempTexture.Width; x++) { Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4))); if (c.A == 0) tlevels = 1; else if (c.A < 255) { tlevels = 2; break; } } if (tlevels == 2) break; } PvrPixelFormat ppf = PvrPixelFormat.Rgb565; if (tlevels == 1) ppf = PvrPixelFormat.Argb1555; else if (tlevels == 2) ppf = PvrPixelFormat.Argb4444; PvrDataFormat pdf = PvrDataFormat.Rectangle; if (tempTexture.Width == tempTexture.Height) pdf = PvrDataFormat.SquareTwiddled; PvrTextureEncoder encoder = new PvrTextureEncoder(tempTexture, ppf, pdf); encoder.GlobalIndex = startingGBIX + imgIndx; string pvrPath = Path.ChangeExtension(texturePath, ".pvr"); encoder.Save(pvrPath); pvmWriter.CreateEntryFromFile(pvrPath); } pvmWriter.Flush(); pvmStream.Close(); Console.WriteLine("PVM was compiled successfully!"); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); return; } else // error, supplied path is invalid { Console.WriteLine("Supplied texturelist does not exist!"); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); return; } }