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