-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-cgm.c
284 lines (220 loc) · 6.93 KB
/
output-cgm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* output-cgm.c: CGM output
Copyright (C) 1999, 2000, 2001 Martin Weber.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* Def: HAVE_CONFIG_H */
#include "types.h"
#include "spline.h"
#include "color.h"
#include "output-cgm.h"
#include "xstd.h"
#include "autotrace.h"
#include <string.h>
#define CGM_BEGINMETAFILE 0x0020
#define CGM_BEGINPICTURE 0x0060
#define CGM_METAFILEVERSION 0x1022
#define CGM_METAFILEDESCRIPTION 0x1040
typedef unsigned short int UI16;
typedef unsigned char UI8;
/* endianess independent IO functions */
static at_bool write16(FILE *fdes, UI16 data)
{
size_t count = 0;
UI8 outch;
outch = (UI8) ((data >> 8) & 0x0FF);
count += fwrite(&outch, 1, 1, fdes);
outch = (UI8) (data & 0x0FF);
count += fwrite(&outch, 1, 1, fdes);
return (count == sizeof(UI16)) ? true : false;
}
static at_bool write8(FILE *fdes, UI8 data)
{
size_t count = 0;
count = fwrite(&data, 1, 1, fdes);
return (count == sizeof(UI8)) ? true : false;
}
static at_bool output_beginmetafilename(FILE *fdes, const char *string)
{
int len = strlen (string);
if (len + 1 < 0x001F)
write16(fdes, CGM_BEGINMETAFILE + len + 1);
else
{
write16(fdes, CGM_BEGINMETAFILE + 0x001F);
write16(fdes, len + 1);
}
write8(fdes, len);
while (*string != '\0')
{
write8(fdes, *string);
string++;
}
if (len % 2 == 0)
write8(fdes, 0);
return true;
}
static at_bool output_beginpicture(FILE *fdes, const char *string)
{
int len = strlen (string);
if (len + 1 < 0x001F)
write16(fdes, CGM_BEGINPICTURE + len + 1);
else
{
write16(fdes, CGM_BEGINPICTURE + 0x001F);
write16(fdes, len + 1);
}
write8(fdes, len);
while (*string != '\0')
{
write8(fdes, *string);
string++;
}
if (len % 2 == 0)
write8(fdes, 0);
return true;
}
static at_bool output_metafiledescription(FILE *fdes, const char *string)
{
int len = strlen (string);
if (len + 1 < 0x001F)
write16(fdes, CGM_METAFILEDESCRIPTION + len + 1);
else
{
write16(fdes, CGM_METAFILEDESCRIPTION + 0x001F);
write16(fdes, len + 1);
}
write8(fdes, len);
while (*string != '\0')
{
write8(fdes, *string);
string++;
}
if (len % 2 == 0)
write8(fdes, 0);
return true;
}
int output_cgm_writer(FILE* cgm_file, at_string name,
int llx, int lly, int urx, int ury,
at_output_opts_type * opts,
spline_list_array_type shape,
at_msg_func msg_func,
at_address msg_data)
{
unsigned this_list;
char *des;
const char * version_string = at_version(true);
output_beginmetafilename (cgm_file, name);
write16 (cgm_file, CGM_METAFILEVERSION);
write16 (cgm_file, 0x0002);
des = (char *) malloc (strlen ("created by ") + strlen (version_string) + 1);
strcpy (des, "created by ");
strcat (des, version_string);
output_metafiledescription (cgm_file, des);
free (des);
write16 (cgm_file, 0x1166); /* metafile element list */
write16 (cgm_file, 0x0001);
write16 (cgm_file, 0xFFFF);
write16 (cgm_file, 0x0001);
output_beginpicture(cgm_file, "pic1");
write16 (cgm_file, 0x2042);/* color selection modes (1 = direct) */
write16 (cgm_file, 0x0001);
write16 (cgm_file, 0x20C8);/* vdc extend */
write16 (cgm_file, llx/*0x0000*/);
write16 (cgm_file, urx/*0x7FFF*/);
write16 (cgm_file, ury/*0x7FFF*/);
write16 (cgm_file, lly/*0x0000*/);
write16 (cgm_file, 0x0080);/* begin picture body */
for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH (shape);
this_list++)
{
unsigned this_spline;
spline_list_type list = SPLINE_LIST_ARRAY_ELT (shape, this_list);
if (this_list > 0)
{
if (shape.centerline)
write16 (cgm_file, 0x0200); /* end a compound line */
else
write16 (cgm_file, 0x0120); /* end a figure */
}
if (shape.centerline)
write16 (cgm_file, 0x5083);/* line */
else
write16 (cgm_file, 0x52E3);/* fill */;
/* color output */
if (list.clockwise && shape.background_color != NULL)
{
write8 (cgm_file, shape.background_color->r);
write8 (cgm_file, shape.background_color->g);
write8 (cgm_file, shape.background_color->b);
}
else
{
write8 (cgm_file, list.color.r);
write8 (cgm_file, list.color.g);
write8 (cgm_file, list.color.b);
}
write8 (cgm_file, 0);
if (shape.centerline)
{
write16 (cgm_file, 0x53C2);/* edge visibility on */
write16 (cgm_file, 0x0001);
}
else
{
write16 (cgm_file, 0x52C2);/* interior style solid */
write16 (cgm_file, 0x0001);
}
if (shape.centerline)
write16 (cgm_file, 0x01E0); /* begin a compound line */
else
write16 (cgm_file, 0x0100); /* begin a figure */
for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (list);
this_spline++)
{
spline_type s = SPLINE_LIST_ELT (list, this_spline);
if (SPLINE_DEGREE (s) == LINEARTYPE)
{
write16 (cgm_file, 0x4028); /* polyline */
write16 (cgm_file, (UI16) START_POINT (s).x);
write16 (cgm_file, ury - (UI16) START_POINT (s).y);
write16 (cgm_file, (UI16) END_POINT (s).x);
write16 (cgm_file, ury - (UI16) END_POINT (s).y);
}
else
{
write16 (cgm_file, 0x4352); /* polybezier */
write16 (cgm_file, 0x0002); /* continuous */
write16 (cgm_file, (UI16) START_POINT (s).x);
write16 (cgm_file, ury - (UI16) START_POINT (s).y);
write16 (cgm_file, (UI16) CONTROL1 (s).x);
write16 (cgm_file, ury - (UI16) CONTROL1 (s).y);
write16 (cgm_file, (UI16) CONTROL2 (s).x);
write16 (cgm_file, ury - (UI16) CONTROL2 (s).y);
write16 (cgm_file, (UI16) END_POINT (s).x);
write16 (cgm_file, ury - (UI16) END_POINT (s).y);
}
}
}
if (SPLINE_LIST_ARRAY_LENGTH(shape) > 0)
{
if (shape.centerline)
write16 (cgm_file, 0x0200); /* end a compound line */
else
write16 (cgm_file, 0x0120); /* end a figure */
}
write16 (cgm_file, 0x00A0); /* end picture */
write16 (cgm_file, 0x0040); /* end metafile */
return(0);
}