File: gplot2.cxx

package info (click to toggle)
imview 1.1.9c-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,068 kB
  • ctags: 3,686
  • sloc: cpp: 28,834; sh: 2,624; ansic: 1,818; makefile: 767; exp: 112; python: 88
file content (221 lines) | stat: -rw-r--r-- 5,638 bytes parent folder | download | duplicates (6)
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
/*
 * $Id: gplot2.cxx,v 4.2 2004/06/17 17:34:18 hut66au Exp $
 *
 * Imview, the portable image analysis application
 * http://www.cmis.csiro.au/Hugues.Talbot/imview
 * ----------------------------------------------------------
 *
 *  Imview is an attempt to provide an image display application
 *  suitable for professional image analysis. It was started in
 *  1997 and is mostly the result of the efforts of Hugues Talbot,
 *  Image Analysis Project, CSIRO Mathematical and Information
 *  Sciences, with help from others (see the CREDITS files for
 *  more information)
 *
 *  Imview is Copyrighted (C) 1997-2001 by Hugues Talbot and was
 *  supported in parts by the Australian Commonwealth Science and 
 *  Industry Research Organisation. Please see the COPYRIGHT file 
 *  for full details. Imview also includes the contributions of 
 *  many others. Please see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program 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 General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 * */

/*
  ImView's interface to gnuplot, after similar work for EiC by
  Ed Breen
  Example usages:
  #include gplot2.c
  plot_t *p1 = openplot("p1");
  send2plot(p1,"plot sin(x) + tan(x)");
  closeplot(p1);
*/   

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "gplot2.hxx"
#include "imview.hxx"
#include "machine.hxx"

using std::list;

static list <char *>  gplottmpnamelist;

void Send_2_plot_(plot_t *p, char * fmt, ...)
{
    char *buff;
    int   stringlength, allocatelength;
    va_list args;

    stringlength = strlen(fmt)*2;
    allocatelength = (512 > stringlength) ? 512:stringlength;
    buff = (char *)malloc(allocatelength); /* fmt string can be VERY long... */

    im_va_start(args,fmt);
    
    vsprintf(buff,fmt,args);

    fputs(buff,getPlotFile(p));
    fputc('\n',getPlotFile(p));

    free(buff);
    va_end(args);
}

void plotDoubles(double *hist, int n,plot_t *p, bool smooth)
{
    int i;
    double min,max;
    FILE *fp;

    fp = fopen(p->fname,"w");
    min = max = *hist;
    for(i=0;i<n;++i,++hist) {
        if(*hist < min)
            min = *hist;
        if(*hist > max)
            max = *hist;
        fprintf(fp,"%d %g\n",i,*hist);
    }
    fclose(fp);

    Send_2_plot_(p,"plot [0:%d] '%s' %s notitle with lines",
		 n-1, p->fname,
		 smooth ? "smooth csplines":"" );
}



static void addToUnlinkList(char *tmpname)
{
    dbgprintf("Adding %s to the list of files to remove later\n", tmpname);
    gplottmpnamelist.push_back(tmpname);
}

static void unlinkAllTmpFiles(void)
{
    while (!gplottmpnamelist.empty()) {
	char *&nameref = gplottmpnamelist.back();
	remove(nameref);
	dbgprintf("Removing %s\n", nameref);
	// this name had been allocated
	free(nameref);
	gplottmpnamelist.pop_back();
    }
}

// Use this in conjunction with dataMPlot
char *writeDoubleVector(double *vec, int n)
{
    char * tmpName = tempnam(NULL, "FlIm"); // this allocates a new area
    FILE *fp = tmpfile();
    int i;

    fp = fopen(tmpName,"w");
    if(!fp)
        return NULL;
    for(i=0;i<n;++i)
        fprintf(fp,"%d %g\n",i,vec[i]);
    
    fclose(fp);
    addToUnlinkList(tmpName);
    return tmpName;
}

// allows multiple plots on the same plot
void dataMPlot(plot_t *p,
               char * (*f)(...),  /* return the name of the file where data is stored  */
               int nv,         /* number of vectors in vecs */
               void **vecs,    /* array of vectors */
	       char **labels,  /* arrays of labels (may not be NULL) */
               int *lens,      /* array of lengths to vecs */   
	       bool smooth)   /* uses splines or straight lines */
{

    /* Assumes that plot title, line style etc  have been
       handled elsewhere.
    */
    char *buff;
    char buf[256];
    int blen = 256;
    char **names = (char **) malloc(nv * sizeof(char*));    
    char *tmp;
    int i,k, tl = 0;

    
    for(i=0;i<nv;i++) {
        tmp = f(vecs[i],lens[i]);
        k = strlen(tmp);
        names[i] = (char *)malloc(k+1);
        tl += k + 5;
        strcpy(names[i],tmp);
    }
    buff = (char *)malloc(blen+tl);
    
    sprintf(buff,"plot '%s' title '%s'",names[0], labels[0]);
    free(names[0]);
    for(i=1;i<nv; i++) {
        sprintf(buf,",'%s' title '%s'",names[i], labels[i]);
        strcat(buff,buf);
        free(names[i]);
    }
    send2plot(p,buff);
    free(names);
    free(buff);
}


void * closePlot(plot_t *p)
{
    if(p != NULL) {
	fflush(p->fp);
        pclose(p->fp);
        remove(p->fname);
        free(p);
    }
    unlinkAllTmpFiles();
    return NULL;
}


plot_t * openPlot(char * plotname)
{
    plot_t * plot = (plot_t*)calloc(sizeof(plot_t),1);

    if(plot != NULL) {
        if ((plot->fp = popen("gnuplot","w")) != 0) {
	    setvbuf(plot->fp,NULL,_IONBF,0); // unbuffered I/O
	    if(plotname != NULL) {
		Send_2_plot_(plot,"set title '%s'",plotname);
	    }
	    tmpnam(plot->fname);
	} else {
	    free(plot);
	    plot = NULL;
	}
    }
    return plot;
}