144#include <config_auto.h>
148#include "allheaders.h"
199 return (GPLOT *)ERROR_PTR(
"rootname not defined", __func__, NULL);
200 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
201 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
202 outformat != GPLOT_PNM)
203 return (GPLOT *)ERROR_PTR(
"outformat invalid", __func__, NULL);
204 stringCheckForChars(rootname,
"`;&|><\"?*$()", &badchar);
206 return (GPLOT *)ERROR_PTR(
"invalid rootname", __func__, NULL);
208#if !defined(HAVE_LIBPNG)
209 if (outformat == GPLOT_PNG) {
210 L_WARNING(
"png library missing; output pnm format\n", __func__);
211 outformat = GPLOT_PNM;
215 gplot = (GPLOT *)LEPT_CALLOC(1,
sizeof(GPLOT));
216 gplot->
cmddata = sarrayCreate(0);
223 newroot = genPathname(rootname, NULL);
226 snprintf(buf, Bufsize,
"%s.cmd", rootname);
227 gplot->
cmdname = stringNew(buf);
228 if (outformat == GPLOT_PNG)
229 snprintf(buf, Bufsize,
"%s.png", newroot);
230 else if (outformat == GPLOT_PS)
231 snprintf(buf, Bufsize,
"%s.ps", newroot);
232 else if (outformat == GPLOT_EPS)
233 snprintf(buf, Bufsize,
"%s.eps", newroot);
234 else if (outformat == GPLOT_LATEX)
235 snprintf(buf, Bufsize,
"%s.tex", newroot);
236 else if (outformat == GPLOT_PNM)
237 snprintf(buf, Bufsize,
"%s.pnm", newroot);
238 gplot->
outname = stringNew(buf);
239 if (title) gplot->
title = stringNew(title);
240 if (xlabel) gplot->
xlabel = stringNew(xlabel);
241 if (ylabel) gplot->
ylabel = stringNew(ylabel);
257 if (pgplot == NULL) {
258 L_WARNING(
"ptr address is null!\n", __func__);
262 if ((gplot = *pgplot) == NULL)
267 sarrayDestroy(&gplot->
cmddata);
274 LEPT_FREE(gplot->
title);
320 const char *plotlabel)
323char emptystring[] =
"";
324char *datastr, *title;
326l_float32 valx, valy, startx, delx;
330 return ERROR_INT(
"gplot not defined", __func__, 1);
332 return ERROR_INT(
"nay not defined", __func__, 1);
333 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
334 return ERROR_INT(
"invalid plotstyle", __func__, 1);
336 if ((n = numaGetCount(nay)) == 0)
337 return ERROR_INT(
"no points to plot", __func__, 1);
338 if (nax && (n != numaGetCount(nax)))
339 return ERROR_INT(
"nax and nay sizes differ", __func__, 1);
340 if (n == 1 && plotstyle == GPLOT_LINES) {
341 L_INFO(
"only 1 pt; changing style to points\n", __func__);
342 plotstyle = GPLOT_POINTS;
346 numaGetParameters(nay, &startx, &delx);
349 title = stringNew(plotlabel);
357 snprintf(buf, Bufsize,
"%s.data.%d", gplot->
rootname, gplot->
nplots);
361 sa = sarrayCreate(n);
362 for (i = 0; i < n; i++) {
364 numaGetFValue(nax, i, &valx);
366 valx = startx + i * delx;
367 numaGetFValue(nay, i, &valy);
368 snprintf(buf, Bufsize,
"%f %f\n", valx, valy);
369 sarrayAddString(sa, buf,
L_COPY);
371 datastr = sarrayToString(sa, 0);
398 return ERROR_INT(
"gplot not defined", __func__, 1);
400 scaling != GPLOT_LOG_SCALE_X &&
401 scaling != GPLOT_LOG_SCALE_Y &&
402 scaling != GPLOT_LOG_SCALE_X_Y)
403 return ERROR_INT(
"invalid gplot scaling", __func__, 1);
426 return (
PIX *)ERROR_PTR(
"gplot not defined", __func__, NULL);
428 return (
PIX *)ERROR_PTR(
"output format not an image", __func__, NULL);
431 return (
PIX *)ERROR_PTR(
"plot output not made", __func__, NULL);
432 return pixRead(gplot->
outname);
462 return ERROR_INT(
"gplot not defined", __func__, 1);
465 L_INFO(
"running gnuplot is disabled; "
466 "use setLeptDebugOK(1) to enable\n", __func__);
471 return ERROR_INT(
"iOS 11 does not support system()", __func__, 0);
476 cmdname = genPathname(gplot->
cmdname, NULL);
479 snprintf(buf, Bufsize,
"gnuplot %s", cmdname);
481 snprintf(buf, Bufsize,
"wgnuplot %s", cmdname);
484 callSystemDebug(buf);
500char *cmdstr, *plotlabel, *dataname;
501l_int32 i, plotstyle, nplots;
505 return ERROR_INT(
"gplot not defined", __func__, 1);
512 snprintf(buf, Bufsize,
"set title '%s'", gplot->
title);
516 snprintf(buf, Bufsize,
"set xlabel '%s'", gplot->
xlabel);
520 snprintf(buf, Bufsize,
"set ylabel '%s'", gplot->
ylabel);
526 snprintf(buf, Bufsize,
"set terminal png; set output '%s'",
528 }
else if (gplot->
outformat == GPLOT_PS) {
529 snprintf(buf, Bufsize,
"set terminal postscript; set output '%s'",
531 }
else if (gplot->
outformat == GPLOT_EPS) {
532 snprintf(buf, Bufsize,
"set terminal postscript eps; set output '%s'",
534 }
else if (gplot->
outformat == GPLOT_LATEX) {
535 snprintf(buf, Bufsize,
"set terminal latex; set output '%s'",
537 }
else if (gplot->
outformat == GPLOT_PNM) {
538 snprintf(buf, Bufsize,
"set terminal pbm color; set output '%s'",
543 if (gplot->
scaling == GPLOT_LOG_SCALE_X ||
544 gplot->
scaling == GPLOT_LOG_SCALE_X_Y) {
545 snprintf(buf, Bufsize,
"set logscale x");
548 if (gplot->
scaling == GPLOT_LOG_SCALE_Y ||
549 gplot->
scaling == GPLOT_LOG_SCALE_X_Y) {
550 snprintf(buf, Bufsize,
"set logscale y");
554 nplots = sarrayGetCount(gplot->
datanames);
555 for (i = 0; i < nplots; i++) {
558 numaGetIValue(gplot->
plotstyles, i, &plotstyle);
560 snprintf(buf, Bufsize,
"plot '%s' title '%s' %s",
564 snprintf(buf, Bufsize,
"plot '%s' title '%s' %s, \\",
566 else if (i < nplots - 1)
567 snprintf(buf, Bufsize,
" '%s' title '%s' %s, \\",
570 snprintf(buf, Bufsize,
" '%s' title '%s' %s",
577 cmdstr = sarrayToString(gplot->
cmddata, 1);
578 if ((fp = fopenWriteStream(gplot->
cmdname,
"w")) == NULL) {
580 return ERROR_INT(
"cmd stream not opened", __func__, 1);
582 fwrite(cmdstr, 1, strlen(cmdstr), fp);
605char *plotdata, *dataname;
610 return ERROR_INT(
"gplot not defined", __func__, 1);
612 nplots = sarrayGetCount(gplot->
datanames);
613 for (i = 0; i < nplots; i++) {
616 if ((fp = fopen(dataname,
"w")) == NULL)
617 return ERROR_INT(
"datafile stream not opened", __func__, 1);
618 fwrite(plotdata, 1, strlen(plotdata), fp);
656 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
658 return ERROR_INT(
"failed to generate plot", __func__, 1);
694 outformat, outroot, title);
696 return ERROR_INT(
"failed to generate plot", __func__, 1);
730 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
732 return ERROR_INT(
"failed to generate plot", __func__, 1);
763 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
765 lept_mkdir(
"lept/gplot/pix");
766 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix1.%d", index++);
767 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, GPLOT_PNG, buf, title);
769 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
773 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
805 return (
PIX *)ERROR_PTR(
"both na1, na2 not defined", __func__, NULL);
807 lept_mkdir(
"lept/gplot/pix");
808 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix2.%d", index++);
809 gplot =
gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES, GPLOT_PNG, buf, title);
811 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
815 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
846 return (
PIX *)ERROR_PTR(
"naa not defined", __func__, NULL);
848 lept_mkdir(
"lept/gplot/pix");
849 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pixN.%d", index++);
850 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, GPLOT_PNG, buf, title);
852 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
856 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
897 return (GPLOT *)ERROR_PTR(
"nay not defined", __func__, NULL);
898 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
899 return (GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
900 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
901 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
902 outformat != GPLOT_PNM)
903 return (GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
905 return (GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
907 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
908 return (GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
953 return (GPLOT *)ERROR_PTR(
"nay1 and nay2 not both defined",
955 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
956 return (GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
957 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
958 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
959 outformat != GPLOT_PNM)
960 return (GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
962 return (GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
964 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
965 return (GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1003 const char *outroot,
1011 return (GPLOT *)ERROR_PTR(
"naay not defined", __func__, NULL);
1012 if ((n = numaaGetCount(naay)) == 0)
1013 return (GPLOT *)ERROR_PTR(
"no numa in array", __func__, NULL);
1014 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1015 return (GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1016 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
1017 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
1018 outformat != GPLOT_PNM)
1019 return (GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
1021 return (GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
1023 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
1024 return (GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1025 for (i = 0; i < n; i++) {
1026 nay = numaaGetNuma(naay, i,
L_CLONE);
1056 const char *rootname,
1065 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
1066 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1067 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1069 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1071 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1073 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1104 const char *rootname,
1113 return (
PIX *)ERROR_PTR(
"na1 not defined", __func__, NULL);
1115 return (
PIX *)ERROR_PTR(
"na2 not defined", __func__, NULL);
1116 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1117 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1119 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1121 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1123 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1154 const char *rootname,
1165 return (
PIX *)ERROR_PTR(
"nax not defined", __func__, NULL);
1167 return (
PIX *)ERROR_PTR(
"naay not defined", __func__, NULL);
1168 if ((n = numaaGetCount(naay)) == 0)
1169 return (
PIX *)ERROR_PTR(
"no numa in array", __func__, NULL);
1170 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1171 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1173 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1175 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1177 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1178 for (i = 0; i < n; i++) {
1179 nay = numaaGetNuma(naay, i,
L_CLONE);
1202char *rootname, *title, *xlabel, *ylabel, *ignores;
1203l_int32 outformat, ret, version, ignore;
1208 return (GPLOT *)ERROR_PTR(
"filename not defined", __func__, NULL);
1210 if ((fp = fopenReadStream(filename)) == NULL)
1211 return (GPLOT *)ERROR_PTR(
"stream not opened", __func__, NULL);
1213 ret = fscanf(fp,
"Gplot Version %d\n", &version);
1216 return (GPLOT *)ERROR_PTR(
"not a gplot file", __func__, NULL);
1218 if (version != GPLOT_VERSION_NUMBER) {
1220 return (GPLOT *)ERROR_PTR(
"invalid gplot version", __func__, NULL);
1223 ignore = fscanf(fp,
"Rootname: %511s\n", buf);
1224 rootname = stringNew(buf);
1225 ignore = fscanf(fp,
"Output format: %d\n", &outformat);
1226 ignores = fgets(buf, Bufsize, fp);
1227 title = stringNew(buf + 7);
1228 title[strlen(title) - 1] =
'\0';
1229 ignores = fgets(buf, Bufsize, fp);
1230 xlabel = stringNew(buf + 14);
1231 xlabel[strlen(xlabel) - 1] =
'\0';
1232 ignores = fgets(buf, Bufsize, fp);
1233 ylabel = stringNew(buf + 14);
1234 ylabel[strlen(ylabel) - 1] =
'\0';
1236 gplot =
gplotCreate(rootname, outformat, title, xlabel, ylabel);
1237 LEPT_FREE(rootname);
1243 return (GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1245 sarrayDestroy(&gplot->
cmddata);
1251 ignore = fscanf(fp,
"Commandfile name: %511s\n", buf);
1252 stringReplace(&gplot->
cmdname, buf);
1253 ignore = fscanf(fp,
"\nCommandfile data:");
1254 gplot->
cmddata = sarrayReadStream(fp);
1255 ignore = fscanf(fp,
"\nDatafile names:");
1256 gplot->
datanames = sarrayReadStream(fp);
1257 ignore = fscanf(fp,
"\nPlot data:");
1258 gplot->
plotdata = sarrayReadStream(fp);
1259 ignore = fscanf(fp,
"\nPlot titles:");
1261 ignore = fscanf(fp,
"\nPlot styles:");
1264 ignore = fscanf(fp,
"Number of plots: %d\n", &gplot->
nplots);
1265 ignore = fscanf(fp,
"Output file name: %511s\n", buf);
1266 stringReplace(&gplot->
outname, buf);
1267 ignore = fscanf(fp,
"Axis scaling: %d\n", &gplot->
scaling);
1288 return ERROR_INT(
"filename not defined", __func__, 1);
1290 return ERROR_INT(
"gplot not defined", __func__, 1);
1292 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1293 return ERROR_INT(
"stream not opened", __func__, 1);
1295 fprintf(fp,
"Gplot Version %d\n", GPLOT_VERSION_NUMBER);
1296 fprintf(fp,
"Rootname: %s\n", gplot->
rootname);
1297 fprintf(fp,
"Output format: %d\n", gplot->
outformat);
1298 fprintf(fp,
"Title: %s\n", gplot->
title);
1299 fprintf(fp,
"X axis label: %s\n", gplot->
xlabel);
1300 fprintf(fp,
"Y axis label: %s\n", gplot->
ylabel);
1302 fprintf(fp,
"Commandfile name: %s\n", gplot->
cmdname);
1303 fprintf(fp,
"\nCommandfile data:");
1304 sarrayWriteStream(fp, gplot->
cmddata);
1305 fprintf(fp,
"\nDatafile names:");
1306 sarrayWriteStream(fp, gplot->
datanames);
1307 fprintf(fp,
"\nPlot data:");
1308 sarrayWriteStream(fp, gplot->
plotdata);
1309 fprintf(fp,
"\nPlot titles:");
1311 fprintf(fp,
"\nPlot styles:");
1314 fprintf(fp,
"Number of plots: %d\n", gplot->
nplots);
1315 fprintf(fp,
"Output file name: %s\n", gplot->
outname);
1316 fprintf(fp,
"Axis scaling: %d\n", gplot->
scaling);
GPLOT * gplotSimpleXYN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXYN()
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
PIX * gplotSimplePixN(NUMAA *naa, const char *title)
gplotSimplePixN()
GPLOT * gplotRead(const char *filename)
gplotRead()
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
PIX * gplotGeneralPixN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPixN()
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
l_ok gplotSimple2(NUMA *na1, NUMA *na2, l_int32 outformat, const char *outroot, const char *title)
gplotSimple2()
l_ok gplotWrite(const char *filename, GPLOT *gplot)
gplotWrite()
PIX * gplotGeneralPix2(NUMA *na1, NUMA *na2, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix2()
l_ok gplotGenCommandFile(GPLOT *gplot)
gplotGenCommandFile()
l_ok gplotSetScaling(GPLOT *gplot, l_int32 scaling)
gplotSetScaling()
PIX * gplotGeneralPix1(NUMA *na, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix1()
const char * gplotstylenames[]
l_ok gplotSimpleN(NUMAA *naa, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleN()
GPLOT * gplotSimpleXY1(NUMA *nax, NUMA *nay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY1()
PIX * gplotMakeOutputPix(GPLOT *gplot)
gplotMakeOutputPix()
GPLOT * gplotSimpleXY2(NUMA *nax, NUMA *nay1, NUMA *nay2, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY2()
l_ok gplotGenDataFiles(GPLOT *gplot)
gplotGenDataFiles()
PIX * gplotSimplePix2(NUMA *na1, NUMA *na2, const char *title)
gplotSimplePix2()
PIX * gplotSimplePix1(NUMA *na, const char *title)
gplotSimplePix1()
const char * gplotfileoutputs[]
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()
struct Sarray * datanames
struct Sarray * plotlabels