1 | PRO SAVEIMAGE, FILE, BMP=BMP, PNG=PNG, PICT=PICT, JPEG=JPEG, TIFF=TIFF, $ |
---|
2 | QUALITY=QUALITY, DITHER=DITHER, QUIET=QUIET |
---|
3 | |
---|
4 | ;+ |
---|
5 | ; NAME: |
---|
6 | ; SAVEIMAGE |
---|
7 | ; |
---|
8 | ; PURPOSE: |
---|
9 | ; Save the current graphics window to an output file (GIF by default). |
---|
10 | ; |
---|
11 | ; The output formats supported are: |
---|
12 | ; GIF 8-bit with color table, |
---|
13 | ; BMP 8-bit with color table, |
---|
14 | ; PNG 8-bit with color table, |
---|
15 | ; PICT 8-bit with color table, |
---|
16 | ; JPEG 24-bit true color, |
---|
17 | ; TIFF 24-bit true-color. |
---|
18 | ; |
---|
19 | ; Any conversions necessary to convert 8-bit or 24-bit images onscreen to |
---|
20 | ; 8-bit or 24-bit output files are done automatically. |
---|
21 | ; |
---|
22 | ; CATEGORY: |
---|
23 | ; Input/Output. |
---|
24 | ; |
---|
25 | ; CALLING SEQUENCE: |
---|
26 | ; SAVEIMAGE, FILE |
---|
27 | ; |
---|
28 | ; INPUTS: |
---|
29 | ; FILE Name of the output file (GIF format by default). |
---|
30 | ; |
---|
31 | ; OPTIONAL INPUTS: |
---|
32 | ; None. |
---|
33 | ; |
---|
34 | ; KEYWORD PARAMETERS: |
---|
35 | ; BMP Set this keyword to create BMP format (8-bit with color table). |
---|
36 | ; PNG Set this keyword to create PNG format (8-bit with color table). |
---|
37 | ; PICT Set this keyword to create PICT format (8-bit with color table). |
---|
38 | ; JPEG Set this keyword to create JPEG format (24-bit true color). |
---|
39 | ; TIFF Set this keyword to create TIFF format (24-bit true color). |
---|
40 | ; QUALITY If set to a named variable, specifies the quality for |
---|
41 | ; JPEG output (default 75). Ranges from 0 ("terrible") to |
---|
42 | ; 100 ("excellent"). Smaller quality values yield higher |
---|
43 | ; compression ratios and smaller output files. |
---|
44 | ; DITHER Set this keyword to dither the output image when creating 8-bit |
---|
45 | ; GIF or BMP format (default is no dithering). |
---|
46 | ; QUIET Set this keyword to suppress the information message |
---|
47 | ; (default is to print an information message). |
---|
48 | ; |
---|
49 | ; OUTPUTS: |
---|
50 | ; None. |
---|
51 | ; |
---|
52 | ; OPTIONAL OUTPUTS: |
---|
53 | ; None |
---|
54 | ; |
---|
55 | ; COMMON BLOCKS: |
---|
56 | ; None |
---|
57 | ; |
---|
58 | ; SIDE EFFECTS: |
---|
59 | ; The output file is overwritten if it exists. |
---|
60 | ; |
---|
61 | ; RESTRICTIONS: |
---|
62 | ; Requires IDL 5.1 or higher. |
---|
63 | ; |
---|
64 | ; EXAMPLE: |
---|
65 | ; |
---|
66 | ;openr, 1, filepath('hurric.dat', subdir='examples/data') |
---|
67 | ;image = bytarr(440, 340) |
---|
68 | ;readu, 1, image |
---|
69 | ;close, 1 |
---|
70 | ;loadct, 13 |
---|
71 | ;tvscl, image |
---|
72 | ;saveimage, 'hurric.gif' |
---|
73 | ; |
---|
74 | ; MODIFICATION HISTORY: |
---|
75 | ; Liam.Gumley@ssec.wisc.edu |
---|
76 | ; $Id$ |
---|
77 | ;- |
---|
78 | |
---|
79 | ;------------------------------------------------------------------------------- |
---|
80 | ;- CHECK INPUT |
---|
81 | ;------------------------------------------------------------------------------- |
---|
82 | |
---|
83 | ;- Check IDL version |
---|
84 | |
---|
85 | if float(!version.release) lt 5.1 then begin |
---|
86 | message, 'IDL 5.1 or higher is required', /continue |
---|
87 | return |
---|
88 | endif |
---|
89 | |
---|
90 | ;- Check arguments |
---|
91 | |
---|
92 | if n_params() ne 1 then begin |
---|
93 | message, 'Usage: SAVEIMAGE, FILE', /continue |
---|
94 | return |
---|
95 | endif |
---|
96 | if n_elements(file) eq 0 then message, 'Argument FILE is undefined' |
---|
97 | if n_elements(file) gt 1 then message, 'Argument FILE must be a scalar string' |
---|
98 | |
---|
99 | ;- Check keywords |
---|
100 | |
---|
101 | output = 'GIF' |
---|
102 | if keyword_set(bmp) then output = 'BMP' |
---|
103 | if keyword_Set(png) then output = 'PNG' |
---|
104 | if keyword_set(pict) then output = 'PICT' |
---|
105 | if keyword_set(jpeg) then output = 'JPEG' |
---|
106 | if keyword_set(tiff) then output = 'TIFF' |
---|
107 | if n_elements(quality) eq 0 then quality = 75 |
---|
108 | |
---|
109 | ;- Check for window capable device with an open window, and get visual depth |
---|
110 | |
---|
111 | if (!d.flags and 256) eq 0 then message, 'Unsupported graphics device' |
---|
112 | if !d.window lt 0 then message, 'No graphics windows are open' |
---|
113 | device, get_visual_depth=depth |
---|
114 | |
---|
115 | ;------------------------------------------------------------------------------- |
---|
116 | ;- GET CONTENTS OF GRAPHICS WINDOW |
---|
117 | ;------------------------------------------------------------------------------- |
---|
118 | |
---|
119 | ;- Save display order, then set images to display from bottom up |
---|
120 | |
---|
121 | current_order = !order |
---|
122 | !order = 0 |
---|
123 | |
---|
124 | ;- Copy the contents of the current display to a pixmap |
---|
125 | |
---|
126 | current_window = !d.window |
---|
127 | xsize = !d.x_size |
---|
128 | ysize = !d.y_size |
---|
129 | window, /free, /pixmap, xsize=xsize, ysize=ysize |
---|
130 | device, copy=[0, 0, xsize, ysize, 0, 0, current_window] |
---|
131 | |
---|
132 | ;- Read the pixmap contents into an array |
---|
133 | |
---|
134 | if depth gt 8 then image = tvrd(true=1) else image = tvrd() |
---|
135 | |
---|
136 | ;- Delete the pixmap |
---|
137 | |
---|
138 | wdelete, !d.window |
---|
139 | wset, current_window |
---|
140 | |
---|
141 | ;- Get the current color table |
---|
142 | |
---|
143 | tvlct, r, g, b, /get |
---|
144 | |
---|
145 | ;- If an 8-bit image was read, reduce the number of colors |
---|
146 | |
---|
147 | if depth le 8 then begin |
---|
148 | reduce_colors, image, index |
---|
149 | r = r[index] |
---|
150 | g = g[index] |
---|
151 | b = b[index] |
---|
152 | endif |
---|
153 | |
---|
154 | ;- Restore display order |
---|
155 | |
---|
156 | !order = current_order |
---|
157 | |
---|
158 | ;------------------------------------------------------------------------------- |
---|
159 | ;- WRITE OUTPUT FILE |
---|
160 | ;------------------------------------------------------------------------------- |
---|
161 | |
---|
162 | ;- Save the image in 8-bit or 24-bit format |
---|
163 | |
---|
164 | case 1 of |
---|
165 | |
---|
166 | ;- Save the image in 8-bit output format |
---|
167 | |
---|
168 | (output eq 'GIF') or (output eq 'BMP') or $ |
---|
169 | (output eq 'PICT') or (output eq 'PNG') : begin |
---|
170 | |
---|
171 | if depth gt 8 then begin |
---|
172 | |
---|
173 | ;- Convert 24-bit image to 8-bit |
---|
174 | |
---|
175 | image = color_quan(image, 1, r, g, b, colors=256, $ |
---|
176 | dither=keyword_set(dither)) |
---|
177 | |
---|
178 | ;- Sort the color table from darkest to brightest |
---|
179 | |
---|
180 | table_sum = total([[long(r)], [long(g)], [long(b)]], 2) |
---|
181 | table_index = sort(table_sum) |
---|
182 | image_index = sort(table_index) |
---|
183 | r = r[table_index] |
---|
184 | g = g[table_index] |
---|
185 | b = b[table_index] |
---|
186 | oldimage = image |
---|
187 | image[*] = image_index[temporary(oldimage)] |
---|
188 | |
---|
189 | endif |
---|
190 | |
---|
191 | ;- Save the image |
---|
192 | |
---|
193 | case output of |
---|
194 | 'GIF' : write_gif, file, image, r, g, b |
---|
195 | 'BMP' : write_bmp, file, image, r, g, b |
---|
196 | 'PNG' : write_png, file, image, r, g, b |
---|
197 | 'PICT' : write_pict, file, image, r, g, b |
---|
198 | endcase |
---|
199 | |
---|
200 | end |
---|
201 | |
---|
202 | ;- Save the image in 24-bit output format |
---|
203 | |
---|
204 | (output eq 'JPEG') or (output eq 'TIFF') : begin |
---|
205 | |
---|
206 | ;- Convert 8-bit image to 24-bit |
---|
207 | |
---|
208 | if depth le 8 then begin |
---|
209 | dims = size(image, /dimensions) |
---|
210 | nx = dims[0] |
---|
211 | ny = dims[1] |
---|
212 | true = bytarr(3, nx, ny) |
---|
213 | true[0, *, *] = r[image] |
---|
214 | true[1, *, *] = g[image] |
---|
215 | true[2, *, *] = b[image] |
---|
216 | image = temporary(true) |
---|
217 | endif |
---|
218 | |
---|
219 | ;- If TIFF format output, reverse image top to bottom |
---|
220 | |
---|
221 | if output eq 'TIFF' then image = reverse(temporary(image), 3) |
---|
222 | |
---|
223 | ;- Write the image |
---|
224 | |
---|
225 | case output of |
---|
226 | 'JPEG' : write_jpeg, file, image, true=1, quality=quality |
---|
227 | 'TIFF' : write_tiff, file, image, 1 |
---|
228 | endcase |
---|
229 | |
---|
230 | end |
---|
231 | |
---|
232 | endcase |
---|
233 | |
---|
234 | ;- Print information for the user |
---|
235 | |
---|
236 | if not keyword_set(quiet) then $ |
---|
237 | print, file, output, format='("Created ",a," in ",a," format")' |
---|
238 | |
---|
239 | END |
---|