Skip to content

Commit

Permalink
[WzLib] Fix png of spine animations being decoded incorrectly
Browse files Browse the repository at this point in the history
reference #105

TODO: check for memory leaks
  • Loading branch information
lastbattle committed Feb 16, 2021
1 parent 100dbf3 commit 95a8f17
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 109 deletions.
6 changes: 4 additions & 2 deletions HaCreator/GUI/EditorPanels/BackgroundPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ private void bgItem_Click(object sender, MouseEventArgs e)
Thread thread = new Thread(() =>
{
WzSpineAnimationItem item = new WzSpineAnimationItem(stringObj);
string path_title = stringObj.Parent?.FullPath ?? "Animate";
// Create xna window
SpineAnimationWindow Window = new SpineAnimationWindow(item);
SpineAnimationWindow Window = new SpineAnimationWindow(item, path_title);
Window.Run();
});
thread.Start();
Expand Down
2 changes: 1 addition & 1 deletion HaCreator/MapSimulator/MapSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ private void DoScreenshot()
GraphicsDevice.GetBackBufferData(backBuffer);

//Copy to texture
using (Texture2D texture = new Texture2D(GraphicsDevice, RenderWidth, RenderHeight, false, SurfaceFormat.Color))
using (Texture2D texture = new Texture2D(GraphicsDevice, RenderWidth, RenderHeight, false, SurfaceFormat.Color /*RGBA8888*/))
{
texture.SetData(backBuffer);

Expand Down
67 changes: 47 additions & 20 deletions MapleLib/WzLib/Spine/WzSpineTextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;

namespace MapleLib.WzLib.Spine
{
public class WzSpineTextureLoader : TextureLoader
{
public WzObject ParentNode { get; private set; }
private GraphicsDevice graphicsDevice;
private readonly GraphicsDevice graphicsDevice;


public WzSpineTextureLoader(WzObject ParentNode, GraphicsDevice graphicsDevice)
Expand Down Expand Up @@ -79,29 +78,57 @@ public void Load(AtlasPage page, string path)

if (canvasProperty != null)
{
Bitmap bitmap = canvasProperty.GetLinkedWzCanvasBitmap();
if (bitmap != null && graphicsDevice != null)
{
Texture2D tex = new Texture2D(graphicsDevice, bitmap.Width, bitmap.Height, true, SurfaceFormat.Color);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

int bufferSize = data.Height * data.Stride;
WzCanvasProperty linkImgProperty = (WzCanvasProperty) canvasProperty.GetLinkedWzImageProperty();

//create data buffer
byte[] bytes = new byte[bufferSize];
WzPngProperty pngProperty = linkImgProperty.PngProperty;
SurfaceFormat surfaceFormat = linkImgProperty.PngProperty.GetXNASurfaceFormat();

// copy bitmap data into buffer
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

// copy our buffer to the texture
tex.SetData(bytes);
if (graphicsDevice != null)
{
Texture2D tex;
tex = new Texture2D(graphicsDevice,
pngProperty.Width, pngProperty.Height,
false, surfaceFormat);
/*switch (surfaceFormat)
{
case SurfaceFormat.Bgra4444:
tex = new Texture2D(graphicsDevice,
pngProperty.Width * 2, pngProperty.Height * 2,
false, surfaceFormat);
break;
case SurfaceFormat.Bgra32:
tex = new Texture2D(graphicsDevice,
pngProperty.Width * 4, pngProperty.Height * 4,
false, surfaceFormat);
break;
case SurfaceFormat.Bgr565:
tex = new Texture2D(graphicsDevice,
pngProperty.Width * 2, pngProperty.Height * 2,
false, surfaceFormat);
break;
case SurfaceFormat.Dxt3:
tex = new Texture2D(graphicsDevice,
pngProperty.Width * 4, pngProperty.Height * 4,
false, surfaceFormat);
break;
case SurfaceFormat.Dxt5:
tex = new Texture2D(graphicsDevice,
pngProperty.Width * 4, pngProperty.Height * 4,
false, surfaceFormat);
break;
default:
tex = new Texture2D(graphicsDevice,
pngProperty.Width, pngProperty.Height,
false, surfaceFormat);
break;
}
*/
pngProperty.ParsePng(true, tex);

// unlock the bitmap data
bitmap.UnlockBits(data);

page.rendererObject = tex;
page.width = bitmap.Width;
page.height = bitmap.Height;
page.width = pngProperty.Width;
page.height = pngProperty.Height;
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions MapleLib/WzLib/WzProperties/WzCanvasProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public override void WriteValue(MapleLib.WzLib.Util.WzBinaryWriter writer)
// Image info
writer.WriteCompressedInt(PngProperty.Width);
writer.WriteCompressedInt(PngProperty.Height);
writer.WriteCompressedInt(PngProperty.format);
writer.Write((byte)PngProperty.format2);
writer.WriteCompressedInt(PngProperty.Format);
writer.Write((byte)PngProperty.Format2);
writer.Write((Int32)0);

// Write image
Expand Down Expand Up @@ -284,6 +284,18 @@ public bool HaveOutlinkProperty()
/// </summary>
/// <returns></returns>
public Bitmap GetLinkedWzCanvasBitmap()
{
return GetLinkedWzImageProperty().GetBitmap();
}

/// <summary>
/// Gets the '_inlink' WzCanvasProperty of this.
///
/// '_inlink' is not implemented as part of WzCanvasProperty as I dont want to override existing Wz structure.
/// It will be handled via HaRepackerMainPanel instead.
/// </summary>
/// <returns></returns>
public WzImageProperty GetLinkedWzImageProperty()
{
string _inlink = ((WzStringProperty)this[InlinkPropertyName])?.Value; // could get nexon'd here. In case they place an _inlink that's not WzStringProperty
string _outlink = ((WzStringProperty)this[OutlinkPropertyName])?.Value; // could get nexon'd here. In case they place an _outlink that's not WzStringProperty
Expand All @@ -300,7 +312,7 @@ public Bitmap GetLinkedWzCanvasBitmap()
WzImageProperty foundProperty = wzImageParent.GetFromPath(_inlink);
if (foundProperty != null && foundProperty is WzImageProperty property)
{
return property.GetBitmap();
return property;
}
}
}
Expand All @@ -316,11 +328,11 @@ public Bitmap GetLinkedWzCanvasBitmap()
WzObject foundProperty = wzFileParent.GetObjectFromPath(_outlink);
if (foundProperty != null && foundProperty is WzImageProperty property)
{
return property.GetBitmap();
return property;
}
}
}
return this.GetBitmap();
return this;
}

/// <summary>
Expand Down
Loading

0 comments on commit 95a8f17

Please sign in to comment.