Index: endiancheck.c
===================================================================
--- endiancheck.c	(revision 0)
+++ endiancheck.c	(revision 0)
@@ -0,0 +1,40 @@
+/*
+ * endian.c
+ * Detects the endianness of the host platform, and writes endianness.h
+ * 
+ * Copyright 2006 by Dale McCoy.
+ * dalestan@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include<stdio.h>
+#include<netinet/in.h>
+
+int main(){
+	bool isbig=false;
+	if(htonl(0x12345678)==0x12345678)isbig=true;
+	else if(htonl(0x12345678)!=0x78563412){
+		printf("Unknown endianness; 0x12345678 converts to 0x%x\n",(unsigned int)htonl(0x12345678));
+		return 1;
+	}
+	FILE*p=fopen("endianness.h","wt");
+	if(!p){
+		printf("Could not open endianness.h for writing.\n");
+		return 1;
+	}
+	fprintf(p,"#define GRFCODEC_%s_ENDIAN\n",isbig?"BIG":"LITTLE");
+	return 0;
+}

Property changes on: endiancheck.c
___________________________________________________________________
Name: svn:eol-style
   + native

Index: endianswap.h
===================================================================
--- endianswap.h	(revision 0)
+++ endianswap.h	(revision 0)
@@ -0,0 +1,88 @@
+#ifndef MISC_ENDIAN_H
+#define MISC_ENDIAN_H
+
+/*
+
+  GLT OpenGL C++ Toolkit (LGPL)
+  Copyright (C) 2000-2002 Nigel Stewart  
+
+  Email: nigels@nigels.com   
+  WWW:   http://www.nigels.com/glt/
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+*/
+
+/*
+  s/GLT_/GRFCODEC_/g, C++ to C conversion, the addition of the next eight lines,
+  change to grfcodec types, and the removal of several
+  unnecessary-to-grfcodec functions performed by Dale McCoy.
+
+  Original file was found at http://www.nigels.com/glt/doc/endian_8h-source.html
+*/
+
+#include "typesize.h"
+
+#ifdef _MSC_VER
+#define GRFCODEC_LITTLE_ENDIAN
+#else
+#include "endianness.h"
+#endif
+
+#ifndef MAKEDEP
+#if !defined(GRFCODEC_LITTLE_ENDIAN) && !defined(GRFCODEC_BIG_ENDIAN)
+#error GRFCODEC_LITTLE_ENDIAN or GRFCODEC_BIG_ENDIAN is not defined!
+#endif
+
+#if defined(GRFCODEC_LITTLE_ENDIAN) && defined(GRFCODEC_BIG_ENDIAN)
+#error GRFCODEC_LITTLE_ENDIAN and GRFCODEC_BIG_ENDIAN are both defined!
+#endif
+#endif//MAKEDEP
+
+inline U32 flip32(const U32 *v)
+{
+    const U8 *b = (const U8 *) v;
+    return b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
+}
+
+inline U16 flip16(const U16 *v)
+{
+    const U8 *b = (const U8 *) v;
+    return b[0]<<8 | b[1];
+}
+
+inline U32 littleEndian32(const U32 &v)
+{
+    #ifdef GRFCODEC_LITTLE_ENDIAN
+    return v;
+    #endif
+
+    #ifdef GRFCODEC_BIG_ENDIAN
+    return flip32(&v);
+    #endif
+}
+
+inline U16 littleEndian16(const U16 &v)
+{
+    #ifdef GRFCODEC_LITTLE_ENDIAN
+    return v;
+    #endif
+
+    #ifdef GRFCODEC_BIG_ENDIAN
+    return flip16(&v);
+    #endif
+}
+
+#endif

Property changes on: endianswap.h
___________________________________________________________________
Name: svn:eol-style
   + native

Index: grfcodec.cc
===================================================================
--- grfcodec.cc	(revision 908)
+++ grfcodec.cc	(working copy)
@@ -55,6 +55,8 @@
 #include "version.h"
 #include "conv.h"
 #include "nfosprite.h"
+#include "typesize.h"
+#include "endianswap.h"
 
 #ifdef WIN32
 #	include "path.h"
@@ -361,7 +363,8 @@
 			totaluncomp += spritesize;
 			spriteno++;
 
-			fwrite(&spritesize, 1, 2, grf);
+			U16 _size=littleEndian16(spritesize);
+			fwrite(&_size, 1, 2, grf);
 			fputc(0xff, grf);
 			fputc(0xff, grf);
 			fputc(namelen, grf);
@@ -386,7 +389,8 @@
 			totaluncomp += size;
 			spriteno++;
 
