~uhh-ssd/+junk/humidity_readout

« back to all changes in this revision

Viewing changes to plplot/plplot-5.9.9/src/plimage.c

  • 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
// plimage()
 
2
//
 
3
// Author: Alessandro Mirone, Nov 2001
 
4
// Adapted: Joao Cardoso
 
5
// Updated: Hezekiah Carty 2008
 
6
//
 
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
 
13
// by the Free Software Foundation; either version 2 of the License, or
 
14
// (at your option) any later version.
 
15
//
 
16
// PLplot is distributed in the hope that it will be useful,
 
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
// GNU Library General Public License for more details.
 
20
//
 
21
// You should have received a copy of the GNU Library General Public License
 
22
// along with PLplot; if not, write to the Free Software
 
23
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
24
//
 
25
 
 
26
#include "plplotP.h"
 
27
 
 
28
#define COLOR_MIN        0.0
 
29
#define COLOR_MAX        1.0
 
30
#define COLOR_NO_PLOT    ( -1.0 )
 
31
 
 
32
// Get better names, those are too cryptic!
 
33
//
 
34
// ZEROW2B: zero writing to buffer ?
 
35
// ZEROW2D: zero writing to display ?
 
36
// ONEW2B: set writing to buffer ?
 
37
// ONEW2D: set writing to display ?
 
38
//
 
39
 
 
40
void
 
41
NoBufferNoPixmap()
 
42
{
 
43
    PLINT op = ZEROW2B;
 
44
 
 
45
    plsc->plbuf_write = 0; // TODO: store previous state
 
46
    plP_esc( PLESC_EXPOSE, NULL );
 
47
    plP_esc( PLESC_IMAGEOPS, &op );
 
48
}
 
49
 
 
50
void
 
51
RestoreWrite2BufferPixmap()
 
52
{
 
53
    PLINT op = ONEW2B;
 
54
 
 
55
    plsc->plbuf_write = 1; // TODO: revert from previous state
 
56
    plP_esc( PLESC_IMAGEOPS, &op );
 
57
}
 
58
 
 
59
void
 
60
disabledisplay()
 
61
{
 
62
    PLINT op = ZEROW2D;
 
63
 
 
64
    plP_esc( PLESC_IMAGEOPS, &op );
 
65
}
 
66
 
 
67
void
 
68
enabledisplay()
 
69
{
 
70
    PLINT op = ONEW2D;
 
71
 
 
72
    plP_esc( PLESC_IMAGEOPS, &op );
 
73
    plP_esc( PLESC_EXPOSE, NULL );
 
74
}
 
75
 
 
76
 
 
77
 
 
78
//
 
79
// NOTE: The plshade* functions require that both pltr and pltr_data are set
 
80
// in order for pltr to be used.  plimageslow does NOT require this, so it is
 
81
// up to the user to make sure pltr_data is something non-NULL if pltr
 
82
// requires it.
 
83
// Plottable values in idata must be scaled between COLOR_MIN and COLOR_MAX.
 
84
// This is an internal function, and should not be used directly.  Its
 
85
// interface may change.
 
86
//
 
87
void
 
88
plimageslow( PLFLT *idata, PLINT nx, PLINT ny,
 
89
             PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
 
90
             void ( *pltr )( PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer ),
 
91
             PLPointer pltr_data )
 
92
{
 
93
    // Indices
 
94
    PLINT ix, iy, i;
 
95
    // Float coordinates
 
96
    PLFLT xf[4], yf[4];
 
97
    // Translated (by pltr) coordinates
 
98
    PLFLT tx, ty;
 
99
    // The corners of a single filled region
 
100
    // int corners[4]; - unreferenced
 
101
    // The color to use in the fill
 
102
    PLFLT color;
 
103
 
 
104
    plP_esc( PLESC_START_RASTERIZE, NULL );
 
105
    for ( ix = 0; ix < nx; ix++ )
 
106
    {
 
107
        for ( iy = 0; iy < ny; iy++ )
 
108
        {
 
109
            // Only plot values within in appropriate range
 
110
            color = idata[ix * ny + iy];
 
111
            if ( color == COLOR_NO_PLOT )
 
112
                continue;
 
113
 
 
114
            // The color value has to be scaled to 0.0 -> 1.0 plcol1 color values
 
115
            plcol1( color / COLOR_MAX );
 
116
 
 
117
            xf[0] = xf[1] = ix;
 
118
            xf[2] = xf[3] = ix + 1;
 
119
            yf[0] = yf[3] = iy;
 
120
            yf[1] = yf[2] = iy + 1;
 
121
 
 
122
            if ( pltr )
 
123
            {
 
124
                for ( i = 0; i < 4; i++ )
 
125
                {
 
126
                    // Translate the points
 
127
                    ( *pltr )( xf[i], yf[i], &tx, &ty, pltr_data );
 
128
                    xf[i] = tx;
 
129
                    yf[i] = ty;
 
130
                }
 
131
            }
 
132
            else
 
133
            {
 
134
                for ( i = 0; i < 4; i++ )
 
135
                {
 
136
                    // Automatic translation to the specified plot area
 
137
                    xf[i] = xmin + xf[i] * dx;
 
138
                    yf[i] = ymin + yf[i] * dy;
 
139
                }
 
140
            }
 
141
            plfill( 4, xf, yf );
 
142
        }
 
143
    }
 
144
    plP_esc( PLESC_END_RASTERIZE, NULL );
 
145
}
 
