📄 bits5.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 */
/* */
/* BITS5.c 1.9 */
/* */
/* COMPONENT */
/* */
/* All */
/* */
/* DESCRIPTION */
/* */
/* This file contains the XLATEIMAGE function. */
/* */
/* AUTHOR */
/* */
/* Robert G. Burrill, Accelerated Technology, Inc. */
/* */
/* DATA STRUCTURES */
/* */
/* None */
/* */
/* FUNCTIONS */
/* */
/* None */
/* */
/* DEPENDENCIES */
/* */
/* None */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* BobB 5/5/98 Corrected to get rid of compiler */
/* warnings */
/* BobB 7/23/98 Corrected mask in SetImPix4 */
/* BobB 12/18/98 Corrected byte ordering */
/* BobB 12/21/98 Corrected 3 byte addressing */
/* */
/*************************************************************************/
#include "meta_wnd.h"
#include "metconst.h" /* MetaWINDOW Constant & Stucture Definitions */
#include "metports.h" /* MetaWINDOW Port & Bitmap Definitions */
#include "grafdata.h"
#include "metmacs3.h"
/* Function XLATEIMAGE translates an image in one format to another.
srcImage:
Source image buffer.
dstImage:
Destination image buffer. If this pointer is null, XlateImage will
only return the size needed for the destination buffer.
dstBits:
Destination bits per pixel.
dstPlanes:
Destination planes per pixel.
xlTable:
color translation table.
Case A: The source is 256 colors or less
Translate color number to target format, using
the source color as the index into the table.
There must be an entry for each source image color value.
Case B: The source has more than 256 colors:
We assume that the color number is made up of 3 color components
(red, green, blue). We then translate each pixel number to the
destination image by:
if the destination is > 256 colors
a) extract each color component.
b) adjust the component value by adding or deleting lsb's.
c) recombine the components to form a new color number.
The xlTable is not used.
if the destination is <= 256 colors
a) extract each color component
b) adjust the component value to 3 bits by deleting lsb's
c) recombine the components to form a 9 bit color number.
d) lookup color number in 512 element xlTable.
If xlTable is null, the color value is passed thru unchanged
RETURNS:
If dstImage is null, returns size needed for dstImage;
else
0 == success
-1 == failure [error found in QueryError()]
NOTES:
1) Currently limited to the following formats
a) 8, 16 bits/pixel, one plane
b) 1 bit/pixel, N planes
c) 8 bit 3 plane (source only)
2) Currently limited to 64k buffers, max for non 386
3) The resulting destination image will always be fully
left aligned, regardless of source alignment.
Method for calculation of destination buffer size is equivalent to
ImageSize. See YORE3.ASM for details. */
/* Variables used by more than one function in this file */
int curX; /* pixel XY */
int curY;
long tmpColor; /* temp color */
int imgFlgs; /* copy of image flags */
int flags; /* copy of grafmap flags */
long *tmpXlPtr; /* temp xlate table ptr */
image *tmpSrcImg; /* pointer to source image */
image *tmpDstImg; /* pointer to source image */
int srcOffset; /* precalculated offsets */
int dstOffset;
long planeSize; /* size of a plane */
long planeTotal; /* size of all planes */
long dstplaneSize; /* size of a destination plane */
long XlateImage( image *argSrcImg, image *argDstImg, int argDstBits, int argDstPlns,
long *argXlPtr )
{
void (*srcPtr)(void); /* code fragment offsets */
void (*dstPtr)(void);
void (*xlatPtr)(void);
/* Some functions used by XLATEIMAGE */
/* calculate destination bytes per row - bits = width * bits/pixel rounded
up to next byte and rounded up to even number of bytes */
#define calcRowByte (planeSize = \
(((((argDstBits * argSrcImg->imWidth + 7) >> 3) + 1) >> 1) << 1))
void GetImPix1(void); /* 1 bit GetImPix */
void GetImPix2(void); /* 2 bit GetImPix */
void GetImPix4(void); /* 4 bit GetImPix */
void GetImPix8(void); /* 8 bit GetImPix */
void GetImPix16(void); /* 16 bit GetImPix */
void GetImPix24(void); /* 24 bit GetImPix */
void GetImPix83(void); /* 8 bit/3 plane GetImPix */
void SetImPix1(void); /* 1 bit SetImPix */
void SetImPix2(void); /* 2 bit SetImPix */
void SetImPix4(void); /* 4 bit SetImPix */
void SetImPix8(void); /* 8 bit SetImPix */
void SetImPix16(void); /* 16 bit SetImPix */
void SetImPix24(void); /* 24 bit SetImPix */
void lowcolor_any(void); /* <= 256 color source */
void RGB16_lowcolor(void); /* 16 bpp source, <= 256 color dest */
void RGB16_RGB24(void); /* 16 bpp source, 24 bit dest */
void RGB24_lowcolor(void); /* 24 bpp source, <= 256 color dest */
void RGB24_RGB16(void); /* 24 bpp source, highcolor dest */
void XL_NOP(void); /* don't translate */
/* get a copy of the default grafmap flags for determining hicolor dest
image format (there is no other way to guess what the destination format
for hicolor should be) */
flags = defGrafMap.mapFlags;
/* test dest image pointer for null */
if (argDstImg == 0)
{ /* yes, so just calculate size required for dest image */
calcRowByte;
/* multiply times height for total per plane (long result in ae)
and add size of imageHdr */
return (planeSize * argSrcImg->imHeight * argDstPlns +
sizeof(imageHdr));
}
/* This routine consists of a loop which calls three subroutines indirectly.
The subroutines are:
1) get a source image pixel value
2) translate the color value
3) put a dest image pixel value
The indirect vectors for these subroutines are loaded depending on the
method which is appropriate for the image formats */
/* set up the destination image header */
argDstImg->imWidth = argSrcImg->imWidth;
argDstImg->imHeight = argSrcImg->imHeight;
argDstImg->imAlign = 0;
argDstImg->imFlags = 0; /* default to less than 64k */
calcRowByte; /* calculate destination bytes per row */
/* BobB 5/5/98 - corrected the following line to get rid of a compiler warning
argDstImg->imBytes = planeSize; */
argDstImg->imBytes = (short) planeSize;
argDstImg->imBits = argDstBits;
argDstImg->imPlanes = argDstPlns;
/* =============== Set up 'get' method ============================== */
switch (argSrcImg->imBits)
{
case 1:
srcPtr = GetImPix1; /* 1 bit GetImPix */
break;
case 2:
srcPtr = GetImPix2; /* 2 bit GetImPix */
break;
case 4:
srcPtr = GetImPix4; /* 4 bit GetImPix */
break;
case 8:
/* check for special case 8 bit 3 plane 24 bit PCX file image format */
if (argSrcImg->imPlanes == 3) srcPtr = GetImPix83;
else srcPtr = GetImPix8; /* 8 bit GetImPix */
break;
case 16:
srcPtr = GetImPix16; /* 16 bit GetImPix */
break;
case 24:
srcPtr = GetImPix24; /* 24 bit GetImPix */
break;
default:
planeSize = c_XlateIma + c_InvDevFunc;
nuGrafErr((short) planeSize); /* report error */
return -1;
}
/* =============== Set up 'put' method ============================== */
switch (argDstBits)
{
case 1:
dstPtr = SetImPix1; /* 1 bit SetImPix */
break;
case 2:
dstPtr = SetImPix2; /* 2 bit SetImPix */
break;
case 4:
dstPtr = SetImPix4; /* 4 bit SetImPix */
break;
case 8:
dstPtr = SetImPix8; /* 8 bit SetImPix */
break;
case 16:
dstPtr = SetImPix16; /* 16 bit SetImPix */
break;
case 24:
dstPtr = SetImPix24; /* 24 bit SetImPix */
break;
default:
planeSize = c_XlateIma + c_InvDevFunc;
nuGrafErr((short) planeSize); /* report error */
return -1;
}
/* ================= Set up translation method ====================== */
planeTotal = (((argSrcImg->imBits * argSrcImg->imPlanes)
>> 3) << 2) + (argDstBits >>3);
switch (planeTotal)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
xlatPtr = lowcolor_any; /* <= 256 color source */
break;
case 8:
case 9:
xlatPtr = RGB16_lowcolor; /* 16 bpp source, <= 256 color dest */
break;
case 10:
xlatPtr = XL_NOP; /* 16 bpp source, highcolor dest */
break;
case 11:
xlatPtr = RGB16_RGB24; /* 16 bpp source, 24 bit dest */
break;
case 12:
case 13:
xlatPtr = RGB24_lowcolor; /* 24 bpp source, <= 256 color dest */
break;
case 14:
xlatPtr = RGB24_RGB16; /* 24 bpp source, highcolor dest */
break;
default:
xlatPtr = XL_NOP; /* 24 bpp source, 24 bit dest */
}
if ((argXlPtr == NULL) && ((xlatPtr != RGB16_RGB24) ||
(xlatPtr != RGB24_RGB16))) xlatPtr = XL_NOP; /* don't do xlate */
/* ============== actual translation loop ============================= */
/* pre-calculate size of a plane */
planeSize = argSrcImg->imBytes * argSrcImg->imHeight;
planeTotal = planeSize * (argSrcImg->imPlanes - 1);
dstplaneSize = argDstImg->imBytes * argDstImg->imHeight;
/* Initialize offsets
Since the Y pixel coordinate value is actually the raster line, it is
calculated once (per raster line) into these two values */
srcOffset = 0;
dstOffset = 0;
tmpXlPtr = argXlPtr; /* xlate table pointer */
imgFlgs = argSrcImg->imFlags;
tmpSrcImg = argSrcImg;
tmpDstImg = argDstImg;
/* Ok, now begin at the top left, and step through the image raster by
raster curY[br], curX[br] are the PIXEL coordinates we want. Each
routine will convert the X offset to an appropriate bit/byte offset. */
for (curY = 0; curY < argSrcImg->imHeight; curY++)
{
for (curX = 0; curX < argSrcImg->imWidth; curX++)
{
srcPtr(); /* get src color */
xlatPtr(); /* xlate into new color */
dstPtr(); /* set dest color */
}
srcOffset += argSrcImg->imBytes;
dstOffset += argDstImg->imBytes;
}
return 0;
}
/* Some functions used by XLATEIMAGE */
/* -------------------- Translate image routines -------------------------- */
void lowcolor_any() /* <= 256 color source */
{
/* expects color between 0 and 255 and returns the (long) table value */
int colorIndex;
colorIndex = tmpColor & 0xff; /* insurance */
tmpColor = tmpXlPtr[colorIndex]; /* fetch long from table set */
return;
}
void RGB16_lowcolor() /* RGB16 source,
<= 256 color dest */
{
/* takes the 3 msbs of the 5 bits of RG&B to make a 9 bit index to xlate
table which must be 512 entries long */
int colorIndex;
tmpColor = (tmpColor >> 2); /* initial shift for all colors */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -