private void BtnSave_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e) { if (modImageBytes != null) { TextureFormat fmt = (TextureFormat)(ddTextureFmt.SelectedIndex + 1); byte[] encImageBytes = TextureEncoderDecoder.Encode(modImageBytes, tex.m_Width, tex.m_Height, fmt); AssetTypeValueField m_StreamData = baseField.Get("m_StreamData"); m_StreamData.Get("offset").GetValue().Set(0); m_StreamData.Get("size").GetValue().Set(0); m_StreamData.Get("path").GetValue().Set(""); baseField.Get("m_TextureFormat").GetValue().Set((int)fmt); baseField.Get("m_Width").GetValue().Set(tex.m_Width); baseField.Get("m_Height").GetValue().Set(tex.m_Height); AssetTypeValueField image_data = baseField.Get("image data"); image_data.GetValue().type = EnumValueTypes.ByteArray; image_data.templateField.valueType = EnumValueTypes.ByteArray; AssetTypeByteArray byteArray = new AssetTypeByteArray() { size = (uint)encImageBytes.Length, data = encImageBytes }; image_data.GetValue().Set(byteArray); } Close(true); }
public static byte[] ImportPng(string file, int width, int height, TextureFormat format) { byte[] decData; using (Image <Rgba32> image = Image.Load <Rgba32>(file)) { image.Mutate(i => i.Flip(FlipMode.Vertical)); if (image.TryGetSinglePixelSpan(out var pixelSpan)) { decData = MemoryMarshal.AsBytes(pixelSpan).ToArray(); } else { return(null); //rip } } byte[] encData = TextureEncoderDecoder.Encode(decData, width, height, format); return(encData); }
private async void BtnSave_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e) { if (modImageBytes != null) { TextureFormat fmt = (TextureFormat)(ddTextureFmt.SelectedIndex + 1); byte[] encImageBytes = TextureEncoderDecoder.Encode(modImageBytes, tex.m_Width, tex.m_Height, fmt); if (encImageBytes == null) { await MessageBoxUtil.ShowDialog(this, "Error", $"Failed to encode texture format {fmt}"); Close(false); return; } AssetTypeValueField m_StreamData = baseField.Get("m_StreamData"); m_StreamData.Get("offset").GetValue().Set(0); m_StreamData.Get("size").GetValue().Set(0); m_StreamData.Get("path").GetValue().Set(""); baseField.Get("m_Name").GetValue().Set(boxName.Text); if (!baseField.Get("m_MipMap").IsDummy()) { baseField.Get("m_MipMap").GetValue().Set(chkHasMipMaps.IsChecked ?? false); } if (!baseField.Get("m_MipCount").IsDummy()) { baseField.Get("m_MipCount").GetValue().Set(1); } if (!baseField.Get("m_ReadAllowed").IsDummy()) { baseField.Get("m_ReadAllowed").GetValue().Set(chkIsReadable.IsChecked ?? false); } AssetTypeValueField m_TextureSettings = baseField.Get("m_TextureSettings"); m_TextureSettings.Get("m_FilterMode").GetValue().Set(ddFilterMode.SelectedIndex); if (int.TryParse(boxAnisotFilter.Text, out int aniso)) { m_TextureSettings.Get("m_Aniso").GetValue().Set(aniso); } if (int.TryParse(boxMipMapBias.Text, out int mipBias)) { m_TextureSettings.Get("m_MipBias").GetValue().Set(mipBias); } m_TextureSettings.Get("m_WrapU").GetValue().Set(ddWrapModeU.SelectedIndex); m_TextureSettings.Get("m_WrapV").GetValue().Set(ddWrapModeV.SelectedIndex); if (boxLightMapFormat.Text.StartsWith("0x")) { if (int.TryParse(boxLightMapFormat.Text, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out int lightFmt)) { baseField.Get("m_LightmapFormat").GetValue().Set(lightFmt); } } else { if (int.TryParse(boxLightMapFormat.Text, out int lightFmt)) { baseField.Get("m_LightmapFormat").GetValue().Set(lightFmt); } } baseField.Get("m_ColorSpace").GetValue().Set(ddColorSpace.SelectedIndex); baseField.Get("m_TextureFormat").GetValue().Set((int)fmt); baseField.Get("m_CompleteImageSize").GetValue().Set(encImageBytes.Length); baseField.Get("m_Width").GetValue().Set(tex.m_Width); baseField.Get("m_Height").GetValue().Set(tex.m_Height); AssetTypeValueField image_data = baseField.Get("image data"); image_data.GetValue().type = EnumValueTypes.ByteArray; image_data.templateField.valueType = EnumValueTypes.ByteArray; AssetTypeByteArray byteArray = new AssetTypeByteArray() { size = (uint)encImageBytes.Length, data = encImageBytes }; image_data.GetValue().Set(byteArray); Close(true); } else { await MessageBoxUtil.ShowDialog(this, "Error", "Texture reencoding is not supported atm.\n" + "If you want to change the texture format,\n" + "export it to png first then reimport it here.\n" + "Sorry for the inconvenience."); Close(false); } }