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

📄 vramop.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL mask;
	WORD totalOffset, num, r;
	
	pixelPos = desLinePos;
	totalOffset = len * BITS_PER_PIXEL + desBitOffset;
	num = totalOffset >> LOG_UNIT;
	r = totalOffset & ( PIXEL_UNIT -1 );
	
	if( desBitOffset != 0 )
	{						
		if( num == 0 )	// 不到一个整单位
		{
			mask = MAKEMASK( totalOffset - desBitOffset ) << desBitOffset;
			(*pixelPos) ^= mask;
		}
		else	// 超过一个整单位
		{
			mask = ~MAKEMASK( desBitOffset );
			(*pixelPos) ^= mask;
			pixelPos++;
			
			for( i = 0; i < num; i++, pixelPos++ )
				(*pixelPos) ^= unitMask;
			
			if( r != 0 )	// 尾部
			{
				mask = MAKEMASK( r );
				(*pixelPos) ^= mask;
			}
		}
	}
	else	// 起始边界对齐
	{
		for( i = 0; i < num; i++, pixelPos++ )
			(*pixelPos) ^= unitMask;
		
		if( r != 0 )	// 尾部
		{
			mask = MAKEMASK( r );
			(*pixelPos) ^= mask;
		}
	}	
	#else	// 其他
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

	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;

	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 )	// 像素尺寸为单位因子
	// 以16比特为一组
	WORD *srcPos = (WORD *)srcLinePos, *desPos = (WORD *)desLinePos;
	WORD mask;
	DWORD totalOffset, num, r; 

	totalOffset = len * BITS_PER_PIXEL + desBitOffset;	// 要求一行的像素个数小于1023
	num = totalOffset >> 4;
	r = totalOffset & 0xf;	// 尾比特数
	if( num == 0 )	// 像素尺寸小于剩余单位
	{
		WORD tempValue;

		mask = MAKEMASK( totalOffset - desBitOffset ) << desBitOffset;
			
		if( srcBitOffset <= desBitOffset )
		{
			tempValue = (*srcPos) << ( desBitOffset - srcBitOffset );
			UnitMaskOp( desPos, tempValue, mask, op );
		}
		else
		{
			tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> ( srcBitOffset - desBitOffset ) ) & 0xffff );
			UnitMaskOp( desPos, tempValue, mask, op );
		}
	}
	else	// 其他
	{
		if( desBitOffset == srcBitOffset )	// 边界对齐
		{
			mask = ~MAKEMASK( srcBitOffset );
			UnitMaskOp( desPos, *srcPos, mask, op );
			desPos++;
			srcPos++;
			switch( op )
			{
				case GPC_REPLACE_STYLE:
				case GPC_COPY_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
						(*desPos) = (*srcPos);
					break;
				case GPC_XOR_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
						(*desPos) ^= (*srcPos);
					break;
				case GPC_AND_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
						(*desPos) &= (*srcPos);
					break;
				case GPC_OR_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
						(*desPos) |= (*srcPos);
					break;
				default:
					break;
			}
			mask = MAKEMASK( r );
			UnitMaskOp( desPos, *srcPos, mask, op );
		}
		else	// 边界不对齐
		{
			WORD tempValue, t;
			
				
			if( desBitOffset < srcBitOffset )
			{
				t = srcBitOffset - desBitOffset;
				tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
			}
			else
			{
				t = desBitOffset - srcBitOffset;
				tempValue = (*srcPos) << t;
			}
			mask = ~MAKEMASK( desBitOffset );
			UnitMaskOp( desPos, tempValue, mask, op );

			desPos++;

			if( desBitOffset < srcBitOffset )
				srcPos++;
			else
				t = 16 - t;
			switch( op )
			{
				case GPC_REPLACE_STYLE:
				case GPC_COPY_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
					{
						tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
						(*desPos) = tempValue;
					}
					break;
				case GPC_XOR_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
					{
						tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
						(*desPos) ^= tempValue;
					}
					break;
				case GPC_AND_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
					{
						tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
						(*desPos) &= tempValue;
					}
					break;
				case GPC_OR_STYLE:
					for( i = 1; i < num; i++, desPos++, srcPos++ )
					{
						tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
						(*desPos) |= tempValue;
					}
					break;
				default:
					break;
			}
			
			mask = MAKEMASK( r );
			tempValue = (WORD)( ( (*((DWORD *)srcPos)) >> t ) );
			UnitMaskOp( desPos, tempValue, mask, op );			
		}
	}
	#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->widthInUnit = ( vram->widthInBit + PIXEL_UNIT -1 ) >> LOG_UNIT;
	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;
}
*/

/////////////////////////////////////////////////////////////////////////////////
void LinearColorMap( BYTE *color, BYTE index, BYTE maxColor, BYTE maxIndex)
{
	//if( index > maxIndex || !maxIndex || !maxColor )
	//	return E_PAR;
	
	if( maxIndex == maxColor )
		*color = index;
	else
		*color = index * maxColor / maxIndex;
		
	return;
}

#if ( BITS_PER_PIXEL == 2 )	//特化
void VLineUnicolorOp( PIXEL *desLinePos, CHAR desBitOffset, WORD len, PIXEL srcValue, WORD op, VRAM *vram )
{
	DWORD	lw = vram->widthInPixel >> 2;
	WORD	i;
	CHAR	k = desBitOffset >> 1;
	PIXEL	srcMask = ByteMask2[k];
	
	srcValue = srcValue << desBitOffset;

	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			for( i = 0; i < len; i++, desLinePos += lw )
				(*desLinePos) = ( (*desLinePos) & ~srcMask ) | ( srcValue & srcMask );
			break;
		case GPC_XOR_STYLE:
			for( i = 0; i < len; i++, desLinePos += lw )
				(*desLinePos) = (*desLinePos) ^ ( srcValue & srcMask );
			break;
		case GPC_AND_STYLE:
			for( i = 0; i < len; i++, desLinePos += lw )
				(*desLinePos) = (*desLinePos) & ( ( ~srcMask ) | ( srcValue & srcMask ) );
			break;
		case GPC_OR_STYLE:
			for( i = 0; i < len; i++, desLinePos += lw )
				(*desLinePos) = (*desLinePos) | ( srcValue & srcMask );
			break;
		default:
			break;
	}

	return;
}
#endif

⌨️ 快捷键说明

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