--- a/filter.h
+++ b/filter.h
@@ -44,18 +44,6 @@
 	#define bzero(dest, len)   memset((dest), 0, (len))
 #endif
 
-
-//---------------------- Types ---------------------------------------------
-
-#define UCHAR	unsigned char
-#define USHORT  unsigned short
-#define ULONG   unsigned long
-
-enum{
-	_UCHAR,
-	_USHORT,
-	_ULONG
-	};
 
 //---------------------- Some useful math defines --------------------------
 
@@ -661,19 +649,22 @@
 void  	FindScript			( struct adjust_Prefs *thePrefs );
 char* 	LoadScript			( fullPath* scriptFile  );
 int 	WriteScript			( char* res, fullPath* scriptFile, int launch );
-int 	writePSD			( Image *im, fullPath* fname);			// On Mac: fname is FSSpec*	
-int 	readPSD				( Image *im, fullPath* fname, int mode);
+// Write PSB and PSD files
+int 	writePS 			( Image *im, fullPath* fname, Boolean bBig );			// On Mac: fname is FSSpec*	
+int 	writePSD			( Image *im, fullPath* fname );
+int 	readPSD				( Image *im, fullPath* fname, int mode); // Can handle both PSD and PSB
+int 	writePSwithLayer	( Image *im, fullPath *fname, Boolean bBig);
+int 	writePSDwithLayer	( Image *im, fullPath *fname);
+int 	addLayerToFile		( Image *im, fullPath* sfile, fullPath* dfile, stBuf *sB); //works with PSD & PSB
+int 	readPSDMultiLayerImage( MultiLayerImage *mim, fullPath* sfile);
 int 	FindFile			( fullPath *fname );
 int 	SaveFileAs			( fullPath *fname, char *prompt, char *name );
 void 	ConvFileName		( fullPath *fname,char *string);
-int 	writePSDwithLayer	( Image *im, fullPath *fname);
-int 	addLayerToFile		( Image *im, fullPath* sfile, fullPath* dfile, stBuf *sB);
 void 	showScript			( fullPath* scriptFile );
 void 	MakeTempName		( fullPath *fspec, char *fname );
 void 	makePathForResult	( fullPath *path );
 int 	makePathToHost 		( fullPath *path );
 void    open_selection		( fullPath *path );
-int 	readPSDMultiLayerImage( MultiLayerImage *mim, fullPath* sfile);
 int 	GetFullPath 		(fullPath *path, char *filename); // Somewhat confusing, for compatibility easons
 int 	StringtoFullPath	(fullPath *path, char *filename);
 int 	IsTextFile			( char* fname );
@@ -742,8 +733,8 @@
 void 	SetInvMakeParamsCorrect( struct fDesc *stack, struct MakeParams *mp, Image *im , Image *pn, int color );
 
 void 	GetControlPointCoordinates(int i, double *x, double *y, AlignInfo *gl );
-void 	ARGBtoRGBA(UCHAR* buf, int width, int bitsPerPixel);
-void 	RGBAtoARGB(UCHAR* buf, int width, int bitsPerPixel);
+void 	ARGBtoRGBA(uint8_t* buf, int width, int bitsPerPixel);
+void 	RGBAtoARGB(uint8_t* buf, int width, int bitsPerPixel);
 int 	CropImage(Image *im, PTRect *r);
 void 	DoColorCorrection( Image *im1, Image *im2, int mode );
 
@@ -952,7 +943,25 @@
 // Endian stuff: Read and write numbers from and to memory (ptr)
 
 #ifdef PT_BIGENDIAN
-	#define	LONGNUMBER( number, ptr )					*ptr++ = ((char*)(&number))[0];	\
+#define	LONGLONGNUMBER( number, ptr )		*ptr++ = ((char*)(&number))[0];	\
+														*ptr++ = ((char*)(&number))[1];	\
+														*ptr++ = ((char*)(&number))[2];	\
+														*ptr++ = ((char*)(&number))[3];	\
+														*ptr++ = ((char*)(&number))[4];	\
+														*ptr++ = ((char*)(&number))[5];	\
+														*ptr++ = ((char*)(&number))[6];	\
+														*ptr++ = ((char*)(&number))[7];	
+
+	#define NUMBERLONGLONG( number, ptr )		((char*)(&number))[0] = *ptr++;	\
+														((char*)(&number))[1] = *ptr++;	\
+														((char*)(&number))[2] = *ptr++;	\
+														((char*)(&number))[3] = *ptr++;	\
+														((char*)(&number))[4] = *ptr++;	\
+														((char*)(&number))[5] = *ptr++;	\
+														((char*)(&number))[6] = *ptr++;	\
+														((char*)(&number))[7] = *ptr++;	
+
+#define	LONGNUMBER( number, ptr )					*ptr++ = ((char*)(&number))[0];	\
 														*ptr++ = ((char*)(&number))[1];	\
 														*ptr++ = ((char*)(&number))[2];	\
 														*ptr++ = ((char*)(&number))[3];	
