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