Annotation of OpenXM_contrib/gnuplot/term/gif.trm, Revision 1.1.1.3
1.1 maekawa 1: /*
1.1.1.3 ! ohara 2: * $Id: gif.trm,v 1.12.2.7 2002/12/12 12:56:44 lhecking Exp $
1.1 maekawa 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: *
1.1.1.2 maekawa 56: * font (tiny,small,medium,large,giant)
1.1 maekawa 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
1.1.1.2 maekawa 72: * Alfred Reibenschuh <alfred.reibenschuh@it-austria.com> or <fredo@blackbox.at>
73: * Ben Laurie <ben@algroup.co.uk>
1.1 maekawa 74: *
75: * send your comments or suggestions to:
76: * info-gnuplot@cs.dartmouth.edu
77: *
78: * This version outputs either color or monochrome GIFs. The default
79: * is 640x480 pixels.
80: *
81: * link with -Lterm/gd -lgd if your directory structure is gnuplot/term/gd
82: *
83: * gd is not distributed with gnuplot, because of the UNISYS license thing.
84: *
85: * find out about gd from http://www.boutell.com/gd/
86: *
1.1.1.3 ! ohara 87: * We recommend to use gd library version 1.3 or 1.4 because it uses
! 88: * Run Length Encoding (RLE) instead of LZW compression. LZW compression
! 89: * is licensed by UNISYS.
! 90: *
1.1.1.2 maekawa 91: * Gd library versions before 1.3, and gd library 1.5 are subject to
92: * the Unisys license. Use at your own risk. From version 1.6 on, gd
93: * library creates png files instead of gif.
1.1 maekawa 94: *
95: */
96:
97: #include "driver.h"
98:
99: #ifdef TERM_REGISTER
100: register_term(gif)
101: #endif
102:
103: #ifdef TERM_PROTO
104: TERM_PUBLIC void GIF_options __PROTO((void));
105: TERM_PUBLIC void GIF_init __PROTO((void));
106: TERM_PUBLIC void GIF_graphics __PROTO((void));
107: TERM_PUBLIC void GIF_text __PROTO((void));
108: TERM_PUBLIC void GIF_linetype __PROTO((int linetype));
109: TERM_PUBLIC void GIF_move __PROTO((unsigned int x, unsigned int y));
110: TERM_PUBLIC void GIF_vector __PROTO((unsigned int x, unsigned int y));
111: TERM_PUBLIC void GIF_put_text __PROTO((unsigned int x, unsigned int y, char str[]));
112: TERM_PUBLIC int GIF_text_angle __PROTO((int ang));
113: TERM_PUBLIC void GIF_reset __PROTO((void));
114:
115: #include "gd.h"
1.1.1.2 maekawa 116:
1.1 maekawa 117: extern gdFontPtr gdFontSmall; /* 6x12 */
118: extern gdFontPtr gdFontLarge; /* 8x16 */
119: extern gdFontPtr gdFontMediumBold; /* 7x13 */
1.1.1.2 maekawa 120: extern gdFontPtr gdFontGiant; /* 9x15 */
121: extern gdFontPtr gdFontTiny; /* 5x8 */
1.1 maekawa 122:
123:
124: #define GREG_XMAX 640
125: #define GREG_YMAX 480
126:
127:
1.1.1.2 maekawa 128: static int GIF_XMAX = GREG_XMAX;
129: static int GIF_YMAX = GREG_YMAX;
1.1 maekawa 130:
131:
132: #define GIF_FONT_SMALL 1
133: #ifdef GIF_FONT_SMALL
134: # define gdfont gdFontSmall
135: # define GIF_VCHAR 12
136: # define GIF_HCHAR 6
137: #else
138: # define gdfont gdFontMediumBold
139: # define GIF_VCHAR 13
140: # define GIF_HCHAR 7
141: #endif
142:
143: static gdFontPtr GIF_font;
144:
145: #define GIF_VTIC (GREG_YMAX/100)
146: #define GIF_HTIC (GREG_XMAX/150)
147:
148: #define GIF_MAX_COLORS 256
149: #define GOT_NEXT_PROTO
150: #endif
151:
152: #ifndef TERM_PROTO_ONLY
153: #ifdef TERM_BODY
154:
155: static struct {
156: gdImagePtr image;
157: gdFontPtr font;
158: unsigned int x, y;
159: int height;
160: int charh, charw;
161: int color;
162: int n_colors;
163: int color_table[GIF_MAX_COLORS];
164: int rgb_table[GIF_MAX_COLORS];
165: int angle;
166: int flags;
167: int linetype;
168: } gif_state;
169:
170: #define GIF_USE_TRANSPARENT 1
171: #define GIF_USE_INTERLACE 2
172:
173: /*
174: * _options() Called when terminal type is selected.
175: * This procedure should parse options on the command line. A list of the
176: * currently selected options should be stored in term_options[] in a form
177: * suitable for use with the set term command. term_options[] is used by
178: * the save command. Use options_null() if no options are available.
179: */
1.1.1.2 maekawa 180: TERM_PUBLIC void
181: GIF_options()
1.1 maekawa 182: {
1.1.1.2 maekawa 183: struct value s;
1.1 maekawa 184: int gif_font, i;
1.1.1.2 maekawa 185: char *string;
186: unsigned long color;
187:
1.1 maekawa 188: term_options[0] = NUL;
189: gif_state.n_colors = 0;
190: gif_state.flags = 0;
191: gif_font = 1;
192: GIF_font = gdfont;
193:
194: while (!END_OF_COMMAND) {
195: if (almost_equals(c_token, "t$ransparent")) {
196: gif_state.flags |= GIF_USE_TRANSPARENT;
197: ++c_token;
198: } else if (almost_equals(c_token, "i$nterlace")) {
199: gif_state.flags |= GIF_USE_INTERLACE;
200: ++c_token;
1.1.1.2 maekawa 201: } else if (almost_equals(c_token, "ti$ny")) {
202: GIF_font=gdFontTiny;
203: gif_font = 0;
204: term->v_char = (unsigned int)(8);
205: term->h_char = (unsigned int)(5);
206: ++c_token;
1.1 maekawa 207: } else if (almost_equals(c_token, "s$mall")) {
208: GIF_font = gdFontSmall;
209: gif_font = 1;
210: term->v_char = (unsigned int) (12);
211: term->h_char = (unsigned int) (6);
212: ++c_token;
213: } else if (almost_equals(c_token, "m$edium")) {
214: GIF_font = gdFontMediumBold;
215: gif_font = 2;
216: term->v_char = (unsigned int) (13);
217: term->h_char = (unsigned int) (7);
218: ++c_token;
219: } else if (almost_equals(c_token, "l$arge")) {
220: GIF_font = gdFontLarge;
221: gif_font = 3;
222: term->v_char = (unsigned int) (16);
223: term->h_char = (unsigned int) (8);
224: ++c_token;
1.1.1.3 ! ohara 225: } else if (almost_equals(c_token, "g$iant")) {
1.1.1.2 maekawa 226: GIF_font=gdFontGiant;
227: gif_font = 4;
228: term->v_char = (unsigned int)(15);
229: term->h_char = (unsigned int)(9);
230: ++c_token;
1.1 maekawa 231: } else if (almost_equals(c_token, "si$ze")) {
232: c_token++;
233: if (END_OF_COMMAND) {
234: GIF_XMAX = GREG_XMAX;
235: GIF_YMAX = GREG_YMAX;
236: term->v_tic = GIF_YMAX / 80;
237: term->h_tic = GIF_XMAX / 80;
238: } else {
239: GIF_XMAX = real(const_express(&s));
240: if (equals(c_token, ",")) {
241: c_token++;
242: GIF_YMAX = real(const_express(&s));
243: term->v_tic = GIF_YMAX / 80;
244: term->h_tic = GIF_XMAX / 80;
245: term->ymax = GIF_YMAX;
246: term->xmax = GIF_XMAX;
247: }
248: }
1.1.1.2 maekawa 249: } else { /* not "size" */
250: string = input_line + token[c_token].start_index;
1.1 maekawa 251: if (sscanf(string, "x%lx", &color) != 1) {
1.1.1.2 maekawa 252: int_error("invalid color spec, must be xRRGGBB",c_token);
1.1 maekawa 253: } else if (gif_state.n_colors == GIF_MAX_COLORS) {
1.1.1.3 ! ohara 254: int_warn("too many colors, ignoring", c_token);
1.1 maekawa 255: ++c_token;
256: } else {
257: gif_state.rgb_table[gif_state.n_colors++] = color;
258: ++c_token;
259: }
260: }
261: }
262:
263:
264: /* now generate options string */
265:
266: if (gif_state.flags & GIF_USE_TRANSPARENT) {
267: strcat(term_options, "transparent ");
268: }
269: if (gif_state.flags & GIF_USE_INTERLACE) {
270: strcat(term_options, "interlace ");
271: }
272: switch (gif_font) {
1.1.1.2 maekawa 273: case 0:
274: strcat(term_options,"tiny ");
275: break;
1.1 maekawa 276: case 1:
277: strcat(term_options, "small ");
278: break;
279: case 2:
280: strcat(term_options, "medium ");
281: break;
282: case 3:
283: strcat(term_options, "large ");
284: break;
1.1.1.2 maekawa 285: case 4:
286: strcat(term_options,"giant ");
287: break;
1.1 maekawa 288: }
289: sprintf(term_options + strlen(term_options),
290: "size %d,%d ", GIF_XMAX, GIF_YMAX);
291:
1.1.1.3 ! ohara 292: for (i = 0; strlen(term_options) + 9 < MAX_LINE_LEN &&
! 293: i < gif_state.n_colors; i++) {
1.1 maekawa 294: sprintf(term_options + strlen(term_options),
295: "x%06x ", gif_state.rgb_table[i]);
296: }
297: }
298:
299:
300: /*
301: * _init() Called once, when the device is first selected. This procedure
302: * should set up things that only need to be set once, like handshaking and
303: * character sets etc...
304: */
1.1.1.2 maekawa 305: TERM_PUBLIC void
306: GIF_init()
1.1 maekawa 307: {
308: gif_state.linetype = 0;
309: }
310:
311: /*
312: * _reset() Called when gnuplot is exited, the output device changed or
313: * the terminal type changed. This procedure should reset the device,
314: * possibly flushing a buffer somewhere or generating a form feed.
315: */
1.1.1.2 maekawa 316: TERM_PUBLIC void
317: GIF_reset()
1.1 maekawa 318: {
319: }
320:
321: /*
322: * _graphics() Called just before a plot is going to be displayed. This
323: * procedure should set the device into graphics mode. Devices which can't
324: * be used as terminals (like plotters) will probably be in graphics mode
325: * always and therefore won't need this.
326: */
1.1.1.2 maekawa 327: TERM_PUBLIC void
328: GIF_graphics()
1.1 maekawa 329: {
330: int i;
331: unsigned int rgb;
332: gif_state.font = GIF_font;
333: gif_state.color = 0;
334: gif_state.image = gdImageCreate((int) (xsize * GIF_XMAX),
335: (int) (ysize * GIF_YMAX));
1.1.1.2 maekawa 336: gif_state.height = (int) (ysize * GIF_YMAX - 1);
1.1 maekawa 337: gif_state.charw = term->h_char; /* gif_state.font->w; */
338: gif_state.charh = term->v_char; /* gif_state.font->h; */
1.1.1.2 maekawa 339: for (i = gif_state.n_colors; i < WEB_N_COLORS; i++)
340: gif_state.rgb_table[i] =
341: (web_color_rgbs[i].r << 16) |
342: (web_color_rgbs[i].g << 8) |
343: web_color_rgbs[i].b;
344: if (gif_state.n_colors < WEB_N_COLORS)
345: gif_state.n_colors = WEB_N_COLORS;
1.1 maekawa 346: for (i = 0; i < gif_state.n_colors; i++) {
347: rgb = gif_state.rgb_table[i];
348: gif_state.color_table[i] =
349: gdImageColorAllocate(gif_state.image, (rgb >> 16) & 0xff,
350: (rgb >> 8) & 0xff, rgb & 0xff);
351: }
352: if (gif_state.flags & GIF_USE_TRANSPARENT)
353: gdImageColorTransparent(gif_state.image,
354: gif_state.color_table[0]);
355: else
356: gdImageColorTransparent(gif_state.image, -1);
357: }
358:
359: /*
360: * _text() Called immediately after a plot is displayed. This procedure
361: * should set the device back into text mode if it is also a terminal, so
362: * that commands can be seen as they're typed. Again, this will probably
363: * do nothing if the device can't be used as a terminal.
364: */
1.1.1.2 maekawa 365: TERM_PUBLIC void
366: GIF_text()
1.1 maekawa 367: {
368: if (gif_state.flags & GIF_USE_INTERLACE)
369: gdImageInterlace(gif_state.image, 1);
370: gdImageGif(gif_state.image, gpoutfile);
371: gdImageDestroy(gif_state.image);
372: }
373:
374: /* _move(x,y) Called at the start of a line. The cursor should move to the
375: * (x,y) position without drawing.
376: */
1.1.1.2 maekawa 377: TERM_PUBLIC void
378: GIF_move(unsigned int x, unsigned int y)
1.1 maekawa 379: {
380: gif_state.x = x;
381: gif_state.y = y;
382: }
383:
384: /* _vector(x,y) Called when a line is to be drawn. This should display a line
385: * from the last (x,y) position given by _move() or _vector() to this new (x,y)
386: * position.
387: */
1.1.1.2 maekawa 388: TERM_PUBLIC void
389: GIF_vector(unsigned int x, unsigned int y)
1.1 maekawa 390: {
391: int gif_linetype_dotted[5];
392:
393: if (gif_state.linetype == -1) {
394: gif_linetype_dotted[0] = gif_state.color_table[2];
395: gif_linetype_dotted[1] = gif_state.color_table[2];
396: gif_linetype_dotted[2] = gif_state.color_table[0];
397: gif_linetype_dotted[3] = gif_state.color_table[0];
398: gif_linetype_dotted[4] = gif_state.color_table[0];
399:
400: gdImageSetStyle(gif_state.image, gif_linetype_dotted, 5);
401: gdImageLine(gif_state.image, gif_state.x, gif_state.height - gif_state.y,
402: x, gif_state.height - y, gdStyled);
403: } else {
404: gdImageLine(gif_state.image, gif_state.x, gif_state.height - gif_state.y,
405: x, gif_state.height - y, gif_state.color);
406: }
407: gif_state.x = x;
408: gif_state.y = y;
409: }
410:
411: /* _linetype(lt) Called to set the line type before text is displayed or
412: * line(s) plotted. This procedure should select a pen color or line
413: * style if the device has these capabilities.
414: * lt is an integer from -2 to 0 or greater.
415: * An lt of -2 is used for the border of the plot.
416: * An lt of -1 is used for the X and Y axes.
417: * lt 0 and upwards are used for plots 0 and upwards.
418: * If _linetype() is called with lt greater than the available line types,
419: * it should map it to one of the available line types.
420: * Most drivers provide 9 different linetypes (lt is 0 to 8).
421: */
1.1.1.2 maekawa 422: TERM_PUBLIC void
423: GIF_linetype(int type)
1.1 maekawa 424: {
425: if (type >= (gif_state.n_colors - 3))
426: type %= (gif_state.n_colors - 3);
427:
428: gif_state.color = gif_state.color_table[type + 3];
429: gif_state.linetype = type;
430: }
431:
432: /* _put_text(x,y,str) Called to display text at the (x,y) position,
433: * while in graphics mode. The text should be vertically (with respect
434: * to the text) justified about (x,y). The text is rotated according
435: * to _text_angle and then horizontally (with respect to the text)
436: * justified according to _justify_text.
437: */
1.1.1.2 maekawa 438: TERM_PUBLIC void
439: GIF_put_text(unsigned int x, unsigned int y, char *string)
1.1 maekawa 440: {
441: if (gif_state.angle == 1) {
442: x -= gif_state.charh / 2;
443: gdImageStringUp(gif_state.image, gif_state.font,
444: x, gif_state.height - y,
445: string, gif_state.color);
446: } else {
447: y += gif_state.charh / 2;
448: gdImageString(gif_state.image, gif_state.font,
449: x, gif_state.height - y,
450: string, gif_state.color);
451: }
452: }
453:
454: /* _text_angle(ang) Called to rotate the text angle when placing the y label.
455: * If ang = 0 then text is horizontal. If ang = 1 then text is vertically
456: * upwards. Returns TRUE if text can be rotated, FALSE otherwise.
457: */
1.1.1.2 maekawa 458: TERM_PUBLIC int
459: GIF_text_angle(int ang)
1.1 maekawa 460: {
461: gif_state.angle = ang;
462: return TRUE;
463: }
464:
1.1.1.2 maekawa 465: TERM_PUBLIC int
466: GIF_set_font(char *fontname)
467: {
468: char name[32];
469: int sep;
470: gdFontPtr font;
471:
472: sep = strcspn(fontname,",");
473: strncpy(name,fontname,sep);
474: name[sep] = NUL;
475:
476: if (!strcmp(fontname,"small"))
477: font = gdFontSmall;
478: else if (!strcmp(fontname,"medium"))
479: font = gdFontMediumBold;
480: else if(!strcmp(fontname,"large"))
481: font = gdFontLarge;
482: else if(!strcmp(fontname,"giant"))
483: font = gdFontGiant;
484: else if(!strcmp(fontname,"tiny"))
485: font = gdFontTiny;
486: else
487: font = GIF_font;
488:
489: gif_state.charw = font->w;
490: gif_state.charh = font->h;
491:
492: gif_state.font = font;
493:
494: return TRUE;
495: }
1.1 maekawa 496:
497: #endif /* TERM_BODY */
498: #ifdef TERM_TABLE
499:
500: TERM_TABLE_START(gif_driver)
501: "gif", "GIF format [mode] [fontsize] [size] [colors]",
502: GREG_XMAX, GREG_YMAX, GIF_VCHAR, GIF_HCHAR,
503: GIF_VTIC, GIF_HTIC, GIF_options, GIF_init, GIF_reset,
504: GIF_text, null_scale, GIF_graphics, GIF_move, GIF_vector,
505: GIF_linetype, GIF_put_text, GIF_text_angle,
1.1.1.2 maekawa 506: null_justify_text, do_point, do_arrow, GIF_set_font,
1.1 maekawa 507: 0, /* pointsize */
508: TERM_CAN_MULTIPLOT | TERM_BINARY
509: TERM_TABLE_END(gif_driver)
510:
511: #undef LAST_TERM
512: #define LAST_TERM gif_driver
513:
514: #endif /* TERM_TABLE */
515: #endif /* TERM_PROTO_ONLY */
516:
517: #ifdef TERM_HELP
518: START_HELP(gif)
519: "1 gif",
520: "?commands set terminal gif",
521: "?set terminal gif",
522: "?set term gif",
523: "?terminal gif",
524: "?term gif",
525: "?gif",
526: " The `gif` terminal driver generates output in GIF format. It uses Thomas",
527: " Boutell's gd library, which is available from http://www.boutell.com/gd/",
528: "",
1.1.1.2 maekawa 529: " By default, the `gif` terminal driver uses a shared Web-friendy palette."
530: "",
1.1 maekawa 531: " Syntax:",
532: " set terminal gif {transparent} {interlace}",
1.1.1.2 maekawa 533: " {tiny | small | medium | large | giant}",
1.1 maekawa 534: " {size <x>,<y>}",
535: " {<color0> <color1> <color2> ...}",
536: "",
537: " `transparent` instructs the driver to generate transparent GIFs. The first",
538: " color will be the transparent one.",
539: "",
540: " `interlace` instructs the driver to generate interlaced GIFs.",
541: "",
1.1.1.2 maekawa 542: " The choice of fonts is `tiny` (5x8 pixels), `small` (6x12 pixels), `medium`",
543: " (7x13 Bold), `large` (8x16) or `giant` (9x15 pixels)",
1.1 maekawa 544: "",
545: " The size <x,y> is given in pixels---it defaults to 640x480. The number of",
546: " pixels can be also modified by scaling with the `set size` command.",
547: "",
548: " Each color must be of the form 'xrrggbb', where x is the literal character",
549: " 'x' and 'rrggbb' are the red, green and blue components in hex. For example,",
550: " 'x00ff00' is green. The background color is set first, then the border",
551: " colors, then the X & Y axis colors, then the plotting colors. The maximum",
552: " number of colors that can be set is 256.",
553: "",
554: " Examples:",
555: " set terminal gif small size 640,480 \\",
556: " xffffff x000000 x404040 \\",
557: " xff0000 xffa500 x66cdaa xcdb5cd \\",
558: " xadd8e6 x0000ff xdda0dd x9500d3 # defaults",
559: "",
560: " which uses white for the non-transparent background, black for borders, gray",
561: " for the axes, and red, orange, medium aquamarine, thistle 3, light blue, blue,",
562: " plum and dark violet for eight plotting colors.",
563: "",
564: " set terminal gif transparent xffffff \\",
565: " x000000 x202020 x404040 x606060 \\",
1.1.1.3 ! ohara 566: " x808080 xA0A0A0 xC0C0C0 xE0E0E0",
! 567: "",
1.1 maekawa 568: " which uses white for the transparent background, black for borders, dark",
569: " gray for axes, and a gray-scale for the six plotting colors.",
570: "",
571: " The page size is 640x480 pixels. The `gif` driver can create either color",
572: " or monochromatic output, but you have no control over which is produced.",
573: "",
574: " The current version of the `gif` driver does not support animated GIFs."
575: END_HELP(gif)
576: #endif /* TERM_HELP */
1.1.1.2 maekawa 577:
578: /*
579: * Local Variables:
580: * mode:C
581: * End:
582: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>