149#include <config_auto.h>
153#include "allheaders.h"
204 return (
GPLOT *)ERROR_PTR(
"rootname not defined", __func__, NULL);
205 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
206 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
207 outformat != GPLOT_PNM)
208 return (
GPLOT *)ERROR_PTR(
"outformat invalid", __func__, NULL);
209 stringCheckForChars(rootname,
"`;&|><\"?*$()", &badchar);
211 return (
GPLOT *)ERROR_PTR(
"invalid rootname", __func__, NULL);
213#if !defined(HAVE_LIBPNG)
214 if (outformat == GPLOT_PNG) {
215 L_WARNING(
"png library missing; output pnm format\n", __func__);
216 outformat = GPLOT_PNM;
220 gplot = (
GPLOT *)LEPT_CALLOC(1,
sizeof(
GPLOT));
221 gplot->
cmddata = sarrayCreate(0);
228 newroot = genPathname(rootname, NULL);
231 snprintf(buf, Bufsize,
"%s.cmd", rootname);
232 gplot->
cmdname = stringNew(buf);
233 if (outformat == GPLOT_PNG)
234 snprintf(buf, Bufsize,
"%s.png", newroot);
235 else if (outformat == GPLOT_PS)
236 snprintf(buf, Bufsize,
"%s.ps", newroot);
237 else if (outformat == GPLOT_EPS)
238 snprintf(buf, Bufsize,
"%s.eps", newroot);
239 else if (outformat == GPLOT_LATEX)
240 snprintf(buf, Bufsize,
"%s.tex", newroot);
241 else if (outformat == GPLOT_PNM)
242 snprintf(buf, Bufsize,
"%s.pnm", newroot);
243 gplot->
outname = stringNew(buf);
244 if (title) gplot->
title = stringNew(title);
245 if (xlabel) gplot->
xlabel = stringNew(xlabel);
246 if (ylabel) gplot->
ylabel = stringNew(ylabel);
262 if (pgplot == NULL) {
263 L_WARNING(
"ptr address is null!\n", __func__);
267 if ((gplot = *pgplot) == NULL)
272 sarrayDestroy(&gplot->
cmddata);
279 LEPT_FREE(gplot->
title);
325 const char *plotlabel)
328char emptystring[] =
"";
329char *datastr, *title;
331l_float32 valx, valy, startx, delx;
335 return ERROR_INT(
"gplot not defined", __func__, 1);
337 return ERROR_INT(
"nay not defined", __func__, 1);
338 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
339 return ERROR_INT(
"invalid plotstyle", __func__, 1);
341 if ((n = numaGetCount(nay)) == 0)
342 return ERROR_INT(
"no points to plot", __func__, 1);
343 if (nax && (n != numaGetCount(nax)))
344 return ERROR_INT(
"nax and nay sizes differ", __func__, 1);
345 if (n == 1 && plotstyle == GPLOT_LINES) {
346 L_INFO(
"only 1 pt; changing style to points\n", __func__);
347 plotstyle = GPLOT_POINTS;
351 numaGetParameters(nay, &startx, &delx);
354 title = stringNew(plotlabel);
362 snprintf(buf, Bufsize,
"%s.data.%d", gplot->
rootname, gplot->
nplots);
366 sa = sarrayCreate(n);
367 for (i = 0; i < n; i++) {
369 numaGetFValue(nax, i, &valx);
371 valx = startx + i * delx;
372 numaGetFValue(nay, i, &valy);
373 snprintf(buf, Bufsize,
"%f %f\n", valx, valy);
374 sarrayAddString(sa, buf,
L_COPY);
376 datastr = sarrayToString(sa, 0);
403 return ERROR_INT(
"gplot not defined", __func__, 1);
405 scaling != GPLOT_LOG_SCALE_X &&
406 scaling != GPLOT_LOG_SCALE_Y &&
407 scaling != GPLOT_LOG_SCALE_X_Y)
408 return ERROR_INT(
"invalid gplot scaling", __func__, 1);
431 return (
PIX *)ERROR_PTR(
"gplot not defined", __func__, NULL);
433 return (
PIX *)ERROR_PTR(
"output format not an image", __func__, NULL);
436 return (
PIX *)ERROR_PTR(
"plot output not made", __func__, NULL);
437 return pixRead(gplot->
outname);
467 return ERROR_INT(
"gplot not defined", __func__, 1);
470 L_INFO(
"running gnuplot is disabled; "
471 "use setLeptDebugOK(1) to enable\n", __func__);
476 return ERROR_INT(
"iOS 11 does not support system()", __func__, 0);
481 cmdname = genPathname(gplot->
cmdname, NULL);
484 snprintf(buf, Bufsize,
"gnuplot %s", cmdname);
486 snprintf(buf, Bufsize,
"wgnuplot %s", cmdname);
489 callSystemDebug(buf);
505char *cmdstr, *plotlabel, *dataname;
506l_int32 i, plotstyle, nplots;
510 return ERROR_INT(
"gplot not defined", __func__, 1);
517 snprintf(buf, Bufsize,
"set title '%s'", gplot->
title);
521 snprintf(buf, Bufsize,
"set xlabel '%s'", gplot->
xlabel);
525 snprintf(buf, Bufsize,
"set ylabel '%s'", gplot->
ylabel);
531 snprintf(buf, Bufsize,
"set terminal png; set output '%s'",
533 }
else if (gplot->
outformat == GPLOT_PS) {
534 snprintf(buf, Bufsize,
"set terminal postscript; set output '%s'",
536 }
else if (gplot->
outformat == GPLOT_EPS) {
537 snprintf(buf, Bufsize,
"set terminal postscript eps; set output '%s'",
539 }
else if (gplot->
outformat == GPLOT_LATEX) {
540 snprintf(buf, Bufsize,
"set terminal latex; set output '%s'",
542 }
else if (gplot->
outformat == GPLOT_PNM) {
543 snprintf(buf, Bufsize,
"set terminal pbm color; set output '%s'",
548 if (gplot->
scaling == GPLOT_LOG_SCALE_X ||
549 gplot->
scaling == GPLOT_LOG_SCALE_X_Y) {
550 snprintf(buf, Bufsize,
"set logscale x");
553 if (gplot->
scaling == GPLOT_LOG_SCALE_Y ||
554 gplot->
scaling == GPLOT_LOG_SCALE_X_Y) {
555 snprintf(buf, Bufsize,
"set logscale y");
559 nplots = sarrayGetCount(gplot->
datanames);
560 for (i = 0; i < nplots; i++) {
563 numaGetIValue(gplot->
plotstyles, i, &plotstyle);
565 snprintf(buf, Bufsize,
"plot '%s' title '%s' %s",
569 snprintf(buf, Bufsize,
"plot '%s' title '%s' %s, \\",
571 else if (i < nplots - 1)
572 snprintf(buf, Bufsize,
" '%s' title '%s' %s, \\",
575 snprintf(buf, Bufsize,
" '%s' title '%s' %s",
582 cmdstr = sarrayToString(gplot->
cmddata, 1);
583 if ((fp = fopenWriteStream(gplot->
cmdname,
"w")) == NULL) {
585 return ERROR_INT_1(
"cmd stream not opened", cmdstr, __func__, 1);
587 fwrite(cmdstr, 1, strlen(cmdstr), fp);
610char *plotdata, *dataname;
615 return ERROR_INT(
"gplot not defined", __func__, 1);
617 nplots = sarrayGetCount(gplot->
datanames);
618 for (i = 0; i < nplots; i++) {
621 if ((fp = fopen(dataname,
"w")) == NULL)
622 return ERROR_INT_1(
"datafile stream not opened",
623 dataname, __func__, 1);
624 fwrite(plotdata, 1, strlen(plotdata), fp);
662 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
664 return ERROR_INT(
"failed to generate plot", __func__, 1);
700 outformat, outroot, title);
702 return ERROR_INT(
"failed to generate plot", __func__, 1);
736 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
738 return ERROR_INT(
"failed to generate plot", __func__, 1);
764static l_atomic index;
769 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
771 lept_mkdir(
"lept/gplot/pix");
772 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix1.%d", index++);
773 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, GPLOT_PNG, buf, title);
775 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
779 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
806static l_atomic index;
811 return (
PIX *)ERROR_PTR(
"both na1, na2 not defined", __func__, NULL);
813 lept_mkdir(
"lept/gplot/pix");
814 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix2.%d", index++);
815 gplot =
gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES, GPLOT_PNG, buf, title);
817 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
821 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
847static l_atomic index;
852 return (
PIX *)ERROR_PTR(
"naa not defined", __func__, NULL);
854 lept_mkdir(
"lept/gplot/pix");
855 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pixN.%d", index++);
856 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, GPLOT_PNG, buf, title);
858 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
862 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
903 return (
GPLOT *)ERROR_PTR(
"nay not defined", __func__, NULL);
904 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
905 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
906 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
907 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
908 outformat != GPLOT_PNM)
909 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
911 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
913 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
914 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
959 return (
GPLOT *)ERROR_PTR(
"nay1 and nay2 not both defined",
961 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
962 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
963 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
964 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
965 outformat != GPLOT_PNM)
966 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
968 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
970 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
971 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1009 const char *outroot,
1017 return (
GPLOT *)ERROR_PTR(
"naay not defined", __func__, NULL);
1018 if ((n = numaaGetCount(naay)) == 0)
1019 return (
GPLOT *)ERROR_PTR(
"no numa in array", __func__, NULL);
1020 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1021 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1022 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
1023 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
1024 outformat != GPLOT_PNM)
1025 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
1027 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
1029 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
1030 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1031 for (i = 0; i < n; i++) {
1032 nay = numaaGetNuma(naay, i,
L_CLONE);
1062 const char *rootname,
1071 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
1072 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1073 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1075 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1077 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1079 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1110 const char *rootname,
1119 return (
PIX *)ERROR_PTR(
"na1 not defined", __func__, NULL);
1121 return (
PIX *)ERROR_PTR(
"na2 not defined", __func__, NULL);
1122 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1123 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1125 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1127 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1129 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1160 const char *rootname,
1171 return (
PIX *)ERROR_PTR(
"nax not defined", __func__, NULL);
1173 return (
PIX *)ERROR_PTR(
"naay not defined", __func__, NULL);
1174 if ((n = numaaGetCount(naay)) == 0)
1175 return (
PIX *)ERROR_PTR(
"no numa in array", __func__, NULL);
1176 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1177 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1179 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1181 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1183 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1184 for (i = 0; i < n; i++) {
1185 nay = numaaGetNuma(naay, i,
L_CLONE);
1208char *rootname, *title, *xlabel, *ylabel, *ignores;
1209l_int32 outformat, ret, version, ignore;
1214 return (
GPLOT *)ERROR_PTR(
"filename not defined", __func__, NULL);
1216 if ((fp = fopenReadStream(filename)) == NULL)
1217 return (
GPLOT *)ERROR_PTR_1(
"stream not opened",
1218 filename, __func__, NULL);
1220 ret = fscanf(fp,
"Gplot Version %d\n", &version);
1223 return (
GPLOT *)ERROR_PTR_1(
"not a gplot file",
1224 filename, __func__, NULL);
1226 if (version != GPLOT_VERSION_NUMBER) {
1228 return (
GPLOT *)ERROR_PTR_1(
"invalid gplot version",
1229 filename, __func__, NULL);
1232 ignore = fscanf(fp,
"Rootname: %511s\n", buf);
1233 rootname = stringNew(buf);
1234 ignore = fscanf(fp,
"Output format: %d\n", &outformat);
1235 ignores = fgets(buf, Bufsize, fp);
1236 title = stringNew(buf + 7);
1237 title[strlen(title) - 1] =
'\0';
1238 ignores = fgets(buf, Bufsize, fp);
1239 xlabel = stringNew(buf + 14);
1240 xlabel[strlen(xlabel) - 1] =
'\0';
1241 ignores = fgets(buf, Bufsize, fp);
1242 ylabel = stringNew(buf + 14);
1243 ylabel[strlen(ylabel) - 1] =
'\0';
1245 gplot =
gplotCreate(rootname, outformat, title, xlabel, ylabel);
1246 LEPT_FREE(rootname);
1252 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1254 sarrayDestroy(&gplot->
cmddata);
1260 ignore = fscanf(fp,
"Commandfile name: %s\n", buf);
1261 stringReplace(&gplot->
cmdname, buf);
1262 ignore = fscanf(fp,
"\nCommandfile data:");
1263 gplot->
cmddata = sarrayReadStream(fp);
1264 ignore = fscanf(fp,
"\nDatafile names:");
1265 gplot->
datanames = sarrayReadStream(fp);
1266 ignore = fscanf(fp,
"\nPlot data:");
1267 gplot->
plotdata = sarrayReadStream(fp);
1268 ignore = fscanf(fp,
"\nPlot titles:");
1270 ignore = fscanf(fp,
"\nPlot styles:");
1273 ignore = fscanf(fp,
"Number of plots: %d\n", &gplot->
nplots);
1274 ignore = fscanf(fp,
"Output file name: %s\n", buf);
1275 stringReplace(&gplot->
outname, buf);
1276 ignore = fscanf(fp,
"Axis scaling: %d\n", &gplot->
scaling);
1297 return ERROR_INT(
"filename not defined", __func__, 1);
1299 return ERROR_INT(
"gplot not defined", __func__, 1);
1301 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1302 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1304 fprintf(fp,
"Gplot Version %d\n", GPLOT_VERSION_NUMBER);
1305 fprintf(fp,
"Rootname: %s\n", gplot->
rootname);
1306 fprintf(fp,
"Output format: %d\n", gplot->
outformat);
1307 fprintf(fp,
"Title: %s\n", gplot->
title);
1308 fprintf(fp,
"X axis label: %s\n", gplot->
xlabel);
1309 fprintf(fp,
"Y axis label: %s\n", gplot->
ylabel);
1311 fprintf(fp,
"Commandfile name: %s\n", gplot->
cmdname);
1312 fprintf(fp,
"\nCommandfile data:");
1313 sarrayWriteStream(fp, gplot->
cmddata);
1314 fprintf(fp,
"\nDatafile names:");
1315 sarrayWriteStream(fp, gplot->
datanames);
1316 fprintf(fp,
"\nPlot data:");
1317 sarrayWriteStream(fp, gplot->
plotdata);
1318 fprintf(fp,
"\nPlot titles:");
1320 fprintf(fp,
"\nPlot styles:");
1323 fprintf(fp,
"Number of plots: %d\n", gplot->
nplots);
1324 fprintf(fp,
"Output file name: %s\n", gplot->
outname);
1325 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