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

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

improvements/corrections of some *.pro headers

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