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

📄 vramop.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************
*
*
*
*
*
* FUNCTION LIST
* GetPixel					获取指定位置的像素值
* GetPixelPosition			获取指定像素的物理地址
* GetNextPixel				获取指定像素的下一个像素值
* GetNextPixelPosition		获取指定像素的下一个像素的物理地址
* GetLine					获取指定行的行首像素的物理地址
* GetNextLine				获取指定行的下一行的行首像素的物理地址(同列)
* SetPixel					设置指定像素的值
* PixelOp					对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
* PixelMaskOp				利用掩码对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
* SetLine					设置指定行的值
* LineUnicolorOp			对指定行进行单色逻辑操作(包括按位与、或、异或、复制等)
* LineOp					对指定的两行进行逻辑操作(包括按位与、或、异或、复制等)
* BlockUnicolorOP			对指定块进行单色逻辑操作(包括按位与、或、异或、复制等)
* BlockOP					对指定的两块区域进行逻辑操作(包括按位与、或、异或、复制等)
*
*
*
*
***********************************************************************/

#include <string.h>
#include "gpc.h"
#include "lmalloc.h"
#include "vramop.h"

#if BITS_PER_PIXEL >= 8
PIXEL INDEX_RED_MASK = MAKEMASK( INDEX_RED_BITS );
PIXEL INDEX_GREEN_MASK = MAKEMASK( INDEX_GREEN_BITS );
PIXEL INDEX_BLUE_MASK = MAKEMASK( INDEX_BLUE_BITS );
const PIXEL PixelMask =
 ( MAKEMASK( INDEX_RED_BITS ) << INDEX_RED_OFFSET ) |
 ( MAKEMASK( INDEX_GREEN_BITS ) << INDEX_GREEN_OFFSET ) |
 ( MAKEMASK( INDEX_BLUE_BITS ) << INDEX_BLUE_OFFSET );
#else
const PIXEL PixelMask =  (MAKEMASK( BITS_PER_PIXEL ));
#endif

#if ( BITS_PER_PIXEL == 2 )	//特化
PIXEL ByteMask2[4] = { 0x03, 0x0c, 0x30, 0xc0 };
#endif


//关于vram的操作
//GetPixel 获取指定位置的像素值
void newGetPixel( PIXEL *pixelValue, PIXEL *pixelPos, CHAR bitOffset )
{
#if BITS_PER_PIXEL == PIXEL_BIT_DEPTH	// 像素存储无冗余
	#if ( BITS_PER_PIXEL != PIXEL_UNIT)	// 非整单位像素
//		PIXEL tempValue;
//		CHAR r;
//		r = PIXEL_UNIT - bitOffset;
//		if( r >= BITS_PER_PIXEL )	// 像素尺寸小于剩余单位
//		{
			*pixelValue = ( (*pixelPos) >> bitOffset ) & PixelMask;
//		}
//		else    // 像素越过边界
//		{
//			tempValue = (*pixelPos) << ( BITS_PER_PIXEL - r );
//			pixelPos++;
//			r = PIXEL_UNIT - BITS_PER_PIXEL + r;
//			*pixelValue = ( tempValue | ( (*pixelPos) >> r ) ) & PixelMask;
//		}
	#else	// 整单位像素
		*pixelValue = (*pixelPos);
	#endif
#else	// 带冗余存储像素
	*pixelValue = (*pixelPos) & PixelMask;
#endif
}

//GetPixelPosition 获取指定像素的物理地址
void GetPixelPosition( PIXEL **pixelPos, CHAR *bitOffset, VRAM *vram, WORD x, WORD y )
{
	DWORD totalOffset;
	
	totalOffset = y * vram->widthInBit + x * BITS_PER_PIXEL;
	(*pixelPos) = vram->ad + ( totalOffset >> LOG_UNIT );
	(*bitOffset) = ( CHAR ) ( totalOffset & ( PIXEL_UNIT -1 ) );
}

//GetNextPixelPosition 获取指定像素的下一个像素的物理地址
void GetNextPixelPosition( PIXEL **nextPixelPos, CHAR *newBitOffset, PIXEL *pixelPos, CHAR bitOffset )
{
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
//	PIXEL tempValue;
	CHAR r;
	r = PIXEL_UNIT - bitOffset;
	if( r > BITS_PER_PIXEL )	// 像素尺寸小于剩余单位
	{
		*nextPixelPos = pixelPos;
		*newBitOffset = bitOffset + BITS_PER_PIXEL;
	}
	else    // 像素越过或恰好到达边界
	{
		*nextPixelPos = pixelPos +1;
		*newBitOffset = BITS_PER_PIXEL - r;
	}
#else	// 整单位像素
	*nextPixelPos = pixelPos +1;
	*newBitOffset = 0;
#endif
}

