forked from danieloneill/SeikoSLPLinuxDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriverUtils.cxx
155 lines (127 loc) · 3.6 KB
/
DriverUtils.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* "$Id: DriverUtils.cxx,v 1.6 Selznick$"
*
* This file contains uncompressed bitmap manipulation commands.
*
* Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* property of Easy Software Products and are protected by Federal
* copyright law. Distribution and use rights are outlined in the file
* "LICENSE.txt" which should have been included with this file. If this
* file is missing or damaged please contact Easy Software Products
* at:
*
* Attn: CUPS Licensing Information
* Easy Software Products
* 44141 Airport View Drive, Suite 204
* Hollywood, Maryland 20636-3142 USA
*
* Voice: (301) 373-9600
* EMail: [email protected]
* WWW: http://www.cups.org
*/
#include "stdlib.h"
#include "string.h"
#include "DriverUtils.h"
/* Get pointer to zero index inLine from bitMap. */
char *BitMapGetLinePtr(BitMap &inBitMap, int inLine)
{
if (inLine >= inBitMap.bounds.bottom - inBitMap.bounds.top)
{
return NULL;
}
if (inLine < 0)
{
return NULL;
}
char *lineBase = inBitMap.baseAddr + (inLine * inBitMap.rowBytes);
return lineBase;
}
bool BitMapCreate(BitMap &outBitMap, char *inBaseAddress, SInt32 inRowBytes, Rect &inBounds)
{
outBitMap = kEmptyBitMap;
SInt32 size = inRowBytes * (inBounds.bottom - inBounds.top);
outBitMap.baseAddr = (Ptr) ::malloc(size);
if (NULL == outBitMap.baseAddr)
{
return false;
}
outBitMap.bounds = inBounds;
outBitMap.rowBytes = inRowBytes;
/* Now copy the buffer */
if (inBaseAddress)
{
::memcpy(outBitMap.baseAddr, inBaseAddress, size);
}
return true;
}
void BitMapDispose(BitMap &ioBitMap)
{
if (NULL != ioBitMap.baseAddr)
{
::free(ioBitMap.baseAddr);
}
ioBitMap = kEmptyBitMap;
}
/* Rotate the source bitmap into the destination bitmap. */
/* src and dest may refer to the same structure. */
bool BitMapRotate90(BitMap &srcBitMap, BitMap &outDestBitMap)
{
UInt16 srcCols = srcBitMap.bounds.right - srcBitMap.bounds.left;
UInt16 srcRows = srcBitMap.bounds.bottom - srcBitMap.bounds.top;
UInt16 destCols = srcRows;
UInt16 destRows = srcCols;
UInt16 destRowBytes = (destCols + 7) >> 3;
/* Each bit in one source row will turn into a whole row in the destination. */
UInt32 destBuffSize = destRows * destRowBytes;
Ptr destBase = (Ptr) ::calloc(destBuffSize, 1);
if (NULL == destBase)
{
return false;
}
BitMap destBitMap = kEmptyBitMap;
destBitMap.baseAddr = destBase;
destBitMap.bounds.left = 0;
destBitMap.bounds.top = 0;
destBitMap.bounds.right = destCols;
destBitMap.bounds.bottom = destRows;
destBitMap.rowBytes = destRowBytes;
for (short srcRow = 0; srcRow < srcRows; srcRow++)
{
for (short srcCol = 0; srcCol < srcCols; srcCol++)
{
/* Get src value */
UInt8 value = 0;
{
SInt32 srcCharIndex = srcRow * srcBitMap.rowBytes;
srcCharIndex += (srcCol >> 3);
UInt8 mask = 0x80 >> (srcCol & 0x07);
value = srcBitMap.baseAddr[srcCharIndex] & mask;
}
if (value)
{
/* Backwards from end of row */
SInt32 destCol = (srcRows - srcRow) - 1;
SInt32 destRow = srcCol;
SInt32 destCharIndex = destRow * destBitMap.rowBytes;
destCharIndex += (destCol >> 3);
UInt8 mask = 0x80 >> (destCol & 0x07);
/* How should we handle bounds error? */
if ((destCharIndex >= 0) and (destCharIndex < destBuffSize))
{
destBitMap.baseAddr[destCharIndex] |= mask;
}
}
}
}
if (&srcBitMap == &outDestBitMap)
{
BitMapDispose(outDestBitMap);
}
outDestBitMap = destBitMap;
return true;
}
/*
* End of "$Id: DriverUtils.cxx,v 1.6 Selznick$"
*/