3
// Author: Alessandro Mirone, Nov 2001
4
// Adapted: Joao Cardoso
5
// Updated: Hezekiah Carty 2008
7
// Copyright (C) 2004 Alan W. Irwin
9
// This file is part of PLplot.
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.
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.
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
30
#define COLOR_NO_PLOT ( -1.0 )
32
// Get better names, those are too cryptic!
34
// ZEROW2B: zero writing to buffer ?
35
// ZEROW2D: zero writing to display ?
36
// ONEW2B: set writing to buffer ?
37
// ONEW2D: set writing to display ?
45
plsc->plbuf_write = 0; // TODO: store previous state
46
plP_esc( PLESC_EXPOSE, NULL );
47
plP_esc( PLESC_IMAGEOPS, &op );
51
RestoreWrite2BufferPixmap()
55
plsc->plbuf_write = 1; // TODO: revert from previous state
56
plP_esc( PLESC_IMAGEOPS, &op );
64
plP_esc( PLESC_IMAGEOPS, &op );
72
plP_esc( PLESC_IMAGEOPS, &op );
73
plP_esc( PLESC_EXPOSE, NULL );
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
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.
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 ),
97
// Translated (by pltr) coordinates
99
// The corners of a single filled region
100
// int corners[4]; - unreferenced
101
// The color to use in the fill
104
plP_esc( PLESC_START_RASTERIZE, NULL );
105
for ( ix = 0; ix < nx; ix++ )
107
for ( iy = 0; iy < ny; iy++ )
109
// Only plot values within in appropriate range
110
color = idata[ix * ny + iy];
111
if ( color == COLOR_NO_PLOT )
114
// The color value has to be scaled to 0.0 -> 1.0 plcol1 color values
115
plcol1( color / COLOR_MAX );
118
xf[2] = xf[3] = ix + 1;
120
yf[1] = yf[2] = iy + 1;
124
for ( i = 0; i < 4; i++ )
126
// Translate the points
127
( *pltr )( xf[i], yf[i], &tx, &ty, pltr_data );
134
for ( i = 0; i < 4; i++ )
136
// Automatic translation to the specified plot area
137
xf[i] = xmin + xf[i] * dx;
138
yf[i] = ymin + yf[i] * dy;
144
plP_esc( PLESC_END_RASTERIZE, NULL );
148
grimage( short *x, short *y, unsigned short *z, PLINT nx, PLINT ny )
153
plsc->dev_nptsX = nx;
154
plsc->dev_nptsY = ny;
156
plP_esc( PLESC_IMAGE, NULL );
159
//--------------------------------------------------------------------------
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]
168
// xmin, xmax, ymin, ymax:
169
// data[0][0] corresponds to (xmin,ymin)
170
// data[nx-1][ny-1] to (xmax,ymax)
173
// only data within bounds zmin <= data <= zmax will be
174
// plotted. If zmin == zmax, all data will be ploted.
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
185
// This allows for multiple plots to use the same color scale
186
// with a consistent value -> color mapping, regardless of the
189
//--------------------------------------------------------------------------
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 )
197
plfimagefr( plf2ops_c(), (PLPointer) idata, nx, ny,
198
xmin, xmax, ymin, ymax, zmin, zmax,
199
valuemin, valuemax, pltr, pltr_data );
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 )
211
// z holds scaled image pixel values
213
// This is used when looping through the image array, checking to
214
// make sure the values are within an acceptable range.
216
// Color palette 0 color in use before the plimage* call
219
PLFLT color_min, color_max, color_range;
221
if ( plsc->level < 3 )
223
plabort( "plimagefr: window must be set up first" );
227
if ( nx <= 0 || ny <= 0 )
229
plabort( "plimagefr: nx and ny must be positive" );
233
if ( ( z = (PLFLT *) malloc( ny * nx * sizeof ( PLFLT ) ) ) == NULL )
235
plexit( "plimagefr: Insufficient memory" );
238
// Save the currently-in-use color.
239
init_color = plsc->icol0;
241
// If no acceptable data range is given, then set the min/max data range
242
// to include all of the given data.
245
// Find the minimum and maximum values in the image
246
idataops->minmax( idatap, nx, ny, &zmin, &zmax );
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;
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
260
for ( ix = 0; ix < nx; ix++ )
262
for ( iy = 0; iy < ny; iy++ )
264
if ( valuemin == valuemax )
266
// If valuemin == valuemax, avoid dividing by zero.
267
z[ix * ny + iy] = ( color_max + color_min ) / 2.0;
271
datum = idataops->get( idatap, ix, iy );
272
if ( isnan( datum ) || datum < zmin || datum > zmax )
274
// Set to a guaranteed-not-to-plot value
275
z[ix * ny + iy] = COLOR_NO_PLOT;
279
if ( datum < valuemin )
283
else if ( datum > valuemax )
287
// Set to a value scaled between color_min and color_max.
289
color_min + ( datum - valuemin + COLOR_MIN ) / ( valuemax - valuemin ) * COLOR_MAX * color_range;
295
// dx and dy are the plot-coordinates pixel sizes for an untransformed
297
dx = ( xmax - xmin ) / (PLFLT) nx;
298
dy = ( ymax - ymin ) / (PLFLT) ny;
300
plP_image( z, nx, ny, xmin, ymin, dx, dy, pltr, pltr_data );
302
plcol0( init_color );
307
//--------------------------------------------------------------------------
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]
316
// xmin, xmax, ymin, ymax:
317
// data[0][0] corresponds to (xmin,ymin)
318
// data[nx-1][ny-1] to (xmax,ymax)
321
// only data within bounds zmin <= data <= zmax will be
322
// plotted. If zmin == zmax, all data will be ploted.
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
328
//--------------------------------------------------------------------------
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 )
334
plfimage( plf2ops_c(), (PLPointer) idata, nx, ny,
335
xmin, xmax, ymin, ymax, zmin, zmax,
336
Dxmin, Dxmax, Dymin, Dymax );
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 )
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)
349
// Was any space allocated for z?
353
if ( nx <= 0 || ny <= 0 )
355
plabort( "plimage: nx and ny must be positive" );
359
if ( Dxmin < xmin || Dxmax > xmax || Dymin < ymin || Dymax > ymax )
361
plabort( "plimage: Dxmin or Dxmax or Dymin or Dymax not compatible with xmin or xmax or ymin or ymax." );
365
if ( Dxmax < Dxmin || xmax < xmin || Dymax < Dymin || ymax < ymin )
367
plabort( "plimage: All (Dxmin < Dxmax) and (Dymin < Dymax) and (xmin < xmax) and (ymin < ymax) must hold." );
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 );
375
if ( xmin == Dxmin && xmax == Dxmax && ymin == Dymin && ymax == Dymax )
377
// If the whole image should be shown, then no copying is needed.
378
z = (PLFLT **) idatap;
385
// dx and dy are the plot-coordinates pixel sizes for an untransformed
387
dx = ( xmax - xmin ) / (PLFLT) nx;
388
dy = ( ymax - ymin ) / (PLFLT) ny;
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 );
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 );
400
// Allocate space for the sub-image
401
plAlloc2dGrid( &z, nnx, nny );
404
// Go through the image and select the pixels within the given
405
// (Dxmin, Dymin) - (Dxmax, Dymax) window.
407
for ( ix = xm; ix < xm + nnx; ix++ )
410
for ( iy = ym; iy < ym + nny; iy++ )
412
z[ixx][iyy++] = idataops->get( idatap, ix, iy );
416
// Set the appropriate values to pass in to plimagefr
420
plfimagefr( zops, (PLPointer) z, nnx, nny, Dxmin, Dxmax, Dymin, Dymax, zmin, zmax,
421
data_min, data_max, NULL, NULL );
423
// Only free the memory if it was allocated by us...
424
if ( copied == TRUE )
426
plFree2dGrid( z, nnx, nny );