1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; Save the current graphics window to an output file (GIF by default). |
---|
5 | ; |
---|
6 | ; The output formats supported are: |
---|
7 | ; GIF 8-bit with color table, |
---|
8 | ; BMP 8-bit with color table, |
---|
9 | ; PNG 8-bit with color table, |
---|
10 | ; PICT 8-bit with color table, |
---|
11 | ; JPEG 24-bit true color, |
---|
12 | ; TIFF 24-bit true-color. |
---|
13 | ; |
---|
14 | ; Any conversions necessary to convert 8-bit or 24-bit images onscreen to |
---|
15 | ; 8-bit or 24-bit output files are done automatically. |
---|
16 | ; |
---|
17 | ; @categories |
---|
18 | ; Input/Output |
---|
19 | ; |
---|
20 | ; @param FILE {in}{required}{default=format GIF} |
---|
21 | ; Name of the output file |
---|
22 | ; |
---|
23 | ; @keyword BMP |
---|
24 | ; Set this keyword to create BMP format (8-bit with color table). |
---|
25 | ; |
---|
26 | ; @keyword PNG |
---|
27 | ; Set this keyword to create PNG format (8-bit with color table). |
---|
28 | ; |
---|
29 | ; @keyword PICT |
---|
30 | ; Set this keyword to create PICT format (8-bit with color table). |
---|
31 | ; |
---|
32 | ; @keyword JPEG |
---|
33 | ; Set this keyword to create JPEG format (24-bit true color). |
---|
34 | ; |
---|
35 | ; @keyword TIFF |
---|
36 | ; Set this keyword to create TIFF format (24-bit true color). |
---|
37 | ; |
---|
38 | ; @keyword QUALITY {default=75} |
---|
39 | ; If set to a named variable, specifies the quality for |
---|
40 | ; JPEG output. Ranges from 0 ("terrible") to |
---|
41 | ; 100 ("excellent"). Smaller quality values yield higher |
---|
42 | ; compression ratios and smaller output files. |
---|
43 | ; |
---|
44 | ; @keyword DITHER {default=no dithering} |
---|
45 | ; If set, dither the output image when creating 8-bit output |
---|
46 | ; which is read from a 24-bit display. |
---|
47 | ; |
---|
48 | ; @keyword CUBE {default=to use statistical method) |
---|
49 | ; If set, use the color cube method to quantize colors when |
---|
50 | ; creating 8-bit output which is read from a 24-bit display. |
---|
51 | ; This may improve the accuracy of colors in the output image, |
---|
52 | ; especially white. |
---|
53 | ; |
---|
54 | ; @keyword QUIET {default=to print an information message} |
---|
55 | ; Set this keyword to suppress the information message. |
---|
56 | ; |
---|
57 | ; @restrictions |
---|
58 | ; The output file is overwritten if it exists. |
---|
59 | ; |
---|
60 | ; requires IDL 5.0 or higher (square bracket array syntax). |
---|
61 | ; |
---|
62 | ; @examples |
---|
63 | ; |
---|
64 | ; IDL> openr, lun, filepath('hurric.dat', subdir='examples/data'), /get_lun |
---|
65 | ; IDL> image = bytarr(440, 330) |
---|
66 | ; IDL> readu, lun, image |
---|
67 | ; IDL> free_lun, lun |
---|
68 | ; IDL> loadct, 13 |
---|
69 | ; IDL> tvscl, image |
---|
70 | ; IDL> saveimage, 'hurric.gif' |
---|
71 | ; |
---|
72 | ; @history |
---|
73 | ; Liam.Gumley\@ssec.wisc.edu |
---|
74 | ; <a href="http://cimss.ssec.wisc.edu/~gumley/"/> |
---|
75 | ; |
---|
76 | ; This program is free software; you can redistribute it and/or |
---|
77 | ; modify it under the terms of the GNU General Public License |
---|
78 | ; as published by the Free Software Foundation; either version 2 |
---|
79 | ; of the License, or (at your option) any later version. |
---|
80 | ; |
---|
81 | ; This program is distributed in the hope that it will be useful, |
---|
82 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
83 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
84 | ; GNU General Public License for more details. |
---|
85 | ; |
---|
86 | ; You should have received a copy of the GNU General Public License |
---|
87 | ; along with this program; if not, write to the Free Software |
---|
88 | ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
89 | ; |
---|
90 | ; @version |
---|
91 | ; $Id$ |
---|
92 | ; |
---|
93 | ;- |
---|
94 | PRO saveimage, FILE, BMP=BMP, PNG=PNG, PICT=PICT, JPEG=JPEG, TIFF=TIFF, $ |
---|
95 | QUALITY=QUALITY, DITHER=DITHER, CUBE=CUBE, QUIET=QUIET, MULTIPLE=multiple |
---|
96 | ; |
---|
97 | compile_opt idl2, strictarrsubs |
---|
98 | ; |
---|
99 | |
---|
100 | rcs_id = '$Id$' |
---|
101 | |
---|
102 | ;------------------------------------------------------------------------------- |
---|
103 | ;- CHECK INPUT |
---|
104 | ;------------------------------------------------------------------------------- |
---|
105 | |
---|
106 | ;- Check arguments |
---|
107 | if (n_params() ne 1) then message, 'Usage: SAVEIMAGE, FILE' |
---|
108 | if (n_elements(file) eq 0) then message, 'Argument FILE is undefined' |
---|
109 | if (n_elements(file) gt 1) then message, 'Argument FILE must be a scalar string' |
---|
110 | |
---|
111 | ;- Check keywords |
---|
112 | output = 'GIF' |
---|
113 | if keyword_set(bmp) then output = 'BMP' |
---|
114 | if keyword_Set(png) then output = 'PNG' |
---|
115 | if keyword_set(pict) then output = 'PICT' |
---|
116 | if keyword_set(jpeg) then output = 'JPEG' |
---|
117 | if keyword_set(tiff) then output = 'TIFF' |
---|
118 | if (n_elements(quality) eq 0) then quality = 75 |
---|
119 | |
---|
120 | ;- Check for TVRD capable device |
---|
121 | if ((!d.flags and 128)) eq 0 then message, 'Unsupported graphics device' |
---|
122 | |
---|
123 | ;- Check for open window |
---|
124 | if (!d.flags and 256) ne 0 then begin |
---|
125 | if (!d.window lt 0) then message, 'No graphics windows are open' |
---|
126 | endif |
---|
127 | |
---|
128 | ;- Get display depth |
---|
129 | depth = 8 |
---|
130 | if (!d.n_colors gt 256) then depth = 24 |
---|
131 | |
---|
132 | ;------------------------------------------------------------------------------- |
---|
133 | ;- GET CONTENTS OF GRAPHICS WINDOW |
---|
134 | ;------------------------------------------------------------------------------- |
---|
135 | |
---|
136 | ;- Handle window devices (other than the Z buffer) |
---|
137 | if (!d.flags and 256) ne 0 then begin |
---|
138 | |
---|
139 | ;- Copy the contents of the current display to a pixmap |
---|
140 | current_window = !d.window |
---|
141 | xsize = !d.x_size |
---|
142 | ysize = !d.y_size |
---|
143 | window, /free, /pixmap, xsize=xsize, ysize=ysize, retain=2 |
---|
144 | device, copy=[0, 0, xsize, ysize, 0, 0, current_window] |
---|
145 | |
---|
146 | ;- Set decomposed color mode for 24-bit displays |
---|
147 | version = float(!version.release) |
---|
148 | if (depth gt 8) then begin |
---|
149 | if (version gt 5.1) then device, get_decomposed=entry_decomposed |
---|
150 | device, decomposed=1 |
---|
151 | endif |
---|
152 | |
---|
153 | endif |
---|
154 | |
---|
155 | ;- Read the pixmap contents into an array |
---|
156 | if (depth gt 8) then begin |
---|
157 | image = tvrd(order=0, true=1) |
---|
158 | endif else begin |
---|
159 | image = tvrd(order=0) |
---|
160 | endelse |
---|
161 | |
---|
162 | ;- Handle window devices (other than the Z buffer) |
---|
163 | if (!d.flags and 256) ne 0 then begin |
---|
164 | |
---|
165 | ;- Restore decomposed color mode for 24-bit displays |
---|
166 | if (depth gt 8) then begin |
---|
167 | if (version gt 5.1) then begin |
---|
168 | device, decomposed=entry_decomposed |
---|
169 | endif else begin |
---|
170 | device, decomposed=0 |
---|
171 | if (keyword_set(quiet) eq 0) then $ |
---|
172 | print, 'Decomposed color was turned off' |
---|
173 | endelse |
---|
174 | endif |
---|
175 | |
---|
176 | ;- Delete the pixmap |
---|
177 | wdelete, !d.window |
---|
178 | wset, current_window |
---|
179 | |
---|
180 | endif |
---|
181 | |
---|
182 | ;- Get the current color table |
---|
183 | tvlct, r, g, b, /get |
---|
184 | |
---|
185 | ;- If an 8-bit image was read, reduce the number of colors |
---|
186 | if (depth le 8) then begin |
---|
187 | reduce_colors, image, index |
---|
188 | r = r[index] |
---|
189 | g = g[index] |
---|
190 | b = b[index] |
---|
191 | endif |
---|
192 | |
---|
193 | ;------------------------------------------------------------------------------- |
---|
194 | ;- WRITE OUTPUT FILE |
---|
195 | ;------------------------------------------------------------------------------- |
---|
196 | |
---|
197 | case 1 of |
---|
198 | |
---|
199 | ;- Save the image in 8-bit output format |
---|
200 | (output eq 'GIF') or (output eq 'BMP') or $ |
---|
201 | (output eq 'PICT') or (output eq 'PNG') : begin |
---|
202 | |
---|
203 | if (depth gt 8) then begin |
---|
204 | |
---|
205 | ;- Convert 24-bit image to 8-bit |
---|
206 | case keyword_set(cube) of |
---|
207 | 0 : image = color_quan(image, 1, r, g, b, colors=256, $ |
---|
208 | dither=keyword_set(dither)) |
---|
209 | 1 : image = color_quan(image, 1, r, g, b, cube=6) |
---|
210 | endcase |
---|
211 | |
---|
212 | ;- Sort the color table from darkest to brightest |
---|
213 | table_sum = total([[long(r)], [long(g)], [long(b)]], 2) |
---|
214 | table_index = sort(table_sum) |
---|
215 | image_index = sort(table_index) |
---|
216 | r = r[table_index] |
---|
217 | g = g[table_index] |
---|
218 | b = b[table_index] |
---|
219 | oldimage = image |
---|
220 | image[*] = image_index[temporary(oldimage)] |
---|
221 | |
---|
222 | endif |
---|
223 | |
---|
224 | ;- Save the image |
---|
225 | case output of |
---|
226 | 'GIF' : write_gif, file, image, r, g, b, MULTIPLE = multiple |
---|
227 | 'BMP' : write_bmp, file, image, r, g, b |
---|
228 | 'PNG' : write_png, file, image, r, g, b |
---|
229 | 'PICT' : write_pict, file, image, r, g, b |
---|
230 | endcase |
---|
231 | |
---|
232 | end |
---|
233 | |
---|
234 | ;- Save the image in 24-bit output format |
---|
235 | (output eq 'JPEG') or (output eq 'TIFF') : begin |
---|
236 | |
---|
237 | ;- Convert 8-bit image to 24-bit |
---|
238 | if (depth le 8) then begin |
---|
239 | info = size(image) |
---|
240 | nx = info[1] |
---|
241 | ny = info[2] |
---|
242 | true = bytarr(3, nx, ny) |
---|
243 | true[0, *, *] = r[image] |
---|
244 | true[1, *, *] = g[image] |
---|
245 | true[2, *, *] = b[image] |
---|
246 | image = temporary(true) |
---|
247 | endif |
---|
248 | |
---|
249 | ;- If TIFF format output, reverse image top to bottom |
---|
250 | if (output eq 'TIFF') then image = reverse(temporary(image), 3) |
---|
251 | |
---|
252 | ;- Write the image |
---|
253 | case output of |
---|
254 | 'JPEG' : write_jpeg, file, image, true=1, quality=quality |
---|
255 | 'TIFF' : write_tiff, file, image, 1 |
---|
256 | endcase |
---|
257 | |
---|
258 | end |
---|
259 | |
---|
260 | endcase |
---|
261 | |
---|
262 | ;- Print information for the user |
---|
263 | if (keyword_set(quiet) eq 0) then $ |
---|
264 | print, file, output, format='("Created ",a," in ",a," format")' |
---|
265 | |
---|
266 | END |
---|