-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl2ps.c
6031 lines (5298 loc) · 179 KB
/
gl2ps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GL2PS, an OpenGL to PostScript Printing Library
* Copyright (C) 1999-2009 C. Geuzaine
*
* 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.
*
* For the latest info about gl2ps and a full list of contributors,
* see http://www.geuz.org/gl2ps/.
*
* Please report all bugs and problems to <[email protected]>.
*/
#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_extension;
const char *description;
} GL2PSbackend;
/* The gl2ps context. gl2ps is not thread safe (we should create a
local GL2PScontext during gl2psBeginPage) */
static GL2PScontext *gl2ps = NULL;
/* Need to forward-declare this one */
static GLint gl2psPrintPrimitives(void);
/*********************************************************************
*
* Utility routines
*
*********************************************************************/
static void gl2psMsg(GLint level, const char *fmt, ...)
{
va_list args;
if(!(gl2ps->options & GL2PS_SILENT)){
switch(level){
case GL2PS_INFO : fprintf(stderr, "GL2PS info: "); break;
case GL2PS_WARNING : fprintf(stderr, "GL2PS warning: "); break;
case GL2PS_ERROR : fprintf(stderr, "GL2PS error: "); break;
}
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
/* if(level == GL2PS_ERROR) exit(1); */
}
static void *gl2psMalloc(size_t size)
{
void *ptr;
if(!size) return NULL;
ptr = malloc(size);
if(!ptr){
gl2psMsg(GL2PS_ERROR, "Couldn't allocate requested memory");
return NULL;
}
return ptr;
}
static void *gl2psRealloc(void *ptr, size_t size)
{
if(!size) return NULL;
ptr = realloc(ptr, size);
if(!ptr){
gl2psMsg(GL2PS_ERROR, "Couldn't reallocate requested memory");
return NULL;
}
return ptr;
}
static void gl2psFree(void *ptr)
{
if(!ptr) return;
free(ptr);
}
static size_t gl2psWriteBigEndian(unsigned long data, size_t bytes)
{
size_t i;
size_t size = sizeof(unsigned long);
for(i = 1; i <= bytes; ++i){
fputc(0xff & (data >> (size-i) * 8), gl2ps->stream);
}
return bytes;
}
/* zlib compression helper routines */
#if defined(GL2PS_HAVE_ZLIB)
static void gl2psSetupCompress(void)
{
gl2ps->compress = (GL2PScompress*)gl2psMalloc(sizeof(GL2PScompress));
gl2ps->compress->src = NULL;
gl2ps->compress->start = NULL;
gl2ps->compress->dest = NULL;
gl2ps->compress->srcLen = 0;
gl2ps->compress->destLen = 0;
}
static void gl2psFreeCompress(void)
{
if(!gl2ps->compress)
return;
gl2psFree(gl2ps->compress->start);
gl2psFree(gl2ps->compress->dest);
gl2ps->compress->src = NULL;
gl2ps->compress->start = NULL;
gl2ps->compress->dest = NULL;
gl2ps->compress->srcLen = 0;
gl2ps->compress->destLen = 0;
}
static int gl2psAllocCompress(unsigned int srcsize)
{
gl2psFreeCompress();
if(!gl2ps->compress || !srcsize)
return GL2PS_ERROR;
gl2ps->compress->srcLen = srcsize;
gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12);
gl2ps->compress->src = (Bytef*)gl2psMalloc(gl2ps->compress->srcLen);
gl2ps->compress->start = gl2ps->compress->src;
gl2ps->compress->dest = (Bytef*)gl2psMalloc(gl2ps->compress->destLen);
return GL2PS_SUCCESS;
}
static void *gl2psReallocCompress(unsigned int srcsize)
{
if(!gl2ps->compress || !srcsize)
return NULL;
if(srcsize < gl2ps->compress->srcLen)
return gl2ps->compress->start;
gl2ps->compress->srcLen = srcsize;
gl2ps->compress->destLen = (int)ceil(1.001 * gl2ps->compress->srcLen + 12);
gl2ps->compress->src = (Bytef*)gl2psRealloc(gl2ps->compress->src,
gl2ps->compress->srcLen);
gl2ps->compress->start = gl2ps->compress->src;
gl2ps->compress->dest = (Bytef*)gl2psRealloc(gl2ps->compress->dest,
gl2ps->compress->destLen);
return gl2ps->compress->start;
}
static size_t gl2psWriteBigEndianCompress(unsigned long data, size_t bytes)
{
size_t i;
size_t size = sizeof(unsigned long);
for(i = 1; i <= bytes; ++i){
*gl2ps->compress->src = (Bytef)(0xff & (data >> (size-i) * 8));
++gl2ps->compress->src;
}
return bytes;
}
static int gl2psDeflate(void)
{
/* For compatibility with older zlib versions, we use compress(...)
instead of compress2(..., Z_BEST_COMPRESSION) */
return compress(gl2ps->compress->dest, &gl2ps->compress->destLen,
gl2ps->compress->start, gl2ps->compress->srcLen);
}
#endif
static int gl2psPrintf(const char* fmt, ...)
{
int ret;
va_list args;
#if defined(GL2PS_HAVE_ZLIB)
unsigned int oldsize = 0;
static char buf[1000];
if(gl2ps->options & GL2PS_COMPRESS){
va_start(args, fmt);
ret = vsprintf(buf, fmt, args);
va_end(args);
oldsize = gl2ps->compress->srcLen;
gl2ps->compress->start = (Bytef*)gl2psReallocCompress(oldsize + ret);
memcpy(gl2ps->compress->start+oldsize, buf, ret);
ret = 0;
}
else{
#endif
va_start(args, fmt);
ret = vfprintf(gl2ps->stream, fmt, args);
va_end(args);
#if defined(GL2PS_HAVE_ZLIB)
}
#endif
return ret;
}
static void gl2psPrintGzipHeader()
{
#if defined(GL2PS_HAVE_ZLIB)
char tmp[10] = {'\x1f', '\x8b', /* magic numbers: 0x1f, 0x8b */
8, /* compression method: Z_DEFLATED */
0, /* flags */
0, 0, 0, 0, /* time */
2, /* extra flags: max compression */
'\x03'}; /* OS code: 0x03 (Unix) */
if(gl2ps->options & GL2PS_COMPRESS){
gl2psSetupCompress();
/* add the gzip file header */
fwrite(tmp, 10, 1, gl2ps->stream);
}
#endif
}
static void gl2psPrintGzipFooter()
{
#if defined(GL2PS_HAVE_ZLIB)
int n;
uLong crc, len;
char tmp[8];
if(gl2ps->options & GL2PS_COMPRESS){
if(Z_OK != gl2psDeflate()){
gl2psMsg(GL2PS_ERROR, "Zlib deflate error");
}
else{
/* determine the length of the header in the zlib stream */
n = 2; /* CMF+FLG */
if(gl2ps->compress->dest[1] & (1<<5)){
n += 4; /* DICTID */
}
/* write the data, without the zlib header and footer */
fwrite(gl2ps->compress->dest+n, gl2ps->compress->destLen-(n+4),
1, gl2ps->stream);
/* add the gzip file footer */
crc = crc32(0L, gl2ps->compress->start, gl2ps->compress->srcLen);
for(n = 0; n < 4; ++n){
tmp[n] = (char)(crc & 0xff);
crc >>= 8;
}
len = gl2ps->compress->srcLen;
for(n = 4; n < 8; ++n){
tmp[n] = (char)(len & 0xff);
len >>= 8;
}
fwrite(tmp, 8, 1, gl2ps->stream);
}
gl2psFreeCompress();
gl2psFree(gl2ps->compress);
gl2ps->compress = NULL;
}
#endif
}
/* The list handling routines */
static void gl2psListRealloc(GL2PSlist *list, GLint n)
{
if(!list){
gl2psMsg(GL2PS_ERROR, "Cannot reallocate NULL list");
return;
}
if(n <= 0) return;
if(!list->array){
list->nmax = n;
list->array = (char*)gl2psMalloc(list->nmax * list->size);
}
else{
if(n > list->nmax){
list->nmax = ((n - 1) / list->incr + 1) * list->incr;
list->array = (char*)gl2psRealloc(list->array,
list->nmax * list->size);
}
}
}
static GL2PSlist *gl2psListCreate(GLint n, GLint incr, GLint size)
{
GL2PSlist *list;
if(n < 0) n = 0;
if(incr <= 0) incr = 1;
list = (GL2PSlist*)gl2psMalloc(sizeof(GL2PSlist));
list->nmax = 0;
list->incr = incr;
list->size = size;
list->n = 0;
list->array = NULL;
gl2psListRealloc(list, n);
return list;
}
static void gl2psListReset(GL2PSlist *list)
{
if(!list) return;
list->n = 0;
}
static void gl2psListDelete(GL2PSlist *list)
{
if(!list) return;
gl2psFree(list->array);
gl2psFree(list);
}
static void gl2psListAdd(GL2PSlist *list, void *data)
{
if(!list){
gl2psMsg(GL2PS_ERROR, "Cannot add into unallocated list");
return;
}
list->n++;
gl2psListRealloc(list, list->n);
memcpy(&list->array[(list->n - 1) * list->size], data, list->size);
}
static int gl2psListNbr(GL2PSlist *list)
{
if(!list)
return 0;
return list->n;
}
static void *gl2psListPointer(GL2PSlist *list, GLint index)
{
if(!list){
gl2psMsg(GL2PS_ERROR, "Cannot point into unallocated list");
return NULL;
}
if((index < 0) || (index >= list->n)){
gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListPointer");
return NULL;
}
return &list->array[index * list->size];
}
static void gl2psListSort(GL2PSlist *list,
int (*fcmp)(const void *a, const void *b))
{
if(!list)
return;
qsort(list->array, list->n, list->size, fcmp);
}
static void gl2psListAction(GL2PSlist *list, void (*action)(void *data))
{
GLint i;
for(i = 0; i < gl2psListNbr(list); i++){
(*action)(gl2psListPointer(list, i));
}
}
static void gl2psListActionInverse(GL2PSlist *list, void (*action)(void *data))
{
GLint i;
for(i = gl2psListNbr(list); i > 0; i--){
(*action)(gl2psListPointer(list, i-1));
}
}
#if defined(GL2PS_HAVE_LIBPNG)
static void gl2psListRead(GL2PSlist *list, int index, void *data)
{
if((index < 0) || (index >= list->n))
gl2psMsg(GL2PS_ERROR, "Wrong list index in gl2psListRead");
memcpy(data, &list->array[index * list->size], list->size);
}
static void gl2psEncodeBase64Block(unsigned char in[3], unsigned char out[4], int len)
{
static const char cb64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
out[0] = cb64[ in[0] >> 2 ];
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=';
out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] : '=';
}
static void gl2psListEncodeBase64(GL2PSlist *list)
{
unsigned char *buffer, in[3], out[4];
int i, n, index, len;
n = list->n * list->size;
buffer = (unsigned char*)gl2psMalloc(n * sizeof(unsigned char));
memcpy(buffer, list->array, n * sizeof(unsigned char));
gl2psListReset(list);
index = 0;
while(index < n) {
len = 0;
for(i = 0; i < 3; i++) {
if(index < n){
in[i] = buffer[index];
len++;
}
else{
in[i] = 0;
}
index++;
}
if(len) {
gl2psEncodeBase64Block(in, out, len);
for(i = 0; i < 4; i++)
gl2psListAdd(list, &out[i]);
}
}
gl2psFree(buffer);
}
#endif
/* Helpers for rgba colors */
static GLboolean gl2psSameColor(GL2PSrgba rgba1, GL2PSrgba rgba2)
{
if(!GL2PS_ZERO(rgba1[0] - rgba2[0]) ||
!GL2PS_ZERO(rgba1[1] - rgba2[1]) ||
!GL2PS_ZERO(rgba1[2] - rgba2[2]))
return GL_FALSE;
return GL_TRUE;
}
static GLboolean gl2psVertsSameColor(const GL2PSprimitive *prim)
{
int i;
for(i = 1; i < prim->numverts; i++){
if(!gl2psSameColor(prim->verts[0].rgba, prim->verts[i].rgba)){
return GL_FALSE;
}
}
return GL_TRUE;
}
static GLboolean gl2psSameColorThreshold(int n, GL2PSrgba rgba[],
GL2PSrgba threshold)
{
int i;
if(n < 2) return GL_TRUE;
for(i = 1; i < n; i++){
if(fabs(rgba[0][0] - rgba[i][0]) > threshold[0] ||
fabs(rgba[0][1] - rgba[i][1]) > threshold[1] ||
fabs(rgba[0][2] - rgba[i][2]) > threshold[2])
return GL_FALSE;
}
return GL_TRUE;
}
static void gl2psSetLastColor(GL2PSrgba rgba)
{
int i;
for(i = 0; i < 3; ++i){
gl2ps->lastrgba[i] = rgba[i];
}
}
static GLfloat gl2psGetRGB(GL2PSimage *im, GLuint x, GLuint y,
GLfloat *red, GLfloat *green, GLfloat *blue)
{
GLsizei width = im->width;
GLsizei height = im->height;
GLfloat *pixels = im->pixels;
GLfloat *pimag;
/* OpenGL image is from down to up, PS image is up to down */
switch(im->format){
case GL_RGBA:
pimag = pixels + 4 * (width * (height - 1 - y) + x);
break;
case GL_RGB:
default:
pimag = pixels + 3 * (width * (height - 1 - y) + x);
break;
}
*red = *pimag; pimag++;
*green = *pimag; pimag++;
*blue = *pimag; pimag++;
return (im->format == GL_RGBA) ? *pimag : 1.0F;
}
/* Helper routines for pixmaps */
static GL2PSimage *gl2psCopyPixmap(GL2PSimage *im)
{
int size;
GL2PSimage *image = (GL2PSimage*)gl2psMalloc(sizeof(GL2PSimage));
image->width = im->width;
image->height = im->height;
image->format = im->format;
image->type = im->type;
switch(image->format){
case GL_RGBA:
size = image->height * image->width * 4 * sizeof(GLfloat);
break;
case GL_RGB:
default:
size = image->height * image->width * 3 * sizeof(GLfloat);
break;
}
image->pixels = (GLfloat*)gl2psMalloc(size);
memcpy(image->pixels, im->pixels, size);
return image;
}
static void gl2psFreePixmap(GL2PSimage *im)
{
if(!im)
return;
gl2psFree(im->pixels);
gl2psFree(im);
}
#if defined(GL2PS_HAVE_LIBPNG)
#if !defined(png_jmpbuf)
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
static void gl2psUserWritePNG(png_structp png_ptr, png_bytep data, png_size_t length)
{
unsigned int i;
GL2PSlist *png = (GL2PSlist*)png_get_io_ptr(png_ptr);
for(i = 0; i < length; i++)
gl2psListAdd(png, &data[i]);
}
static void gl2psUserFlushPNG(png_structp png_ptr)
{
}
static void gl2psConvertPixmapToPNG(GL2PSimage *pixmap, GL2PSlist *png)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned char *row_data;
GLfloat dr, dg, db;
int row, col;
if(!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
return;
if(!(info_ptr = png_create_info_struct(png_ptr))){
png_destroy_write_struct(&png_ptr, NULL);
return;
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_set_write_fn(png_ptr, (void *)png, gl2psUserWritePNG, gl2psUserFlushPNG);
png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
png_set_IHDR(png_ptr, info_ptr, pixmap->width, pixmap->height, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
row_data = (unsigned char*)gl2psMalloc(3 * pixmap->width * sizeof(unsigned char));
for(row = 0; row < pixmap->height; row++){
for(col = 0; col < pixmap->width; col++){
gl2psGetRGB(pixmap, col, row, &dr, &dg, &db);
row_data[3*col] = (unsigned char)(255. * dr);
row_data[3*col+1] = (unsigned char)(255. * dg);
row_data[3*col+2] = (unsigned char)(255. * db);
}
png_write_row(png_ptr, (png_bytep)row_data);
}
gl2psFree(row_data);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
}
#endif
/* Helper routines for text strings */
static GLint gl2psAddText(GLint type, const char *str, const char *fontname,
GLshort fontsize, GLint alignment, GLfloat angle)
{
GLfloat pos[4];
GL2PSprimitive *prim;
GLboolean valid;
if(!gl2ps || !str || !fontname) return GL2PS_UNINITIALIZED;
if(gl2ps->options & GL2PS_NO_TEXT) return GL2PS_SUCCESS;
glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
if(GL_FALSE == valid) return GL2PS_SUCCESS; /* the primitive is culled */
glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive));
prim->type = type;
prim->boundary = 0;
prim->numverts = 1;
prim->verts = (GL2PSvertex*)gl2psMalloc(sizeof(GL2PSvertex));
prim->verts[0].xyz[0] = pos[0];
prim->verts[0].xyz[1] = pos[1];
prim->verts[0].xyz[2] = pos[2];
prim->culled = 0;
prim->offset = 0;
prim->pattern = 0;
prim->factor = 0;
prim->width = 1;
glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
prim->data.text = (GL2PSstring*)gl2psMalloc(sizeof(GL2PSstring));
prim->data.text->str = (char*)gl2psMalloc((strlen(str)+1)*sizeof(char));
strcpy(prim->data.text->str, str);
prim->data.text->fontname = (char*)gl2psMalloc((strlen(fontname)+1)*sizeof(char));
strcpy(prim->data.text->fontname, fontname);
prim->data.text->fontsize = fontsize;
prim->data.text->alignment = alignment;
prim->data.text->angle = angle;
gl2psListAdd(gl2ps->auxprimitives, &prim);
glPassThrough(GL2PS_TEXT_TOKEN);
return GL2PS_SUCCESS;
}
static GL2PSstring *gl2psCopyText(GL2PSstring *t)
{
GL2PSstring *text = (GL2PSstring*)gl2psMalloc(sizeof(GL2PSstring));
text->str = (char*)gl2psMalloc((strlen(t->str)+1)*sizeof(char));
strcpy(text->str, t->str);
text->fontname = (char*)gl2psMalloc((strlen(t->fontname)+1)*sizeof(char));
strcpy(text->fontname, t->fontname);
text->fontsize = t->fontsize;
text->alignment = t->alignment;
text->angle = t->angle;
return text;
}
static void gl2psFreeText(GL2PSstring *text)
{
if(!text)
return;
gl2psFree(text->str);
gl2psFree(text->fontname);
gl2psFree(text);
}
/* Helpers for blending modes */
static GLboolean gl2psSupportedBlendMode(GLenum sfactor, GLenum dfactor)
{
/* returns TRUE if gl2ps supports the argument combination: only two
blending modes have been implemented so far */
if( (sfactor == GL_SRC_ALPHA && dfactor == GL_ONE_MINUS_SRC_ALPHA) ||
(sfactor == GL_ONE && dfactor == GL_ZERO) )
return GL_TRUE;
return GL_FALSE;
}
static void gl2psAdaptVertexForBlending(GL2PSvertex *v)
{
/* Transforms vertex depending on the actual blending function -
currently the vertex v is considered as source vertex and his
alpha value is changed to 1.0 if source blending GL_ONE is
active. This might be extended in the future */
if(!v || !gl2ps)
return;
if(gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending){
v->rgba[3] = 1.0F;
return;
}
switch(gl2ps->blendfunc[0]){
case GL_ONE:
v->rgba[3] = 1.0F;
break;
default:
break;
}
}
static void gl2psAssignTriangleProperties(GL2PStriangle *t)
{
/* int i; */
t->prop = T_VAR_COLOR;
/* Uncommenting the following lines activates an even more fine
grained distinction between triangle types - please don't delete,
a remarkable amount of PDF handling code inside this file depends
on it if activated */
/*
t->prop = T_CONST_COLOR;
for(i = 0; i < 3; ++i){
if(!GL2PS_ZERO(t->vertex[0].rgba[i] - t->vertex[1].rgba[i]) ||
!GL2PS_ZERO(t->vertex[1].rgba[i] - t->vertex[2].rgba[i])){
t->prop = T_VAR_COLOR;
break;
}
}
*/
if(!GL2PS_ZERO(t->vertex[0].rgba[3] - t->vertex[1].rgba[3]) ||
!GL2PS_ZERO(t->vertex[1].rgba[3] - t->vertex[2].rgba[3])){
t->prop |= T_VAR_ALPHA;
}
else{
if(t->vertex[0].rgba[3] < 1)
t->prop |= T_ALPHA_LESS_1;
else
t->prop |= T_ALPHA_1;
}
}
static void gl2psFillTriangleFromPrimitive(GL2PStriangle *t, GL2PSprimitive *p,
GLboolean assignprops)
{
t->vertex[0] = p->verts[0];
t->vertex[1] = p->verts[1];
t->vertex[2] = p->verts[2];
if(GL_TRUE == assignprops)
gl2psAssignTriangleProperties(t);
}
static void gl2psInitTriangle(GL2PStriangle *t)
{
int i;
GL2PSvertex vertex = { {-1.0F, -1.0F, -1.0F}, {-1.0F, -1.0F, -1.0F, -1.0F} };
for(i = 0; i < 3; i++)
t->vertex[i] = vertex;
t->prop = T_UNDEFINED;
}
/* Miscellaneous helper routines */
static GL2PSprimitive *gl2psCopyPrimitive(GL2PSprimitive *p)
{
GL2PSprimitive *prim;