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

📄 wndo2.c

📁 nucleus 文件系统,内核和彩色图形系统,在小系统上非常好用
💻 C
字号:
/*************************************************************************/
/*                                                                       */
/*         Copyright (c) 1997 - 1999 Accelerated Technology, Inc.        */
/*                                                                       */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      */
/* subject matter of this material.  All manufacturing, reproduction,    */
/* use, and sales rights pertaining to this subject matter are governed  */
/* by the license agreement.  The recipient of this software implicitly  */
/* accepts the terms of the license.                                     */
/*                                                                       */
/*************************************************************************/

/*************************************************************************/
/*                                                                       */
/* FILE NAME                                            VERSION          */
/*                                                                       */
/*      WNDO2.c                                          1.9             */
/*                                                                       */
/* COMPONENT                                                             */
/*                                                                       */
/*      All                                                              */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This file contains the CheckRes, FilldevTech & mwIMEM internal   */
/*  allocation routines & CloseBitmap function.                          */
/*                                                                       */
/* AUTHOR                                                                */
/*                                                                       */
/*      Robert G. Burrill, Accelerated Technology, Inc.                  */
/*                                                                       */
/* DATA STRUCTURES                                                       */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* FUNCTIONS                                                             */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* DEPENDENCIES                                                          */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*         NAME            DATE                    REMARKS               */
/*                                                                       */
/*                                                                       */
/*************************************************************************/

#include "meta_wnd.h"
#include "metconst.h"    /* MetaWINDOW Constant & Stucture Definitions */
#include "metports.h"    /* MetaWINDOW Port & Bitmap Definitions */
#include "grafdata.h"
#include "wndo.h"
#include "file.h"

/* Local functions  */
int InitGrafDriver(grafMap *argGRAFMAP);
void *GrafAlloc(int memSize);
void InitRowTable(grafMap *argBitMap, int argInrLve, int argInrSeg, int argInrSize);
int CloseGrafDriver(grafMap *argGRAFMAP);
void GrafFree(void *freePtr);


/* This module contains the routines that initialize the various 'virtual'
types of bitmaps that MetaWINDOW supports internally. Each routine returns
various types of GrafError style error codes, or -1 if driver can not be
loaded. */

/* Function CheckRes checks the pixResX and pixResY values for range */
void CheckRes(grafMap *gmap)
{

	if (gmap->pixResX > 0x0FFF) gmap->pixResX = 100;	/* limit it */
	if (gmap->pixResY > 0x0FFF) gmap->pixResY = 100;	/* ditto */

	return;
}


/* Function FilldevTech sets the device tech based on the number of bits
per pixel */
void FilldevTech(grafMap *gmap)
{
	
	if (gmap->pixPlanes > 1)
	{	/* devTech to planar */
		gmap->devTech = dtPlan;
		return;
	}

	if (gmap->pixBits == 8)
	{	/* devTech to 8 bit */
		gmap->devTech = dt8Bit;
		return;
	}
	if (gmap->pixBits == 16)
	{	/* devTech to 16 bit */
		gmap->devTech = dt16Bit;
		return;
	}
	if (gmap->pixBits == 24)
	{	/* devTech to 24 bit */
		gmap->devTech = dt24Bit;
		return;
	}
	if (gmap->pixBits == 1)
	{	/* devTech to mono */
		gmap->devTech = dtMono;
		return;
	}

	/* n bits fall through  */
	gmap->devTech = dtBits;
	return;
}


/* Function mwIMEM initializes a memory grafMap.  This routine uses GrafAlloc
to allocate rowtable(s) and local memory bitmap.  Rowtable entries should be
freed, as should the rowtable (maptable entries) themselves upon either an
error return from this routine, or when the grafMap is no longer required. */
int mwIMEM(grafMap *gmap)
{
	int rtSize;
	int myPlane;
	int myRow;
	long pixelBytes;
	long rowTablePtr;
	long *rowTablePtrPtr;

	CheckRes(gmap);	/* check resX and resY */

	gmap->mapTable[0] = 0;	/* zero maptable entries */
	gmap->mapTable[1] = 0;
	gmap->mapTable[2] = 0;
	gmap->mapTable[3] = 0;

	FilldevTech(gmap);	/* set managers and drivers */

	/* initialize primitives; error return if problem loading driver */
	if (InitGrafDriver(gmap) != 0) return(-1);

	/* calculate the size necessary for one planes rowtable */
	#if CPU386 == no	/* will shift left 2 loose bits? */
		if (gmap->pixHeight >= 0x4000) return(c_DivOflow);
	#endif

	rtSize = (gmap->pixHeight << 2);

	for (myPlane = 0; myPlane < gmap->pixPlanes; myPlane++)
	{	/* allocate each rowtable */
		gmap->mapTable[myPlane] = (long **) GrafAlloc(rtSize);
		if (gmap->mapTable[myPlane] == NULL) return(c_OutofMem);
	}

	gmap->mapFlags |= mfRowTabl;	/* set mapflags */

	/* calculate the size in bytes necessary for each raster */
	pixelBytes = (((gmap->pixBits * gmap->pixWidth) + 7) >> 3);
	if (pixelBytes > 0xFFFF) return(c_DivOflow);	

	gmap->pixBytes = (short) pixelBytes;
		
	/* try to allocate the whole thing in one chunk */
	pixelBytes *= gmap->pixHeight;
	if(pixelBytes <= 0x7FFFFFFF)
	{	/* so far okay */
		rtSize = (int) pixelBytes;	/* save plane size */
		pixelBytes *= gmap->pixPlanes;
		if(pixelBytes <= 0x7FFFFFFF)
		{	/* yes we can */
			rowTablePtr = (long) GrafAlloc(pixelBytes);	/* get pointer */
			if(rowTablePtr != NULL)
			{
				#ifdef FIXUP386
				/* fixup the ds relative rowtable pointers to be dll
				relative */
				rowTablePtr += dllFixup;
				#endif

				for (myPlane = 0; myPlane < gmap->pixPlanes; myPlane++)
				{
					*gmap->mapTable[myPlane] = (long *) rowTablePtr;
					rowTablePtr += rtSize;
				}

				InitRowTable(gmap, 0, 0, 1);
				return(0);
			}
		}
	}

	/* can't allocate big enough chunk so allocate each raster
	individually; since we gotta do it a raster at a time, do the
	rowtable(s) as well */
	gmap->mapFlags &= ~mfRowTabl;	/* now it's interleaved */

	for (myPlane = 0; myPlane < gmap->pixPlanes; myPlane++)
	{
		rowTablePtrPtr = (long *) gmap->mapTable[myPlane];
		for (myRow = 0; myRow < gmap->pixHeight; myRow++)
		{
			rowTablePtr = (long) GrafAlloc(gmap->pixBytes);
			if(rowTablePtr == NULL) return(c_OutofMem);

			#ifdef FIXUP386
			/* fixup the ds relative rowtable pointers to be dll
			relative */
			rowTablePtr += dllFixup;
			#endif

			*rowTablePtrPtr++ = rowTablePtr;
		}
	}
	return(0);
}


/* Function CloseBitmap deallocates memory assigned to the grafMap.  It
returns 0 if sucessful or a GrafError style error code (and posts an error)
if not. */
int CloseBitmap(grafMap *argCGMAP)
{
	int norows;	/* do not free rows flag */
	int retcode;	/* error return code */
	int myPlane;
	int myRaster;
	long *rowTablePtr;

	retcode = 0;	/* clear return code */
	if (CloseGrafDriver(argCGMAP))	/* release driver */
	{
		retcode = c_CloseBit + c_BadDevTech;
		nuGrafErr((short) retcode);
	}

	/* if one of our 'special' devices, do special close routine */
	switch (argCGMAP->devMode)
	{
	case cMEMORY:	/* close a memory grafMap */
		norows = 0;	/* this flag is used to skip the portion of the
					loop which frees raster lines, so that only
					rowtable arrays are freed */
		for (myPlane = 0; myPlane < argCGMAP->pixPlanes; myPlane++)
		{
			#ifdef FIXUP386
			/* un-fixup the dll relative maptable pointers */
			argCGMAP->mapTable[myPlane] -= dllFixup;
			#endif

			if (argCGMAP->mapTable[myPlane] == NULL) return(retcode);

			if (norows == 0)	/* check flag */
			{	/* free the rasters */
				rowTablePtr = *argCGMAP->mapTable[myPlane];
				for (myRaster = 0; myRaster < argCGMAP->pixHeight; myRaster++)
				{
					if (rowTablePtr == NULL)
					{
						norows = 1;
						break;
					}

					#ifdef FIXUP386
					rowTablePtr -= dllFixup;	/* unfix the pointer */
					#endif

					GrafFree(rowTablePtr);

					/* if it's a linear grafMap, then it was allocated in
					one big chunk */
					if (argCGMAP->mapFlags & mfRowTabl)
					{
						norows = 1;
						break;
					}

					rowTablePtr++;
				}
			}

			/* free this planes rowtable */
			GrafFree(argCGMAP->mapTable[myPlane]);
		}
		return(retcode);
	case cUSER:	/* user device, hands off rowtable */
		return(retcode);
	default:
		#ifdef FIXUP386
		/* un-fixup the dll relative maptable pointers */
		argCGMAP->mapTable[0] -= dllFixup;
		argCGMAP->mapTable[1] -= dllFixup;
		argCGMAP->mapTable[2] -= dllFixup;
		argCGMAP->mapTable[3] -= dllFixup;
		#endif

		/* free rowtable for each plane. */
		for (myPlane = 0; myPlane < argCGMAP->pixPlanes; myPlane++)
		{
			/* check for null, exit on any null pointer */
			if (argCGMAP->mapTable[myPlane] == NULL) return(retcode);
			GrafFree(argCGMAP->mapTable[myPlane]);

			/* check if using same rowtable for other planes */
			if (argCGMAP->mapTable[myPlane] ==
				argCGMAP->mapTable[myPlane + 1]) return(retcode);
		}
		return(retcode);
	}
}

⌨️ 快捷键说明

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