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

Linux Cross Reference
Tina6/tina-tools/tinatool/gphx/gdk2/gphxGdk_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/gdk2/gphxGdk_epsf_dump.c,v $
 37  * Date    :  $Date: 2007/12/07 01:09:00 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: gphxGdk_epsf_dump.c,v 1.2 2007/12/07 01:09:00 paul Exp $
 40  *
 41  * Author  :  paul.bromiley@manchester.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  gphxGdk_epsf_dump.c
 47  *  @brief Dump the image displayed on-screen in a Tv to an EPS file.
 48  *
 49  *  Note: the functions in this file are duplicated for each graphics lib (x11, GDK, etc ...).
 50  * 
 51  *  The xview and motif code uses the max of r,g,b in the B&W code and a comparison to the 
 52  *  TINA background color in the 1bpp code. The former gives a problem writing out imcalc
 53  *  histograms as B&W (the red histogram bars disappear) and the latter would require some
 54  *  awkward colormap conversions from TINA colors to GdkRGB.  Therefore, I have changed 
 55  *  both of these.  The B&W code now writes out intesity (i.e. the I of an HSI conversion 
 56  *  of the RGB).  The B&W code splits the intensity at 128: this should work since all TINA
 57  *  Tv's scale the dynamic range of the images, regardless of the highest intensity.
 58  *  PAB 03/12/2007 
 59  *
 60  */
 61 
 62 
 63 #include "gphxGdk_epsf_dump.h"
 64 
 65 #if HAVE_CONFIG_H
 66   #include <config.h>
 67 #endif
 68 
 69 #include <gtk/gtk.h>
 70 #include <tinatool/gphx/gdk2/gphx_GdkDef.h>
 71 #include <tinatool/gphx/gdk2/gphxGdk_picture.h>
 72 #include <tinatool/gphx/gdk2/gphxGdk_size.h>
 73 #include <tinatool/draw/drawDef.h>
 74 #include <tinatool/draw/drawPro.h>
 75 
 76 
 77 static void (*epsf_dump) (Tv_screen *, char *) = NULL;
 78 static int no_of_bytes_written = 0;
 79 
 80 
 81 static void write_byte(FILE * out, int byte)
 82 {
 83     (void) fprintf(out, "%2.2x", byte);
 84     no_of_bytes_written++;
 85     if ((no_of_bytes_written % 72) == 0)
 86     {
 87         (void) fprintf(out, "\n");
 88         no_of_bytes_written = 0;
 89     }
 90 }
 91 
 92 
 93 static void tv_screen_print_data_1bpp(FILE * fp, Tv_screen * tv_screen)
 94 {
 95     Tv_picture *picture=NULL;
 96     GdkPixbuf   *pbuf;
 97     guchar *pixels=NULL, *src=NULL;
 98     guint width, height, channel, src_stride;
 99     unsigned long grey;
100     int x, y, r, g, b, bit_count, byte;
101 
102     if (tv_screen == NULL || tv_screen->tv == NULL) return;
103 
104     picture = tv_screen_get_picture(tv_screen);
105     pbuf = (GdkPixbuf *)picture->pbuf;
106     if(pbuf==NULL) return;
107 
108     width = gdk_pixbuf_get_width(pbuf);
109     height = gdk_pixbuf_get_height(pbuf);
110     channel = gdk_pixbuf_get_n_channels(pbuf);
111     pixels = gdk_pixbuf_get_pixels(pbuf);
112     src_stride = gdk_pixbuf_get_rowstride (pbuf);
113     no_of_bytes_written = 0;
114 
115     for (y = 0; y < height; y++, pixels += src_stride)
116     {
117         src = pixels;
118         bit_count = 0;
119         byte = 0xff;
120         for (x = 0; x < width; x++, src += channel)
121         {
122             r = src[0];
123             g = src[1];
124             b = src[2];
125 
126             grey = (r+g+b)/3;
127 
128             if (grey<128) byte ^= 0x80 >> bit_count;
129             bit_count = (bit_count + 1) % 8;
130             if (bit_count == 0)
131             {
132                 write_byte(fp, byte);
133                 byte = 0xff;
134             }
135         }
136         if (bit_count > 0) write_byte(fp, byte);
137     }
138 
139     picture_free(picture);
140 }
141 
142 
143 static void tv_screen_epsf_dump_1bpp(Tv_screen * tv_screen, char *epsf_dump_file)
144 {
145     FILE   *fp = fopen(epsf_dump_file, "w");
146     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
147     float   totsize, border = 0.05;
148 
149     if (fp == NULL)
150     {
151       error("epsf_dump: couldn't open file\n", non_fatal);
152       return;
153     }
154 
155     width = tv_screen_check_width(tv_screen);
156     height = tv_screen_check_height(tv_screen);
157     depth = tv_screen_check_depth(tv_screen);
158     pixwidth = width;           /* scale setting in PSpoint */
159     pixheight = height;
160     bitsperpixel = 1;
161     bytes = width / 8;          /* size of string for image routine */
162     if (width % 8)
163         bytes++;
164     totsize = 1.0 + 2.0 * border;
165 
166     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
167     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
168     /* NB linethickness too ! */
169     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
170     (void) fprintf(fp, "%%%%EndComments\n");
171 
172     (void) fprintf(fp, "/bytes %d def\n", bytes);
173     (void) fprintf(fp, "/width %d def\n", width);
174     (void) fprintf(fp, "/height %d def\n", height);
175     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
176     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
177     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
178 
179     (void) fprintf(fp, "/picstr bytes string def\n");
180     (void) fprintf(fp, "/dopic {\n");
181     (void) fprintf(fp, "gsave width height bitsperpixel\n");
182     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
183     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
184     (void) fprintf(fp, "image grestore} def\n");
185 
186     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
187     (void) fprintf(fp, "newpath\n");
188     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
189     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
190     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
191     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
192     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
193     (void) fprintf(fp, "closepath\n");
194     (void) fprintf(fp, "0.005 setlinewidth\n");
195     (void) fprintf(fp, "stroke\n");
196 
197     (void) fprintf(fp, "dopic\n");
198 
199     tv_screen_print_data_1bpp(fp, tv_screen);
200 
201     (void) fprintf(fp, "\nshowpage\n");
202     (void) fprintf(fp, "%%%%Trailer\n");
203     fclose(fp);
204 }
205 
206 
207 static void tv_screen_print_data_bw(FILE * fp, Tv_screen * tv_screen)
208 {
209     Tv_picture *picture=NULL;
210     GdkPixbuf   *pbuf;
211     guchar *pixels=NULL, *src=NULL;
212     guint width, height, channel, src_stride;
213     unsigned int grey;
214     int x, y, r, g, b, count=0;
215 
216     if (tv_screen == NULL || tv_screen->tv == NULL) return;
217 
218     picture = tv_screen_get_picture(tv_screen);
219     pbuf = (GdkPixbuf *)picture->pbuf;
220     if(pbuf==NULL) return;
221 
222     width = gdk_pixbuf_get_width(pbuf);
223     height = gdk_pixbuf_get_height(pbuf);
224     channel = gdk_pixbuf_get_n_channels(pbuf);
225     pixels = gdk_pixbuf_get_pixels(pbuf);
226     src_stride = gdk_pixbuf_get_rowstride (pbuf);
227     no_of_bytes_written = 0;
228 
229     for (y = 0; y < height; y++, pixels += src_stride)
230     {
231         src = pixels;
232         for (x = 0; x < width; x++, src += channel)
233         {
234             r = src[0];
235             g = src[1];
236             b = src[2];
237             grey = (r+g+b)/3;
238 
239             (void) fprintf(fp, "%2.2x", grey);
240             if ((++count % 64) == 0) (void) fprintf(fp, "\n");
241         }
242     }
243     picture_free(picture);
244 }
245 
246 
247 static void tv_screen_epsf_dump_bw(Tv_screen * tv_screen, char *epsf_dump_file)
248 {
249     FILE   *fp = fopen(epsf_dump_file, "w");
250     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
251     float   totsize, border = 0.05;
252 
253     if (fp == NULL)
254     {
255       error("epsf_dump: couldn't open file\n", non_fatal);
256       return;
257     }
258     width = tv_screen_check_width(tv_screen);
259     height = tv_screen_check_height(tv_screen);
260     depth = tv_screen_check_depth(tv_screen);
261     pixwidth = width;           /* scale setting in PSpoint */
262     pixheight = height;
263     bitsperpixel = 8;
264     bytes = width;              /* size of string for image routine */
265     totsize = 1.0 + 2.0 * border;
266 
267     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
268     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
269     /* NB linethickness too ! */
270     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
271     (void) fprintf(fp, "%%%%EndComments\n");
272 
273     (void) fprintf(fp, "/bytes %d def\n", bytes);
274     (void) fprintf(fp, "/width %d def\n", width);
275     (void) fprintf(fp, "/height %d def\n", height);
276     (void) fprintf(fp, "/pixwidth %d def\n", pixwidth);
277     (void) fprintf(fp, "/pixheight %d def\n", pixheight);
278     (void) fprintf(fp, "/bitsperpixel %d def\n", bitsperpixel);
279 
280     (void) fprintf(fp, "/picstr bytes string def\n");
281     (void) fprintf(fp, "/dopic {\n");
282     (void) fprintf(fp, "gsave width height bitsperpixel\n");
283     (void) fprintf(fp, "[width 0 0 height neg 0 height]\n");
284     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
285     (void) fprintf(fp, "image grestore} def\n");
286 
287     (void) fprintf(fp, "0 0 translate pixwidth pixheight scale\n");
288     (void) fprintf(fp, "newpath\n");
289     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
290     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
291     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
292     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
293     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
294     (void) fprintf(fp, "closepath\n");
295     (void) fprintf(fp, "0.005 setlinewidth\n");
296     (void) fprintf(fp, "stroke\n");
297 
298     (void) fprintf(fp, "dopic\n");
299 
300     tv_screen_print_data_bw(fp, tv_screen);
301 
302     (void) fprintf(fp, "\nshowpage\n");
303     (void) fprintf(fp, "%%%%Trailer\n");
304     fclose(fp);
305 }
306 
307 
308 
309 static void tv_screen_print_data_color(FILE * fp, Tv_screen * tv_screen)
310 {
311     Tv_picture *picture=NULL;
312     GdkPixbuf   *pbuf;
313     guchar *pixels=NULL, *src=NULL;
314     guint width, height, channel, src_stride;
315     int x, y;
316 
317     if (tv_screen == NULL || tv_screen->tv == NULL) return;
318 
319     picture = tv_screen_get_picture(tv_screen);
320     pbuf = (GdkPixbuf *)picture->pbuf;
321     if(pbuf==NULL) return;
322 
323     width = gdk_pixbuf_get_width(pbuf);
324     height = gdk_pixbuf_get_height(pbuf);
325     channel = gdk_pixbuf_get_n_channels(pbuf);
326     pixels = gdk_pixbuf_get_pixels(pbuf);
327     src_stride = gdk_pixbuf_get_rowstride (pbuf);
328     no_of_bytes_written = 0;
329 
330     for (y = 0; y < height; y++, pixels += src_stride)
331     {
332         src = pixels;
333         for (x = 0; x < width; x++, src += channel)
334         {
335             write_byte(fp, src[0]);
336             write_byte(fp, src[1]);
337             write_byte(fp, src[2]);
338         }
339     }
340     picture_free(picture);
341 }
342 
343 
344 static void tv_screen_epsf_dump_color(Tv_screen * tv_screen, char *epsf_dump_file)
345 {
346     FILE   *fp = fopen(epsf_dump_file, "w");
347     int     width, height, depth, pixwidth, pixheight;
348     float   totsize, border = 0.05;
349 
350     if (fp == NULL)
351     {
352       error("epsf_dump: couldn't open file\n", non_fatal);
353       return;
354     }
355     width = tv_screen_check_width(tv_screen);
356     height = tv_screen_check_height(tv_screen);
357     depth = tv_screen_check_depth(tv_screen);
358     pixwidth = width;           /* scale setting in PSpoint */
359     pixheight = height;
360     totsize = 1.0 + 2.0 * border;
361 
362     (void) fprintf(fp, "%%!PS-Adobe-2.0 EPFS-2.0\n");
363     (void) fprintf(fp, "%%%%BoundingBox: %f %f %f %f\n", -border * pixwidth, -border * pixheight, width + border * pixwidth, height + border * pixheight);
364     (void) fprintf(fp, "%%%%Creator: TINA epsf_dump\n");
365     (void) fprintf(fp, "%%%%EndComments\n");
366 
367     /* do border */
368     (void) fprintf(fp, "0 0 translate %d %d scale\n", pixwidth, pixheight);
369     (void) fprintf(fp, "newpath\n");
370     (void) fprintf(fp, "%f %f moveto\n", -border, -border);
371     (void) fprintf(fp, "%f %f rlineto\n", 0.0, totsize);
372     (void) fprintf(fp, "%f %f rlineto\n", totsize, 0.0);
373     (void) fprintf(fp, "%f %f rlineto\n", 0.0, -totsize);
374     (void) fprintf(fp, "%f %f rlineto\n", -totsize, 0.0);
375     (void) fprintf(fp, "closepath\n");
376     (void) fprintf(fp, "0.005 setlinewidth\n");
377     (void) fprintf(fp, "stroke\n");
378 
379     (void) fprintf(fp, "/picstr %d string def\n", 3 * width);
380     (void) fprintf(fp, "%d %d 8\n", width, height);
381     (void) fprintf(fp, "[%d 0 0 -%d 0 %d]\n", width, height, height);
382     (void) fprintf(fp, "{currentfile picstr readhexstring pop}\n");
383     (void) fprintf(fp, "false 3\n");
384     (void) fprintf(fp, "colorimage\n");
385 
386     tv_screen_print_data_color(fp, tv_screen);
387 
388     (void) fprintf(fp, "\nshowpage\n");
389     (void) fprintf(fp, "%%%%Trailer\n");
390     fclose(fp);
391 }
392 
393 static void set_epsf_dump_type(Dump_type type)
394 {
395     if (epsf_dump == NULL) epsf_dump = tv_screen_epsf_dump_color;
396 
397     switch (type)
398     {
399         case DT_BW:
400         epsf_dump = tv_screen_epsf_dump_bw;
401         break;
402     case DT_COLOR:
403         epsf_dump = tv_screen_epsf_dump_color;
404         break;
405     case DT_ONE_BIT_PER_PIXEL:
406         epsf_dump = tv_screen_epsf_dump_1bpp;
407         break;
408     }
409 }
410 
411 
412 void    tv_screen_epsf_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
413 {
414     if (tv_screen == NULL || tv_screen->tv == NULL)
415         return;
416     set_epsf_dump_type(type);
417     (*epsf_dump) (tv_screen, filename);
418 }
419 
420 
421 

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