changeset 6:d2e49291986e default tip

Add Linux (x86_64) support to SquishWrapper Also includes rebuild instructions for future
author IBBoard <dev@ibboard.co.uk>
date Sat, 06 Oct 2018 16:45:48 +0100
parents caaa54391116
children
files README SquishWrapper.cs SquishWrapper.csproj libsquish-1.10.patch libsquish.so
diffstat 5 files changed, 168 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README	Sat Oct 06 16:45:48 2018 +0100
@@ -0,0 +1,5 @@
+To rebuild libsquish.so:
+ * Download libsquish 1.10 from https://code.google.com/archive/p/libsquish/downloads and extract it
+ * Patch it with "patch -p1 < path/to/SquishWrapper/libsquish-1.10.patch"
+ * Run "make all"
+ * Replace libsquish.so in the root of this project with the new version
\ No newline at end of file
--- a/SquishWrapper.cs	Sun Oct 16 20:43:15 2016 +0100
+++ b/SquishWrapper.cs	Sat Oct 06 16:45:48 2018 +0100
@@ -50,6 +50,13 @@
 		kColourIterativeClusterFit	= ( 1 << 8 ),		// Use a very slow but very high quality colour compressor.
 	}
 
+	public enum OS {
+		Windows,
+		Mac,
+		Linux,
+		Other
+	}
+
 	/// <summary>
 	/// Summary description for Class1.
 	/// </summary>
@@ -60,16 +67,66 @@
 			return ( Marshal.SizeOf( IntPtr.Zero ) == 8 ); 
 		}
 
