📄 graphics.c
字号:
//===========================================================================
// graphics.c - Graphics library
// (Tabs set to every 4 spaces)
//---------------------------------------------------------------------------
// Copyright (c) 2002, 2003 Epson Research and Development, Inc.
// All Rights Reserved.
//===========================================================================
#include "hal.h"
#include "graphics.h"
/*-------------------------------------------------------------------------*/
static const char Revision[] = "graphics.c=$Revision: 3 $";
/*-------------------------------------------------------------------------*/
static UInt32 stride = 0;
static UInt32 addr = 0;
static int BitsPerPixel = 8;
static int UseIndirectMode = FALSE;
//---------------------------------------------------------------------------
// FUNCTION: InitGraphics()
//
// DESCRIPTION:
// This routine initializes the graphics library.
//
// PARAMETERS:
// gStride - number of bytes per line
// gAddr - pointer to the start of the image in display memory
// (for example, *((UInt8 *) addr)=0 sets the first byte
// of the image to 0)
// gBitsPerPixel - bits per pixel
// gIndirect - TRUE if indirect mode used
//
// RETURNS:
// Nothing.
//
// MODIFIES:
// Nothing.
//---------------------------------------------------------------------------
void InitGraphics(UInt32 gStride, UInt32 gAddr, int gBitsPerPixel, int gIndirect)
{
stride = gStride;
addr = gAddr;
BitsPerPixel = gBitsPerPixel;
UseIndirectMode = gIndirect;
}
/*-------------------------------------------------------------------------*/
static void _Swap(Int32 *pa, Int32 *pb)
{
Int32 t;
t = *pa;
*pa = *pb;
*pb = t;
}
/*-------------------------------------------------------------------------*/
/*
** _Line() should be used only to draw diagonal lines
*/
static void _Line(Int32 x1, Int32 y1, Int32 x2, Int32 y2, UInt32 color, void (*pSetPixel)(Int32 x, Int32 y, UInt32 color))
{
Int32 d, dx, dy;
Int32 Aincr, Bincr, xincr, yincr;
Int32 x, y;
dx = x2 - x1;
if (dx < 0)
dx = -dx;
dy = y2 - y1;
if (dy < 0)
dy = -dy;
if (dx > dy)
{
if (x1 > x2)
{
_Swap(&x1, &x2);
_Swap(&y1, &y2);
}
if (y2 > y1)
yincr = 1;
else
yincr = -1;
dx = x2 - x1;
dy = y2 - y1; /* dy = abs(y2 - y1); */
if (dy < 0)
dy = -dy;
d = 2 * dy - dx;
Aincr = 2 * (dy - dx);
Bincr = 2 * dy;
x = x1;
y = y1;
(*pSetPixel)(x, y, color);
for (x = x1 + 1; x < x2; ++x)
{
if (d >= 0)
{
y += yincr;
d += Aincr;
}
else
d += Bincr;
(*pSetPixel)(x, y, color);
}
}
else
{
if (y1 > y2)
{
_Swap(&x1, &x2);
_Swap(&y1, &y2);
}
if (x2 > x1)
xincr = 1;
else
xincr = -1;
dy = y2 - y1;
dx = x2 - x1; /* dx = abs(x2 - x1); */
if (dx < 0)
dx = -dx;
d = 2 * dx - dy;
Aincr = 2 * (dx - dy);
Bincr = 2 * dx;
x = x1;
y = y1;
(*pSetPixel)(x, y, color);
for (y = y1 + 1; y < y2; ++y)
{
if (d >= 0)
{
x += xincr;
d += Aincr;
}
else
d += Bincr;
(*pSetPixel)(x, y, color);
}
}
}
/*-------------------------------------------------------------------------*/
static void _Pixel1bpp(Int32 x, Int32 y, UInt32 color32)
{
UInt32 left;
UInt8 color1 = (UInt8) (color32 & 0x01);
pvUInt8 bpt;
UInt32 offset;
UInt8 val;
color1 |= (color1 << 1);
color1 |= (color1 << 2);
color1 |= (color1 << 4);
left = 0x80 >> (x & 0x07);
if (UseIndirectMode)
{
offset = (y * stride) + (x / 8);
val = halReadDisplay8(offset);
val &= ~left;
val |= (color1 & left);
halWriteDisplay8(offset, val, 1);
}
else
{
bpt = (pvUInt8) (addr + (y * stride) + (x / 8));
*bpt &= ~left;
*bpt |= (color1 & left);
}
}
/*-------------------------------------------------------------------------*/
//
// _Line1bpp()
//
// For horizontal/vertical lines only
// Does not include the last point in the line (x2, y2)
//
static void _Line1bpp(Int32 x1, Int32 y1, Int32 x2, Int32 y2, UInt32 color32)
{
Int32 y;
UInt8 color1 = (UInt8) (color32 & 0x01);
pvUInt8 bpt = 0;
UInt32 offset = 0;
UInt8 val;
if (UseIndirectMode)
offset = (y1 * stride) + (x1 / 8);
else
bpt = (pvUInt8) (addr + (y1 * stride) + (x1 / 8));
if (y1 == y2) /* horiz. line */
{
UInt32 left,right;
UInt32 bytes;
color1 |= (color1 << 1);
color1 |= (color1 << 2);
color1 |= (color1 << 4);
// don't include endpoint
if (x2 > 0)
--x2;
left = 0xFF >> (x1 & 0x07);
bytes = x2/8 - x1/8 - 1;
right = 0xFFFF >> ((x2+1) & 0x07);
if (bytes < 0) /* one byte only */
{
right = 0xFFC0 >> (x1 & 0x07);
left &= right;
if (UseIndirectMode)
{
val = halReadDisplay8(offset);
val &= ~left;
val |= (color1 & left);
halWriteDisplay8(offset, val, 1);
}
else
{
*bpt &= ~left;
*bpt |= (color1 & left);
}
return;
}
if (UseIndirectMode)
{
val = halReadDisplay8(offset);
val &= ~left;
val |= (color1 & left);
halWriteDisplay8(offset, val, 1);
offset++;
//halWriteDisplayAddress(offset);
while (bytes-- > 0)
{
*pIndirectDataWrite = (UInt8) color1;
++offset;
}
val = halReadDisplay8(offset);
val &= ~right;
val |= (color1 & right);
halWriteDisplay8(offset, val, 1);
}
else
{
*bpt &= ~left;
*bpt |= (color1 & left);
bpt++;
while (bytes-- > 0)
*bpt++ = (UInt8) color1;
*bpt &= ~right;
*bpt |= (color1 & right);
}
return;
}
if (x1 == x2) /* vert. line */
{
UInt8 BitPosition = (UInt8) (7 - (x1 & 0x07));
UInt8 mask = (UInt8) (0x01 << BitPosition);
color1 <<= BitPosition;
if (UseIndirectMode)
{
for (y = y1; y < y2; y++, offset += stride)
{
val = halReadDisplay8(offset);
val &= ~mask;
val |= color1;
halWriteDisplay8(offset, val, 1);
}
}
else
{
for (y = y1; y < y2; y++, bpt += stride)
{
*bpt &= ~mask;
*bpt |= color1;
}
}
return;
}
}
/*-------------------------------------------------------------------------*/
static void _Pixel2bpp(Int32 x, Int32 y, UInt32 color32)
{
unsigned left, right;
UInt8 color2 = (UInt8) (color32 & 0x03);
pvUInt8 bpt;
UInt32 offset;
UInt8 val;
color2 |= (color2 << 2);
color2 |= (color2 << 4);
left = 0xFF >> ((x & 0x3)*2);
right = 0xFFC0 >> ((x & 0x3)*2);
left &= right;
if (UseIndirectMode)
{
offset = (y * stride) + (x / 4);
val = halReadDisplay8(offset);
val &= ~left;
val |= (color2 & left);
halWriteDisplay8(offset, val, 1);
}
else
{
bpt = (pvUInt8) (addr + (y * stride) + (x / 4));
*bpt &= ~left;
*bpt |= (color2 & left);
}
}
/*-------------------------------------------------------------------------*/
//
// _Line2bpp()
//
// For horizontal/vertical lines only
// Does not include the last point in the line (x2, y2)
//
static void _Line2bpp(Int32 x1, Int32 y1, Int32 x2, Int32 y2, UInt32 color32)
{
Int32 y;
UInt8 color2 = (UInt8) (color32 & 0x03);
pvUInt8 bpt = 0;
UInt32 offset = 0;
UInt8 val;
if (UseIndirectMode)
offset = (y1 * stride) + (x1 / 4);
else
bpt = (pvUInt8) (addr + (y1 * stride) + (x1 / 4));
if (y1 == y2) /* horiz. line */
{
unsigned left,right;
Int32 bytes;
color2 |= (color2 << 2);
color2 |= (color2 << 4);
// don't include endpoint
if (x2 > 0)
--x2;
left = 0xFF >> ((x1 & 0x3)*2);
bytes = x2/4 - x1/4 - 1;
right = 0xFFFF >> (((x2+1) & 0x3)*2);
if (bytes < 0) /* one byte only */
{
right = 0xFFC0 >> ((x1 & 0x3) * 2);
left &= right;
if (UseIndirectMode)
{
val = halReadDisplay8(offset);
val &= ~left;
val |= (color2 & left);
halWriteDisplay8(offset, val, 1);
}
else
{
*bpt &= ~left;
*bpt |= (color2 & left);
}
return;
}
if (UseIndirectMode)
{
val = halReadDisplay8(offset);
val &= ~left;
val |= (color2 & left);
halWriteDisplay8(offset, val, 1);
offset++;
//halWriteDisplay8Address(offset);
while (bytes-- > 0)
{
*pIndirectDataWrite = (UInt8) color2;
++offset;
}
val = halReadDisplay8(offset);
val &= ~right;
val |= (color2 & right);
halWriteDisplay8(offset, val, 1);
}
else
{
*bpt &= ~left;
*bpt |= (color2 & left);
bpt++;
while (bytes-- > 0)
*bpt++ = (UInt8) color2;
*bpt &= ~right;
*bpt |= (color2 & right);
}
return;
}
if (x1 == x2) /* vert. line */
{
UInt8 BitPosition = (UInt8) ((3 - (x1 & 0x3)) <<1);
UInt8 mask = (UInt8) (0x3 << BitPosition);
color2 <<= BitPosition;
if (UseIndirectMode)
{
for (y = y1; y < y2; y++, offset += stride)
{
val = halReadDisplay8(offset);
val &= ~mask;
val |= color2;
halWriteDisplay8(offset, val, 1);
}
}
else
{
for (y = y1; y < y2; y++, bpt += stride)
{
*bpt &= ~mask;
*bpt |= color2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -