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