Leptonica 1.84.1
Image processing and image analysis suite
Loading...
Searching...
No Matches
gplot.c
Go to the documentation of this file.
1/*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
148#ifdef HAVE_CONFIG_H
149#include <config_auto.h>
150#endif /* HAVE_CONFIG_H */
151
152#include <string.h>
153#include "allheaders.h"
154
155#define Bufsize 512 /* hardcoded below in fscanf */
156
157const char *gplotstylenames[] = {"with lines",
158 "with points",
159 "with impulses",
160 "with linespoints",
161 "with dots"};
162const char *gplotfileoutputs[] = {"",
163 "PNG",
164 "PS",
165 "EPS",
166 "LATEX",
167 "PNM"};
168
169
170/*-----------------------------------------------------------------*
171 * Basic Plotting Functions *
172 *-----------------------------------------------------------------*/
191GPLOT *
192gplotCreate(const char *rootname,
193 l_int32 outformat,
194 const char *title,
195 const char *xlabel,
196 const char *ylabel)
197{
198char *newroot;
199char buf[Bufsize];
200l_int32 badchar;
201GPLOT *gplot;
202
203 if (!rootname)
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);
210 if (badchar) /* danger of command injection */
211 return (GPLOT *)ERROR_PTR("invalid rootname", __func__, NULL);
212
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;
217 }
218#endif
219
220 gplot = (GPLOT *)LEPT_CALLOC(1, sizeof(GPLOT));
221 gplot->cmddata = sarrayCreate(0);
222 gplot->datanames = sarrayCreate(0);
223 gplot->plotdata = sarrayCreate(0);
224 gplot->plotlabels = sarrayCreate(0);
225 gplot->plotstyles = numaCreate(0);
226
227 /* Save title, labels, rootname, outformat, cmdname, outname */
228 newroot = genPathname(rootname, NULL);
229 gplot->rootname = newroot;
230 gplot->outformat = outformat;
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);
247
248 return gplot;
249}
250
251
257void
259{
260GPLOT *gplot;
261
262 if (pgplot == NULL) {
263 L_WARNING("ptr address is null!\n", __func__);
264 return;
265 }
266
267 if ((gplot = *pgplot) == NULL)
268 return;
269
270 LEPT_FREE(gplot->rootname);
271 LEPT_FREE(gplot->cmdname);
272 sarrayDestroy(&gplot->cmddata);
273 sarrayDestroy(&gplot->datanames);
274 sarrayDestroy(&gplot->plotdata);
275 sarrayDestroy(&gplot->plotlabels);
276 numaDestroy(&gplot->plotstyles);
277 LEPT_FREE(gplot->outname);
278 if (gplot->title)
279 LEPT_FREE(gplot->title);
280 if (gplot->xlabel)
281 LEPT_FREE(gplot->xlabel);
282 if (gplot->ylabel)
283 LEPT_FREE(gplot->ylabel);
284
285 LEPT_FREE(gplot);
286 *pgplot = NULL;
287}
288
289
320l_ok
322 NUMA *nax,
323 NUMA *nay,
324 l_int32 plotstyle,
325 const char *plotlabel)
326{
327char buf[Bufsize];
328char emptystring[] = "";
329char *datastr, *title;
330l_int32 n, i;
331l_float32 valx, valy, startx, delx;
332SARRAY *sa;
333
334 if (!gplot)
335 return ERROR_INT("gplot not defined", __func__, 1);
336 if (!nay)
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);
340
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;
348 }
349
350 /* Save plotstyle and plotlabel */
351 numaGetParameters(nay, &startx, &delx);
352 numaAddNumber(gplot->plotstyles, plotstyle);
353 if (plotlabel) {
354 title = stringNew(plotlabel);
355 sarrayAddString(gplot->plotlabels, title, L_INSERT);
356 } else {
357 sarrayAddString(gplot->plotlabels, emptystring, L_COPY);
358 }
359
360 /* Generate and save data filename */
361 gplot->nplots++;
362 snprintf(buf, Bufsize, "%s.data.%d", gplot->rootname, gplot->nplots);
363 sarrayAddString(gplot->datanames, buf, L_COPY);
364
365 /* Generate data and save as a string */
366 sa = sarrayCreate(n);
367 for (i = 0; i < n; i++) {
368 if (nax)
369 numaGetFValue(nax, i, &valx);
370 else
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);
375 }
376 datastr = sarrayToString(sa, 0);
377 sarrayAddString(gplot->plotdata, datastr, L_INSERT);
378 sarrayDestroy(&sa);
379
380 return 0;
381}
382
383
398l_ok
400 l_int32 scaling)
401{
402 if (!gplot)
403 return ERROR_INT("gplot not defined", __func__, 1);
404 if (scaling != GPLOT_LINEAR_SCALE &&
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);
409 gplot->scaling = scaling;
410 return 0;
411}
412
413
427PIX *
429{
430 if (!gplot)
431 return (PIX *)ERROR_PTR("gplot not defined", __func__, NULL);
432 if (gplot->outformat != GPLOT_PNG && gplot->outformat != GPLOT_PNM)
433 return (PIX *)ERROR_PTR("output format not an image", __func__, NULL);
434
435 if (gplotMakeOutput(gplot))
436 return (PIX *)ERROR_PTR("plot output not made", __func__, NULL);
437 return pixRead(gplot->outname);
438}
439
440
460l_ok
462{
463char buf[Bufsize];
464char *cmdname;
465
466 if (!gplot)
467 return ERROR_INT("gplot not defined", __func__, 1);
468
469 if (!LeptDebugOK) {
470 L_INFO("running gnuplot is disabled; "
471 "use setLeptDebugOK(1) to enable\n", __func__);
472 return 0;
473 }
474
475#ifdef OS_IOS /* iOS 11 does not support system() */
476 return ERROR_INT("iOS 11 does not support system()", __func__, 0);
477#endif /* OS_IOS */
478
479 gplotGenCommandFile(gplot);
480 gplotGenDataFiles(gplot);
481 cmdname = genPathname(gplot->cmdname, NULL);
482
483#ifndef _WIN32
484 snprintf(buf, Bufsize, "gnuplot %s", cmdname);
485#else
486 snprintf(buf, Bufsize, "wgnuplot %s", cmdname);
487#endif /* _WIN32 */
488
489 callSystemDebug(buf); /* gnuplot || wgnuplot */
490 LEPT_FREE(cmdname);
491 return 0;
492}
493
494
501l_ok
503{
504char buf[Bufsize];
505char *cmdstr, *plotlabel, *dataname;
506l_int32 i, plotstyle, nplots;
507FILE *fp;
508
509 if (!gplot)
510 return ERROR_INT("gplot not defined", __func__, 1);
511
512 /* Remove any previous command data */
513 sarrayClear(gplot->cmddata);
514
515 /* Generate command data instructions */
516 if (gplot->title) { /* set title */
517 snprintf(buf, Bufsize, "set title '%s'", gplot->title);
518 sarrayAddString(gplot->cmddata, buf, L_COPY);
519 }
520 if (gplot->xlabel) { /* set xlabel */
521 snprintf(buf, Bufsize, "set xlabel '%s'", gplot->xlabel);
522 sarrayAddString(gplot->cmddata, buf, L_COPY);
523 }
524 if (gplot->ylabel) { /* set ylabel */
525 snprintf(buf, Bufsize, "set ylabel '%s'", gplot->ylabel);
526 sarrayAddString(gplot->cmddata, buf, L_COPY);
527 }
528
529 /* Set terminal type and output */
530 if (gplot->outformat == GPLOT_PNG) {
531 snprintf(buf, Bufsize, "set terminal png; set output '%s'",
532 gplot->outname);
533 } else if (gplot->outformat == GPLOT_PS) {
534 snprintf(buf, Bufsize, "set terminal postscript; set output '%s'",
535 gplot->outname);
536 } else if (gplot->outformat == GPLOT_EPS) {
537 snprintf(buf, Bufsize, "set terminal postscript eps; set output '%s'",
538 gplot->outname);
539 } else if (gplot->outformat == GPLOT_LATEX) {
540 snprintf(buf, Bufsize, "set terminal latex; set output '%s'",
541 gplot->outname);
542 } else if (gplot->outformat == GPLOT_PNM) {
543 snprintf(buf, Bufsize, "set terminal pbm color; set output '%s'",
544 gplot->outname);
545 }
546 sarrayAddString(gplot->cmddata, buf, L_COPY);
547
548 if (gplot->scaling == GPLOT_LOG_SCALE_X ||
549 gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
550 snprintf(buf, Bufsize, "set logscale x");
551 sarrayAddString(gplot->cmddata, buf, L_COPY);
552 }
553 if (gplot->scaling == GPLOT_LOG_SCALE_Y ||
554 gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
555 snprintf(buf, Bufsize, "set logscale y");
556 sarrayAddString(gplot->cmddata, buf, L_COPY);
557 }
558
559 nplots = sarrayGetCount(gplot->datanames);
560 for (i = 0; i < nplots; i++) {
561 plotlabel = sarrayGetString(gplot->plotlabels, i, L_NOCOPY);
562 dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
563 numaGetIValue(gplot->plotstyles, i, &plotstyle);
564 if (nplots == 1) {
565 snprintf(buf, Bufsize, "plot '%s' title '%s' %s",
566 dataname, plotlabel, gplotstylenames[plotstyle]);
567 } else {
568 if (i == 0)
569 snprintf(buf, Bufsize, "plot '%s' title '%s' %s, \\",
570 dataname, plotlabel, gplotstylenames[plotstyle]);
571 else if (i < nplots - 1)
572 snprintf(buf, Bufsize, " '%s' title '%s' %s, \\",
573 dataname, plotlabel, gplotstylenames[plotstyle]);
574 else
575 snprintf(buf, Bufsize, " '%s' title '%s' %s",
576 dataname, plotlabel, gplotstylenames[plotstyle]);
577 }
578 sarrayAddString(gplot->cmddata, buf, L_COPY);
579 }
580
581 /* Write command data to file */
582 cmdstr = sarrayToString(gplot->cmddata, 1);
583 if ((fp = fopenWriteStream(gplot->cmdname, "w")) == NULL) {
584 LEPT_FREE(cmdstr);
585 return ERROR_INT_1("cmd stream not opened", cmdstr, __func__, 1);
586 }
587 fwrite(cmdstr, 1, strlen(cmdstr), fp);
588 fclose(fp);
589 LEPT_FREE(cmdstr);
590 return 0;
591}
592
593
607l_ok
609{
610char *plotdata, *dataname;
611l_int32 i, nplots;
612FILE *fp;
613
614 if (!gplot)
615 return ERROR_INT("gplot not defined", __func__, 1);
616
617 nplots = sarrayGetCount(gplot->datanames);
618 for (i = 0; i < nplots; i++) {
619 plotdata = sarrayGetString(gplot->plotdata, i, L_NOCOPY);
620 dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
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);
625 fclose(fp);
626 }
627
628 return 0;
629}
630
631
632/*-----------------------------------------------------------------*
633 * Quick one-line plots *
634 *-----------------------------------------------------------------*/
654l_ok
656 l_int32 outformat,
657 const char *outroot,
658 const char *title)
659{
660GPLOT *gplot;
661
662 gplot = gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
663 if (!gplot)
664 return ERROR_INT("failed to generate plot", __func__, 1);
665 gplotDestroy(&gplot);
666 return 0;
667}
668
669
690l_ok
692 NUMA *na2,
693 l_int32 outformat,
694 const char *outroot,
695 const char *title)
696{
697GPLOT *gplot;
698
699 gplot = gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES,
700 outformat, outroot, title);
701 if (!gplot)
702 return ERROR_INT("failed to generate plot", __func__, 1);
703 gplotDestroy(&gplot);
704 return 0;
705}
706
707
728l_ok
730 l_int32 outformat,
731 const char *outroot,
732 const char *title)
733{
734GPLOT *gplot;
735
736 gplot = gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
737 if (!gplot)
738 return ERROR_INT("failed to generate plot", __func__, 1);
739 gplotDestroy(&gplot);
740 return 0;
741}
742
743
759PIX *
761 const char *title)
762{
763char buf[64];
764static l_atomic index;
765GPLOT *gplot;
766PIX *pix;
767
768 if (!na)
769 return (PIX *)ERROR_PTR("na not defined", __func__, NULL);
770
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);
774 if (!gplot)
775 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
776 pix = pixRead(gplot->outname);
777 gplotDestroy(&gplot);
778 if (!pix)
779 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
780 return pix;
781}
782
783
800PIX *
802 NUMA *na2,
803 const char *title)
804{
805char buf[64];
806static l_atomic index;
807GPLOT *gplot;
808PIX *pix;
809
810 if (!na1 || !na2)
811 return (PIX *)ERROR_PTR("both na1, na2 not defined", __func__, NULL);
812
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);
816 if (!gplot)
817 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
818 pix = pixRead(gplot->outname);
819 gplotDestroy(&gplot);
820 if (!pix)
821 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
822 return pix;
823}
824
825
842PIX *
844 const char *title)
845{
846char buf[64];
847static l_atomic index;
848GPLOT *gplot;
849PIX *pix;
850
851 if (!naa)
852 return (PIX *)ERROR_PTR("naa not defined", __func__, NULL);
853
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);
857 if (!gplot)
858 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
859 pix = pixRead(gplot->outname);
860 gplotDestroy(&gplot);
861 if (!pix)
862 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
863 return pix;
864}
865
866
892GPLOT *
894 NUMA *nay,
895 l_int32 plotstyle,
896 l_int32 outformat,
897 const char *outroot,
898 const char *title)
899{
900GPLOT *gplot;
901
902 if (!nay)
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);
910 if (!outroot)
911 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
912
913 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
914 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
915 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
916 gplotMakeOutput(gplot);
917 return gplot;
918}
919
920
947GPLOT *
949 NUMA *nay1,
950 NUMA *nay2,
951 l_int32 plotstyle,
952 l_int32 outformat,
953 const char *outroot,
954 const char *title)
955{
956GPLOT *gplot;
957
958 if (!nay1 || !nay2)
959 return (GPLOT *)ERROR_PTR("nay1 and nay2 not both defined",
960 __func__, NULL);
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);
967 if (!outroot)
968 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
969
970 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
971 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
972 gplotAddPlot(gplot, nax, nay1, plotstyle, NULL);
973 gplotAddPlot(gplot, nax, nay2, plotstyle, NULL);
974 gplotMakeOutput(gplot);
975 return gplot;
976}
977
978
1004GPLOT *
1006 NUMAA *naay,
1007 l_int32 plotstyle,
1008 l_int32 outformat,
1009 const char *outroot,
1010 const char *title)
1011{
1012l_int32 i, n;
1013GPLOT *gplot;
1014NUMA *nay;
1015
1016 if (!naay)
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);
1026 if (!outroot)
1027 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
1028
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);
1033 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1034 numaDestroy(&nay);
1035 }
1036 gplotMakeOutput(gplot);
1037 return gplot;
1038}
1039
1040
1059PIX *
1061 l_int32 plotstyle,
1062 const char *rootname,
1063 const char *title,
1064 const char *xlabel,
1065 const char *ylabel)
1066{
1067GPLOT *gplot;
1068PIX *pix;
1069
1070 if (!na)
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);
1074 if (!rootname)
1075 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1076
1077 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1078 if (!gplot)
1079 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1080 gplotAddPlot(gplot, NULL, na, plotstyle, NULL);
1081 pix = gplotMakeOutputPix(gplot);
1082 gplotDestroy(&gplot);
1083 return pix;
1084}
1085
1086
1106PIX *
1108 NUMA *na2,
1109 l_int32 plotstyle,
1110 const char *rootname,
1111 const char *title,
1112 const char *xlabel,
1113 const char *ylabel)
1114{
1115GPLOT *gplot;
1116PIX *pix;
1117
1118 if (!na1)
1119 return (PIX *)ERROR_PTR("na1 not defined", __func__, NULL);
1120 if (!na2)
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);
1124 if (!rootname)
1125 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1126
1127 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1128 if (!gplot)
1129 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1130 gplotAddPlot(gplot, na1, na2, plotstyle, NULL);
1131 pix = gplotMakeOutputPix(gplot);
1132 gplotDestroy(&gplot);
1133 return pix;
1134}
1135
1136
1156PIX *
1158 NUMAA *naay,
1159 l_int32 plotstyle,
1160 const char *rootname,
1161 const char *title,
1162 const char *xlabel,
1163 const char *ylabel)
1164{
1165l_int32 i, n;
1166GPLOT *gplot;
1167NUMA *nay;
1168PIX *pix;
1169
1170 if (!nax)
1171 return (PIX *)ERROR_PTR("nax not defined", __func__, NULL);
1172 if (!naay)
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);
1178 if (!rootname)
1179 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1180
1181 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1182 if (!gplot)
1183 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1184 for (i = 0; i < n; i++) {
1185 nay = numaaGetNuma(naay, i, L_CLONE);
1186 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1187 numaDestroy(&nay);
1188 }
1189 pix = gplotMakeOutputPix(gplot);
1190 gplotDestroy(&gplot);
1191 return pix;
1192}
1193
1194
1195/*-----------------------------------------------------------------*
1196 * Serialize for I/O *
1197 *-----------------------------------------------------------------*/
1204GPLOT *
1205gplotRead(const char *filename)
1206{
1207char buf[Bufsize];
1208char *rootname, *title, *xlabel, *ylabel, *ignores;
1209l_int32 outformat, ret, version, ignore;
1210FILE *fp;
1211GPLOT *gplot;
1212
1213 if (!filename)
1214 return (GPLOT *)ERROR_PTR("filename not defined", __func__, NULL);
1215
1216 if ((fp = fopenReadStream(filename)) == NULL)
1217 return (GPLOT *)ERROR_PTR_1("stream not opened",
1218 filename, __func__, NULL);
1219
1220 ret = fscanf(fp, "Gplot Version %d\n", &version);
1221 if (ret != 1) {
1222 fclose(fp);
1223 return (GPLOT *)ERROR_PTR_1("not a gplot file",
1224 filename, __func__, NULL);
1225 }
1226 if (version != GPLOT_VERSION_NUMBER) {
1227 fclose(fp);
1228 return (GPLOT *)ERROR_PTR_1("invalid gplot version",
1229 filename, __func__, NULL);
1230 }
1231
1232 ignore = fscanf(fp, "Rootname: %511s\n", buf); /* Bufsize - 1 */
1233 rootname = stringNew(buf);
1234 ignore = fscanf(fp, "Output format: %d\n", &outformat);
1235 ignores = fgets(buf, Bufsize, fp); /* Title: ... */
1236 title = stringNew(buf + 7);
1237 title[strlen(title) - 1] = '\0';
1238 ignores = fgets(buf, Bufsize, fp); /* X axis label: ... */
1239 xlabel = stringNew(buf + 14);
1240 xlabel[strlen(xlabel) - 1] = '\0';
1241 ignores = fgets(buf, Bufsize, fp); /* Y axis label: ... */
1242 ylabel = stringNew(buf + 14);
1243 ylabel[strlen(ylabel) - 1] = '\0';
1244
1245 gplot = gplotCreate(rootname, outformat, title, xlabel, ylabel);
1246 LEPT_FREE(rootname);
1247 LEPT_FREE(title);
1248 LEPT_FREE(xlabel);
1249 LEPT_FREE(ylabel);
1250 if (!gplot) {
1251 fclose(fp);
1252 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
1253 }
1254 sarrayDestroy(&gplot->cmddata);
1255 sarrayDestroy(&gplot->datanames);
1256 sarrayDestroy(&gplot->plotdata);
1257 sarrayDestroy(&gplot->plotlabels);
1258 numaDestroy(&gplot->plotstyles);
1259
1260 ignore = fscanf(fp, "Commandfile name: %s\n", buf); /* Bufsize - 1 */
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:");
1269 gplot->plotlabels = sarrayReadStream(fp);
1270 ignore = fscanf(fp, "\nPlot styles:");
1271 gplot->plotstyles = numaReadStream(fp);
1272
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);
1277
1278 fclose(fp);
1279 return gplot;
1280}
1281
1282
1290l_ok
1291gplotWrite(const char *filename,
1292 GPLOT *gplot)
1293{
1294FILE *fp;
1295
1296 if (!filename)
1297 return ERROR_INT("filename not defined", __func__, 1);
1298 if (!gplot)
1299 return ERROR_INT("gplot not defined", __func__, 1);
1300
1301 if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1302 return ERROR_INT_1("stream not opened", filename, __func__, 1);
1303
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);
1310
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:");
1319 sarrayWriteStream(fp, gplot->plotlabels);
1320 fprintf(fp, "\nPlot styles:");
1321 numaWriteStderr(gplot->plotstyles);
1322
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);
1326
1327 fclose(fp);
1328 return 0;
1329}
GPLOT * gplotSimpleXYN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXYN()
Definition gplot.c:1005
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
Definition gplot.c:321
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
Definition gplot.c:461
PIX * gplotSimplePixN(NUMAA *naa, const char *title)
gplotSimplePixN()
Definition gplot.c:843
GPLOT * gplotRead(const char *filename)
gplotRead()
Definition gplot.c:1205
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
Definition gplot.c:192
PIX * gplotGeneralPixN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPixN()
Definition gplot.c:1157
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
Definition gplot.c:258
l_ok gplotSimple2(NUMA *na1, NUMA *na2, l_int32 outformat, const char *outroot, const char *title)
gplotSimple2()
Definition gplot.c:691
l_ok gplotWrite(const char *filename, GPLOT *gplot)
gplotWrite()
Definition gplot.c:1291
PIX * gplotGeneralPix2(NUMA *na1, NUMA *na2, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix2()
Definition gplot.c:1107
l_ok gplotGenCommandFile(GPLOT *gplot)
gplotGenCommandFile()
Definition gplot.c:502
l_ok gplotSetScaling(GPLOT *gplot, l_int32 scaling)
gplotSetScaling()
Definition gplot.c:399
PIX * gplotGeneralPix1(NUMA *na, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix1()
Definition gplot.c:1060
const char * gplotstylenames[]
Definition gplot.c:157
l_ok gplotSimpleN(NUMAA *naa, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleN()
Definition gplot.c:729
GPLOT * gplotSimpleXY1(NUMA *nax, NUMA *nay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY1()
Definition gplot.c:893
PIX * gplotMakeOutputPix(GPLOT *gplot)
gplotMakeOutputPix()
Definition gplot.c:428
GPLOT * gplotSimpleXY2(NUMA *nax, NUMA *nay1, NUMA *nay2, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY2()
Definition gplot.c:948
l_ok gplotGenDataFiles(GPLOT *gplot)
gplotGenDataFiles()
Definition gplot.c:608
PIX * gplotSimplePix2(NUMA *na1, NUMA *na2, const char *title)
gplotSimplePix2()
Definition gplot.c:801
PIX * gplotSimplePix1(NUMA *na, const char *title)
gplotSimplePix1()
Definition gplot.c:760
const char * gplotfileoutputs[]
Definition gplot.c:162
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()
Definition gplot.c:655
@ GPLOT_LINEAR_SCALE
Definition gplot.h:66
@ L_COPY
Definition pix.h:505
@ L_CLONE
Definition pix.h:506
@ L_NOCOPY
Definition pix.h:503
@ L_INSERT
Definition pix.h:504
Definition gplot.h:77
struct Sarray * datanames
Definition gplot.h:81
char * title
Definition gplot.h:89
l_int32 nplots
Definition gplot.h:85
char * rootname
Definition gplot.h:78
struct Sarray * plotdata
Definition gplot.h:82
char * cmdname
Definition gplot.h:79
char * outname
Definition gplot.h:86
struct Numa * plotstyles
Definition gplot.h:84
struct Sarray * plotlabels
Definition gplot.h:83
l_int32 scaling
Definition gplot.h:88
char * ylabel
Definition gplot.h:91
struct Sarray * cmddata
Definition gplot.h:80
l_int32 outformat
Definition gplot.h:87
char * xlabel
Definition gplot.h:90