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