+		// OS detection taken from https://stackoverflow.com/a/38795621/283242, because there's not a guaranteed
+		// cross-library way of doing it that we can rely on.
+		private static OS DetectOS() {
+			string windir = Environment.GetEnvironmentVariable("windir");
+			Console.WriteLine(windir);
+
+			if (!string.IsNullOrEmpty(windir) && windir.Contains(@"\") && Directory.Exists(windir))
+			{
+				Console.WriteLine("Windows");
+				return OS.Windows;
+			}
+			else if (File.Exists(@"/proc/sys/kernel/ostype"))
+			{
+				Console.WriteLine("Found ostype");
+				string osType = File.ReadAllText(@"/proc/sys/kernel/ostype");
+				if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
+				{
+					Console.WriteLine("Linux");
+					// Note: Android gets here too
+					return OS.Linux;
+				}
+				else
+				{
+					return OS.Other;
+				}
+			}
+			else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
+			{
+				// Note: iOS gets here too
+				return OS.Mac;
+			}
+			else
+			{
+				return OS.Other;
+			}
+		}
+
 		private sealed class SquishInterface_32
 		{
             [DllImport("squishinterface_x86.dll")]
 			internal static extern unsafe void CompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
+            [DllImport("squishinterface_x86.dll")]
+			internal static	extern unsafe void DecompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
 		}
    
 		private sealed class SquishInterface_64
 		{
 			[DllImport("squishinterface_x64.dll", EntryPoint="SquishCompressImage")]
 			internal static extern unsafe void CompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
+			[DllImport("squishinterface_x64.dll", EntryPoint="SquishDecompressImage")]
+			internal static	extern unsafe void DecompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
+		}
+
+		private sealed class SquishInterface_x86_64
+		{
+			// Ugly "mangled" C++ names are ugly, but we shouldn't be rebuilding this too often
+			[DllImport("libsquish.so")]
+			internal static extern unsafe void CompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
+			[DllImport("libsquish.so")]
+			internal static	extern unsafe void DecompressImage( byte* rgba, int width, int height, byte* blocks, int flags );
 		}
 
 		private static unsafe void	CallCompressImage( byte[] rgba, int width, int height, byte[] blocks, int flags )
@@ -78,13 +135,24 @@
 			{
 				fixed ( byte* pBlocks = blocks )
 				{
-					if ( Is64Bit() )
+					OS curr_os = DetectOS();
+					if (curr_os == OS.Windows)
 					{
-						SquishInterface_64.CompressImage( pRGBA, width, height, pBlocks, flags );
+						if ( Is64Bit() )
+						{
+							SquishInterface_64.CompressImage( pRGBA, width, height, pBlocks, flags );
+						}
+						else
+						{
+							SquishInterface_32.CompressImage( pRGBA, width, height, pBlocks, flags );
+						}
 					}
-					else
+					else if (curr_os == OS.Linux)
 					{
-						SquishInterface_32.CompressImage( pRGBA, width, height, pBlocks, flags );
+						if ( Is64Bit() )
+						{
+							SquishInterface_x86_64.CompressImage( pRGBA, width, height, pBlocks, flags );
+						}
 					}
 				}
 			}
--- a/SquishWrapper.csproj	Sun Oct 16 20:43:15 2016 +0100
+++ b/SquishWrapper.csproj	Sat Oct 06 16:45:48 2018 +0100
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <ProjectType>Local</ProjectType>
@@ -81,6 +81,9 @@
     <Content Include="squishinterface_x86.dll">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>
+    <Content Include="libsquish.so">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libsquish-1.10.patch	Sat Oct 06 16:45:48 2018 +0100
@@ -0,0 +1,87 @@
+diff -Nau /home/ibboard/temp/squish-1.10/alpha.cpp ./alpha.cpp
+--- /home/ibboard/temp/squish-1.10/alpha.cpp	2006-06-29 13:43:24.000000000 +0100
++++ ./alpha.cpp	2013-11-30 14:40:56.668747938 +0000
+@@ -25,6 +25,7 @@
+    
+ #include "alpha.h"
+ #include <algorithm>
++#include <limits.h>
+ 
+ namespace squish {
+ 
+diff -Nau /home/ibboard/temp/squish-1.10/config ./config
+--- /home/ibboard/temp/squish-1.10/config	2006-10-01 20:40:09.000000000 +0100
++++ ./config	2018-10-06 15:43:13.521557495 +0100
+@@ -4,10 +4,10 @@
+ USE_ALTIVEC ?= 0
+ 
+ # define to 1 to use SSE2 instructions
+-USE_SSE ?= 0
++USE_SSE ?= 1
+ 
+ # default flags
+-CXXFLAGS ?= -O2
++CXXFLAGS ?= -O2 -fPIC
+ ifeq ($(USE_ALTIVEC),1)
+ CPPFLAGS += -DSQUISH_USE_ALTIVEC=1
+ CXXFLAGS += -maltivec
+Common subdirectories: /home/ibboard/temp/squish-1.10/extra and ./extra
+diff -Nau /home/ibboard/temp/squish-1.10/Makefile ./Makefile
+--- /home/ibboard/temp/squish-1.10/Makefile	2006-04-07 18:30:11.000000000 +0100
++++ ./Makefile	2018-10-06 16:39:32.710493023 +0100
+@@ -5,9 +5,12 @@
+ 
+ OBJ = $(SRC:%.cpp=%.o)
+ 
++SO = libsquish.so
++
+ LIB = libsquish.a
+ 
+ all : $(LIB)
++	$(CXX) $(OBJ) -shared -o $(SO)
+ 
+ install : $(LIB)
+ 	install squish.h $(INSTALL_DIR)/include 
+@@ -25,7 +28,7 @@
+ 	$(CXX) $(CPPFLAGS) -I. $(CXXFLAGS) -o$@ -c $<
+ 
+ clean :
+-	$(RM) $(OBJ) $(LIB)
++	$(RM) $(OBJ) $(LIB) $(SO)
+ 
+ 
+ 
+diff -Nau /home/ibboard/temp/squish-1.10/singlecolourfit.cpp ./singlecolourfit.cpp
+--- /home/ibboard/temp/squish-1.10/singlecolourfit.cpp	2007-03-21 19:43:59.000000000 +0000
++++ ./singlecolourfit.cpp	2013-11-30 14:40:56.670747952 +0000
+@@ -26,6 +26,7 @@
+ #include "singlecolourfit.h"
+ #include "colourset.h"
+ #include "colourblock.h"
++#include <limits.h>
+ 
+ namespace squish {
+ 
+diff -Nau /home/ibboard/temp/squish-1.10/squish.h ./squish.h
+--- /home/ibboard/temp/squish-1.10/squish.h	2007-03-21 20:13:51.000000000 +0000
++++ ./squish.h	2018-10-06 15:02:57.777805856 +0100
+@@ -214,7 +214,7 @@
+ 	much memory is required in the compressed image, use
+ 	squish::GetStorageRequirements.
+ */
+-void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags );
++extern "C" void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags );
+ 
+ // -----------------------------------------------------------------------------
+ 
+@@ -237,7 +237,7 @@
+ 
+ 	Internally this function calls squish::Decompress for each block.
+ */
+-void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags );
++extern "C" void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags );
+ 
+ // -----------------------------------------------------------------------------
+ 
+Common subdirectories: /home/ibboard/temp/squish-1.10/squish.xcodeproj and ./squish.xcodeproj
+Common subdirectories: /home/ibboard/temp/squish-1.10/vs7 and ./vs7
Binary file libsquish.so has changed