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

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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