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

Last change on this file since 138 was 136, checked in by pinsard, 18 years ago

some improvements and corrections in some .pro file according to
aspell and idldoc log file

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