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

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

modification of some headers (+some corrections) to prepare usage of the new idldoc

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