1 /* Copyright (C) 1992-1998 The Geometry Center
2  * Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips
3  *
4  * This file is part of Geomview.
5  *
6  * Geomview is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Geomview is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Geomview; see the file COPYING.  If not, write
18  * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19  * USA, or visit http://www.gnu.org.
20  */
21 
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 
26 #if 0
27 static char copyright[] = "Copyright (C) 1992-1998 The Geometry Center\n\
28 Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips";
29 #endif
30 
31 
32 
33 /* Author: Timothy Rowley */
34 
35 /* {{{ Includes */
36 
37 #include <stdio.h>
38 #include "mgP.h"
39 #include "mgx11P.h"
40 
41 /* }}} */
42 
43 /* {{{ dither and masking tables */
44 
45 static unsigned char dither[65][8] = {
46 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
47 	{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
48 	{0x80, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00},
49 	{0x80, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00},
50 	{0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00},
51 	{0x88, 0x00, 0x20, 0x00, 0x88, 0x00, 0x00, 0x00},
52 	{0x88, 0x00, 0x20, 0x00, 0x88, 0x00, 0x02, 0x00},
53 	{0x88, 0x00, 0x20, 0x00, 0x88, 0x00, 0x22, 0x00},
54 	{0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00},
55 	{0x88, 0x00, 0xA2, 0x00, 0x88, 0x00, 0x22, 0x00},
56 	{0x88, 0x00, 0xA2, 0x00, 0x88, 0x00, 0x2A, 0x00},
57 	{0x88, 0x00, 0xA2, 0x00, 0x88, 0x00, 0xAA, 0x00},
58 	{0x88, 0x00, 0xAA, 0x00, 0x88, 0x00, 0xAA, 0x00},
59 	{0xA8, 0x00, 0xAA, 0x00, 0x88, 0x00, 0xAA, 0x00},
60 	{0xA8, 0x00, 0xAA, 0x00, 0x8A, 0x00, 0xAA, 0x00},
61 	{0xA8, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00},
62 	{0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00},
63 	{0xAA, 0x40, 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00},
64 	{0xAA, 0x40, 0xAA, 0x00, 0xAA, 0x04, 0xAA, 0x00},
65 	{0xAA, 0x40, 0xAA, 0x00, 0xAA, 0x44, 0xAA, 0x00},
66 	{0xAA, 0x44, 0xAA, 0x00, 0xAA, 0x44, 0xAA, 0x00},
67 	{0xAA, 0x44, 0xAA, 0x10, 0xAA, 0x44, 0xAA, 0x00},
68 	{0xAA, 0x44, 0xAA, 0x10, 0xAA, 0x44, 0xAA, 0x01},
69 	{0xAA, 0x44, 0xAA, 0x10, 0xAA, 0x44, 0xAA, 0x11},
70 	{0xAA, 0x44, 0xAA, 0x11, 0xAA, 0x44, 0xAA, 0x11},
71 	{0xAA, 0x44, 0xAA, 0x51, 0xAA, 0x44, 0xAA, 0x11},
72 	{0xAA, 0x44, 0xAA, 0x51, 0xAA, 0x44, 0xAA, 0x15},
73 	{0xAA, 0x44, 0xAA, 0x51, 0xAA, 0x44, 0xAA, 0x55},
74 	{0xAA, 0x44, 0xAA, 0x55, 0xAA, 0x44, 0xAA, 0x55},
75 	{0xAA, 0x54, 0xAA, 0x55, 0xAA, 0x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96 	{0xEA, 0xFF, 0xAA, 0xFF, 0xAE, 0xFF, 0xAA, 0xFF},
97 	{0xEA, 0xFF, 0xAA, 0xFF, 0xEE, 0xFF, 0xAA, 0xFF},
98 	{0xEE, 0xFF, 0xAA, 0xFF, 0xEE, 0xFF, 0xAA, 0xFF},
99 	{0xEE, 0xFF, 0xBA, 0xFF, 0xEE, 0xFF, 0xAA, 0xFF},
100 	{0xEE, 0xFF, 0xBA, 0xFF, 0xEE, 0xFF, 0xAB, 0xFF},
101 	{0xEE, 0xFF, 0xBA, 0xFF, 0xEE, 0xFF, 0xBB, 0xFF},
102 	{0xEE, 0xFF, 0xBB, 0xFF, 0xEE, 0xFF, 0xBB, 0xFF},
103 	{0xEE, 0xFF, 0xFB, 0xFF, 0xEE, 0xFF, 0xBB, 0xFF},
104 	{0xEE, 0xFF, 0xFB, 0xFF, 0xEE, 0xFF, 0xBF, 0xFF},
105 	{0xEE, 0xFF, 0xFB, 0xFF, 0xEE, 0xFF, 0xFF, 0xFF},
106 	{0xEE, 0xFF, 0xFF, 0xFF, 0xEE, 0xFF, 0xFF, 0xFF},
107 	{0xFE, 0xFF, 0xFF, 0xFF, 0xEE, 0xFF, 0xFF, 0xFF},
108 	{0xFE, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF, 0xFF, 0xFF},
109 	{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
110 	{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
111 };
112 
113 static unsigned char bits[8] = {
114     0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
115 
116 #if 0
117 static unsigned char startBits[8] = {
118     0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01};
119 
120 static unsigned char endBits[8] = {
121     0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF};
122 #endif
123 
124 /* }}} */
125 
126 static endPoint *mug=NULL;
127 static int mugSize = 0;
128 static int flipped = 0;
129 
130 static int
RGB2gray(int * color)131 RGB2gray(int *color)
132 {
133     int n;
134     n = (64.0*(color[0]*0.299 + color[1]*0.587 + color[2]*0.114))/255.0;
135     if (n>64)
136 	return 64;
137     else
138 	return n;
139 }
140 
141 #if 0
142 static int
143 RGB2gray2(float r, float g, float b)
144 {
145     int n;
146     n = 64.0*(r*0.299 + g*0.587 + b*0.114);
147     if (n>64)
148 	return 64;
149     else
150 	return n;
151 }
152 #endif
153 
154 static inline void
setZpixel(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int * color)155 setZpixel(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
156 	  CPoint3 *p, int *color)
157 {
158     int x, y;
159     x = p->x;
160     y = p->y;
161     if (p->z < zbuf[y*zwidth+x])
162 	buf[y*width+(x>>3)] =
163 	    (buf[y*width+(x>>3)] & ~bits[x&0x07]) |
164 	    (bits[x&0x07] & dither[RGB2gray(color)][y&0x07]);
165 }
166 
167 #ifdef __GNUC__
168 inline
169 #endif
170 static void
setpixel(unsigned char * buf,int zwidth,int width,int height,CPoint3 * p,int * color)171 setpixel(unsigned char *buf, int zwidth, int width, int height,
172 	  CPoint3 *p, int *color)
173 {
174     int x, y;
175     x = p->x;
176     y = p->y;
177     buf[y*width+(x>>3)] =
178 	(buf[y*width+(x>>3)] & ~bits[x&0x07]) |
179 	(bits[x&0x07] & dither[RGB2gray(color)][y&0x07]);
180 }
181 
182 #ifdef __GNUC__
183 inline
184 #endif
185 static void
setPixel(unsigned char * buf,int zwidth,int width,int height,int x,int y,int color)186 setPixel(unsigned char *buf, int zwidth, int width, int height, int x, int y,
187 	 int color)
188 {
189     buf[y*width+(x>>3)] =
190 	(buf[y*width+(x>>3)] & ~bits[x&0x07]) |
191 	(bits[x&0x07] & dither[color][y&0x07]);
192 }
193 
194 void
Xmgr_1init(int blackPixel)195 Xmgr_1init(int blackPixel)
196 {
197     int col, i;
198 
199     if (blackPixel && (!flipped))
200     {
201 	for (col=0; col<65; col++)
202 	    for (i=0; i<8; i++)
203 		dither[col][i] = ~dither[col][i];
204 	flipped = 1;
205     }
206 }
207 
208 void
Xmgr_1clear(unsigned char * buf,float * zbuf,int zwidth,int width,int height,int * color,int flag,int fullclear,int xmin,int ymin,int xmax,int ymax)209 Xmgr_1clear(unsigned char *buf, float *zbuf, int zwidth,
210 	     int width, int height, int *color, int flag,
211 	    int fullclear, int xmin, int ymin, int xmax, int ymax)
212 {
213     int i, length, pos, x;
214     int col = RGB2gray(color);
215 
216     if (mug==NULL)
217     {
218 	mug = (endPoint *)malloc(sizeof(endPoint)*height);
219 	mugSize = height;
220     }
221     if (height>mugSize)
222     {
223 	mug = (endPoint *)realloc(mug, sizeof(endPoint)*height);
224 	mugSize = height;
225     }
226 
227     if (fullclear)
228     {
229         for (i=0; i<height; i++)
230             memset(buf+i*width, dither[col][i&0x07], width);
231 
232         if (flag)
233             for (i=0; i<zwidth*height; i++)
234                 zbuf[i] = 1.0;
235 	return;
236     }
237     xmin = MAX(xmin,0) >> 3;
238     length = (MIN(zwidth-1,xmax)-xmin+8) >> 3;
239     ymin = MAX(ymin,0);
240     ymax = MIN(height-1,ymax);
241 
242     for (i=ymin; i<=ymax; i++)
243 	memset(buf+i*width+xmin, dither[col][i&0x07], length);
244 
245     length = MIN(zwidth-1,xmax)-xmin+1;
246     if (flag)
247 	for (i=ymin; i<=ymax; i++)
248 	{
249 	    pos = i*zwidth+xmin;
250 	    for (x=0; x<length; x++)
251 	        zbuf[pos+x] = 1.0;
252 	}
253 }
254 
255 /* {{{ single lines */
256 
257 #define WIDENAME wideDline
258 #define NAME Xmgr_1Dline
259 #define WIDEYDOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
260 #define WIDEXDOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
261 #define DOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
262 #define VARIABLES int col=RGB2gray(color);
263 #include "MGRline.h"
264 
265 #define WIDENAME wideDZline
266 #define NAME Xmgr_1DZline
267 #define ZBUFFER
268 #define WIDEYDOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
269 #define WIDEXDOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
270 #define DOPIXEL setPixel(buf, zwidth, width, height, x, y, col);
271 #define VARIABLES int col=RGB2gray(color);
272 #include "MGRline.h"
273 
274 
275 #define WIDENAME wideDGZline
276 #define NAME Xmgr_1DGZline
277 #define ZBUFFER
278 #define GOURAUD
279 #define WIDEYDOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
280 #define WIDEXDOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
281 #define DOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
282 #include "MGRline.h"
283 
284 
285 #define WIDENAME wideDGline
286 #define NAME Xmgr_1DGline
287 #define GOURAUD
288 #define WIDEYDOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
289 #define WIDEXDOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
290 #define DOPIXEL setPixel(buf, zwidth, width, height, x, y, r);
291 #include "MGRline.h"
292 
293 /* }}} */
294 
295 /* {{{ polygon scan convers */
296 
297 #define NAME Xmgr_DdoLines
298 #define YCODE pattern = dith[y&0x07]; ptr=buf+y*width;
299 #define DOPIXEL	\
300     ptr[i>>3] = (ptr[i>>3] & ~bits[i&0x07]) | (bits[i&0x07] & pattern);
301 #define VARIABLES    unsigned char *dith = dither[RGB2gray(color)]; \
302                      unsigned char pattern; \
303 		     unsigned char *ptr;
304 #include "MGRdolines.h"
305 
306 #define NAME Xmgr_DZdoLines
307 #define ZBUFFER
308 #define YCODE pattern = dith[y&0x07]; ptr=buf+y*width;
309 #define DOPIXEL	\
310     ptr[i>>3] = (ptr[i>>3] & ~bits[i&0x07]) | (bits[i&0x07] & pattern);
311 #define VARIABLES    unsigned char *dith = dither[RGB2gray(color)]; \
312                      unsigned char pattern; \
313 		     unsigned char *ptr;
314 #include "MGRdolines.h"
315 
316 
317 #define NAME Xmgr_DGdoLines
318 #define GOURAUD
319 #define DOPIXEL	setPixel(buf, zwidth, width, height, i, y, r);
320 #include "MGRdolines.h"
321 
322 
323 #define NAME Xmgr_DGZdoLines
324 #define ZBUFFER
325 #define GOURAUD
326 #define DOPIXEL	setPixel(buf, zwidth, width, height, i, y, r);
327 #include "MGRdolines.h"
328 
329 
330 void
Xmgr_1Dpoly(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int * color)331 Xmgr_1Dpoly(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
332 	    CPoint3 *p, int n, int *color)
333 {
334     Xmgr_polyscan(buf, zbuf, zwidth, width, height, p, n, color, mug,
335 		  Xmgr_DdoLines);
336 }
337 
338 void
Xmgr_1DZpoly(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int * color)339 Xmgr_1DZpoly(unsigned char *buf, float *zbuf, int zwidth,
340 	     int width, int height, CPoint3 *p, int n, int *color)
341 {
342     Xmgr_Zpolyscan(buf, zbuf, zwidth, width, height, p, n, color, mug,
343 		  Xmgr_DZdoLines);
344 }
345 
346 void
Xmgr_1DGpoly(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int * color)347 Xmgr_1DGpoly(unsigned char *buf, float *zbuf, int zwidth,
348 	     int width, int height, CPoint3 *p, int n, int *color)
349 {
350     Xmgr_Graypolyscan(buf, zbuf, zwidth, width, height, p, n, color, mug,
351 		       Xmgr_DGdoLines);
352 }
353 
354 void
Xmgr_1DGZpoly(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int * color)355 Xmgr_1DGZpoly(unsigned char *buf, float *zbuf, int zwidth,
356 	     int width, int height, CPoint3 *p, int n, int *color)
357 {
358     Xmgr_GrayZpolyscan(buf, zbuf, zwidth, width, height, p, n, color, mug,
359 		       Xmgr_DGZdoLines);
360 }
361 
362 /* }}} */
363 
364 /* {{{ multi-line scan conversion */
365 
366 void
Xmgr_1Dpolyline(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int lwidth,int * color)367 Xmgr_1Dpolyline(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
368 		CPoint3 *p, int n, int lwidth, int *color)
369 {
370     int i;
371 
372     if (n == 1)
373     {
374 	setpixel(buf, zwidth, width, height, p, color);
375 	return;
376     }
377 
378     for (i=0; i<n-1; i++)
379 	if (p[i].drawnext)
380 	    Xmgr_1Dline(buf, zbuf, zwidth, width, height, p+i, p+i+1,
381 			 lwidth, color);
382 }
383 
384 
385 void
Xmgr_1DZpolyline(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int lwidth,int * color)386 Xmgr_1DZpolyline(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
387 		 CPoint3 *p, int n, int lwidth, int *color)
388 {
389     int i;
390 
391     if (n == 1)
392     {
393 	setZpixel(buf, zbuf, zwidth, width, height, p, color);
394 	return;
395     }
396 
397     for (i=0; i<n-1; i++)
398 	if (p[i].drawnext)
399 	    Xmgr_1DZline(buf, zbuf, zwidth, width, height, p+i, p+i+1,
400 			 lwidth, color);
401 }
402 
403 void
Xmgr_1DGpolyline(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int lwidth,int * color)404 Xmgr_1DGpolyline(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
405 		 CPoint3 *p, int n, int lwidth, int *color)
406 {
407     int i;
408 
409     if (n == 1)
410     {
411 	setpixel(buf, zwidth, width, height, p, color);
412 	return;
413     }
414 
415     for (i=0; i<n-1; i++)
416 	if (p[i].drawnext)
417 	    Xmgr_gradWrapper(buf, zbuf, zwidth, width, height, p+1, p+i+1,
418 			     lwidth, Xmgr_1Dline, Xmgr_1DGline);
419 }
420 
421 void
Xmgr_1DGZpolyline(unsigned char * buf,float * zbuf,int zwidth,int width,int height,CPoint3 * p,int n,int lwidth,int * color)422 Xmgr_1DGZpolyline(unsigned char *buf, float *zbuf, int zwidth, int width, int height,
423 		 CPoint3 *p, int n, int lwidth, int *color)
424 {
425     int i;
426 
427     if (n == 1)
428     {
429 	setZpixel(buf, zbuf, zwidth, width, height, p, color);
430 	return;
431     }
432 
433     for (i=0; i<n-1; i++)
434 	if (p[i].drawnext)
435 	    Xmgr_gradWrapper(buf, zbuf, zwidth, width, height, p+1, p+i+1,
436 			     lwidth, Xmgr_1DZline, Xmgr_1DGZline);
437 }
438 
439 /* }}} */
440 
441 /*
442 Local variables:
443 folded-file: t
444 End:
445 */
446 
447 
448