1 /* Copyright (C) 1995-2003 artofcode LLC. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16
17 /* $Id: gdevcgm.c,v 1.10 2004/04/01 04:51:42 dan Exp $ */
18 /* CGM (Computer Graphics Metafile) driver */
19 #include "memory_.h"
20 #include "gx.h"
21 #include "gserrors.h"
22 #include "gxdevice.h"
23 #include "gp.h"
24 #include "gsparam.h"
25 #include "gdevcgml.h"
26 #include "gdevpccm.h"
27
28 /**************** Future optimizations:
29 Do tile_rectangle with pattern
30 Keep track of painted area,
31 do masked copy_mono with cell array if possible
32 ****************/
33
34 typedef struct gx_device_cgm_s {
35 gx_device_common;
36 char fname[gp_file_name_sizeof];
37 FILE *file;
38 cgm_state *st;
39 bool in_picture;
40 } gx_device_cgm;
41
42 /* GC descriptor */
43 gs_private_st_suffix_add1_final(st_device_cgm, gx_device_cgm,
44 "gx_device_cgm", device_cgm_enum_ptrs, device_cgm_reloc_ptrs,
45 gx_device_finalize, st_device, st);
46
47 /* Device procedures */
48 private dev_proc_open_device(cgm_open);
49 private dev_proc_output_page(cgm_output_page);
50 private dev_proc_close_device(cgm_close);
51 private dev_proc_fill_rectangle(cgm_fill_rectangle);
52
53 #if 0
54 private dev_proc_tile_rectangle(cgm_tile_rectangle);
55
56 #else
57 #define cgm_tile_rectangle NULL
58 #endif
59 private dev_proc_copy_mono(cgm_copy_mono);
60 private dev_proc_copy_color(cgm_copy_color);
61 private dev_proc_get_params(cgm_get_params);
62 private dev_proc_put_params(cgm_put_params);
63
64 /* In principle, all the drawing operations should be polymorphic, */
65 /* but it's just as easy just to test the depth, since we're not */
66 /* very concerned about performance. */
67 #define cgm_device(dname, depth, max_value, dither, map_rgb_color, map_color_rgb)\
68 { std_device_color_stype_body(gx_device_cgm, 0, dname, &st_device_cgm,\
69 850, 1100, 100, 100, depth, max_value, dither),\
70 { cgm_open,\
71 NULL, /* get_initial_matrix */\
72 NULL, /* sync_output */\
73 cgm_output_page,\
74 cgm_close,\
75 map_rgb_color,\
76 map_color_rgb,\
77 cgm_fill_rectangle,\
78 cgm_tile_rectangle,\
79 cgm_copy_mono,\
80 cgm_copy_color,\
81 NULL, /* draw_line */\
82 NULL, /* get_bits */\
83 cgm_get_params,\
84 cgm_put_params\
85 },\
86 { 0 }, /* fname */\
87 0, /* file */\
88 0, /* st */\
89 0 /*false*/ /* in_picture */\
90 }
91
92 gx_device_cgm gs_cgmmono_device =
93 cgm_device("cgmmono", 1, 1, 2,
94 gx_default_map_rgb_color, gx_default_w_b_map_color_rgb);
95
96 gx_device_cgm gs_cgm8_device =
97 cgm_device("cgm8", 8, 5, 6,
98 pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
99
100 gx_device_cgm gs_cgm24_device =
101 cgm_device("cgm24", 24, 255, 255,
102 gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
103
104 /* Define allocator procedures for the CGM library. */
105 private void *
cgm_gs_alloc(void * private_data,uint size)106 cgm_gs_alloc(void *private_data, uint size)
107 {
108 gx_device_cgm *cdev = private_data;
109
110 return gs_alloc_bytes(cdev->memory, size, "cgm_gs_alloc");
111 }
112 private void
cgm_gs_free(void * private_data,void * obj)113 cgm_gs_free(void *private_data, void *obj)
114 {
115 gx_device_cgm *cdev = private_data;
116
117 gs_free_object(cdev->memory, obj, "cgm_gs_free");
118 }
119
120 /* ---------------- Utilities ---------------- */
121
122 /* Convert a CGM result code to our error values. */
123 private int
cgm_error_code(cgm_result result)124 cgm_error_code(cgm_result result)
125 {
126 switch (result) {
127 default:
128 case cgm_result_wrong_state:
129 return gs_error_unknownerror;
130 case cgm_result_out_of_range:
131 return gs_error_rangecheck;
132 case cgm_result_io_error:
133 return gs_error_ioerror;
134 }
135 }
136 #define check_result(result)\
137 if ( result != cgm_result_ok ) return_error(cgm_error_code(result))
138
139 /* ---------------- Device control ---------------- */
140
141 /* Open the device */
142 private int
cgm_open(gx_device * dev)143 cgm_open(gx_device * dev)
144 {
145 gx_device_cgm *cdev = (gx_device_cgm *) dev;
146 cgm_allocator cal;
147 static const int elements[] =
148 {-1, 1};
149 cgm_metafile_elements meta;
150 cgm_result result;
151
152 cdev->file = fopen(cdev->fname, "wb");
153 if (cdev->file == 0)
154 return_error(gs_error_ioerror);
155 cal.private_data = cdev;
156 cal.alloc = cgm_gs_alloc;
157 cal.free = cgm_gs_free;
158 cdev->st = cgm_initialize(cdev->file, &cal);
159 if (cdev->st == 0)
160 return_error(gs_error_VMerror);
161 result = cgm_BEGIN_METAFILE(cdev->st, "", 0);
162 check_result(result);
163 meta.metafile_version = 1;
164 meta.vdc_type = cgm_vdc_integer;
165 meta.integer_precision = sizeof(cgm_int) * 8;
166 meta.index_precision = sizeof(cgm_int) * 8;
167 meta.color_precision = 8;
168 /* If we use color indices at all, they are only 1 byte. */
169 meta.color_index_precision = 8;
170 meta.maximum_color_index = (1L << cdev->color_info.depth) - 1;
171 meta.metafile_element_list = elements,
172 meta.metafile_element_list_count = countof(elements) / 2;
173 result = cgm_set_metafile_elements(cdev->st, &meta,
174 cgm_set_METAFILE_VERSION |
175 cgm_set_VDC_TYPE |
176 cgm_set_INTEGER_PRECISION |
177 cgm_set_INDEX_PRECISION |
178 cgm_set_COLOR_PRECISION |
179 cgm_set_COLOR_INDEX_PRECISION |
180 cgm_set_MAXIMUM_COLOR_INDEX |
181 cgm_set_METAFILE_ELEMENT_LIST);
182 check_result(result);
183 cdev->in_picture = false;
184 return 0;
185 }
186
187 /* Output a page */
188 private int
cgm_output_page(gx_device * dev,int num_copies,int flush)189 cgm_output_page(gx_device * dev, int num_copies, int flush)
190 {
191 gx_device_cgm *cdev = (gx_device_cgm *) dev;
192
193 if (cdev->in_picture) {
194 cgm_result result = cgm_END_PICTURE(cdev->st);
195
196 check_result(result);
197 cdev->in_picture = false;
198 return gx_finish_output_page(dev, num_copies, flush);
199 }
200 return 0;
201 }
202
203 /* Close the device */
204 private int
cgm_close(gx_device * dev)205 cgm_close(gx_device * dev)
206 {
207 gx_device_cgm *cdev = (gx_device_cgm *) dev;
208 int code = cgm_output_page(dev, 1, 0);
209 cgm_result result;
210
211 if (code < 0)
212 return code;
213 result = cgm_END_METAFILE(cdev->st);
214 check_result(result);
215 result = cgm_terminate(cdev->st);
216 check_result(result);
217 cdev->st = 0;
218 fclose(cdev->file);
219 cdev->file = 0;
220 return 0;
221 }
222
223 /* Get parameters. CGM devices add OutputFile to the default set. */
224 private int
cgm_get_params(gx_device * dev,gs_param_list * plist)225 cgm_get_params(gx_device * dev, gs_param_list * plist)
226 {
227 gx_device_cgm *cdev = (gx_device_cgm *) dev;
228 int code = gx_default_get_params(dev, plist);
229 gs_param_string ofns;
230
231 if (code < 0)
232 return code;
233 ofns.data = (const byte *)cdev->fname,
234 ofns.size = strlen(cdev->fname),
235 ofns.persistent = false;
236 return param_write_string(plist, "OutputFile", &ofns);
237 }
238
239 /* Put parameters. */
240 private int
cgm_put_params(gx_device * dev,gs_param_list * plist)241 cgm_put_params(gx_device * dev, gs_param_list * plist)
242 {
243 gx_device_cgm *cdev = (gx_device_cgm *) dev;
244 int ecode = 0;
245 int code;
246 const char *param_name;
247 gs_param_string ofs;
248
249 switch (code = param_read_string(plist, (param_name = "OutputFile"), &ofs)) {
250 case 0:
251 if (dev->LockSafetyParams &&
252 bytes_compare(ofs.data, ofs.size,
253 (const byte *)cdev->fname, strlen(cdev->fname))) {
254 ecode = gs_note_error(gs_error_invalidaccess);
255 goto ofe;
256 }
257 if (ofs.size >= gp_file_name_sizeof)
258 ecode = gs_error_limitcheck;
259 else
260 break;
261 goto ofe;
262 default:
263 ecode = code;
264 ofe:param_signal_error(plist, param_name, ecode);
265 case 1:
266 ofs.data = 0;
267 break;
268 }
269
270 if (ecode < 0)
271 return ecode;
272 code = gx_default_put_params(dev, plist);
273 if (code < 0)
274 return code;
275
276 if (ofs.data != 0) { /* Close the file if it's open. */
277 if (cdev->file != 0) {
278 fclose(cdev->file);
279 cdev->file = 0;
280 }
281 memcpy(cdev->fname, ofs.data, ofs.size);
282 cdev->fname[ofs.size] = 0;
283 cdev->file = fopen(cdev->fname, "wb");
284 if (cdev->file == 0)
285 return_error(gs_error_ioerror);
286 }
287 return 0;
288 }
289
290 /* ---------------- Drawing ---------------- */
291
292 /* Set the corner points for a rectangle. It appears (although */
293 /* this is not obvious from the CGM specification) that rectangles */
294 /* are specified with closed, rather than half-open, intervals. */
295 #define cgm_set_rect(points, xo, yo, w, h)\
296 points[1].integer.x = (points[0].integer.x = xo) + (w) - 1,\
297 points[1].integer.y = (points[0].integer.y = yo) + (h) - 1
298
299 /* Set the points for a cell array. */
300 #define cgm_set_cell_points(pqr, xo, yo, w, h)\
301 pqr[0].integer.x = (xo),\
302 pqr[0].integer.y = (yo),\
303 pqr[1].integer.x = (xo) + (w),\
304 pqr[1].integer.y = (yo) + (h),\
305 pqr[2].integer.x = (xo) + (w),\
306 pqr[2].integer.y = (yo)
307
308 /* Begin a picture if necessary. */
309 #define begin_picture(cdev)\
310 if ( !cdev->in_picture ) cgm_begin_picture(cdev)
311 private int
cgm_begin_picture(gx_device_cgm * cdev)312 cgm_begin_picture(gx_device_cgm * cdev)
313 {
314 cgm_picture_elements pic;
315 cgm_result result;
316 cgm_edge_width edge;
317
318 result = cgm_BEGIN_PICTURE(cdev->st, "", 0);
319 check_result(result);
320 pic.scaling_mode = cgm_scaling_abstract;
321 pic.color_selection_mode =
322 (cdev->color_info.depth <= 8 ?
323 cgm_color_selection_indexed :
324 cgm_color_selection_direct);
325 pic.line_width_specification_mode = cgm_line_marker_absolute;
326 pic.edge_width_specification_mode = cgm_line_marker_absolute;
327 cgm_set_rect(pic.vdc_extent, 0, 0, cdev->width, cdev->height);
328 result = cgm_set_picture_elements(cdev->st, &pic,
329 cgm_set_SCALING_MODE |
330 cgm_set_COLOR_SELECTION_MODE |
331 cgm_set_LINE_WIDTH_SPECIFICATION_MODE |
332 cgm_set_EDGE_WIDTH_SPECIFICATION_MODE |
333 cgm_set_VDC_EXTENT);
334 check_result(result);
335 result = cgm_BEGIN_PICTURE_BODY(cdev->st);
336 check_result(result);
337 result = cgm_VDC_INTEGER_PRECISION(cdev->st,
338 (cdev->width <= 0x7fff &&
339 cdev->height <= 0x7fff ?
340 16 : sizeof(cdev->width) * 8));
341 check_result(result);
342 edge.absolute.integer = 0;
343 result = cgm_EDGE_WIDTH(cdev->st, &edge);
344 check_result(result);
345 if (cdev->color_info.depth <= 8) {
346 cgm_color colors[256];
347 int i;
348
349 for (i = 0; i < (1 << cdev->color_info.depth); i++) {
350 gx_color_value rgb[3];
351
352 (*dev_proc(cdev, map_color_rgb)) ((gx_device *) cdev,
353 (gx_color_index) i, rgb);
354 colors[i].rgb.r =
355 rgb[0] >> (gx_color_value_bits - 8);
356 colors[i].rgb.g =
357 rgb[1] >> (gx_color_value_bits - 8);
358 colors[i].rgb.b =
359 rgb[2] >> (gx_color_value_bits - 8);
360 }
361 result = cgm_COLOR_TABLE(cdev->st, 0, colors,
362 1 << cdev->color_info.depth);
363 check_result(result);
364 }
365 cdev->in_picture = true;
366 return 0;
367 }
368
369 /* Convert a gx_color_index to a CGM color. */
370 private void
cgm_color_from_color_index(cgm_color * pcc,const gx_device_cgm * cdev,gx_color_index color)371 cgm_color_from_color_index(cgm_color * pcc, const gx_device_cgm * cdev,
372 gx_color_index color)
373 {
374 if (cdev->color_info.depth <= 8)
375 pcc->index = color;
376 else {
377 pcc->rgb.r = color >> 16;
378 pcc->rgb.g = (color >> 8) & 255;
379 pcc->rgb.b = color & 255;
380 }
381 }
382
383 /* Fill a rectangle. */
384 private int
cgm_fill_rectangle(gx_device * dev,int x,int y,int w,int h,gx_color_index color)385 cgm_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
386 gx_color_index color)
387 {
388 gx_device_cgm *cdev = (gx_device_cgm *) dev;
389 cgm_color fill_color;
390 cgm_point points[2];
391 cgm_result result;
392
393 fit_fill(dev, x, y, w, h);
394 if (!cdev->in_picture) { /* Check for erasepage. */
395 gx_color_value blank[3] = {gx_max_color_value, gx_max_color_value,
396 gx_max_color_value};
397 if (color == (*dev_proc(dev, encode_color)) (dev, blank))
398 return 0;
399 cgm_begin_picture(cdev);
400 }
401 cgm_color_from_color_index(&fill_color, cdev, color);
402 result = cgm_FILL_COLOR(cdev->st, &fill_color);
403 check_result(result);
404 result = cgm_INTERIOR_STYLE(cdev->st, cgm_interior_style_solid);
405 check_result(result);
406 cgm_set_rect(points, x, y, w, h);
407 result = cgm_RECTANGLE(cdev->st, &points[0], &points[1]);
408 check_result(result);
409 return 0;
410 }
411
412 #if 0
413 /* Tile a rectangle. We should do this with a pattern if possible. */
414 private int
415 cgm_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
416 int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
417 int px, int py)
418 {
419 }
420 #endif
421
422 /* Copy a monochrome bitmap. Unfortunately, CGM doesn't provide a */
423 /* masked fill operation; if one of the colors is transparent, */
424 /* we have to do the copy by filling lots of tiny little rectangles. */
425 /* A much better way to implement this would be to remember whether */
426 /* the destination region is still white; if so, we can use a cell array */
427 /* (or, even better, a pattern). However, we still need the slow method */
428 /* for the case where we don't know the background color or it isn't white. */
429 private int
cgm_copy_mono(gx_device * dev,const byte * base,int sourcex,int raster,gx_bitmap_id id,int x,int y,int w,int h,gx_color_index zero,gx_color_index one)430 cgm_copy_mono(gx_device * dev,
431 const byte * base, int sourcex, int raster, gx_bitmap_id id,
432 int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
433 {
434 gx_device_cgm *cdev = (gx_device_cgm *) dev;
435
436 /* The current implementation is about as inefficient as */
437 /* one could possibly imagine! */
438 int ix, iy;
439 cgm_result result;
440
441 fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
442 begin_picture(cdev);
443 if (zero == 0 && one == 1 && cdev->color_info.depth == 1) {
444 cgm_point pqr[3];
445
446 cgm_set_cell_points(pqr, x, y, w, h);
447 result = cgm_CELL_ARRAY(cdev->st, pqr, w, h, 1,
448 cgm_cell_mode_packed,
449 base, sourcex, raster);
450 check_result(result);
451 } else {
452 result = cgm_INTERIOR_STYLE(cdev->st, cgm_interior_style_solid);
453 check_result(result);
454 for (iy = 0; iy < h; iy++)
455 for (ix = 0; ix < w; ix++) {
456 int px = ix + sourcex;
457 const byte *pixel = &base[iy * raster + (px >> 3)];
458 byte mask = 0x80 >> (px & 7);
459 gx_color_index color = (*pixel & mask ? one : zero);
460
461 if (color != gx_no_color_index) {
462 cgm_color fill_color;
463 cgm_point points[2];
464
465 cgm_color_from_color_index(&fill_color, cdev, color);
466 cgm_set_rect(points, x, y, 1, 1);
467 result = cgm_RECTANGLE(cdev->st, &points[0], &points[1]);
468 check_result(result);
469 }
470 }
471 }
472 return 0;
473 }
474
475 /* Copy a color bitmap. */
476 private int
cgm_copy_color(gx_device * dev,const byte * base,int sourcex,int raster,gx_bitmap_id id,int x,int y,int w,int h)477 cgm_copy_color(gx_device * dev,
478 const byte * base, int sourcex, int raster, gx_bitmap_id id,
479 int x, int y, int w, int h)
480 {
481 gx_device_cgm *cdev = (gx_device_cgm *) dev;
482 int depth = cdev->color_info.depth;
483 uint source_bit = sourcex * depth;
484 cgm_point pqr[3];
485 cgm_result result;
486
487 if (depth == 1)
488 return cgm_copy_mono(dev, base, sourcex, raster, id,
489 x, y, w, h,
490 (gx_color_index) 0, (gx_color_index) 1);
491 fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
492 begin_picture(cdev);
493 cgm_set_cell_points(pqr, x, y, w, h);
494 result = cgm_CELL_ARRAY(cdev->st, pqr, w, h, 0, cgm_cell_mode_packed,
495 base, source_bit, raster);
496 check_result(result);
497 return 0;
498 }
499