# HG changeset patch # User jwe # Date 812710864 0 # Node ID 79ca6398b78eb15a958a51847aa38579cd1192e8 # Parent 3a66934240ea882e2c92b34478befcc6cf423447 [project @ 1995-10-03 08:56:53 by jwe] diff -r 3a66934240ea -r 79ca6398b78e scripts/image/Makefile.in --- a/scripts/image/Makefile.in Tue Oct 03 03:26:13 1995 +0000 +++ b/scripts/image/Makefile.in Tue Oct 03 09:01:04 1995 +0000 @@ -31,7 +31,7 @@ IMAGE_FILES = $(wildcard $(srcdir)/*.img) IMAGE_FILES_NO_DIR = $(notdir $(IMAGE_FILES)) -all: octtopnm +all: .PHONY: all octtopnm: octtopnm.o @@ -49,7 +49,7 @@ $(INSTALL_DATA) $(srcdir)/$$f $(imagedir)/$$f ; \ done $(top_srcdir)/mkinstalldirs $(archlibdir) - $(INSTALL_PROGRAM) octtopnm $(archlibdir)/octtopnm +# $(INSTALL_PROGRAM) octtopnm $(archlibdir)/octtopnm .PHONY: install uninstall: @@ -59,7 +59,7 @@ for f in $(IMAGE_FILES_NO_DIR) ; \ do rm -f $(imagedir)/$$f ; \ done - rm -f $(archlibdir)/octtopnm +# rm -f $(archlibdir)/octtopnm .PHONY: uninstall clean: diff -r 3a66934240ea -r 79ca6398b78e scripts/image/octtopnm.c --- a/scripts/image/octtopnm.c Tue Oct 03 03:26:13 1995 +0000 +++ b/scripts/image/octtopnm.c Tue Oct 03 09:01:04 1995 +0000 @@ -197,13 +197,13 @@ for(row=0; row +# Department of Computer Science +# University of British Columbia +# +# and is part of the portable bitmap utilities, +# # SEE ALSO: loadimage, save, load, colormap # Written by Tony Richardson (amr@mpl.ucsd.edu) July 1994. +# 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. + if (nargin < 2 || nargin > 4) usage ("saveimage (filename, matrix, [format, [colormap]])"); endif @@ -53,7 +78,10 @@ if (nargin < 4) map = colormap (); endif - if (columns (map) != 3) + + [map_nr, map_nc] = size (map); + + if (map_nc != 3) error ("colormap should be an N x 3 matrix"); endif @@ -64,10 +92,10 @@ elseif (! (strcmp (img_form, "img") || strcmp (img_form, "ppm") || strcmp (img_form, "ps"))) - error ("unsupported image format specification"); + error ("unsupported image format specification"); endif - if (! is_matrix (X)) + if (! is_matrix (img)) warning ("image variable is not a matrix"); endif @@ -78,31 +106,202 @@ # If we just want Octave image format, save and return. if (strcmp (img_form, "img")) - eval (strcat ("save -ascii ", filename, " map X")); + eval (strcat ("save -ascii ", filename, " map img")); return; endif - unwind_protect - - oct_file = octave_tmp_file_name (); - -# Save image in Octave image file format - - eval (strcat ("save -ascii ", oct_file, " map X")); - # Convert to another format if requested. - if (strcmp (img_form, "ppm")) - shell_cmd (sprintf ("octtopnm %s > %s", oct_file, filename)); - elseif (strcmp (img_form, "ps") == 1) - shell_cmd (sprintf ("octtopnm %s | pnmtops > %s 2> /dev/null", - oct_file, filename)); + grey = ! (any (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 = img'; + [img_nr, img_nc] = size (img); + + img_sz = img_nr * img_nc; + img = reshape (img, img_sz, 1); + + idx = find (img >= map_nr - 1); + 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) + + map = map(:,1); + + if (map(1) != 0) + map(1) = 1; + else + map(2) = 1; + endif + + img = map(img); + n_long = rem (img_sz, 8); + if (n_long == 0) + n_long = 8; + else + n_long = 1 + nlong; + endif + + idx = 1:8:img_sz; + s_len = length (idx) - 1; + + tmp = img (1:8:img_sz) * 128; + for i = 2:n_long + tmp = tmp + img (i:8:img_sz) * 2^(8-i); + endfor + size (tmp) + for i = (n_long+1):8 + tmp(1:s_len) = tmp(1:s_len) + img (i:8:img_sz) * 2^(8-i); + endfor + + fid = fopen (filename, "w"); + fprintf (fid, "P4\n%d %d\n", img_nr, img_nc); + fwrite (fid, tmp, "char"); + 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 - unwind_protect_cleanup + 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; + + 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"); - shell_cmd (sprintf ("rm -f %s", oct_file)); + fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); + fprintf (fid, "%%%%Creator: pnmtops\n"); + 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" ); - end_unwind_protect + img = map(img); + +# XXX FIXME XXX -- this would be much faster if fprintf knew about +# vector arguments. + + count = 0; + for i = 1:img_sz + fprintf (fid, "%x", img (i)) + if (++count == 30) + count = 0; + fprintf (fid, "\n"); + endif + endfor + + fprintf (fid, "\n" ); + 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 + +