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

📄 opvram_v0.1a.c

📁 一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************
*
*
*
*
*
* FUNCTION LIST
* GetPixel					获取指定位置的像素值
* GetPixelPosition			获取指定像素的物理地址
* GetNextPixel				获取指定像素的下一个像素值
* GetNextPixelPosition		获取指定像素的下一个像素的物理地址
* GetLine					获取指定行的行首像素的物理地址
* GetNextLine				获取指定行的下一行的行首像素的物理地址(同列)
* SetPixel					设置指定像素的值
* PixelOp					对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
* PixelMaskOp				利用掩码对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
* SetLine					设置指定行的值
* LineUnicolorOp			对指定行进行单色逻辑操作(包括按位与、或、异或、复制等)
* LineOp					对指定的两行进行逻辑操作(包括按位与、或、异或、复制等)
* BlockUnicolorOP			对指定块进行单色逻辑操作(包括按位与、或、异或、复制等)
* BlockOP					对指定的两块区域进行逻辑操作(包括按位与、或、异或、复制等)
*
*
*
*
***********************************************************************/

#include <string.h>
#include <sys\gpc.h>
#include <sys\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 );
PIXEL PixelMask =
 ( MAKEMASK( INDEX_RED_BITS ) << INDEX_RED_OFFSET ) |
 ( MAKEMASK( INDEX_GREEN_BITS ) << INDEX_GREEN_OFFSET ) |
 ( MAKEMASK( INDEX_BLUE_BITS ) << INDEX_BLUE_OFFSET );
#else
PIXEL PixelMask = MAKEMASK( BITS_PER_PIXEL );
#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) >> ( r - BITS_PER_PIXEL ) ) & 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 >> ( 3 + ( PIXEL_BYTE >> 1 ) ) );
	(*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 >> ( 3 + ( PIXEL_BYTE >> 1 ) ) );
	(*bitOffset) = ( CHAR ) ( totalOffset & ( PIXEL_UNIT -1 ) );
}

//GetNextLine 获取指定行的下一行的行首像素的物理地址
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 );
	}
#else	// 整单位像素
	*pixelPos = pixelValue;
#endif
}

//SetLine 设置指定行的值
void SetLine( PIXEL *linePos, CHAR bitOffset, WORD len, PIXEL lineValue )
{
	PIXEL *pixelPos;
	WORD i;
	
#if ( BITS_PER_PIXEL != PIXEL_UNIT )	// 非整单位像素
	#if ( PIXEL_UNIT % BITS_PER_PIXEL == 0 )	// 像素尺寸为单位因子
	PIXEL unitValue = lineValue;
	PIXEL tailValue, tailMask;
	WORD num, r;
	
	pixelPos = linePos;
	num = PIXEL_UNIT / BITS_PER_PIXEL;
	for( i = 1; i < num; i++ )
		unitValue = lineValue | ( unitValue << BITS_PER_PIXEL );
	
	if( bitOffset != 0 )
	{		
		WORD totalOffset = len * BITS_PER_PIXEL;	// 要求len小于1310
		
		tailMask = MAKEMASK( bitOffset );
		tailValue = unitValue >> ( PIXEL_UNIT - bitOffset );
//		unitValue = ( unitValue << bitOffset ) | tailValue;
				
		if( bitOffset + totalOffset <= PIXEL_UNIT )
		{
			r = PIXEL_UNIT - totalOffset - bitOffset;
			tailMask = MAKEMASK( totalOffset ) << r;
			(*pixelPos) = ( (*pixelPos) & ~tailMask ) | ( tailValue & tailMask );
		}
		else
		{
			(*pixelPos) = ( (*pixelPos) & tailMask ) | tailValue;	// 头部
			num = ( len - ( PIXEL_UNIT - bitOffset )/ BITS_PER_PIXEL )/ num;

			for( i = 0; i < num; i++, pixelPos++ )
				(*pixelPos) = unitValue;
			
			r = len * BITS_PER_PIXEL - num * PIXEL_UNIT - bitOffset;
			if( r != 0 )	// 尾部
			{
				tailMask = MAKEMASK( PIXEL_UNIT - r );
				(*pixelPos) = ( (*pixelPos) & tailMask ) | ( unitValue & ~tailMask );
			}
		}
	}
	else	// 起始边界对齐
	{
		num = len / num;
		for( i = 0; i < num; i++, pixelPos++ )
			(*pixelPos) = unitValue;
		
		r = len * BITS_PER_PIXEL - num * PIXEL_UNIT;
		if( r != 0 )	// 尾部
		{
			tailMask = MAKEMASK( PIXEL_UNIT - r );
			(*pixelPos) = ( (*pixelPos) & tailMask ) | ( unitValue & ~tailMask );
		}
	}	
	#else
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

//	newSetPixel( linePos, bitOffset, lineValue );
//	GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, bitOffset );
//	pixelPos = nextPixelPos;
//	newBitOffset = nextBitOffset;
	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 对指定像素进行逻辑操作(包括按位与、或、异或、复制等)
//PixelOp( PIXEL *desPixelPos, CHAR desBitOffset, PIXEL *srcPixelPos, CHAR srcBitOffset, WORD op )
void PixelOp( PIXEL *desPixelPos, CHAR desBitOffset, PIXEL srcValue, WORD op )
{
//	PIXEL srcValue;
	PIXEL desValue;
	
//	GetPixel( &srcValue, srcPixelPos, srcBitOffset );
	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 );	
}

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

