1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
## Copyright (C) 1996 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
## Save a matrix to disk in image format.
##
## saveimage (filename, x) saves matrix x to file filename in octave's
## image format. The current colormap is saved in the file also.
##
## saveimage (filename, x, "img") saves the image in the default format
## and is the same as saveimage (filename, x).
##
## saveimage (filename, x, "ppm") saves the image in ppm format instead
## of the default octave image format.
##
## saveimage (filename, x, "ps") saves the image in PostScript format
## instead of the default octave image format. (Note: images saved in
## PostScript format can not be read back into octave with loadimage.)
##
## saveimage (filename, x, format, map) saves the image along with the
## specified colormap in the specified format.
##
## Note: If the colormap contains only two entries and these entries
## are black and white, the bitmap ppm and PostScript formats are used.
## If the image is a gray scale image (the entries within each row of
## the colormap are equal) the gray scale ppm and PostScript image
## formats are used, otherwise the full color formats are used.
##
## The conversion to PostScript is based on pbmtolps.c, which was
## written by
##
## George Phillips <phillips@cs.ubc.ca>
## Department of Computer Science
## University of British Columbia
##
## and is part of the portable bitmap utilities,
##
## SEE ALSO: loadimage, save, load, colormap
## Author: Tony Richardson <arichard@stark.cc.oh.us>
## Created: July 1994
## Adapted-By: jwe
## Rewritten by jwe to avoid using octoppm and pbm routines so that
## people who don't have the the pbm stuff installed can still use this
## function.
##
## The conversion to PostScript is based on pnmtops.c, which is part of
## the portable bitmap utilties and bears this copyright notice:
##
## Copyright (C) 1989 by Jef Poskanzer.
##
## Permission to use, copy, modify, and distribute this software and its
## documentation for any purpose and without fee is hereby granted, provided
## that the above copyright notice appear in all copies and that both that
## copyright notice and this permission notice appear in supporting
## documentation. This software is provided "as is" without express or
## implied warranty.
function saveimage (filename, img, img_form, map)
if (nargin < 2 || nargin > 4)
usage ("saveimage (filename, matrix, [format, [colormap]])");
endif
if (nargin < 4)
map = colormap ();
endif
[map_nr, map_nc] = size (map);
if (map_nc != 3)
error ("colormap should be an N x 3 matrix");
endif
if (nargin < 3)
img_form = "img";
elseif (! isstr (img_form))
error ("image format specification must be a string");
elseif (! (strcmp (img_form, "img")
|| strcmp (img_form, "ppm")
|| strcmp (img_form, "ps")))
error ("unsupported image format specification");
endif
if (! is_matrix (img))
warning ("image variable is not a matrix");
endif
if (! isstr (filename))
error ("file name must be a string");
endif
## If we just want Octave image format, save and return.
if (strcmp (img_form, "img"))
eval (strcat ("save -ascii ", filename, " map img"));
return;
endif
## Convert to another format if requested.
grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3));
pbm = pgm = ppm = 0;
map_sz = map_nr * map_nc;
map = reshape (map, map_sz, 1);
idx = find (map > 1);
map (idx) = ones (size (idx));
idx = find (map < 0);
map (idx) = zeros (size (idx));
map = round (255 * map);
bw = (map_nr == 2
&& ((map(1,1) == 0 && map(2,1) == 255)
|| (map(1,1) == 255 && map(2,1) == 0)));
img = round (img');
[img_nr, img_nc] = size (img);
img_sz = img_nr * img_nc;
img = reshape (img, img_sz, 1);
idx = find (img > map_nr);
img (idx) = ones (size (idx)) * map_nr;
idx = find (img <= 0);
img (idx) = ones (size (idx));
if (strcmp (img_form, "ppm"))
if (grey && map_nr == 2 && bw)
if (map(1) != 0)
map = [1; 0];
else
map = [0; 1];
endif
n_long = rem (img_nc, 8);
tmp = zeros (ceil (img_nc/8), img_nr);
for i = 1:img_nr
idx = (i-1)*img_nc+1:i*img_nc;
if (n_long > 0)
img_row = [map(img(idx)); (zeros (8-n_long, 1))];
else
img_row = map(img(idx));
endif
if (img_nc < 8)
for j = 1:8
tmp(:,i) = tmp(:,i) + img_row (j) * 2^(8-j);
endfor
else
for j = 1:8
tmp(:,i) = tmp(:,i) + img_row (j:8:img_nc) * 2^(8-j);
endfor
endif
endfor
fid = fopen (filename, "w");
fprintf (fid, "P4\n%d %d\n", img_nr, img_nc);
fwrite (fid, tmp, "uchar");
fprintf (fid, "\n");
fclose (fid);
elseif (grey)
fid = fopen (filename, "w");
fprintf (fid, "P5\n%d %d\n255\n", img_nr, img_nc);
fwrite (fid, map(img), "uchar");
fprintf (fid, "\n");
fclose (fid);
else
img_idx = ((1:3:3*img_sz)+2)';
map_idx = ((2*map_nr+1):map_sz)';
tmap = map(map_idx);
tmp(img_idx--) = tmap(img);
map_idx = map_idx - map_nr;
tmap = map(map_idx);
tmp(img_idx--) = tmap(img);
map_idx = map_idx - map_nr;
tmap = map(map_idx);
tmp(img_idx--) = tmap(img);
fid = fopen (filename, "w");
fprintf (fid, "P6\n%d %d\n255\n", img_nr, img_nc);
fwrite (fid, tmp, "uchar");
fprintf (fid, "\n");
fclose (fid);
endif
elseif (strcmp (img_form, "ps") == 1)
if (! grey)
error ("must have a greyscale color map for conversion to PostScript");
endif
bps = 8;
dpi = 300;
pagewid = 612;
pagehgt = 762;
MARGIN = 0.95;
devpix = dpi / 72.0 + 0.5;
pixfac = 72.0 / dpi * devpix;
## Compute padding to round cols * bps up to the nearest multiple of 8
## (nr and nc are switched because we transposed the image above).
padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps;
scols = img_nr * pixfac;
srows = img_nc * pixfac;
scale = 1;
if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN)
if (scols > pagewid * MARGIN)
scale = scale * (pagewid / scols * MARGIN);
scols = scale * img_nr * pixfac;
srows = scale * img_nc * pixfac;
endif
if (srows > pagehgt * MARGIN)
scale = scale * (pagehgt / srows * MARGIN);
scols = scale * img_nr * pixfac;
srows = scale * img_nc * pixfac;
endif
warning ("image too large for page, rescaling to %g", scale);
endif
llx = (pagewid - scols) / 2;
lly = (pagehgt - srows) / 2;
urx = llx + fix (scols + 0.5);
ury = lly + fix (srows + 0.5);
fid = fopen (filename, "w");
fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf (fid, "%%%%Creator: Octave %s (saveimage.m)\n", OCTAVE_VERSION);
fprintf (fid, "%%%%Title: %s\n", filename);
fprintf (fid, "%%%%Pages: 1\n");
fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n",
fix (llx), fix (lly), fix (urx), fix (ury));
fprintf (fid, "%%%%EndComments\n" );
fprintf (fid, "/readstring {\n");
fprintf (fid, " currentfile exch readhexstring pop\n");
fprintf (fid, "} bind def\n");
fprintf (fid, "/picstr %d string def\n",
fix ((img_nr + padright) * bps / 8));
fprintf (fid, "%%%%EndProlog\n");
fprintf (fid, "%%%%Page: 1 1\n");
fprintf (fid, "gsave\n");
fprintf (fid, "%g %g translate\n", llx, lly);
fprintf (fid, "%g %g scale\n", scols, srows);
fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps);
fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc);
fprintf (fid, "{ picstr readstring }\n" );
fprintf (fid, "image\n" );
img = map(img);
fmt = "%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x\n";
fprintf (fid, fmt, img);
if (rem (img_sz, 30) != 0)
fprintf (fid, "\n" );
endif
fprintf (fid, "grestore\n" );
fprintf (fid, "showpage\n" );
fprintf (fid, "%%%%Trailer\n" );
fclose (fid);
else
error ("saveimage: what happened to the image type?");
endif
endfunction
|