[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[136] | 3 | ; @file_comments |
---|
[133] | 4 | ; Save the current graphics window to an output file (GIF by default). |
---|
[2] | 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 | ; |
---|
[230] | 17 | ; @categories |
---|
| 18 | ; Input/Output |
---|
[2] | 19 | ; |
---|
[163] | 20 | ; @param FILE {in}{required}{default=format GIF} |
---|
| 21 | ; Name of the output file |
---|
[2] | 22 | ; |
---|
[136] | 23 | ; @keyword BMP |
---|
| 24 | ; Set this keyword to create BMP format (8-bit with color table). |
---|
[69] | 25 | ; |
---|
[136] | 26 | ; @keyword PNG |
---|
| 27 | ; Set this keyword to create PNG format (8-bit with color table). |
---|
[69] | 28 | ; |
---|
[136] | 29 | ; @keyword PICT |
---|
| 30 | ; Set this keyword to create PICT format (8-bit with color table). |
---|
[133] | 31 | ; |
---|
[136] | 32 | ; @keyword JPEG |
---|
| 33 | ; Set this keyword to create JPEG format (24-bit true color). |
---|
[133] | 34 | ; |
---|
[136] | 35 | ; @keyword TIFF |
---|
| 36 | ; Set this keyword to create TIFF format (24-bit true color). |
---|
[133] | 37 | ; |
---|
[163] | 38 | ; @keyword QUALITY {default=75} |
---|
[136] | 39 | ; If set to a named variable, specifies the quality for |
---|
[163] | 40 | ; JPEG output. Ranges from 0 ("terrible") to |
---|
[136] | 41 | ; 100 ("excellent"). Smaller quality values yield higher |
---|
| 42 | ; compression ratios and smaller output files. |
---|
[133] | 43 | ; |
---|
[163] | 44 | ; @keyword DITHER {default=no dithering} |
---|
[136] | 45 | ; If set, dither the output image when creating 8-bit output |
---|
[163] | 46 | ; which is read from a 24-bit display. |
---|
[133] | 47 | ; |
---|
[163] | 48 | ; @keyword CUBE {default=to use statistical method) |
---|
[136] | 49 | ; If set, use the color cube method to quantize colors when |
---|
[163] | 50 | ; creating 8-bit output which is read from a 24-bit display. |
---|
[230] | 51 | ; This may improve the accuracy of colors in the output image, |
---|
[163] | 52 | ; especially white. |
---|
[2] | 53 | ; |
---|
[163] | 54 | ; @keyword QUIET {default=to print an information message} |
---|
| 55 | ; Set this keyword to suppress the information message. |
---|
[2] | 56 | ; |
---|
[136] | 57 | ; @restrictions |
---|
| 58 | ; The output file is overwritten if it exists. |
---|
[2] | 59 | ; |
---|
[136] | 60 | ; requires IDL 5.0 or higher (square bracket array syntax). |
---|
| 61 | ; |
---|
[133] | 62 | ; @examples |
---|
[2] | 63 | ; |
---|
[136] | 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' |
---|
[2] | 71 | ; |
---|
[230] | 72 | ; @history |
---|
| 73 | ; Liam.Gumley@ssec.wisc.edu |
---|
[69] | 74 | ; 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. |
---|
[133] | 89 | ; |
---|
[230] | 90 | ; @version |
---|
| 91 | ; $Id$ |
---|
[133] | 92 | ; |
---|
[2] | 93 | ;- |
---|
[163] | 94 | PRO saveimage, FILE, BMP=BMP, PNG=PNG, PICT=PICT, JPEG=JPEG, TIFF=TIFF, $ |
---|
[133] | 95 | QUALITY=QUALITY, DITHER=DITHER, CUBE=CUBE, QUIET=QUIET, MULTIPLE = multiple |
---|
| 96 | ; |
---|
| 97 | compile_opt idl2, strictarrsubs |
---|
| 98 | ; |
---|
[2] | 99 | |
---|
[69] | 100 | rcs_id = '$Id$' |
---|
| 101 | |
---|
[2] | 102 | ;------------------------------------------------------------------------------- |
---|
| 103 | ;- CHECK INPUT |
---|
| 104 | ;------------------------------------------------------------------------------- |
---|
| 105 | |
---|
| 106 | ;- Check arguments |
---|
[69] | 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' |
---|
[2] | 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' |
---|
[69] | 118 | if (n_elements(quality) eq 0) then quality = 75 |
---|
[2] | 119 | |
---|
[69] | 120 | ;- Check for TVRD capable device |
---|
| 121 | if ((!d.flags and 128)) eq 0 then message, 'Unsupported graphics device' |
---|
[2] | 122 | |
---|
[69] | 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 |
---|
[2] | 127 | |
---|
[69] | 128 | ;- Get display depth |
---|
| 129 | depth = 8 |
---|
| 130 | if (!d.n_colors gt 256) then depth = 24 |
---|
| 131 | |
---|
[2] | 132 | ;------------------------------------------------------------------------------- |
---|
| 133 | ;- GET CONTENTS OF GRAPHICS WINDOW |
---|
| 134 | ;------------------------------------------------------------------------------- |
---|
| 135 | |
---|
[69] | 136 | ;- Handle window devices (other than the Z buffer) |
---|
| 137 | if (!d.flags and 256) ne 0 then begin |
---|
[2] | 138 | |
---|
[69] | 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] |
---|
[2] | 145 | |
---|
[69] | 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 |
---|
[2] | 152 | |
---|
[69] | 153 | endif |
---|
[2] | 154 | |
---|
| 155 | ;- Read the pixmap contents into an array |
---|
[69] | 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 |
---|
[2] | 161 | |
---|
[69] | 162 | ;- Handle window devices (other than the Z buffer) |
---|
| 163 | if (!d.flags and 256) ne 0 then begin |
---|
[2] | 164 | |
---|
[69] | 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 |
---|
[2] | 175 | |
---|
[69] | 176 | ;- Delete the pixmap |
---|
| 177 | wdelete, !d.window |
---|
| 178 | wset, current_window |
---|
[2] | 179 | |
---|
[69] | 180 | endif |
---|
| 181 | |
---|
[2] | 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 |
---|
[69] | 186 | if (depth le 8) then begin |
---|
[2] | 187 | reduce_colors, image, index |
---|
| 188 | r = r[index] |
---|
| 189 | g = g[index] |
---|
| 190 | b = b[index] |
---|
[69] | 191 | endif |
---|
[2] | 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 | |
---|
[69] | 203 | if (depth gt 8) then begin |
---|
[2] | 204 | |
---|
| 205 | ;- Convert 24-bit image to 8-bit |
---|
[69] | 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 |
---|
[2] | 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)] |
---|
[69] | 221 | |
---|
[2] | 222 | endif |
---|
[69] | 223 | |
---|
[2] | 224 | ;- Save the image |
---|
| 225 | case output of |
---|
[69] | 226 | 'GIF' : write_gif, file, image, r, g, b, MULTIPLE = multiple |
---|
[2] | 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 |
---|
[69] | 231 | |
---|
[2] | 232 | end |
---|
[69] | 233 | |
---|
[2] | 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 |
---|
[69] | 238 | if (depth le 8) then begin |
---|
| 239 | info = size(image) |
---|
| 240 | nx = info[1] |
---|
| 241 | ny = info[2] |
---|
[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 |
---|
[69] | 250 | if (output eq 'TIFF') then image = reverse(temporary(image), 3) |
---|
| 251 | |
---|
[2] | 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 |
---|
[69] | 257 | |
---|
[2] | 258 | end |
---|
[69] | 259 | |
---|
[2] | 260 | endcase |
---|
| 261 | |
---|
| 262 | ;- Print information for the user |
---|
[69] | 263 | if (keyword_set(quiet) eq 0) then $ |
---|
| 264 | print, file, output, format='("Created ",a," in ",a," format")' |
---|
[2] | 265 | |
---|
| 266 | END |
---|