1 /* eps_img.c
2 
3    MolScript v2.1.2
4 
5    Encapsulated PostScript (EPS) image file.
6 
7    This implementation relies on the 'image.c' code, and therefore
8    implicitly on OpenGL and GLX.
9 
10    Copyright (C) 1997-1998 Per Kraulis
11     13-Sep-1997  working
12 */
13 
14 #include <assert.h>
15 #include <stdlib.h>
16 
17 #include <GL/gl.h>
18 
19 #include "eps_img.h"
20 #include "global.h"
21 #include "graphics.h"
22 #include "image.h"
23 #include "opengl.h"
24 
25 
26 /*============================================================*/
27 static int components = 3;
28 static float scale = 1.0;
29 
30 
31 /*------------------------------------------------------------*/
32 void
eps_set(void)33 eps_set (void)
34 {
35   ogl_set();
36 
37   output_first_plot = eps_first_plot;
38   output_finish_output = eps_finish_output;
39   output_start_plot = ogl_start_plot_general;
40 
41   output_pickable = NULL;
42 
43   output_mode = EPS_MODE;
44 }
45 
46 
47 /*------------------------------------------------------------*/
48 void
eps_first_plot(void)49 eps_first_plot (void)
50 {
51   image_first_plot();
52   set_outfile ("w");
53 
54   if (fprintf (outfile, "%%!PS-Adobe-3.0 EPSF-3.0\n") < 0)
55     yyerror ("could not write to the output EPS file");
56 
57   fprintf (outfile, "%%%%BoundingBox: 0 0 %i %i\n",
58 	            (int) (scale * output_width + 0.49999),
59 	            (int) (scale * output_height + 0.4999));
60   if (title) fprintf (outfile, "%%%%Title: %s\n", title);
61   fprintf (outfile, "%%%%Creator: %s, %s\n", program_str, copyright_str);
62   if (user_str[0] != '\0') fprintf (outfile, "%%%%For: %s\n", user_str);
63   PRINT ("%%EndComments\n");
64   PRINT ("%%BeginProlog\n");
65   PRINT ("10 dict begin\n");
66   PRINT ("/bwproc {\n");
67   PRINT ("  rgbproc\n");
68   PRINT ("  dup length 3 idiv string 0 3 0\n");
69   PRINT ("  5 -1 roll {\n");
70   PRINT ("  add 2 1 roll 1 sub dup 0 eq\n");
71   PRINT ("  { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
72   PRINT ("    3 1 roll 5 -1 roll put 1 add 3 0 }\n");
73   PRINT ("  { 2 1 roll } ifelse\n");
74   PRINT ("  } forall\n");
75   PRINT ("  pop pop pop\n");
76   PRINT ("} def\n");
77   PRINT ("systemdict /colorimage known not {\n");
78   PRINT ("  /colorimage {\n");
79   PRINT ("    pop pop\n");
80   PRINT ("    /rgbproc exch def\n");
81   PRINT ("    { bwproc } image\n");
82   PRINT ("  } def\n");
83   PRINT ("} if\n");
84   fprintf (outfile, "/picstr %i string def\n", components * output_width);
85   PRINT ("%%EndProlog\n");
86   PRINT ("%%BeginSetup\n");
87   PRINT ("gsave\n");
88   fprintf (outfile, "%g %g scale\n",
89 	   scale * (float) output_width, scale * (float) output_height);
90   PRINT ("%%EndSetup\n");
91   fprintf (outfile, "%i %i 8\n", output_width, output_height);
92   fprintf (outfile, "[%i 0 0 %i 0 0]\n", output_width, output_height);
93   PRINT ("{currentfile picstr readhexstring pop}\n");
94   fprintf (outfile, "false %i\n", components);
95   fprintf (outfile, "%%%%BeginData: %i Hex Bytes\n",
96 	            2 * output_width * output_height * components + 11);
97   PRINT ("colorimage\n");
98 }
99 
100 
101 /*------------------------------------------------------------*/
102 void
eps_finish_output(void)103 eps_finish_output (void)
104 {
105   int byte_count = output_width * components;
106   int row, pos, slot;
107   GLenum format;
108   unsigned char *buffer;
109   char *pix;
110 
111   format = (components == 1) ? GL_LUMINANCE : GL_RGB;
112 
113   image_render();
114 
115   buffer = malloc (byte_count * sizeof (unsigned char));
116 
117   for (row = 0; row < output_height; row++) {
118     glReadPixels (0, row, output_width, 1, format, GL_UNSIGNED_BYTE, buffer);
119     pos = 0;
120     pix = (char *) buffer;
121     for (slot = 0; slot < byte_count; slot++) {
122       fprintf (outfile, "%02hx", *pix++);
123       if (++pos >= 32) {
124 	fprintf (outfile, "\n");
125 	pos = 0;
126       }
127     }
128     if (pos) fprintf (outfile, "\n");
129   }
130 
131   free (buffer);
132 
133   PRINT ("%%EndData\n");
134   PRINT ("grestore\n");
135   PRINT ("end\n");
136 
137   image_close();
138 }
139 
140 
141 /*------------------------------------------------------------*/
142 void
eps_set_bw(void)143 eps_set_bw (void)
144 {
145   components = 1;
146 }
147 
148 
149 /*------------------------------------------------------------*/
150 void
eps_set_scale(float new)151 eps_set_scale (float new)
152 {
153   assert (new > 0.0);
154 
155   scale = new;
156 }
157