Annotation of OpenXM_contrib/gnuplot/term/gif.trm, Revision 1.1.1.1
1.1 maekawa 1: /*
2: * $Id: $
3: */
4:
5: /* GNUPLOT -- gif.trm */
6:
7: /*[
8: * Copyright 1998
9: *
10: * Permission to use, copy, and distribute this software and its
11: * documentation for any purpose with or without fee is hereby granted,
12: * provided that the above copyright notice appear in all copies and
13: * that both that copyright notice and this permission notice appear
14: * in supporting documentation.
15: *
16: * Permission to modify the software is granted, but not the right to
17: * distribute the complete modified source code. Modifications are to
18: * be distributed as patches to the released version. Permission to
19: * distribute binaries produced by compiling modified sources is granted,
20: * provided you
21: * 1. distribute the corresponding source modifications from the
22: * released version in the form of a patch file along with the binaries,
23: * 2. add special version identification to distinguish your version
24: * in addition to the base release version number,
25: * 3. provide your name and address as the primary contact for the
26: * support of your modified version, and
27: * 4. retain our contact information in regard to use of the base
28: * software.
29: * Permission to distribute the released version of the source code along
30: * with corresponding source modifications in the form of a patch file is
31: * granted with same provisions 2 through 4 for binary distributions.
32: *
33: * This software is provided "as is" without express or implied warranty
34: * to the extent permitted by applicable law.
35: ]*/
36:
37: /*
38: * This file is included by ../term.c.
39: *
40: * This terminal driver supports:
41: * GD GIF library 1.2 & 1.3
42: *
43: * To Use:
44: *
45: * set terminal gif ?options ...?
46: *
47: * Where an option is:
48: *
49: * transparent - generate transparent GIFs. The first color will
50: * be the transparent one.
51: *
52: * interlace - generate interlaced GIFs.
53: *
54: * size (in pixels)
55: *
56: * font (small,medium,large)
57: *
58: * xrrggbb - sets the next color. x is the literal character 'x',
59: * rrggbb are the red green and blue components in hex. For example
60: * x00ff00 is green. The background color is set first, then the
61: * color borders, then the X & Y axis, then the plotting colors.
62: * (The wierd color spec is in order to get around limitations
63: * in gnuplot's scanner.)
64: *
65: * This driver is modeled after the PBM driver pbm.trm.
66: *
67: * AUTHORS
68: * Sam Shen <sls@mh1.lbl.gov>
69: * Alex Woo <woo@playfair.stanford.edu>
70: *
71: * CONTRIBUTORS
72: * Alfred Reibenschuh <alfred.reibenschuh@cait.co.at> or <fredo@blackbox.at>
73: *
74: * send your comments or suggestions to:
75: * info-gnuplot@cs.dartmouth.edu
76: *
77: * This version outputs either color or monochrome GIFs. The default
78: * is 640x480 pixels.
79: *
80: * link with -Lterm/gd -lgd if your directory structure is gnuplot/term/gd
81: *
82: * gd is not distributed with gnuplot, because of the UNISYS license thing.
83: *
84: * find out about gd from http://www.boutell.com/gd/
85: *
86: * [Update: as of version 1.3, gd does not use any more UNISYS licensed
87: * code. The gnuplot team may decide to include this version some day.]
88: *
89: */
90:
91: #include "driver.h"
92:
93: #ifdef TERM_REGISTER
94: register_term(gif)
95: #endif
96:
97: #ifdef TERM_PROTO
98: TERM_PUBLIC void GIF_options __PROTO((void));
99: TERM_PUBLIC void GIF_init __PROTO((void));
100: TERM_PUBLIC void GIF_graphics __PROTO((void));
101: TERM_PUBLIC void GIF_text __PROTO((void));
102: TERM_PUBLIC void GIF_linetype __PROTO((int linetype));
103: TERM_PUBLIC void GIF_move __PROTO((unsigned int x, unsigned int y));
104: TERM_PUBLIC void GIF_vector __PROTO((unsigned int x, unsigned int y));
105: TERM_PUBLIC void GIF_put_text __PROTO((unsigned int x, unsigned int y, char str[]));
106: TERM_PUBLIC int GIF_text_angle __PROTO((int ang));
107: TERM_PUBLIC void GIF_reset __PROTO((void));
108:
109: #include "gd.h"
110: extern gdFontPtr gdFontSmall; /* 6x12 */
111: extern gdFontPtr gdFontLarge; /* 8x16 */
112: extern gdFontPtr gdFontMediumBold; /* 7x13 */
113:
114:
115: #define GREG_XMAX 640
116: #define GREG_YMAX 480
117:
118:
119: int GIF_XMAX = GREG_XMAX;
120: int GIF_YMAX = GREG_YMAX;
121:
122:
123: #define GIF_FONT_SMALL 1
124: #ifdef GIF_FONT_SMALL
125: # define gdfont gdFontSmall
126: # define GIF_VCHAR 12
127: # define GIF_HCHAR 6
128: #else
129: # define gdfont gdFontMediumBold
130: # define GIF_VCHAR 13
131: # define GIF_HCHAR 7
132: #endif
133:
134: static gdFontPtr GIF_font;
135:
136: #define GIF_VTIC (GREG_YMAX/100)
137: #define GIF_HTIC (GREG_XMAX/150)
138:
139: #define GIF_MAX_COLORS 256
140: #define GOT_NEXT_PROTO
141: #endif
142:
143: #ifndef TERM_PROTO_ONLY
144: #ifdef TERM_BODY
145:
146: static struct {
147: gdImagePtr image;
148: gdFontPtr font;
149: unsigned int x, y;
150: int height;
151: int charh, charw;
152: int color;
153: int n_colors;
154: int color_table[GIF_MAX_COLORS];
155: int rgb_table[GIF_MAX_COLORS];
156: int angle;
157: int flags;
158: int linetype;
159: } gif_state;
160:
161: #define GIF_USE_TRANSPARENT 1
162: #define GIF_USE_INTERLACE 2
163:
164: static unsigned int gif_color_rgbs[] =
165: {
166: 0xffffff, /* background: white */
167: 0x000000, /* borders: black */
168: 0x404040, /* x & y axes: grey */
169: 0xff0000, /* color 01: red */
170: 0x00c000, /* color 02: dark green */
171: 0x0080ff, /* color 03: dark blue */
172: 0xc000ff, /* color 04: dark magenta */
173: 0xc0ff40, /* color 05: yellow */
174: 0xc04000, /* color 06: orange */
175: 0x40ff80, /* color 07: sea green */
176: 0x2020c0, /* color 08: royal blue */
177: 0x8000c0, /* color 09: dark violet */
178: /* please note: these colors are optimized for web216 compatibility */
179: 0x006080, /* DeepSkyBlue4 */
180: 0x008000, /* green4 */
181: 0x008040, /* SpringGreen4 */
182: 0x008080, /* dark cyan, turquoise4 */
183: 0x00c060, /* SpringGreen3 */
184: 0x00c0c0, /* cyan3, turquoise3 */
185: 0x00ff00, /* green */
186: 0x208020, /* forest green */
187: 0x306080, /* SteelBlue4 */
188: 0x404040, /* grey25-31 */
189: 0x408000, /* chartreuse4 */
190: 0x000080, /* dark blue, navy blue */
191: 0x806000, /* DarkGoldenrod4 */
192: 0x806010, /* goldenrod4 */
193: 0x806060, /* pink4 */
194: 0x806080, /* plum4 */
195: 0x0000c0, /* medium blue */
196: 0x0000ff, /* blue */
197: 0x006000, /* dark green */
198: 0x40c080, /* SeaGreen3 */
199: 0x60a0c0, /* SkyBlue3 */
200: 0x60c000, /* chartreuse3 */
201: 0x60c0a0, /* medium aquamarine */
202: 0x800000, /* dark red */
203: 0x800080, /* dark magenta */
204: 0x602080, /* DarkOrchid4 */
205: 0x606060, /* dim grey */
206: 0x00ffff, /* cyan1, turquoise1 */
207: 0x202020, /* grey13-18 */
208: 0x204040, /* dark slate grey */
209: 0x204080, /* RoyalBlue4 */
210: 0x608020, /* olive drab */
211: 0x608060, /* DarkSeaGreen4 */
212: 0x608080, /* LightBlue4, PaleTurquoise4 */
213: 0x808040, /* LightGoldenrod4, khaki4 */
214: 0x808080, /* grey51-56 */
215: 0xa0a0a0, /* dark grey, grey63-68 */
216: 0xa0d0e0, /* light blue */
217: 0xc02020, /* firebrick3 */
218: 0xc06000, /* DarkOrange3 */
219: 0x80c0e0, /* sky blue */
220: 0xc060c0, /* orchid3 */
221: 0xc08000, /* orange3 */
222: 0xc08060, /* LightSalmon3 */
223: 0xff4000, /* orange red */
224: 0xff4040, /* brown1, tomato */
225: 0x80c0ff, /* light sky blue */
226: 0xff8060, /* salmon */
227: 0xff8080, /* light coral */
228: 0xc0a000, /* gold3 */
229: 0xc0c0c0, /* grey76-81, honeydew3, ivory3, snow3 */
230: 0xc0ffc0, /* DarkSeaGreen1 */
231: 0xff0000, /* red */
232: 0xff00ff, /* magenta */
233: 0xff80a0, /* PaleVioletRed1 */
234: 0xff80ff, /* orchid1 */
235: 0xc0c0a0, /* LemonChiffon3 */
236: 0xff6060, /* IndianRed1 */
237: 0xff8000, /* dark orange */
238: 0xffa000, /* orange */
239: 0x80e0e0, /* CadetBlue2, DarkSlateGray2 */
240: 0xa0e0e0, /* pale turquoise */
241: 0xa0ff20, /* green yellow */
242: 0xc00000, /* red3 */
243: 0xc000c0, /* magenta3 */
244: 0xa02020, /* brown */
245: 0xa020ff, /* purple */
246: 0x802000, /* OrangeRed4 */
247: 0x802020, /* brown4 */
248: 0x804000, /* DarkOrange4 */
249: 0x804020, /* sienna4 */
250: 0x804080, /* orchid4 */
251: 0x8060c0, /* MediumPurple3 */
252: 0x8060ff, /* SlateBlue1 */
253: 0x808000, /* yellow4 */
254: 0xa080ff, /* MediumPurple1 */
255: 0xc06080, /* PaleVioletRed3 */
256: 0xc0c000, /* yellow3 */
257: 0xff8040, /* sienna1 */
258: 0xffa040, /* tan1 */
259: 0xffa060, /* sandy brown */
260: 0xffa070, /* light salmon */
261: 0xffc020, /* goldenrod1 */
262: 0xffc0c0, /* RosyBrown1, pink */
263: 0xffff00, /* yellow */
264: 0xffff80, /* khaki1 */
265: 0xffffc0 /* lemon chiffon */
266: };
267: #define GIF_N_DEFAULT_COLORS (sizeof(gif_color_rgbs)/sizeof(gif_color_rgbs[0]))
268:
269: /*
270: * _options() Called when terminal type is selected.
271: * This procedure should parse options on the command line. A list of the
272: * currently selected options should be stored in term_options[] in a form
273: * suitable for use with the set term command. term_options[] is used by
274: * the save command. Use options_null() if no options are available.
275: */
276: TERM_PUBLIC void GIF_options()
277: {
278: int gif_font, i;
279: term_options[0] = NUL;
280: gif_state.n_colors = 0;
281: gif_state.flags = 0;
282: gif_font = 1;
283: GIF_font = gdfont;
284:
285: while (!END_OF_COMMAND) {
286: if (almost_equals(c_token, "t$ransparent")) {
287: gif_state.flags |= GIF_USE_TRANSPARENT;
288: ++c_token;
289: } else if (almost_equals(c_token, "i$nterlace")) {
290: gif_state.flags |= GIF_USE_INTERLACE;
291: ++c_token;
292: } else if (almost_equals(c_token, "s$mall")) {
293: GIF_font = gdFontSmall;
294: gif_font = 1;
295: term->v_char = (unsigned int) (12);
296: term->h_char = (unsigned int) (6);
297: ++c_token;
298: } else if (almost_equals(c_token, "m$edium")) {
299: GIF_font = gdFontMediumBold;
300: gif_font = 2;
301: term->v_char = (unsigned int) (13);
302: term->h_char = (unsigned int) (7);
303: ++c_token;
304: } else if (almost_equals(c_token, "l$arge")) {
305: GIF_font = gdFontLarge;
306: gif_font = 3;
307: term->v_char = (unsigned int) (16);
308: term->h_char = (unsigned int) (8);
309: ++c_token;
310: } else if (almost_equals(c_token, "si$ze")) {
311: struct value s;
312: c_token++;
313: if (END_OF_COMMAND) {
314: GIF_XMAX = GREG_XMAX;
315: GIF_YMAX = GREG_YMAX;
316: term->v_tic = GIF_YMAX / 80;
317: term->h_tic = GIF_XMAX / 80;
318: } else {
319: GIF_XMAX = real(const_express(&s));
320: if (equals(c_token, ",")) {
321: c_token++;
322: GIF_YMAX = real(const_express(&s));
323: term->v_tic = GIF_YMAX / 80;
324: term->h_tic = GIF_XMAX / 80;
325: term->ymax = GIF_YMAX;
326: term->xmax = GIF_XMAX;
327: }
328: }
329: } else { /* not "size" */
330: char *string = input_line + token[c_token].start_index;
331: unsigned long color;
332: if (sscanf(string, "x%lx", &color) != 1) {
333: int_error("invalid color spec, must be xRRGGBB", c_token);
334: } else if (gif_state.n_colors == GIF_MAX_COLORS) {
335: int_warn("too many colors, ingoring", c_token);
336: ++c_token;
337: } else {
338: gif_state.rgb_table[gif_state.n_colors++] = color;
339: ++c_token;
340: }
341: }
342: }
343:
344:
345: /* now generate options string */
346:
347: if (gif_state.flags & GIF_USE_TRANSPARENT) {
348: strcat(term_options, "transparent ");
349: }
350: if (gif_state.flags & GIF_USE_INTERLACE) {
351: strcat(term_options, "interlace ");
352: }
353: switch (gif_font) {
354: case 1:
355: strcat(term_options, "small ");
356: break;
357: case 2:
358: strcat(term_options, "medium ");
359: break;
360: case 3:
361: strcat(term_options, "large ");
362: break;
363: }
364: sprintf(term_options + strlen(term_options),
365: "size %d,%d ", GIF_XMAX, GIF_YMAX);
366:
367: for (i = 0; i < gif_state.n_colors; i++) {
368: sprintf(term_options + strlen(term_options),
369: "x%06x ", gif_state.rgb_table[i]);
370: }
371: }
372:
373:
374: /*
375: * _init() Called once, when the device is first selected. This procedure
376: * should set up things that only need to be set once, like handshaking and
377: * character sets etc...
378: */
379: TERM_PUBLIC void GIF_init()
380: {
381: gif_state.linetype = 0;
382: }
383:
384: /*
385: * _reset() Called when gnuplot is exited, the output device changed or
386: * the terminal type changed. This procedure should reset the device,
387: * possibly flushing a buffer somewhere or generating a form feed.
388: */
389: TERM_PUBLIC void GIF_reset()
390: {
391: }
392:
393: /*
394: * _graphics() Called just before a plot is going to be displayed. This
395: * procedure should set the device into graphics mode. Devices which can't
396: * be used as terminals (like plotters) will probably be in graphics mode
397: * always and therefore won't need this.
398: */
399: TERM_PUBLIC void GIF_graphics()
400: {
401: int i;
402: unsigned int rgb;
403: gif_state.font = GIF_font;
404: gif_state.color = 0;
405: gif_state.image = gdImageCreate((int) (xsize * GIF_XMAX),
406: (int) (ysize * GIF_YMAX));
407: gif_state.height = (int) (ysize * GIF_YMAX);
408: gif_state.charw = term->h_char; /* gif_state.font->w; */
409: gif_state.charh = term->v_char; /* gif_state.font->h; */
410: for (i = gif_state.n_colors; i < GIF_N_DEFAULT_COLORS; i++)
411: gif_state.rgb_table[i] = gif_color_rgbs[i];
412: if (gif_state.n_colors < GIF_N_DEFAULT_COLORS)
413: gif_state.n_colors = GIF_N_DEFAULT_COLORS;
414: for (i = 0; i < gif_state.n_colors; i++) {
415: rgb = gif_state.rgb_table[i];
416: gif_state.color_table[i] =
417: gdImageColorAllocate(gif_state.image, (rgb >> 16) & 0xff,
418: (rgb >> 8) & 0xff, rgb & 0xff);
419: }
420: if (gif_state.flags & GIF_USE_TRANSPARENT)
421: gdImageColorTransparent(gif_state.image,
422: gif_state.color_table[0]);
423: else
424: gdImageColorTransparent(gif_state.image, -1);
425: }
426:
427: /*
428: * _text() Called immediately after a plot is displayed. This procedure
429: * should set the device back into text mode if it is also a terminal, so
430: * that commands can be seen as they're typed. Again, this will probably
431: * do nothing if the device can't be used as a terminal.
432: */
433: TERM_PUBLIC void GIF_text()
434: {
435: if (gif_state.flags & GIF_USE_INTERLACE)
436: gdImageInterlace(gif_state.image, 1);
437: gdImageGif(gif_state.image, gpoutfile);
438: gdImageDestroy(gif_state.image);
439: }
440:
441: /* _move(x,y) Called at the start of a line. The cursor should move to the
442: * (x,y) position without drawing.
443: */
444: TERM_PUBLIC void GIF_move(unsigned int x, unsigned int y)
445: {
446: gif_state.x = x;
447: gif_state.y = y;
448: }
449:
450: /* _vector(x,y) Called when a line is to be drawn. This should display a line
451: * from the last (x,y) position given by _move() or _vector() to this new (x,y)
452: * position.
453: */
454: TERM_PUBLIC void GIF_vector(unsigned int x, unsigned int y)
455: {
456: int gif_linetype_dotted[5];
457:
458: if (gif_state.linetype == -1) {
459: gif_linetype_dotted[0] = gif_state.color_table[2];
460: gif_linetype_dotted[1] = gif_state.color_table[2];
461: gif_linetype_dotted[2] = gif_state.color_table[0];
462: gif_linetype_dotted[3] = gif_state.color_table[0];
463: gif_linetype_dotted[4] = gif_state.color_table[0];
464:
465: gdImageSetStyle(gif_state.image, gif_linetype_dotted, 5);
466: gdImageLine(gif_state.image, gif_state.x, gif_state.height - gif_state.y,
467: x, gif_state.height - y, gdStyled);
468: } else {
469: gdImageLine(gif_state.image, gif_state.x, gif_state.height - gif_state.y,
470: x, gif_state.height - y, gif_state.color);
471: }
472: gif_state.x = x;
473: gif_state.y = y;
474: }
475:
476: /* _linetype(lt) Called to set the line type before text is displayed or
477: * line(s) plotted. This procedure should select a pen color or line
478: * style if the device has these capabilities.
479: * lt is an integer from -2 to 0 or greater.
480: * An lt of -2 is used for the border of the plot.
481: * An lt of -1 is used for the X and Y axes.
482: * lt 0 and upwards are used for plots 0 and upwards.
483: * If _linetype() is called with lt greater than the available line types,
484: * it should map it to one of the available line types.
485: * Most drivers provide 9 different linetypes (lt is 0 to 8).
486: */
487: TERM_PUBLIC void GIF_linetype(int type)
488: {
489: if (type >= (gif_state.n_colors - 3))
490: type %= (gif_state.n_colors - 3);
491:
492: gif_state.color = gif_state.color_table[type + 3];
493: gif_state.linetype = type;
494: }
495:
496: /* _put_text(x,y,str) Called to display text at the (x,y) position,
497: * while in graphics mode. The text should be vertically (with respect
498: * to the text) justified about (x,y). The text is rotated according
499: * to _text_angle and then horizontally (with respect to the text)
500: * justified according to _justify_text.
501: */
502: TERM_PUBLIC void GIF_put_text(unsigned int x, unsigned int y, char *string)
503: {
504: if (gif_state.angle == 1) {
505: x -= gif_state.charh / 2;
506: gdImageStringUp(gif_state.image, gif_state.font,
507: x, gif_state.height - y,
508: string, gif_state.color);
509: } else {
510: y += gif_state.charh / 2;
511: gdImageString(gif_state.image, gif_state.font,
512: x, gif_state.height - y,
513: string, gif_state.color);
514: }
515: }
516:
517: /* _text_angle(ang) Called to rotate the text angle when placing the y label.
518: * If ang = 0 then text is horizontal. If ang = 1 then text is vertically
519: * upwards. Returns TRUE if text can be rotated, FALSE otherwise.
520: */
521: TERM_PUBLIC int GIF_text_angle(int ang)
522: {
523: gif_state.angle = ang;
524: return TRUE;
525: }
526:
527: /*
528: * Local Variables:
529: * mode:C
530: * End:
531: */
532:
533: #endif /* TERM_BODY */
534: #ifdef TERM_TABLE
535:
536: TERM_TABLE_START(gif_driver)
537: "gif", "GIF format [mode] [fontsize] [size] [colors]",
538: GREG_XMAX, GREG_YMAX, GIF_VCHAR, GIF_HCHAR,
539: GIF_VTIC, GIF_HTIC, GIF_options, GIF_init, GIF_reset,
540: GIF_text, null_scale, GIF_graphics, GIF_move, GIF_vector,
541: GIF_linetype, GIF_put_text, GIF_text_angle,
542: null_justify_text, do_point, do_arrow, set_font_null,
543: 0, /* pointsize */
544: TERM_CAN_MULTIPLOT | TERM_BINARY
545: TERM_TABLE_END(gif_driver)
546:
547: #undef LAST_TERM
548: #define LAST_TERM gif_driver
549:
550: #endif /* TERM_TABLE */
551: #endif /* TERM_PROTO_ONLY */
552:
553: #ifdef TERM_HELP
554: START_HELP(gif)
555: "1 gif",
556: "?commands set terminal gif",
557: "?set terminal gif",
558: "?set term gif",
559: "?terminal gif",
560: "?term gif",
561: "?gif",
562: " The `gif` terminal driver generates output in GIF format. It uses Thomas",
563: " Boutell's gd library, which is available from http://www.boutell.com/gd/",
564: "",
565: " Syntax:",
566: " set terminal gif {transparent} {interlace}",
567: " {small | medium | large}",
568: " {size <x>,<y>}",
569: " {<color0> <color1> <color2> ...}",
570: "",
571: " `transparent` instructs the driver to generate transparent GIFs. The first",
572: " color will be the transparent one.",
573: "",
574: " `interlace` instructs the driver to generate interlaced GIFs.",
575: "",
576: " The choice of fonts is `small` (6x12 pixels), `medium` (7x13 Bold) or `large`",
577: " (8x16).",
578: "",
579: " The size <x,y> is given in pixels---it defaults to 640x480. The number of",
580: " pixels can be also modified by scaling with the `set size` command.",
581: "",
582: " Each color must be of the form 'xrrggbb', where x is the literal character",
583: " 'x' and 'rrggbb' are the red, green and blue components in hex. For example,",
584: " 'x00ff00' is green. The background color is set first, then the border",
585: " colors, then the X & Y axis colors, then the plotting colors. The maximum",
586: " number of colors that can be set is 256.",
587: "",
588: " Examples:",
589: " set terminal gif small size 640,480 \\",
590: " xffffff x000000 x404040 \\",
591: " xff0000 xffa500 x66cdaa xcdb5cd \\",
592: " xadd8e6 x0000ff xdda0dd x9500d3 # defaults",
593: "",
594: " which uses white for the non-transparent background, black for borders, gray",
595: " for the axes, and red, orange, medium aquamarine, thistle 3, light blue, blue,",
596: " plum and dark violet for eight plotting colors.",
597: "",
598: " set terminal gif transparent xffffff \\",
599: " x000000 x202020 x404040 x606060 \\",
600: " x808080 xA0A0A0 xC0C0C0 xE0E0E0 \\",
601: " which uses white for the transparent background, black for borders, dark",
602: " gray for axes, and a gray-scale for the six plotting colors.",
603: "",
604: " The page size is 640x480 pixels. The `gif` driver can create either color",
605: " or monochromatic output, but you have no control over which is produced.",
606: "",
607: " The current version of the `gif` driver does not support animated GIFs."
608: END_HELP(gif)
609: #endif /* TERM_HELP */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>