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

📄 opvram_v0.1a.c

📁 一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上
💻 C
📖 第 1 页 / 共 2 页
字号:
	WORD i;
	PIXEL unitMask = -1;
		
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL tailMask;
	WORD num, r;
	
	pixelPos = desLinePos;
	num = PIXEL_UNIT / BITS_PER_PIXEL;
	
	if( desBitOffset != 0 )
	{		
		WORD totalOffset = len * BITS_PER_PIXEL;	// 要求len小于1310
		
		tailMask = MAKEMASK( PIXEL_UNIT - desBitOffset );
				
		if( desBitOffset + totalOffset <= PIXEL_UNIT )
		{
			r = PIXEL_UNIT - totalOffset - desBitOffset;
			tailMask = MAKEMASK( totalOffset ) << r;
			(*pixelPos) ^= tailMask;
		}
		else
		{
			(*pixelPos) ^= tailMask;
			num = ( len - ( PIXEL_UNIT - desBitOffset )/ BITS_PER_PIXEL )/ num;
			pixelPos++;
			
			for( i = 0; i < num; i++, pixelPos++ )
				(*pixelPos) ^= unitMask;
			
			r = ( totalOffset + desBitOffset ) & ( PIXEL_UNIT -1 );
			if( r != 0 )	// 尾部
			{
				tailMask = ~( MAKEMASK( PIXEL_UNIT - r ) );
				(*pixelPos) ^= tailMask;
			}
		}
	}
	else	// 起始边界对齐
	{
		num = len / num;

		for( i = 0; i < num; i++, pixelPos++ )
			(*pixelPos) ^= unitMask;
		
		r = len * BITS_PER_PIXEL - num * PIXEL_UNIT;
		if( r != 0 )	// 尾部
		{
			tailMask = ~( MAKEMASK( PIXEL_UNIT - r ) );
			(*pixelPos) ^= tailMask;
		}
	}	
	#else	// 其他
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

//	InvertPixel( desLinePos, desBitOffset );
//	GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, desBitOffset );
//	pixelPos = nextPixelPos;
//	newBitOffset = nextBitOffset;
	pixelPos = desLinePos;
	newBitOffset = desBitOffset;
	for( i = 0; i < len; i++ )
	{
		InvertPixel( pixelPos, newBitOffset );
		GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, newBitOffset );
		pixelPos = nextPixelPos;
		newBitOffset = nextBitOffset;
	}
	#endif
#else	// 整单位像素
	pixelPos = desLinePos;

	for( i = 0; i < len; i++, pixelPos++ )
		(*pixelPos) ^= unitMask;
#endif
}

void LineUnicolorMaskOp( PIXEL *desLinePos, CHAR desBitOffset, WORD len, PIXEL srcValue, WORD mask, WORD op )
{
	PIXEL *pixelPos;
	PIXEL unitValue = srcValue;
	WORD i, enBit = 0;
		
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

//	PixelOp( desLinePos, desBitOffset, srcValue, op );
//	GetNextPixelPosition( &nextPixelPos, &nextBitOffset, desLinePos, desBitOffset );
//	pixelPos = nextPixelPos;
//	newBitOffset = nextBitOffset;
	pixelPos = desLinePos;
	newBitOffset = desBitOffset;
	for( i = 0; i < len; i++ )
	{
		if( enBit == 0 )
			enBit = 0x8000;
		if( enBit & mask )
			PixelOp( pixelPos, newBitOffset, srcValue, op );
		GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, newBitOffset );
		pixelPos = nextPixelPos;
		newBitOffset = nextBitOffset;
		enBit = enBit >> 1;
	}
#else	// 整单位像素
	pixelPos = desLinePos;
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			for( i = 0; i < len; i++, pixelPos++ )
			{
				if( enBit == 0 )
					enBit = 0x8000;
				if( enBit & mask )
					(*pixelPos) = unitValue;
				enBit = enBit >> 1;
			}
			break;
		case GPC_XOR_STYLE:
			for( i = 0; i < len; i++, pixelPos++ )
			{
				if( enBit == 0 )
					enBit = 0x8000;
				if( enBit & mask )
					(*pixelPos) ^= unitValue;
				enBit = enBit >> 1;
			}
			break;
		case GPC_AND_STYLE:
			for( i = 0; i < len; i++, pixelPos++ )
			{
				if( enBit == 0 )
					enBit = 0x8000;
				if( enBit & mask )
					(*pixelPos) &= unitValue;
				enBit = enBit >> 1;
			}
			break;
		case GPC_OR_STYLE:
			for( i = 0; i < len; i++, pixelPos++ )
			{
				if( enBit == 0 )
					enBit = 0x8000;
				if( enBit & mask )
					(*pixelPos) |= unitValue;
				enBit = enBit >> 1;
			}
			break;
		default:
			break;
	}
