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

Linux Cross Reference
Tina5/tina-tools/tinatool/gphx/x11/gphxX11_epsf_dump.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/gphx/x11/gphxX11_epsf_dump.c,v $
 37  * Date    :  $Date: 2007/02/15 01:55:50 $
 38  * Version :  $Revision: 1.3 $
 39  * CVS Id  :  $Id: gphxX11_epsf_dump.c,v 1.3 2007/02/15 01:55:50 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  *
 45  * Create Encapsulated PostScript Files
 46  *
 47  *********
 48 */
 49 
 50 #include "gphxX11_epsf_dump.h"
 51 
 52 #if HAVE_CONFIG_H
 53   #include <config.h>
 54 #endif
 55 
 56 #include <tinatool/draw/drawDef.h>
 57 #include <tinatool/draw/drawPro.h>
 58 #include <tinatool/gphx/x11/gphxX11_size.h>
 59 
 60 static void (*epsf_dump) (Tv_screen *, char *) = NULL;
 61 static int no_of_bytes_written = 0;
 62 
 63 static void write_byte(FILE * out, int byte)
 64 {
 65 
 66     (void) fprintf(out, "%2.2x", byte);
 67     no_of_bytes_written++;
 68     if ((no_of_bytes_written % 72) == 0)
 69     {
 70         (void) fprintf(out, "\n");
 71         no_of_bytes_written = 0;
 72     }
 73 }
 74 
 75 static void tv_screen_print_data_1bpp(FILE * fp, Tv_screen * tv_screen)
 76 {
 77     int     width, height;
 78     XImage *ximage;
 79     int     bkgrndpix;
 80     Tina_pixel *lut;
 81     int     x, y;
 82     int     bit_count, byte, pixel;
 83 
 84     width = tv_screen_check_width(tv_screen);
 85     height = tv_screen_check_height(tv_screen);
 86 
 87     ximage = XGetImage(tv_screen->display, tv_screen->window,
 88                        0, 0, width, height, AllPlanes, ZPixmap);
 89     lut = tv_screen->tv->cmap_data_visible->std_lut;
 90 
 91     bkgrndpix = (int) lut[bkgrnd];
 92 
 93     no_of_bytes_written = 0;
 94     for (y = 0; y < height; y++)
 95     {
 96         bit_count = 0;
 97         byte = 0xff;
 98         for (x = 0; x < width; x++)
 99         {
100             pixel = XGetPixel(ximage, x, y);
101             if (pixel != bkgrndpix)
102                 byte ^= 0x80 >> bit_count;
103             bit_count = (bit_count + 1) % 8;
104             if (bit_count == 0)
105             {
106                 write_byte(fp, byte);
107                 byte = 0xff;
108             }
109         }
110         if (bit_count > 0)
111             write_byte(fp, byte);
112     }
113     XDestroyImage(ximage);
114 }
115 
116 
117 
118 static void tv_screen_epsf_dump_1bpp(Tv_screen * tv_screen, char *epsf_dump_file)
119 {
120     FILE   *fp = fopen(epsf_dump_file, "w");
121     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
122     float   totsize, border = 0.05;
123 
124     if (fp == NULL)
125     {
126       error("epsf_dump: couldn't open file\n", non_fatal);
127       return;
128     }
129 
130     width = tv_screen_check_width(tv_screen);
131     height = tv_screen_check_height(tv_screen);
132     depth = tv_screen_check_depth(tv_screen);
133     pixwidth = width;           /* scale setting in PSpoint */
134     pixheight = height;
135     bitsperpixel = 1;
136     bytes = width / 8;          /* size of string for image routine */
137     if (width % 8)
138         bytes++;
139     totsize = 1.0 + 2.0 * border;
140 
141     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
142     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
143     /* NB linethickness too ! */
144     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
145     (void) fprintf(fp, "%%%%EndComments\n");
146 
147     (void) fprintf(fp, "/bytes %d def\n", bytes);
148     (void) fprintf(fp, "/width %d def\n", width);
149     (void) fprintf(fp, "/height %d def\n", height);
150     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
151     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
152     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
153 
154     (void) fprintf(fp, "/picstr bytes string def\n");
155     (void) fprintf(fp, "/dopic {\n");
156     (void) fprintf(fp, "gsave width height bitsperpixel\n");
157     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
158     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
159     (void) fprintf(fp, "image grestore} def\n");
160 
161     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
162     (void) fprintf(fp, "newpath\n");
163     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
164     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
165     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
166     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
167     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
168     (void) fprintf(fp, "closepath\n");
169     (void) fprintf(fp, "0.005 setlinewidth\n");
170     (void) fprintf(fp, "stroke\n");
171 
172     (void) fprintf(fp, "dopic\n");
173 
174     tv_screen_print_data_1bpp(fp, tv_screen);
175 
176     (void) fprintf(fp, "\nshowpage\n");
177     (void) fprintf(fp, "%%%%Trailer\n");
178     fclose(fp);
179 }
180 
181 
182 static void tv_screen_print_data_bw(FILE * fp, Tv_screen * tv_screen)
183 {
184     int     i, j, width, height;
185     int     count = 0;
186     XImage *ximage;
187     int     bkgrndpix;
188     Tina_pixel *lut;
189 
190     width = tv_screen_check_width(tv_screen);
191     height = tv_screen_check_height(tv_screen);
192 
193     ximage = XGetImage(tv_screen->display, tv_screen->window,
194                        0, 0, width, height, AllPlanes, ZPixmap);
195 
196     lut = tv_screen->tv->cmap_data_visible->std_lut;
197 
198     bkgrndpix = (int) lut[bkgrnd];
199 
200     count = 0;
201     for (i = 0; i < height; i++)
202     {
203         for (j = 0; j < width; j++)
204         {
205             unsigned long c, grey;
206             XColor  color;
207             int     r, g, b;
208 
209 
210             c = XGetPixel(ximage, j, i);        /* X pixel value */
211             color.pixel = c;
212             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
213             r = color.red >> 8;
214             g = color.green >> 8;
215             b = color.blue >> 8;
216             if (r == g && g == b)
217                 grey = r;       /** grey **/
218             else
219                 grey = MAX3(r, g, b);    
220             (void) fprintf(fp, "%2.2x", grey);
221             if ((++count % 64) == 0)
222                 (void) fprintf(fp, "\n");
223         }
224     }
225     XDestroyImage(ximage);
226 
227 }
228 
229 static void tv_screen_epsf_dump_bw(Tv_screen * tv_screen, char *epsf_dump_file)
230 {
231     FILE   *fp = fopen(epsf_dump_file, "w");
232     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
233     float   totsize, border = 0.05;
234 
235     if (fp == NULL)
236     {
237       error("epsf_dump: couldn't open file\n", non_fatal);
238       return;
239     }
240     width = tv_screen_check_width(tv_screen);
241     height = tv_screen_check_height(tv_screen);
242     depth = tv_screen_check_depth(tv_screen);
243     pixwidth = width;           /* scale setting in PSpoint */
244     pixheight = height;
245     bitsperpixel = 8;
246     bytes = width;              /* size of string for image routine */
247     totsize = 1.0 + 2.0 * border;
248 
249     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
250     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
251     /* NB linethickness too ! */
252     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
253     (void) fprintf(fp, "%%%%EndComments\n");
254 
255     (void) fprintf(fp, "/bytes %d def\n", bytes);
256     (void) fprintf(fp, "/width %d def\n", width);
257     (void) fprintf(fp, "/height %d def\n", height);
258     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
259     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
260     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
261 
262     (void) fprintf(fp, "/picstr bytes string def\n");
263     (void) fprintf(fp, "/dopic {\n");
264     (void) fprintf(fp, "gsave width height bitsperpixel\n");
265     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
266     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
267     (void) fprintf(fp, "image grestore} def\n");
268 
269     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
270     (void) fprintf(fp, "newpath\n");
271     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
272     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
273     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
274     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
275     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
276     (void) fprintf(fp, "closepath\n");
277     (void) fprintf(fp, "0.005 setlinewidth\n");
278     (void) fprintf(fp, "stroke\n");
279 
280     (void) fprintf(fp, "dopic\n");
281 
282     tv_screen_print_data_bw(fp, tv_screen);
283 
284     (void) fprintf(fp, "\nshowpage\n");
285     (void) fprintf(fp, "%%%%Trailer\n");
286     fclose(fp);
287 }
288 
289 /* This awaits a Level 2 postscript to be tested! */
290 static void tv_screen_print_color_map(FILE * fp, Tv_screen * tv_screen)
291 {
292     int     i;
293     XColor  x_color;
294 
295     if (tv_screen == NULL || tv_screen->tv == NULL)
296         return;
297 
298     /* write out color map */
299     (void) fprintf(fp, "[/Indexed /DeviceRGB 255\n < ");
300     for (i = 0; i < 256; i++)
301     {
302         x_color.pixel = i;
303         XQueryColor(tv_screen->display, tv_screen->colormap, &x_color);
304         write_byte(fp, x_color.red >> 8);
305         write_byte(fp, x_color.green >> 8);
306         write_byte(fp, x_color.blue >> 8);
307         (void) fprintf(fp, " ");
308     }
309     (void) fprintf(fp, ">\n] setcolorspace\n\n");
310 }
311 
312 static void tv_screen_print_data_color(FILE * fp, Tv_screen * tv_screen)
313 {
314     int     i, width, height;
315     XImage *ximage;
316     int     x, y;
317     int     pixel;
318     XColor  xcolor;
319 
320     if (tv_screen == NULL || tv_screen->tv == NULL)
321         return;
322 
323     width = tv_screen_check_width(tv_screen);
324     height = tv_screen_check_height(tv_screen);
325 
326     ximage = XGetImage(tv_screen->display, tv_screen->window,
327                        0, 0, width, height, AllPlanes, ZPixmap);
328 
329     no_of_bytes_written = 0;
330     for (y = 0; y < height; y++)
331     {
332         for (x = 0; x < width; x++)
333         {
334             pixel = XGetPixel(ximage, x, y);
335             xcolor.pixel = pixel;
336             XQueryColor(tv_screen->display, tv_screen->colormap, &xcolor);
337             write_byte(fp, xcolor.red >> 8);
338             write_byte(fp, xcolor.green >> 8);
339             write_byte(fp, xcolor.blue >> 8);
340         }
341     }
342     XDestroyImage(ximage);
343 }
344 
345 static void tv_screen_epsf_dump_color(Tv_screen * tv_screen, char *epsf_dump_file)
346 {
347     FILE   *fp = fopen(epsf_dump_file, "w");
348     int     width, height, depth, pixwidth, pixheight;
349     float   totsize, border = 0.05;
350 
351     if (fp == NULL)
352     {
353       error("epsf_dump: couldn't open file\n", non_fatal);
354       return;
355     }
356     width = tv_screen_check_width(tv_screen);
357     height = tv_screen_check_height(tv_screen);
358     depth = tv_screen_check_depth(tv_screen);
359     pixwidth = width;           /* scale setting in PSpoint */
360     pixheight = height;
361     totsize = 1.0 + 2.0 * border;
362 
363     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
364     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
365     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
366     (void) fprintf(fp, "%%%%EndComments\n");
367 
368     /* do border */
369     (void) fprintf(fp, "0 0 translate %d %d scale\n", pixwidth, pixheight);
370     (void) fprintf(fp, "newpath\n");
371     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
372     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
373     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
374     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
375     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
376     (void) fprintf(fp, "closepath\n");
377     (void) fprintf(fp, "0.005 setlinewidth\n");
378     (void) fprintf(fp, "stroke\n");
379 
380     (void) fprintf(fp, "/picstr %d string def\n", 3 * width);
381     (void) fprintf(fp, "%d %d 8\n", width, height);
382     (void) fprintf(fp, "[%d 0 0 -%d 0 %d]\n", width, height, height);
383     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
384     (void) fprintf(fp, "false 3\n");
385     (void) fprintf(fp, "colorimage\n");
386 
387     tv_screen_print_data_color(fp, tv_screen);
388 
389     (void) fprintf(fp, "\nshowpage\n");
390     (void) fprintf(fp, "%%%%Trailer\n");
391     fclose(fp);
392 }
393 
394 static void set_epsf_dump_type(Dump_type type)
395 {
396         if (epsf_dump == NULL)
397                 epsf_dump = tv_screen_epsf_dump_color;
398                 
399     switch (type)
400     {
401         case DT_BW:
402         epsf_dump = tv_screen_epsf_dump_bw;
403         break;
404     case DT_COLOR:
405         epsf_dump = tv_screen_epsf_dump_color;
406         break;
407     case DT_ONE_BIT_PER_PIXEL:
408         epsf_dump = tv_screen_epsf_dump_1bpp;
409         break;
410     }
411 }
412 
413 
414 void    tv_screen_epsf_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
415 {
416     if (tv_screen == NULL || tv_screen->tv == NULL)
417         return;
418     set_epsf_dump_type(type);
419     (*epsf_dump) (tv_screen, filename);
420 }
421 
422 
423 

~ [ 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.