//GetNextPixel 获取指定像素的下一个像素值
void GetNextPixel( PIXEL *pixelValue, PIXEL *pixelPos, CHAR bitOffset )
{
	PIXEL *nextPixelPos;
	CHAR newBitOffset;
	
	GetNextPixelPosition( &nextPixelPos, &newBitOffset, pixelPos, bitOffset );
	newGetPixel( pixelValue, nextPixelPos, newBitOffset );
}

//GetLine 获取指定行的行首像素的物理地址
void GetLine( PIXEL **pixelPos, CHAR *bitOffset, VRAM *vram, WORD y )
{
	DWORD totalOffset;

	totalOffset = y * vram->widthInBit;
	*pixelPos = vram->ad + ( totalOffset >> LOG_UNIT );
	(*bitOffset) = ( CHAR ) ( totalOffset & ( PIXEL_UNIT -1 ) );
}

//GetNextLine 获取指定行的下一行的行首像素的物理地址
// longn_qi 2002/05/16 replace with macro
/*
void GetNextLine( PIXEL **nextPixelPos, CHAR *nextBitOffset, PIXEL *pixelPos, CHAR bitOffset, VRAM *vram )
{
	DWORD totalOffset;

	totalOffset = bitOffset + vram->widthInBit;
	*nextPixelPos = pixelPos + ( totalOffset >> ( 3 + ( PIXEL_BYTE >> 1 ) ) );
	(*nextBitOffset) = ( CHAR )( totalOffset & ( PIXEL_UNIT -1 ) );
}
*/

//SetPixel 设置指定像素的值
void newSetPixel( PIXEL *pixelPos, CHAR bitOffset, PIXEL pixelValue )
{
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
/*	CHAR r;
	
	r = PIXEL_UNIT - bitOffset;
	if( r >= BITS_PER_PIXEL )	// 像素尺寸小于剩余单位
	{
		PIXEL tempValue;

		tempValue = (*pixelPos) & ~( PixelMask << ( r - BITS_PER_PIXEL ) );
		(*pixelPos) = ( pixelValue << ( r - BITS_PER_PIXEL ) ) | tempValue;
	}
	else    // 像素越过边界
	{
		PIXEL mask;

		mask = MAKEMASK( r );
		(*pixelPos) = ( pixelValue >> ( BITS_PER_PIXEL - r ) ) | ( (*pixelPos) & ~mask );
		pixelPos++;
		r = PIXEL_UNIT - BITS_PER_PIXEL + r;
		mask = MAKEMASK( r );
		(*pixelPos) = ( (*pixelPos) & mask ) | ( pixelValue << r );
	}
*/
	CHAR	r;

	r = PIXEL_UNIT - bitOffset;
	if( r >= BITS_PER_PIXEL )	// 像素尺寸小于剩余单位
	{
		PIXEL	mask = MAKEMASK( BITS_PER_PIXEL );

		(*pixelPos) = ( (*pixelPos) & ~( mask << bitOffset ) ) | ( pixelValue << bitOffset );
	}
	else    // 像素越过边界
	{
	#if ( PIXEL_UNIT == 8 )
		WORD	mask = MAKEMASK( BITS_PER_PIXEL );
		WORD	*p = (WORD *)pixelPos;
		(*p) = ( (*p) & ~( mask << bitOffset ) ) | ( ( (WORD) pixelValue ) << bitOffset );
	#elif ( PIXEL_UNIT == 16 )
		DWORD	mask = MAKEMASK( BITS_PER_PIXEL );
		DWORD	*p = (DWORD *)pixelPos;
		(*p) = ( (*p) & ~( mask << bitOffset ) ) | ( ( (DWORD) pixelValue ) << bitOffset );
	#elif ( PIXEL_UNIT > 16 )
		PIXEL	mask;

		mask = MAKEMASK( bitOffset );
		(*pixelPos) = ( (*pixelPos) & mask ) | ( pixelValue << bitOffset );
		pixelPos++;
		mask = ~MAKEMASK( BITS_PER_PIXEL - r );
		(*pixelPos) = ( (*pixelPos) & mask ) | ( pixelValue >> r );	
	#endif
	}
#else	// 整单位像素
	*pixelPos = pixelValue;
#endif
}

