source: trunk/ToBeReviewed/IMAGE/showimage.pro @ 23

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

upgrade of IMAGE according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 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;    PNG   8-bit with color table,
13;    BMP   8-bit with color table or 24-bit true-color,
14;    PICT  8-bit with color table,
15;    TIFF  8-bit with color table or 24-bit true-color,
16;    JPEG 24-bit true color,
17;
18;    Any conversions necessary to translate 8-bit or 24-bit files
19;    to 8-bit or 24-bit images on-screen are done automatically.
20;
21; CATEGORY:
22;    Input/Output.
23;
24; CALLING SEQUENCE:
25;    SHOWIMAGE, FILE
26;
27; INPUTS:
28;    FILE     Name of the output file (format is identified automatically).
29;
30; OPTIONAL INPUTS:
31;    None.
32;
33; KEYWORD PARAMETERS:
34;    DITHER   Set this keyword to dither the input image when displaying
35;             24-bit images on an 8-bit display (default is no dithering).
36;    CURRENT  Set this keyword to display the image in the current window
37;             (default is to create a new window sized to fit the image).
38;
39; OUTPUTS:
40;    None.
41;
42; OPTIONAL OUTPUTS:
43;    None
44;
45; COMMON BLOCKS:
46;    None
47;
48; SIDE EFFECTS:
49;    The color table is modified.
50;
51; RESTRICTIONS:
52;    Requires IDL 5.2 or higher (image QUERY functions).
53;
54; EXAMPLE:
55;
56;showimage, filepath('rose.jpg', subdir='examples/data')
57;
58; MODIFICATION HISTORY:
59; Liam.Gumley@ssec.wisc.edu
60; $Id$
61;-
62
63;-------------------------------------------------------------------------------
64;- CHECK INPUT
65;-------------------------------------------------------------------------------
66
67;- Check IDL version
68
69if float(!version.release) lt 5.2 then begin
70  message, 'IDL 5.2 or higher is required', /continue
71  return
72endif
73
74;- Check input arguments
75
76case 1 of
77  n_params() ne 1           : error = 'Usage: SHOWIMAGE, FILE'
78  n_elements(file) eq 0     : error = 'Argument FILE is undefined'
79  n_elements(file) gt 1     : error = 'Argument FILE must be a scalar string'
80  (findfile(file))[0] eq '' : error = 'Argument FILE was not found'
81  else                      : error = ''
82endcase
83
84if error ne '' then begin
85  message, error, /continue
86  return
87endif
88
89;-------------------------------------------------------------------------------
90;- CHECK THE GRAPHICS DEVICE
91;-------------------------------------------------------------------------------
92
93;- Check for device supporting windows and tvrd()
94
95if ((!d.flags and 256) eq 0) or ((!d.flags and 128) eq 0) then begin
96  error = string(!d.name, format='("Graphics device (",a,") is not supported")')
97  message, error, /continue
98  return
99endif
100
101;- Make sure a window has been opened in this session and get visual depth
102
103if !d.window lt 0 then begin
104  window, /free, /pixmap, xsize=20, ysize=20
105  wdelete, !d.window
106endif
107device, get_visual_depth=depth
108
109;- If 8-bit display is low on colors, print a message
110
111if (depth eq 8) and (!d.table_size) lt 64 then message, $
112  'Display has less than 64 colors; image quality may degrade', /continue
113
114;-------------------------------------------------------------------------------
115;- IDENTIFY FILE AND READ IMAGE
116;-------------------------------------------------------------------------------
117
118;- Identify the file format
119
120result = query_gif(file, info)
121if result eq 0 then result = query_bmp(file, info)
122if result eq 0 then result = query_pict(file, info)
123if result eq 0 then result = query_tiff(file, info)
124if result eq 0 then result = query_jpeg(file, info)
125if result eq 0 then result = query_png(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  'PNG' : read_png, 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  if nx gt (0.95 * screen[0]) then begin
208    xsize = 0.85 * nx
209    scroll = 1
210  endif
211  if ny gt (0.95 * screen[1]) then begin
212    ysize = 0.85 * ny
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.