386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / gstdev.c
CommitLineData
019754da
WJ
1/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* gstdev.c */
21/* Device tracing for Ghostscript library */
22#include "gx.h"
23#include "gxfixed.h" /* for gxmatrix.h */
24#include "gxmatrix.h" /* for gxdevice.h */
25#include "gxdevice.h"
26
27#ifdef DEBUG
28
29/* ------ Tracing 'device' ------*/
30
31/* To avoid unpleasant interactions with copydevice, */
32/* the tracing 'device' uses an external linked list to keep track of */
33/* the real procedures that were replaced in the procedure vector. */
34
35typedef struct trace_record_s trace_record;
36struct trace_record_s {
37 trace_record *next;
38 gx_device_procs *tprocs;
39 gx_device_procs procs;
40 int index;
41};
42
43private gx_device *trace_cache_device = NULL;
44private gx_device_procs *trace_cache_procs;
45private trace_record *trace_list = NULL;
46private int trace_next_index = 0;
47
48#define rprocs\
49 (dev == trace_cache_device ? trace_cache_procs :\
50 trace_find_procs(dev))
51
52/* Procedure structure */
53private dev_proc_open_device(trace_open_device);
54private dev_proc_get_initial_matrix(trace_get_initial_matrix);
55private dev_proc_sync_output(trace_sync_output);
56private dev_proc_output_page(trace_output_page);
57private dev_proc_close_device(trace_close_device);
58private dev_proc_map_rgb_color(trace_map_rgb_color);
59private dev_proc_map_color_rgb(trace_map_color_rgb);
60private dev_proc_fill_rectangle(trace_fill_rectangle);
61private dev_proc_tile_rectangle(trace_tile_rectangle);
62private dev_proc_copy_mono(trace_copy_mono);
63private dev_proc_copy_color(trace_copy_color);
64private dev_proc_draw_line(trace_draw_line);
65private dev_proc_get_bits(trace_get_bits);
66private dev_proc_get_props(trace_get_props);
67private dev_proc_put_props(trace_put_props);
68
69private gx_device_procs trace_procs = {
70 trace_open_device,
71 trace_get_initial_matrix,
72 trace_sync_output,
73 trace_output_page,
74 trace_close_device,
75 trace_map_rgb_color,
76 trace_map_color_rgb,
77 trace_fill_rectangle,
78 trace_tile_rectangle,
79 trace_copy_mono,
80 trace_copy_color,
81 trace_draw_line,
82 trace_get_bits,
83 trace_get_props,
84 trace_put_props
85};
86
87/* Find the real procedures for a traced device */
88private gx_device_procs *
89trace_find_procs(gx_device *tdev)
90{ gx_device_procs *tprocs = tdev->procs;
91 register trace_record *tp = trace_list;
92 while ( tp != NULL )
93 { if ( tp->tprocs == tprocs )
94 { trace_cache_device = tdev;
95 return (trace_cache_procs = &tp->procs);
96 }
97 tp = tp->next;
98 }
99 lprintf("Traced procedures not found!\n");
100 gs_exit(1);
101}
102
103/* Trace a device. */
104gx_device *
105gs_trace_device(gx_device *rdev)
106{ trace_record *tp;
107 if ( rdev->procs->open_device == trace_procs.open_device )
108 return rdev; /* already traced */
109 tp = (trace_record *)gs_malloc(1, sizeof(trace_record),
110 "gs_trace_device");
111 if ( tp == 0 ) return 0;
112 tp->next = trace_list;
113 tp->tprocs = rdev->procs;
114 tp->procs = *rdev->procs;
115 tp->index = ++trace_next_index;
116 trace_list = tp;
117 *rdev->procs = trace_procs;
118 return rdev;
119}
120
121/* Utilities */
122private int
123trace_print_code(int result)
124{ if ( result == 0 )
125 dprintf(";\n");
126 else
127 dprintf1(";\t/* = %d */\n", result);
128 return result;
129}
130private void
131trace_print_tile(gx_bitmap *tile)
132{ int i;
133 dprintf1("\t{ static byte data = { 0x%x", tile->data[0]);
134 for ( i = 1; i < tile->raster * tile->size.y; i++ )
135 dprintf1(", 0x%x", tile->data[i]);
136 dprintf4(" };\n\t static gx_bitmap tile = { &data, %d, %d, %d, 0x%lx };\n",
137 tile->raster, tile->size.x, tile->size.y, tile->id);
138}
139
140/* Procedures */
141private int
142trace_open_device(gx_device *dev)
143{ int result = (*rprocs->open_device)(dev);
144if ( gs_debug['V'] )
145 dprintf2("[V]\topen_device(0x%lx /*%s*/)", (ulong)dev, dev->dname),
146 trace_print_code(result);
147 return result;
148}
149private void
150trace_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
151{ (*rprocs->get_initial_matrix)(dev, pmat);
152if ( gs_debug['V'] )
153 dprintf6("[V]\tget_initial_matrix(dev) = (%6g, %6g, %6g, %6g, %6g, %6g);\n",
154 pmat->xx, pmat->xy, pmat->yx, pmat->yy, pmat->tx, pmat->ty);
155}
156private int
157trace_sync_output(gx_device *dev)
158{ int result = (*rprocs->sync_output)(dev);
159if ( gs_debug['V'] )
160 dprintf("[V]\tsync_output(dev)"),
161 trace_print_code(result);
162 return result;
163}
164private int
165trace_output_page(gx_device *dev, int num_copies, int flush)
166{ int result = (*rprocs->output_page)(dev, num_copies, flush);
167if ( gs_debug['V'] )
168 dprintf2("[V]\toutput_page(dev, %d, %d)", num_copies, flush),
169 trace_print_code(result);
170 return result;
171}
172private int
173trace_close_device(gx_device *dev)
174{ int result = (*rprocs->close_device)(dev);
175if ( gs_debug['V'] )
176 dprintf2("[V]\tclose_device(0x%lx /*%s*/)", (ulong)dev, dev->dname),
177 trace_print_code(result);
178 return result;
179}
180private gx_color_index
181trace_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
182 gx_color_value b)
183{ gx_color_index result = (*rprocs->map_rgb_color)(dev, r, g, b);
184if ( gs_debug['V'] )
185 dprintf4("[V]\tmap_rgb_color(dev, %u, %u, %u) /*= %ld */;\n",
186 r, g, b, (long)result);
187 return result;
188}
189private int
190trace_map_color_rgb(gx_device *dev, gx_color_index color,
191 gx_color_value prgb[3])
192{ int result = (*rprocs->map_color_rgb)(dev, color, prgb);
193if ( gs_debug['V'] )
194 dprintf4("[V]\t{ gx_color_value rgb[3]; map_color_rgb(dev, %ld, rgb /* %u, %u, %u */); }",
195 (long)color, prgb[0], prgb[1], prgb[2]),
196 trace_print_code(result);
197 return result;
198}
199private int
200trace_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
201 gx_color_index color)
202{ int result = (*rprocs->fill_rectangle)(dev, x, y, w, h, color);
203if ( gs_debug['V'] )
204 dprintf5("[V]\tfill_rectangle(dev, %d, %d, %d, %d, %ld)",
205 x, y, w, h, (long)color),
206 trace_print_code(result);
207 return result;
208}
209private int
210trace_tile_rectangle(gx_device *dev, gx_bitmap *tile,
211 int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
212 int px, int py)
213{ int result = (*rprocs->tile_rectangle)(dev, tile,
214 x, y, w, h, zero, one, px, py);
215if ( gs_debug['V'] )
216 { trace_print_tile(tile);
217 dprintf8("[V]\t tile_rectangle(dev, &tile, %d, %d, %d, %d, %ld, %ld, %d, %d);\n\t}",
218 x, y, w, h, (long)zero, (long)one, px, py);
219 trace_print_code(result);
220 }
221 return result;
222}
223private int
224trace_copy_mono(gx_device *dev, byte *data,
225 int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
226 gx_color_index zero, gx_color_index one)
227{ int result = (*rprocs->copy_mono)(dev, data,
228 dx, raster, id, x, y, w, h, zero, one);
229if ( gs_debug['V'] )
230 dprintf9("[V]\tcopy_mono(dev, data, %d, %d, 0x%lx, %d, %d, %d, %d, %ld, %ld)",
231 dx, raster, id, x, y, w, h, (long)zero, (long)one),
232 trace_print_code(result);
233 return result;
234}
235private int
236trace_copy_color(gx_device *dev, byte *data,
237 int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h)
238{ int result = (*rprocs->copy_color)(dev, data,
239 dx, raster, id, x, y, w, h);
240if ( gs_debug['V'] )
241 dprintf7("[V]\tcopy_color(dev, data, %d, %d, 0x%lx, %d, %d, %d, %d)",
242 dx, raster, id, x, y, w, h),
243 trace_print_code(result);
244 return result;
245}
246private int
247trace_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
248 gx_color_index color)
249{ int result = (*rprocs->draw_line)(dev, x0, y0, x1, y1, color);
250if ( gs_debug['V'] )
251 dprintf5("[V]\tdraw_line(dev, %d, %d, %d, %d, %ld)",
252 x0, y0, x1, y1, (long)color),
253 trace_print_code(result);
254 return result;
255}
256private int
257trace_get_bits(gx_device *dev, int y, byte *data, uint size, int pad)
258{ int result = (*rprocs->get_bits)(dev, y, data, size, pad);
259if ( gs_debug['V'] )
260 dprintf3("[V]\tget_bits(dev, %d, data, %d, %d)",
261 y, size, pad),
262 trace_print_code(result);
263 return result;
264}
265private int
266trace_get_props(gx_device *dev, gs_prop_item *plist)
267{ int result = (*rprocs->get_props)(dev, plist);
268if ( gs_debug['V'] )
269 dprintf("[V]\tget_props(dev, plist)\n"),
270 trace_print_code(result);
271 return result;
272}
273private int
274trace_put_props(gx_device *dev, gs_prop_item *plist, int count)
275{ int result = (*rprocs->put_props)(dev, plist, count);
276if ( gs_debug['V'] )
277 dprintf1("[V]\tput_props(dev, plist, %d)", count),
278 trace_print_code(result);
279 return result;
280}
281
282#endif