Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "runsmap.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RunsMap::setRunLength(TPixelGR8 *pix, TUINT32 length)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixelGR8 *origPix = pix;
Toshihiro Shimizu 890ddd
	pix = origPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelGR8 *pixRev = pix + length - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	--length;
Toshihiro Shimizu 890ddd
	if (length < 3)
Toshihiro Shimizu 890ddd
		pix->value = pixRev->value = (length << 6);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		pix->value = pixRev->value = (3 << 6);
Toshihiro Shimizu 890ddd
		++pix, --pixRev;
Toshihiro Shimizu 890ddd
		if (length < 255)
Toshihiro Shimizu 890ddd
			pix->value = pixRev->value = length;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			pix->value = pixRev->value = 255;
Toshihiro Shimizu 890ddd
			++pix, pixRev -= 4;
Toshihiro Shimizu 890ddd
			TUINT32 *l = (TUINT32 *)pix;
Toshihiro Shimizu 890ddd
			*l = length;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(runLength(origPix) == (length + 1));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUINT32 RunsMap::runLength(const TPixelGR8 *pix, bool reversed) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int length = pix->value >> 6;
Toshihiro Shimizu 890ddd
	if (length >= 3) {
Toshihiro Shimizu 890ddd
		pix += (reversed) ? -1 : 1, length = pix->value;
Toshihiro Shimizu 890ddd
		if (length >= 255) {
Toshihiro Shimizu 890ddd
			pix += (reversed) ? -4 : 1;
Toshihiro Shimizu 890ddd
			length = *(TUINT32 *)pix;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return length + 1;
Toshihiro Shimizu 890ddd
}