www.pudn.com > sfalic-03-src.rar.rar > bigendian.c


#include "bigendian.h" 
#include "cdftypes.h" 
#include "assert.h" 
#include  
 
/* sprawdzenie ustawien kompilacji i poprawnosci zalozen implementacyjnych */ 
/* dotyczy calego kompresora, nie tylko tego modulu */ 
/* zwraca 0-ok, >=1 blad */ 
int CheckAssumptions() 
{ 
	volatile unsigned int ui=1; 
#ifdef BE_MACHINE 
	if (!BEmachine()) 
		return 1; 
#endif 
 
	ui++; 
	if (ui!=(ui>>(ui-2))) /* czy x==(x>>0), powinno byc */ 
		return ui; 
 
	return 0; 
} 
 
 
/* zwroc endianness maszyny 1-BigEndian, 0-Little */ 
int BEmachine() 
{ 
	static int one=1; 
 
#ifdef BE_MACHINE 
	assert(!*((BYTE *)&one)); 
#endif 
 
	return !*((BYTE *)&one); 
} 
 
/* endian-aware*/ 
/* zamienia wczytane z pliku piksele BigEndian rawBYTESpp Bajtow/piksel (1 lub 2), */ 
/* na ActualEndian sizeof(PIXEL) Bajtow/piksel */ 
/* width- liczba pikseli */ 
void BErawrowtopixelrow(const BYTE * const filerow, PIXEL * const currow, const int width, const int rawBYTESpp) 
{ 
	assert(width>0); 
	assert( ((PIXEL)-1) > 0 ); /* piksel musi byc unsigned! */ 
	assert( sizeof(PIXEL) >= rawBYTESpp ); /* przyszlosciowo, wersja do 8bpp */ 
 
	if (sizeof(PIXEL)==1) /* przyszlosciowo, wersja do 8bpp */ 
	{ 
		memcpy(currow, filerow, sizeof(PIXEL)*width); 
		return; 
	} 
	 
	assert(sizeof(PIXEL)==2);  
 
	if (rawBYTESpp==1) /* trzeba skonwertowac Bajty do Pikseli */ 
	{ 
		unsigned int i; 
 
		for(i=0; i<(unsigned)width; i++) 
			currow[i]=(PIXEL)filerow[i]; 
 
		return; 
	} 
	 
	if (!BEmachine())	/* maszyna Little Endian, trzeba skonwertowac piksele BE do LE */ 
	{ 
		unsigned int i; 
		const PIXEL * const BEfilerow=(PIXEL *)filerow; 
 
		for(i=0; i<(unsigned)width; i++) 
		{ 
			const PIXEL old=BEfilerow[i]; 
			currow[i]=(old>>8) | (old<<8); 
		} 
		return; 
	} 
	else		/* maszyna Big Endian */ 
	{ 
		memcpy(currow, filerow, sizeof(PIXEL)*width); 
		return; 
	} 
 
	assert(0); 
} 
 
 
/* endian-aware, odwrotna do powyzszej*/ 
void BEpixelrowtorawrow(BYTE * const filerow, const PIXEL * const currow, const int width, const int rawBYTESpp) 
{ 
	assert(width>0); 
	assert( ((PIXEL)-1) > 0 ); /* piksel musi byc unsigned! */ 
	assert( sizeof(PIXEL) >= rawBYTESpp ); /* przyszlosciowo, wersja do 8bpp */ 
 
	if (sizeof(PIXEL)==1) /* przyszlosciowo, wersja do 8bpp */ 
	{ 
		memcpy(filerow, currow, sizeof(PIXEL)*width); 
		return; 
	} 
	 
	assert(sizeof(PIXEL)==2);  
 
	if (rawBYTESpp==1) /* trzeba skonwertowac Piksele do Bajtow*/ 
	{ 
		unsigned int i; 
 
		for(i=0; i<(unsigned)width; i++) 
			filerow[i]=(BYTE)currow[i]; 
 
		return; 
	} 
	 
	if (!BEmachine())	/* maszyna Little Endian, trzeba skonwertowac piksele LE do BE*/ 
	{ 
		unsigned int i; 
		PIXEL * const BEfilerow=(PIXEL *)filerow; 
 
		for(i=0; i<(unsigned)width; i++) 
		{ 
			const PIXEL old=currow[i]; 
			BEfilerow[i]=(old>>8) | (old<<8); 
		} 
		return; 
	} 
	else		/* maszyna Big Endian */ 
	{ 
		memcpy(filerow, currow, sizeof(PIXEL)*width); 
		return; 
	} 
 
	assert(0); 
} 
 
 
int BEwrite2Bytes(int val, FILE *f) 
{ 
	putc((BYTE)(val>>8), f); 
	return putc((BYTE)val, f); 
} 
 
 
int BEread2Bytes(FILE *f) 
{ 
	int result; 
	result=(BYTE)getc(f); 
	result<<=8; 
	result|=(BYTE)getc(f); 
 
	return result; 
} 
 
 
int BEwrite4Bytes(int val, FILE *f) 
{ 
	putc((BYTE)(val>>24), f); 
	putc((BYTE)(val>>16), f); 
	putc((BYTE)(val>>8), f); 
	return putc((BYTE)val, f); 
} 
 
 
int BEread4Bytes(FILE *f) 
{ 
	int result; 
	result=(BYTE)getc(f); 
	result<<=8; 
	result|=(BYTE)getc(f); 
	result<<=8; 
	result|=(BYTE)getc(f); 
	result<<=8; 
	result|=(BYTE)getc(f); 
 
	return result; 
}