source: trunk/IMAGE/showimage.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

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