-			fwrite(&size, 1, 2, grf);
+			U16 _size=littleEndian16(size);
+			fwrite(&_size, 1, 2, grf);
 			fputc(0xff, grf);
 			fwrite(sprite.GetData(),1,size,grf);
 			if(spriteno == 1 && sprite.size() == 4){
Index: grfcodec.vcproj
===================================================================
--- grfcodec.vcproj	(revision 908)
+++ grfcodec.vcproj	(working copy)
@@ -332,6 +332,9 @@
 				RelativePath=".\conv.h">
 			</File>
 			<File
+				RelativePath=".\endian.h">
+			</File>
+			<File
 				RelativePath=".\error.h">
 			</File>
 			<File
Index: Makefile.common
===================================================================
--- Makefile.common	(revision 910)
+++ Makefile.common	(working copy)
@@ -1,5 +1,7 @@
 # Common commands for both Makefile and Makefile.win
 
+.SUFFIXES:
+
 FORCE:
 %_r: FORCE
 	$(_E) [REBUILD] $(@:%_r=%)
@@ -44,6 +46,14 @@
 	$(_E) [PERL] $@
 	$(_C)perl pal2c.pl $(PAL_FILES) > $@
 
+endiancheck: 	endiancheck.c
+	$(_E) [CC/LD] $@
+	$(_C)$(CC) -o $@ $(CFLAGS) $<
+
+endianness.h:	endiancheck
+	$(_E) [ENDIANCHECK] $@
+	$(_C)$<
+
 # Gnu compiler rules
 
 readinfo.o: readinfo.cc
Index: pcxsprit.cc
===================================================================
--- pcxsprit.cc	(revision 908)
+++ pcxsprit.cc	(working copy)
@@ -1,6 +1,7 @@
 
 #include "pcxsprit.h"
 #include "ttdpal.h"
+#include "endianswap.h"
 
 extern bool _mapAll;
 
@@ -202,7 +203,20 @@
 	header.window[3] = totaly - 1;
 	header.screen[1] = totaly - 1;
 
-	fwrite(&header, sizeof(pcxheader), 1, curfile);
+	pcxheader h=header;
+
+	h.window[0]=littleEndian16(h.window[0]);
+	h.window[1]=littleEndian16(h.window[1]);
+	h.window[2]=littleEndian16(h.window[2]);
+	h.window[3]=littleEndian16(h.window[3]);
+	h.dpi[0]=littleEndian16(h.dpi[0]);
+	h.dpi[1]=littleEndian16(h.dpi[1]);
+	h.bpl=littleEndian16(h.bpl);
+	h.palinfo=littleEndian16(h.palinfo);
+	h.screen[0]=littleEndian16(h.screen[0]);
+	h.screen[1]=littleEndian16(h.screen[1]);
+
+	fwrite(&h, sizeof(pcxheader), 1, curfile);
 	fseek(curfile, oldpos, SEEK_SET);
 }
 
@@ -285,6 +299,18 @@
 
 	fread(&header, sizeof(pcxheader), 1, curfile);
 
+// Yes, "littleEndian" is slightly non-intuitive here. 
+	header.window[0]=littleEndian16(header.window[0]);
+	header.window[1]=littleEndian16(header.window[1]);
+	header.window[2]=littleEndian16(header.window[2]);
+	header.window[3]=littleEndian16(header.window[3]);
+	header.dpi[0]=littleEndian16(header.dpi[0]);
+	header.dpi[1]=littleEndian16(header.dpi[1]);
+	header.bpl=littleEndian16(header.bpl);
+	header.palinfo=littleEndian16(header.palinfo);
+	header.screen[0]=littleEndian16(header.screen[0]);
+	header.screen[1]=littleEndian16(header.screen[1]);
+
 	if (header.nplanes == 3) {
 		fprintf(stderr, "Cannot read truecolour PCX files!\n");
 		exit(2);
Index: readinfo.cc
===================================================================
--- readinfo.cc	(revision 909)
+++ readinfo.cc	(working copy)
@@ -40,6 +40,7 @@
 
 using namespace std;
 
+#ifndef MAKEDEP
 #ifndef NO_BOOST
 // grfcodec requires boost::date_time for its processing of the \wYMD and
 // \wDMY formats. Get boost from www.boost.org
@@ -49,6 +50,7 @@
 using namespace boost::gregorian;
 
 #endif//NO_BOOST
+#endif//MAKEDEP
 
 #include"nfosprite.h"
 #include"allocarray.h"
Index: sprites.cc
===================================================================
--- sprites.cc	(revision 908)
+++ sprites.cc	(working copy)
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include "error.h"
+#include "endianswap.h"
 
 #define _SPRITES_C
 #include "sprites.h"
@@ -35,6 +36,21 @@
 			ftell(stream));
 		exit(2);
 	}
+	switch(size){
+	case 1:break;
+	case 2:{
+		U16*buf=(U16*)ptr;
+		for(size_t i=0;i<n;i++)
+			buf[i]=littleEndian16(buf[i]);
+		break;
+	}case 4:{
+		U32*buf=(U32*)ptr;
+		for(size_t i=0;i<n;i++)
+			buf[i]=littleEndian32(buf[i]);
+		break;
+	}default:
+		fperror("\nDon't know how to read elements of size %d.",size);
+	}
 }
 
 
@@ -193,6 +209,10 @@
 
 	cfread(info+1, 1, 7, grf);
 
+	((U16*)info)[1]=littleEndian16(((U16*)info)[1]);
+	((U16*)info)[2]=littleEndian16(((U16*)info)[2]);
+	((U16*)info)[3]=littleEndian16(((U16*)info)[3]);
+
 	orgsize = size;
 
 	// output buffer: y * x_low + x_high<<8 + 8 bytes info
@@ -644,7 +664,11 @@
 		}
 	}
 
-	fwrite(&size, 1, 2, grf);
+	U16 _size=littleEndian16(size);
+	fwrite(&_size, 1, 2, grf);
+	((U16*)compr)[1]=littleEndian16(((U16*)compr)[1]);
+	((U16*)compr)[2]=littleEndian16(((U16*)compr)[2]);
+	((U16*)compr)[3]=littleEndian16(((U16*)compr)[3]);
 	fwrite(compr, 1, realcompsize + 8, grf);
 
 	free(compr);
