/// <summary> /// Generates a full set of mipmaps for the texture. /// </summary> /// <param name="overwriteExistingMipmaps">true if the existing mipmap set is replaced with the new set; false otherwise.</param> public virtual void GenerateMipmaps(bool overwriteExistingMipmaps) { var imageAttr = new ImageAttributes(); imageAttr.SetWrapMode(WrapMode.TileFlipXY); // If we already have mipmaps and we're not supposed to overwrite // them then return without any generation. if (!overwriteExistingMipmaps && faces.Any(f => f.Count > 1)) return; // Generate the mips for each face. foreach (var face in faces) { // Remove any existing mipmaps. var faceBitmap = face[0]; face.Clear(); face.Add(faceBitmap); int width = faceBitmap.Width, height = faceBitmap.Height; while (width > 1 && height > 1) { var systemBitmap = face[face.Count-1].ToSystemBitmap(); width /= 2; height /= 2; var bitmap = new Bitmap(width,height); using (var graphics = System.Drawing.Graphics.FromImage(bitmap)) { var destRect = new System.Drawing.Rectangle(0, 0, width, height); graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; graphics.DrawImage(systemBitmap, destRect, 0, 0, width * 2, height * 2, GraphicsUnit.Pixel, imageAttr); } face.Add(bitmap.ToXnaBitmap(false)); //we dont want to flip textures twice systemBitmap.Dispose(); } } }
internal static void Resize(this TextureContent content, int newWidth, int newHeight) { // TODO: This should be refactored to use FreeImage // with a higher quality filter. var destination = new Bitmap(newWidth, newHeight); using (var source = content.Faces[0][0].ToSystemBitmap()) using (var graphics = System.Drawing.Graphics.FromImage(destination)) { var imageAttr = new ImageAttributes(); imageAttr.SetWrapMode(WrapMode.TileFlipXY); var destRect = new System.Drawing.Rectangle(0, 0, newWidth, newHeight); graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; graphics.DrawImage(source, destRect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, imageAttr); } content.Faces[0][0] = destination.ToXnaBitmap(false); //we dont want to flip colors twice }