//SetLine 设置指定行的值
void SetLine( PIXEL *linePos, CHAR bitOffset, WORD len, PIXEL lineValue )
{
	PIXEL *pixelPos;
	PIXEL unitValue = lineValue;
	WORD i;
	
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL mask;
	WORD totalOffset, num, r;
	
	pixelPos = linePos;
	totalOffset = len * BITS_PER_PIXEL + bitOffset;
	num = totalOffset >> LOG_UNIT;
	r = totalOffset & ( PIXEL_UNIT -1 );
	for( i = BITS_PER_PIXEL; i < PIXEL_UNIT; i += BITS_PER_PIXEL )
		unitValue = lineValue | ( unitValue << BITS_PER_PIXEL );
	
	if( bitOffset != 0 )
	{						
		if( num == 0 )	// 不到一个整单位
		{
			mask = MAKEMASK( totalOffset - bitOffset ) << bitOffset;
			(*pixelPos) = ( (*pixelPos) & ~mask ) | ( unitValue & mask );
		}
		else	// 超过一个整单位
		{
			mask = ~MAKEMASK( bitOffset );
			(*pixelPos) = ( (*pixelPos) & ~mask ) | ( unitValue & mask );
			pixelPos++;
			
			for( i = 1; i < num; i++, pixelPos++ )
				(*pixelPos) = unitValue;
			
			if( r != 0 )	// 尾部
			{
				mask = MAKEMASK( r );
				(*pixelPos) = ( (*pixelPos) & ~mask ) | ( unitValue & mask );
			}
		}
	}
	else	// 起始边界对齐
	{
		for( i = 0; i < num; i++, pixelPos++ )
			(*pixelPos) = unitValue;
		
		if( r != 0 )	// 尾部
		{
			mask = MAKEMASK( r );
			(*pixelPos) = ( (*pixelPos) & ~mask ) | ( unitValue & mask );
		}
	}	
	#else
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

	pixelPos = linePos;
	newBitOffset = bitOffset;
	for( i = 0; i < len; i++ )
	{
		newSetPixel( pixelPos, newBitOffset, lineValue );
		GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, newBitOffset );
		pixelPos = nextPixelPos;
		newBitOffset = nextBitOffset;
	}
	#endif
#else	// 整单位像素
	pixelPos = linePos;
	for( i = 0; i < len; i++, pixelPos++ )
		*pixelPos = lineValue;
#endif
}

//PixelOp 对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
void PixelOp( PIXEL *desPixelPos, CHAR desBitOffset, PIXEL srcValue, WORD op )
{
	PIXEL desValue;
	
/*	newGetPixel( &desValue, desPixelPos, desBitOffset );
	
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			desValue = srcValue;
			break;
		case GPC_XOR_STYLE:
			desValue ^= srcValue;
			break;
		case GPC_AND_STYLE:
			desValue &= srcValue;
			break;
		case GPC_OR_STYLE:
			desValue |= srcValue;
			break;
		default:
			break;
	}
	newSetPixel( desPixelPos, desBitOffset, desValue );	
*/
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL	mask;

	mask = MAKEMASK( BITS_PER_PIXEL ) << desBitOffset;
	PixelMaskOp( desPixelPos, (PIXEL)(srcValue << desBitOffset), mask, op );
	#else	// 其他
	
	#endif
#else	// 整单位像素
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			(*desPixelPos) = srcValue;
			break;
		case GPC_XOR_STYLE:
			(*desPixelPos) ^= srcValue;
			break;
		case GPC_AND_STYLE:
			(*desPixelPos) &= srcValue;
			break;
		case GPC_OR_STYLE:
			(*desPixelPos) |= srcValue;
			break;
		default:
			break;
	}
#endif
}

void InvertPixel( PIXEL *desPixelPos, CHAR desBitOffset )
{
	PIXEL desValue;
	
	newGetPixel( &desValue, desPixelPos, desBitOffset );
	newSetPixel( desPixelPos, desBitOffset, (PIXEL)( ~desValue ) );	
}

