Leptonica  1.82.0
Image processing and image analysis suite
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 
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 
152 const char *gplotstylenames[] = {"with lines",
153  "with points",
154  "with impulses",
155  "with linespoints",
156  "with dots"};
157 const char *gplotfileoutputs[] = {"",
158  "PNG",
159  "PS",
160  "EPS",
161  "LATEX",
162  "PNM"};
163 
164 
165 /*-----------------------------------------------------------------*
166  * Basic Plotting Functions *
167  *-----------------------------------------------------------------*/
186 GPLOT *
187 gplotCreate(const char *rootname,
188  l_int32 outformat,
189  const char *title,
190  const char *xlabel,
191  const char *ylabel)
192 {
193 char *newroot;
194 char buf[Bufsize];
195 l_int32 badchar;
196 GPLOT *gplot;
197 
198  PROCNAME("gplotCreate");
199 
200  if (!rootname)
201  return (GPLOT *)ERROR_PTR("rootname not defined", procName, NULL);
202  if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
203  outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
204  outformat != GPLOT_PNM)
205  return (GPLOT *)ERROR_PTR("outformat invalid", procName, NULL);
206  stringCheckForChars(rootname, "`;&|><\"?*$()", &badchar);
207  if (badchar) /* danger of command injection */
208  return (GPLOT *)ERROR_PTR("invalid rootname", procName, NULL);
209 
210 #if !defined(HAVE_LIBPNG)
211  if (outformat == GPLOT_PNG) {
212  L_WARNING("png library missing; output pnm format\n", procName);
213  outformat = GPLOT_PNM;
214  }
215 #endif
216 
217  gplot = (GPLOT *)LEPT_CALLOC(1, sizeof(GPLOT));
218  gplot->cmddata = sarrayCreate(0);
219  gplot->datanames = sarrayCreate(0);
220  gplot->plotdata = sarrayCreate(0);
221  gplot->plotlabels = sarrayCreate(0);
222  gplot->plotstyles = numaCreate(0);
223 
224  /* Save title, labels, rootname, outformat, cmdname, outname */
225  newroot = genPathname(rootname, NULL);
226  gplot->rootname = newroot;
227  gplot->outformat = outformat;
228  snprintf(buf, Bufsize, "%s.cmd", rootname);
229  gplot->cmdname = stringNew(buf);
230  if (outformat == GPLOT_PNG)
231  snprintf(buf, Bufsize, "%s.png", newroot);
232  else if (outformat == GPLOT_PS)
233  snprintf(buf, Bufsize, "%s.ps", newroot);
234  else if (outformat == GPLOT_EPS)
235  snprintf(buf, Bufsize, "%s.eps", newroot);
236  else if (outformat == GPLOT_LATEX)
237  snprintf(buf, Bufsize, "%s.tex", newroot);
238  else if (outformat == GPLOT_PNM)
239  snprintf(buf, Bufsize, "%s.pnm", newroot);
240  gplot->outname = stringNew(buf);
241  if (title) gplot->title = stringNew(title);
242  if (xlabel) gplot->xlabel = stringNew(xlabel);
243  if (ylabel) gplot->ylabel = stringNew(ylabel);
244 
245  return gplot;
246 }
247 
248 
254 void
256 {
257 GPLOT *gplot;
258 
259  PROCNAME("gplotDestroy");
260 
261  if (pgplot == NULL) {
262  L_WARNING("ptr address is null!\n", procName);
263  return;
264  }
265 
266  if ((gplot = *pgplot) == NULL)
267  return;
268 
269  LEPT_FREE(gplot->rootname);
270  LEPT_FREE(gplot->cmdname);
271  sarrayDestroy(&gplot->cmddata);
272  sarrayDestroy(&gplot->datanames);
273  sarrayDestroy(&gplot->plotdata);
274  sarrayDestroy(&gplot->plotlabels);
275  numaDestroy(&gplot->plotstyles);
276  LEPT_FREE(gplot->outname);
277  if (gplot->title)
278  LEPT_FREE(gplot->title);
279  if (gplot->xlabel)
280  LEPT_FREE(gplot->xlabel);
281  if (gplot->ylabel)
282  LEPT_FREE(gplot->ylabel);
283 
284  LEPT_FREE(gplot);
285  *pgplot = NULL;
286 }
287 
288 
319 l_ok
321  NUMA *nax,
322  NUMA *nay,
323  l_int32 plotstyle,
324  const char *plotlabel)
325 {
326 char buf[Bufsize];
327 char emptystring[] = "";
328 char *datastr, *title;
329 l_int32 n, i;
330 l_float32 valx, valy, startx, delx;
331 SARRAY *sa;
332 
333  PROCNAME("gplotAddPlot");
334 
335  if (!gplot)
336  return ERROR_INT("gplot not defined", procName, 1);
337  if (!nay)
338  return ERROR_INT("nay not defined", procName, 1);
339  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
340  return ERROR_INT("invalid plotstyle", procName, 1);
341 
342  if ((n = numaGetCount(nay)) == 0)
343  return ERROR_INT("no points to plot", procName, 1);
344  if (nax && (n != numaGetCount(nax)))
345  return ERROR_INT("nax and nay sizes differ", procName, 1);
346  if (n == 1 && plotstyle == GPLOT_LINES) {
347  L_INFO("only 1 pt; changing style to points\n", procName);
348  plotstyle = GPLOT_POINTS;
349  }
350 
351  /* Save plotstyle and plotlabel */
352  numaGetParameters(nay, &startx, &delx);
353  numaAddNumber(gplot->plotstyles, plotstyle);
354  if (plotlabel) {
355  title = stringNew(plotlabel);
356  sarrayAddString(gplot->plotlabels, title, L_INSERT);
357  } else {
358  sarrayAddString(gplot->plotlabels, emptystring, L_COPY);
359  }
360 
361  /* Generate and save data filename */
362  gplot->nplots++;
363  snprintf(buf, Bufsize, "%s.data.%d", gplot->rootname, gplot->nplots);
364  sarrayAddString(gplot->datanames, buf, L_COPY);
365 
366  /* Generate data and save as a string */
367  sa = sarrayCreate(n);
368  for (i = 0; i < n; i++) {
369  if (nax)
370  numaGetFValue(nax, i, &valx);
371  else
372  valx = startx + i * delx;
373  numaGetFValue(nay, i, &valy);
374  snprintf(buf, Bufsize, "%f %f\n", valx, valy);
375  sarrayAddString(sa, buf, L_COPY);
376  }
377  datastr = sarrayToString(sa, 0);
378  sarrayAddString(gplot->plotdata, datastr, L_INSERT);
379  sarrayDestroy(&sa);
380 
381  return 0;
382 }
383 
384 
399 l_ok
401  l_int32 scaling)
402 {
403  PROCNAME("gplotSetScaling");
404 
405  if (!gplot)
406  return ERROR_INT("gplot not defined", procName, 1);
407  if (scaling != GPLOT_LINEAR_SCALE &&
408  scaling != GPLOT_LOG_SCALE_X &&
409  scaling != GPLOT_LOG_SCALE_Y &&
410  scaling != GPLOT_LOG_SCALE_X_Y)
411  return ERROR_INT("invalid gplot scaling", procName, 1);
412  gplot->scaling = scaling;
413  return 0;
414 }
415 
416 
430 PIX *
432 {
433  PROCNAME("gplotMakeOutputPix");
434 
435  if (!gplot)
436  return (PIX *)ERROR_PTR("gplot not defined", procName, NULL);
437  if (gplot->outformat != GPLOT_PNG && gplot->outformat != GPLOT_PNM)
438  return (PIX *)ERROR_PTR("output format not an image", procName, NULL);
439 
440  if (gplotMakeOutput(gplot))
441  return (PIX *)ERROR_PTR("plot output not made", procName, NULL);
442  return pixRead(gplot->outname);
443 }
444 
445 
465 l_ok
467 {
468 char buf[Bufsize];
469 char *cmdname;
470 
471  PROCNAME("gplotMakeOutput");
472 
473  if (!gplot)
474  return ERROR_INT("gplot not defined", procName, 1);
475 
476  if (!LeptDebugOK) {
477  L_INFO("running gnuplot is disabled; "
478  "use setLeptDebugOK(1) to enable\n", procName);
479  return 0;
480  }
481 
482 #ifdef OS_IOS /* iOS 11 does not support system() */
483  return ERROR_INT("iOS 11 does not support system()", procName, 0);
484 #endif /* OS_IOS */
485 
486  gplotGenCommandFile(gplot);
487  gplotGenDataFiles(gplot);
488  cmdname = genPathname(gplot->cmdname, NULL);
489 
490 #ifndef _WIN32
491  snprintf(buf, Bufsize, "gnuplot %s", cmdname);
492 #else
493  snprintf(buf, Bufsize, "wgnuplot %s", cmdname);
494 #endif /* _WIN32 */
495 
496  callSystemDebug(buf); /* gnuplot || wgnuplot */
497  LEPT_FREE(cmdname);
498  return 0;
499 }
500 
501 
508 l_ok
510 {
511 char buf[Bufsize];
512 char *cmdstr, *plotlabel, *dataname;
513 l_int32 i, plotstyle, nplots;
514 FILE *fp;
515 
516  PROCNAME("gplotGenCommandFile");
517 
518  if (!gplot)
519  return ERROR_INT("gplot not defined", procName, 1);
520 
521  /* Remove any previous command data */
522  sarrayClear(gplot->cmddata);
523 
524  /* Generate command data instructions */
525  if (gplot->title) { /* set title */
526  snprintf(buf, Bufsize, "set title '%s'", gplot->title);
527  sarrayAddString(gplot->cmddata, buf, L_COPY);
528  }
529  if (gplot->xlabel) { /* set xlabel */
530  snprintf(buf, Bufsize, "set xlabel '%s'", gplot->xlabel);
531  sarrayAddString(gplot->cmddata, buf, L_COPY);
532  }
533  if (gplot->ylabel) { /* set ylabel */
534  snprintf(buf, Bufsize, "set ylabel '%s'", gplot->ylabel);
535  sarrayAddString(gplot->cmddata, buf, L_COPY);
536  }
537 
538  /* Set terminal type and output */
539  if (gplot->outformat == GPLOT_PNG) {
540  snprintf(buf, Bufsize, "set terminal png; set output '%s'",
541  gplot->outname);
542  } else if (gplot->outformat == GPLOT_PS) {
543  snprintf(buf, Bufsize, "set terminal postscript; set output '%s'",
544  gplot->outname);
545  } else if (gplot->outformat == GPLOT_EPS) {
546  snprintf(buf, Bufsize, "set terminal postscript eps; set output '%s'",
547  gplot->outname);
548  } else if (gplot->outformat == GPLOT_LATEX) {
549  snprintf(buf, Bufsize, "set terminal latex; set output '%s'",
550  gplot->outname);
551  } else if (gplot->outformat == GPLOT_PNM) {
552  snprintf(buf, Bufsize, "set terminal pbm color; set output '%s'",
553  gplot->outname);
554  }
555  sarrayAddString(gplot->cmddata, buf, L_COPY);
556 
557  if (gplot->scaling == GPLOT_LOG_SCALE_X ||
558  gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
559  snprintf(buf, Bufsize, "set logscale x");
560  sarrayAddString(gplot->cmddata, buf, L_COPY);
561  }
562  if (gplot->scaling == GPLOT_LOG_SCALE_Y ||
563  gplot->scaling == GPLOT_LOG_SCALE_X_Y) {
564  snprintf(buf, Bufsize, "set logscale y");
565  sarrayAddString(gplot->cmddata, buf, L_COPY);
566  }
567 
568  nplots = sarrayGetCount(gplot->datanames);
569  for (i = 0; i < nplots; i++) {
570  plotlabel = sarrayGetString(gplot->plotlabels, i, L_NOCOPY);
571  dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
572  numaGetIValue(gplot->plotstyles, i, &plotstyle);
573  if (nplots == 1) {
574  snprintf(buf, Bufsize, "plot '%s' title '%s' %s",
575  dataname, plotlabel, gplotstylenames[plotstyle]);
576  } else {
577  if (i == 0)
578  snprintf(buf, Bufsize, "plot '%s' title '%s' %s, \\",
579  dataname, plotlabel, gplotstylenames[plotstyle]);
580  else if (i < nplots - 1)
581  snprintf(buf, Bufsize, " '%s' title '%s' %s, \\",
582  dataname, plotlabel, gplotstylenames[plotstyle]);
583  else
584  snprintf(buf, Bufsize, " '%s' title '%s' %s",
585  dataname, plotlabel, gplotstylenames[plotstyle]);
586  }
587  sarrayAddString(gplot->cmddata, buf, L_COPY);
588  }
589 
590  /* Write command data to file */
591  cmdstr = sarrayToString(gplot->cmddata, 1);
592  if ((fp = fopenWriteStream(gplot->cmdname, "w")) == NULL) {
593  LEPT_FREE(cmdstr);
594  return ERROR_INT("cmd stream not opened", procName, 1);
595  }
596  fwrite(cmdstr, 1, strlen(cmdstr), fp);
597  fclose(fp);
598  LEPT_FREE(cmdstr);
599  return 0;
600 }
601 
602 
616 l_ok
618 {
619 char *plotdata, *dataname;
620 l_int32 i, nplots;
621 FILE *fp;
622 
623  PROCNAME("gplotGenDataFiles");
624 
625  if (!gplot)
626  return ERROR_INT("gplot not defined", procName, 1);
627 
628  nplots = sarrayGetCount(gplot->datanames);
629  for (i = 0; i < nplots; i++) {
630  plotdata = sarrayGetString(gplot->plotdata, i, L_NOCOPY);
631  dataname = sarrayGetString(gplot->datanames, i, L_NOCOPY);
632  if ((fp = fopen(dataname, "w")) == NULL)
633  return ERROR_INT("datafile stream not opened", procName, 1);
634  fwrite(plotdata, 1, strlen(plotdata), fp);
635  fclose(fp);
636  }
637 
638  return 0;
639 }
640 
641 
642 /*-----------------------------------------------------------------*
643  * Quick one-line plots *
644  *-----------------------------------------------------------------*/
664 l_ok
666  l_int32 outformat,
667  const char *outroot,
668  const char *title)
669 {
670 GPLOT *gplot;
671 
672  PROCNAME("gplotSimple1");
673 
674  gplot = gplotSimpleXY1(NULL, na, GPLOT_LINES, outformat, outroot, title);
675  if (!gplot)
676  return ERROR_INT("failed to generate plot", procName, 1);
677  gplotDestroy(&gplot);
678  return 0;
679 }
680 
681 
702 l_ok
704  NUMA *na2,
705  l_int32 outformat,
706  const char *outroot,
707  const char *title)
708 {
709 GPLOT *gplot;
710 
711  PROCNAME("gplotSimple2");
712 
713  gplot = gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES,
714  outformat, outroot, title);
715  if (!gplot)
716  return ERROR_INT("failed to generate plot", procName, 1);
717  gplotDestroy(&gplot);
718  return 0;
719 }
720 
721 
742 l_ok
744  l_int32 outformat,
745  const char *outroot,
746  const char *title)
747 {
748 GPLOT *gplot;
749 
750  PROCNAME("gplotSimpleN");
751 
752  gplot = gplotSimpleXYN(NULL, naa, GPLOT_LINES, outformat, outroot, title);
753  if (!gplot)
754  return ERROR_INT("failed to generate plot", procName, 1);
755  gplotDestroy(&gplot);
756  return 0;
757 }
758 
759 
775 PIX *
777  const char *title)
778 {
779 char buf[64];
780 static l_int32 index;
781 GPLOT *gplot;
782 PIX *pix;
783 
784  PROCNAME("gplotSimplePix1");
785 
786  if (!na)
787  return (PIX *)ERROR_PTR("na not defined", procName, NULL);
788 
789  lept_mkdir("lept/gplot/pix");
790  snprintf(buf, sizeof(buf), "/tmp/lept/gplot/pix1.%d", index++);
791  gplot = gplotSimpleXY1(NULL, na, GPLOT_LINES, GPLOT_PNG, buf, title);
792  if (!gplot)
793  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
794  pix = pixRead(gplot->outname);
795  gplotDestroy(&gplot);
796  if (!pix)
797  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
798  return pix;
799 }
800 
801 
818 PIX *
820  NUMA *na2,
821  const char *title)
822 {
823 char buf[64];
824 static l_int32 index;
825 GPLOT *gplot;
826 PIX *pix;
827 
828  PROCNAME("gplotSimplePix2");
829 
830  if (!na1 || !na2)
831  return (PIX *)ERROR_PTR("both na1, na2 not defined", procName, NULL);
832 
833  lept_mkdir("lept/gplot/pix");
834  snprintf(buf, sizeof(buf), "/tmp/lept/gplot/pix2.%d", index++);
835  gplot = gplotSimpleXY2(NULL, na1, na2, GPLOT_LINES, GPLOT_PNG, buf, title);
836  if (!gplot)
837  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
838  pix = pixRead(gplot->outname);
839  gplotDestroy(&gplot);
840  if (!pix)
841  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
842  return pix;
843 }
844 
845 
862 PIX *
864  const char *title)
865 {
866 char buf[64];
867 static l_int32 index;
868 GPLOT *gplot;
869 PIX *pix;
870 
871  PROCNAME("gplotSimplePixN");
872 
873  if (!naa)
874  return (PIX *)ERROR_PTR("naa not defined", procName, NULL);
875 
876  lept_mkdir("lept/gplot/pix");
877  snprintf(buf, sizeof(buf), "/tmp/lept/gplot/pixN.%d", index++);
878  gplot = gplotSimpleXYN(NULL, naa, GPLOT_LINES, GPLOT_PNG, buf, title);
879  if (!gplot)
880  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
881  pix = pixRead(gplot->outname);
882  gplotDestroy(&gplot);
883  if (!pix)
884  return (PIX *)ERROR_PTR("failed to generate plot", procName, NULL);
885  return pix;
886 }
887 
888 
914 GPLOT *
916  NUMA *nay,
917  l_int32 plotstyle,
918  l_int32 outformat,
919  const char *outroot,
920  const char *title)
921 {
922 GPLOT *gplot;
923 
924  PROCNAME("gplotSimpleXY1");
925 
926  if (!nay)
927  return (GPLOT *)ERROR_PTR("nay not defined", procName, NULL);
928  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
929  return (GPLOT *)ERROR_PTR("invalid plotstyle", procName, NULL);
930  if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
931  outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
932  outformat != GPLOT_PNM)
933  return (GPLOT *)ERROR_PTR("invalid outformat", procName, NULL);
934  if (!outroot)
935  return (GPLOT *)ERROR_PTR("outroot not specified", procName, NULL);
936 
937  if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
938  return (GPLOT *)ERROR_PTR("gplot not made", procName, NULL);
939  gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
940  gplotMakeOutput(gplot);
941  return gplot;
942 }
943 
944 
971 GPLOT *
973  NUMA *nay1,
974  NUMA *nay2,
975  l_int32 plotstyle,
976  l_int32 outformat,
977  const char *outroot,
978  const char *title)
979 {
980 GPLOT *gplot;
981 
982  PROCNAME("gplotSimpleXY2");
983 
984  if (!nay1 || !nay2)
985  return (GPLOT *)ERROR_PTR("nay1 and nay2 not both defined",
986  procName, NULL);
987  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
988  return (GPLOT *)ERROR_PTR("invalid plotstyle", procName, NULL);
989  if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
990  outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
991  outformat != GPLOT_PNM)
992  return (GPLOT *)ERROR_PTR("invalid outformat", procName, NULL);
993  if (!outroot)
994  return (GPLOT *)ERROR_PTR("outroot not specified", procName, NULL);
995 
996  if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
997  return (GPLOT *)ERROR_PTR("gplot not made", procName, NULL);
998  gplotAddPlot(gplot, nax, nay1, plotstyle, NULL);
999  gplotAddPlot(gplot, nax, nay2, plotstyle, NULL);
1000  gplotMakeOutput(gplot);
1001  return gplot;
1002 }
1003 
1004 
1030 GPLOT *
1032  NUMAA *naay,
1033  l_int32 plotstyle,
1034  l_int32 outformat,
1035  const char *outroot,
1036  const char *title)
1037 {
1038 l_int32 i, n;
1039 GPLOT *gplot;
1040 NUMA *nay;
1041 
1042  PROCNAME("gplotSimpleXYN");
1043 
1044  if (!naay)
1045  return (GPLOT *)ERROR_PTR("naay not defined", procName, NULL);
1046  if ((n = numaaGetCount(naay)) == 0)
1047  return (GPLOT *)ERROR_PTR("no numa in array", procName, NULL);
1048  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1049  return (GPLOT *)ERROR_PTR("invalid plotstyle", procName, NULL);
1050  if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
1051  outformat != GPLOT_EPS && outformat != GPLOT_LATEX &&
1052  outformat != GPLOT_PNM)
1053  return (GPLOT *)ERROR_PTR("invalid outformat", procName, NULL);
1054  if (!outroot)
1055  return (GPLOT *)ERROR_PTR("outroot not specified", procName, NULL);
1056 
1057  if ((gplot = gplotCreate(outroot, outformat, title, NULL, NULL)) == 0)
1058  return (GPLOT *)ERROR_PTR("gplot not made", procName, NULL);
1059  for (i = 0; i < n; i++) {
1060  nay = numaaGetNuma(naay, i, L_CLONE);
1061  gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1062  numaDestroy(&nay);
1063  }
1064  gplotMakeOutput(gplot);
1065  return gplot;
1066 }
1067 
1068 
1087 PIX *
1089  l_int32 plotstyle,
1090  const char *rootname,
1091  const char *title,
1092  const char *xlabel,
1093  const char *ylabel)
1094 {
1095 GPLOT *gplot;
1096 PIX *pix;
1097 
1098  PROCNAME("gplotGeneralPix1");
1099 
1100  if (!na)
1101  return (PIX *)ERROR_PTR("na not defined", procName, NULL);
1102  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1103  return (PIX *)ERROR_PTR("invalid plotstyle", procName, NULL);
1104  if (!rootname)
1105  return (PIX *)ERROR_PTR("rootname not defined", procName, NULL);
1106 
1107  gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1108  if (!gplot)
1109  return (PIX *)ERROR_PTR("gplot not made", procName, NULL);
1110  gplotAddPlot(gplot, NULL, na, plotstyle, NULL);
1111  pix = gplotMakeOutputPix(gplot);
1112  gplotDestroy(&gplot);
1113  return pix;
1114 }
1115 
1116 
1136 PIX *
1138  NUMA *na2,
1139  l_int32 plotstyle,
1140  const char *rootname,
1141  const char *title,
1142  const char *xlabel,
1143  const char *ylabel)
1144 {
1145 GPLOT *gplot;
1146 PIX *pix;
1147 
1148  PROCNAME("gplotGeneralPix2");
1149 
1150  if (!na1)
1151  return (PIX *)ERROR_PTR("na1 not defined", procName, NULL);
1152  if (!na2)
1153  return (PIX *)ERROR_PTR("na2 not defined", procName, NULL);
1154  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1155  return (PIX *)ERROR_PTR("invalid plotstyle", procName, NULL);
1156  if (!rootname)
1157  return (PIX *)ERROR_PTR("rootname not defined", procName, NULL);
1158 
1159  gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1160  if (!gplot)
1161  return (PIX *)ERROR_PTR("gplot not made", procName, NULL);
1162  gplotAddPlot(gplot, na1, na2, plotstyle, NULL);
1163  pix = gplotMakeOutputPix(gplot);
1164  gplotDestroy(&gplot);
1165  return pix;
1166 }
1167 
1168 
1188 PIX *
1190  NUMAA *naay,
1191  l_int32 plotstyle,
1192  const char *rootname,
1193  const char *title,
1194  const char *xlabel,
1195  const char *ylabel)
1196 {
1197 l_int32 i, n;
1198 GPLOT *gplot;
1199 NUMA *nay;
1200 PIX *pix;
1201 
1202  PROCNAME("gplotGeneralPixN");
1203 
1204  if (!nax)
1205  return (PIX *)ERROR_PTR("nax not defined", procName, NULL);
1206  if (!naay)
1207  return (PIX *)ERROR_PTR("naay not defined", procName, NULL);
1208  if ((n = numaaGetCount(naay)) == 0)
1209  return (PIX *)ERROR_PTR("no numa in array", procName, NULL);
1210  if (plotstyle < 0 || plotstyle >= NUM_GPLOT_STYLES)
1211  return (PIX *)ERROR_PTR("invalid plotstyle", procName, NULL);
1212  if (!rootname)
1213  return (PIX *)ERROR_PTR("rootname not defined", procName, NULL);
1214 
1215  gplot = gplotCreate(rootname, GPLOT_PNG, title, xlabel, ylabel);
1216  if (!gplot)
1217  return (PIX *)ERROR_PTR("gplot not made", procName, NULL);
1218  for (i = 0; i < n; i++) {
1219  nay = numaaGetNuma(naay, i, L_CLONE);
1220  gplotAddPlot(gplot, nax, nay, plotstyle, NULL);
1221  numaDestroy(&nay);
1222  }
1223  pix = gplotMakeOutputPix(gplot);
1224  gplotDestroy(&gplot);
1225  return pix;
1226 }
1227 
1228 
1229 /*-----------------------------------------------------------------*
1230  * Serialize for I/O *
1231  *-----------------------------------------------------------------*/
1238 GPLOT *
1239 gplotRead(const char *filename)
1240 {
1241 char buf[Bufsize];
1242 char *rootname, *title, *xlabel, *ylabel, *ignores;
1243 l_int32 outformat, ret, version, ignore;
1244 FILE *fp;
1245 GPLOT *gplot;
1246 
1247  PROCNAME("gplotRead");
1248 
1249  if (!filename)
1250  return (GPLOT *)ERROR_PTR("filename not defined", procName, NULL);
1251 
1252  if ((fp = fopenReadStream(filename)) == NULL)
1253  return (GPLOT *)ERROR_PTR("stream not opened", procName, NULL);
1254 
1255  ret = fscanf(fp, "Gplot Version %d\n", &version);
1256  if (ret != 1) {
1257  fclose(fp);
1258  return (GPLOT *)ERROR_PTR("not a gplot file", procName, NULL);
1259  }
1260  if (version != GPLOT_VERSION_NUMBER) {
1261  fclose(fp);
1262  return (GPLOT *)ERROR_PTR("invalid gplot version", procName, NULL);
1263  }
1264 
1265  ignore = fscanf(fp, "Rootname: %511s\n", buf); /* Bufsize - 1 */
1266  rootname = stringNew(buf);
1267  ignore = fscanf(fp, "Output format: %d\n", &outformat);
1268  ignores = fgets(buf, Bufsize, fp); /* Title: ... */
1269  title = stringNew(buf + 7);
1270  title[strlen(title) - 1] = '\0';
1271  ignores = fgets(buf, Bufsize, fp); /* X axis label: ... */
1272  xlabel = stringNew(buf + 14);
1273  xlabel[strlen(xlabel) - 1] = '\0';
1274  ignores = fgets(buf, Bufsize, fp); /* Y axis label: ... */
1275  ylabel = stringNew(buf + 14);
1276  ylabel[strlen(ylabel) - 1] = '\0';
1277 
1278  gplot = gplotCreate(rootname, outformat, title, xlabel, ylabel);
1279  LEPT_FREE(rootname);
1280  LEPT_FREE(title);
1281  LEPT_FREE(xlabel);
1282  LEPT_FREE(ylabel);
1283  if (!gplot) {
1284  fclose(fp);
1285  return (GPLOT *)ERROR_PTR("gplot not made", procName, NULL);
1286  }
1287  sarrayDestroy(&gplot->cmddata);
1288  sarrayDestroy(&gplot->datanames);
1289  sarrayDestroy(&gplot->plotdata);
1290  sarrayDestroy(&gplot->plotlabels);
1291  numaDestroy(&gplot->plotstyles);
1292 
1293  ignore = fscanf(fp, "Commandfile name: %511s\n", buf); /* Bufsize - 1 */
1294  stringReplace(&gplot->cmdname, buf);
1295  ignore = fscanf(fp, "\nCommandfile data:");
1296  gplot->cmddata = sarrayReadStream(fp);
1297  ignore = fscanf(fp, "\nDatafile names:");
1298  gplot->datanames = sarrayReadStream(fp);
1299  ignore = fscanf(fp, "\nPlot data:");
1300  gplot->plotdata = sarrayReadStream(fp);
1301  ignore = fscanf(fp, "\nPlot titles:");
1302  gplot->plotlabels = sarrayReadStream(fp);
1303  ignore = fscanf(fp, "\nPlot styles:");
1304  gplot->plotstyles = numaReadStream(fp);
1305 
1306  ignore = fscanf(fp, "Number of plots: %d\n", &gplot->nplots);
1307  ignore = fscanf(fp, "Output file name: %511s\n", buf);
1308  stringReplace(&gplot->outname, buf);
1309  ignore = fscanf(fp, "Axis scaling: %d\n", &gplot->scaling);
1310 
1311  fclose(fp);
1312  return gplot;
1313 }
1314 
1315 
1323 l_ok
1324 gplotWrite(const char *filename,
1325  GPLOT *gplot)
1326 {
1327 FILE *fp;
1328 
1329  PROCNAME("gplotWrite");
1330 
1331  if (!filename)
1332  return ERROR_INT("filename not defined", procName, 1);
1333  if (!gplot)
1334  return ERROR_INT("gplot not defined", procName, 1);
1335 
1336  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1337  return ERROR_INT("stream not opened", procName, 1);
1338 
1339  fprintf(fp, "Gplot Version %d\n", GPLOT_VERSION_NUMBER);
1340  fprintf(fp, "Rootname: %s\n", gplot->rootname);
1341  fprintf(fp, "Output format: %d\n", gplot->outformat);
1342  fprintf(fp, "Title: %s\n", gplot->title);
1343  fprintf(fp, "X axis label: %s\n", gplot->xlabel);
1344  fprintf(fp, "Y axis label: %s\n", gplot->ylabel);
1345 
1346  fprintf(fp, "Commandfile name: %s\n", gplot->cmdname);
1347  fprintf(fp, "\nCommandfile data:");
1348  sarrayWriteStream(fp, gplot->cmddata);
1349  fprintf(fp, "\nDatafile names:");
1350  sarrayWriteStream(fp, gplot->datanames);
1351  fprintf(fp, "\nPlot data:");
1352  sarrayWriteStream(fp, gplot->plotdata);
1353  fprintf(fp, "\nPlot titles:");
1354  sarrayWriteStream(fp, gplot->plotlabels);
1355  fprintf(fp, "\nPlot styles:");
1356  numaWriteStderr(gplot->plotstyles);
1357 
1358  fprintf(fp, "Number of plots: %d\n", gplot->nplots);
1359  fprintf(fp, "Output file name: %s\n", gplot->outname);
1360  fprintf(fp, "Axis scaling: %d\n", gplot->scaling);
1361 
1362  fclose(fp);
1363  return 0;
1364 }
PIX * gplotSimplePix2(NUMA *na1, NUMA *na2, const char *title)
gplotSimplePix2()
Definition: gplot.c:819
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
Definition: gplot.c:320
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
Definition: gplot.c:466
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:1189
PIX * gplotMakeOutputPix(GPLOT *gplot)
gplotMakeOutputPix()
Definition: gplot.c:431
GPLOT * gplotSimpleXY1(NUMA *nax, NUMA *nay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY1()
Definition: gplot.c:915
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
Definition: gplot.c:255
l_ok gplotSimple2(NUMA *na1, NUMA *na2, l_int32 outformat, const char *outroot, const char *title)
gplotSimple2()
Definition: gplot.c:703
l_ok gplotWrite(const char *filename, GPLOT *gplot)
gplotWrite()
Definition: gplot.c:1324
l_ok gplotGenCommandFile(GPLOT *gplot)
gplotGenCommandFile()
Definition: gplot.c:509
l_ok gplotSetScaling(GPLOT *gplot, l_int32 scaling)
gplotSetScaling()
Definition: gplot.c:400
GPLOT * gplotRead(const char *filename)
gplotRead()
Definition: gplot.c:1239
PIX * gplotGeneralPix1(NUMA *na, l_int32 plotstyle, const char *rootname, const char *title, const char *xlabel, const char *ylabel)
gplotGeneralPix1()
Definition: gplot.c:1088
PIX * gplotSimplePixN(NUMAA *naa, const char *title)
gplotSimplePixN()
Definition: gplot.c:863
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:743
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:1137
GPLOT * gplotSimpleXYN(NUMA *nax, NUMAA *naay, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXYN()
Definition: gplot.c:1031
l_ok gplotGenDataFiles(GPLOT *gplot)
gplotGenDataFiles()
Definition: gplot.c:617
PIX * gplotSimplePix1(NUMA *na, const char *title)
gplotSimplePix1()
Definition: gplot.c:776
GPLOT * gplotSimpleXY2(NUMA *nax, NUMA *nay1, NUMA *nay2, l_int32 plotstyle, l_int32 outformat, const char *outroot, const char *title)
gplotSimpleXY2()
Definition: gplot.c:972
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:665
@ GPLOT_LINEAR_SCALE
Definition: gplot.h:66
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
Definition: numabasic.c:478
NUMA * numaReadStream(FILE *fp)
numaReadStream()
Definition: numabasic.c:1137
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
Definition: numabasic.c:719
l_ok numaWriteStderr(NUMA *na)
numaWriteStderr()
Definition: numabasic.c:1313
NUMA * numaCreate(l_int32 n)
numaCreate()
Definition: numabasic.c:194
l_int32 numaaGetCount(NUMAA *naa)
numaaGetCount()
Definition: numabasic.c:1631
NUMA * numaaGetNuma(NUMAA *naa, l_int32 index, l_int32 accessflag)
numaaGetNuma()
Definition: numabasic.c:1740
void numaDestroy(NUMA **pna)
numaDestroy()
Definition: numabasic.c:366
l_int32 numaGetCount(NUMA *na)
numaGetCount()
Definition: numabasic.c:658
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
Definition: numabasic.c:754
l_ok numaGetParameters(NUMA *na, l_float32 *pstartx, l_float32 *pdelx)
numaGetParameters()
Definition: numabasic.c:963
@ L_COPY
Definition: pix.h:712
@ L_CLONE
Definition: pix.h:713
@ L_NOCOPY
Definition: pix.h:710
@ L_INSERT
Definition: pix.h:711
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:193
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:170
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:703
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
Definition: sarray1.c:1479
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:643
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:362
l_ok sarrayClear(SARRAY *sa)
sarrayClear()
Definition: sarray1.c:616
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:451
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
Definition: sarray1.c:785
l_ok sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
Definition: sarray1.c:1614
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
Definition: array.h:71
Definition: array.h:83
Definition: pix.h:139
Definition: array.h:127
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:223
l_ok stringReplace(char **pdest, const char *src)
stringReplace()
Definition: utils2.c:345
void callSystemDebug(const char *cmd)
callSystemDebug()
Definition: utils2.c:2752
l_ok stringCheckForChars(const char *src, const char *chars, l_int32 *pfound)
stringCheckForChars()
Definition: utils2.c:787
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1975
char * genPathname(const char *dir, const char *fname)
genPathname()
Definition: utils2.c:3173
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
Definition: utils2.c:2218
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1932