#endif
}

//LineOp 对指定的两行进行逻辑操作(包括按位与、或、异或、复制等)
void LineOp( PIXEL *desLinePos, CHAR desBitOffset, PIXEL *srcLinePos, CHAR srcBitOffset, WORD len, WORD op )
{
	WORD i;

#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL *srcPos = srcLinePos, *desPos = desLinePos;
	PIXEL mask;
	WORD totalOffset, num; 

	totalOffset = len * BITS_PER_PIXEL + desBitOffset;	// 要求len小于1023
	num = totalOffset >> ( 3 + ( PIXEL_BYTE >> 1 ) );
	if( num == 0 )	// 像素尺寸小于剩余单位
	{
		WORD r = PIXEL_UNIT - totalOffset - desBitOffset;
		PIXEL tempValue;

		mask = MAKEMASK( totalOffset ) << r;
		if( srcBitOffset <= desBitOffset )
		{
			tempValue = (*srcPos) >> ( desBitOffset - srcBitOffset );
			PixelMaskOp( desPos, *srcPos, mask, op );
		}
		else	// (低效)
		{
			PIXEL *curDesPos = desPos, *curSrcPos = srcPos;
			PIXEL *nextDesPos, *nextSrcPos;
			CHAR curDesOffset = desBitOffset, curSrcOffset = srcBitOffset;
			CHAR nextDesOffset, nextSrcOffset;

			for( i = 0; i < r; i++ )
			{
				newGetPixel( &tempValue, curSrcPos, curSrcOffset );
				PixelOp( curDesPos, curDesOffset, tempValue, op );
				GetNextPixelPosition( &nextDesPos, &nextDesOffset, curDesPos, curDesOffset );
				GetNextPixelPosition( &nextSrcPos, &nextSrcOffset, curSrcPos, curSrcOffset );
				curDesPos = nextDesPos;
				curDesOffset = nextDesOffset;
				curSrcPos = nextSrcPos;
				curSrcOffset = nextSrcOffset;
			}
		}
	}
	else	// 其他
	{
		WORD r = totalOffset & ( PIXEL_UNIT -1 );	// 尾比特数

		if( desBitOffset == srcBitOffset )	// 边界对齐
		{
			mask = MAKEMASK( PIXEL_UNIT - srcBitOffset );
			PixelMaskOp( desPos, *srcPos, mask, op );
			switch( op )
			{
				case GPC_REPLACE_STYLE:
				case GPC_COPY_STYLE:
					for( i = 0; i < num; i++, desPos++, srcPos++ )
						(*desPos) = (*srcPos);
					break;
				case GPC_XOR_STYLE:
					for( i = 0; i < num; i++, desPos++, srcPos++ )
						(*desPos) = (*srcPos);
					break;
				case GPC_AND_STYLE:
					for( i = 0; i < num; i++, desPos++, srcPos++ )
						(*desPos) = (*srcPos);
					break;
				case GPC_OR_STYLE:
					for( i = 0; i < num; i++, desPos++, srcPos++ )
						(*desPos) = (*srcPos);
					break;
				default:
					break;
			}
			mask = ~( MAKEMASK( PIXEL_UNIT - r ) );
			PixelMaskOp( desPos, *srcPos, mask, op );
		}
		else	// (低效)
		{
			PIXEL *curDesPos = desPos, *curSrcPos = srcPos;
			PIXEL *nextDesPos, *nextSrcPos;
			PIXEL tempValue;
			CHAR curDesOffset = desBitOffset, curSrcOffset = srcBitOffset;
			CHAR nextDesOffset, nextSrcOffset;

			for( i = 0; i < len; i++ )
			{
				newGetPixel( &tempValue, curSrcPos, curSrcOffset );
				PixelOp( curDesPos, curDesOffset, tempValue, op );
				GetNextPixelPosition( &nextDesPos, &nextDesOffset, curDesPos, curDesOffset );
				GetNextPixelPosition( &nextSrcPos, &nextSrcOffset, curSrcPos, curSrcOffset );
				curDesPos = nextDesPos;
				curDesOffset = nextDesOffset;
				curSrcPos = nextSrcPos;
				curSrcOffset = nextSrcOffset;
			}
		}
	}
	#else
		#if ( BITS_PER_PIXEL & 0x7 )	// 非整字节像素(低效)
		PIXEL *curDesPos = desLinePos, *curSrcPos = srcLinePos;
		PIXEL *nextDesPos, *nextSrcPos;
		PIXEL tempValue;
		CHAR curDesOffset = 0, curSrcOffset = srcBitOffset;
		CHAR nextDesOffset, nextSrcOffset;

		for( i = 0; i < len; i++ )
		{
			newGetPixel( &tempValue, curSrcPos, curSrcOffset );
			PixelOp( curDesPos, curDesOffset, tempValue, op );
			GetNextPixelPosition( &nextDesPos, &nextDesOffset, curDesPos, curDesOffset );
			GetNextPixelPosition( &nextSrcPos, &nextSrcOffset, curSrcPos, curSrcOffset );
			curDesPos = nextDesPos;
			curDesOffset = nextDesOffset;
			curSrcPos = nextSrcPos;
			curSrcOffset = nextSrcOffset;
		}
		#else	// 整字节像素
		BYTE *srcPos, *desPos;
		WORD num = len * ( BITS_PER_PIXEL >> 3 );

		srcPos = (( BYTE *) srcLinePos) + ( srcBitOffset >> 3 );
		desPos = (( BYTE *) desLinePos) + ( desBitOffset >> 3 );

		switch( op )
		{
			case GPC_REPLACE_STYLE:
			case GPC_COPY_STYLE:
				for( i = 0; i < num; i++, desPos++, srcPos++ )
					(*desPos) = (*srcPos);
				break;
			case GPC_XOR_STYLE:
				for( i = 0; i < num; i++, desPos++, srcPos++ )
					(*desPos) = (*srcPos);
				break;
			case GPC_AND_STYLE:
				for( i = 0; i < num; i++, desPos++, srcPos++ )
					(*desPos) = (*srcPos);
				break;
			case GPC_OR_STYLE:
				for( i = 0; i < num; i++, desPos++, srcPos++ )
					(*desPos) = (*srcPos);
				break;
			default:
				break;
		}
		#endif
	#endif
#else	// 整单位像素
	PIXEL *srcPos = srcLinePos, *desPos = desLinePos;
	
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			for( i = 0; i < len; i++, desPos++, srcPos++ )
				(*desPos) = (*srcPos);
			break;
		case GPC_XOR_STYLE:
			for( i = 0; i < len; i++, desPos++, srcPos++ )
				(*desPos) ^= (*srcPos);
			break;
		case GPC_AND_STYLE:
			for( i = 0; i < len; i++, desPos++, srcPos++ )
				(*desPos) &= (*srcPos);
			break;
		case GPC_OR_STYLE:
			for( i = 0; i < len; i++, desPos++, srcPos++ )
				(*desPos) |= (*srcPos);
			break;
		default:
			break;
	}
