Leptonica 1.68
C Image Processing Library

plottest.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 
00017 /*
00018  * plottest.c
00019  *
00020  *            plottest
00021  *
00022  *     This tests the gplot library functions that generate
00023  *     the plot commands and data required for input to gnuplot.
00024  */
00025 
00026 #include <string.h>
00027 #include <math.h>
00028 
00029 #include "allheaders.h"
00030 
00031     /* for GPLOT_STYLE, use one of the following set:
00032      *    GPLOT_LINES
00033      *    GPLOT_POINTS
00034      *    GPLOT_IMPULSE
00035      *    GPLOT_LINESPOINTS
00036      *    GPLOT_DOTS */
00037 #define  GPLOT_STYLE   GPLOT_LINES
00038 
00039     /* for GPLOT_OUTPUT use one of the following set:
00040      *    GPLOT_PNG
00041      *    GPLOT_PS
00042      *    GPLOT_EPS
00043      *    GPLOT_X11
00044      *    GPLOT_LATEX
00045      */
00046 #define  GPLOT_OUTPUT   GPLOT_X11
00047 
00048 
00049 main(int    argc,
00050      char **argv)
00051 {
00052 char        *str1, *str2;
00053 l_int32      i;
00054 size_t       size1, size2;
00055 l_float32    x, y1, y2, pi;
00056 GPLOT       *gplot1, *gplot2, *gplot3, *gplot4, *gplot5;
00057 NUMA        *nax, *nay1, *nay2;
00058 static char  mainName[] = "plottest";
00059 
00060     if (argc != 1)
00061         exit(ERROR_INT(" Syntax:  plottest", mainName, 1));
00062 
00063         /* Generate plot data */
00064     nax = numaCreate(0);
00065     nay1 = numaCreate(0);
00066     nay2 = numaCreate(0);
00067     pi = 3.1415926535;
00068     for (i = 0; i < 180; i++) {
00069         x = (pi / 180.) * i;
00070         y1 = (l_float32)sin(2.4 * x);
00071         y2 = (l_float32)cos(2.4 * x);
00072         numaAddNumber(nax, x);
00073         numaAddNumber(nay1, y1);
00074         numaAddNumber(nay2, y2);
00075     }
00076 
00077         /* Show the plot */
00078     gplot1 = gplotCreate("/tmp/plotroot1", GPLOT_OUTPUT, "Example plots",
00079                          "theta", "f(theta)");
00080     gplotAddPlot(gplot1, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)");
00081     gplotAddPlot(gplot1, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)");
00082     gplotMakeOutput(gplot1);
00083 
00084         /* Also save the plot to png */
00085     gplot1->outformat = GPLOT_PNG;
00086     stringReplace(&gplot1->outname, "/tmp/plotroot1.png");
00087     gplotMakeOutput(gplot1);
00088 
00089         /* Test gplot serialization */
00090     gplotWrite("/tmp/gplot1", gplot1);
00091     if ((gplot2 = gplotRead("/tmp/gplot1")) == NULL)
00092         exit(ERROR_INT("gplotRead failure!", mainName, 1));
00093     gplotWrite("/tmp/gplot2", gplot2);
00094 
00095         /* Are the two written gplot files the same? */
00096     str1 = (char *)l_binaryRead("/tmp/gplot1", &size1);
00097     str2 = (char *)l_binaryRead("/tmp/gplot2", &size2);
00098     if (size1 != size2)
00099         fprintf(stderr, "Error: size1 = %ld, size2 = %ld\n", size1, size2);
00100     else
00101         fprintf(stderr, "Correct: size1 = size2 = %ld\n", size1);
00102     if (strcmp(str1, str2))
00103         fprintf(stderr, "Error: str1 != str2\n");
00104     else
00105         fprintf(stderr, "Correct: str1 == str2\n");
00106     lept_free(str1);
00107     lept_free(str2);
00108 
00109         /* Read from file and regenerate the plot */
00110     gplot3 = gplotRead("/tmp/gplot2");
00111     stringReplace(&gplot3->title , "Example plots regen");
00112     gplot3->outformat = GPLOT_X11;
00113     gplotMakeOutput(gplot3);
00114 
00115         /* Build gplot but do not make the output formatted stuff */
00116     gplot4 = gplotCreate("/tmp/plotroot2", GPLOT_OUTPUT, "Example plots 2",
00117                          "theta", "f(theta)");
00118     gplotAddPlot(gplot4, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)");
00119     gplotAddPlot(gplot4, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)");
00120 
00121         /* Write, read back, and generate the plot */
00122     gplotWrite("/tmp/gplot4", gplot4);
00123     if ((gplot5 = gplotRead("/tmp/gplot4")) == NULL)
00124         exit(ERROR_INT("gplotRead failure!", mainName, 1));
00125     gplotMakeOutput(gplot5);
00126 
00127     gplotDestroy(&gplot1);
00128     gplotDestroy(&gplot2);
00129     gplotDestroy(&gplot3);
00130     gplotDestroy(&gplot4);
00131     gplotDestroy(&gplot5);
00132     numaDestroy(&nax);
00133     numaDestroy(&nay1);
00134     numaDestroy(&nay2);
00135     return 0;
00136 }
00137 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines