⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bigendian.c

📁 一个简单而且快速的无损压缩算法。包含源代码实现
💻 C
字号:
#include "bigendian.h"
#include "cdftypes.h"
#include "assert.h"
#include <memory.h>

/* 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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -