source: trunk/ToBeReviewed/IMAGE/saveimage.pro @ 69

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

debug + new xxx

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 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;+
5; NAME:
6;    SAVEIMAGE
7;
8; PURPOSE:
9;    Save the current graphics window to an output file (GIF by default).
10;
11;    The output formats supported are:
12;    GIF   8-bit with color table,
13;    BMP   8-bit with color table,
14;    PNG   8-bit with color table,
15;    PICT  8-bit with color table,
16;    JPEG 24-bit true color,
17;    TIFF 24-bit true-color.
18;
19;    Any conversions necessary to convert 8-bit or 24-bit images onscreen to
20;    8-bit or 24-bit output files are done automatically.
21;
22; CATEGORY:
23;    Input/Output.
24;
25; CALLING SEQUENCE:
26;    SAVEIMAGE, FILE
27;
28; INPUTS:
29;    FILE     Name of the output file (GIF format by default).
30;
31; OPTIONAL INPUTS:
32;    None.
33;
34; KEYWORD PARAMETERS:
35;    BMP      Set this keyword to create BMP format (8-bit with color table).
36;    PNG      Set this keyword to create PNG format (8-bit with color table).
37;    PICT     Set this keyword to create PICT format (8-bit with color table).
38;    JPEG     Set this keyword to create JPEG format (24-bit true color).
39;    TIFF     Set this keyword to create TIFF format (24-bit true color).
40;    QUALITY  If set to a named variable, specifies the quality for
41;             JPEG output (default 75). Ranges from 0 ("terrible") to
42;             100 ("excellent"). Smaller quality values yield higher
43;             compression ratios and smaller output files.
44;    DITHER   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;    CUBE     If set, use the color cube method to quantize colors when
47;             creating 8-bit output which is read from a 24-bit display
48;             (default is to use the statistical method). This may improve
49;             the accuracy of colors in the output image, especially white.
50;    QUIET    Set this keyword to suppress the information message
51;             (default is to print an information message).
52;    MULTIPLE to write multiple gif image
53;
54; OUTPUTS:
55;    None.
56;
57; OPTIONAL OUTPUTS:
58;    None
59;
60; COMMON BLOCKS:
61;    None
62;
63; SIDE EFFECTS:
64;    The output file is overwritten if it exists.
65;
66; RESTRICTIONS:
67;    Requires IDL 5.0 or higher (square bracket array syntax).
68;
69; EXAMPLE:
70;
71;openr, lun, filepath('hurric.dat', subdir='examples/data'), /get_lun
72;image = bytarr(440, 330)
73;readu, lun, image
74;free_lun, lun
75;loadct, 13
76;tvscl, image
77;saveimage, 'hurric.gif'
78;
79; MODIFICATION HISTORY:
80; Liam.Gumley@ssec.wisc.edu
81; http://cimss.ssec.wisc.edu/~gumley
82; $Id$
83;
84; Copyright (C) 1999 Liam E. Gumley
85;
86; This program is free software; you can redistribute it and/or
87; modify it under the terms of the GNU General Public License
88; as published by the Free Software Foundation; either version 2
89; of the License, or (at your option) any later version.
90;
91; This program is distributed in the hope that it will be useful,
92; but WITHOUT ANY WARRANTY; without even the implied warranty of
93; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
94; GNU General Public License for more details.
95;
96; You should have received a copy of the GNU General Public License
97; along with this program; if not, write to the Free Software
98; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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.