Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2013, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
/*
37
This file contains the routines needed in order to dither using the
38
spadcolors. The routines will have names such as XSpadFill, ... The user
39
simply gives the normal arguments as with the corresponding XFill routine,
40
with two additional arguments which choose the shade and the hue.
41
42
The file will maintain twoGC's: stippleGC - will be used when stippling the
43
backgrounds. solidGC - will be used when the background should be solid
44
45
The user should call XSpadInit to get everthing going. This routine has the
46
job of Initializing the dithering routines, and getting the colors all
47
into place.
48
49
*/
50
51
52
#include <stdio.h>
53
#include <stdlib.h>
54
55
#include <X11/Xlib.h>
56
#include <X11/Xutil.h>
57
#include <X11/Xos.h>
58
#include <X11/Intrinsic.h>
59
#include <X11/StringDefs.h>
60
61
#include "openaxiom-c-macros.h"
62
#include "spadcolors.h"
63
#include "XSpadFill.h"
64
#include "XShade.h"
65
#include "XDither.h"
66
67
extern unsigned long *spadColors;
68
static GC stippleGC, solidGC;
69
Colormap cmap;
70
int SpadFillInit = 0;
71
long white, black;
72
int max_spad_shades;
73
extern Display *dsply;
74
75
extern int totalHues;
76
extern int totalDithered;
77
extern int totalSolid;
78
extern int totalShades;
79
extern int totalColors;
80
extern int maxGreyShade;
81
82
int
83
XInitSpadFill(Display *dsply, int scr, Colormap * mapOfColors, int * hues,
84
int *solid, int * dithered, int * shades)
85
{
86
XColor BlackColor, WhiteColor;
87
XColor retColor;
88
int maxSolid;
89
90
SpadFillInit = 1;
91
92
93
/*
94
* First thing I should do is get the GC's
95
*/
96
stippleGC = XCreateGC(dsply, RootWindow(dsply, scr), 0, NULL);
97
solidGC = XCreateGC(dsply, RootWindow(dsply, scr), 0, NULL);
98
XSetArcMode(dsply, solidGC, ArcPieSlice);
99
XSetArcMode(dsply, stippleGC, ArcPieSlice);
100
101
102
cmap = DefaultColormap(dsply, scr);
103
*mapOfColors = cmap;
104
XAllocNamedColor(dsply, cmap, "Black", &BlackColor, &retColor);
105
XAllocNamedColor(dsply, cmap, "White", &WhiteColor, &retColor);
106
black = BlackColor.pixel;
107
white = WhiteColor.pixel;
108
109
/*
110
* Now I check to see if I am on a monochrome display. If so then I
111
* simply set totalHues to be one, and total Shades to be 2. I also have
112
* to allocate balck and white colors. This I put into the first two
113
* memory locations of spadcolors.
114
*
115
* was if(DisplayPlanes(dsply, scr) < 2) changed temporarily to < 8
116
* because of problems with screens with 4 planes . Now if we don't have
117
* 8 planes to play with we switch to monochrome
118
*/
119
120
if (DisplayPlanes(dsply, scr) < 8) {
121
*dithered = totalDithered = maxGreyShade = XInitShades(dsply, scr);
122
spadColors = (unsigned long *) malloc(2 * sizeof(unsigned long));
123
spadColors[0] = BlackColor.pixel;
124
spadColors[1] = WhiteColor.pixel;
125
*hues = totalHues = 1;
126
*solid = totalSolid = 2;
127
*shades = totalColors = totalShades = totalDithered;
128
return (totalColors);
129
}
130
131
/*
132
* Now I have to get all the spad colors as every good spad program
133
* should Now I should initialize the dithering routines
134
*/
135
136
*dithered = totalDithered =
137
XInitDither(dsply, scr, stippleGC, black, white);
138
139
if ((maxSolid = makeColors(dsply, scr, &cmap, &spadColors, &totalSolid)) > 0) {
140
*solid = totalSolid + 2;
141
*hues = totalHues = maxSolid / totalSolid;
142
*shades = totalShades = (totalSolid + 1) * (totalDithered - 1) + 1;
143
totalColors = totalHues * totalShades;
144
return (totalColors);
145
}
146
else {
147
148
/*
149
* makeColors managed to fail -- switch to mono
150
*/
151
*dithered = totalDithered = maxGreyShade = XInitShades(dsply, scr);
152
spadColors = (unsigned long *) malloc(2 * sizeof(unsigned long));
153
spadColors[0] = BlackColor.pixel;
154
spadColors[1] = WhiteColor.pixel;
155
*hues = totalHues = 1;
156
*solid = totalSolid = 2;
157
*shades = totalColors = totalShades = totalDithered;
158
return (totalColors);
159
}
160
}
161
162
163
void
164
XSpadFillSetArcMode(Display *dsply, int mode)
165
{
166
XSetArcMode(dsply, solidGC, mode);
167
XSetArcMode(dsply, stippleGC, mode);
168
}
169
170
GC
171
SpadFillGC(Display *dsply,int hue, int theshade, const char* fill_routine)
172
{
173
int dither;
174
int color;
175
176
177
if (!SpadFillInit) {
178
fprintf(stderr, "Tried to use SpadFillGC before calling XInitSpadFill\n");
179
exit(0);
180
}
181
182
if (theshade >= totalShades) {
183
fprintf(stderr, "Shade %d out of range\n",theshade);
184
exit(-1);
185
}
186
if (hue >= totalHues) {
187
fprintf(stderr, "Error Hue %d is out of range\n",hue);
188
exit(-1);
189
}
190
dither = ((theshade) % (totalDithered - 1));
191
if (dither != 0) {
192
XChangeDither(dsply, stippleGC, dither);
193
if (theshade < totalDithered) { /* Dither to black */
194
color = totalSolid * hue;
195
XSetForeground(dsply, stippleGC, black);
196
XSetBackground(dsply, stippleGC, spadColors[color]);
197
}
198
else if (theshade > (totalShades - totalDithered)) { /* Dither to white */
199
color = ((theshade) / (totalDithered - 1)) + totalSolid * hue - 1;
200
XSetForeground(dsply, stippleGC, spadColors[color]);
201
XSetBackground(dsply, stippleGC, white);
202
}
203
else {
204
color = ((theshade) / (totalDithered - 1)) + totalSolid * hue - 1;
205
XSetForeground(dsply, stippleGC, spadColors[color]);
206
XSetBackground(dsply, stippleGC, spadColors[color + 1]);
207
}
208
return (stippleGC);
209
}
210
else {
211
if (theshade == 0)
212
XSetForeground(dsply, solidGC, black);
213
else if (theshade == (totalShades - 1))
214
XSetForeground(dsply, solidGC, white);
215
else {
216
color = ((theshade) / (totalDithered - 1)) + totalSolid * hue - 1;
217
XSetForeground(dsply, solidGC, spadColors[color]);
218
}
219
return (solidGC);
220
}
221
222
}
223
224
unsigned long
225
XSolidColor(int hue, int theshade)
226
{
227
if (hue >= totalHues)
228
return -1;
229
if (theshade >= totalSolid)
230
return -1;
231
return (spadColors[hue * (totalSolid) + theshade]);
232
}
233
234
void
235
XSpadFillRectangle(Display *dsply, Drawable drawable, int x, int y,
236
unsigned int width, unsigned int height,
237
int hue, int theshade)
238
{
239
240
XFillRectangle(dsply, drawable,
241
SpadFillGC(dsply, hue, theshade, "XSpadFillRectangle"),
242
x, y, width, height);
243
244
}
245
246
247
void
248
XSpadFillRectangles(Display *dsply, Drawable drawable,
249
XRectangle * rectangles, int nrectangles,
250
int hue, int theshade)
251
{
252
253
254
XFillRectangles(dsply, drawable,
255
SpadFillGC(dsply, hue, theshade, "XSpadFillRectangle"),
256
rectangles, nrectangles);
257
258
}
259
260
261
void
262
XSpadFillPolygon(Display *dsply, Drawable drawable, XPoint * points,
263
int npoints, int shape, int mode, int hue, int theshade)
264
{
265
XFillPolygon(dsply, drawable,
266
SpadFillGC(dsply, hue, theshade, "XSpadFillRectangle"),
267
points, npoints, shape, mode);
268
269
}
270
271
void
272
XSpadFillArc(Display *dsply, Drawable drawable, int x, int y,
273
unsigned int width, unsigned int height,
274
int angle1, int angle2, int hue, int theshade)
275
{
276
277
XFillArc(dsply, drawable,
278
SpadFillGC(dsply, hue, theshade, "XSpadFillRectangle"),
279
x, y, width, height, angle1, angle2);
280
}
281
282
283
void
284
XSpadFillArcs(Display *dsply, Drawable drawable,XArc *arcs, int narcs,
285
int hue, int theshade)
286
{
287
XFillArcs(dsply, drawable,
288
SpadFillGC(dsply, hue, theshade, "XSpadFillArcs"),
289
arcs, narcs);
290
}
291
292
293
294
295