Leptonica 1.83.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
142
143#ifdef HAVE_CONFIG_H
144#include <config_auto.h>
145#endif /* HAVE_CONFIG_H */
146
147#include <string.h>
148#include "allheaders.h"
149
150#define Bufsize 512 /* hardcoded below in fscanf */
151
152const char *gplotstylenames[] = {"with lines",
153 "with points",
154 "with impulses",
155 "with linespoints",
156 "with dots"};
157const char *gplotfileoutputs[] = {"",
158 "PNG",
159 "PS",
160 "EPS",
161 "LATEX",
162 "PNM"};
163
164
165/*-----------------------------------------------------------------*
166 * Basic Plotting Functions *
167 *-----------------------------------------------------------------*/
186GPLOT *
187gplotCreate(const char *rootname,
188 l_int32 outformat,
189 const char *title,
190 const char *xlabel,
191 const char *ylabel)
192{
193char *newroot;
194char buf[Bufsize];
195l_int32 badchar;
196GPLOT *gplot;
197
198 if (!rootname)
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);
205 if (badchar) /* danger of command injection */
206 return (GPLOT *)ERROR_PTR("invalid rootname", __func__, NULL);
207
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;
212 }
213#endif
214
215 gplot = (GPLOT *)LEPT_CALLOC(1, sizeof(GPLOT));
216 gplot->cmddata = sarrayCreate(0);
217 gplot->datanames = sarrayCreate(0);
218 gplot->plotdata = sarrayCreate(0);
219 gplot->plotlabels = sarrayCreate(0);
220 gplot->plotstyles = numaCreate(0);
221
222 /* Save title, labels, rootname, outformat, cmdname, outname */
223 newroot = genPathname(rootname, NULL);
224 gplot->rootname = newroot;
225 gplot->outformat = outformat;
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);
242
243 return gplot;
244}
245
246
252void
253gplotDestroy(GPLOT **pgplot)
254{
255GPLOT *gplot;
256
257 if (pgplot == NULL) {
258 L_WARNING("ptr address is null!\n", __func__);
259 return;
260 }
261
262 if ((gplot = *pgplot) == NULL)
263 return;
264
265 LEPT_FREE(gplot->rootname);
266 LEPT_FREE(gplot->cmdname);
267 sarrayDestroy(&gplot->cmddata);
268 sarrayDestroy(&gplot->datanames);
269 sarrayDestroy(&gplot->plotdata);
270 sarrayDestroy(&gplot->plotlabels);
271 numaDestroy(&gplot->plotstyles);
272 LEPT_FREE(gplot->outname);
273 if (gplot->title)
274 LEPT_FREE(gplot->title);
275 if (gplot->xlabel)
276 LEPT_FREE(gplot->xlabel);
277 if (gplot->ylabel)
278 LEPT_FREE(gplot->ylabel);
279
280 LEPT_FREE(gplot);
281 *pgplot = NULL;
282}
283
284
315l_ok
316gplotAddPlot(GPLOT *gplot,
317 NUMA *nax,
318 NUMA *nay,
319 l_int32 plotstyle,
320 const char *plotlabel)
321{
322char buf[Bufsize];
323char emptystring[] = "";
324char *datastr, *title;
325l_int32 n, i;
326l_float32 valx, valy, startx, delx;
327SARRAY *sa;
328
329 if (!gplot)
330 return ERROR_INT("gplot not defined", __func__, 1);
331 if (!nay)
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);
335
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;
343 }
344
345 /* Save plotstyle and plotlabel */
346 numaGetParameters(nay, &startx, &delx);
347 numaAddNumber(gplot->plotstyles, plotstyle);
348 if (plotlabel) {
349 title = stringNew(plotlabel);
350 sarrayAddString(gplot->plotlabels, title, L_INSERT);
351 } else {
352 sarrayAddString(gplot->plotlabels, emptystring, L_COPY);
353 }
354
355 /* Generate and save data filename */
356 gplot->nplots++;
357 snprintf(buf, Bufsize, "%s.data.%d", gplot->rootname, gplot->nplots);
358 sarrayAddString(gplot->datanames, buf, L_COPY);
359
360 /* Generate data and save as a string */
361 sa = sarrayCreate(n);
362 for (i = 0; i < n; i++) {
363 if (nax)
364 numaGetFValue(nax, i, &valx);
365 else
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);
370 }
371 datastr = sarrayToString(sa, 0);
372 sarrayAddString(gplot->plotdata, datastr, L_INSERT);
373 sarrayDestroy(&sa);
374
375 return 0;
376}
377
378
393l_ok
394gplotSetScaling(GPLOT *gplot,
395 l_int32 scaling)
396{
397 if (!gplot)
398 return ERROR_INT("gplot not defined", __func__, 1);
399 if (scaling != GPLOT_LINEAR_SCALE &&
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);
404 gplot->scaling = scaling;
405 return 0;
406}
407
408
422PIX *
424{
425 if (!gplot)
426 return (PIX *)ERROR_PTR("gplot not defined", __func__, NULL);
427 if (gplot->outformat != GPLOT_PNG && gplot->outformat != GPLOT_PNM)
428 return (PIX *)ERROR_PTR("output format not an image", __func__, NULL);
429
430 if (gplotMakeOutput(gplot))
431 return (PIX *)ERROR_PTR("plot output not made", __func__, NULL);
432 return pixRead(gplot->outname);
433}
434
435
455l_ok
456gplotMakeOutput(GPLOT *gplot)
457{
458char buf[Bufsize];
459char *cmdname;
460
461 if (!gplot)
462 return ERROR_INT("gplot not defined", __func__, 1);
463
464 if (!LeptDebugOK) {
465 L_INFO("running gnuplot is disabled; "
466 "use setLeptDebugOK(1) to enable\n", __func__);
467 return 0;
468 }
469
470#ifdef OS_IOS /* iOS 11 does not support system() */
471 return ERROR_INT("iOS 11 does not support system()", __func__, 0);
472#endif /* OS_IOS */
473
474 gplotGenCommandFile(gplot);
475 gplotGenDataFiles(gplot);
476 cmdname = genPathname(gplot->cmdname, NULL);
477
478#ifndef _WIN32
479 snprintf(buf, Bufsize, "gnuplot %s", cmdname);
480#else
481 snprintf(buf, Bufsize, "wgnuplot %s", cmdname);
482#endif /* _WIN32 */
483
484 callSystemDebug(buf); /* gnuplot || wgnuplot */
485 LEPT_FREE(cmdname);
486 return 0;
487}
488
489
496l_ok
498{
499char buf[Bufsize];
500char *cmdstr, *plotlabel, *dataname;
501l_int32 i, plotstyle, nplots;
502FILE *fp;
503
504 if (!gplot)
505 return ERROR_INT("gplot not defined", __func__, 1);
506
507 /* Remove any previous command data */
508 sarrayClear(gplot->cmddata);
509
510 /* Generate command data instructions */
511 if (gplot->title) { /* set title */
512 snprintf(buf, Bufsize, "set title '%s'", gplot->title);
513 sarrayAddString(gplot->cmddata, buf, L_COPY);
514 }
515 if (gplot->xlabel) { /* set xlabel */
516 snprintf(buf, Bufsize, "set xlabel '%s'", gplot->xlabel);
517 sarrayAddString(gplot->cmddata, buf, L_COPY);
518 }
519 if (gplot->ylabel) { /* set ylabel */
520 snprintf(buf, Bufsize, "set ylabel '%s'", gplot->ylabel);
521 sarrayAddString(gplot->cmddata, buf, L_COPY);
522 }
523
524 /* Set terminal type and output */
525 if (gplot->outformat == GPLOT_PNG) {
526 snprintf(buf, Bufsize, "set terminal png; set output '%s'",
527 gplot->outname);
528 } else if (gplot->outformat == GPLOT_PS) {
529 snprintf(buf, Bufsize, "set terminal postscript; set output '%s'",
530 gplot->outname);
531 } else if (gplot->outformat == GPLOT_EPS) {
532 snprintf(buf, Bufsize, "set terminal postscript eps; set output '%s'",
533 gplot->outname);
534 } else if (gplot->outformat == GPLOT_LATEX) {
535 snprintf(buf, Bufsize, "set terminal latex; set output '%s'",
536 gplot->outname);
537 } else if (gplot->outformat == GPLOT_PNM) {
538 snprintf(buf, Bufsize, "set terminal pbm color; set output '%s'",
539 gplot->outname);
540 }
541 sarrayAddString(gplot->cmddata, buf, L_COPY);
542
543 if (gplot->scaling == GPLOT_LOG_SCALE_X ||
544 gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
545 snprintf(buf, Bufsize, "set logscale x");
546 sarrayAddString(gplot->cmddata, buf, L_COPY);
547 }
548 if (gplot->scaling == GPLOT_LOG_SCALE_Y ||
549 gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
550 snprintf(buf, Bufsize, "set logscale y");
551 sarrayAddString(gplot->cmddata, buf, L_COPY);
552 }
553
554 nplots = sarrayGetCount(gplot->datanames);
555 for (i = 0; i < nplots; i++) {
556 plotlabel = sarrayGetString(gplot->plotlabels, i, L_NOCOPY);
557 dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
558 numaGetIValue(gplot->plotstyles, i, &plotstyle);
559 if (nplots == 1) {
560 snprintf(buf, Bufsize, "plot '%s' title '%s' %s",
561 dataname, plotlabel, gplotstylenames[plotstyle]);
562 } else {
563 if (i == 0)
564 snprintf(buf, Bufsize, "plot '%s' title '%s' %s, \\",
565 dataname, plotlabel, gplotstylenames[plotstyle]);
566 else if (i < nplots - 1)
567 snprintf(buf, Bufsize, " '%s' title '%s' %s, \\",
568 dataname, plotlabel, gplotstylenames[plotstyle]);
569 else
570 snprintf(buf, Bufsize, " '%s' title '%s' %s",
571 dataname, plotlabel, gplotstylenames[plotstyle]);
572 }
573 sarrayAddString(gplot->cmddata, buf, L_COPY);
574 }
575
576 /* Write command data to file */
577 cmdstr = sarrayToString(gplot->cmddata, 1);
578 if ((fp = fopenWriteStream(gplot->cmdname, "w")) == NULL) {
579 LEPT_FREE(cmdstr);
580 return ERROR_INT("cmd stream not opened", __func__, 1);
581 }
582 fwrite(cmdstr, 1, strlen(cmdstr), fp);
583 fclose(fp);
584 LEPT_FREE(cmdstr);
585 return 0;
586}
587
588
602l_ok
603gplotGenDataFiles(GPLOT *gplot)
604{
605char *plotdata, *dataname;
606l_int32 i, nplots;
607FILE *fp;
608
609 if (!gplot)
610 return ERROR_INT("gplot not defined", __func__, 1);
611
612 nplots = sarrayGetCount(gplot->datanames);
613 for (i = 0; i < nplots; i++) {
614 plotdata = sarrayGetString(gplot->plotdata, i, L_NOCOPY);
615 dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
616 if ((fp = fopen(dataname, "w")) == NULL)
617 return ERROR_INT("datafile stream not opened", __func__, 1);
618 fwrite(plotdata, 1, strlen(plotdata), fp);
619 fclose(fp);
620 }
621
622 return 0;
623}
624
625
626/*-----------------------------------------------------------------*
627 * Quick one-line plots *
628 *-----------------------------------------------------------------*/
648l_ok
650 l_int32 outformat,
651 const char *outroot,
652 const char *title)
653{
654GPLOT *gplot;
655
656 gplot = gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
657 if (!gplot)
658 return ERROR_INT("failed to generate plot", __func__, 1);
659 gplotDestroy(&gplot);
660 return 0;
661}
662
663
684l_ok
686 NUMA *na2,
687 l_int32 outformat,
688 const char *outroot,
689 const char *title)
690{
691GPLOT *gplot;
692
693 gplot = gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES,
694 outformat, outroot, title);
695 if (!gplot)
696 return ERROR_INT("failed to generate plot", __func__, 1);
697 gplotDestroy(&gplot);
698 return 0;
699}
700
701
722l_ok
724 l_int32 outformat,
725 const char *outroot,
726 const char *title)
727{
728GPLOT *gplot;
729
730 gplot = gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
731 if (!gplot)
732 return ERROR_INT("failed to generate plot", __func__, 1);
733 gplotDestroy(&gplot);
734 return 0;
735}
736
737
753PIX *
755 const char *title)
756{
757char buf[64];
758static l_int32 index;
759GPLOT *gplot;
760PIX *pix;
761
762 if (!na)
763 return (PIX *)ERROR_PTR("na not defined", __func__, NULL);
764
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);
768 if (!gplot)
769 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
770 pix = pixRead(gplot->outname);
771 gplotDestroy(&gplot);
772 if (!pix)
773 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
774 return pix;
775}
776
777
794PIX *
796 NUMA *na2,
797 const char *title)
798{
799char buf[64];
800static l_int32 index;
801GPLOT *gplot;
802PIX *pix;
803
804 if (!na1 || !na2)
805 return (PIX *)ERROR_PTR("both na1, na2 not defined", __func__, NULL);
806
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);
810 if (!gplot)
811 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
812 pix = pixRead(gplot->outname);
813 gplotDestroy(&gplot);
814 if (!pix)
815 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
816 return pix;
817}
818
819
836PIX *
838 const char *title)
839{
840char buf[64];
841static l_int32 index;
842GPLOT *gplot;
843PIX *pix;
844
845 if (!naa)
846 return (PIX *)ERROR_PTR("naa not defined", __func__, NULL);
847
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);
851 if (!gplot)
852 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
853 pix = pixRead(gplot->outname);
854 gplotDestroy(&gplot);
855 if (!pix)
856 return (PIX *)ERROR_PTR("failed to generate plot", __func__, NULL);
857 return pix;
858}
859
860
886GPLOT *
888 NUMA *nay,
889 l_int32 plotstyle,
890 l_int32 outformat,
891 const char *outroot,
892 const char *title)
893{
894GPLOT *gplot;
895
896 if (!nay)
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);
904 if (!outroot)
905 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
906
907 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
908 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
909 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
910 gplotMakeOutput(gplot);
911 return gplot;
912}
913
914
941GPLOT *
943 NUMA *nay1,
944 NUMA *nay2,
945 l_int32 plotstyle,
946 l_int32 outformat,
947 const char *outroot,
948 const char *title)
949{
950GPLOT *gplot;
951
952 if (!nay1 || !nay2)
953 return (GPLOT *)ERROR_PTR("nay1 and nay2 not both defined",
954 __func__, NULL);
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);
961 if (!outroot)
962 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
963
964 if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
965 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
966 gplotAddPlot(gplot, nax, nay1, plotstyle, NULL);
967 gplotAddPlot(gplot, nax, nay2, plotstyle, NULL);
968 gplotMakeOutput(gplot);
969 return gplot;
970}
971
972
998GPLOT *
1000 NUMAA *naay,
1001 l_int32 plotstyle,
1002 l_int32 outformat,
1003 const char *outroot,
1004 const char *title)
1005{
1006l_int32 i, n;
1007GPLOT *gplot;
1008NUMA *nay;
1009
1010 if (!naay)
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);
1020 if (!outroot)
1021 return (GPLOT *)ERROR_PTR("outroot not specified", __func__, NULL);
1022
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);
1027 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1028 numaDestroy(&nay);
1029 }
1030 gplotMakeOutput(gplot);
1031 return gplot;
1032}
1033
1034
1053PIX *
1055 l_int32 plotstyle,
1056 const char *rootname,
1057 const char *title,
1058 const char *xlabel,
1059 const char *ylabel)
1060{
1061GPLOT *gplot;
1062PIX *pix;
1063
1064 if (!na)
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);
1068 if (!rootname)
1069 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1070
1071 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1072 if (!gplot)
1073 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1074 gplotAddPlot(gplot, NULL, na, plotstyle, NULL);
1075 pix = gplotMakeOutputPix(gplot);
1076 gplotDestroy(&gplot);
1077 return pix;
1078}
1079
1080
1100PIX *
1102 NUMA *na2,
1103 l_int32 plotstyle,
1104 const char *rootname,
1105 const char *title,
1106 const char *xlabel,
1107 const char *ylabel)
1108{
1109GPLOT *gplot;
1110PIX *pix;
1111
1112 if (!na1)
1113 return (PIX *)ERROR_PTR("na1 not defined", __func__, NULL);
1114 if (!na2)
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);
1118 if (!rootname)
1119 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1120
1121 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1122 if (!gplot)
1123 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1124 gplotAddPlot(gplot, na1, na2, plotstyle, NULL);
1125 pix = gplotMakeOutputPix(gplot);
1126 gplotDestroy(&gplot);
1127 return pix;
1128}
1129
1130
1150PIX *
1152 NUMAA *naay,
1153 l_int32 plotstyle,
1154 const char *rootname,
1155 const char *title,
1156 const char *xlabel,
1157 const char *ylabel)
1158{
1159l_int32 i, n;
1160GPLOT *gplot;
1161NUMA *nay;
1162PIX *pix;
1163
1164 if (!nax)
1165 return (PIX *)ERROR_PTR("nax not defined", __func__, NULL);
1166 if (!naay)
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);
1172 if (!rootname)
1173 return (PIX *)ERROR_PTR("rootname not defined", __func__, NULL);
1174
1175 gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1176 if (!gplot)
1177 return (PIX *)ERROR_PTR("gplot not made", __func__, NULL);
1178 for (i = 0; i < n; i++) {
1179 nay = numaaGetNuma(naay, i, L_CLONE);
1180 gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1181 numaDestroy(&nay);
1182 }
1183 pix = gplotMakeOutputPix(gplot);
1184 gplotDestroy(&gplot);
1185 return pix;
1186}
1187
1188
1189/*-----------------------------------------------------------------*
1190 * Serialize for I/O *
1191 *-----------------------------------------------------------------*/
1198GPLOT *
1199gplotRead(const char *filename)
1200{
1201char buf[Bufsize];
1202char *rootname, *title, *xlabel, *ylabel, *ignores;
1203l_int32 outformat, ret, version, ignore;
1204FILE *fp;
1205GPLOT *gplot;
1206
1207 if (!filename)
1208 return (GPLOT *)ERROR_PTR("filename not defined", __func__, NULL);
1209
1210 if ((fp = fopenReadStream(filename)) == NULL)
1211 return (GPLOT *)ERROR_PTR("stream not opened", __func__, NULL);
1212
1213 ret = fscanf(fp, "Gplot Version %d\n", &version);
1214 if (ret != 1) {
1215 fclose(fp);
1216 return (GPLOT *)ERROR_PTR("not a gplot file", __func__, NULL);
1217 }
1218 if (version != GPLOT_VERSION_NUMBER) {
1219 fclose(fp);
1220 return (GPLOT *)ERROR_PTR("invalid gplot version", __func__, NULL);
1221 }
1222
1223 ignore = fscanf(fp, "Rootname: %511s\n", buf); /* Bufsize - 1 */
1224 rootname = stringNew(buf);
1225 ignore = fscanf(fp, "Output format: %d\n", &outformat);
1226 ignores = fgets(buf, Bufsize, fp); /* Title: ... */
1227 title = stringNew(buf + 7);
1228 title[strlen(title) - 1] = '\0';
1229 ignores = fgets(buf, Bufsize, fp); /* X axis label: ... */
1230 xlabel = stringNew(buf + 14);
1231 xlabel[strlen(xlabel) - 1] = '\0';
1232 ignores = fgets(buf, Bufsize, fp); /* Y axis label: ... */
1233 ylabel = stringNew(buf + 14);
1234 ylabel[strlen(ylabel) - 1] = '\0';
1235
1236 gplot = gplotCreate(rootname, outformat, title, xlabel, ylabel);
1237 LEPT_FREE(rootname);
1238 LEPT_FREE(title);
1239 LEPT_FREE(xlabel);
1240 LEPT_FREE(ylabel);
1241 if (!gplot) {
1242 fclose(fp);
1243 return (GPLOT *)ERROR_PTR("gplot not made", __func__, NULL);
1244 }
1245 sarrayDestroy(&gplot->cmddata);
1246 sarrayDestroy(&gplot->datanames);
1247 sarrayDestroy(&gplot->plotdata);
1248 sarrayDestroy(&gplot->plotlabels);
1249 numaDestroy(&gplot->plotstyles);
1250
1251 ignore = fscanf(fp, "Commandfile name: %511s\n", buf); /* Bufsize - 1 */
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:");
1260 gplot->plotlabels = sarrayReadStream(fp);
1261 ignore = fscanf(fp, "\nPlot styles:");
1262 gplot->plotstyles = numaReadStream(fp);
1263
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);
1268
1269 fclose(fp);
1270 return gplot;
1271}
1272
1273
1281l_ok
1282gplotWrite(const char *filename,
1283 GPLOT *gplot)
1284{
1285FILE *fp;
1286
1287 if (!filename)
1288 return ERROR_INT("filename not defined", __func__, 1);
1289 if (!gplot)
1290 return ERROR_INT("gplot not defined", __func__, 1);
1291
1292 if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1293 return ERROR_INT("stream not opened", __func__, 1);
1294
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);
1301
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:");
1310 sarrayWriteStream(fp, gplot->plotlabels);
1311 fprintf(fp, "\nPlot styles:");
1312 numaWriteStderr(gplot->plotstyles);
1313
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);
1317
1318 fclose(fp);
1319 return 0;
1320}
struct Numaa NUMAA
Definition array.h:69
struct Numa NUMA
Definition array.h:66
struct Sarray SARRAY
Definition array.h:81
GPLOT * gplotSimpleXYN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXYN()
Definition gplot.c:999
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
Definition gplot.c:316
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
Definition gplot.c:456
PIX * gplotSimplePixN(NUMAA *naa, const char *title)
gplotSimplePixN()
Definition gplot.c:837
GPLOT * gplotRead(const char *filename)
gplotRead()
Definition gplot.c:1199
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
Definition gplot.c:187
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:1151
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
Definition gplot.c:253
l_ok gplotSimple2(NUMA *na1, NUMA *na2, l_int32 outformat, const char *outroot, const char *title)
gplotSimple2()
Definition gplot.c:685
l_ok gplotWrite(const char *filename, GPLOT *gplot)
gplotWrite()
Definition gplot.c:1282
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:1101
l_ok gplotGenCommandFile(GPLOT *gplot)
gplotGenCommandFile()
Definition gplot.c:497
l_ok gplotSetScaling(GPLOT *gplot, l_int32 scaling)
gplotSetScaling()
Definition gplot.c:394
PIX * gplotGeneralPix1(NUMA *na, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix1()
Definition gplot.c:1054
const char * gplotstylenames[]
Definition gplot.c:152
l_ok gplotSimpleN(NUMAA *naa, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleN()
Definition gplot.c:723
GPLOT * gplotSimpleXY1(NUMA *nax, NUMA *nay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY1()
Definition gplot.c:887
PIX * gplotMakeOutputPix(GPLOT *gplot)
gplotMakeOutputPix()
Definition gplot.c:423
GPLOT * gplotSimpleXY2(NUMA *nax, NUMA *nay1, NUMA *nay2, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY2()
Definition gplot.c:942
l_ok gplotGenDataFiles(GPLOT *gplot)
gplotGenDataFiles()
Definition gplot.c:603
PIX * gplotSimplePix2(NUMA *na1, NUMA *na2, const char *title)
gplotSimplePix2()
Definition gplot.c:795
PIX * gplotSimplePix1(NUMA *na, const char *title)
gplotSimplePix1()
Definition gplot.c:754
const char * gplotfileoutputs[]
Definition gplot.c:157
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()
Definition gplot.c:649
@ 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
struct Pix PIX
Definition pix.h:228
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