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) {
584 L_ERROR(
"stream not opened for command: %s\n", __func__, cmdstr);
588 fwrite(cmdstr, 1, strlen(cmdstr), fp);
611char *plotdata, *dataname;
616 return ERROR_INT(
"gplot not defined", __func__, 1);
618 nplots = sarrayGetCount(gplot->
datanames);
619 for (i = 0; i < nplots; i++) {
622 if ((fp = fopen(dataname,
"w")) == NULL)
623 return ERROR_INT_1(
"datafile stream not opened",
624 dataname, __func__, 1);
625 fwrite(plotdata, 1, strlen(plotdata), fp);
663 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
665 return ERROR_INT(
"failed to generate plot", __func__, 1);
701 outformat, outroot, title);
703 return ERROR_INT(
"failed to generate plot", __func__, 1);
737 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
739 return ERROR_INT(
"failed to generate plot", __func__, 1);
765static l_atomic index;
770 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
772 lept_mkdir(
"lept/gplot/pix");
773 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix1.%d", index++);
774 gplot =
gplotSimpleXY1(NULL, na, GPLOT_LINES, GPLOT_PNG, buf, title);
776 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
780 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
807static l_atomic index;
812 return (
PIX *)ERROR_PTR(
"both na1, na2 not defined", __func__, NULL);
814 lept_mkdir(
"lept/gplot/pix");
815 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pix2.%d", index++);
816 gplot =
gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES, GPLOT_PNG, buf, title);
818 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
822 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
848static l_atomic index;
853 return (
PIX *)ERROR_PTR(
"naa not defined", __func__, NULL);
855 lept_mkdir(
"lept/gplot/pix");
856 snprintf(buf,
sizeof(buf),
"/tmp/lept/gplot/pixN.%d", index++);
857 gplot =
gplotSimpleXYN(NULL, naa, GPLOT_LINES, GPLOT_PNG, buf, title);
859 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
863 return (
PIX *)ERROR_PTR(
"failed to generate plot", __func__, NULL);
904 return (
GPLOT *)ERROR_PTR(
"nay not defined", __func__, NULL);
905 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
906 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
907 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
908 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
909 outformat != GPLOT_PNM)
910 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
912 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
914 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
915 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
960 return (
GPLOT *)ERROR_PTR(
"nay1 and nay2 not both defined",
962 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
963 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
964 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
965 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
966 outformat != GPLOT_PNM)
967 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
969 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
971 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
972 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1010 const char *outroot,
1018 return (
GPLOT *)ERROR_PTR(
"naay not defined", __func__, NULL);
1019 if ((n = numaaGetCount(naay)) == 0)
1020 return (
GPLOT *)ERROR_PTR(
"no numa in array", __func__, NULL);
1021 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1022 return (
GPLOT *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1023 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
1024 outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
1025 outformat != GPLOT_PNM)
1026 return (
GPLOT *)ERROR_PTR(
"invalid outformat", __func__, NULL);
1028 return (
GPLOT *)ERROR_PTR(
"outroot not specified", __func__, NULL);
1030 if ((gplot =
gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
1031 return (
GPLOT *)ERROR_PTR(
"gplot not made", __func__, NULL);
1032 for (i = 0; i < n; i++) {
1033 nay = numaaGetNuma(naay, i,
L_CLONE);
1063 const char *rootname,
1072 return (
PIX *)ERROR_PTR(
"na not defined", __func__, NULL);
1073 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1074 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1076 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1078 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1080 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1111 const char *rootname,
1120 return (
PIX *)ERROR_PTR(
"na1 not defined", __func__, NULL);
1122 return (
PIX *)ERROR_PTR(
"na2 not defined", __func__, NULL);
1123 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1124 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1126 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1128 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1130 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1161 const char *rootname,
1172 return (
PIX *)ERROR_PTR(
"nax not defined", __func__, NULL);
1174 return (
PIX *)ERROR_PTR(
"naay not defined", __func__, NULL);
1175 if ((n = numaaGetCount(naay)) == 0)
1176 return (
PIX *)ERROR_PTR(
"no numa in array", __func__, NULL);
1177 if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1178 return (
PIX *)ERROR_PTR(
"invalid plotstyle", __func__, NULL);
1180 return (
PIX *)ERROR_PTR(
"rootname not defined", __func__, NULL);
1182 gplot =
gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1184 return (
PIX *)ERROR_PTR(
"gplot not made", __func__, NULL);
1185 for (i = 0; i < n; i++) {
1186 nay = numaaGetNuma(naay, i,
L_CLONE);
1209char *rootname, *title, *xlabel, *ylabel, *ignores;
1210l_int32 outformat, ret, version, ignore;
1215 return (
GPLOT *)ERROR_PTR(
"filename not defined", __func__, NULL);
1217 if ((fp = fopenReadStream(filename)) == NULL)
1218 return (
GPLOT *)ERROR_PTR_1(
"stream not opened",
1219 filename, __func__, NULL);
1221 ret = fscanf(fp,
"Gplot Version %d\n", &version);
1224 return (
GPLOT *)ERROR_PTR_1(
"not a gplot file",
1225 filename, __func__, NULL);
1227 if (version != GPLOT_VERSION_NUMBER) {
1229 return (
GPLOT *)ERROR_PTR_1(
"invalid gplot version",
1230 filename, __func__, NULL);
1233 ignore = fscanf(fp,
"Rootname: %511s\n", buf);
1234 rootname = stringNew(buf);
1235 ignore = fscanf(fp,
"Output format: %d\n", &outformat);
1236 ignores = fgets(buf, Bufsize, fp);
1237 title = stringNew(buf + 7);
1238 title[strlen(title) - 1] =
'\0';
1239 ignores = fgets(buf, Bufsize, fp);
1240 xlabel = stringNew(buf + 14);
1241 xlabel[strlen(xlabel) - 1] =
'\0';
1242 ignores = fgets(buf, Bufsize, fp);
1243 ylabel = stringNew(buf + 14);
1244 ylabel[strlen(ylabel) - 1] =
'\0';
1246 gplot =
gplotCreate(rootname, outformat, title, xlabel, ylabel);
1247 LEPT_FREE(rootname);
1253 return (
GPLOT *)ERROR_PTR_1(
"gplot not made", filename, __func__, NULL);
1255 sarrayDestroy(&gplot->
cmddata);
1261 ignore = fscanf(fp,
"Commandfile name: %s\n", buf);
1262 stringReplace(&gplot->
cmdname, buf);
1263 ignore = fscanf(fp,
"\nCommandfile data:");
1264 gplot->
cmddata = sarrayReadStream(fp);
1265 ignore = fscanf(fp,
"\nDatafile names:");
1266 gplot->
datanames = sarrayReadStream(fp);
1267 ignore = fscanf(fp,
"\nPlot data:");
1268 gplot->
plotdata = sarrayReadStream(fp);
1269 ignore = fscanf(fp,
"\nPlot titles:");
1271 ignore = fscanf(fp,
"\nPlot styles:");
1274 ignore = fscanf(fp,
"Number of plots: %d\n", &gplot->
nplots);
1275 ignore = fscanf(fp,
"Output file name: %s\n", buf);
1276 stringReplace(&gplot->
outname, buf);
1277 ignore = fscanf(fp,
"Axis scaling: %d\n", &gplot->
scaling);
1298 return ERROR_INT(
"filename not defined", __func__, 1);
1300 return ERROR_INT(
"gplot not defined", __func__, 1);
1302 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1303 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1305 fprintf(fp,
"Gplot Version %d\n", GPLOT_VERSION_NUMBER);
1306 fprintf(fp,
"Rootname: %s\n", gplot->
rootname);
1307 fprintf(fp,
"Output format: %d\n", gplot->
outformat);
1308 fprintf(fp,
"Title: %s\n", gplot->
title);
1309 fprintf(fp,
"X axis label: %s\n", gplot->
xlabel);
1310 fprintf(fp,
"Y axis label: %s\n", gplot->
ylabel);
1312 fprintf(fp,
"Commandfile name: %s\n", gplot->
cmdname);
1313 fprintf(fp,
"\nCommandfile data:");
1314 sarrayWriteStream(fp, gplot->
cmddata);
1315 fprintf(fp,
"\nDatafile names:");
1316 sarrayWriteStream(fp, gplot->
datanames);
1317 fprintf(fp,
"\nPlot data:");
1318 sarrayWriteStream(fp, gplot->
plotdata);
1319 fprintf(fp,
"\nPlot titles:");
1321 fprintf(fp,
"\nPlot styles:");
1324 fprintf(fp,
"Number of plots: %d\n", gplot->
nplots);
1325 fprintf(fp,
"Output file name: %s\n", gplot->
outname);
1326 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