#endif
}

//BlockUnicolorOP 对指定块进行单色逻辑操作(包括按位与、或、异或、复制等)
void BlockUnicolorOp( PIXEL *desLinePos, CHAR desBitOffset, WORD width, WORD height, PIXEL srcValue, WORD op, VRAM *vram )
{
	PIXEL *curPos = desLinePos, *nextPos;
	CHAR curBitOffset = desBitOffset, nextBitOffset;
	WORD i;

	for( i = 0; i < height; i++ )
	{
		LineUnicolorOp( curPos, curBitOffset, width, srcValue, op );
		GetNextLine( &nextPos, &nextBitOffset, curPos, curBitOffset, vram );
		curPos = nextPos;
		curBitOffset = nextBitOffset;
	}
}

void BlockUnicolorMaskOp( PIXEL *desLinePos, CHAR desBitOffset, WORD width, WORD height, PIXEL srcValue, WORD mask, WORD op, VRAM *vram )
{
	PIXEL *curPos = desLinePos, *nextPos;
	CHAR curBitOffset = desBitOffset, nextBitOffset;
	WORD i;

	for( i = 0; i < height; i++ )
	{
		LineUnicolorMaskOp( curPos, curBitOffset, width, srcValue, mask, op );
		GetNextLine( &nextPos, &nextBitOffset, curPos, curBitOffset, vram );
		curPos = nextPos;
		curBitOffset = nextBitOffset;
	}
}

