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

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

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