~uhh-ssd/+junk/humidity_readout

« back to all changes in this revision

Viewing changes to plplot/plplot-5.9.9/examples/java/x20.java

  • Committer: Joachim Erfle
  • Date: 2013-07-24 13:53:41 UTC
  • Revision ID: joachim.erfle@desy.de-20130724135341-1qojpp701zsn009p
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//--------------------------------------------------------------------------
 
2
// $Id: x20.java 11301 2010-11-02 17:21:08Z airwin $
 
3
//--------------------------------------------------------------------------
 
4
//
 
5
//--------------------------------------------------------------------------
 
6
// Copyright (C) 2004,2006  Andrew Ross
 
7
// Copyright (C) 2004  Alan W. Irwin
 
8
//
 
9
// This file is part of PLplot.
 
10
//
 
11
// PLplot is free software; you can redistribute it and/or modify
 
12
// it under the terms of the GNU Library General Public License as published by
 
13
// the Free Software Foundation; version 2 of the License.
 
14
//
 
15
// PLplot is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Library General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Library General Public License
 
21
// along with PLplot; if not, write to the Free Software
 
22
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 
23
//--------------------------------------------------------------------------
 
24
//
 
25
//--------------------------------------------------------------------------
 
26
// Implementation of PLplot example 20 in Java.
 
27
//--------------------------------------------------------------------------
 
28
 
 
29
//
 
30
// Current user defined command line options are not supported in
 
31
// the Java bindings
 
32
//
 
33
 
 
34
package plplot.examples;
 
35
 
 
36
import plplot.core.*;
 
37
 
 
38
import java.io.*;
 
39
import java.util.*;
 
