/* * TransFig: Facility for Translating Fig code * This routine is from PSencode.c, in the xwpick package by: * E.Chernyaev (IHEP/Protvino) * Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul * Parts Copyright (c) 1989-2002 by Brian V. Smith * * Any party obtaining a copy of these files is granted, free of charge, a * full and unrestricted irrevocable, world-wide, paid up, royalty-free, * nonexclusive right and license to deal in this software and * documentation files (the "Software"), including without limitation the * rights to use, copy, modify, merge, publish and/or distribute copies of * the Software, and to permit persons who receive copies from any such * party to do so, with the only requirement being that this copyright * notice remain intact. * */ #include "fig2dev.h" #define MAXWIDTH 8192 #define put_string nc=strlen(s); for(i=0;i> def", " /tinteger exch def", " /height exch def", " /width exch def", " /transparent 1 string def", " transparent 0 tinteger put", " olddict /ImageMatrix [width 0 0 height neg 0 height] put", " /newdict olddict maxlength dict def", " olddict newdict copy pop", " newdict /Width width put", " newdict /Height height put", " /w newdict /Width get def", " /str w string def", " /substrlen 2 w log 2 log div floor exp cvi def", " /substrs [", " {", " substrlen string", " 0 1 substrlen 1 sub {", " 1 index exch tinteger put", " } for", " /substrlen substrlen 2 idiv def", " substrlen 0 eq {exit} if", " } loop", " ] def", " /h newdict /Height get def", " 1 w div 1 h div matrix scale", " olddict /ImageMatrix get exch matrix concatmatrix", " matrix invertmatrix concat", " newdict /Height 1 put", " newdict /DataSource str put", " /mat [w 0 0 h 0 0] def", " newdict /ImageMatrix mat put", " 0 1 h 1 sub {", " mat 5 3 -1 roll neg put", " % get Width bytes from the image via the RLE decoder", " 0 1 w 1 sub { str exch RLEPacket putinterval } for", " /tail str def", " /x 0 def", " {", " tail transparent search dup /done exch not def", " {exch pop exch pop} if", " /w1 1 index length def", " w1 0 ne {", " newdict /DataSource 3 -1 roll put", " newdict /Width w1 put", " mat 4 x neg put", " /x x w1 add def", " newdict image", " /tail tail w1 tail length w1 sub getinterval def", " } {", " pop", " } ifelse", " done {exit} if", " tail substrs {", " anchorsearch {pop} if", " } forall", " /tail exch def", " tail length 0 eq {exit} if", " /x w tail length sub def", " } loop", " } for", " end", " grestore", "} bind def", "", "% number of bytes remaining in current RLE run", "/Nbyte 0 def", "% color of current RLE run", "/color 1 string def", "", "/byte 1 string def", "", "%***************************************************", "/RLEPacket % Decode RLE color packet *", "%***************************************************", "{", " Nbyte 0 eq {", " currentfile byte readhexstring pop 0 get", " /Nbyte exch 1 add def ", " currentfile color readhexstring pop pop", " } if", " /Nbyte Nbyte 1 sub def", " color ", "} bind def", NULL }; for (str=Transpheader; *str; str++) { fprintf(tfp,"%s\n",*str); } /* set flag saying we've emitted the header */ transp_header_done = True; } /*********************************************************************** * * * Name: PSencode Date: 13.01.93 * * * * Function: Output image in PostScript format (runlength encoding) * * * * Input: Width - image width * * Height - image height * * Transparent - index of transparent color (-1 if none) * * Ncol - number of colors * * R[] - red components * * G[] - green components * * B[] - blue components * * data[] - array for image data (byte per pixel) * * * * Return: size of PS * * * ***********************************************************************/ long PSencode(Width, Height, Transparent, Ncol, R, G, B, data) int Width, Height, Transparent, Ncol; byte R[], G[], B[]; unsigned char *data; { long Nbyte; char s[80]; byte *ptr, *end; int i, nc, k, current, previous, run, y; static char h[] = "0123456789abcdef"; /* CHECK PARAMETERS */ if (Width <= 0 || Width > MAXWIDTH || Height <= 0 || Height > MAXWIDTH) { fprintf(stderr, "\nIncorrect image size: %d x %d\n", Width, Height); return 0; } if (Ncol <= 0 || Ncol > 256) { fprintf(stderr,"\nWrong number of colors: %d\n", Ncol); return 0; } if (Transparent != -1) { fprintf(tfp,"[/Indexed /DeviceRGB %d <\n",Ncol-1); } else { fprintf(tfp, "%%***********************************************\n"); fprintf(tfp, "%%* Image decoding *\n"); fprintf(tfp, "%%***********************************************\n"); fprintf(tfp, "DisplayImage\n"); fprintf(tfp,"%d %d\n", Width, Height); fprintf(tfp,"%d\n",Ncol); } Nbyte = 0; /* colormap */ for (k=0; k ] setcolorspace\n"); fprintf(tfp,"%d %d %d transparentimage\n",Width,Height,Transparent); } /* RUN-LENGTH COMPRESSION */ run = 0; nc = 0; s[72] = '\n'; s[73] = '\0'; for(y=0; y