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

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

change *.pro file properties (del eof-style, del executable, set keywords Id

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