source: trunk/SRC/ToBeReviewed/IMAGE/saveimage.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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