146
 
 
147
void
 
148
grimage( short *x, short *y, unsigned short *z, PLINT nx, PLINT ny )
 
149
{
 
150
    plsc->dev_ix    = x;
 
151
    plsc->dev_iy    = y;
 
152
    plsc->dev_z     = z;
 
153
    plsc->dev_nptsX = nx;
 
154
    plsc->dev_nptsY = ny;
 
155
 
 
156
    plP_esc( PLESC_IMAGE, NULL );
 
157
}
 
158
 
 
159
//--------------------------------------------------------------------------
 
160
// plimagefr
 
161
//
 
162
// arguments are
 
163
//   idata: array containing image data
 
164
//   nx: dimension of the array in the X axis.
 
165
//   ny: dimension of the  array in the Y axis
 
166
//   The array data is indexed like data[ix][iy]
 
167
//
 
168
//   xmin, xmax, ymin, ymax:
 
169
//       data[0][0] corresponds to (xmin,ymin)
 
170
//       data[nx-1][ny-1] to (xmax,ymax)
 
171
//
 
172
//   zmin, zmax:
 
173
//       only data within bounds zmin <= data <= zmax will be
 
174
//       plotted. If zmin == zmax, all data will be ploted.
 
175
//
 
176
//   valuemin, valuemax:
 
177
//       The minimum and maximum values to use for value -> color
 
178
//       mappings.  A value in idata of valuemin or less will have
 
179
//       color 0.0 and a value in idata of valuemax or greater will
 
180
//       have color 1.0.  Values between valuemin and valuemax will
 
181
//       map linearly to to the colors between 0.0 and 1.0.
 
182
//       If you do not want to display values outside of the
 
183
//       (valuemin -> valuemax) range, then set zmin = valuemin and
 
184
//       zmax = valuemax.
 
185
//       This allows for multiple plots to use the same color scale
 
186
//       with a consistent value -> color mapping, regardless of the
 
187
//       image content.
 
188
//
 
189
//--------------------------------------------------------------------------
 
190
void
 
191
c_plimagefr( const PLFLT **idata, PLINT nx, PLINT ny,
 
192
             PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
 
193
             PLFLT valuemin, PLFLT valuemax,
 
194
             void ( *pltr )( PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer ),
 
195
             PLPointer pltr_data )
 
196
{
 
197
    plfimagefr( plf2ops_c(), (PLPointer) idata, nx, ny,
 
198
        xmin, xmax, ymin, ymax, zmin, zmax,
 
199
        valuemin, valuemax, pltr, pltr_data );
 
200
}
 
201
 
 
202
void
 
203
plfimagefr( PLF2OPS idataops, PLPointer idatap, PLINT nx, PLINT ny,
 
204
            PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
 
205
            PLFLT valuemin, PLFLT valuemax,
 
206
            void ( *pltr )( PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer ),
 
207
            PLPointer pltr_data )
 
