private void ReplaceImgBtn_Click(object sender, EventArgs e) { var text = KeyGenerator.OpenOrSaveFile("Select the image file to replace the texture.", "All Supported Formats|*.dds;*.bmp;*.jpg;*.gif;*.png|DDS Texture|*.dds|Bitmap|*.bmp|JPEG|*.jpg|Graphics Interchange Format|*.gif|Portable Network Graphics|*.png", true); if (text == "") { return; } Image image; if (text.EndsWith(".dds", StringComparison.OrdinalIgnoreCase)) { image = new DdsTexture(text).GetImage(); } else { image = Image.FromFile(text); } if (!image.Size.Equals(_size0) && DialogResult.Yes == MessageBox.Show( "The image dimensions don't match. Do you wish scale to the original dimension? (Ratio may change!)", "Replace Texture", MessageBoxButtons.YesNo)) { image = KeyGenerator.ScaleImage(image, _size0); } _currentTexFile.ReplaceTexture(_imgList.SelectedIndex, image, _currentTexturePixelFormat); _rebuildBtn.Enabled = true; }
/// <summary> /// Decode BMP Header. /// </summary> /// <param name="reader">Binary reader.</param> /// <param name="databoxes">Databoxes array.</param> /// <param name="description">Image Description.</param> public unsafe void DecodeData(BinaryReader reader, out DataBox[] databoxes, out ImageDescription description) { reader.BaseStream.Seek(0, SeekOrigin.Begin); byte[] bytes = reader.ReadBytes((int)reader.BaseStream.Length); DdsTexture dds = new DdsTexture(bytes); uint faces = 1; if (dds.MiscFlags10.HasFlag(ResourceMiscFlags.TextureCube)) { faces = 6; } description = new ImageDescription() { imageFormat = ImageFormat.DDS, Width = (uint)dds.Width, Height = (uint)dds.Height, Depth = (uint)dds.Depth, MipLevels = (uint)dds.MipInfos.Length, ArraySize = (uint)dds.Data.Length / faces, Faces = faces, pixelFormat = (WaveEngine.Common.Graphics.PixelFormat)dds.DxgiFormat, }; databoxes = new DataBox[description.ArraySize * description.Faces * description.MipLevels]; for (uint i = 0; i < description.ArraySize; i++) { for (uint j = 0; j < description.Faces; j++) { byte[] data = dds.Data[(i * description.Faces) + j]; GCHandle pinnedHandle = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr dataPointer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0); for (uint z = 0; z < description.MipLevels; z++) { ImageMipInfo mipInfo = dds.MipInfos[z]; uint slicePitch = (uint)mipInfo.SizeInBytes; uint rowPitch = slicePitch / (uint)mipInfo.Height; uint index = (i * description.Faces) + (j * description.MipLevels) + z; databoxes[index] = new DataBox(IntPtr.Add(dataPointer, (int)mipInfo.OffsetInBytes), rowPitch, slicePitch); } pinnedHandle.Free(); } } }