1 #   include	"bitmapConfig.h"
2 
3 #   include	<stdlib.h>
4 #   include	<stdio.h>
5 #   include	<string.h>
6 
7 #   include	<sioFileio.h>
8 
9 #   include	"bmintern.h"
10 #   include	"bmBitmapPrinter.h"
11 
12 #   include	<psPrint.h>
13 #   include	<utilMemoryBufferPrintf.h>
14 
15 #   include	<appDebugon.h>
16 
17 /************************************************************************/
18 /*									*/
19 /*  We do our best to print anything.					*/
20 /*									*/
21 /************************************************************************/
22 
bmCanWriteEpsFile(const BitmapDescription * bd,int privateFormat)23 int bmCanWriteEpsFile(	const BitmapDescription *	bd,
24 			int				privateFormat )
25     {
26     switch( bd->bdBitsPerSample )
27 	{
28 	case 1: case 2: case 4: case 8: case 16:
29 	    return 0;
30 	default:
31 	    /* LDEB(bd->bdBitsPerSample); */ return -1;
32 	}
33     }
34 
35 
36 /************************************************************************/
37 /*									*/
38 /*  Save a bitmap image to Encapsulated PostScript (EPS) Format.	*/
39 /*									*/
40 /*  1)  Only 1,2,4,8,16 bits per sample images are supported.		*/
41 /*  2)  Open the output file.						*/
42 /*  3)  Determine the bounding box of the image.			*/
43 /*  4)  Issue the EPS header comments.					*/
44 /*  5)  Issue the Image contents.					*/
45 /*  6)  Include a 'showpage' command.					*/
46 /*  7)  Finish with a general header.					*/
47 /*									*/
48 /************************************************************************/
49 
bmWriteEps(SimpleOutputStream * sos,const char * filename,const unsigned char * buffer,const BitmapDescription * bd,int privateFormat)50 static int bmWriteEps(	SimpleOutputStream *		sos,
51 			const char *			filename,
52 			const unsigned char *		buffer,
53 			const BitmapDescription *	bd,
54 			int				privateFormat )
55     {
56     int				useFilters= 1;
57     int				indexedImages= 1;
58 
59     if  ( privateFormat < 2 )
60 	{ useFilters= indexedImages= 0;	}
61 
62     /*  1  */
63     switch( bd->bdBitsPerSample )
64 	{
65 	case 1:
66 	case 2:
67 	case 4:
68 	case 8:
69 	case 16:
70 	    break;
71 
72 	default:
73 	    LDEB(bd->bdBitsPerSample); return -1;
74 	}
75 
76     bmStartEpsFile( sos, filename, bd );
77 
78     psSetPdfmarkEmulation( sos );
79     if  ( useFilters )
80 	{ psImageQualityDistillerparams( sos );     }
81 
82     /*  5  */
83     if  ( bmPsPrintBitmap( sos, privateFormat, 1.0, 1.0, 0, 0,
84 				    (const DocumentRectangle *)0,
85 				    useFilters, indexedImages, bd, buffer ) )
86 	{ LDEB(1); return -1;	}
87 
88     psFinishEpsFile( sos );
89 
90     return 0;
91     }
92 
bmWriteEpsFile(const MemoryBuffer * filename,const unsigned char * buffer,const BitmapDescription * bd,int privateFormat)93 int bmWriteEpsFile(	const MemoryBuffer *		filename,
94 			const unsigned char *		buffer,
95 			const BitmapDescription *	bd,
96 			int				privateFormat )
97     {
98     int				rval= 0;
99     SimpleOutputStream *	sos;
100 
101     char *			psLevel= getenv( "PSLEVEL" );
102 
103     if  ( psLevel && ! strcmp( psLevel, "2" ) )
104 	{ privateFormat= 2;	}
105 
106     /*  2  */
107     sos= sioOutFileioOpen( filename );
108     if  ( ! sos )
109 	{ XDEB(sos); rval= -1; goto ready;	}
110 
111     if  ( bmWriteEps( sos, utilMemoryBufferGetString( filename ), buffer, bd, privateFormat ) )
112 	{ LDEB(1); rval= -1; goto ready;	}
113 
114   ready:
115 
116     sioOutClose( sos );
117 
118     return rval;
119     }
120 
bmStartEpsFile(SimpleOutputStream * sos,const char * filename,const BitmapDescription * bd)121 void bmStartEpsFile(	SimpleOutputStream *		sos,
122 			const char *			filename,
123 			const BitmapDescription *	bd )
124     {
125     int			twipsWide;
126     int			twipsHigh;
127 
128     /*  3  */
129     bmImageSizeTwips( &twipsWide, &twipsHigh, bd );
130 
131     /*  4  */
132     psStartEpsFile( sos,
133 		    "Scan, Mark de Does, mark@mdedoes.com",
134 		    filename,
135 		    ( twipsWide+ 19 )/ 20, ( twipsHigh+ 19 )/ 20	);
136 
137     return;
138     }
139 
140 /************************************************************************/
141 
142 # include <sioPipe.h>
143 
bmCanWritePdfFile(const BitmapDescription * bd,int privateFormat)144 int bmCanWritePdfFile(	const BitmapDescription *	bd,
145 			int				privateFormat )
146     { return bmCanWriteEpsFile( bd, 2 );	}
147 
bmWritePdfFile(const MemoryBuffer * filename,const unsigned char * buffer,const BitmapDescription * bd,int privateFormat)148 int bmWritePdfFile(	const MemoryBuffer *		filename,
149 			const unsigned char *		buffer,
150 			const BitmapDescription *	bd,
151 			int				privateFormat )
152     {
153     int				rval= 0;
154     SimpleOutputStream *	sos= (SimpleOutputStream *)0;
155     int				rectangleWideTwips;
156     int				rectangleHighTwips;
157 
158     MemoryBuffer		command;
159 
160     utilInitMemoryBuffer( &command );
161 
162     if  ( strchr( (char *)filename->mbBytes, '\'' )	||
163 	  filename->mbSize < 1				||
164 	  filename->mbBytes[filename->mbSize-1] == '\\'	)
165 	{ LDEB(1); rval= -1; goto ready;	}
166 
167     bmRectangleSizeTwips( &rectangleWideTwips, &rectangleHighTwips,
168 				    bd, bd->bdPixelsWide, bd->bdPixelsHigh );
169 
170     utilMemoryBufferPrintf( &command,
171 	    "ps2pdf -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d - '%s'",
172 				( rectangleWideTwips+ 19)/20,
173 				( rectangleHighTwips+ 19)/20,
174 				utilMemoryBufferGetString( filename ) );
175 
176     sos= sioOutPipeOpen( &command );
177     if  ( ! sos )
178 	{ XDEB(sos); rval= -1; goto ready;	}
179 
180     if  ( bmWriteEps( sos, utilMemoryBufferGetString( filename ), buffer, bd, 2 ) )
181 	{ LDEB(1); rval= -1; goto ready;	}
182 
183   ready:
184 
185     utilCleanMemoryBuffer( &command );
186 
187     if  ( sos )
188 	{
189 	if  ( sioOutClose( sos ) )
190 	    { LDEB(1); rval= -1;	}
191 	}
192 
193     return rval;
194     }
195