home *** CD-ROM | disk | FTP | other *** search
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % SSSSS U U N N 22222 PPPP SSSSS %
- % SS U U NN N 2 P P S %
- % SSS U U N N N 222 PPPP SSS %
- % SS U U N NN 2 P S %
- % SSSSS UUU N N 22222 P SSSSS %
- % %
- % %
- % Import SUN image to a postscript format. %
- % %
- % %
- % %
- % Software Design %
- % John Cristy %
- % January 1990 %
- % %
- % %
- % Copyright 1990 E. I. Dupont de Nemours & Company %
- % %
- % Permission to use, copy, modify, distribute, and sell this software and %
- % its documentation for any purpose is hereby granted without fee, %
- % provided that the above copyright notice appear in all copies and that %
- % both that copyright notice and this permission notice appear in %
- % supporting documentation, and that the name of E. I. Dupont de Nemours %
- % & Company not be used in advertising or publicity pertaining to %
- % distribution of the software without specific, written prior %
- % permission. E. I. Dupont de Nemours & Company makes no representations %
- % about the suitability of this software for any purpose. It is provided %
- % "as is" without express or implied warranty. %
- % %
- % E. I. Dupont de Nemours & Company disclaims all warranties with regard %
- % to this software, including all implied warranties of merchantability %
- % and fitness, in no event shall E. I. Dupont de Nemours & Company be %
- % liable for any special, indirect or consequential damages or any %
- % damages whatsoever resulting from loss of use, data or profits, whether %
- % in an action of contract, negligence or other tortious action, arising %
- % out of or in connection with the use or performance of this software. %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % The Sun2ps program reads a sun image and converts it to postscript format.
- % The image can then be printed on a postscript compatible printer in either
- % color or grayscale.
- %
- % The Sun2ps program command syntax is:
- %
- % Usage: sun2ps [options ...] file
- %
- % Where options include:
- % +grayscale print image as gray scale colors
- %
- % Specify 'file' as '-' for standard output.
- %
- % Change '+' to '-' in any option above to reverse its effect.
- % For example, -borders means do not include image borders).
- %
- %
- */
-
- #include <string.h>
- #include "display.h"
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C o m p r e s s I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function CompressImage compresses an image into runlength-encoded packets.
- %
- % The format of the CompressImage routine is:
- %
- % CompressImage(image)
- %
- % A description of each parameter follows:
- %
- % o image: The address of a structure of type Image.
- %
- %
- */
- unsigned int CompressImage(image)
- Image
- *image;
- {
- Image
- compressed_image;
-
- register int
- i;
-
- register RunlengthPacket
- *p;
-
- unsigned int
- InitializeChannel(),
- WritePacket();
-
- compressed_image.pixels=(RunlengthPacket *)
- malloc(image->packets*sizeof(RunlengthPacket));
- if (compressed_image.pixels == (RunlengthPacket *) NULL)
- {
- (void) fprintf(stderr,"Can't compress image, not enough memory.\n");
- return(False);
- }
- compressed_image.channel=InitializeChannel(compressed_image.pixels);
- compressed_image.packets=0;
- p=image->pixels;
- for (i=0; i < image->packets; i++)
- {
- compressed_image.packets+=WritePacket(compressed_image.channel,p->red,
- p->green,p->blue,p->index,p->length+1);
- p++;
- }
- free((char *) image->pixels);
- image->packets=compressed_image.packets;
- image->pixels=compressed_image.pixels;
- image->pixels=(RunlengthPacket *)
- realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
- return(True);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % P r i n t I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function PrintImage translates a MIFF image to postscript for printing.
- %
- % The format of the PrintImage routine is:
- %
- % status=PrintImage(image,grayscale)
- %
- % A description of each parameter follows:
- %
- % o status: Function PrintImage return True if the image is printed.
- % False is returned if the image file cannot be opened for printing.
- %
- % o image: The address of a structure of type Image; returned from
- % ReadImage.
- %
- % o grayscale: An integer that specifies a grayscale image if non-zero.
- %
- %
- */
- unsigned int PrintImage(image,grayscale)
- Image
- *image;
-
- unsigned int
- grayscale;
- {
- #define PointsPerInch 72.0
- #define PageBorder 0.25
- #define PageWidth 8.5
- #define PageHeight 11.0
-
- static char
- *commands[]=
- {
- "%!",
- "%",
- "% Display a runlength-encoded grayscale or color image.",
- "%",
- "/buffer 512 string def",
- "/byte 1 string def",
- "/color_packet 3 string def",
- "/gray_packet 1 string def",
- "/expanded_packet 768 string def",
- " ",
- "/ReadColorPacket",
- "{",
- " %",
- " % read a runlength-encoded color packet (length red green blue).",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /count exch 1 add def",
- " /count count 3 mul def",
- " currentfile color_packet readhexstring pop",
- " 0 3 count 1 sub",
- " {",
- " expanded_packet exch color_packet putinterval",
- " } for pop",
- " expanded_packet 0 count getinterval",
- "} def",
- " ",
- "/DisplayColorImage",
- "{",
- " %",
- " % display a runlength-encoded color image.",
- " %",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { ReadColorPacket } false 3 colorimage",
- "} def",
- " ",
- "/ReadGrayPacket",
- "{",
- " %",
- " % read a runlength-encoded grayscale packet (length gray).",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /count exch 1 add def",
- " currentfile gray_packet readhexstring pop",
- " 0 1 count 1 sub",
- " {",
- " expanded_packet exch gray_packet putinterval",
- " } for pop",
- " expanded_packet 0 count getinterval",
- "} def",
- " ",
- "/DisplayGrayImage",
- "{",
- " %",
- " % display a runlength-encoded grayscale image.",
- " %",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { ReadGrayPacket } image",
- "} def",
- " ",
- "/DisplayImage",
- "{",
- " %",
- " % display a runlength-encoded grayscale or color image.",
- " %",
- " initgraphics",
- " gsave",
- " currentfile buffer readline pop",
- " token { /degrees exch def } { } ifelse",
- " degrees rotate",
- " currentfile buffer readline pop",
- " token { /x exch def } { } ifelse",
- " token { /y exch def } { } ifelse",
- " x y translate",
- " currentfile buffer readline pop",
- " token { /x exch def } { } ifelse",
- " token { /y exch def } { } ifelse",
- " x y scale",
- " currentfile buffer readline pop",
- " currentfile buffer readline pop",
- " token { /columns exch def } { } ifelse",
- " token { /rows exch def } { } ifelse",
- " currentfile buffer readline pop",
- " token { /grayscale exch def } { } ifelse",
- " grayscale 0 gt { DisplayGrayImage } { DisplayColorImage } ifelse",
- " grestore",
- " showpage",
- "} def",
- " ",
- "%",
- "% DisplayImage parameters:",
- "% degrees rotation.",
- "% x & y translation.",
- "% x & y scale.",
- "% image name.",
- "% image columns & rows.",
- "% grayscale.",
- "% hex grayscale or color runlength-encoded packets.",
- "% ",
- "DisplayImage",
- NULL
- };
-
- char
- **q;
-
- double
- delta_x,
- delta_y,
- image_height,
- image_width,
- max,
- min,
- rotate,
- scale,
- scale_x,
- scale_y,
- translate_x,
- translate_y;
-
- register RunlengthPacket
- *p;
-
- register int
- i,
- j;
-
- register unsigned char
- blue,
- gray,
- green,
- red;
-
- /*
- Open output image file.
- */
- if (*image->filename == '-')
- image->file=stdout;
- else
- image->file=fopen(image->filename,"w");
- if (image->file == (FILE *) NULL)
- {
- (void) fprintf(stderr,"Can't open %s for printing\n",image->filename);
- return(False);
- }
- /*
- Compute image rotation.
- */
- if (((double) image->columns/(double) image->rows) > 1.0)
- rotate=(-90.0);
- else
- rotate=0.0;
- /*
- Compute image scaling.
- */
- image_width=(double) image->columns/PointsPerInch;
- image_height=(double) image->rows/PointsPerInch;
- /*
- Check max page sizes
- */
- max=image_width > image_height ? image_width : image_height;
- if (max > (PageHeight-(2.0*PageBorder)))
- {
- scale=(PageHeight-(2.0*PageBorder))/max;
- image_height*=scale;
- image_width*=scale;
- }
- min=image_width > image_height ? image_height : image_width;
- if (min > (PageWidth-(2.0*PageBorder)))
- {
- scale=(PageWidth-(2.0*PageBorder))/min;
- image_width*=scale;
- image_height*=scale;
- }
- scale_x=image_width*PointsPerInch;
- scale_y=image_height*PointsPerInch;
- translate_x=0.0;
- translate_y=0.0;
- if (rotate == 0.0)
- {
- delta_x=PageWidth-(image_width+(2.0*PageBorder));
- delta_y=PageHeight-(image_height+(2.0*PageBorder));
- if (delta_x >= 0.0)
- translate_x=((delta_x/2.0+PageBorder)* PointsPerInch);
- else
- translate_x=PageBorder*PointsPerInch;
- if (delta_y >= 0.0)
- translate_y=((delta_y/2.0+PageBorder)* PointsPerInch);
- else
- translate_y=PageBorder*PointsPerInch;
- }
- else
- {
- delta_x=PageHeight-(image_width+(2.0*PageBorder));
- delta_y=PageWidth-(image_height+(2.0*PageBorder));
- if (delta_x >= 0.0)
- translate_x=((delta_x/2.0+PageBorder-PageHeight)*PointsPerInch);
- else
- translate_x=(PageBorder-PageHeight)*PointsPerInch;
- if (delta_y >= 0.0)
- translate_y=((delta_y/2.0+PageBorder)*PointsPerInch);
- else
- translate_y=PageBorder*PointsPerInch;
- }
- /*
- Output postscript commands.
- */
- for (q=commands; *q; q++)
- (void) fprintf(image->file,"%s\n",*q);
- /*
- Output image data.
- */
- (void) fprintf(image->file,"%f\n%f %f\n%f %f\n%s\n%d %d\n%d\n",rotate,
- translate_x,translate_y,scale_x,scale_y,image->filename,image->columns,
- image->rows,grayscale);
- p=image->pixels;
- j=0;
- for (i=0; i < image->packets; i++)
- {
- j++;
- if (grayscale)
- {
- gray=Intensity(*p);
- (void) fprintf(image->file,"%02x%02x ",p->length,gray);
- if ((j % 6) == 0)
- (void) fprintf(image->file,"\n");
- }
- else
- {
- red=p->red;
- green=p->green;
- blue=p->blue;
- (void) fprintf(image->file,"%02x%02x%02x%02x ",p->length,red,green,
- blue);
- if ((j % 3) == 0)
- (void) fprintf(image->file,"\n");
- }
- p++;
- }
- (void) fprintf(image->file,"\n\n");
- if (image->file != stdin)
- (void) fclose(image->file);
- return(True);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % S u n R e a d I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Procedure SunReadImage reads a Sun raster image from a file on disk.
- %
- % The format of the SunReadImage routine is:
- %
- % SunReadImage(image);
- %
- % A description of each parameter follows.
- %
- % o image: Specifies a pointer to an Image structure.
- %
- %
- %
- */
- void SunReadImage(image)
- Image
- *image;
- {
- typedef struct _Rasterfile
- {
- int
- magic,
- width,
- height,
- depth,
- length,
- type,
- maptype,
- maplength;
- } Rasterfile;
-
- Rasterfile
- sun_header;
-
- register int
- bit,
- i,
- x,
- y;
-
- register RunlengthPacket
- *q;
-
- register unsigned char
- *p;
-
- unsigned char
- *sun_pixels;
-
- /*
- Open image file.
- */
- if (*image->filename == '-')
- image->file=stdin;
- else
- if (strcmp(image->filename+strlen(image->filename)-2,".Z"))
- image->file=fopen(image->filename,"r");
- else
- {
- char
- command[256];
-
- /*
- Image file is compressed-- uncompress it.
- */
- (void) sprintf(command,"uncompress -c %s",image->filename);
- image->file=popen(command,"r");
- }
- if (image->file == (FILE *) NULL)
- {
- (void) fprintf(stderr,
- "Can't continue, Sun raster image does not exist.\n");
- exit(1);
- }
- (void) fread((char *) &sun_header,1,sizeof(Rasterfile),image->file);
- if (sun_header.magic != 0x59a66a95)
- {
- (void) fprintf(stderr,"Can't continue, %s is not a Sun raster image.\n",
- image->filename);
- exit(1);
- }
- if (sun_header.type != 1)
- {
- (void) fprintf(stderr,
- "Can't continue, image is not in standard format.\n");
- exit(1);
- }
- if (sun_header.maplength > 0)
- {
- unsigned char
- *sun_colormap;
-
- /*
- Read Sun raster colormap.
- */
- image->colors=sun_header.maplength/3;
- image->colormap=(ColorPacket *)
- malloc(image->colors*sizeof(ColorPacket));
- sun_colormap=(unsigned char *)
- malloc(image->colors*sizeof(unsigned char));
- if ((image->colormap == (ColorPacket *) NULL) ||
- (sun_colormap == (unsigned char *) NULL))
- {
- (void) fprintf(stderr,"Can't continue, not enough memory.\n");
- exit(1);
- }
- (void) fread((char *) sun_colormap,1,(int) image->colors,image->file);
- for (i=0; i < image->colors; i++)
- image->colormap[i].red=sun_colormap[i];
- (void) fread((char *) sun_colormap,1,(int) image->colors,image->file);
- for (i=0; i < image->colors; i++)
- image->colormap[i].green=sun_colormap[i];
- (void) fread((char *) sun_colormap,1,(int) image->colors,image->file);
- for (i=0; i < image->colors; i++)
- image->colormap[i].blue=sun_colormap[i];
- (void) free((char *) sun_colormap);
- }
- else
- if (sun_header.depth < 24)
- {
- /*
- Create linear color ramp.
- */
- image->colors=1 << sun_header.depth;
- image->colormap=(ColorPacket *)
- malloc(image->colors*sizeof(ColorPacket));
- if (image->colormap == (ColorPacket *) NULL)
- {
- (void) fprintf(stderr,"Can't continue, not enough memory.\n");
- exit(1);
- }
- for (i=0; i < image->colors; i++)
- {
- image->colormap[i].red=(255*i)/(image->colors-1);
- image->colormap[i].green=(255*i)/(image->colors-1);
- image->colormap[i].blue=(255*i)/(image->colors-1);
- }
- }
- sun_pixels=(unsigned char *)
- malloc((unsigned int) sun_header.length*sizeof(unsigned char));
- if (sun_pixels == (unsigned char *) NULL)
- {
- (void) fprintf(stderr,"Can't continue, not enough memory.\n");
- exit(1);
- }
- (void) fread((char *) sun_pixels,1,sun_header.length,image->file);
- /*
- Create image.
- */
- image->comments=(char *) malloc((unsigned int) (strlen(image->filename)+256));
- if (image->comments == (char *) NULL)
- {
- (void) fprintf(stderr,
- "Can't import Sun raster image, not enough memory.\n");
- exit(1);
- }
- (void) strcpy(image->comments," Imported from Sun raster image: ");
- (void) strcat(image->comments,image->filename);
- image->class=(sun_header.depth < 24 ? PseudoClass : DirectClass);
- image->compression=RunlengthEncodedCompression;
- image->scene=0;
- image->columns=sun_header.width;
- image->rows=sun_header.height;
- image->packets=image->columns*image->rows;
- image->pixels=(RunlengthPacket *)
- malloc(image->packets*sizeof(RunlengthPacket));
- if (image->pixels == (RunlengthPacket *) NULL)
- {
- (void) fprintf(stderr,"Can't continue, not enough memory.\n");
- exit(1);
- }
- /*
- Convert Sun raster image to runlength-encoded packets.
- */
- p=sun_pixels;
- q=image->pixels;
- if (sun_header.depth == 1)
- for (y=0; y < image->rows; y++)
- {
- /*
- Convert bitmap scanline to runlength-encoded color packets.
- */
- for (x=0; x < (image->columns >> 3); x++)
- {
- for (bit=7; bit >= 0; bit--)
- {
- q->index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
- q->red=image->colormap[q->index].red;
- q->green=image->colormap[q->index].green;
- q->blue=image->colormap[q->index].blue;
- q->length=0;
- q++;
- }
- p++;
- }
- if (image->columns % 8)
- {
- for (bit=7; bit >= (8-(image->columns % 8)); bit--)
- {
- q->index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
- q->red=image->colormap[q->index].red;
- q->green=image->colormap[q->index].green;
- q->blue=image->colormap[q->index].blue;
- q->length=0;
- q++;
- }
- p++;
- }
- }
- else
- if (image->class == PseudoClass)
- for (y=0; y < image->rows; y++)
- {
- /*
- Convert PseudoColor scanline to runlength-encoded color packets.
- */
- for (x=0; x < image->columns; x++)
- {
- q->index=(*p++);
- q->red=image->colormap[q->index].red;
- q->green=image->colormap[q->index].green;
- q->blue=image->colormap[q->index].blue;
- q->length=0;
- q++;
- }
- if (image->columns % 2)
- p++;
- }
- else
- for (y=0; y < image->rows; y++)
- {
- /*
- Convert DirectColor scanline to runlength-encoded color packets.
- */
- for (x=0; x < image->columns; x++)
- {
- q->red=(*p++);
- q->green=(*p++);
- q->blue=(*p++);
- q->index=0;
- q->length=0;
- q++;
- }
- if (image->columns % 2)
- p++;
- }
- (void) free((char *) sun_pixels);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % U s a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Procedure Usage displays the program usage;
- %
- % The format of the Usage routine is:
- %
- % Usage(program_name,message)
- %
- % A description of each parameter follows:
- %
- % program_name: Specifies the name of this program.
- %
- % message: Specifies a specific message to display to the user.
- %
- */
- void Usage(program_name,message)
- char
- *message,
- *program_name;
- {
- char
- **p;
-
- static char
- *options[]=
- {
- "+grayscale print image as grayscale",
- NULL
- };
- if (message)
- (void) fprintf(stderr,"Can't continue, %s\n\n",message);
- (void) fprintf(stderr,"Usage: %s [options ...] file\n",program_name);
- (void) fprintf(stderr,"\nWhere options include:\n");
- for (p=options; *p; *p++)
- (void) fprintf(stderr," %s\n",*p);
- (void) fprintf(stderr,
- "\nSpecify 'file' as '-' for standard output.\n");
- (void) fprintf(stderr,
- "\nChange '+' to '-' in any option above to reverse its effect.\n");
- (void) fprintf(stderr,
- "For example, -grayscale means print the image in color.\n");
- exit(1);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M a i n %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- */
- main(argc,argv)
- int
- argc;
-
- char
- *argv[];
- {
- char
- *option,
- *program_name;
-
- Image
- image;
-
- int
- i;
-
- unsigned int
- grayscale;
-
- program_name=argv[0];
- grayscale=False;
- for (i=1; i < argc-1; i++)
- {
- option=argv[i];
- if ((strlen(option) > 1) && ((*option == '-') || (*option == '+')))
- switch(*(option+1))
- {
- case 'g':
- {
- grayscale=(*option == '+');
- break;
- }
- case 'h':
- {
- Usage(program_name,(char *) NULL);
- break;
- }
- default:
- Usage(program_name,(char *) NULL);
- }
- }
- if (argc == 1)
- Usage(program_name,(char *) NULL);
- (void) strcpy(image.filename,argv[argc-1]);
- SunReadImage(&image);
- (void) CompressImage(&image);
- (void) strcpy(image.filename,"-");
- (void) fprintf(stderr,"%s %dx%d\n",image.filename,image.columns,image.rows);
- (void) PrintImage(&image,grayscale);
- }
-