home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xmandel / part01 next >
Encoding:
Text File  |  1989-04-01  |  19.3 KB  |  1,012 lines

  1. Path: uunet!island!argv
  2. From: argv@island.uu.net (Dan Heller)
  3. Newsgroups: comp.sources.x
  4. Subject: v03i063:  xmandel -- mandelbrot program for color suns, Part01/01
  5. Message-ID: <665@island.uu.net>
  6. Date: 31 Mar 89 21:52:55 GMT
  7. Organization: Island Graphics, Marin County, California
  8. Lines: 1001
  9. Approved: island!argv@sun.com
  10.  
  11. Submitted-by: Arthur Smith <arthur@helios.tn.cornell.edu>
  12. Posting-number: Volume 3, Issue 63
  13. Archive-name: xmandel/part01
  14.  
  15. [ This runs on color suns -only-  The makefile is very small and I didn't
  16.   see a need for an Imake file.  It is rather slow and it will almost
  17.   certainly eat up your cpu.  I compiled and ran this on my sun3/60 with 8M.
  18.   I had "to make xmandel68881" because the default wouldn't work for me
  19.   but your milage may vary.  The README says "sit and wait" and it isn't
  20.   kidding.  The mandelbrot image overwrites your background on the root
  21.   window it seems.  I ran it as: "xmandel -display unix:0.1" --argv]
  22.  
  23. # This is a shell archive.  Remove anything before this line,
  24. # then unpack it by saving it in a file and typing "sh file".
  25. #
  26. # Wrapped by helios!arthur on Sun Mar 26 15:27:09 EST 1989
  27. # Contents:  Makefile README colors.h graphics.c graphics.h wrt.c xmandel.c
  28.  
  29. echo x - Makefile
  30. sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
  31. CFLAGS = -O -ffpa
  32. wrt.o: colors.h
  33. xmandel: xmandel.o graphics.o wrt.o
  34.     cc $(CFLAGS) xmandel.o graphics.o wrt.o -o xmandel -lX11 -lm
  35. xmandel6.o: xmandel.c; cc -O -f68881 xmandel.c -c -o xmandel6.o
  36. graphics6.o: graphics.c; cc -O -f68881 graphics.c -c -o graphics6.o
  37. wrt6.o: wrt.c colors.h; cc -O -f68881 wrt.c -c -o wrt6.o
  38. xmandel68881: xmandel6.o graphics6.o wrt6.o
  39.     cc -O -f68881 xmandel6.o graphics6.o wrt6.o -o xmandel -lX11 -lm
  40. @//E*O*F Makefile//
  41. chmod u=rw,g=r,o=r Makefile
  42.  
  43. echo x - README
  44. sed 's/^@//' > "README" <<'@//E*O*F README//'
  45. xmandel - A mandelbrot program for color suns, running under X11.
  46.  
  47.     This is a crude program which I am submitting simply because
  48.     it actually works (under Release 2 at least). It generates
  49.     the Mandelbrot set at desired coordinates, on the display
  50.     you want, and with the resolution scale you want, and then
  51.     the location and magnification can be interactively
  52.     changed by using the mouse and the "Alternate" key.
  53.  
  54.     ALT-left button causes the scale to expand by a factor of 5,
  55.     ALT-right button reduces by the same factor,
  56.     ALT-middle causes the screen to shift to be centered on the
  57.         point where the button is pressed.
  58.  
  59.     If you just let the thing sit and wait, it
  60.     gradually produces a higher resolution picture, eventually
  61.     reaching the maximum resolution possible on the screen.
  62.     There are various defaults set, and maybe some other things
  63.     that people will want to change.
  64.  
  65.     The graphics stuff is really quite general: for example somebody
  66.     here just changed the function passed to produce Julia sets
  67.     instead of the Mandelbrot set. So I hope other people find this
  68.     useful!
  69.  
  70.             Arthur Smith
  71.             (arthur@helios.tn.cornell.edu)
  72. @//E*O*F README//
  73. chmod u=rw,g=r,o=r README
  74.  
  75. echo x - colors.h
  76. sed 's/^@//' > "colors.h" <<'@//E*O*F colors.h//'
  77. unsigned char colors[] = {
  78.     255, 255, 255,
  79.     0, 0, 0,
  80.     216, 216, 191,
  81.     84, 84, 84,
  82.     0, 255, 127,
  83.     255, 0, 255,
  84.     255, 0, 0,
  85.     79, 47, 79,
  86.     35, 142, 35,
  87.     35, 107, 142,
  88.     255, 127, 0,
  89.     79, 47, 47,
  90.     204, 50, 50,
  91.     112, 219, 147,
  92.     192, 32, 0,
  93.     224, 32, 0,
  94.     0, 64, 0,
  95.     32, 64, 0,
  96.     64, 64, 0,
  97.     96, 64, 0,
  98.     128, 64, 0,
  99.     160, 64, 0,
  100.     192, 64, 0,
  101.     224, 64, 0,
  102.     0, 96, 0,
  103.     32, 96, 0,
  104.     64, 96, 0,
  105.     96, 96, 0,
  106.     128, 96, 0,
  107.     160, 96, 0,
  108.     192, 96, 0,
  109.     224, 96, 0,
  110.     0, 128, 0,
  111.     32, 128, 0,
  112.     64, 128, 0,
  113.     96, 128, 0,
  114.     128, 128, 0,
  115.     160, 128, 0,
  116.     192, 128, 0,
  117.     224, 128, 0,
  118.     0, 160, 0,
  119.     32, 160, 0,
  120.     64, 160, 0,
  121.     96, 160, 0,
  122.     128, 160, 0,
  123.     160, 160, 0,
  124.     192, 160, 0,
  125.     224, 160, 0,
  126.     0, 192, 0,
  127.     32, 192, 0,
  128.     64, 192, 0,
  129.     96, 192, 0,
  130.     128, 192, 0,
  131.     160, 192, 0,
  132.     192, 192, 0,
  133.     224, 192, 0,
  134.     0, 224, 0,
  135.     32, 224, 0,
  136.     64, 224, 0,
  137.     96, 224, 0,
  138.     128, 224, 0,
  139.     160, 224, 0,
  140.     192, 224, 0,
  141.     224, 224, 0,
  142.     0, 0, 64,
  143.     32, 0, 64,
  144.     64, 0, 64,
  145.     96, 0, 64,
  146.     128, 0, 64,
  147.     160, 0, 64,
  148.     192, 0, 64,
  149.     224, 0, 64,
  150.     0, 32, 64,
  151.     32, 32, 64,
  152.     64, 32, 64,
  153.     96, 32, 64,
  154.     128, 32, 64,
  155.     160, 32, 64,
  156.     192, 32, 64,
  157.     224, 32, 64,
  158.     0, 64, 64,
  159.     32, 64, 64,
  160.     64, 64, 64,
  161.     96, 64, 64,
  162.     128, 64, 64,
  163.     160, 64, 64,
  164.     192, 64, 64,
  165.     224, 64, 64,
  166.     0, 96, 64,
  167.     32, 96, 64,
  168.     64, 96, 64,
  169.     96, 96, 64,
  170.     128, 96, 64,
  171.     160, 96, 64,
  172.     192, 96, 64,
  173.     224, 96, 64,
  174.     0, 128, 64,
  175.     32, 128, 64,
  176.     64, 128, 64,
  177.     96, 128, 64,
  178.     128, 128, 64,
  179.     160, 128, 64,
  180.     192, 128, 64,
  181.     224, 128, 64,
  182.     0, 160, 64,
  183.     32, 160, 64,
  184.     64, 160, 64,
  185.     96, 160, 64,
  186.     128, 160, 64,
  187.     160, 160, 64,
  188.     192, 160, 64,
  189.     224, 160, 64,
  190.     0, 192, 64,
  191.     32, 192, 64,
  192.     64, 192, 64,
  193.     96, 192, 64,
  194.     128, 192, 64,
  195.     160, 192, 64,
  196.     192, 192, 64,
  197.     224, 192, 64,
  198.     0, 224, 64,
  199.     32, 224, 64,
  200.     64, 224, 64,
  201.     96, 224, 64,
  202.     128, 224, 64,
  203.     160, 224, 64,
  204.     192, 224, 64,
  205.     224, 224, 64,
  206.     0, 0, 128,
  207.     32, 0, 128,
  208.     64, 0, 128,
  209.     96, 0, 128,
  210.     128, 0, 128,
  211.     160, 0, 128,
  212.     192, 0, 128,
  213.     224, 0, 128,
  214.     0, 32, 128,
  215.     32, 32, 128,
  216.     64, 32, 128,
  217.     96, 32, 128,
  218.     128, 32, 128,
  219.     160, 32, 128,
  220.     192, 32, 128,
  221.     224, 32, 128,
  222.     0, 64, 128,
  223.     32, 64, 128,
  224.     64, 64, 128,
  225.     96, 64, 128,
  226.     128, 64, 128,
  227.     160, 64, 128,
  228.     192, 64, 128,
  229.     224, 64, 128,
  230.     0, 96, 128,
  231.     32, 96, 128,
  232.     64, 96, 128,
  233.     96, 96, 128,
  234.     128, 96, 128,
  235.     160, 96, 128,
  236.     192, 96, 128,
  237.     224, 96, 128,
  238.     0, 128, 128,
  239.     32, 128, 128,
  240.     64, 128, 128,
  241.     96, 128, 128,
  242.     128, 128, 128,
  243.     160, 128, 128,
  244.     192, 128, 128,
  245.     224, 128, 128,
  246.     0, 160, 128,
  247.     32, 160, 128,
  248.     64, 160, 128,
  249.     96, 160, 128,
  250.     128, 160, 128,
  251.     160, 160, 128,
  252.     192, 160, 128,
  253.     224, 160, 128,
  254.     0, 192, 128,
  255.     32, 192, 128,
  256.     64, 192, 128,
  257.     96, 192, 128,
  258.     128, 192, 128,
  259.     160, 192, 128,
  260.     192, 192, 128,
  261.     224, 192, 128,
  262.     0, 224, 128,
  263.     32, 224, 128,
  264.     64, 224, 128,
  265.     96, 224, 128,
  266.     128, 224, 128,
  267.     160, 224, 128,
  268.     192, 224, 128,
  269.     224, 224, 128,
  270.     0, 0, 192,
  271.     32, 0, 192,
  272.     64, 0, 192,
  273.     96, 0, 192,
  274.     128, 0, 192,
  275.     160, 0, 192,
  276.     192, 0, 192,
  277.     224, 0, 192,
  278.     0, 32, 192,
  279.     32, 32, 192,
  280.     64, 32, 192,
  281.     96, 32, 192,
  282.     128, 32, 192,
  283.     160, 32, 192,
  284.     192, 32, 192,
  285.     224, 32, 192,
  286.     0, 64, 192,
  287.     32, 64, 192,
  288.     64, 64, 192,
  289.     96, 64, 192,
  290.     128, 64, 192,
  291.     160, 64, 192,
  292.     192, 64, 192,
  293.     224, 64, 192,
  294.     0, 96, 192,
  295.     32, 96, 192,
  296.     64, 96, 192,
  297.     96, 96, 192,
  298.     128, 96, 192,
  299.     160, 96, 192,
  300.     192, 96, 192,
  301.     224, 96, 192,
  302.     0, 128, 192,
  303.     32, 128, 192,
  304.     64, 128, 192,
  305.     96, 128, 192,
  306.     128, 128, 192,
  307.     160, 128, 192,
  308.     192, 128, 192,
  309.     224, 128, 192,
  310.     0, 160, 192,
  311.     32, 160, 192,
  312.     64, 160, 192,
  313.     96, 160, 192,
  314.     128, 160, 192,
  315.     160, 160, 192,
  316.     192, 160, 192,
  317.     224, 160, 192,
  318.     0, 192, 192,
  319.     32, 192, 192,
  320.     64, 192, 192,
  321.     96, 192, 192,
  322.     128, 192, 192,
  323.     160, 192, 192,
  324.     192, 192, 192,
  325.     224, 192, 192,
  326.     0, 224, 192,
  327.     32, 224, 192,
  328.     64, 224, 192,
  329.     96, 224, 192,
  330.     128, 224, 192,
  331.     160, 224, 192,
  332.     192, 224, 192,
  333.     224, 224, 192
  334. };
  335. @//E*O*F colors.h//
  336. chmod u=rw,g=r,o=r colors.h
  337.  
  338. echo x - graphics.c
  339. sed 's/^@//' > "graphics.c" <<'@//E*O*F graphics.c//'
  340. #include <X11/Xos.h>
  341. #include <X11/Xlib.h>
  342. #include <X11/Xutil.h>
  343. #include <X11/Xatom.h>
  344. #include <stdio.h>
  345. #include <math.h>
  346. #include <signal.h>
  347. #include "graphics.h"
  348.  
  349. #define NMAX 256
  350. #define SCFAC 5
  351. #define FLOOR2(x) ((x)/2)
  352. #define CEIL2(x)  ((x) - (x)/2)
  353.  
  354. unsigned long pixellist[NMAX];
  355.  
  356. char *malloc();
  357.  
  358. int ntx,nrx,nty,nry;
  359. int nmaxt,nmint,numpix;
  360. int nomin = 1;
  361. int nomax = NMAX-1;
  362. char *display = NULL;
  363. double xin = 0;
  364. double yin = 0;
  365. double scale = 1;
  366. int rsiz = 12;
  367. double dx,dy;
  368. double pscale;
  369. int restartflag;
  370. int (*func)();
  371. char *name = "XGraphics";
  372.  
  373. Rectangle root;
  374.  
  375. int rscreen;
  376. Display *dpy;
  377. Colormap cid;
  378. Window rwindow,nwindow;
  379. Pixmap pixmap;
  380. Pixmap npixmap;
  381. GC gc;
  382. XGCValues gcv;
  383.  
  384. graphics(display0,xin0,yin0,scale0,res,f)
  385. char *display0;
  386. double xin0,yin0,scale0;
  387. int res,(*f)();
  388. {
  389.     display = display0;
  390.     xin = xin0;
  391.     yin = yin0;
  392.     scale = scale0;
  393.     func = f;
  394.     rsiz = res;
  395.  
  396.     root.x = root.y = 0;
  397.     root.w = root.h = rsiz;
  398.     rectinit(&root);
  399.     init();
  400.     doplot();
  401.  
  402.     finish();
  403. }
  404.  
  405. init()
  406. {
  407.     int sigfin();
  408.     int i,width,height;
  409.     XWindowAttributes war;
  410.     XSetWindowAttributes setwa;
  411.     XColor sdr;
  412.  
  413.     signal(SIGINT, sigfin);
  414.     signal(SIGQUIT, sigfin);
  415.     signal(SIGTERM, sigfin);
  416.  
  417.         if ((dpy = XOpenDisplay(display)) == 0) {
  418.                 fprintf(stderr, "Graphics: Can't open display \"%s\"\n",
  419.                     XDisplayName(display));
  420.                 exit(1);
  421.         }
  422.  
  423.     rwindow = XDefaultRootWindow(dpy);
  424.     pointerinit();
  425.  
  426.     rscreen = XDefaultScreen(dpy);
  427.  
  428.     XGetWindowAttributes(dpy,rwindow,&war);
  429.     width = war.width;
  430.     height = war.height;
  431.     nrx = ceil(((double) width)/rsiz);
  432.     ntx = nrx*rsiz;
  433.     nry = ceil(((double) height)/rsiz);
  434.     nty = nry*rsiz;
  435.  
  436.     nwindow = XCreateWindow(dpy, rwindow, 0, 0,
  437.         (unsigned int) nrx, (unsigned int) nry, 0, 0,
  438.         CopyFromParent, CopyFromParent, 0L, &setwa);
  439.      /* Give the window a name */
  440.     XChangeProperty(dpy, nwindow, XA_WM_NAME, XA_STRING, 8,
  441.         PropModeReplace, name, strlen(name));
  442.     XMapWindow(dpy,nwindow);
  443.     XMoveWindow(dpy,nwindow,0,0);
  444.  
  445.     cid = XDefaultColormap(dpy,rscreen);
  446.  
  447.     numpix = 1<<war.depth;
  448.     pixellist[0] = 1;
  449.     for (i=1;i<NMAX;i++){
  450.         pixellist[i] = (i + 1) % numpix;
  451.     }
  452.  
  453.     pixmap = XCreatePixmap(dpy,rwindow,ntx,nty,war.depth);
  454.     if (pixmap==0){
  455.         fprintf(stderr, "Graphics: Unable to create Pixmap");
  456.         exit(1);
  457.     }
  458.     npixmap = XCreatePixmap(dpy,nwindow,nrx,nry,war.depth);
  459.     if (npixmap==0){
  460.         fprintf(stderr, "Graphics: Unable to create Pixmap");
  461.         exit(1);
  462.     }
  463.     gc = XCreateGC(dpy,pixmap,0L,&gcv);
  464. }
  465.  
  466. initpix()
  467. {
  468.     int i;
  469.  
  470.     pixellist[0] = 1;
  471.     nomax = nmaxt;
  472.     nomin = nmint;
  473.     for (i=1;i<NMAX;i++){
  474.         pixellist[i] = (2 + ((i - nomin)*(numpix-2)) /
  475.                     (nomax-nomin)) % numpix;
  476.     }
  477. }
  478.  
  479. pointerinit()
  480. {
  481.     long event_mask;
  482.     int mkpm;
  483.     XModifierKeymap *modmap;
  484.     KeyCode *mkdptr;
  485.  
  486.     modmap = XGetModifierMapping(dpy);
  487.     mkpm = modmap->max_keypermod;
  488.     mkdptr = modmap->modifiermap;
  489.     mkdptr[4*mkpm] = (char) 26; /* This is the keycode for the Alt key,
  490.                     and Mod2 has position 4 */
  491.     XSetModifierMapping(dpy,modmap);
  492.  
  493.     event_mask = ButtonPressMask;
  494.     XGrabButton(dpy,AnyButton,Mod2Mask,rwindow,True,event_mask,
  495.         GrabModeAsync,GrabModeAsync,None,0);
  496. }
  497.  
  498. int rescale(n)
  499. int n;
  500. {
  501.     int i;
  502.  
  503.     i = abs(n)%NMAX;
  504.     if (i>nmaxt) nmaxt = i;
  505.     if ((i>0)&&(i<nmint)) nmint = i;
  506.     return i;
  507. }
  508.  
  509. plotpt(i,j,w,h,n)
  510. int i,j,w,h,n;
  511. {
  512.     XSetForeground(dpy,gc,pixellist[n]);
  513.     XFillRectangle(dpy,pixmap,gc,i,j,w,h);
  514.     if ((i%rsiz==0)&&(j%rsiz==0))
  515.             XDrawPoint(dpy,npixmap,gc,i/rsiz,j/rsiz);
  516. }
  517.  
  518. plotall()
  519. {
  520.     XSetWindowBackgroundPixmap(dpy,rwindow,pixmap);
  521.     XSetWindowBackgroundPixmap(dpy,nwindow,npixmap);
  522.     XClearWindow(dpy,rwindow);
  523.     XClearWindow(dpy,nwindow);
  524.     XFlush(dpy);
  525.     checkevent();
  526. }
  527.  
  528. checkevent()
  529. {
  530.     long event_mask;
  531.     XEvent evr;
  532.     XButtonEvent xbevr;
  533.     char *args = NULL;
  534.     Bool checkb2();
  535.  
  536.     event_mask = ButtonPressMask;
  537.     if (XCheckIfEvent(dpy,&evr,checkb2,args)== True){
  538.         restartflag = 1;
  539.         xbevr = evr.xbutton;
  540.         xin += (-(nrx+1)*rsiz + 2*xbevr.x)/(pscale*rsiz);
  541.         yin += (-(nry+1)*rsiz + 2*xbevr.y)/(pscale*rsiz);
  542.     }
  543.     while(XCheckMaskEvent(dpy,event_mask,&evr)==True){
  544.         restartflag = 1;
  545.         xbevr = evr.xbutton;
  546.         if (xbevr.button == Button1) scale /= SCFAC;
  547.         if (xbevr.button == Button3) scale *= SCFAC;
  548.     }
  549.     printf("xin,yin: %g, %g, scale: %g\n",xin,yin,scale);
  550. }
  551.  
  552. Bool checkb2(display,event,args)
  553. Display *display;
  554. XEvent *event;
  555. char *args;
  556. {
  557.     XButtonEvent xbevr;
  558.     xbevr = event->xbutton;
  559.     if ((xbevr.button == Button2)&&
  560.         (event->type==ButtonPress)) return True;
  561.     else return False;
  562. }
  563.  
  564. Rectangle *newrect()
  565. {
  566.     Rectangle *t;
  567.  
  568.     t = (Rectangle *) malloc(sizeof(Rectangle));
  569.     return t;
  570. }
  571.  
  572. rectinit(rptr)
  573. Rectangle *rptr;
  574. {
  575.     Rectangle *nptr;
  576.     int w,h;
  577.  
  578.     w  = rptr->w;
  579.     h = rptr->h;
  580.  
  581.     if ((h==1)&&(w==1)) rptr->ul = NULL;
  582.     else {
  583.         nptr = rptr->ul = newrect();
  584.         nptr->x = rptr->x;
  585.         nptr->y = rptr->y;
  586.         nptr->w = CEIL2(w);
  587.         nptr->h = CEIL2(h);
  588.         rectinit(nptr);
  589.     }
  590.     if (w==1) rptr->ur = NULL;
  591.     else {
  592.  
  593.         nptr = rptr->ur = newrect();
  594.         nptr->x = rptr->x + CEIL2(w);
  595.         nptr->y = rptr->y;
  596.         nptr->w = FLOOR2(w);
  597.         nptr->h = CEIL2(h);
  598.         rectinit(nptr);
  599.     }
  600.     if (h == 1) rptr->ll = NULL;
  601.     else {
  602.         nptr = rptr->ll = newrect();
  603.         nptr->x = rptr->x;
  604.         nptr->y = rptr->y + CEIL2(h);
  605.         nptr->w = CEIL2(w);
  606.         nptr->h = FLOOR2(h);
  607.         rectinit(nptr);
  608.     }
  609.     if ((h == 1)||(w == 1)) rptr->lr = NULL;
  610.     else {
  611.         nptr = rptr->lr = newrect();
  612.         nptr->x = rptr->x + CEIL2(w);
  613.         nptr->y = rptr->y + CEIL2(h);
  614.         nptr->w = FLOOR2(w);
  615.         nptr->h = FLOOR2(h);
  616.         rectinit(nptr);
  617.     }
  618. }
  619.  
  620. doplot()
  621. {
  622.     int dographics();
  623.     int l,maxl;
  624.  
  625.     maxl = 0;
  626.     l = 1;
  627.     while (l<rsiz){
  628.         l *=2;
  629.         maxl++;
  630.     }
  631. restart:
  632.     
  633.     restartflag = 0;
  634.     pscale = nrx*scale;
  635.     dx = 2/(pscale);
  636.     dy = 2/(pscale);
  637.     nmaxt = 0;
  638.     nmint = NMAX;
  639.     if (dographics(&root,0)==0) goto restart;
  640.     initpix();
  641.     for (l=0;l<=maxl;l++){
  642.         if (dographics(&root,l)==0) goto restart;
  643.     }
  644. }
  645.  
  646. int dographics(rptr,level)
  647. Rectangle *rptr;
  648. int level;
  649. {
  650.     int i,j,ii,jj,n;
  651.     double xstart,ystart,x,y;
  652.  
  653.     if (rptr==NULL) return 1;
  654.     if (level==0){
  655.         ii = rptr->x;
  656.         jj = rptr->y;
  657.         xstart = xin - nrx/pscale + (-rsiz+2*ii)/(pscale*rsiz);
  658.         ystart = yin - nry/pscale + (-rsiz + 2*jj)/(pscale*rsiz);
  659.         for (x = xstart,i=0;i<nrx;i++,x += dx)
  660.          for (y = ystart, j=0;j<nry;j++,y += dy){
  661.             n = rescale((*func)(x,y));
  662.             plotpt(rsiz*i+ii,rsiz*j+jj,rptr->w,rptr->h,n);
  663.         }
  664.         plotall();
  665.         if (restartflag==1) return(0);
  666.         return(1);
  667.     }
  668.     if (dographics2(rptr,level)==0) return 0;
  669.     return 1;
  670. }
  671.  
  672. int dographics2(rptr,level)
  673. Rectangle *rptr;
  674. int level;
  675. {
  676.     if (rptr==NULL) return 1;
  677.     if (dographics2(rptr->ul,level-1)==0) return 0;
  678.     if (dographics(rptr->ur,level-1)==0) return 0;
  679.     if (dographics(rptr->ll,level-1)==0) return 0;
  680.     if (dographics(rptr->lr,level-1)==0) return 0;
  681.     return 1;
  682. }
  683.  
  684. finish()
  685. {
  686.     XFreeGC(dpy,gc);
  687.     XFreePixmap(dpy,pixmap);
  688.     XFreePixmap(dpy,npixmap);
  689.     XDestroyWindow(dpy,nwindow);
  690. }
  691.  
  692. sigfin(sig,code,scp)
  693. int sig,code;
  694. struct sigcontext *scp;
  695. {
  696.     finish();
  697.     exit(sig);
  698. }
  699. @//E*O*F graphics.c//
  700. chmod u=rw,g=r,o=r graphics.c
  701.  
  702. echo x - graphics.h
  703. sed 's/^@//' > "graphics.h" <<'@//E*O*F graphics.h//'
  704. typedef struct rectangle{
  705.     int x,y,w,h;
  706.     struct rectangle *ul,*ur,*ll,*lr;
  707. } Rectangle;
  708. @//E*O*F graphics.h//
  709. chmod u=rw,g=r,o=r graphics.h
  710.  
  711. echo x - wrt.c
  712. sed 's/^@//' > "wrt.c" <<'@//E*O*F wrt.c//'
  713. #include <stdio.h>
  714. #include <sys/file.h>
  715. #include <math.h>
  716. #include <rasterfile.h>
  717.  
  718. #define XSIZ 1152
  719. #define YSIZ 900
  720. #define TOTSIZ 1036800
  721. #define LINE 80
  722. #define MAXCHAR 256
  723.  
  724. extern double xin,yin,scale;
  725. extern int nomin,nomax;
  726. int head[] = {
  727.     RAS_MAGIC,
  728.     XSIZ,     /* Width */
  729.     YSIZ,    /* height */
  730.     8,    /* depth */
  731.     TOTSIZ,    /* length - set later */
  732.     RT_STANDARD,    /* type 1 rasterfile */
  733.     RMT_EQUAL_RGB,        /* RGB vectors */
  734.     768        /* Size of colormap = 3*256 */
  735. };
  736.  
  737. writeman(func)
  738. int (*func)();
  739. {
  740.     char filename[LINE];
  741.     char instr[LINE];
  742.     unsigned char *buffer;
  743.     unsigned char colormap[768];
  744.     int rsiz,fd;
  745.     int nrx,nry;
  746.     int i,j;
  747.     unsigned char n;
  748.     double x,xstart,y,ystart;
  749.     double pscale,dx,dy;
  750.  
  751.     fprintf(stderr,"Name of file: ");
  752.     fflush(stderr);
  753.     fgets(instr, LINE, stdin);
  754.     sscanf(instr,"%s",filename);
  755.     rsiz = igetd("resolution scale", 1);
  756.     if ((fd = open(filename,O_CREAT|O_WRONLY,0644))==-1){
  757.         fprintf(stderr,"Sorry, can't open file\n");
  758.         exit(-1);
  759.     }
  760.     if (fork()) exit(0);
  761.  
  762.     write(fd, head, sizeof(head));
  763.     putcolor(colormap);
  764.     write(fd, colormap, 768);
  765.  
  766.     buffer = (unsigned char *) malloc(TOTSIZ*sizeof(char));
  767.  
  768.     nrx = (int) ceil(((double) XSIZ)/rsiz);
  769.     nry = (int) ceil(((double) YSIZ)/rsiz);
  770.  
  771. /*    write(fd, &nrx, sizeof(int));
  772.     write(fd, &nry, sizeof(int));*/
  773.  
  774.     pscale = nrx*scale;
  775.     dy = dx = 2/pscale;
  776.     xstart = xin - nrx/pscale;
  777.     ystart = yin - nry/pscale;
  778.     for (y=ystart, j=0;j<nry;j++,y += dy)
  779.         for (x = xstart,i=0;i<nrx;i++,x += dx){
  780.             n = abs((*func)(x,y))%MAXCHAR;
  781.             if (n==0) n=1;
  782.             else n = (2 + ((n - nomin)*(MAXCHAR-2)) /
  783.                     (nomax-nomin)) % MAXCHAR;
  784.             writebuf(buffer, n, i, j, rsiz);
  785.         }
  786.     close(fd);
  787. }
  788.  
  789. writebuf(buf, c, x, y, rsiz)
  790. unsigned char *buf, c;
  791. int x, y, rsiz;
  792. {
  793.     int index,i,j,ind;
  794.  
  795.     index = x*rsiz + y*rsiz*XSIZ;
  796.     for (i=0;i<rsiz;i++){
  797.         ind = index + i*XSIZ;
  798.         for(j = 0;j<rsiz;j++){
  799.             if (ind >= TOTSIZ) break;
  800.             buf[ind++] = c;
  801.         }
  802.     }
  803. }
  804.  
  805. igetd(mesg,deflt)
  806. char *mesg;
  807. int deflt;
  808. {
  809.     int i;
  810.     char s[LINE];
  811.     fprintf(stderr,"*Enter %s [%d]: ",mesg,deflt);
  812.     fflush(stderr);
  813.     if (!fgets(s,LINE,stdin)) exit(-1);
  814.     if(sscanf(s,"%d%>",&i)==EOF)
  815.         return(deflt);
  816.     else
  817.         return(i);
  818. }
  819.  
  820. #include "colors.h"
  821.  
  822. putcolor(cm)
  823. unsigned char *cm;
  824. {
  825.     int i, j;
  826.  
  827.     for (i=0;i<3;i++)
  828.         for (j=0;j<256;j++)
  829.             *cm++ = colors[3*j + i];
  830. }
  831. @//E*O*F wrt.c//
  832. chmod u=rw,g=r,o=r wrt.c
  833.  
  834. echo x - xmandel.c
  835. sed 's/^@//' > "xmandel.c" <<'@//E*O*F xmandel.c//'
  836. #include <stdio.h>
  837. #include <signal.h>
  838. #include <math.h>
  839. #include <string.h>
  840. #include <ctype.h>
  841.  
  842. #define NMAX 200
  843. #define LINE 80
  844. #define SCFAC 5
  845.  
  846. typedef struct complx{
  847.     double re;
  848.     double im;
  849. } Cmplx;
  850.  
  851. char *argv0;
  852. int rsiz0 = 12;
  853. double xin0 = 0;
  854. double yin0 = 0;
  855. double scale0 = 1;
  856. char *display0 = NULL;
  857. char *malloc();
  858. double xo,yo;
  859. FILE *fp = stderr;
  860. FILE *fpin = stdin;
  861.  
  862. main(argc,argv)
  863. int argc;
  864. char **argv;
  865. {
  866.     int nmand(), length;
  867.     void exithandler();
  868.  
  869.     argv0 = argv[0];
  870. again:
  871.     if (argc > 2 && strcmp(argv[1], "-display") == 0) {
  872.         argv++;
  873.         argc--;
  874.         length = strlen(argv[1]);
  875.         display0 = malloc(length + 5);
  876.         strcpy(display0,argv[1]);
  877.         if ((length < 4) || (argv[1][length - 4] != ':'))
  878.             strcat(display0,":0.0");
  879.         argv++;
  880.         argc--;
  881.         goto again;
  882.     }
  883.     if (argc > 2 && strcmp(argv[1], "-x") == 0) {
  884.         argv++;
  885.         argc--;
  886.         if (sscanf(argv[1], "%F", &xin0) !=1)
  887.             usage();
  888.         argv++;
  889.         argc--;
  890.         goto again;
  891.     }
  892.     if (argc > 2 && strcmp(argv[1], "-y") == 0) {
  893.         argv++;
  894.         argc--;
  895.         if (sscanf(argv[1], "%F", &yin0) !=1)
  896.             usage();
  897.         argv++;
  898.         argc--;
  899.         goto again;
  900.     }
  901.     if (argc > 2 && strcmp(argv[1], "-s") == 0) {
  902.         argv++;
  903.         argc--;
  904.         if (sscanf(argv[1], "%F", &scale0) !=1)
  905.             usage();
  906.         argv++;
  907.         argc--;
  908.         goto again;
  909.     }
  910.     if (argc > 2 && strcmp(argv[1], "-r") == 0) {
  911.         argv++;
  912.         argc--;
  913.         if (sscanf(argv[1], "%d", &rsiz0) !=1)
  914.             usage();
  915.         argv++;
  916.         argc--;
  917.         goto again;
  918.     }
  919.  
  920.     if (argc > 1) usage();
  921.  
  922.     on_exit(exithandler);
  923.     graphics(display0,xin0,yin0,scale0,rsiz0,nmand,NMAX);
  924. }
  925.  
  926. int nmand(x,y)
  927. double x,y;
  928. {
  929.     Cmplx map(),z;
  930.     int n;
  931.  
  932.     n = 1;
  933.     xo = z.re = x;
  934.     yo = z.im = y;
  935.     for(;z.re*z.re+z.im*z.im < 4;z = map(z))
  936.         if (++n>=NMAX) break;
  937.     return NMAX - n;
  938. }
  939.  
  940. Cmplx map(z)
  941. Cmplx z;
  942. {
  943.     Cmplx t;
  944.  
  945.     t.re = z.re*z.re - z.im*z.im + xo;
  946.     t.im = 2*z.re*z.im + yo;
  947.     return t;
  948. }
  949.  
  950. usage()
  951. {
  952.     fprintf(stderr,
  953.         "usage: %s [-display host] [-x x0] [-y y0] [-s scale]\n",argv0);
  954.     fprintf(stderr,"       [-r resolution]\n");
  955.     exit(1);
  956. }
  957.  
  958. void exithandler(status)
  959. int status;
  960. {
  961.     int wtf,nmand();
  962.  
  963.     if (status != SIGINT) exit(0);
  964.     wtf = lgetd("Write to file",0);
  965.     if (wtf == 0) exit(0);
  966.     writeman(nmand);
  967. }
  968.  
  969. lgetd(mesg,deflt)
  970. char *mesg;
  971. int deflt;
  972. {
  973.     char s[LINE], *sptr;
  974.     for(;;) {
  975.         fprintf(fp,"*%s [%c]: ",mesg,(deflt ? 'y' : 'n'));
  976.         fflush(fp);
  977.         sptr = fgets(s,LINE,fpin);
  978.         if (!sptr) exit(-1);
  979.         while(isspace(*sptr)) sptr++;
  980.         if(*sptr=='\0') return(deflt);
  981.         if(isupper(*sptr)) *sptr = tolower(*sptr);
  982.           if(*sptr == 'y')
  983.                 return(1);
  984.           else if(*sptr == 'n')
  985.                 return(0);
  986.           else
  987.                 fprintf(fp,"   Say what? Enter y or n.\n");
  988.     }
  989. }
  990. @//E*O*F xmandel.c//
  991. chmod u=rw,g=r,o=r xmandel.c
  992.  
  993. echo Inspecting for damage in transit...
  994. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  995. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  996. cat > $temp <<\!!!
  997.        9      61     431 Makefile
  998.       27     181    1131 README
  999.      258     774    3643 colors.h
  1000.      359     802    6833 graphics.c
  1001.        4      10      88 graphics.h
  1002.      118     297    2300 wrt.c
  1003.      154     365    2666 xmandel.c
  1004.      929    2490   17092 total
  1005. !!!
  1006. wc  Makefile README colors.h graphics.c graphics.h wrt.c xmandel.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  1007. if [ -s $dtemp ]
  1008. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  1009. else echo "No problems found."
  1010. fi
  1011. exit 0
  1012.