208
{
 
209
    PLINT ix, iy;
 
210
    PLFLT dx, dy;
 
211
    // z holds scaled image pixel values
 
212
    PLFLT *z;
 
213
    // This is used when looping through the image array, checking to
 
214
    // make sure the values are within an acceptable range.
 
215
    PLFLT datum;
 
216
    // Color palette 0 color in use before the plimage* call
 
217
    PLINT init_color;
 
218
    // Color range
 
219
    PLFLT color_min, color_max, color_range;
 
220
 
 
221
    if ( plsc->level < 3 )
 
222
    {
 
223
        plabort( "plimagefr: window must be set up first" );
 
224
        return;
 
225
    }
 
226
 
 
227
    if ( nx <= 0 || ny <= 0 )
 
228
    {
 
229
        plabort( "plimagefr: nx and ny must be positive" );
 
230
        return;
 
231
    }
 
232
 
 
233
    if ( ( z = (PLFLT *) malloc( ny * nx * sizeof ( PLFLT ) ) ) == NULL )
 
234
    {
 
235
        plexit( "plimagefr: Insufficient memory" );
 
236
    }
 
237
 
 
238
    // Save the currently-in-use color.
 
239
    init_color = plsc->icol0;
 
240
 
 
241
    // If no acceptable data range is given, then set the min/max data range
 
242
    // to include all of the given data.
 
243
    if ( zmin == zmax )
 
244
    {
 
245
        // Find the minimum and maximum values in the image
 
246
        idataops->minmax( idatap, nx, ny, &zmin, &zmax );
 
247
    }
 
248
 
 
249
    // Calculate the size of the color range to use
 
250
    color_min   = plsc->cmap1_min;
 
251
    color_max   = plsc->cmap1_max;
 
252
    color_range = color_max - color_min;
 
253
 
 
254
    // Go through the image values and scale them to fit in
 
255
    // the COLOR_MIN to COLOR_MAX range.
 
256
    // Any values greater than valuemax are set to valuemax,
 
257
    // and values less than valuemin are set to valuemin.
 
258
    // Any values outside of zmin to zmax are flagged so they
 
259
    // are not plotted.
 
260
    for ( ix = 0; ix < nx; ix++ )
 
261
    {
 
262
        for ( iy = 0; iy < ny; iy++ )
 
263
        {
 
264
            if ( valuemin == valuemax )
 
265
            {
 
266
                // If valuemin == valuemax, avoid dividing by zero.
 
267
                z[ix * ny + iy] = ( color_max + color_min ) / 2.0;
 
268
            }
 
269
            else
 
270
            {
 
271
                datum = idataops->get( idatap, ix, iy );
 
272
                if ( isnan( datum ) || datum < zmin || datum > zmax )
 
273
                {
 
274
                    // Set to a guaranteed-not-to-plot value
 
275
                    z[ix * ny + iy] = COLOR_NO_PLOT;
 
276
                }
 
277
                else
 
278
                {
 
279
                    if ( datum < valuemin )
 
280
                    {
 
281
                        datum = valuemin;
 
282
                    }
 
283
                    else if ( datum > valuemax )
 
284
                    {
 
285
                        datum = valuemax;
 
286
                    }
 
287
                    // Set to a value scaled between color_min and color_max.
 
288
                    z[ix * ny + iy] =
 
289
                        color_min + ( datum - valuemin + COLOR_MIN ) / ( valuemax - valuemin ) * COLOR_MAX * color_range;
 
290
                }
 
291
            }
 
292
        }
 
293
    }
 
294
 
 
295
    // dx and dy are the plot-coordinates pixel sizes for an untransformed
 
296
    // image
 
297
    dx = ( xmax - xmin ) / (PLFLT) nx;
 
298
    dy = ( ymax - ymin ) / (PLFLT) ny;
 
299
 
 
300
    plP_image( z, nx, ny, xmin, ymin, dx, dy, pltr, pltr_data );
 
301
 
 
302
    plcol0( init_color );
 
303
 
 
304
    free( z );
 
305
}
 
306
 
 
307
//--------------------------------------------------------------------------
 
308
// plimage
 
309
//
 
310
// arguments are
 
311
//   idata: array containing image data
 
312
//   nx: dimension of the array in the X axis.
 
313
//   ny: dimension of the  array in the Y axis
 
314
//   The array data is indexed like data[ix][iy]
 
315
//
 
316
//   xmin, xmax, ymin, ymax:
 
317
//       data[0][0] corresponds to (xmin,ymin)
 
318
//       data[nx-1][ny-1] to (xmax,ymax)
 
319
//
 
320
//   zmin, zmax:
 
321
//       only data within bounds zmin <= data <= zmax will be
 
322
//       plotted. If zmin == zmax, all data will be ploted.
 
323
//
 
324
//   Dxmin, Dxmax, Dymin, Dymax:
 
325
//       plots only the window of points whose(x,y)'s fall
 
326
//       inside the [Dxmin->Dxmax]X[Dymin->Dymax] window
 
327
//
 
328
//--------------------------------------------------------------------------
 
329
void
 
330
c_plimage( const PLFLT **idata, PLINT nx, PLINT ny,
 
331
           PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
 
332
           PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax )
 
