private Mipmap GenerateMipmap(Bitmap inputImage, TEXFile.PixelFormat pixelFormat, bool preMultiplyAlpha) { byte[] rgba = new byte[inputImage.Width * inputImage.Height * 4]; for (int y = 0; y < inputImage.Height; y++) { for (int x = 0; x < inputImage.Width; x++) { Color c = inputImage.GetPixel(x, y); if (preMultiplyAlpha) { float alphamod = (float)c.A / 255.0f; // Normalize. var newR = (byte)(c.R * alphamod); var newG = (byte)(c.G * alphamod); var newB = (byte)(c.B * alphamod); c = Color.FromArgb(c.A, newR, newG, newB); } rgba[y * inputImage.Width * 4 + x * 4 + 0] = c.R; rgba[y * inputImage.Width * 4 + x * 4 + 1] = c.G; rgba[y * inputImage.Width * 4 + x * 4 + 2] = c.B; rgba[y * inputImage.Width * 4 + x * 4 + 3] = c.A; } } byte[] finalImageData = null; switch (pixelFormat) { case TEXFile.PixelFormat.DXT1: finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt1); break; case TEXFile.PixelFormat.DXT3: finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt3); break; case TEXFile.PixelFormat.DXT5: finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt5); break; case TEXFile.PixelFormat.ARGB: finalImageData = rgba; break; } var mipmap = new Mipmap(); mipmap.Width = (ushort)inputImage.Width; mipmap.Height = (ushort)inputImage.Height; mipmap.Pitch = 0; mipmap.ARGBData = finalImageData; return(mipmap); }
private Mipmap GenerateMipmap(Bitmap inputImage, TEXFile.PixelFormat pixelFormat, int width, int height, InterpolationMode mode, bool preMultiplyAlpha) { Bitmap b = new Bitmap(width, height); using (Graphics g = Graphics.FromImage((Image)b)) { g.InterpolationMode = mode; g.DrawImage(inputImage, 0, 0, width, height); } return(GenerateMipmap(b, pixelFormat, preMultiplyAlpha)); }
private void ConvertPNGToTex(string inputFile, FileStream outputStream) { TEXFile.PixelFormat PixelFormat = (TEXFile.PixelFormat)Enum.Parse(typeof(TEXFile.PixelFormat), this.pixelFormatComboBox.Text); InterpolationMode interpolationMode = (InterpolationMode)Enum.Parse(typeof(InterpolationMode), this.mipmapFilterComboBox.Text); Bitmap inputImage = (Bitmap)Bitmap.FromFile(inputFile); inputImage.RotateFlip(RotateFlipType.RotateNoneFlipY); bool GenerateMipmaps = generateMipmapsCheckBox.Checked; List <Mipmap> Mipmaps = new List <Mipmap>(); bool preMultiplyAlpha = preMultiplyAlphaCheckBox.Checked; Mipmaps.Add(GenerateMipmap(inputImage, PixelFormat, preMultiplyAlpha)); if (GenerateMipmaps) { var width = inputImage.Width; var height = inputImage.Height; while (Math.Max(width, height) > 1) { width = Math.Max(1, width >> 1); height = Math.Max(1, height >> 1); Mipmaps.Add(GenerateMipmap(inputImage, PixelFormat, width, height, interpolationMode, preMultiplyAlpha)); } } TEXFile outputTEXFile = new TEXFile(); outputTEXFile.File.Header.Platform = 0; outputTEXFile.File.Header.PixelFormat = (uint)PixelFormat; outputTEXFile.File.Header.TextureType = (uint)EnumHelper <TEXFile.TextureType> .GetValueFromDescription(this.textureTypeComboBox.Text); outputTEXFile.File.Header.NumMips = (uint)Mipmaps.Count; outputTEXFile.File.Header.Flags = 0; MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); foreach (Mipmap mip in Mipmaps) { writer.Write(mip.Width); writer.Write(mip.Height); writer.Write(mip.Pitch); writer.Write((uint)mip.ARGBData.Length); } foreach (Mipmap mip in Mipmaps) { writer.Write(mip.ARGBData); } writer.Close(); outputTEXFile.File.Raw = ms.ToArray(); outputTEXFile.SaveFile(outputStream); }