source: trunk/SRC/Picture/showimage.pro @ 134

Last change on this file since 134 was 134, checked in by navarro, 18 years ago

change *.pro file properties (del eof-style, del executable, set keywords Id

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