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

Last change on this file since 157 was 136, checked in by pinsard, 18 years ago

some improvements and corrections in some .pro file according to
aspell and idldoc log file

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