//PixelMaskOp 利用掩码对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
void PixelMaskOp( PIXEL *desPixelPos, PIXEL srcValue, PIXEL srcMask, WORD op )
{
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			(*desPixelPos) = ( (*desPixelPos) & ~srcMask ) | ( srcValue & srcMask );
			break;
		case GPC_XOR_STYLE:
			(*desPixelPos) = (*desPixelPos) ^ ( srcValue & srcMask );
			break;
		case GPC_AND_STYLE:
			(*desPixelPos) = (*desPixelPos) & ( ( ~srcMask ) | ( srcValue & srcMask ) );
			break;
		case GPC_OR_STYLE:
			(*desPixelPos) = (*desPixelPos) | ( srcValue & srcMask );
			break;
		default:
			break;
	}	
}

//UnitMaskOp 利用掩码对指定一组像素进行逻辑操作(包括按位与、或、异或、复制等)
void UnitMaskOp( WORD *desPixelPos, WORD srcValue, WORD srcMask, WORD op )
{
	switch( op )
	{
		case GPC_REPLACE_STYLE:
		case GPC_COPY_STYLE:
			(*desPixelPos) = ( (*desPixelPos) & ~srcMask ) | ( srcValue & srcMask );
			break;
		case GPC_XOR_STYLE:
			(*desPixelPos) = (*desPixelPos) ^ ( srcValue & srcMask );
			break;
		case GPC_AND_STYLE:
			(*desPixelPos) = (*desPixelPos) & ( ( ~srcMask ) | ( srcValue & srcMask ) );
			break;
		case GPC_OR_STYLE:
			(*desPixelPos) = (*desPixelPos) | ( srcValue & srcMask );
			break;
		default:
			break;
	}	
}

//LineUnicolorOp 对指定行进行单色逻辑操作(包括按位与、或、异或、复制等)
void LineUnicolorOp( PIXEL *desLinePos, CHAR desBitOffset, WORD len, PIXEL srcValue, WORD op )
{
	PIXEL *pixelPos;
	PIXEL unitValue = srcValue;
	WORD i;
		
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#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 );
	for( i = BITS_PER_PIXEL; i < PIXEL_UNIT; i += BITS_PER_PIXEL )
		unitValue = srcValue | ( unitValue << BITS_PER_PIXEL );
	
	if( desBitOffset != 0 )
	{						
		if( num == 0 )	// 不到一个整单位
		{
			mask = MAKEMASK( totalOffset - desBitOffset ) << desBitOffset;
			PixelMaskOp( pixelPos, unitValue, mask, op );
		}
		else	// 超过一个整单位
		{
			mask = ~MAKEMASK( desBitOffset );
			PixelMaskOp( pixelPos, unitValue, mask, op );	// 头部
			pixelPos++;
			
			switch( op )
			{
				case GPC_REPLACE_STYLE:
				case GPC_COPY_STYLE:
					for( i = 1; i < num; i++, pixelPos++ )
						(*pixelPos) = unitValue;
					break;
				case GPC_XOR_STYLE:
					for( i = 1; i < num; i++, pixelPos++ )
						(*pixelPos) ^= unitValue;
					break;
				case GPC_AND_STYLE:
					for( i = 1; i < num; i++, pixelPos++ )
						(*pixelPos) &= unitValue;
					break;
				case GPC_OR_STYLE:
					for( i = 1; i < num; i++, pixelPos++ )
						(*pixelPos) |= unitValue;
					break;
				default:
					break;
			}
			
			if( r != 0 )	// 尾部
			{
				mask = MAKEMASK( r );
				PixelMaskOp( pixelPos, unitValue, mask, op );
			}
		}
	}
	else	// 起始边界对齐
	{
		switch( op )
		{
			case GPC_REPLACE_STYLE:
			case GPC_COPY_STYLE:
				for( i = 0; i < num; i++, pixelPos++ )
					(*pixelPos) = unitValue;
				break;
			case GPC_XOR_STYLE:
				for( i = 0; i < num; i++, pixelPos++ )
					(*pixelPos) ^= unitValue;
				break;
			case GPC_AND_STYLE:
				for( i = 0; i < num; i++, pixelPos++ )
					(*pixelPos) &= unitValue;
				break;
			case GPC_OR_STYLE:
				for( i = 0; i < num; i++, pixelPos++ )
					(*pixelPos) |= unitValue;
				break;
			default:
				break;
		}
		
		if( r != 0 )	// 尾部
		{
			mask = MAKEMASK( r );
			PixelMaskOp( pixelPos, unitValue, mask, op );
		}
	}	
	#else	// 其他
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

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

void InvertLine( PIXEL *desLinePos, CHAR desBitOffset, WORD len )
{
	PIXEL *pixelPos;
	WORD i;
	PIXEL unitMask = -1;
		
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素

⌨️ 快捷键说明

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