home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_49.arc / PS-FR386.ARC / PS-FR386.C < prev   
Text File  |  1989-07-11  |  17KB  |  494 lines

  1. /* Micro Cornucopia Magazine Issue #49
  2.    PostScriptal article code
  3.  
  4.    PS-FR386.C -- 386-specific, fixed point Mandelbrot generator for PostScript
  5.    file generation.
  6.  
  7.    This code generates four files comprising a four-color separation, all set
  8.    for your favorite frinting press. I should say *my* favorite press. The
  9.    output is:
  10.  
  11.    - right reading
  12.    - emulsion side down
  13.    - negative images with the following color layer assignments:
  14.  
  15.      layer      color        output file
  16.      -------------------------------------
  17.        1        yellow        frac1.ps
  18.        2        magenta       frac2.ps
  19.        3        cyan          frac3.ps
  20.        4        black         frac4.ps
  21.  
  22.    PS-FR386.C is a heavily butchered version of Harlan Stockman's FR386.C.
  23.    (See Micro Cornucopia Issue #43.) So give Harlan credit for the good stuff,
  24.    and blame the weirdnesses on me.
  25.  
  26.    Everything's hard coded for the cover of Micro C. As usual, I've built no
  27.    user interface.
  28.  
  29.    Compiled with DeSmet C88. Requires the assembly file, MAND.A.
  30.  
  31.    Larry Fogg - July, 1989
  32.  
  33. */
  34.  
  35. #define maxc 750                           /* image size in columns and rows */
  36. #define maxr 1200
  37. #define maxcolors 82   /* number of colors defined - could be up to 64K here */
  38. #define matx "750 "                           /* ASCII version of image size */
  39. #define maty "300 "
  40. #define bits "4"  /* # bits per point - ie 16 possible shades per point here */
  41. #define str_length "375 "     /* 750 points @ 4 bits/point -> 375 bytes/line */
  42. #define scr_lines "150 "                /* # lines per inch on output device */
  43. #define num_copies "1"
  44.  
  45. #include <stdio.h>
  46.  
  47. int switchpt, i, itermax, mcol, mrow;
  48. double xmin, xmax, ymin, ymax, newdy, newdx, dx, dy, fabs();
  49. char key;
  50. int mandel();
  51. void mark_page (), do_crops ();
  52. void file_header (), file_tailer (), draw_points (), get_layers ();
  53. FILE *fp1, *fp2, *fp3, *fp4;
  54.  
  55. main()
  56. {
  57.   if(!is_386()) puts("SORRY! Need Intel 386!\n"), exit(0);
  58.  
  59.   mcol = maxc, mrow = maxr;
  60.  
  61.   puts ("80386 fixed point PostScript Mandelbrot set generator.\n\n");
  62.   xmin=-0.7504, xmax=-0.7408, ymin=0.105, ymax=0.12036;
  63.   itermax=2000, switchpt=200;  /* bug in DeSmet requires < 4 comma phrases */
  64.   printf ("Press 'Q' to quit...\n");
  65.  
  66.   fp1 = fopen ("frac1.ps", "w");                             /* yellow layer */
  67.   fp2 = fopen ("frac2.ps", "w");                            /* magenta layer */
  68.   fp3 = fopen ("frac3.ps", "w");                               /* cyan layer */
  69.   fp4 = fopen ("frac4.ps", "w");                              /* black layer */
  70.   file_header (fp1);                                  /* defs and procedures */
  71.   file_header (fp2);
  72.   file_header (fp3);
  73.   file_header (fp4);
  74.   if ('q' == mandel (xmin, xmax, ymin, ymax, mcol, mrow))    /* get hex data */
  75.     exit(0);
  76.   file_tailer (fp1);              /* finish main part of PostScript programs */
  77.   file_tailer (fp2);
  78.   file_tailer (fp3);
  79.   file_tailer (fp4);
  80.  
  81.   puts ("\n\nLast PostScriptal:\n");
  82.   printf ("x= %.5f to %.5f\ny= %.5f to %.5f\nswitchpt= %d, iter= %d",
  83.            xmin,xmax,ymin,ymax,switchpt,itermax);
  84.   printf ("\nOutput files = FRAC(1C,2C,3C,4C).PS\n");
  85.   fclose (fp1);
  86.   fclose (fp2);
  87.   fclose (fp3);
  88.   fclose (fp4);
  89. }  /* main */
  90.  
  91.  
  92.  
  93. /* mandel() IS ANALOGOUS TO L. FOGG'S VERSION IN DEC/JAN 87/88 MicroC ... */
  94. /* ncol AND nrow ARE THE NUMBER OF COLUMNS AND ROWS THAT CAN BE DISPLAYED */
  95. /* endcolor IS ONE LESS THAN MAXIMUM # COLORS DEFINED. nmax IS THE */
  96. /* MAXIMUM NUMBER OF ITERATIONS WE WILL ALLOW FOR MANDELBROT SERIES... */
  97. /* DeSmet FUNCTION scr_csts() READS KEYBOARD BUF, BUT DOESN'T WAIT IF NO KEY */
  98. /* WAS PRESSED ... SIMILAR TO Turbo C COMBINATION OF kbhit() AND getch()... */
  99.  
  100. mandel (Pmin, Pmax, Qmin, Qmax, ncol, nrow)
  101. double Pmin, Pmax, Qmin, Qmax;
  102. int ncol, nrow;
  103. {
  104.   int row, col, iter, mandwhile();
  105.   int point1, point2;
  106.   long P, Q, P0, Q0, dP, dQ;
  107.   long muldiv();
  108.  
  109.   dP = (Pmax-Pmin)*16777216 + 0.5;               /* 16777216 = 2 to the 24th */
  110.   dQ = (Qmax-Qmin)*16777216 + 0.5;
  111.   P0 = Pmin*16777216 + 0.5;
  112.   Q0 = Qmin*16777216 + 0.5;
  113.  
  114.   for (row=0; row<nrow; ++row)
  115.   {
  116.     printf ("%d rows of %d\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", row, mrow);
  117.     for (col=ncol-1; col>=0; --col) /* backwards to make emulsion side down */
  118.     {
  119.       P = P0 + muldiv (dP, col, ncol-1);
  120.       Q = Q0 + muldiv (dQ, row, nrow-1);
  121.       iter = mandwhile(P, Q, itermax) + 1;  /*+1 DEPENDS ON HOW 1ST IT DEF'ED*/
  122.       if ((col %2) == 1)            /* this is the first of a pair of points */
  123.         point1 = iter;
  124.       else
  125.       {
  126.         point2 = iter;                     /* this is the second of the pair */
  127.         draw_points (point1, point2); /*draw the pair (generate one hex byte)*/
  128.       }
  129.     }
  130.     if (scr_csts()=='q') return ('q');                        /* wanna quit? */
  131.   }
  132. }  /* mandel */
  133.  
  134.  
  135.  
  136.  
  137.  
  138. void draw_points (pt1, pt2)
  139. int pt1, pt2;
  140. {
  141.   int transform();
  142.   unsigned char color;
  143.   unsigned char yellow, magenta, cyan, black;
  144.   unsigned char y_temp, m_temp, c_temp, b_temp;
  145.  
  146.   yellow = magenta = cyan = black = 0;
  147.   y_temp = m_temp = c_temp = b_temp = 0;
  148.   if (pt1 == itermax + 1)                      /* it's in the Mandelbrot set */
  149.   {                /* these color assignments draw the point in a deep black */
  150.     cyan = 0x90;    /* assignments made to high order nybble for first point */
  151.     yellow = 0x30;
  152.     black = 0xf0;
  153.   }
  154.   else                                                   /* point not in set */
  155.   {
  156.     color = transform (pt1, switchpt) % maxcolors;              /* get color */
  157.     get_layers (color, &y_temp, &m_temp, &c_temp, &b_temp); /*seperate layers*/
  158.     yellow = y_temp << 4;         /* shift assignments to high order nybbles */
  159.     magenta = m_temp << 4;
  160.     cyan = c_temp << 4;
  161.     black = b_temp << 4;
  162.   }
  163.   y_temp = m_temp = c_temp = b_temp = 0;
  164.   if (pt2 == itermax + 1)         /* do point 2 - it's in the Mandelbrot set */
  165.   {                                                         /* make it black */
  166.     cyan += 0x09;                            /* low order nybble for point 2 */
  167.     yellow += 0x03;
  168.     black += 0x0f;
  169.   }
  170.   else                                             /* point 2 not in the set */
  171.   {
  172.     color = transform (pt2, switchpt) % maxcolors;
  173.     get_layers (color, &y_temp, &m_temp, &c_temp, &b_temp);
  174.     yellow += y_temp;
  175.     magenta += m_temp;
  176.     cyan += c_temp;
  177.     black += b_temp;
  178.   }
  179.  
  180.   /* screwed with the color assignments here. Changing them made for a nicer
  181.      looking cover.
  182.  
  183.      magenta data --> yellow layer
  184.      cyan data    --> magenta layer
  185.      yellow data  --> cyan layer
  186.      black        --> black layer
  187.   */
  188.  
  189.   if (yellow < 0x10)      /* pad single digit (hexit) numbers with leading 0 */
  190.     fprintf (fp3, "0");
  191.   if (magenta < 0x10)
  192.     fprintf (fp1, "0");
  193.   if (cyan < 0x10)
  194.     fprintf (fp2, "0");
  195.   if (black < 0x10)
  196.     fprintf (fp4, "0");
  197.   fprintf (fp3, "%x", yellow); /*write the 2 point hex values to the 4 layers*/
  198.   fprintf (fp1, "%x", magenta);
  199.   fprintf (fp2, "%x", cyan);
  200.   fprintf (fp4, "%x", black);
  201. }  /* draw_points */
  202.  
  203.  
  204.  
  205.  
  206.  
  207. void do_crops (fp)        /* crop marks on 8.5 X 11 paper, .5" in from edges */
  208. FILE *fp;
  209. {
  210.   fprintf (fp, "\nnewpath\n");                       /* clear existing paths */
  211.   fprintf (fp, "0.01 inch setlinewidth\n");         /* set narrow line width */
  212.  
  213.   fprintf (fp, "1 inch 0.9 inch moveto\n");                    /* lower left */
  214.   fprintf (fp, "0 inch -0.5 inch rlineto\n");
  215.   fprintf (fp, "0.9 inch 1 inch moveto\n");
  216.   fprintf (fp, "-0.5 inch 0 inch rlineto\n");
  217.  
  218.   fprintf (fp, "7.5 inch 0.9 inch moveto\n");                 /* lower right */
  219.   fprintf (fp, "0 inch -0.5 inch rlineto\n");
  220.   fprintf (fp, "7.6 inch 1 inch moveto\n");
  221.   fprintf (fp, "0.5 inch 0 inch rlineto\n");
  222.  
  223.   fprintf (fp, "7.5 inch 10.1 inch moveto\n");                /* upper right */
  224.   fprintf (fp, "0 inch 0.5 inch rlineto\n");
  225.   fprintf (fp, "7.6 inch 10 inch moveto\n");
  226.   fprintf (fp, "0.5 inch 0 inch rlineto\n");
  227.  
  228.   fprintf (fp, "1 inch 10.1 inch moveto\n");                   /* upper left */
  229.   fprintf (fp, "0 inch 0.5 inch rlineto\n");
  230.   fprintf (fp, "0.9 inch 10 inch moveto\n");
  231.   fprintf (fp, "-0.5 inch 0 inch rlineto\n");
  232.  
  233.   fprintf (fp, "stroke\n");
  234. }  /* do_crops */
  235.  
  236.  
  237.  
  238.  
  239.  
  240. void mark_page (fp)                /* make identifying marks on the 4 layers */
  241. FILE *fp;
  242. {
  243.   fprintf (fp, "\nnewpath\n");                       /* clear existing paths */
  244.   fprintf (fp, "0.05 inch setlinewidth\n");           /* set wide line width */
  245.   fprintf (fp, "2 inch 0.9 inch moveto\n");              /* layer 1, 2, 3, 4 */
  246.   fprintf (fp, "0 inch -0.2 inch rlineto\n");
  247.   if ((fp==fp2) || (fp==fp3) || (fp==fp4))                  /* layer 2, 3, 4 */
  248.   {
  249.     fprintf (fp, "2.1 inch 0.9 inch moveto\n");
  250.     fprintf (fp, "0 inch -0.2 inch rlineto\n");
  251.   }
  252.   if ((fp==fp3) || (fp==fp4))                                  /* layer 3, 4 */
  253.   {
  254.     fprintf (fp, "2.2 inch 0.9 inch moveto\n");
  255.     fprintf (fp, "0 inch -0.2 inch rlineto\n");
  256.   }
  257.   if (fp==fp4)                                                    /* layer 4 */
  258.   {
  259.     fprintf (fp, "2.3 inch 0.9 inch moveto\n");
  260.     fprintf (fp, "0 inch -0.2 inch rlineto\n");
  261.   }
  262.   fprintf (fp, "stroke\n");
  263. }  /* mark_page */
  264.  
  265.  
  266.  
  267.  
  268. void file_header (f)
  269. FILE *f;
  270. {
  271.   fprintf (f, "/inch {72 mul} def\n");              /* define inch procedure */
  272.   do_crops (f);
  273.   mark_page (f);
  274.   fprintf (f, scr_lines);                       /* set scr_lines line screen */
  275.   if (f == fp1)                           /* yellow - 90 degree screen angle */
  276.     fprintf (f, "90 ");
  277.   if (f == fp2)                          /* magenta - 75 degree screen angle */
  278.     fprintf (f, "75 ");
  279.   if (f == fp3)                            /* cyan - 105 degree screen angle */
  280.     fprintf (f, "105 ");
  281.   if (f == fp4)                           /* black -  45 degree screen angle */
  282.     fprintf (f, "45 ");
  283.   /* obscure spot function */
  284.   fprintf (f, "{dup mul exch dup mul add 1 exch sub} setscreen\n");
  285.   fprintf (f, "/picstr ");                           /* bytes per image line */
  286.   fprintf (f, str_length);
  287.   fprintf (f, "string def\n");
  288.   fprintf (f, "/fractal\n");                     /* define fractal procedure */
  289.   fprintf (f, "{");
  290.   fprintf (f, matx);                                           /* image size */
  291.   fprintf (f, maty);
  292.   fprintf (f, bits);
  293.   fprintf (f, "[");                                 /* transformation matrix */
  294.   fprintf (f, matx);
  295.   fprintf (f, "0 0 ");
  296.   fprintf (f, maty);
  297.   fprintf (f, "0 0]\n");
  298.   fprintf (f, "{currentfile picstr readhexstring pop}\n");/* read image data */
  299.   fprintf (f, "image\n}def\n");
  300.   fprintf (f, "1.75 inch 1.5 inch translate\n");         /* center the image */
  301.   fprintf (f, "5 inch 8 inch scale\n");                   /* make it 5" x 8" */
  302.   fprintf (f, "fractal\n");                    /* call the fractal procedure */
  303. }  /* file_header */
  304.  
  305.  
  306.  
  307.  
  308.  
  309. void file_tailer (f)
  310. FILE *f;
  311. {
  312.   fprintf (f, "\n/#copies ");                       /* set # copies to print */
  313.   fprintf (f, num_copies);
  314.   fprintf (f, " def\n");
  315.   fprintf (f, "showpage\n");                               /* print the page */
  316.   putc (0x04, f);                                          /* write a ^D EOF */
  317. }  /* file_tailer */
  318.  
  319.  
  320.  
  321.  
  322. void get_layers (clr, y, m, c, b)
  323. unsigned char clr;
  324. unsigned char *y, *m, *c, *b;
  325. {
  326.   switch (clr)
  327.   {  /*for each of the 82 colors, assign value for each of the 4 color layers*/
  328.     case 0: *m = 0x0f; *c = 0x0b; *b = 0x06;
  329.             break;
  330.     case 1: *m = 0x0d; *c = 0x0b; *b = 0x06;
  331.             break;
  332.     case 2: *m = 0x0c; *c = 0x0b; *b = 0x06;
  333.             break;
  334.     case 3: *m = 0x0b; *c = 0x0b; *b = 0x06;
  335.             break;
  336.     case 4: *m = 0x0a; *c = 0x0b; *b = 0x06;
  337.             break;
  338.     case 5: *m = 0x09; *c = 0x0b; *b = 0x06;
  339.             break;
  340.     case 6: *m = 0x08; *c = 0x0b; *b = 0x06;
  341.             break;
  342.     case 7: *m = 0x07; *c = 0x0b; *b = 0x06;
  343.             break;
  344.     case 8: *m = 0x06; *c = 0x0b; *b = 0x06;
  345.             break;
  346.     case 9: *m = 0x05; *c = 0x0b; *b = 0x06;
  347.             break;
  348.     case 10: *m = 0x04; *c = 0x0b; *b = 0x06;
  349.              break;
  350.     case 11: *m = 0x03; *c = 0x0b; *b = 0x06;
  351.              break;
  352.     case 12: *m = 0x02; *c = 0x0b; *b = 0x06;
  353.              break;
  354.     case 13: *m = 0x01; *c = 0x0b; *b = 0x06;
  355.              break;
  356.     case 14: *m = 0x00; *c = 0x0b; *b = 0x06;
  357.              break;
  358.     case 15: *c = 0x0b; *b = 0x05;
  359.              break;
  360.     case 16: *c = 0x0b; *b = 0x04;
  361.              break;
  362.     case 17: *c = 0x0b; *b = 0x03;
  363.              break;
  364.     case 18: *c = 0x0b; *b = 0x02;
  365.              break;
  366.     case 19: *c = 0x0b; *b = 0x01;
  367.              break;
  368.     case 20: *c = 0x0b; *b = 0x00;
  369.              break;
  370.     case 21: *c = 0x0b; *y = 0x01;
  371.              break;
  372.     case 22: *c = 0x0b; *y = 0x02;
  373.              break;
  374.     case 23: *c = 0x0b; *y = 0x03;
  375.              break;
  376.     case 24: *c = 0x0b; *y = 0x04;
  377.              break;
  378.     case 25: *c = 0x0b; *y = 0x05;
  379.              break;
  380.     case 26: *c = 0x0b; *y = 0x06;
  381.              break;
  382.     case 27: *c = 0x0b; *y = 0x07;
  383.              break;
  384.     case 28: *c = 0x0b; *y = 0x08;
  385.              break;
  386.     case 29: *c = 0x0b; *y = 0x09;
  387.              break;
  388.     case 30: *c = 0x0b; *y = 0x0a;
  389.              break;
  390.     case 31: *c = 0x0b; *y = 0x0b;
  391.              break;
  392.     case 32: *c = 0x0b; *y = 0x0c;
  393.              break;
  394.     case 33: *c = 0x0b; *y = 0x0d;
  395.              break;
  396.     case 34: *c = 0x0b; *y = 0x0e;
  397.              break;
  398.     case 35: *c = 0x0b; *y = 0x0f;
  399.              break;
  400.     case 36: *y = 0x0f; *c = 0x09;
  401.              break;
  402.     case 37: *y = 0x0f; *c = 0x08;
  403.              break;
  404.     case 38: *y = 0x0f; *c = 0x07;
  405.              break;
  406.     case 39: *y = 0x0f; *c = 0x06;
  407.              break;
  408.     case 40: *y = 0x0f; *c = 0x05;
  409.              break;
  410.     case 41: *y = 0x0f; *c = 0x04;
  411.              break;
  412.     case 42: *y = 0x0f; *c = 0x03;
  413.              break;
  414.     case 43: *y = 0x0f; *c = 0x02;
  415.              break;
  416.     case 44: *y = 0x0f; *c = 0x01;
  417.              break;
  418.     case 45: *y = 0x0f; *c = 0x00;
  419.              break;
  420.     case 46: *y = 0x0b; *m = 0x01; *b = 0x00;
  421.              break;
  422.     case 47: *y = 0x0b; *m = 0x01; *b = 0x01;
  423.              break;
  424.     case 48: *y = 0x0b; *m = 0x01; *b = 0x02;
  425.              break;
  426.     case 49: *y = 0x0b; *m = 0x02; *b = 0x02;
  427.              break;
  428.     case 50: *y = 0x0b; *m = 0x03; *b = 0x02;
  429.              break;
  430.     case 51: *y = 0x0b; *m = 0x04; *b = 0x02;
  431.              break;
  432.     case 52: *y = 0x0b; *m = 0x05; *b = 0x02;
  433.              break;
  434.     case 53: *y = 0x0b; *m = 0x06; *b = 0x02;
  435.              break;
  436.     case 54: *y = 0x0b; *m = 0x07; *b = 0x02;
  437.              break;
  438.     case 55: *y = 0x0b; *m = 0x08; *b = 0x02;
  439.              break;
  440.     case 56: *y = 0x0b; *m = 0x09; *b = 0x02;
  441.              break;
  442.     case 57: *y = 0x0b; *m = 0x0a; *b = 0x02;
  443.              break;
  444.     case 58: *y = 0x0b; *m = 0x0b; *b = 0x02;
  445.              break;
  446.     case 59: *y = 0x0b; *m = 0x0c; *b = 0x02;
  447.              break;
  448.     case 60: *y = 0x0b; *m = 0x0d; *b = 0x02;
  449.              break;
  450.     case 61: *y = 0x0b; *m = 0x0e; *b = 0x02;
  451.              break;
  452.     case 62: *y = 0x0b; *m = 0x0f; *b = 0x02;
  453.              break;
  454.     case 63: *y = 0x0f; *m = 0x0f; *c = 0x09;
  455.              break;
  456.     case 64: *y = 0x0f; *m = 0x0f; *c = 0x0a;
  457.              break;
  458.     case 65: *y = 0x0f; *m = 0x0f; *c = 0x0b;
  459.              break;
  460.     case 66: *y = 0x0f; *m = 0x0f; *c = 0x0c;
  461.              break;
  462.     case 67: *y = 0x0e; *m = 0x0f; *c = 0x0c;
  463.              break;
  464.     case 68: *y = 0x0d; *m = 0x0f; *c = 0x0c;
  465.              break;
  466.     case 69: *y = 0x0c; *m = 0x0f; *c = 0x0c;
  467.              break;
  468.     case 70: *y = 0x0b; *m = 0x0f; *c = 0x0c;
  469.              break;
  470.     case 71: *y = 0x0a; *m = 0x0f; *c = 0x0c;
  471.              break;
  472.     case 72: *y = 0x09; *m = 0x0f; *c = 0x0c;
  473.              break;
  474.     case 73: *y = 0x08; *m = 0x0f; *c = 0x0c;
  475.              break;
  476.     case 74: *y = 0x07; *m = 0x0f; *c = 0x0c;
  477.              break;
  478.     case 75: *y = 0x06; *m = 0x0f; *c = 0x0c;
  479.              break;
  480.     case 76: *y = 0x05; *m = 0x0f; *c = 0x0c;
  481.              break;
  482.     case 77: *y = 0x04; *m = 0x0f; *c = 0x0c;
  483.              break;
  484.     case 78: *y = 0x03; *m = 0x0f; *c = 0x0c;
  485.              break;
  486.     case 79: *y = 0x02; *m = 0x0f; *c = 0x0c;
  487.              break;
  488.     case 80: *y = 0x01; *m = 0x0f; *c = 0x0c;
  489.              break;
  490.     case 81: *y = 0x00; *m = 0x0f; *c = 0x0c;
  491.              break;
  492.   }
  493. }  /* get_layers */
  494.