~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/X11/epsf_dump.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Create Encapsulated PostScript Files
  2  */
  3 #include <tina/sys.h>
  4 #include <tina/math.h>
  5 #include <tina/vision.h>
  6 #include <tina/tv.h>
  7 #include <tina/tvfuncs.h>
  8 #include <tina/file.h>
  9 #include <tina/draw.h>
 10 #include <tina/tv_screen.h>
 11 
 12 static void tv_screen_epsf_dump_bw(Tv_screen *, char *);
 13 static void tv_screen_epsf_dump_1bpp(Tv_screen *, char *);
 14 static void tv_screen_epsf_dump_color(Tv_screen *, char *);
 15 static void (*epsf_dump) (Tv_screen *, char *) = tv_screen_epsf_dump_color;
 16 static int no_of_bytes_written = 0;
 17 
 18 
 19 static void set_epsf_dump_type(Dump_type type)
 20 {
 21     switch (type)
 22     {
 23         case DT_BW:
 24         epsf_dump = tv_screen_epsf_dump_bw;
 25         break;
 26     case DT_COLOR:
 27         epsf_dump = tv_screen_epsf_dump_color;
 28         break;
 29     case DT_ONE_BIT_PER_PIXEL:
 30         epsf_dump = tv_screen_epsf_dump_1bpp;
 31         break;
 32     }
 33 }
 34 
 35 static void write_byte(FILE * out, int byte)
 36 {
 37 
 38     (void) fprintf(out, "%2.2x", byte);
 39     no_of_bytes_written++;
 40     if ((no_of_bytes_written % 72) == 0)
 41     {
 42         (void) fprintf(out, "\n");
 43         no_of_bytes_written = 0;
 44     }
 45 }
 46 
 47 static void tv_screen_print_data_1bpp(FILE * fp, Tv_screen * tv_screen)
 48 {
 49     int     width, height;
 50     XImage *ximage;
 51     int     bkgrndpix;
 52     Tina_pixel *lut;
 53     int     x, y;
 54     int     bit_count, byte, pixel;
 55 
 56     width = tv_screen_check_width(tv_screen);
 57     height = tv_screen_check_height(tv_screen);
 58 
 59     ximage = XGetImage(tv_screen->display, tv_screen->window,
 60                        0, 0, width, height, AllPlanes, ZPixmap);
 61     lut = tv_screen->tv->cmap_data_visible->std_lut;
 62 
 63     bkgrndpix = (int) lut[bkgrnd];
 64 
 65     no_of_bytes_written = 0;
 66     for (y = 0; y < height; y++)
 67     {
 68         bit_count = 0;
 69         byte = 0xff;
 70         for (x = 0; x < width; x++)
 71         {
 72             pixel = XGetPixel(ximage, x, y);
 73             if (pixel != bkgrndpix)
 74                 byte ^= 0x80 >> bit_count;
 75             bit_count = (bit_count + 1) % 8;
 76             if (bit_count == 0)
 77             {
 78                 write_byte(fp, byte);
 79                 byte = 0xff;
 80             }
 81         }
 82         if (bit_count > 0)
 83             write_byte(fp, byte);
 84     }
 85     XDestroyImage(ximage);
 86 }
 87 
 88 
 89 
 90 static void tv_screen_epsf_dump_1bpp(Tv_screen * tv_screen, char *epsf_dump_file)
 91 {
 92     FILE   *fp = fopen(epsf_dump_file, "w");
 93     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
 94     float   totsize, border = 0.05;
 95 
 96     if (fp == NULL)
 97     {
 98       error("epsf_dump: couldn't open file\n", non_fatal);
 99       return;
100     }
101 
102     width = tv_screen_check_width(tv_screen);
103     height = tv_screen_check_height(tv_screen);
104     depth = tv_screen_check_depth(tv_screen);
105     pixwidth = width;           /* scale setting in PSpoint */
106     pixheight = height;
107     bitsperpixel = 1;
108     bytes = width / 8;          /* size of string for image routine */
109     if (width % 8)
110         bytes++;
111     totsize = 1.0 + 2.0 * border;
112 
113     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
114     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
115     /* NB linethickness too ! */
116     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
117     (void) fprintf(fp, "%%%%EndComments\n");
118 
119     (void) fprintf(fp, "/bytes %d def\n", bytes);
120     (void) fprintf(fp, "/width %d def\n", width);
121     (void) fprintf(fp, "/height %d def\n", height);
122     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
123     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
124     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
125 
126     (void) fprintf(fp, "/picstr bytes string def\n");
127     (void) fprintf(fp, "/dopic {\n");
128     (void) fprintf(fp, "gsave width height bitsperpixel\n");
129     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
130     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
131     (void) fprintf(fp, "image grestore} def\n");
132 
133     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
134     (void) fprintf(fp, "newpath\n");
135     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
136     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
137     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
138     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
139     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
140     (void) fprintf(fp, "closepath\n");
141     (void) fprintf(fp, "0.005 setlinewidth\n");
142     (void) fprintf(fp, "stroke\n");
143 
144     (void) fprintf(fp, "dopic\n");
145 
146     tv_screen_print_data_1bpp(fp, tv_screen);
147 
148     (void) fprintf(fp, "\nshowpage\n");
149     (void) fprintf(fp, "%%%%Trailer\n");
150     fclose(fp);
151 }
152 
153 
154 static void tv_screen_print_data_bw(FILE * fp, Tv_screen * tv_screen)
155 {
156     int     i, j, width, height;
157     int     count = 0;
158     XImage *ximage;
159     int     bkgrndpix;
160     Tina_pixel *lut;
161 
162     width = tv_screen_check_width(tv_screen);
163     height = tv_screen_check_height(tv_screen);
164 
165     ximage = XGetImage(tv_screen->display, tv_screen->window,
166                        0, 0, width, height, AllPlanes, ZPixmap);
167 
168     lut = tv_screen->tv->cmap_data_visible->std_lut;
169 
170     bkgrndpix = (int) lut[bkgrnd];
171 
172     count = 0;
173     for (i = 0; i < height; i++)
174     {
175         for (j = 0; j < width; j++)
176         {
177             unsigned long c, grey;
178             XColor  color;
179             int     r, g, b;
180 
181 
182             c = XGetPixel(ximage, j, i);        /* X pixel value */
183             color.pixel = c;
184             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
185             r = color.red >> 8;
186             g = color.green >> 8;
187             b = color.blue >> 8;
188             if (r == g && g == b)
189                 grey = r;       /** grey **/
190             else
191                 grey = MAX3(r, g, b);    
192             (void) fprintf(fp, "%2.2x", grey);
193             if ((++count % 64) == 0)
194                 (void) fprintf(fp, "\n");
195         }
196     }
197     XDestroyImage(ximage);
198 
199 }
200 
201 static void tv_screen_epsf_dump_bw(Tv_screen * tv_screen, char *epsf_dump_file)
202 {
203     FILE   *fp = fopen(epsf_dump_file, "w");
204     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
205     float   totsize, border = 0.05;
206 
207     if (fp == NULL)
208     {
209       error("epsf_dump: couldn't open file\n", non_fatal);
210       return;
211     }
212     width = tv_screen_check_width(tv_screen);
213     height = tv_screen_check_height(tv_screen);
214     depth = tv_screen_check_depth(tv_screen);
215     pixwidth = width;           /* scale setting in PSpoint */
216     pixheight = height;
217     bitsperpixel = 8;
218     bytes = width;              /* size of string for image routine */
219     totsize = 1.0 + 2.0 * border;
220 
221     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
222     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
223     /* NB linethickness too ! */
224     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
225     (void) fprintf(fp, "%%%%EndComments\n");
226 
227     (void) fprintf(fp, "/bytes %d def\n", bytes);
228     (void) fprintf(fp, "/width %d def\n", width);
229     (void) fprintf(fp, "/height %d def\n", height);
230     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
231     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
232     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
233 
234     (void) fprintf(fp, "/picstr bytes string def\n");
235     (void) fprintf(fp, "/dopic {\n");
236     (void) fprintf(fp, "gsave width height bitsperpixel\n");
237     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
238     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
239     (void) fprintf(fp, "image grestore} def\n");
240 
241     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
242     (void) fprintf(fp, "newpath\n");
243     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
244     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
245     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
246     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
247     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
248     (void) fprintf(fp, "closepath\n");
249     (void) fprintf(fp, "0.005 setlinewidth\n");
250     (void) fprintf(fp, "stroke\n");
251 
252     (void) fprintf(fp, "dopic\n");
253 
254     tv_screen_print_data_bw(fp, tv_screen);
255 
256     (void) fprintf(fp, "\nshowpage\n");
257     (void) fprintf(fp, "%%%%Trailer\n");
258     fclose(fp);
259 }
260 
261 /* This awaits a Level 2 postscript to be tested! */
262 static void tv_screen_print_color_map(FILE * fp, Tv_screen * tv_screen)
263 {
264     int     i;
265     XColor  x_color;
266 
267     if (tv_screen == NULL || tv_screen->tv == NULL)
268         return;
269 
270     /* write out color map */
271     (void) fprintf(fp, "[/Indexed /DeviceRGB 255\n < ");
272     for (i = 0; i < 256; i++)
273     {
274         x_color.pixel = i;
275         XQueryColor(tv_screen->display, tv_screen->colormap, &x_color);
276         write_byte(fp, x_color.red >> 8);
277         write_byte(fp, x_color.green >> 8);
278         write_byte(fp, x_color.blue >> 8);
279         (void) fprintf(fp, " ");
280     }
281     (void) fprintf(fp, ">\n] setcolorspace\n\n");
282 }
283 
284 static void tv_screen_print_data_color(FILE * fp, Tv_screen * tv_screen)
285 {
286     int     i, width, height;
287     XImage *ximage;
288     int     x, y;
289     int     pixel;
290     XColor  xcolor;
291 
292     if (tv_screen == NULL || tv_screen->tv == NULL)
293         return;
294 
295     width = tv_screen_check_width(tv_screen);
296     height = tv_screen_check_height(tv_screen);
297 
298     ximage = XGetImage(tv_screen->display, tv_screen->window,
299                        0, 0, width, height, AllPlanes, ZPixmap);
300 
301     no_of_bytes_written = 0;
302     for (y = 0; y < height; y++)
303     {
304         for (x = 0; x < width; x++)
305         {
306             pixel = XGetPixel(ximage, x, y);
307             xcolor.pixel = pixel;
308             XQueryColor(tv_screen->display, tv_screen->colormap, &xcolor);
309             write_byte(fp, xcolor.red >> 8);
310             write_byte(fp, xcolor.green >> 8);
311             write_byte(fp, xcolor.blue >> 8);
312         }
313     }
314     XDestroyImage(ximage);
315 }
316 
317 static void tv_screen_epsf_dump_color(Tv_screen * tv_screen, char *epsf_dump_file)
318 {
319     FILE   *fp = fopen(epsf_dump_file, "w");
320     int     width, height, depth, pixwidth, pixheight;
321     float   totsize, border = 0.05;
322 
323     if (fp == NULL)
324     {
325       error("epsf_dump: couldn't open file\n", non_fatal);
326       return;
327     }
328     width = tv_screen_check_width(tv_screen);
329     height = tv_screen_check_height(tv_screen);
330     depth = tv_screen_check_depth(tv_screen);
331     pixwidth = width;           /* scale setting in PSpoint */
332     pixheight = height;
333     totsize = 1.0 + 2.0 * border;
334 
335     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
336     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
337     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
338     (void) fprintf(fp, "%%%%EndComments\n");
339 
340     /* do border */
341     (void) fprintf(fp, "0 0 translate %d %d scale\n", pixwidth, pixheight);
342     (void) fprintf(fp, "newpath\n");
343     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
344     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
345     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
346     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
347     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
348     (void) fprintf(fp, "closepath\n");
349     (void) fprintf(fp, "0.005 setlinewidth\n");
350     (void) fprintf(fp, "stroke\n");
351 
352     (void) fprintf(fp, "/picstr %d string def\n", 3 * width);
353     (void) fprintf(fp, "%d %d 8\n", width, height);
354     (void) fprintf(fp, "[%d 0 0 -%d 0 %d]\n", width, height, height);
355     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
356     (void) fprintf(fp, "false 3\n");
357     (void) fprintf(fp, "colorimage\n");
358 
359     tv_screen_print_data_color(fp, tv_screen);
360 
361     (void) fprintf(fp, "\nshowpage\n");
362     (void) fprintf(fp, "%%%%Trailer\n");
363     fclose(fp);
364 }
365 
366 void    tv_screen_epsf_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
367 {
368     if (tv_screen == NULL || tv_screen->tv == NULL)
369         return;
370     set_epsf_dump_type(type);
371     (*epsf_dump) (tv_screen, filename);
372 }
373 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.