@@ -969,6 +978,24 @@
 														((char*)(&number))[1] = *ptr++;	\
 
 #else
+	#define	LONGLONGNUMBER( number, ptr )		*ptr++ = ((char*)(&number))[7];	\
+														*ptr++ = ((char*)(&number))[6];	\
+														*ptr++ = ((char*)(&number))[5];	\
+														*ptr++ = ((char*)(&number))[4];	\
+														*ptr++ = ((char*)(&number))[3];	\
+														*ptr++ = ((char*)(&number))[2];	\
+														*ptr++ = ((char*)(&number))[1];	\
+														*ptr++ = ((char*)(&number))[0];	
+
+	#define NUMBERLONGLONG( number, ptr )		((char*)(&number))[7] = *ptr++;	\
+														((char*)(&number))[6] = *ptr++;	\
+														((char*)(&number))[5] = *ptr++;	\
+														((char*)(&number))[4] = *ptr++;	\
+														((char*)(&number))[3] = *ptr++;	\
+														((char*)(&number))[2] = *ptr++;	\
+														((char*)(&number))[1] = *ptr++;	\
+														((char*)(&number))[0] = *ptr++;	
+
 	#define	LONGNUMBER( number, ptr )					*ptr++ = ((char*)(&number))[3];	\
 														*ptr++ = ((char*)(&number))[2];	\
 														*ptr++ = ((char*)(&number))[1];	\
@@ -979,7 +1006,7 @@
 														((char*)(&number))[1] = *ptr++;	\
 														((char*)(&number))[0] = *ptr++;	
 
-	#define	SHORTNUMBER( number, ptr )					*ptr++ = ((char*)(&number))[1];	\
+#define	SHORTNUMBER( number, ptr )					*ptr++ = ((char*)(&number))[1];	\
 														*ptr++ = ((char*)(&number))[0];	\
 
 	#define NUMBERSHORT( number, ptr )					((char*)(&number))[1] = *ptr++;	\
@@ -990,6 +1017,7 @@
 #endif // PT_BIGENDIAN
 
 //TODO:  JMW These File i/o macros are to be replaced in code with the error catching functions below
+#if USEWRITEREADMACROS
 #define WRITEUCHAR( theChar )       ch = theChar; count = 1; mywrite(fnum,count,&ch);
 
 #define WRITESHORT( theShort )      svar = theShort; d = data; SHORTNUMBER( svar, d ); \
@@ -1002,12 +1030,19 @@
                                             d = data; NUMBERLONG( var, d );     \
                                             theLong = var;
                                     
+#define WRITEINT64( theLong )       var64 = theLong; d = data; LONGLONGNUMBER( var64, d ); \
+                                    count = 8; mywrite  (fnum,count,data);
+
+#define READINT64( theLong )                count = 8; myread(src,count,data);  \
+                                            d = data; NUMBERLONGLONG( var64, d );     \
+                                            theLong = var64;
+                                    
 #define READSHORT( theShort )               count = 2; myread(src,count,data);  \
                                             d = data; NUMBERSHORT( svar, d );   \
                                             theShort = svar;
 
 #define READUCHAR( theChar )                count = 1; myread(src,count,&ch); theChar = ch; 
-
+#endif
 // Cross platform file functions
 
 #ifdef __Mac__
@@ -1059,13 +1094,17 @@
 #endif
 
 /* ENDIAN aware file i/o funtions.  Used for reading and writing photoshop files */
-Boolean panoWriteUCHAR(nfile_spec fnum, UCHAR   theChar );
-Boolean panoWriteSHORT(nfile_spec fnum, USHORT  theShort );
-Boolean panoWriteINT32(nfile_spec fnum, ULONG   theLong );
-Boolean panoReadUCHAR (nfile_spec fnum, UCHAR  *pChar );
-Boolean panoReadSHORT (nfile_spec fnum, USHORT *pShort );
-Boolean panoReadINT32 (nfile_spec fnum, ULONG  *pLong );
-    
+Boolean panoWriteUCHAR(file_spec fnum, uint8_t   theChar );
+Boolean panoWriteSHORT(file_spec fnum, uint16_t  theShort );
+Boolean panoWriteINT32(file_spec fnum, uint32_t   theLong );
+Boolean panoWriteINT64(file_spec fnum, int64_t theLongLong );
+Boolean panoWriteINT32or64(file_spec fnum, int64_t theLongLong, Boolean bBig );
+Boolean panoReadUCHAR (file_spec fnum, uint8_t  *pChar );
+Boolean panoReadSHORT (file_spec fnum, uint16_t *pShort );
+Boolean panoReadINT32 (file_spec fnum, uint32_t  *pLong );
+Boolean panoReadINT64 (file_spec fnum, int64_t  *pLongLong );
+Boolean panoReadINT32or64(file_spec fnum, int64_t  *pLongLong, Boolean bBig );
+
 
 #define PANO_DEFAULT_PIXELS_PER_RESOLUTION  150.0
 #define PANO_DEFAULT_TIFF_RESOLUTION_UNITS  RESUNIT_INCH