exportimage.c

Go to the documentation of this file.
00001 /*
00002  * gEDA - GNU Electronic Design Automation
00003  * This file is a part of gerbv.
00004  *
00005  *   Copyright (C) 2000-2002 Stefan Petersen (spe@stacken.kth.se)
00006  *
00007  * Contributed by Dino Ghilardi <dino.ghilardi@ieee.org> 
00008  *
00009  * $Id$
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
00024  */
00025 
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034 
00035 #ifdef HAVE_UNISTD_H
00036 #include <unistd.h>
00037 #endif
00038 
00039 #include <math.h>
00040 #include <gdk-pixbuf/gdk-pixbuf.h>
00041 #include <png.h>
00042 
00043 #include "gerbv.h"
00044 
00045 #ifdef RENDER_USING_GDK
00046   #include "draw-gdk.h"
00047 #else
00048   #include "draw.h"
00049   #include <cairo.h>
00050   #include <cairo-pdf.h>
00051   #include <cairo-ps.h>
00052   #include <cairo-svg.h>
00053 #endif
00054 
00055 extern gerbv_render_info_t screenRenderInfo;
00056 
00057 #ifdef RENDER_USING_GDK
00058 static gboolean 
00059 exportimage_save_pixbuf_to_file (GdkPixbuf* pixbuf, gchar const* filename);
00060 #endif
00061 
00062 #ifndef RENDER_USING_GDK
00063 void exportimage_render_to_surface_and_destroy (gerbv_project_t *gerbvProject,
00064               cairo_surface_t *cSurface, gerbv_render_info_t *renderInfo, gchar const* filename) {
00065       cairo_t *cairoTarget = cairo_create (cSurface);
00066       
00067       gerbv_render_all_layers_to_cairo_target_for_vector_output (gerbvProject, cairoTarget, renderInfo);
00068        cairo_destroy (cairoTarget);
00069        cairo_surface_destroy (cSurface);
00070 }
00071 #endif
00072 
00073 
00074 void gerbv_export_png_file_from_project_autoscaled (gerbv_project_t *gerbvProject, int widthInPixels,
00075               int heightInPixels, gchar const* filename) {
00076 #ifdef EXPORT_PNG
00077 #ifdef RENDER_USING_GDK
00078        gerbv_render_info_t renderInfo = {1.0, 1.0, 0, 0, 0, widthInPixels, heightInPixels};
00079 #else
00080        gerbv_render_info_t renderInfo = {1.0, 1.0, 0, 0, 3, widthInPixels, heightInPixels};
00081 #endif 
00082        gerbv_render_zoom_to_fit_display (gerbvProject, &renderInfo);
00083        gerbv_export_png_file_from_project (gerbvProject, &renderInfo, filename);
00084 #endif
00085 }
00086 
00087 void gerbv_export_png_file_from_project (gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const* filename) {
00088 #ifdef EXPORT_PNG
00089 #ifdef RENDER_USING_GDK
00090 
00091        GdkPixmap *renderedPixmap = gdk_pixmap_new (NULL, renderInfo->displayWidth,
00092                                                         renderInfo->displayHeight, 24);
00093        GdkColormap *colormap=NULL;
00094        GdkPixbuf *tempPixbuf=NULL;
00095        
00096        gerbv_render_to_pixmap_using_gdk (gerbvProject, renderedPixmap, renderInfo, NULL, NULL);
00097        colormap = gdk_drawable_get_colormap(renderedPixmap);
00098        if ((tempPixbuf = gdk_pixbuf_get_from_drawable(tempPixbuf, renderedPixmap, 
00099                                          colormap, 0, 0, 0, 0, 
00100                                          renderInfo->displayWidth, renderInfo->displayHeight))) {
00101               exportimage_save_pixbuf_to_file (tempPixbuf, filename);
00102               gdk_pixbuf_unref(tempPixbuf);
00103        }
00104        gdk_pixmap_unref(renderedPixmap);
00105        
00106        //png_export (NULL, filename);
00107 #else
00108        cairo_surface_t *cSurface = cairo_image_surface_create  (CAIRO_FORMAT_ARGB32,
00109                                                          renderInfo->displayWidth, renderInfo->displayHeight);
00110        cairo_t *cairoTarget = cairo_create (cSurface);
00111        gerbv_render_all_layers_to_cairo_target (gerbvProject, cairoTarget, renderInfo);
00112        cairo_surface_write_to_png (cSurface, filename);
00113        cairo_destroy (cairoTarget);
00114        cairo_surface_destroy (cSurface);
00115 #endif
00116 #endif
00117 }
00118 
00119 
00120 void gerbv_export_pdf_file_from_project_autoscaled (gerbv_project_t *gerbvProject, int widthInPoints,
00121               int heightInPoints, gchar const* filename) {
00122        gerbv_render_info_t renderInfo = {1.0, 1.0, 0, 0, 3, widthInPoints, heightInPoints};
00123        
00124        gerbv_render_zoom_to_fit_display (gerbvProject, &renderInfo);
00125        gerbv_export_pdf_file_from_project (gerbvProject, &renderInfo, filename);
00126 }
00127 
00128 void gerbv_export_pdf_file_from_project (gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo,
00129               gchar const* filename) {
00130 #ifndef RENDER_USING_GDK
00131        cairo_surface_t *cSurface = cairo_pdf_surface_create (filename, renderInfo->displayWidth,
00132                                                         renderInfo->displayHeight);
00133 
00134       exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
00135 #endif
00136 }
00137 
00138 void gerbv_export_postscript_file_from_project_autoscaled (gerbv_project_t *gerbvProject, int widthInPoints,
00139               int heightInPoints, gchar const* filename) {
00140        gerbv_render_info_t renderInfo = {1.0, 1.0, 0, 0, 3, widthInPoints, heightInPoints};
00141        
00142        gerbv_render_zoom_to_fit_display (gerbvProject, &renderInfo);
00143        gerbv_export_postscript_file_from_project (gerbvProject, &renderInfo, filename);
00144 }
00145 
00146 void gerbv_export_postscript_file_from_project (gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo,
00147               gchar const* filename) {
00148 #ifndef RENDER_USING_GDK
00149        cairo_surface_t *cSurface = cairo_ps_surface_create (filename, renderInfo->displayWidth,
00150                                                         renderInfo->displayHeight);
00151       exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
00152 #endif
00153 }
00154 
00155 void gerbv_export_svg_file_from_project_autoscaled (gerbv_project_t *gerbvProject, int widthInPoints,
00156               int heightInPoints, gchar const* filename) {
00157        gerbv_render_info_t renderInfo = {1.0, 1.0, 0, 0, 3, widthInPoints, heightInPoints};
00158        
00159        gerbv_render_zoom_to_fit_display (gerbvProject, &renderInfo);
00160        gerbv_export_svg_file_from_project (gerbvProject, &renderInfo, filename);
00161 }
00162 
00163 void gerbv_export_svg_file_from_project (gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo,
00164               gchar const* filename) {
00165 #ifndef RENDER_USING_GDK
00166        cairo_surface_t *cSurface = cairo_svg_surface_create (filename, renderInfo->displayWidth,
00167                                                         renderInfo->displayHeight);
00168       exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
00169 #endif
00170 }
00171 
00172 #ifdef EXPORT_PNG
00173 #ifdef RENDER_USING_GDK
00174 static gboolean 
00175 exportimage_save_pixbuf_to_file (GdkPixbuf *pixbuf, gchar const* filename)
00176 {
00177        FILE *handle;
00178        int width, height, depth, rowstride;
00179        guchar *pixels;
00180        png_structp png_ptr;
00181        png_infop info_ptr;
00182        png_text text[2];
00183        png_byte **row_ptr;
00184        int i;
00185 
00186        g_return_val_if_fail (pixbuf != NULL, FALSE);
00187        g_return_val_if_fail (filename != NULL, FALSE);
00188        g_return_val_if_fail (filename[0] != '\0', FALSE);
00189 
00190         handle = fopen (filename, "wb");
00191         if (handle == NULL) {
00192            return FALSE;
00193        }
00194 
00195        png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00196        if (png_ptr == NULL) {
00197            fclose (handle);
00198            return FALSE;
00199        }
00200 
00201        info_ptr = png_create_info_struct (png_ptr);
00202        if (info_ptr == NULL) {
00203            png_destroy_write_struct (&png_ptr, (png_infopp)NULL);
00204            fclose (handle);
00205            return FALSE;
00206        }
00207 
00208        png_init_io (png_ptr, handle);
00209        
00210        width = gdk_pixbuf_get_width (pixbuf);
00211        height = gdk_pixbuf_get_height (pixbuf);
00212        depth = gdk_pixbuf_get_bits_per_sample (pixbuf);
00213        pixels = gdk_pixbuf_get_pixels (pixbuf);
00214        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
00215        
00216        png_set_IHDR (png_ptr, info_ptr, width, height,
00217                     depth, PNG_COLOR_TYPE_RGB,
00218                     PNG_INTERLACE_NONE,
00219                     PNG_COMPRESSION_TYPE_DEFAULT,
00220                     PNG_FILTER_TYPE_DEFAULT);
00221        
00222        /* Some text to go with the png image */
00223        text[0].key = "Title";
00224        text[0].text = g_strdup(filename);
00225        text[0].compression = PNG_TEXT_COMPRESSION_NONE;
00226        text[1].key = "Generator";
00227        text[1].text = "gerbv";
00228        text[1].compression = PNG_TEXT_COMPRESSION_NONE;
00229        png_set_text (png_ptr, info_ptr, text, 2);
00230        g_free (text[0].text);
00231        /* Write header data */
00232        png_write_info (png_ptr, info_ptr);
00233        
00234        /* Build up a vector of row pointers */   
00235        row_ptr = (png_byte **)g_malloc(height * sizeof(png_byte *));
00236        for (i = 0; i < height; i++) {
00237            row_ptr[i] = (png_byte *)pixels;
00238            pixels += rowstride;
00239        }
00240 
00241        /* Write it and free the row vector */
00242        png_write_image(png_ptr, row_ptr);
00243        g_free(row_ptr);
00244 
00245        /* Finish of PNG writing */
00246        png_write_end (png_ptr, info_ptr);
00247        png_destroy_write_struct (&png_ptr, &info_ptr);
00248        
00249        fclose (handle);
00250        return TRUE;
00251 } /* pixbuf_to_file_as_png */
00252 #endif
00253 #endif

Generated on Tue Aug 19 00:14:48 2008 for gerbv by  doxygen 1.5.6