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

Last change on this file since 378 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

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