source: trunk/SRC/ToBeReviewed/IMAGE/showimage.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1PRO SHOWIMAGE, FILE, DITHER=DITHER, CURRENT=CURRENT
2;
3  compile_opt idl2, strictarrsubs
4;
5
6;+
7; NAME:
8;    SHOWIMAGE
9;
10; PURPOSE:
11;    Show the contents of a graphics file in the current window.
12;
13;    The input formats supported are:
14;    GIF   8-bit with color table,
15;    BMP   8-bit with color table or 24-bit true-color,
16;    PICT  8-bit with color table,
17;    TIFF  8-bit with color table or 24-bit true-color,
18;    JPEG 24-bit true color,
19;
20;    Any conversions necessary to translate 8-bit or 24-bit files
21;    to 8-bit or 24-bit images on-screen are done automatically.
22;
23; CATEGORY:
24;    Input/Output.
25;
26; CALLING SEQUENCE:
27;    SHOWIMAGE, FILE
28;
29; INPUTS:
30;    FILE     Name of the output file (format is identified automatically).
31;
32; OPTIONAL INPUTS:
33;    None.
34;
35; KEYWORD PARAMETERS:
36;    DITHER   Set this keyword to dither the input image when displaying
37;             24-bit images on an 8-bit display (default is no dithering).
38;    CURRENT  Set this keyword to display the image in the current window
39;             (default is to create a new window sized to fit the image).
40;
41; OUTPUTS:
42;    None.
43;
44; OPTIONAL OUTPUTS:
45;    None
46;
47; COMMON BLOCKS:
48;    None
49;
50; SIDE EFFECTS:
51;    The color table is modified.
52;
53; RESTRICTIONS:
54;    Requires IDL 5.2 or higher (image QUERY functions).
55;
56; EXAMPLE:
57;
58;showimage, filepath('rose.jpg', subdir='examples/data')
59;
60; MODIFICATION HISTORY:
61; Liam.Gumley@ssec.wisc.edu
62; http://cimss.ssec.wisc.edu/~gumley
63; $Id$
64;
65; Copyright (C) 1999 Liam E. Gumley
66;
67; This program is free software; you can redistribute it and/or
68; modify it under the terms of the GNU General Public License
69; as published by the Free Software Foundation; either version 2
70; of the License, or (at your option) any later version.
71;
72; This program is distributed in the hope that it will be useful,
73; but WITHOUT ANY WARRANTY; without even the implied warranty of
74; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
75; GNU General Public License for more details.
76;
77; You should have received a copy of the GNU General Public License
78; along with this program; if not, write to the Free Software
79; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
80;-
81
82rcs_id = '$Id$'
83
84;-------------------------------------------------------------------------------
85;- CHECK INPUT
86;-------------------------------------------------------------------------------
87
88;- Check IDL version
89
90if float(!version.release) lt 5.2 then begin
91  message, 'IDL 5.2 or higher is required', /continue
92  return
93endif
94
95;- Check input arguments
96
97case 1 of
98  n_params() ne 1           : error = 'Usage: SHOWIMAGE, FILE'
99  n_elements(file) eq 0     : error = 'Argument FILE is undefined'
100  n_elements(file) gt 1     : error = 'Argument FILE must be a scalar string'
101  (findfile(file))[0] eq '' : error = 'Argument FILE was not found'
102  else                      : error = ''
103endcase
104
105if error ne '' then begin
106  message, error, /continue
107  return
108endif
109
110;-------------------------------------------------------------------------------
111;- CHECK THE GRAPHICS DEVICE
112;-------------------------------------------------------------------------------
113
114;- Check for device supporting windows and tvrd()
115
116if ((!d.flags and 256) eq 0) or ((!d.flags and 128) eq 0) then begin
117  error = string(!d.name, format='("Graphics device (",a,") is not supported")')
118  message, error, /continue
119  return
120endif
121
122;- Make sure a window has been opened in this session and get visual depth
123
124if !d.window lt 0 then begin
125  window, /free, /pixmap, xsize=20, ysize=20
126  wdelete, !d.window
127endif
128device, get_visual_depth=depth
129
130;- If 8-bit display is low on colors, print a message
131
132if (depth eq 8) and (!d.table_size) lt 64 then message, $
133  'Display has less than 64 colors; image quality may degrade', /continue
134
135;-------------------------------------------------------------------------------
136;- IDENTIFY FILE AND READ IMAGE
137;-------------------------------------------------------------------------------
138
139;- Identify the file format
140
141result = query_gif(file, info)
142if result eq 0 then result = query_bmp(file, info)
143if result eq 0 then result = query_pict(file, info)
144if result eq 0 then result = query_tiff(file, info)
145if result eq 0 then result = query_jpeg(file, info)
146if result eq 0 then begin
147  message, 'File format not recognized', /continue
148  return
149endif
150
151;- Fix the channel information for GIF images
152
153if info.type eq 'GIF' then info.channels = 1
154
155;- Read the image
156
157case info.type of
158
159  'GIF' : read_gif, file, image, r, g, b
160
161  'BMP' : begin
162    if info.channels eq 1 then begin
163      image = read_bmp(file, r, g, b)
164    endif else begin
165      image = read_bmp(file)
166      image = reverse(temporary(image), 1)
167    endelse
168  end
169
170  'PICT' : read_pict, file, image, r, g, b
171
172  'TIFF' : begin
173    if info.channels eq 1 then begin
174      image = read_tiff(file, r, g, b, order=order)
175      image = reverse(temporary(image), 2)
176    endif else begin
177      image = read_tiff(file, order=order)
178      image = reverse(temporary(image), 3)
179    endelse
180  end
181
182  'JPEG' : read_jpeg, file, image
183
184endcase
185
186;- If an 8-bit image was read, reduce the number of colors
187
188if info.channels eq 1 then begin
189  reduce_colors, image, index
190  r = r[index]
191  g = g[index]
192  b = b[index]
193endif
194
195;- Get image size
196
197dims = size(image, /dimensions)
198if n_elements(dims) eq 2 then begin
199  nx = dims[0]
200  ny = dims[1]
201endif else begin
202  nx = dims[1]
203  ny = dims[2]
204endelse
205
206;-------------------------------------------------------------------------------
207;- CREATE A WINDOW
208;-------------------------------------------------------------------------------
209
210;- Create a draw widget sized to fit the image
211
212if not keyword_set(current) then begin
213
214  ;- Set default window size
215
216  scroll = 0
217  xsize = nx
218  ysize = ny
219  draw_xsize = nx
220  draw_ysize = ny
221
222  ;- Adjust the window size if the image is too large
223
224  device, get_screen_size=screen
225  screen_xsize = screen[0]
226  screen_ysize = screen[1]
227  if (nx gt screen_xsize) then begin
228    xsize = 0.9 * screen_xsize
229    scroll = 1
230  endif
231  if (ny gt screen_ysize) then begin
232    ysize = 0.9 * screen_ysize
233    scroll = 1
234  endif
235
236  ;- Create the draw widget
237
238  base = widget_base(title=file)
239  draw = widget_draw(base, scroll=scroll)
240  widget_control, draw, xsize=xsize, ysize=ysize, $
241    draw_xsize=draw_xsize, draw_ysize=draw_ysize
242
243endif
244
245;-------------------------------------------------------------------------------
246;- HANDLE IDL 8-BIT MODE
247;-------------------------------------------------------------------------------
248
249if depth eq 8 then begin
250
251  ;- If the color table of an 8-bit image is larger than
252  ;- the current display table, convert the image to 24-bit
253
254  if (info.channels eq 1) and (n_elements(r) gt !d.table_size) then begin
255
256    ;- Convert to 24-bit
257
258    dims = size(image, /dimensions)
259    nx = dims[0]
260    ny = dims[1]
261    true = bytarr(3, nx, ny)
262    true[0, *, *] = r[image]
263    true[1, *, *] = g[image]
264    true[2, *, *] = b[image]
265    image = temporary(true)
266
267    ;- Reset the number of channels
268
269    info.channels = 3
270
271  endif
272
273  ;- If image is 24-bit, convert to 8-bit
274
275  if info.channels eq 3 then begin
276
277    ;- Convert 24-bit image to 8-bit
278
279    image = color_quan(image, 1, r, g, b, colors=!d.table_size, $
280      dither=keyword_set(dither))
281
282    ;- Sort the color table from darkest to brightest
283
284    table_sum = total([[long(r)], [long(g)], [long(b)]], 2)
285    table_index = sort(table_sum)
286    image_index = sort(table_index)
287    r = r[table_index]
288    g = g[table_index]
289    b = b[table_index]
290    oldimage = image
291    image[*] = image_index[temporary(oldimage)]
292
293    ;- Reset the number of channels
294
295    info.channels = 1
296
297  endif
298
299endif
300
301;-------------------------------------------------------------------------------
302;- DISPLAY THE IMAGE
303;-------------------------------------------------------------------------------
304
305;- Realize the draw widget
306
307if not keyword_set(current) then widget_control, base, /realize
308
309;- Save current decomposed mode and display order
310
311device, get_decomposed=current_decomposed
312current_order = !order
313
314;- Set image to display from bottom up
315
316!order = 0
317
318;- Display the image
319
320if info.channels eq 1 then begin
321
322  device, decomposed=0
323  tvlct, r, g, b
324  tv, image
325
326endif else begin
327
328  device, decomposed=1
329  tv, image, true=1
330
331endelse
332
333;- Restore decomposed mode and display order
334
335device, decomposed=current_decomposed
336!order = current_order
337
338END
Note: See TracBrowser for help on using the repository browser.