source: trunk/SRC/Picture/saveimage.pro @ 231

Last change on this file since 231 was 231, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.7 KB
RevLine 
[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;-
[231]94;
[163]95PRO saveimage, FILE, BMP=BMP, PNG=PNG, PICT=PICT, JPEG=JPEG, TIFF=TIFF, $
[133]96  QUALITY=QUALITY, DITHER=DITHER, CUBE=CUBE, QUIET=QUIET, MULTIPLE = multiple
97;
98  compile_opt idl2, strictarrsubs
99;
[2]100
[69]101rcs_id = '$Id$'
102
[2]103;-------------------------------------------------------------------------------
104;- CHECK INPUT
105;-------------------------------------------------------------------------------
106
107;- Check arguments
[69]108if (n_params() ne 1) then message, 'Usage: SAVEIMAGE, FILE'
109if (n_elements(file) eq 0) then message, 'Argument FILE is undefined'
110if (n_elements(file) gt 1) then message, 'Argument FILE must be a scalar string'
[2]111
112;- Check keywords
113output = 'GIF'
114if keyword_set(bmp)  then output = 'BMP'
115if keyword_Set(png)  then output = 'PNG'
116if keyword_set(pict) then output = 'PICT'
117if keyword_set(jpeg) then output = 'JPEG'
118if keyword_set(tiff) then output = 'TIFF'
[69]119if (n_elements(quality) eq 0) then quality = 75
[2]120
[69]121;- Check for TVRD capable device
122if ((!d.flags and 128)) eq 0 then message, 'Unsupported graphics device'
[2]123
[69]124;- Check for open window
125if (!d.flags and 256) ne 0 then begin
126  if (!d.window lt 0) then message, 'No graphics windows are open'
127endif
[2]128
[69]129;- Get display depth
130depth = 8
131if (!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)
138if (!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]154endif
[2]155
156;- Read the pixmap contents into an array
[69]157if (depth gt 8) then begin
158  image = tvrd(order=0, true=1)
159endif else begin
160  image = tvrd(order=0)
161endelse
[2]162
[69]163;- Handle window devices (other than the Z buffer)
164if (!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]181endif
182
[2]183;- Get the current color table
184tvlct, r, g, b, /get
185
186;- If an 8-bit image was read, reduce the number of colors
[69]187if (depth le 8) then begin
[2]188  reduce_colors, image, index
189  r = r[index]
190  g = g[index]
191  b = b[index]
[69]192endif
[2]193
194;-------------------------------------------------------------------------------
195;- WRITE OUTPUT FILE
196;-------------------------------------------------------------------------------
197
198case 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]261endcase
262
263;- Print information for the user
[69]264if (keyword_set(quiet) eq 0) then $
265  print, file, output, format='("Created ",a," in ",a," format")'
[2]266
267END
Note: See TracBrowser for help on using the repository browser.