Leptonica 1.85.0
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 L_ERROR("stream not opened for command: %s\n", __func__, cmdstr);
585 LEPT_FREE(cmdstr);
586 return 1;
587 }
588 fwrite(cmdstr, 1, strlen(cmdstr), fp);
589 fclose(fp);
590 LEPT_FREE(cmdstr);
591 return 0;
592}
593
594
608l_ok
610{
611char *plotdata, *dataname;
612l_int32 i, nplots;
613FILE *fp;
614
615 if (!gplot)
616 return ERROR_INT("gplot not defined", __func__, 1);
617
618 nplots = sarrayGetCount(gplot->datanames);
619 for (i = 0; i < nplots; i++) {
620 plotdata = sarrayGetString(gplot->plotdata, i, L_NOCOPY);
621 dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
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);
626 fclose(fp);
627 }
628
629 return 0;
630}
631
632
633/*-----------------------------------------------------------------*
634 * Quick one-line plots *
635 *-----------------------------------------------------------------*/
655l_ok
657 l_int32 outformat,
658 const char *outroot,
659 const char *title)
660{
661GPLOT *gplot;
662
663 gplot = gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
664 if (!gplot)
665 return ERROR_INT("failed to generate plot", __func__, 1);
666 gplotDestroy(&gplot);
667 return 0;
668}
669
670
691l_ok
693 NUMA *na2,
694 l_int32 outformat,
695 const char *outroot,
696 const char *title)
697{
698GPLOT *gplot;
699
700 gplot = gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES,
701 outformat, outroot, title);
702 if (!gplot)
703 return ERROR_INT("failed to generate plot", __func__, 1);
704 gplotDestroy(&gplot);
705 return 0;
706}
707
708
729l_ok
731 l_int32 outformat,
732 const char *outroot,
733 const char *title)
734{
735GPLOT *gplot;
736
737 gplot = gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
738 if (!gplot)
739 return ERROR_INT("failed to generate plot", __func__, 1);
740 gplotDestroy(&gplot);
741 return 0;
742}
743
744
760PIX *
762 const char *title)
763{
764char buf[64];
765static l_atomic index;
766GPLOT *gplot;
767PIX *pix;
768
769 if (!na)
770 return (PIX *)ERROR_PTR("na not defined", __func__, NULL);
771
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);
775 if (!gplot)
776 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
777 pix = pixRead(gplot->outname);
778 gplotDestroy(&gplot);
779 if (!pix)
780 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
781 return pix;
782}
783
784
801PIX *
803 NUMA *na2,
804 const char *title)
805{
806char buf[64];
807static l_atomic index;
808GPLOT *gplot;
809PIX *pix;
810
811 if (!na1 || !na2)
812 return (PIX *)ERROR_PTR("both na1, na2 not defined", __func__, NULL);
813
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);
817 if (!gplot)
818 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
819 pix = pixRead(gplot->outname);
820 gplotDestroy(&gplot);
821 if (!pix)
822 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
823 return pix;
824}
825
826
843PIX *
845 const char *title)
846{
847char buf[64];
848static l_atomic index;
849GPLOT *gplot;
850PIX *pix;
851
852 if (!naa)
853 return (PIX *)ERROR_PTR("naa not defined", __func__, NULL);
854
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);
858 if (!gplot)
859 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
860 pix = pixRead(gplot->outname);
861 gplotDestroy(&gplot);
862 if (!pix)
863 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
864 return pix;
865}
866
867
893GPLOT *
895 NUMA *nay,
896 l_int32 plotstyle,
897 l_int32 outformat,
898 const char *outroot,
899 const char *title)
900{
901GPLOT *gplot;
902
903 if (!nay)
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);
911 if (!outroot)
912 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
913
914 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
915 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
916 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
917 gplotMakeOutput(gplot);
918 return gplot;
919}
920
921
948GPLOT *
950 NUMA *nay1,
951 NUMA *nay2,
952 l_int32 plotstyle,
953 l_int32 outformat,
954 const char *outroot,
955 const char *title)
956{
957GPLOT *gplot;
958
959 if (!nay1 || !nay2)
960 return (GPLOT *)ERROR_PTR("nay1 and nay2 not both defined",
961 __func__, NULL);
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);
968 if (!outroot)
969 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
970
971 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
972 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
973 gplotAddPlot(gplot, nax, nay1, plotstyle, NULL);
974 gplotAddPlot(gplot, nax, nay2, plotstyle, NULL);
975 gplotMakeOutput(gplot);
976 return gplot;
977}
978
979
1005GPLOT *
1007 NUMAA *naay,
1008 l_int32 plotstyle,
1009 l_int32 outformat,
1010 const char *outroot,
1011 const char *title)
1012{
1013l_int32 i, n;
1014GPLOT *gplot;
1015NUMA *nay;
1016
1017 if (!naay)
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);
1027 if (!outroot)
1028 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
1029
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);
1034 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1035 numaDestroy(&nay);
1036 }
1037 gplotMakeOutput(gplot);
1038 return gplot;
1039}
1040
1041
1060PIX *
1062 l_int32 plotstyle,
1063 const char *rootname,
1064 const char *title,
1065 const char *xlabel,
1066 const char *ylabel)
1067{
1068GPLOT *gplot;
1069PIX *pix;
1070
1071 if (!na)
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);
1075 if (!rootname)
1076 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1077
1078 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1079 if (!gplot)
1080 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1081 gplotAddPlot(gplot, NULL, na, plotstyle, NULL);
1082 pix = gplotMakeOutputPix(gplot);
1083 gplotDestroy(&gplot);
1084 return pix;
1085}
1086
1087
1107PIX *
1109 NUMA *na2,
1110 l_int32 plotstyle,
1111 const char *rootname,
1112 const char *title,
1113 const char *xlabel,
1114 const char *ylabel)
1115{
1116GPLOT *gplot;
1117PIX *pix;
1118
1119 if (!na1)
1120 return (PIX *)ERROR_PTR("na1 not defined", __func__, NULL);
1121 if (!na2)
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);
1125 if (!rootname)
1126 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1127
1128 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1129 if (!gplot)
1130 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1131 gplotAddPlot(gplot, na1, na2, plotstyle, NULL);
1132 pix = gplotMakeOutputPix(gplot);
1133 gplotDestroy(&gplot);
1134 return pix;
1135}
1136
1137
1157PIX *
1159 NUMAA *naay,
1160 l_int32 plotstyle,
1161 const char *rootname,
1162 const char *title,
1163 const char *xlabel,
1164 const char *ylabel)
1165{
1166l_int32 i, n;
1167GPLOT *gplot;
1168NUMA *nay;
1169PIX *pix;
1170
1171 if (!nax)
1172 return (PIX *)ERROR_PTR("nax not defined", __func__, NULL);
1173 if (!naay)
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);
1179 if (!rootname)
1180 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1181
1182 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1183 if (!gplot)
1184 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1185 for (i = 0; i < n; i++) {
1186 nay = numaaGetNuma(naay, i, L_CLONE);
1187 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1188 numaDestroy(&nay);
1189 }
1190 pix = gplotMakeOutputPix(gplot);
1191 gplotDestroy(&gplot);
1192 return pix;
1193}
1194
1195
1196/*-----------------------------------------------------------------*
1197 * Serialize for I/O *
1198 *-----------------------------------------------------------------*/
1205GPLOT *
1206gplotRead(const char *filename)
1207{
1208char buf[Bufsize];
1209char *rootname, *title, *xlabel, *ylabel, *ignores;
1210l_int32 outformat, ret, version, ignore;
1211FILE *fp;
1212GPLOT *gplot;
1213
1214 if (!filename)
1215 return (GPLOT *)ERROR_PTR("filename not defined", __func__, NULL);
1216
1217 if ((fp = fopenReadStream(filename)) == NULL)
1218 return (GPLOT *)ERROR_PTR_1("stream not opened",
1219 filename, __func__, NULL);
1220
1221 ret = fscanf(fp, "Gplot Version %d\n", &version);
1222 if (ret != 1) {
1223 fclose(fp);
1224 return (GPLOT *)ERROR_PTR_1("not a gplot file",
1225 filename, __func__, NULL);
1226 }
1227 if (version != GPLOT_VERSION_NUMBER) {
1228 fclose(fp);
1229 return (GPLOT *)ERROR_PTR_1("invalid gplot version",
1230 filename, __func__, NULL);
1231 }
1232
1233 ignore = fscanf(fp, "Rootname: %511s\n", buf); /* Bufsize - 1 */
1234 rootname = stringNew(buf);
1235 ignore = fscanf(fp, "Output format: %d\n", &outformat);
1236 ignores = fgets(buf, Bufsize, fp); /* Title: ... */
1237 title = stringNew(buf + 7);
1238 title[strlen(title) - 1] = '\0';
1239 ignores = fgets(buf, Bufsize, fp); /* X axis label: ... */
1240 xlabel = stringNew(buf + 14);
1241 xlabel[strlen(xlabel) - 1] = '\0';
1242 ignores = fgets(buf, Bufsize, fp); /* Y axis label: ... */
1243 ylabel = stringNew(buf + 14);
1244 ylabel[strlen(ylabel) - 1] = '\0';
1245
1246 gplot = gplotCreate(rootname, outformat, title, xlabel, ylabel);
1247 LEPT_FREE(rootname);
1248 LEPT_FREE(title);
1249 LEPT_FREE(xlabel);
1250 LEPT_FREE(ylabel);
1251 if (!gplot) {
1252 fclose(fp);
1253 return (GPLOT *)ERROR_PTR_1("gplot not made", filename, __func__, NULL);
1254 }
1255 sarrayDestroy(&gplot->cmddata);
1256 sarrayDestroy(&gplot->datanames);
1257 sarrayDestroy(&gplot->plotdata);
1258 sarrayDestroy(&gplot->plotlabels);
1259 numaDestroy(&gplot->plotstyles);
1260
1261 ignore = fscanf(fp, "Commandfile name: %s\n", buf); /* Bufsize - 1 */
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:");
1270 gplot->plotlabels = sarrayReadStream(fp);
1271 ignore = fscanf(fp, "\nPlot styles:");
1272 gplot->plotstyles = numaReadStream(fp);
1273
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);
1278
1279 fclose(fp);
1280 return gplot;
1281}
1282
1283
1291l_ok
1292gplotWrite(const char *filename,
1293 GPLOT *gplot)
1294{
1295FILE *fp;
1296
1297 if (!filename)
1298 return ERROR_INT("filename not defined", __func__, 1);
1299 if (!gplot)
1300 return ERROR_INT("gplot not defined", __func__, 1);
1301
1302 if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1303 return ERROR_INT_1("stream not opened", filename, __func__, 1);
1304
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);
1311
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:");
1320 sarrayWriteStream(fp, gplot->plotlabels);
1321 fprintf(fp, "\nPlot styles:");
1322 numaWriteStderr(gplot->plotstyles);
1323
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);
1327
1328 fclose(fp);
1329 return 0;
1330}
GPLOT * gplotSimpleXYN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXYN()
Definition gplot.c:1006
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:844
GPLOT * gplotRead(const char *filename)
gplotRead()
Definition gplot.c:1206
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:1158
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:692
l_ok gplotWrite(const char *filename, GPLOT *gplot)
gplotWrite()
Definition gplot.c:1292
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:1108
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:1061
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:730
GPLOT * gplotSimpleXY1(NUMA *nax, NUMA *nay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY1()
Definition gplot.c:894
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:949
l_ok gplotGenDataFiles(GPLOT *gplot)
gplotGenDataFiles()
Definition gplot.c:609
PIX * gplotSimplePix2(NUMA *na1, NUMA *na2, const char *title)
gplotSimplePix2()
Definition gplot.c:802
PIX * gplotSimplePix1(NUMA *na, const char *title)
gplotSimplePix1()
Definition gplot.c:761
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:656
@ 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