//InvertBlock 对指定块进行反色操作
void InvertBlock( PIXEL *desLinePos, CHAR desBitOffset, WORD width, WORD height, VRAM *vram )
{
	PIXEL *curPos = desLinePos, *nextPos;
	CHAR curBitOffset = desBitOffset, nextBitOffset;
	WORD i;

	for( i = 0; i < height; i++ )
	{
		InvertLine( curPos, curBitOffset, width );
		GetNextLine( &nextPos, &nextBitOffset, curPos, curBitOffset, vram );
		curPos = nextPos;
		curBitOffset = nextBitOffset;
	}
}

//BlockOP 对指定的两块区域进行逻辑操作(包括按位与、或、异或、复制等)
void BlockOp( PIXEL *desLinePos, CHAR desBitOffset, PIXEL *srcLinePos, CHAR srcBitOffset, WORD width, WORD height, WORD op, VRAM *desVram, VRAM *srcVram )
{
	PIXEL *curDesPos = desLinePos, *curSrcPos = srcLinePos;
	PIXEL *nextDesPos, *nextSrcPos;
	CHAR curDesOffset = desBitOffset, curSrcOffset = srcBitOffset;
	CHAR nextDesOffset, nextSrcOffset;
	WORD i;

	for( i = 0; i < height; i++ )
	{
		LineOp( curDesPos, curDesOffset, curSrcPos, curSrcOffset, width, op );
		GetNextLine( &nextDesPos, &nextDesOffset, curDesPos, curDesOffset, desVram );
		GetNextLine( &nextSrcPos, &nextSrcOffset, curSrcPos, curSrcOffset, srcVram );
		curDesPos = nextDesPos;
		curDesOffset = nextDesOffset;
		curSrcPos = nextSrcPos;
		curSrcOffset = nextSrcOffset;
	}
}
//---------------functions for test------------------------
//GetVRAM 申请一块指定大小的vram
VRAM *GetVRAM( WORD width, WORD height )
{
	VRAM *vram;

	vram = (VRAM *)SysLmalloc( sizeof( VRAM ) );
	if( vram == NULL )
		return NULL;

	vram->widthInPixel = width;
	vram->widthInBit = width * BITS_PER_PIXEL;
	vram->height = height;
	vram->lcdx = 0;
	vram->lcdy = 0;
	vram->ad = ( PIXEL * )SysLmalloc( ( vram->widthInBit * vram->height ) >> 3 );
	if( vram->ad == NULL )
		return NULL;
	else
	{
		memset( vram->ad, 0xff, ( vram->widthInBit * vram->height ) >> 3 );
		return vram;
	}
}

//GetBlock 申请一块指定大小的vram
PIXEL *GetBlock( WORD width, WORD height )
{
	PIXEL *block;
	
	block = ( PIXEL * )SysLmalloc( ( width * height * BITS_PER_PIXEL ) >> 3 );
	if( block == NULL )
		return NULL;
	else
		return block;
}

//FreeVRAM 释放一块指定的vram
void FreeVRAM( VRAM *vram )
{
	if( vram == NULL )
		return;

	if( vram->ad != NULL )
		SysLfree( vram->ad );

	SysLfree( vram );
	
	return;
}

/*
void VRAMMap( WORD *x2, WORD *y2, WORD x1, WORD y1, VRAM *vram )
{
	*x2 = x1 < vram->lcdx ? 0 : x1 - vram->lcdx;
	*y2 = y1 < vram->lcdy ? 0 : y1 - vram->lcdy;
	if( *x2 >= vram->widthInPixel )
		*x2 = vram->widthInPixel -1;
	if( *y2 >= vram->height )
		*y2 = vram->height -1;
}
*/

⌨️ 快捷键说明

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