/* $Id: gl2ps.c 173 2007-05-20 15:51:51Z krischnamurti $ */
/*
* GL2PS, an OpenGL to PostScript Printing Library
* Copyright (C) 1999-2006 Christophe Geuzaine <geuz@geuz.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of either:
*
* a) the GNU Library General Public License as published by the Free
* Software Foundation, either version 2 of the License, or (at your
* option) any later version; or
*
* b) the GL2PS License as published by Christophe Geuzaine, either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
* the GNU Library General Public License or the GL2PS License for
* more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library in the file named "COPYING.LGPL";
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
* Cambridge, MA 02139, USA.
*
* You should have received a copy of the GL2PS License with this
* library in the file named "COPYING.GL2PS"; if not, I will be glad
* to provide one.
*
* Contributors:
* Michael Sweet <mike@easysw.com>
* Marc Ume <marc.ume@digitalgraphics.be>
* Jean-Francois Remacle <remacle@gce.ucl.ac.be>
* Bart Kaptein <B.L.Kaptein@lumc.nl>
* Quy Nguyen-Dai <quy@nguyendai.org>
* Sam Buss <sbuss@ucsd.edu>
* Shane Hill <Shane.Hill@dsto.defence.gov.au>
* Romain Boman <r_boman@yahoo.fr>
* Rouben Rostamian <rostamian@umbc.edu>
* Diego Santa Cruz <Diego.SantaCruz@epfl.ch>
* Shahzad Muzaffar <Shahzad.Muzaffar@cern.ch>
* Lassi Tuura <lassi.tuura@cern.ch>
* Guy Barrand <barrand@lal.in2p3.fr>
* Prabhu Ramachandran <prabhu@aero.iitm.ernet.in>
* Micha Bieber <bieber@traits.de>
* Olivier Couet <couet@mail.cern.ch>
* Shai Ayal <shaiay@gmail.com>
* Fabian Wenzel <wenzel@tu-harburg.de>
* Ian D. Gay <gay@sfu.ca>
* Cosmin Truta <cosmin@cs.toronto.edu>
* Baiju Devani <b.devani@gmail.com>
* Alexander Danilov <danilov@lanl.gov>
*
* For the latest info about gl2ps, see http://www.geuz.org/gl2ps/.
* Please report all bugs and problems to <gl2ps@geuz.org>.
*/
#include "gl2ps.h"
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <stdarg.h>
#include <time.h>
#include <float.h>
#if defined(GL2PS_HAVE_ZLIB)
#include <zlib.h>
#endif
#if defined(GL2PS_HAVE_LIBPNG)
#include <png.h>
#endif
/*********************************************************************
*
* Private definitions, data structures and prototypes
*
*********************************************************************/
/* Magic numbers (assuming that the order of magnitude of window
coordinates is 10^3) */
#define GL2PS_EPSILON 5.0e-3F
#define GL2PS_ZSCALE 1000.0F
#define GL2PS_ZOFFSET 5.0e-2F
#define GL2PS_ZOFFSET_LARGE 20.0F
#define GL2PS_ZERO(arg) (fabs(arg) < 1.e-20)
/* Primitive types */
#define GL2PS_NO_TYPE -1
#define GL2PS_TEXT 1
#define GL2PS_POINT 2
#define GL2PS_LINE 3
#define GL2PS_QUADRANGLE 4
#define GL2PS_TRIANGLE 5
#define GL2PS_PIXMAP 6
#define GL2PS_IMAGEMAP 7
#define GL2PS_IMAGEMAP_WRITTEN 8
#define GL2PS_IMAGEMAP_VISIBLE 9
#define GL2PS_SPECIAL 10
/* BSP tree primitive comparison */
#define GL2PS_COINCIDENT 1
#define GL2PS_IN_FRONT_OF 2
#define GL2PS_IN_BACK_OF 3
#define GL2PS_SPANNING 4
/* 2D BSP tree primitive comparison */
#define GL2PS_POINT_COINCIDENT 0
#define GL2PS_POINT_INFRONT 1
#define GL2PS_POINT_BACK 2
/* Internal feedback buffer pass-through tokens */
#define GL2PS_BEGIN_OFFSET_TOKEN 1
#define GL2PS_END_OFFSET_TOKEN 2
#define GL2PS_BEGIN_BOUNDARY_TOKEN 3
#define GL2PS_END_BOUNDARY_TOKEN 4
#define GL2PS_BEGIN_STIPPLE_TOKEN 5
#define GL2PS_END_STIPPLE_TOKEN 6
#define GL2PS_POINT_SIZE_TOKEN 7
#define GL2PS_LINE_WIDTH_TOKEN 8
#define GL2PS_BEGIN_BLEND_TOKEN 9
#define GL2PS_END_BLEND_TOKEN 10
#define GL2PS_SRC_BLEND_TOKEN 11
#define GL2PS_DST_BLEND_TOKEN 12
#define GL2PS_IMAGEMAP_TOKEN 13
#define GL2PS_DRAW_PIXELS_TOKEN 14
#define GL2PS_TEXT_TOKEN 15
typedef enum {
T_UNDEFINED = -1,
T_CONST_COLOR = 1,
T_VAR_COLOR = 1<<1,
T_ALPHA_1 = 1<<2,
T_ALPHA_LESS_1 = 1<<3,
T_VAR_ALPHA = 1<<4
} GL2PS_TRIANGLE_PROPERTY;
typedef GLfloat GL2PSxyz[3];
typedef GLfloat GL2PSplane[4];
typedef struct _GL2PSbsptree2d GL2PSbsptree2d;
struct _GL2PSbsptree2d {
GL2PSplane plane;
GL2PSbsptree2d *front, *back;
};
typedef struct {
GLint nmax, size, incr, n;
char *array;
} GL2PSlist;
typedef struct _GL2PSbsptree GL2PSbsptree;
struct _GL2PSbsptree {
GL2PSplane plane;
GL2PSlist *primitives;
GL2PSbsptree *front, *back;
};
typedef struct {
GL2PSxyz xyz;
GL2PSrgba rgba;
} GL2PSvertex;
typedef struct {
GL2PSvertex vertex[3];
int prop;
} GL2PStriangle;
typedef struct {
GLshort fontsize;
char *str, *fontname;
/* Note: for a 'special' string, 'alignment' holds the format
(PostScript, PDF, etc.) of the special string */
GLint alignment;
GLfloat angle;
} GL2PSstring;
typedef struct {
GLsizei width, height;
/* Note: for an imagemap, 'type' indicates if it has already been
written to the file or not, and 'format' indicates if it is
visible or not */
GLenum format, type;
GLfloat *pixels;
} GL2PSimage;
typedef struct _GL2PSimagemap GL2PSimagemap;
struct _GL2PSimagemap {
GL2PSimage *image;
GL2PSimagemap *next;
};
typedef struct {
GLshort type, numverts;
GLushort pattern;
char boundary, offset, culled;
GLint factor;
GLfloat width;
GL2PSvertex *verts;
union {
GL2PSstring *text;
GL2PSimage *image;
} data;
} GL2PSprimitive;
typedef struct {
#if defined(GL2PS_HAVE_ZLIB)
Bytef *dest, *src, *start;
uLongf destLen, srcLen;
#else
int dummy;
#endif
} GL2PScompress;
typedef struct{
GL2PSlist* ptrlist;
int gsno, fontno, imno, shno, maskshno, trgroupno;
int gsobjno, fontobjno, imobjno, shobjno, maskshobjno, trgroupobjno;
} GL2PSpdfgroup;
typedef struct {
/* General */
GLint format, sort, options, colorsize, colormode, buffersize;
char *title, *producer, *filename;
GLboolean boundary, blending;
GLfloat *feedback, offset[2], lastlinewidth;
GLint viewport[4], blendfunc[2], lastfactor;
GL2PSrgba *colormap, lastrgba, threshold, bgcolor;
GLushort lastpattern;
GL2PSvertex lastvertex;
GL2PSlist *primitives, *auxprimitives;
FILE *stream;
GL2PScompress *compress;
GLboolean header;
/* BSP-specific */
GLint maxbestroot;
/* Occlusion culling-specific */
GLboolean zerosurfacearea;
GL2PSbsptree2d *imagetree;
GL2PSprimitive *primitivetoadd;
/* PDF-specific */
int streamlength;
GL2PSlist *pdfprimlist, *pdfgrouplist;
int *xreflist;
int objects_stack; /* available objects */
int extgs_stack; /* graphics state object number */
int font_stack; /* font object number */
int im_stack; /* image object number */
int trgroupobjects_stack; /* xobject numbers */
int shader_stack; /* shader object numbers */
int mshader_stack; /* mask shader object numbers */
/* for image map list */
GL2PSimagemap *imagemap_head;
GL2PSimagemap *imagemap_tail;
} GL2PScontext;
typedef struct {
void (*printHeader)(void);
void (*printFooter)(void);
void (*beginViewport)(GLint viewport[4]);
GLint (*endViewport)(void);
void (*printPrimitive)(void *data);
void (*printFinalPrimitive)(void);
const char *file_extensio