Skip to content

Commit

Permalink
Updated to the latest version of StbSharp (MonoGame#6131)
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 authored and nkast committed Jan 28, 2018
1 parent 42c0f06 commit 8aa3e55
Show file tree
Hide file tree
Showing 11 changed files with 1,334 additions and 1,270 deletions.
6 changes: 0 additions & 6 deletions Build/Projects/MonoGame.Framework.definition
Original file line number Diff line number Diff line change
Expand Up @@ -1215,12 +1215,6 @@
<Compile Include="Utilities\Imaging\PinnedArray.cs">
<Platforms>WindowsGL,Linux,MacOS</Platforms>
</Compile>
<Compile Include="Utilities\Imaging\TinyJpeg.cs">
<Platforms>WindowsGL,Linux,MacOS</Platforms>
</Compile>
<Compile Include="Utilities\Imaging\TinyJpeg.Generated.cs">
<Platforms>WindowsGL,Linux,MacOS</Platforms>
</Compile>



Expand Down
59 changes: 0 additions & 59 deletions MonoGame.Framework/Utilities/Imaging/ImageReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -81,63 +80,5 @@ public byte[] Read(Stream stream, out int x, out int y, out int comp, int req_co
_stream = null;
}
}

public AnimatedGifFrame[] ReadAnimatedGif(Stream stream, out int x, out int y, out int comp, int req_comp)
{
try
{
x = y = comp = 0;

var res = new List<AnimatedGifFrame>();
_stream = stream;

var context = new Imaging.stbi__context();
Imaging.stbi__start_callbacks(context, _callbacks, null);

if (Imaging.stbi__gif_test(context) == 0)
{
throw new Exception("Input stream is not GIF file.");
}

var g = new Imaging.stbi__gif();

do
{
int ccomp;
var result = Imaging.stbi__gif_load_next(context, g, &ccomp, req_comp);
if (result == null)
{
break;
}

comp = ccomp;
var c = req_comp != 0 ? req_comp : comp;
var data = new byte[g.w*g.h*c];
Marshal.Copy(new IntPtr(result), data, 0, data.Length);
Operations.Free(result);

var frame = new AnimatedGifFrame
{
Data = data,
Delay = g.delay
};
res.Add(frame);
} while (true);

Operations.Free(g._out_);

if (res.Count > 0)
{
x = g.w;
y = g.h;
}

return res.ToArray();
}
finally
{
_stream = null;
}
}
}
}
7 changes: 1 addition & 6 deletions MonoGame.Framework/Utilities/Imaging/ImageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ private int WriteCallback(void* context, void* data, int size)
return size;
}

private void WriteCallback2(void* context, void* data, int size)
{
WriteCallback(context, data, size);
}

public void Write(byte[] bytes, int x, int y, int comp, ImageWriterFormat format, Stream dest)
{
try
Expand All @@ -59,7 +54,7 @@ public void Write(byte[] bytes, int x, int y, int comp, ImageWriterFormat format
Imaging.stbi_write_tga_to_func(WriteCallback, null, x, y, comp, b);
break;
case ImageWriterFormat.Jpg:
Imaging.tje_encode_with_func(WriteCallback2, null, 2, x, y, comp, b);
Imaging.stbi_write_jpg_to_func(WriteCallback, null, x, y, comp, b, 90);
break;

case ImageWriterFormat.Png:
Expand Down
13 changes: 5 additions & 8 deletions MonoGame.Framework/Utilities/Imaging/Operations.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Collections.Generic;
using System.Collections.Concurrent;

namespace MonoGame.Utilities
{
internal static unsafe class Operations
{
internal static Dictionary<long, Pointer> _pointers = new Dictionary<long, Pointer>();
internal static long _allocatedTotal;
internal static object _lock = new object();
internal static ConcurrentDictionary<long, Pointer> _pointers = new ConcurrentDictionary<long, Pointer>();

public static long AllocatedTotal
{
get { return _allocatedTotal; }
get { return Pointer.AllocatedTotal; }
}

public static void* Malloc(long size)
Expand Down Expand Up @@ -43,12 +41,11 @@ public static void MemMove(void* a, void* b, long size)
public static void Free(void* a)
{
Pointer pointer;
if (!_pointers.TryGetValue((long) a, out pointer))
if (!_pointers.TryRemove((long) a, out pointer))
{
return;
}

_pointers.Remove((long) pointer.Ptr);
pointer.Dispose();
}

Expand All @@ -70,7 +67,7 @@ public static void Free(void* a)
var result = Malloc(newSize);
Memcpy(result, a, pointer.Size);

_pointers.Remove((long) pointer.Ptr);
_pointers.TryRemove((long) pointer.Ptr, out pointer);
pointer.Dispose();

return result;
Expand Down
16 changes: 12 additions & 4 deletions MonoGame.Framework/Utilities/Imaging/PinnedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ namespace MonoGame.Utilities
{
internal abstract unsafe class Pointer : IDisposable
{
protected static long _allocatedTotal;
protected static object _lock = new object();

public abstract long Size { get; }
public abstract void* Ptr { get; }

public static long AllocatedTotal
{
get { return _allocatedTotal; }
}

public abstract void Dispose();

public static implicit operator void*(Pointer ptr)
Expand Down Expand Up @@ -86,9 +94,9 @@ public PinnedArray(T[] data)
_size = 0;
}

lock (Operations._lock)
lock (_lock)
{
Operations._allocatedTotal += _size;
_allocatedTotal += _size;
}
}

Expand Down Expand Up @@ -116,9 +124,9 @@ protected virtual void Dispose(bool disposing)
return;
}

lock (Operations._lock)
lock (_lock)
{
Operations._allocatedTotal -= Size;
_allocatedTotal -= Size;
}

if (Data != null)
Expand Down
Loading

0 comments on commit 8aa3e55

Please sign in to comment.