//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 tailValue, tailMask;
	WORD num, r;
	
	pixelPos = desLinePos;
	num = PIXEL_UNIT / BITS_PER_PIXEL;
	for( i = 1; i < num; i++ )
		unitValue = srcValue | ( unitValue << BITS_PER_PIXEL );
	
	if( desBitOffset != 0 )
	{		
		WORD totalOffset = len * BITS_PER_PIXEL;	// 要求len小于1310
		
		tailMask = MAKEMASK( PIXEL_UNIT - desBitOffset );
		tailValue = unitValue & tailMask;
//		tailValue = unitValue >> ( PIXEL_UNIT - desBitOffset );
//		unitValue = ( unitValue << desBitOffset ) | tailValue;
				
		if( desBitOffset + totalOffset <= PIXEL_UNIT )
		{
			r = PIXEL_UNIT - totalOffset - desBitOffset;
			tailMask = MAKEMASK( totalOffset ) << r;
			PixelMaskOp( pixelPos, tailValue, tailMask, op );
		}
		else
		{
			PixelMaskOp( pixelPos, tailValue, tailMask, op );	// 头部
			num = ( len - ( PIXEL_UNIT - desBitOffset )/ BITS_PER_PIXEL )/ num;
			pixelPos++;
			
			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;
			}
			
			//r = len * BITS_PER_PIXEL - num * PIXEL_UNIT - desBitOffset;
			r = (WORD)( ( totalOffset + desBitOffset ) & ( PIXEL_UNIT -1) );
			if( r != 0 )	// 尾部
			{
				tailMask = ~( MAKEMASK( PIXEL_UNIT - r ) );
				PixelMaskOp( pixelPos, unitValue, tailMask, op );
			}
		}
	}
	else	// 起始边界对齐
	{
		num = len / num;
		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;
		}
		
		r = len * BITS_PER_PIXEL - num * PIXEL_UNIT;
		if( r != 0 )	// 尾部
		{
			tailMask = ~( MAKEMASK( PIXEL_UNIT - r ) );
			PixelMaskOp( pixelPos, unitValue, tailMask, op );
		}
	}	
	#else	// 其他
	PIXEL *nextPixelPos;
	CHAR nextBitOffset, newBitOffset;

//	PixelOp( desLinePos, desBitOffset, srcValue, op );
//	GetNextPixelPosition( &nextPixelPos, &nextBitOffset, pixelPos, desBitOffset );
//	pixelPos = nextPixelPos;
//	newBitOffset = nextBitOffset;
	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;

⌨️ 快捷键说明

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