333
{
 
334
    plfimage( plf2ops_c(), (PLPointer) idata, nx, ny,
 
335
        xmin, xmax, ymin, ymax, zmin, zmax,
 
336
        Dxmin, Dxmax, Dymin, Dymax );
 
337
}
 
338
 
 
339
void
 
340
plfimage( PLF2OPS idataops, PLPointer idatap, PLINT nx, PLINT ny,
 
341
          PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
 
342
          PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax )
 
343
{
 
344
    PLINT   ix, iy, ixx, iyy, xm, ym, nnx, nny;
 
345
    PLFLT   data_min, data_max, dx, dy;
 
346
    // z holds the subimage (Dxmin, Dymin) - (Dxmax, Dymax)
 
347
    PLFLT   **z;
 
348
    PLF2OPS zops;
 
349
    // Was any space allocated for z?
 
350
    PLBOOL  copied;
 
351
    copied = FALSE;
 
352
 
 
353
    if ( nx <= 0 || ny <= 0 )
 
354
    {
 
355
        plabort( "plimage: nx and ny must be positive" );
 
356
        return;
 
357
    }
 
358
 
 
359
    if ( Dxmin < xmin || Dxmax > xmax || Dymin < ymin || Dymax > ymax )
 
360
    {
 
361
        plabort( "plimage: Dxmin or Dxmax or Dymin or Dymax not compatible with xmin or xmax or ymin or ymax." );
 
362
        return;
 
363
    }
 
364
 
 
365
    if ( Dxmax < Dxmin || xmax < xmin || Dymax < Dymin || ymax < ymin )
 
366
    {
 
367
        plabort( "plimage: All (Dxmin < Dxmax) and (Dymin < Dymax) and (xmin < xmax) and (ymin < ymax) must hold." );
 
368
        return;
 
369
    }
 
370
 
 
371
    // Find the minimum and maximum values in the image.  Use these values to
 
372
    // for the color scale range.
 
373
    idataops->minmax( idatap, nx, ny, &data_min, &data_max );
 
374
 
 
375
    if ( xmin == Dxmin && xmax == Dxmax && ymin == Dymin && ymax == Dymax )
 
376
    {
 
377
        // If the whole image should be shown, then no copying is needed.
 
378
        z    = (PLFLT **) idatap;
 
379
        zops = idataops;
 
380
        nnx  = nx;
 
381
        nny  = ny;
 
382
    }
 
383
    else
 
384
    {
 
385
        // dx and dy are the plot-coordinates pixel sizes for an untransformed
 
386
        // image
 
387
        dx = ( xmax - xmin ) / (PLFLT) nx;
 
388
        dy = ( ymax - ymin ) / (PLFLT) ny;
 
389
 
 
390
        // Pixel dimensions of the (Dxmin, Dymin) to (Dxmax, Dymax) box
 
391
        nnx = (PLINT) ceil( ( Dxmax - Dxmin ) / dx );
 
392
        nny = (PLINT) ceil( ( Dymax - Dymin ) / dy );
 
393
 
 
394
        // Call plimagefr with the value -> color range mapped to the minimum
 
395
        // Offsets for the idata indices to select
 
396
        // (Dxmin, Dymin) to (Dxmax, Dymax)
 
397
        xm = (PLINT) floor( ( Dxmin - xmin ) / dx );
 
398
        ym = (PLINT) floor( ( Dymin - ymin ) / dy );
 
399
 
 
400
        // Allocate space for the sub-image
 
401
        plAlloc2dGrid( &z, nnx, nny );
 
402
        zops = plf2ops_c();
 
403
 
 
404
        // Go through the image and select the pixels within the given
 
405
        // (Dxmin, Dymin) - (Dxmax, Dymax) window.
 
406
        ixx = -1;
 
407
        for ( ix = xm; ix < xm + nnx; ix++ )
 
408
        {
 
409
            ixx++; iyy = 0;
 
410
            for ( iy = ym; iy < ym + nny; iy++ )
 
411
            {
 
412
                z[ixx][iyy++] = idataops->get( idatap, ix, iy );
 
413
            }
 
414
        }
 
415
 
 
416
        // Set the appropriate values to pass in to plimagefr
 
417
        copied = TRUE;
 
418
    }
 
419
 
 
420
    plfimagefr( zops, (PLPointer) z, nnx, nny, Dxmin, Dxmax, Dymin, Dymax, zmin, zmax,
 
421
        data_min, data_max, NULL, NULL );
 
422
 
 
423
    // Only free the memory if it was allocated by us...
 
424
    if ( copied == TRUE )
 
425
    {
 
426
        plFree2dGrid( z, nnx, nny );
 
427
    }
 
428
}