19 #define FILE_BUFFER_CAPACITY (1024*4)
33 int XMin,
int YMin,
int XMax,
int YMax)
37 if(!(File = fopen(FileName,
"wb")))
49 "%%%%BoundingBox: 0 0 %d %d\n"
50 "<< /PageSize [%d %d] >> setpagedevice\n", XMax, YMax, XMax, YMax) < 0
51 || ((XMin != 0 || YMin != 0) && fprintf(File,
52 "gsave %d %d translate\n", -XMin, -YMin) < 0)
73 || fprintf(File,
"showpage\n") < 0
114 "systemdict /setdistillerparams known {\n"
115 "<< %s >> setdistillerparams\n"
116 "} if\n", Params) < 0)
129 static int WriteASCII85(FILE *File,
const uint8_t *Data,
int NumBytes)
131 unsigned long Tuple, Plain[4];
132 unsigned int Encoded[5];
133 int i, k, LineCount, Padding;
137 for(i = 0, LineCount = 0; i < NumBytes; i += 4)
139 for(k = 0; k < 4; k++)
140 Plain[k] = Data[i + k];
142 Tuple = (Plain[0] << 24) | (Plain[1] << 16)
143 | (Plain[2] << 8) | Plain[3];
145 for(k = 4; k >= 0; k--)
147 Encoded[k] = Tuple % 85;
151 for(k = 0; k < 5; k++)
152 fputc(33 + Encoded[k], File);
155 if(++LineCount >= 15)
159 if(fprintf(File,
"\n") < 0)
167 for(k = 0; i + k < NumBytes; k++)
168 Plain[k] = Data[i + k];
170 for(Padding = 0; k < 4; k++, Padding++)
173 Tuple = (Plain[0] << 24) | (Plain[1] << 16)
174 | (Plain[2] << 8) | Plain[3];
176 for(k = 4; k >= 0; k--)
178 Encoded[k] = Tuple % 85;
182 for(k = 0; k < 5 - Padding; k++)
183 fputc(33 + Encoded[k], File);
186 && fprintf(File,
"\n") < 0)
190 if(fprintf(File,
"~>\n") < 0 || ferror(File))
216 const uint8_t *Image,
int Width,
int Height)
219 if(!File || !Image || fprintf(File,
221 "/DeviceGray setcolorspace\n"
222 "0 %d translate %d %d scale\n"
226 " /ImageMatrix [%d 0 0 -%d 0 0]\n"
227 " /BitsPerComponent 8\n"
229 " /DataSource currentfile /ASCII85Decode filter\n"
230 " /Interpolate false\n"
232 Height, Width, Height,
233 Width, Height, Width, Height) < 0
234 || !WriteASCII85(File, Image, Width*Height)
235 || fprintf(File,
"grestore\n") < 0)
261 const uint8_t *Image,
int Width,
int Height)
264 if(!File || !Image || fprintf(File,
266 "/DeviceRGB setcolorspace\n"
267 "0 %d translate %d %d scale\n"
271 " /ImageMatrix [%d 0 0 -%d 0 0]\n"
272 " /BitsPerComponent 8\n"
273 " /Decode [0 1 0 1 0 1]\n"
274 " /DataSource currentfile /ASCII85Decode filter\n"
275 " /Interpolate false\n"
277 Height, Width, Height,
278 Width, Height, Width, Height) < 0
279 || !WriteASCII85(File, Image, 3*Width*Height)
280 || fprintf(File,
"grestore\n") < 0)