source: trunk/SRC/Picture/imdisp.pro @ 232

Last change on this file since 232 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 27.5 KB
Line 
1;+
2; @hidden
3;-
4;
5FUNCTION imdisp_getpos, ASPECT, POSITION=POSITION, MARGIN=MARGIN
6;
7  compile_opt idl2, strictarrsubs
8;
9
10;- Compute a position vector given an aspect ratio (called by IMDISP_IMSIZE)
11
12;- Check arguments
13if (n_params() ne 1) then message, 'Usage: RESULT = IMDISP_GETPOS(ASPECT)'
14if (n_elements(aspect) eq 0) then message, 'ASPECT is undefined'
15
16;- Check keywords
17if (n_elements(position) eq 0) then position = [0.0, 0.0, 1.0, 1.0]
18if (n_elements(margin) eq 0) then margin = 0.1
19
20;- Get range limited aspect ratio and margin input values
21aspect_val = (float(aspect[0]) > 0.01) < 100.0
22margin_val = (float(margin[0]) > 0.0) < 0.495
23
24;- Compute aspect ratio of position vector in this window
25xsize = (position[2] - position[0]) * !d.x_vsize
26ysize = (position[3] - position[1]) * !d.y_vsize
27cur_aspect = ysize / xsize
28
29;- Compute aspect ratio of this window
30win_aspect = float(!d.y_vsize) / float(!d.x_vsize)
31
32;- Compute height and width in normalized units
33if (aspect_val ge cur_aspect) then begin
34  height = (position[3] - position[1]) - 2.0 * margin
35  width  = height * (win_aspect / aspect_val)
36endif else begin
37  width  = (position[2] - position[0]) - 2.0 * margin
38  height = width * (aspect_val / win_aspect)
39endelse
40
41;- Compute and return position vector
42xcenter = 0.5 * (position[0] + position[2])
43ycenter = 0.5 * (position[1] + position[3])
44x0 = xcenter - 0.5 * width
45y0 = ycenter - 0.5 * height
46x1 = xcenter + 0.5 * width
47y1 = ycenter + 0.5 * height
48return, [x0, y0, x1, y1]
49
50END
51;+
52; @hidden
53;-
54;
55FUNCTION imdisp_imscale, IMAGE, RANGE=RANGE, BOTTOM=BOTTOM, NCOLORS=NCOLORS, $
56  NEGATIVE=NEGATIVE
57;
58  compile_opt idl2, strictarrsubs
59;
60
61;- Byte-scale an image (called by IMDISP)
62
63;- Check arguments
64if (n_params() ne 1) then message, 'Usage: RESULT = IMDISP_IMSCALE(IMAGE)'
65if (n_elements(image) eq 0) then message, 'Argument IMAGE is undefined'
66
67;- Check keywords
68if (n_elements(range) eq 0) then begin
69  min_value = min(image, max=max_value)
70  range = [min_value, max_value]
71endif
72if (n_elements(bottom) eq 0) then bottom = 0B
73if (n_elements(ncolors) eq 0) then ncolors = !d.table_size - bottom
74
75;- Compute the scaled image
76scaled = bytscl(image, min=range[0], max=range[1], top=(ncolors - 1))
77
78;- Create a negative image if required
79if keyword_set(negative) then scaled = byte(ncolors - 1) - scaled
80
81;- Return the scaled image in the correct color range
82return, scaled + byte(bottom)
83
84END
85;
86;+
87; @hidden
88;-
89;
90FUNCTION imdisp_imregrid, DATA, NX, NY, INTERP=INTERP
91;
92  compile_opt idl2, strictarrsubs
93;
94
95;- Regrid a 2D array (called by IMDISP)
96
97;- Check arguments
98if (n_params() ne 3) then $
99  message, 'Usage: RESULT = IMDISP_IMREGRID(DATA, NX, NY)'
100if (n_elements(data) eq 0) then message, 'Argument DATA is undefined'
101result = size(data)
102ndims = result[0]
103dims = result[1:ndims]
104if (ndims ne 2) then message, 'Argument DATA must have 2 dimensions'
105if (n_elements(nx) eq 0) then message, 'Argument NX is undefined'
106if (n_elements(ny) eq 0) then message, 'Argument NY is undefined'
107if (nx lt 1) then message, 'NX must be 1 or greater'
108if (ny lt 1) then message, 'NY must be 1 or greater'
109
110;- Copy the array if the requested size is the same as the current size
111if (nx eq dims[0]) and (ny eq dims[1]) then begin
112  new = data
113  return, new
114endif
115
116;- Compute index arrays for bilinear interpolation
117xindex = (findgen(nx) + 0.5) * (dims[0] / float(nx)) - 0.5
118yindex = (findgen(ny) + 0.5) * (dims[1] / float(ny)) - 0.5
119
120;- Round the index arrays if nearest neighbor sampling is required
121if (keyword_set(interp) eq 0) then begin
122  xindex = round(xindex)
123  yindex = round(yindex)
124endif
125
126;- Return regridded array
127return, interpolate(data, xindex, yindex, /grid)
128
129END
130;
131;+
132;
133; @hidden
134;
135;-
136;
137PRO imdisp_imsize, IMAGE, X0, Y0, XSIZE, YSIZE, ASPECT=ASPECT, $
138  POSITION=POSITION, MARGIN=MARGIN
139;
140  compile_opt idl2, strictarrsubs
141;
142
143;- Compute the size and offset for an image (called by IMDISP)
144
145;- Check arguments
146if (n_params() ne 5) then $
147  message, 'Usage: IMDISP_IMSIZE, IMAGE, X0, Y0, XSIZE, YSIZE'
148if (n_elements(image) eq 0) then $
149  message, 'Argument IMAGE is undefined'
150if (n_elements(position) eq 0) then position = [0.0, 0.0, 1.0, 1.0]
151if (n_elements(position) ne 4) then $
152  message, 'POSITION must be a 4 element vector'
153if (n_elements(margin) eq 0) then margin = 0.1
154if (n_elements(margin) ne 1) then $
155  message, 'MARGIN must be a scalar'
156
157;- Get image dimensions
158result = size(image)
159ndims = result[0]
160if (ndims ne 2) then message, 'IMAGE must be a 2D array'
161dims = result[1 : ndims]
162
163;- Get aspect ratio for image
164if (n_elements(aspect) eq 0) then $
165  aspect = float(dims[1]) / float(dims[0])
166if (n_elements(aspect) ne 1) then $
167  message, 'ASPECT must be a scalar'
168
169;- Check output parameters
170if (arg_present(x0) ne 1) then message, 'Argument XO cannot be set'
171if (arg_present(y0) ne 1) then message, 'Argument YO cannot be set'
172if (arg_present(xsize) ne 1) then message, 'Argument XSIZE cannot be set'
173if (arg_present(ysize) ne 1) then message, 'Argument YSIZE cannot be set'
174
175;- Get approximate image position
176position = imdisp_getpos(aspect, position=position, margin=margin)
177
178;- Compute lower left position of image (device units)
179x0 = round(position[0] * !d.x_vsize) > 0L
180y0 = round(position[1] * !d.y_vsize) > 0L
181
182;- Compute size of image (device units)
183xsize = round((position[2] - position[0]) * !d.x_vsize) > 2L
184ysize = round((position[3] - position[1]) * !d.y_vsize) > 2L
185
186;- Recompute the image position based on actual image size
187position = fltarr(4)
188position[0] = x0 / float(!d.x_vsize)
189position[1] = y0 / float(!d.y_vsize)
190position[2] = (x0 + xsize) / float(!d.x_vsize)
191position[3] = (y0 + ysize) / float(!d.y_vsize)
192
193END
194;
195;+
196;
197; @file_comments
198;    Display an image on the current graphics device.
199;    IMDISP is an advanced replacement for TV and TVSCL.
200;
201;    - Supports WIN, MAC, X, CGM, PCL, PRINTER, PS, and Z graphics devices,
202;    - Image is automatically byte-scaled (can be disabled),
203;    - Custom byte-scaling of Pseudo color images via the RANGE keyword,
204;    - Pseudo (indexed) color and True color images are handled automatically,
205;    - 8-bit and 24-bit graphics devices  are handled automatically,
206;    - Decomposed color settings are handled automatically,
207;    - Image is automatically sized to fit the display (can be disabled),
208;    - The !P.MULTI system variable is honored for multiple image display,
209;    - Image can be positioned via the POSITION keyword,
210;    - Color table splitting via the BOTTOM and NCOLORS keywords,
211;    - Image aspect ratio customization via the ASPECT keyword,
212;    - Resized images can be resampled (default) or interpolated,
213;    - Top down image display via the ORDER keyword (!ORDER is ignored),
214;    - Selectable display channel (R/G/B) via the CHANNEL keyword,
215;    - Background can be set to a specified color via the BACKGROUND keyword,
216;    - Screen can be erased prior to image display via the ERASE keyword,
217;    - Plot axes can be drawn on the image via the AXIS keyword,
218;    - Photographic negative images can be displayed via the NEGATIVE keyword.
219;
220; @categories
221; Picture
222;
223; @param IMAGE {in}{required}
224; Array containing image data.
225; Pseudo (indexed) color images must have 2 dimensions.
226; True color images must have 3 dimensions, in either
227; [3, NX, NY], [NX, 3, NY], or [NX, NY, 3] form.
228;
229; @keyword RANGE {type=vector}{default=min and max array values}
230; For Pseudo Color images only, a vector with two elements
231; specifying the minimum and maximum values of the image
232; array to be considered when the image is byte-scaled
233; This keyword is ignored for True Color images,
234; or if the NOSCALE keyword is set.
235;
236; @keyword BOTTOM {default=0}
237; Bottom value in the color table to be used
238; for the byte-scaled image.
239; This keyword is ignored if the NOSCALE keyword is set.
240;
241; @keyword NCOLORS {default=!D.TABLE_SIZE - BOTTOM}
242; Number of colors in the color table to be used
243; for the byte-scaled image
244; This keyword is ignored if the NOSCALE keyword is set.
245;
246; @keyword MARGIN {default=0.1 or 0.025 if !P.MULTI is set to display multiple images}
247; A scalar value specifying the margin to be maintained
248; around the image in normal coordinates
249;
250; @keyword INTERP {default=nearest neighbor sampling}
251; If set, the resized image will be interpolated using
252; bilinear interpolation
253;
254; @keyword DITHER {default=no dithering}
255; If set, true color images will be dithered when displayed
256; on an 8-bit graphics device
257;
258; @keyword ASPECT {default=maintain native aspect ratio}
259; A scalar value specifying the aspect ratio (height/width)
260; for the displayed image
261;
262; @keyword POSITION {default= [0.0,0.0,1.0,1.0]}
263; On input, a 4-element vector specifying the position
264; of the displayed image in the form [X0,Y0,X1,Y1] in
265; in normal coordinates
266; See the examples below to display an image where only the
267; offset and size are known (e.g. MAP_IMAGE output).
268;
269; @keyword OUT_POS
270; On output, a 4-element vector specifying the position
271; actually used to display the image.
272;
273; @keyword NOSCALE {default=to byte-scale the image}
274; If set, the image will not be byte-scaled.
275;
276; @keyword NORESIZE {default=To resize the image to fit the display}
277; If set, the image will not be resized.
278;
279; @keyword ORDER {default=To display the image from the bottom up}
280; If set, the image is displayed from the top down
281; Note that the system variable !ORDER is always ignored.
282;
283; @keyword USEPOS {default=To honor ASPECT and MARGIN when POSITION vector is supplied}
284; If set, the image will be sized to exactly fit a supplied
285; POSITION vector, over-riding ASPECT and MARGIN.
286;
287; @keyword CHANNEL
288; Display channel (Red, Green, or Blue) to be written.
289; 0 => All channels (the default)
290; 1 => Red channel
291; 2 => Green channel
292; 3 => Blue channel
293; This keyword is only recognized by graphics devices which
294; support 24-bit decomposed color (WIN, MAC, X). It is ignored
295; by all other graphics devices. However True color (RGB)
296; images can be displayed on any device supported by IMDISP.
297;
298; @keyword BACKGROUND
299; If set to a positive integer, the background will be filled
300; with the color defined by BACKGROUND.
301;
302; @keyword ERASE
303; If set, the screen contents will be erased. Note that if
304; !P.MULTI is set to display multiple images, the screen is
305; always erased when the first image is displayed.
306;
307; @keyword AXIS
308; If set, plot axes will be drawn on the image. The default
309; x and y axis ranges are determined by the size of the image.
310; When the AXIS keyword is set, IMDISP accepts any keywords
311; supported by PLOT (e.g. TITLE, COLOR, CHARSIZE etc.).
312;
313; @keyword NEGATIVE
314; If set, a photographic negative of the image is displayed.
315; The values of BOTTOM and NCOLORS are honored. This keyword
316; allows True color images scanned from color negatives to be
317; displayed. It also allows Pseudo color images to be displayed
318; as negatives without reversing the color table. This keyword
319; is ignored if the NOSCALE keyword is set.
320;
321; @restrictions
322; The image is displayed on the current graphics device.
323;
324; @restrictions
325; Requires IDL 5.0 or higher (square bracket array syntax).
326;
327; @examples
328;
329;;- Load test data
330;
331; openr, lun, filepath('ctscan.dat', subdir='examples/data'), /get_lun
332;ctscan = bytarr(256, 256)
333;readu, lun, ctscan
334;free_lun, lun
335;openr, lun, filepath('hurric.dat', subdir='examples/data'), /get_lun
336;hurric = bytarr(440, 330)
337;readu, lun, hurric
338;free_lun, lun
339;read_jpeg, filepath('rose.jpg', subdir='examples/data'), rose
340;help, ctscan, hurric, rose
341;
342;;- Display single images
343;
344;!p.multi = 0
345;loadct, 0
346;imdisp, hurric, /erase
347;wait, 3.0
348;imdisp, rose, /interp, /erase
349;wait, 3.0
350;
351;;- Display multiple images without color table splitting
352;;- (works on 24-bit displays only; top 2 images are garbled on 8-bit displays)
353;
354;!p.multi = [0, 1, 3, 0, 0]
355;loadct, 0
356;imdisp, ctscan, margin=0.02
357;loadct, 13
358;imdisp, hurric, margin=0.02
359;imdisp, rose, margin=0.02
360;wait, 3.0
361;
362;;- Display multiple images with color table splitting
363;;- (works on 8-bit or 24-bit displays)
364;
365;!p.multi = [0, 1, 3, 0, 0]
366;loadct, 0, ncolors=64, bottom=0
367;imdisp, ctscan, margin=0.02, ncolors=64, bottom=0
368;loadct, 13, ncolors=64, bottom=64
369;imdisp, hurric, margin=0.02, ncolors=64, bottom=64
370;imdisp, rose, margin=0.02, ncolors=64, bottom=128
371;wait, 3.0
372;
373;;- Display an image at a specific position, over-riding aspect and margin
374;
375;!p.multi = 0
376;loadct, 0
377;imdisp, hurric, position=[0.0, 0.0, 1.0, 0.5], /usepos, /erase
378;wait, 3.0
379;
380;;- Display an image with axis overlay
381;
382;!p.multi = 0
383;loadct, 0
384;imdisp, rose, /axis, /erase
385;wait, 3.0
386;
387;;- Display an image with contour plot overlay
388;
389;!p.multi = 0
390;loadct, 0
391;imdisp, hurric, out_pos=out_pos, /erase
392;contour, smooth(hurric, 10, /edge), /noerase, position=out_pos, $
393;  xstyle=1, ystyle=1, levels=findgen(5)*40.0, /follow
394;wait, 3.0
395;
396;;- Display a small image with correct resizing
397;
398;!p.multi = 0
399;loadct, 0
400;data = (dist(8))[1:7, 1:7]
401;imdisp, data, /erase
402;wait, 3.0
403;imdisp, data, /interp
404;wait, 3.0
405;
406;;- Display a true color image without and with interpolation
407;
408;!p.multi = 0
409;imdisp, rose, /erase
410;wait, 3.0
411;imdisp, rose, /interp
412;wait, 3.0
413;
414;;- Display a true color image as a photographic negative
415;
416;imdisp, rose, /negative, /erase
417;wait, 3.0
418;
419;;- Display a true color image on PostScript output
420;;- (note that color table is handled automatically)
421;
422;current_device = !d.name
423;set_plot, 'PS'
424;device, /color, bits_per_pixel=8, filename='imdisp_true.ps'
425;imdisp, rose, /axis, title='PostScript True Color Output'
426;device, /close
427;set_plot, current_device
428;
429;;- Display a pseudo color image on PostScript output
430;
431;current_device = !d.name
432;set_plot, 'PS'
433;device, /color, bits_per_pixel=8, filename='imdisp_pseudo.ps'
434;loadct, 0
435;imdisp, hurric, /axis, title='PostScript Pseudo Color Output'
436;device, /close
437;set_plot, current_device
438;
439;;- Display an image where only the offset and size are known
440;
441;;- Read world elevation data
442;file = filepath('worldelv.dat', subdir='examples/data')
443;openr, lun, file, /get_lun
444;data = bytarr(360, 360)
445;readu, lun, data
446;free_lun, lun
447;;- Reorganize array so it spans 180W to 180E
448;world = data
449;world[0:179, *] = data[180:*, *]
450;world[180:*, *] = data[0:179, *]
451;;- Create remapped image
452;map_set, /orthographic, /isotropic, /noborder
453;remap = map_image(world, x0, y0, xsize, ysize, compress=1)
454;;- Convert offset and size to position vector
455;pos = fltarr(4)
456;pos[0] = x0 / float(!d.x_vsize)
457;pos[1] = y0 / float(!d.y_vsize)
458;pos[2] = (x0 + xsize) / float(!d.x_vsize)
459;pos[3] = (y0 + ysize) / float(!d.y_vsize)
460;;- Display the image
461;loadct, 0
462;imdisp, remap, pos=pos, /usepos
463;map_continents
464;map_grid
465;
466; @history
467; Liam.Gumley\@ssec.wisc.edu
468; <a href="http://cimss.ssec.wisc.edu/~gumley"/>
469;
470; Copyright (C) 1999, 2000 Liam E. Gumley
471;
472; This program is free software; you can redistribute it and/or
473; modify it under the terms of the GNU General Public License
474; as published by the Free Software Foundation; either version 2
475; of the License, or (at your option) any later version.
476;
477; This program is distributed in the hope that it will be useful,
478; but WITHOUT ANY WARRANTY; without even the implied warranty of
479; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
480; GNU General Public License for more details.
481;
482; You should have received a copy of the GNU General Public License
483; along with this program; if not, write to the Free Software
484; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
485;
486; @version
487; $Id$
488;
489;-
490;
491PRO imdisp, IMAGE, RANGE=RANGE, BOTTOM=BOTTOM, NCOLORS=NCOLORS, $
492  MARGIN=MARGIN, INTERP=INTERP, DITHER=DITHER, ASPECT=ASPECT, $
493  POSITION=POSITION, OUT_POS=OUT_POS, NOSCALE=NOSCALE, NORESIZE=NORESIZE, $
494  ORDER=ORDER, USEPOS=USEPOS, CHANNEL=CHANNEL, $
495  BACKGROUND=BACKGROUND, ERASE=ERASE, $
496  AXIS=AXIS, NEGATIVE=NEGATIVE, _EXTRA=EXTRA_KEYWORDS
497;
498  compile_opt idl2, strictarrsubs
499;
500
501rcs_id = '$Id$'
502
503;-------------------------------------------------------------------------------
504;- CHECK INPUT
505;-------------------------------------------------------------------------------
506
507;- Check arguments
508if (n_params() ne 1) then message, 'Usage: IMDISP, IMAGE'
509if (n_elements(image) eq 0) then message, 'Argument IMAGE is undefined'
510if (max(!p.multi) eq 0) then begin
511  if (n_elements(margin) eq 0) then begin
512    if (n_elements(position) eq 4) then margin = 0.0 else margin = 0.1
513  endif
514endif else begin
515  if (n_elements(margin) eq 0) then margin = 0.025
516endelse
517if (n_elements(order) eq 0) then order = 0
518if (n_elements(channel) eq 0) then channel = 0
519
520;- Check position vector
521if (n_elements(position) gt 0) then begin
522  if (n_elements(position) ne 4) then $
523    message, 'POSITION must be a 4 element vector of the form [X0, Y0, X1, Y1]'
524  if (position[0] lt 0.0) then message, 'POSITION[0] must be GE 0.0'
525  if (position[1] lt 0.0) then message, 'POSITION[1] must be GE 0.0'
526  if (position[2] gt 1.0) then message, 'POSITION[2] must be LE 1.0'
527  if (position[3] gt 1.0) then message, 'POSITION[3] must be LE 1.0'
528  if (position[0] ge position[2]) then $
529    message, 'POSITION[0] must be LT POSITION[2]'
530  if (position[1] ge position[3]) then $
531    message, 'POSITION[1] must be LT POSITION[3]'
532endif
533
534;- Check the image dimensions
535result = size(image)
536ndims = result[0]
537if (ndims lt 2) or (ndims gt 3) then $
538  message, 'IMAGE must be a Pseudo Color (2D) or True Color (3D) image array'
539dims = result[1:ndims]
540
541;- Check that 3D image array is in valid true color format
542true = 0
543if (ndims eq 3) then begin
544  index = where(dims eq 3L, count)
545  if (count eq 0) then $
546    message, 'True Color dimensions must be [3,NX,NY], [NX,3,NY], or [NX,NY,3]'
547  true = 1
548  truedim = index[0]
549endif
550
551;- Check scaling range for pseudo color images
552if (true eq 0) then begin
553  if (n_elements(range) eq 0) then begin
554    min_value = min(image, max=max_value)
555    range = [min_value, max_value]
556  endif
557  if (n_elements(range) ne 2) then $
558    message, 'RANGE keyword must be a 2-element vector'
559endif else begin
560  if (n_elements(range) gt 0) then $
561    message, 'RANGE keyword is not used for True Color images', /continue
562endelse
563
564;- Check for supported graphics devices
565names = ['WIN', 'MAC', 'X', 'CGM', 'PCL', 'PRINTER', 'PS', 'Z']
566result = where((!d.name eq names), count)
567if (count eq 0) then message, 'Graphics device is not supported'
568
569;- Get color table information
570if ((!d.flags and 256) ne 0) and (!d.window lt 0) then begin
571  window, /free, /pixmap
572  wdelete, !d.window
573endif
574if (n_elements(bottom) eq 0) then bottom = 0
575if (n_elements(ncolors) eq 0) then ncolors = !d.table_size - bottom
576
577;- Get IDL version number
578version = float(!version.release)
579
580;- Check for IDL 5.2 or higher if printer device is selected
581if (version lt 5.2) and (!d.name eq 'PRINTER') then $
582  message, 'IDL 5.2 or higher is required for PRINTER device support'
583
584;-------------------------------------------------------------------------------
585;- GET RED, GREEN, AND BLUE COMPONENTS OF TRUE COLOR IMAGE
586;-------------------------------------------------------------------------------
587
588if (true eq 1) then begin
589    case truedim of
590      0 : begin
591            red = image[0, *, *]
592            grn = image[1, *, *]
593            blu = image[2, *, *]
594      end
595      1 : begin
596            red = image[*, 0, *]
597            grn = image[*, 1, *]
598            blu = image[*, 2, *]
599      end
600      2 : begin
601            red = image[*, *, 0]
602            grn = image[*, *, 1]
603            blu = image[*, *, 2]
604      end
605  endcase
606  red = reform(red, /overwrite)
607  grn = reform(grn, /overwrite)
608  blu = reform(blu, /overwrite)
609endif
610
611;-------------------------------------------------------------------------------
612;- COMPUTE POSITION FOR IMAGE
613;-------------------------------------------------------------------------------
614
615;- Save first element of !p.multi
616multi_first = !p.multi[0]
617
618;- Establish image position if not defined
619if (n_elements(position) eq 0) then begin
620  if (max(!p.multi) eq 0) then begin
621    position = [0.0, 0.0, 1.0, 1.0]
622  endif else begin
623    plot, [0], /nodata, xstyle=4, ystyle=4, xmargin=[0, 0], ymargin=[0, 0]
624    position = [!x.window[0], !y.window[0], !x.window[1], !y.window[1]]
625  endelse
626endif
627
628;- Erase and fill the background if required
629if (multi_first eq 0) then begin
630  if keyword_set(erase) then erase
631  if (n_elements(background) gt 0) then begin
632    polyfill, [-0.01,  1.01,  1.01, -0.01, -0.01], $
633      [-0.01, -0.01,  1.01,  1.01, -0.01], /normal, color=background[0]
634  endif
635endif
636
637;- Compute image aspect ratio if not defined
638if (n_elements(aspect) eq 0) then begin
639  case true of
640    0 : result = size(image)
641    1 : result = size(red)
642  endcase
643  dims = result[1:2]
644  aspect = float(dims[1]) / float(dims[0])
645endif
646
647;- Save image xrange and yrange for axis overlays
648xrange = [0, dims[0]]
649yrange = [0, dims[1]]
650if (order eq 1) then yrange = reverse(yrange)
651
652;- Set the aspect ratio and margin to fill the position window if requested
653if keyword_set(usepos) then begin
654  xpos_size = float(!d.x_vsize) * (position[2] - position[0])
655  ypos_size = float(!d.y_vsize) * (position[3] - position[1])
656  aspect_value = ypos_size / xpos_size
657  margin_value = 0.0
658endif else begin
659  aspect_value = aspect
660  margin_value = margin
661endelse
662
663;- Compute size of displayed image and save output position
664pos = position
665case true of
666  0 : imdisp_imsize, image, x0, y0, xsize, ysize, position=pos, $
667        aspect=aspect_value, margin=margin_value
668  1 : imdisp_imsize,   red, x0, y0, xsize, ysize, position=pos, $
669        aspect=aspect_value, margin=margin_value
670endcase
671out_pos = pos
672
673;-------------------------------------------------------------------------------
674;- BYTE-SCALE THE IMAGE IF REQUIRED
675;-------------------------------------------------------------------------------
676
677;- Choose whether to scale the image or not
678if (keyword_set(noscale) eq 0) then begin
679
680  ;- Scale the image
681  case true of
682    0 : scaled = imdisp_imscale(image, bottom=bottom, ncolors=ncolors, $
683          range=range, negative=keyword_set(negative))
684    1 : begin
685          scaled_dims = (size(red))[1:2]
686          scaled = bytarr(scaled_dims[0], scaled_dims[1], 3)
687          scaled[0, 0, 0] = imdisp_imscale(red, bottom=0, ncolors=256, $
688            negative=keyword_set(negative))
689          scaled[0, 0, 1] = imdisp_imscale(grn, bottom=0, ncolors=256, $
690            negative=keyword_set(negative))
691          scaled[0, 0, 2] = imdisp_imscale(blu, bottom=0, ncolors=256, $
692            negative=keyword_set(negative))
693        end
694  endcase
695
696endif else begin
697
698  ;- Don't scale the image
699  case true of
700    0 : scaled = image
701    1 : begin
702          scaled_dims = (size(red))[1:2]
703          scaled = replicate(red[0], scaled_dims[0], scaled_dims[1], 3)
704          scaled[0, 0, 0] = red
705          scaled[0, 0, 1] = grn
706          scaled[0, 0, 2] = blu
707        end
708  endcase
709
710endelse
711
712;-------------------------------------------------------------------------------
713;- DISPLAY IMAGE ON PRINTER DEVICE
714;-------------------------------------------------------------------------------
715
716if (!d.name eq 'PRINTER') then begin
717
718  ;- Display the image
719  case true of
720    0 : begin
721          device, /index_color
722          tv, scaled, x0, y0, xsize=xsize, ysize=ysize, order=order
723        end
724    1 : begin
725          device, /true_color
726          tv, scaled, x0, y0, xsize=xsize, ysize=ysize, order=order, true=3
727        end
728  endcase
729
730  ;- Draw axes if required
731  if keyword_set(axis) then $
732    plot, [0], /nodata, /noerase, position=out_pos, $
733      xrange=xrange, xstyle=1, yrange=yrange, ystyle=1, $
734      _extra=extra_keywords
735
736  ;- Return to caller
737  return
738
739endif
740
741;-------------------------------------------------------------------------------
742;- DISPLAY IMAGE ON GRAPHICS DEVICES WHICH HAVE SCALEABLE PIXELS
743;-------------------------------------------------------------------------------
744
745if ((!d.flags and 1) ne 0) then begin
746
747  ;- Display the image
748  case true of
749    0 : tv, scaled, x0, y0, xsize=xsize, ysize=ysize, order=order
750    1 : begin
751          tvlct, r, g, b, /get
752          loadct, 0, /silent
753          tv, scaled, x0, y0, xsize=xsize, ysize=ysize, order=order, true=3
754          tvlct, r, g, b
755        end
756  endcase
757
758  ;- Draw axes if required
759  if keyword_set(axis) then $
760    plot, [0], /nodata, /noerase, position=out_pos, $
761      xrange=xrange, xstyle=1, yrange=yrange, ystyle=1, $
762      _extra=extra_keywords
763
764  ;- Return to caller
765  return
766
767endif
768
769;-------------------------------------------------------------------------------
770;- RESIZE THE IMAGE
771;-------------------------------------------------------------------------------
772
773;- Resize the image
774if (keyword_set(noresize) eq 0) then begin
775  if (true eq 0) then begin
776    resized = imdisp_imregrid(scaled, xsize, ysize, interp=keyword_set(interp))
777  endif else begin
778    resized = replicate(scaled[0], xsize, ysize, 3)
779    resized[0, 0, 0] = imdisp_imregrid(reform(scaled[*, *, 0]), xsize, ysize, $
780      interp=keyword_set(interp))
781    resized[0, 0, 1] = imdisp_imregrid(reform(scaled[*, *, 1]), xsize, ysize, $
782      interp=keyword_set(interp))
783    resized[0, 0, 2] = imdisp_imregrid(reform(scaled[*, *, 2]), xsize, ysize, $
784      interp=keyword_set(interp))
785  endelse
786endif else begin
787  resized = temporary(scaled)
788  x0 = 0
789  y0 = 0
790endelse
791
792;-------------------------------------------------------------------------------
793;- GET BIT DEPTH FOR THIS DISPLAY
794;-------------------------------------------------------------------------------
795
796;- If this device supports windows, make sure a window has been opened
797if (!d.flags and 256) ne 0 then begin
798  if (!d.window lt 0) then begin
799    window, /free, /pixmap
800    wdelete, !d.window
801  endif
802endif
803
804;- Set default display depth
805depth = 8
806
807;- Get actual bit depth on supported displays
808if (!d.name eq 'WIN') or (!d.name eq 'MAC') or (!d.name eq 'X') then begin
809  if (version ge 5.1) then begin
810    device, get_visual_depth=depth
811  endif else begin
812    if (!d.n_colors gt 256) then depth = 24
813  endelse
814endif
815
816;-------------------------------------------------------------------------------
817;- SELECT DECOMPOSED COLOR MODE (ON OR OFF) FOR 24-BIT DISPLAYS
818;-------------------------------------------------------------------------------
819
820if (!d.name eq 'WIN') or (!d.name eq 'MAC') or (!d.name eq 'X') then begin
821  if (depth gt 8) then begin
822    if (version ge 5.2) then device, get_decomposed=entry_decomposed else $
823      entry_decomposed = 0
824    if (true eq 1) or (channel gt 0) then device, decomposed=1 else $
825      device, decomposed=0
826  endif
827endif
828
829;-------------------------------------------------------------------------------
830;- DISPLAY THE IMAGE
831;-------------------------------------------------------------------------------
832
833;- If the display is 8-bit and the image is true color,
834;- convert image from true color to indexed color
835if (depth le 8) and (true eq 1) then begin
836  resized = color_quan(temporary(resized), 3, r, g, b, $
837    colors=ncolors, dither=keyword_set(dither)) + byte(bottom)
838  tvlct, r, g, b, bottom
839  true = 0
840endif
841
842;- Set channel value for supported devices
843if (!d.name eq 'WIN') or (!d.name eq 'MAC') or (!d.name eq 'X') then begin
844  channel_value = channel
845endif else begin
846  channel_value = 0
847endelse
848
849;- Display the image
850case true of
851  0 : tv, resized, x0, y0, order=order, channel=channel_value
852  1 : tv, resized, x0, y0, order=order, true=3
853endcase
854
855;-------------------------------------------------------------------------------
856;- RESTORE THE DECOMPOSED COLOR MODE FOR 24-BIT DISPLAYS
857;-------------------------------------------------------------------------------
858
859if ((!d.name eq 'WIN') or (!d.name eq 'MAC') or (!d.name eq 'X')) and $
860  (depth gt 8) then begin
861  device, decomposed=entry_decomposed
862  if (!d.name eq 'MAC') then tv, [0], -1, -1
863endif
864
865;-------------------------------------------------------------------------------
866;- DRAW AXES IF REQUIRED
867;-------------------------------------------------------------------------------
868
869if keyword_set(axis) then $
870  plot, [0], /nodata, /noerase, position=out_pos, $
871    xrange=xrange, xstyle=1, yrange=yrange, ystyle=1, $
872    _extra=extra_keywords
873
874END
Note: See TracBrowser for help on using the repository browser.