40
 
 
41
class x20 {
 
42
    // Class data
 
43
    PLStream       pls = new PLStream();
 
44
 
 
45
    static int     XDIM          = 260;
 
46
    static int     YDIM          = 220;
 
47
    static boolean dbg           = false;
 
48
    static boolean nosombrero    = false;
 
49
    static boolean nointeractive = false;
 
50
    static String  f_name        = null;
 
51
 
 
52
    //static PLOptionTable options[];
 
53
 
 
54
    // PLOptionTable options[] = {
 
55
    // {
 
56
    //     "dbg",                       /* extra debugging plot */
 
57
    //     NULL,
 
58
    //     NULL,
 
59
    //     &dbg,
 
60
    //     PL_OPT_BOOL,
 
61
    //     "-dbg",
 
62
    //     "Extra debugging plot" },
 
63
    // {
 
64
    //     "nosombrero",                        /* Turns on test of xor function */
 
65
    //     NULL,
 
66
    //     NULL,
 
67
    //     &nosombrero,
 
68
    //     PL_OPT_BOOL,
 
69
    //     "-nosombrero",
 
70
    //     "No sombrero plot" },
 
71
    // {
 
72
    //     "nointeractive",                     /* Turns on test of xor function */
 
73
    //     NULL,
 
74
    //     NULL,
 
75
    //     &nointeractive,
 
76
    //     PL_OPT_BOOL,
 
77
    //     "-nointeractive",
 
78
    //     "No interactive selection" },
 
79
    // {
 
80
    //     "save",                      /* For saving in postscript */
 
81
    //     NULL,
 
82
    //     NULL,
 
83
    //     &f_name,
 
84
    //     PL_OPT_STRING,
 
85
    //     "-save filename",
 
86
    //       "Save sombrero plot in color postscript `filename'" },
 
87
    // {
 
88
    //     NULL,                        /* option */
 
89
    //     NULL,                        /* handler */
 
90
    //     NULL,                        /* client data */
 
91
    //     NULL,                        /* address of variable to set */
 
92
    //     0,                           /* mode flag */
 
93
    //     NULL,                        /* short syntax */
 
94
    //     NULL }                       /* long syntax */
 
95
    // };
 
96
 
 
97
 
 
98
    x20( String[] args )
 
99
    {
 
100
        double x[]   = new double[XDIM];
 
101
        double y[]   = new double[YDIM];
 
102
        double z[][] = new double[XDIM][YDIM];
 
103
        double r[][];
 
104
        double xi[] = new double[1];
 
105
        double yi[] = new double[1];
 
106
        double xe[] = new double[1];
 
107
        double ye[] = new double[1];
 
108
        int    i, j, width, height, num_col;
 
109
        int    n[] = new int[1];
 
110
        double img_f[][];
 
111
        double img_min;
 
112
        double img_max;
 
113
        double maxmin[] = new double[2];
 
114
        double x0, y0, dy, stretch;
 
115
        double xg[][], yg[][];
 
116
 
 
117
 
 
118
        //
 
119
        // Bugs in plimage():
 
120
        // -at high magnifications, the left and right edge are ragged, try
 
121
        // ./x20c -dev xwin -wplt 0.3,0.3,0.6,0.6 -ori 0.5
 
122
        //
 
123
        // Bugs in x20c.c:
 
124
        // -if the window is resized after a selection is made on "lena", when
 
125
        // making a new selection the old one will re-appear.
 
126
        //
 
127
 
 
128
        // plplot initialization
 
129
 
 
130
        // Parse and process command line arguments.
 
131
        //pls.MergeOpts(options, "x20c options", NULL);
 
132
        pls.parseopts( args, PLStream.PL_PARSE_FULL | PLStream.PL_PARSE_NOPROGRAM );
 
133
 
 
134
        // Initialize PLplot.
 
135
        pls.init();
 
136
 
 
137
        // view image border pixels
 
138
        if ( dbg )
 
139
        {
 
140
            pls.env( 1., (double) XDIM, 1., (double) YDIM, 1, 1 ); // no plot box
 
141
 
 
142
            // build a one pixel square border, for diagnostics
 
143
            for ( i = 0; i < XDIM; i++ )
 
144
                z[i][YDIM - 1] = 1.; // right
 
145
            for ( i = 0; i < XDIM; i++ )
 
146
                z[i][0] = 1.;        // left
 
147
 
 
148
            for ( i = 0; i < YDIM; i++ )
 
149
                z[0][i] = 1.;        // top
 
150
            for ( i = 0; i < YDIM; i++ )
 
151
                z[XDIM - 1][i] = 1.; // botton
 
152
 
 
153
            pls.lab( "...around a blue square.", " ", "A red border should appear..." );
 
154
 
 
155
            pls.image( z, 1., (double) XDIM, 1., (double) YDIM, 0., 0.,
 
156
                1., (double) XDIM, 1., (double) YDIM );
 
157
        }
 
158
 
 
159
        // sombrero-like demo
 
160
        if ( !nosombrero )
 
161
        {
 
162
            r = new double[XDIM][YDIM];
 
163
            pls.col0( 2 ); // draw a yellow plot box, useful for diagnostics! :(
 
164
            pls.env( 0., 2. * Math.PI, 0, 3. * Math.PI, 1, -1 );
 
165
 
 
166
            for ( i = 0; i < XDIM; i++ )
 
167
                x[i] = i * 2. * Math.PI / ( XDIM - 1 );
 
168
            for ( i = 0; i < YDIM; i++ )
 
169
                y[i] = i * 3. * Math.PI / ( YDIM - 1 );
 
170
 
 
171
            for ( i = 0; i < XDIM; i++ )
 
172
                for ( j = 0; j < YDIM; j++ )
 
173
                {
 
174
                    r[i][j] = Math.sqrt( x[i] * x[i] + y[j] * y[j] ) + 1e-3;
 
175
                    z[i][j] = Math.sin( r[i][j] ) / ( r[i][j] );
 
176
                }
 
177
 
 
178
            pls.lab( "No, an amplitude clipped \"sombrero\"", "", "Saturn?" );
 
179
            pls.ptex( 2., 2., 3., 4., 0., "Transparent image" );
 
180
            pls.image( z, 0., 2. * Math.PI, 0., 3. * Math.PI, 0.05, 1.,
 
181
                0., 2. * Math.PI, 0., 3. * Math.PI );
 
182
 
 
183
            // save the plot
 
184
            if ( f_name != null )
 
185
                save_plot( f_name );
 
186
        }
 
187
 
 
188
 
 
189
        // read Lena image
 
190
        if ( ( img_f = read_img( "lena.pgm", n ) ) == null )
 
191
        {
 
192
            if ( ( img_f = read_img( "../lena.pgm", n ) ) == null )
 
193
            {
 
194
                System.out.println( "File error - aborting" );
 
195
                pls.end();
 
196
                System.exit( 1 );
 
197
            }
 
198
        }
 
199
        num_col = n[0];
 
200
        width   = img_f.length;
 
201
        height  = img_f[0].length;
 
202
 
 
203
        // set gray colormap
 
204
        gray_cmap( num_col );
 
205
 
 
206
        // display Lena
 
207
        pls.env( 1., width, 1., height, 1, -1 );
 
208
 
 
209
        if ( !nointeractive )
 
210
            pls.lab( "Set and drag Button 1 to (re)set selection, Button 2 to finish.", " ", "Lena..." );
 
211
        else
 
212
            pls.lab( "", " ", "Lena..." );
 
213
 
 
214
        pls.image( img_f, 1., (double) width, 1., (double) height, 0., 0.,
 
215
            1., width, 1., height );
 
216
 
 
217
        // selection/expansion demo
 
218
        if ( !nointeractive )
 
219
        {
 
220
            xi[0] = 200.; xe[0] = 330.;
 
221
            yi[0] = 280.; ye[0] = 220.;
 
222
 
 
223
            if ( get_clip( xi, xe, yi, ye ) )   // get selection rectangle
 
224
            {
 
225
                pls.end();
 
226
                System.exit( 0 );
 
227
            }
 
228
 
 
229
            //
 
230
            // I'm unable to continue, clearing the plot and advancing to the next
 
231
            // one, without hiting the enter key, or pressing the button... help!
 
232
            //
 
233
            // Forcing the xwin driver to leave locate mode and destroying the
 
234
            // xhairs (in GetCursorCmd()) solves some problems, but I still have
 
235
            // to press the enter key or press Button-2 to go to next plot, even
 
236
            // if a pladv() is not present!  Using plbop() solves the problem, but
 
237
            // it shouldn't be needed!
 
238
            //
 
239
 
 
240
            // pls.bop();
 
241
 
 
242
            //
 
243
            // spause(false), adv(0), spause(true), also works,
 
244
            // but the above question remains.
 
245
            // With this approach, the previous pause state is lost,
 
246
            // as there is no API call to get its current state.
 
247
            //
 
248
 
 
249
            pls.spause( false );
 
250
            pls.adv( 0 );
 
251
 
 
252
            // display selection only
 
253
            pls.image( img_f, 1., (double) width, 1., (double) height, 0., 0., xi[0], xe[0], ye[0], yi[0] );
 
254
 
 
255
            pls.spause( true );
 
256
 
 
257
            // zoom in selection
 
258
            pls.env( xi[0], xe[0], ye[0], yi[0], 1, -1 );
 
259
            pls.image( img_f, 1., (double) width, 1., (double) height, 0., 0., xi[0], xe[0], ye[0], yi[0] );
 
260
        }
 
261
 
 
262
        // Base the dynamic range on the image contents.
 
263
        f2mnmx( img_f, width, height, maxmin );
 
264
        img_max = maxmin[0];
 
265
        img_min = maxmin[1];
 
266
 
 
267
        // For java we use 2-d arrays to replace the pltr function
 
268
        // even for the NULL case.
 
269
        xg = new double[width + 1][height + 1];
 
270
        yg = new double[width + 1][height + 1];
 
271
 
 
272
        for ( i = 0; i <= width; i++ )
 
273
        {
 
274
            for ( j = 0; j <= height; j++ )
 
275
            {
 
276
                xg[i][j] = (double) i;
 
277
                yg[i][j] = (double) j;
 
278
            }
 
279
        }
 
280
        // Draw a saturated version of the original image.  Only use
 
281
        // the middle 50% of the image's full dynamic range.
 
282
        pls.col0( 2 );
 
283
        pls.env( 0, width, 0, height, 1, -1 );
 
284
        pls.lab( "", "", "Reduced dynamic range image example" );
 
285
        pls.imagefr( img_f, 0., (double) width, 0., (double) height, 0., 0.,
 
286
            img_min + img_max * 0.25,
 
287
            img_max - img_max * 0.25,
 
288
            xg, yg );
 
289
 
 
290
        // Draw a distorted version of the original image, showing its full dynamic range.
 
291
        pls.env( 0, width, 0, height, 1, -1 );
 
292
        pls.lab( "", "", "Distorted image example" );
 
293
 
 
294
        x0      = 0.5 * width;
 
295
        y0      = 0.5 * height;
 
296
        dy      = 0.5 * height;
 
297
        stretch = 0.5;
 
298
 
 
299
        // In C / C++ the following would work, with plimagefr directly calling
 
300
        // mypltr. For compatibilty with other language bindings the same effect
 
301
        // can be achieved by generating the transformed grid first and then
 
302
        // using pltr2.
 
303
        // plimagefr(img_f, width, height, 0., width, 0., height, 0., 0., img_min, img_max, mypltr, (PLPointer) &stretch);
 
304
 
 
305
        for ( i = 0; i <= width; i++ )
 
306
        {
 
307
            for ( j = 0; j <= height; j++ )
 
308
            {
 
309
                xg[i][j] = x0 + ( x0 - i ) * ( 1.0 - stretch *
 
310
                                               Math.cos( ( j - y0 ) / dy * Math.PI * 0.5 ) );
 
311
                yg[i][j] = j;
 
312
            }
 
313
        }
 
314
 
 
315
        pls.imagefr( img_f, 0., (double) width, 0., (double) height, 0., 0., img_min, img_max, xg, yg );
 
316
        pls.end();
 
317
    }
 
318
 
 
319
    // read image from file in binary ppm format
 
320
    double [][] read_img( String fname, int [] num_col )
 
321
    {
 
322
        BufferedReader  in;
 
323
        DataInputStream in2;
 
324
        double[][] img;
 
325
        String          line;
 
326
        StringTokenizer st;
 
327
        int             i, j, w, h;
 
328
 
 
329
        // naive grayscale binary ppm reading. If you know how to, improve it
 
330
        try {
 
331
            in  = new BufferedReader( new FileReader( fname ) );
 
332
            in2 = new DataInputStream( new DataInputStream( new BufferedInputStream( new FileInputStream( fname ) ) ) );
 
333
        }
 
334
        catch ( FileNotFoundException e ) {
 
335
            System.out.println( "File " + fname + " not found" );
 
336
            return null;
 
337
        }
 
338
 
 
339
        try {
 
340
            line = in.readLine();
 
341
 
 
342
            if ( line.compareTo( "P5\n" ) == 0 )   // I only understand this!
 
343
            {
 
344
                System.out.println( line );
 
345
                System.out.println( "unknown file format " + fname );
 
346
                return null;
 
347
            }
 
348
            in2.skip( line.getBytes().length + 1 );
 
349
 
 
350
            do
 
351
            {
 
352
                line = in.readLine();
 
353
                in2.skip( line.getBytes().length + 1 );
 
354
            } while ( line.charAt( 0 ) == '#' );
 
355
 
 
356
            st = new StringTokenizer( line );
 
357
            w  = Integer.parseInt( st.nextToken() );
 
358
            h  = Integer.parseInt( st.nextToken() );
 
359
 
 
360
            line = in.readLine();
 
361
            in2.skip( line.getBytes().length + 1 );
 
362
            st         = new StringTokenizer( line );
 
363
            num_col[0] = Integer.parseInt( st.nextToken() );
 
364
 
 
365
            img = new double[w][h];
 
366
 
 
367
            for ( j = 0; j < h; j++ )
 
368
            {
 
369
                for ( i = 0; i < w; i++ )
 
370
                {
 
371
                    img[i][h - j - 1] = (double) in2.readUnsignedByte();
 
372
                }
 
373
            }
 
374
        }
 
375
        catch ( IOException e ) {
 
376
            System.out.println( "Error reading " + fname );
 
377
            return null;
 
378
        }
 
379
 
 
380
        return img;
 
381
    }
 
382
 
 
383
    // save plot
 
384
    void save_plot( String fname )
 
385
    {
 
386
        PLStream pls2 = new PLStream(); // create a new one
 
387
 
 
388
        pls2.sdev( "psc" );             // new device type. Use a known existing driver
 
389
        pls2.sfnam( fname );            // file name
 
390
 
 
391
        pls2.cpstrm( pls, false );      // copy old stream parameters to new stream
 
392
        pls2.replot();                  // do the save
 
393
    }
 
394
 
 
395
    //  get selection square interactively
 
396
    boolean get_clip( double[] xi, double[] xe, double[] yi, double[] ye )
 
397
    {
 
398
        PLGraphicsIn gin   = new PLGraphicsIn();
 
399
        double       xxi   = xi[0], yyi = yi[0], xxe = xe[0], yye = ye[0], t;
 
400
        boolean      start = false;
 
401
        boolean[] st = new boolean[1];
 
402
 
 
403
        pls.xormod( true, st ); // enter xor mode to draw a selection rectangle
 
404
 
 
405
        if ( st[0] )            // driver has xormod capability, continue
 
406
        {
 
407
            double sx[] = new double[5];
 
408
            double sy[] = new double[5];
 
409
            while ( true )
 
410
            {
 
411
                pls.xormod( false, st );
 
412
                pls.getCursor( gin );
 
413
                pls.xormod( true, st );
 
414
 
 
415
                if ( gin.getButton() == 1 )
 
416
                {
 
417
                    xxi = gin.getWX(); yyi = gin.getWY();
 
418
                    if ( start )
 
419
                        pls.line( sx, sy ); // clear previous rectangle
 
420
 
 
421
                    start = false;
 
422
 
 
423
                    sx[0] = xxi; sy[0] = yyi;
 
424
                    sx[4] = xxi; sy[4] = yyi;
 
425
                }
 
426
 
 
427
                if ( ( gin.getState() & (long) 0x100 ) != 0 )
 
428
                {
 
429
                    xxe = gin.getWX(); yye = gin.getWY();
 
430
                    if ( start )
 
431
                        pls.line( sx, sy ); // clear previous rectangle
 
432
 
 
433
                    start = true;
 
434
 
 
435
                    sx[2] = xxe; sy[2] = yye;
 
436
                    sx[1] = xxe; sy[1] = yyi;
 
437
                    sx[3] = xxi; sy[3] = yye;
 
438
                    pls.line( sx, sy ); // draw new rectangle
 
439
                }
 
440
 
 
441
                if ( gin.getButton() == 3 || gin.getKeysym() == 0x0D || gin.getKeysym() == 'Q' )
 
442
                {
 
443
                    if ( start )
 
444
                        pls.line( sx, sy ); // clear previous rectangle
 
445
                    break;
 
446
                }
 
447
            }
 
448
            pls.xormod( false, st ); // leave xor mod
 
449
            if ( xxe < xxi )
 
450
            {
 
451
                t = xxi; xxi = xxe; xxe = t;
 
452
            }
 
453
 
 
454
            if ( yyi < yye )
 
455
            {
 
456
                t = yyi; yyi = yye; yye = t;
 
457
            }
 
458
 
 
459
            xe[0] = xxe; xi[0] = xxi;
 
460
            ye[0] = yye; yi[0] = yyi;
 
461
 
 
462
            return ( gin.getKeysym() == 'Q' );
 
463
        }
 
464
 
 
465
        return false;
 
466
    }
 
467
 
 
468
    // set gray colormap
 
469
    void gray_cmap( int num_col )
 
470
    {
 
471
        double r[]   = new double[2];
 
472
        double g[]   = new double[2];
 
473
        double b[]   = new double[2];
 
474
        double pos[] = new double[2];
 
475
 
 
476
        r[0] = g[0] = b[0] = 0.0;
 
477
        r[1] = g[1] = b[1] = 1.0;
 
478
 
 
479
        pos[0] = 0.0;
 
480
        pos[1] = 1.0;
 
481
 
 
482
        pls.scmap1n( num_col );
 
483
        pls.scmap1l( true, pos, r, g, b );
 
484
    }
 
485
 
 
486
    // Calculate the minimum and maximum of a 2-d array
 
487
    void f2mnmx( double [][] f, int nx, int ny, double[] fmaxmin )
 
488
    {
 
489
        int i, j;
 
490
 
 
491
        fmaxmin[0] = f[0][0];
 
492
        fmaxmin[1] = fmaxmin[0];
 
493
 
 
494
        for ( i = 0; i < nx; i++ )
 
495
        {
 
496
            for ( j = 0; j < ny; j++ )
 
497
            {
 
498
                fmaxmin[0] = Math.max( fmaxmin[0], f[i][j] );
 
499
                fmaxmin[1] = Math.min( fmaxmin[1], f[i][j] );
 
500
            }
 
501
        }
 
502
    }
 
503
 
 
504
    public static void main( String[] args )
 
505
    {
 
506
        new x20( args );
 
507
    }
 
508
}
 
509
 
 
510
 
 
511
//--------------------------------------------------------------------------
 
512
//                              End of x20.cc
 
513
//--------------------------------------------------------------------------