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