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
RevLine 
[2]1;+
2;
[136]3; @file_comments
[133]4; Save the current graphics window to an output file (GIF by default).
[2]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;
[133]17; @categories Input/Output.
[2]18;
[163]19; @param FILE {in}{required}{default=format GIF}
20; Name of the output file
[2]21;
[136]22; @keyword BMP
23; Set this keyword to create BMP format (8-bit with color table).
[69]24;
[136]25; @keyword PNG
26; Set this keyword to create PNG format (8-bit with color table).
[69]27;
[136]28; @keyword PICT
29; Set this keyword to create PICT format (8-bit with color table).
[133]30;
[136]31; @keyword JPEG
32; Set this keyword to create JPEG format (24-bit true color).
[133]33;
[136]34; @keyword TIFF
35; Set this keyword to create TIFF format (24-bit true color).
[133]36;
[163]37; @keyword QUALITY {default=75}
[136]38; If set to a named variable, specifies the quality for
[163]39; JPEG output. Ranges from 0 ("terrible") to
[136]40; 100 ("excellent"). Smaller quality values yield higher
41; compression ratios and smaller output files.
[133]42;
[163]43; @keyword DITHER {default=no dithering}
[136]44; If set, dither the output image when creating 8-bit output
[163]45; which is read from a 24-bit display.
[133]46;
[163]47; @keyword CUBE {default=to use statistical method)
[136]48; If set, use the color cube method to quantize colors when
[163]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.
[2]52;
[163]53; @keyword QUIET {default=to print an information message}
54; Set this keyword to suppress the information message.
[2]55;
[136]56; @restrictions
57; The output file is overwritten if it exists.
[2]58;
[136]59; @restrictions
60; requires IDL 5.0 or higher (square bracket array syntax).
61;
[133]62; @examples
[2]63;
[136]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'
[2]71;
[133]72; @history Liam.Gumley@ssec.wisc.edu
[69]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.
[133]88;
89; @version $Id$
90;
[2]91;-
[163]92PRO saveimage, FILE, BMP=BMP, PNG=PNG, PICT=PICT, JPEG=JPEG, TIFF=TIFF, $
[133]93  QUALITY=QUALITY, DITHER=DITHER, CUBE=CUBE, QUIET=QUIET, MULTIPLE = multiple
94;
95  compile_opt idl2, strictarrsubs
96;
[2]97
[69]98rcs_id = '$Id$'
99
[2]100;-------------------------------------------------------------------------------
101;- CHECK INPUT
102;-------------------------------------------------------------------------------
103
104;- Check arguments
[69]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'
[2]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'
[69]116if (n_elements(quality) eq 0) then quality = 75
[2]117
[69]118;- Check for TVRD capable device
119if ((!d.flags and 128)) eq 0 then message, 'Unsupported graphics device'
[2]120
[69]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
[2]125
[69]126;- Get display depth
127depth = 8
128if (!d.n_colors gt 256) then depth = 24
129
[2]130;-------------------------------------------------------------------------------
131;- GET CONTENTS OF GRAPHICS WINDOW
132;-------------------------------------------------------------------------------
133
[69]134;- Handle window devices (other than the Z buffer)
135if (!d.flags and 256) ne 0 then begin
[2]136
[69]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]
[2]143
[69]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
[2]150
[69]151endif
[2]152
153;- Read the pixmap contents into an array
[69]154if (depth gt 8) then begin
155  image = tvrd(order=0, true=1)
156endif else begin
157  image = tvrd(order=0)
158endelse
[2]159
[69]160;- Handle window devices (other than the Z buffer)
161if (!d.flags and 256) ne 0 then begin
[2]162
[69]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
[2]173
[69]174  ;- Delete the pixmap
175  wdelete, !d.window
176  wset, current_window
[2]177
[69]178endif
179
[2]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
[69]184if (depth le 8) then begin
[2]185  reduce_colors, image, index
186  r = r[index]
187  g = g[index]
188  b = b[index]
[69]189endif
[2]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
[69]201    if (depth gt 8) then begin
[2]202
203      ;- Convert 24-bit image to 8-bit
[69]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
[2]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)]
[69]219
[2]220    endif
[69]221
[2]222    ;- Save the image
223    case output of
[69]224      'GIF'  : write_gif,  file, image, r, g, b, MULTIPLE = multiple
[2]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
[69]229
[2]230  end
[69]231
[2]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
[69]236    if (depth le 8) then begin
237      info = size(image)
238      nx = info[1]
239      ny = info[2]
[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
[69]248    if (output eq 'TIFF') then image = reverse(temporary(image), 3)
249
[2]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
[69]255
[2]256  end
[69]257
[2]258endcase
259
260;- Print information for the user
[69]261if (keyword_set(quiet) eq 0) then $
262  print, file, output, format='("Created ",a," in ",a," format")'
[2]263
264END
Note: See TracBrowser for help on using the repository browser.