File: gd.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (1563 lines) | stat: -rw-r--r-- 49,143 bytes parent folder | download | duplicates (7)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
/* $Id: gd.c,v 1.7 2010/10/06 16:02:49 rice Exp $

         PNG, GIF, and JPEG device driver based on libgd

   Copyright (C) 2004  Joao Cardoso
   Copyright (C) 2002, 2003, 2004  Andrew Roach

   This file is part of PLplot.

   PLplot is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published
   by the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   PLplot 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 the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with PLplot; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*             GIF SUPPORT
 *
 *  Following the expiration of Unisys's worldwide patents on lzw compression
 *  GD 2.0.28+ have reinstated support for GIFs, and so support for this
 *  format has been added to the GD family of drivers. GIF's only support
 *  1, 4 and 8 bit, so no truecolour. Why would you want GIFs though ? PNG is
 *  a far superior format, not only giving you 1,4,8 and 24 bit, but also
 *  better compression and just about all browsers now support them.
 */

/*
 *  The GD drivers, PNG, GIF, and JPEG, support a number of different options
 *  depending on the version of GD installed.
 *
 *  If you have installed GD Ver 2.+ you gain support for truecolour (24
 *  bit, 16 millionish) modes as well as different line widths. These
 *  capibilities are part of GD more so than the GD driver, so they aren't
 *  available in any 1.? versions of the driver.
 *
 *  24 bit support is, by default, set to "auto" if you have V2.+ of GD.
 *  What this means is the *driver* decides when to use 24 bit or 8 bit
 *  modes for PNG files. The logic is rather simple - if you have less than
 *  257 colours, it is set to 8 bit mode, if more then it's in 24 bit mode.
 *  This should work fine for most people, most of the time, in most
 *  situations; however, it can be overridden in case it has to via the
 *  "-drvopt" command line switch. The png driver has two related settings:
 *              8bit    and
 *              24bit
 *
 *  If either of these command line toggles are set, that mode becomes the
 *  standard used regardless of the number of colours used. It can be envoked
 *  as follows:
 *                 x08c -dev png -drvopt 8bit -fam -o 8bitpng
 *                                      or
 *                 x08c -dev png -drvopt 24bit -fam -o 24bitpng
 *
 *  NOTE:
 *  The 24 bit PNG file is an RGBA file, not RGB - it includes alpha channel
 *  (transparency). Transparency is set to opaque, but the fact it is an
 *  RGBA and not an RGB might cause some problems with some viewers.
 *  Sadly, I can't do anything about it... sorry.
 *
 *  GIF files can only have 256 colours, so naturally truecolour mode is not
 *  supported for this sub-driver.
 *
 *  Stuff for GD V1.? as well as V2.+
 *
 *  optimise
 *
 *  From version 1.17 of the GD driver, a command line option has been
 *  added to try and optimise the PNG files. If successful, the optimise
 *  command will create 4 bit (16 colour) PNGs instead of 8 bit (256 colour)
 *  ones. This results in slightly smaller files with no loss in any colour
 *  information. The function has no real memory overhead, but does have a
 *  slight speed hit in exchange for the optimisation. For example:
 *         x08c -dev png -drvopt 8bit,optimise -fam -o 8bitpng
 *  forces the png driver to make 8bit pngs, and will then optimise any PNG
 *  images with 16 or less colours into a 4 bit PNG. Note, this DOESN'T WORK
 *  WITH 24bit PNGs yet, and will never work with JPEGs.
 *
 *
 *  Also as of version 1.17 of the GD driver, the options for palette
 *  modification previously set with the command line option "-hack" have
 *  now been moved to two options settable from the -drvopt switch.
 *
 *  def_black15
 *
 *  -drvopt def_black15 sets index 15, usually white, to black if index 0,
 *  the background colour and usually black, has been set to white from the
 *  command line option -bg
 *
 *  swp_red15
 *
 *  -drvopt swp_red15 swaps index 15, usually white, with index 1, which is
 *  usually red. This might be desirable occasionally, but it is principally
 *  included for cases when the background has been set on the command line
 *  to white, and the "def_black15" option has been issued to redefine index
 *  15 as black. By issuing a command like:
 *                 x08c -dev png -bg ffffff -drvopt def_black15,swp_red15
 *  the driver will set the background to white, then redefine index 15 of
 *  cmap0, which is usually white to black, then swap index 2 (red) to 15
 *  (white originally, now black), so at the end of the day, the "default"
 *  plotting colour is now black. Why do all of this ? It is a very quick
 *  way of making a nice web-friendly png without having to redefine the
 *  cmaps within your program.
 *
 *  smooth
 *
 *  -drvopt smooth=2 turns on anti-aliased line and polygong drawing if
 *  you are using a 24bit mode. Unfortunately gd doesn't honour line
 *  width when anti-aliasing, so by default it is off.
 */


#include "plDevs.h"


#if defined(PLD_png) || defined(PLD_jpeg) || defined(PLD_gif)

#include "plplotP.h"
#include "drivers.h"

#include <gd.h>

/*  Device info
 *
 *  Don't knoq if all this logic is necessary, but basically we are going to
 *  start with all three sub-drivers present, then work out way down to two
 *  and finally one of each.
 */

const char* plD_DEVICE_INFO_gd = 
#if defined(PLD_png)
  "png:PNG file:0:gd:39:png\n"
#endif
#if defined(PLD_jpeg)
  "jpeg:JPEG file:0:gd:40:jpeg\n"
#endif
#if defined(PLD_gif)
  "gif:GIF file:0:gd:47:gif"
#endif
;


#ifdef HAVE_FREETYPE

/*
 *  Freetype support has been added to the GD family of drivers using the
 *  plfreetype.c module, and implemented as a driver-specific optional extra
 *  invoked via the -drvopt command line toggle. It uses the
 *  "PLESC_HAS_TEXT" command for rendering within the driver.
 *
 *  Freetype support is turned on/off at compile time by defining
 *  "HAVE_FREETYPE".
 *
 *  To give the user some level of control over the fonts that are used,
 *  environmental variables can be set to over-ride the definitions used by
 *  the five default plplot fonts.
 *
 *  Freetype rendering is used with the command line "-drvopt text".
 *  Anti-aliased fonts can be used by issuing "-drvopt text,smooth"
 */

#include "plfreetype.h"

#endif

/* Prototypes for functions in this file. */

static void	fill_polygon	(PLStream *pls);
static void	setcmap		(PLStream *pls);
static void     plD_init_png_Dev(PLStream *pls);
static void     plD_gd_optimise (PLStream *pls);
static void     plD_black15_gd  (PLStream *pls);
static void     plD_red15_gd    (PLStream *pls);
#ifdef PLD_gif
static void     plD_init_gif_Dev(PLStream *pls);
#endif

#ifdef HAVE_FREETYPE

static void plD_pixel_gd (PLStream *pls, short x, short y);
static PLINT plD_read_pixel_gd (PLStream *pls, short x, short y);
static void plD_set_pixel_gd (PLStream *pls, short x, short y, PLINT colour);
static void init_freetype_lv1 (PLStream *pls);
static void init_freetype_lv2 (PLStream *pls);

extern void plD_FreeType_init(PLStream *pls);
extern void plD_render_freetype_text (PLStream *pls, EscText *args);
extern void plD_FreeType_Destroy(PLStream *pls);
extern void pl_set_extended_cmap0(PLStream *pls, int ncol0_width, int ncol0_org);

#endif

/* top level declarations */

static int NCOLOURS=gdMaxColors;

/* In an attempt to fix a problem with the hidden line removal functions
 * that results in hidden lines *not* being removed from "small" plot
 * pages (ie, like a normal video screen), a "virtual" page of much
 * greater size is used to trick the algorithm into working correctly.
 * If, in future, this gets fixed on its own, then don't define
 * "use_experimental_hidden_line_hack"
 */

#define use_experimental_hidden_line_hack

/* I think the current version of Freetype supports up to a maximum of
 * 128 grey levels for text smoothing. You can get quite acceptable
 * results with as few as 4 grey-levels. Uusually only about 5 get used
 * anyway, but the question is where, in the "grey spectrum" will they be ?
 * Who knows ? The following define lets you set a maximum limit on the
 * number of grey-levels used. It is really only here for the 24bit mode
 * and could be set to 255, but that would slow things down and use more
 * memory. 64 seems to be a nice compromise, but if you want to change it,
 * then change it here.
 */

#ifndef max_number_of_grey_levels_used_in_text_smoothing
#define max_number_of_grey_levels_used_in_text_smoothing 64
#endif

/* Not present in versions before 2.0 */

#ifndef gdImagePalettePixel
#define gdImagePalettePixel(  im, x, y )   (im)->pixels[(y)][(x)]
#endif

/* Struct to hold device-specific info. */

typedef struct {

        gdImagePtr im_out;                      /* Graphics pointer */
        PLINT pngx;
        PLINT pngy;

        int colour;                             /* Current Colour               */
        int totcol;                             /* Total number of colours      */
        int ncol1;                              /* Actual size of ncol1 we got  */

        int scale;                              /* scaling factor to "blow up" to */
                                                /* the "virtual" page in removing hidden lines*/

        int optimise;                           /* Flag used for 4bit pngs */
        int black15;                            /* Flag used for forcing a black colour */
        int red15;                              /* Flag for swapping red and 15 */

#if GD2_VERS >= 2
        int truecolour;                         /* Flag to ALWAYS force 24 bit mode */
        int palette;                            /* Flag to ALWAYS force  8 bit mode */
        unsigned char smooth;                   /* Flag to ask for line smoothing */
	unsigned char Padding[3];
#else
        unsigned char TRY_BLENDED_ANTIALIASING;  /* Flag to try and set up BLENDED ANTIALIASING */
	unsigned char Padding[3];
#endif


} png_Dev;

void plD_init_png		(PLStream *);
void plD_line_png		(PLStream *, short, short, short, short);
void plD_polyline_png		(PLStream *, short *, short *, PLINT);
void plD_eop_png		(PLStream *);
void plD_eop_jpeg		(PLStream *);
void plD_bop_png		(PLStream *);
void plD_tidy_png		(PLStream *);
void plD_state_png		(PLStream *, PLINT);
void plD_esc_png		(PLStream *, PLINT, void *);
#ifdef PLD_gif
void plD_init_gif		(PLStream *);
void plD_eop_gif		(PLStream *);
#endif

#ifdef PLD_png

void plD_dispatch_init_png( PLDispatchTable *pdt )
{
#ifndef ENABLE_DYNDRIVERS
    pdt->pl_MenuStr  = "PNG file";
    pdt->pl_DevName  = "png";
#endif
    pdt->pl_type     = plDevType_FileOriented;
    pdt->pl_seq      = 39;
    pdt->pl_init     = (plD_init_fp)     plD_init_png;
    pdt->pl_line     = (plD_line_fp)     plD_line_png;
    pdt->pl_polyline = (plD_polyline_fp) plD_polyline_png;
    pdt->pl_eop      = (plD_eop_fp)      plD_eop_png;
    pdt->pl_bop      = (plD_bop_fp)      plD_bop_png;
    pdt->pl_tidy     = (plD_tidy_fp)     plD_tidy_png;
    pdt->pl_state    = (plD_state_fp)    plD_state_png;
    pdt->pl_esc      = (plD_esc_fp)      plD_esc_png;
}

#endif

#ifdef PLD_jpeg

void plD_dispatch_init_jpeg( PLDispatchTable *pdt )
{
#ifndef ENABLE_DYNDRIVERS
    pdt->pl_MenuStr  = "JPEG File";
    pdt->pl_DevName  = "jpeg";
#endif
    pdt->pl_type     = plDevType_FileOriented;
    pdt->pl_seq      = 40;
    pdt->pl_init     = (plD_init_fp)     plD_init_png;
    pdt->pl_line     = (plD_line_fp)     plD_line_png;
    pdt->pl_polyline = (plD_polyline_fp) plD_polyline_png;
    pdt->pl_eop      = (plD_eop_fp)      plD_eop_jpeg;
    pdt->pl_bop      = (plD_bop_fp)      plD_bop_png;
    pdt->pl_tidy     = (plD_tidy_fp)     plD_tidy_png;
    pdt->pl_state    = (plD_state_fp)    plD_state_png;
    pdt->pl_esc      = (plD_esc_fp)      plD_esc_png;
}
#endif


#ifdef PLD_gif

void plD_dispatch_init_gif( PLDispatchTable *pdt )
{
#ifndef ENABLE_DYNDRIVERS
    pdt->pl_MenuStr  = "GIF File";
    pdt->pl_DevName  = "gif";
#endif
    pdt->pl_type     = plDevType_FileOriented;
    pdt->pl_seq      = 47;
    pdt->pl_init     = (plD_init_fp)     plD_init_gif;
    pdt->pl_line     = (plD_line_fp)     plD_line_png;
    pdt->pl_polyline = (plD_polyline_fp) plD_polyline_png;
    pdt->pl_eop      = (plD_eop_fp)      plD_eop_gif;
    pdt->pl_bop      = (plD_bop_fp)      plD_bop_png;
    pdt->pl_tidy     = (plD_tidy_fp)     plD_tidy_png;
    pdt->pl_state    = (plD_state_fp)    plD_state_png;
    pdt->pl_esc      = (plD_esc_fp)      plD_esc_png;
}
#endif


/*--------------------------------------------------------------------------*\
 * plD_init_png_Dev()
 *
\*--------------------------------------------------------------------------*/

static void
plD_init_png_Dev(PLStream *pls)
{
    png_Dev *dev;

/*  Stuff for the driver options, these vars are copied into the driver
 *  structure so that everything is thread safe and reenterant.
 */

    static int optimise=0;
    static int black15=0;
    static int red15=0;
#if GD2_VERS >= 2
    static int truecolour=0;
    static int palette=0;
#endif
#ifdef HAVE_FREETYPE
    static int freetype=1;
    static int smooth_text=1;
    FT_Data *FT;
#endif

    DrvOpt gd_options[] = {{"optimise", DRV_INT, 0, &optimise, "Optimise PNG palette when possible"},
                              {"def_black15", DRV_INT, 0, &black15, "Define idx 15 as black. If the background is \"whiteish\" (from \"-bg\" option), force index 15 (traditionally white) to be \"black\""},
                              {"swp_red15", DRV_INT, 0, &red15, "Swap index 1 (usually red) and 1 (usually white); always done after \"black15\"; quite useful for quick changes to web pages"},
#if GD2_VERS >= 2
                              {"8bit", DRV_INT, 0, &palette, "Palette (8 bit) mode"},
                              {"24bit", DRV_INT, 0, &truecolour, "Truecolor (24 bit) mode"},
#endif
#ifdef HAVE_FREETYPE
                              {"text", DRV_INT, 0, &freetype, "Use driver text (FreeType)"},
                              {"smooth", DRV_INT, 0, &smooth_text, "Turn text smoothing on (1) or off (0)"},
#endif
			      {NULL, DRV_INT, 0, NULL, NULL}};


/* Allocate and initialize device-specific data */

    if (pls->dev != NULL)
	free((void *) pls->dev);

    pls->dev = calloc(1, (size_t) sizeof(png_Dev));
    if (pls->dev == NULL)
	plexit("plD_init_png_Dev: Out of memory.");

    dev = (png_Dev *) pls->dev;

    dev->colour=1;  /* Set a fall back pen colour in case user doesn't */

/*
 *  Set the compression/quality level for JPEG files
 *  The higher the value, the bigger/better the image is
 */
    if ( (pls->dev_compression<=0)||(pls->dev_compression>99) )
       pls->dev_compression=90;

/* Check for and set up driver options */

    plParseDrvOpts(gd_options);

    dev->black15=black15;
    dev->red15=red15;
    dev->optimise=optimise;

#if GD2_VERS >= 2

    dev->palette=palette;
    dev->truecolour=truecolour;



    if ((dev->truecolour>0) && (dev->palette>0))
       plwarn("Selecting both \"truecolor\" AND \"palette\" driver options is contradictory, so\nI will just use my best judgment.\n");
    else if (dev->truecolour>0)
       NCOLOURS=16777216;
    else if ((dev->truecolour==0)&&(dev->palette==0)&&((pls->ncol1+pls->ncol0)>NCOLOURS))
       {
        NCOLOURS=16777216;
       }

#ifdef HAVE_FREETYPE
    if ((dev->palette==0)&&(dev->optimise==0)&&(smooth_text>1)) dev->smooth=1; /* Allow smoothing of lines if we have a truecolour device */
#endif

#endif

#ifdef HAVE_FREETYPE
if (freetype)
   {
    pls->dev_text = 1; /* want to draw text */
    pls->dev_unicode = 1; /* want unicode */

    /* As long as we aren't optimising, we'll try to use better antialaising
     * We can also only do this if the user wants smoothing, and hasn't
     * selected a palette mode.
     */


    init_freetype_lv1(pls);
    FT=(FT_Data *)pls->FT;
    FT->want_smooth_text=smooth_text>0 ? 1 : 0;
    if ((dev->optimise==0)&&(dev->palette==0)&&(smooth_text!=0))
      {
        FT->BLENDED_ANTIALIASING=1;
        dev->truecolour=1;
      }
   }

#endif
}

/*----------------------------------------------------------------------*\
 * plD_init_png()
 *
 * Initialize device.
\*----------------------------------------------------------------------*/

void plD_init_png(PLStream *pls)
{
    png_Dev *dev=NULL;

    pls->termin = 0;            /* Not an interactive device */
    pls->icol0 = 1;
    pls->bytecnt = 0;
    pls->page = 0;
    pls->dev_fill0 = 1;         /* Can do solid fills */
    pls->family = 1;            /* pmr:  allow multiple pages. */

    if (!pls->colorset)
	pls->color = 1;         /* Is a color device */

/* Initialize family file info */
    plFamInit(pls);

/* Prompt for a file name if not already set */
    plOpenFile(pls);

/* Allocate and initialize device-specific data */
    plD_init_png_Dev(pls);
    dev=(png_Dev *)pls->dev;

      if (pls->xlength <= 0 || pls->ylength <=0)
      {
/* use default width, height of 800x600 if not specifed by -geometry option
 * or plspage */
	 plspage(0., 0., 800, 600, 0, 0);
      }

     pls->graphx = GRAPHICS_MODE;

     dev->pngx = pls->xlength - 1;	/* should I use -1 or not??? */
     dev->pngy = pls->ylength - 1;

#ifdef use_experimental_hidden_line_hack

     if (dev->pngx>dev->pngy)    /* Work out the scaling factor for the  */
        {                        /* "virtual" (oversized) page           */
        dev->scale=PIXELS_X/dev->pngx;
        }
     else
        {
        dev->scale=PIXELS_Y/dev->pngy;
        }
#else

     dev->scale=1;

#endif


     if (pls->xdpi<=0)
     {
/* This corresponds to a typical monitor resolution of 4 pixels/mm. */
        plspage(4.*25.4, 4.*25.4, 0, 0, 0, 0);
     }
     else
     {
        pls->ydpi=pls->xdpi;        /* Set X and Y dpi's to the same value */
     }
/* Convert DPI to pixels/mm */
     plP_setpxl(dev->scale*pls->xdpi/25.4,dev->scale*pls->ydpi/25.4);

     plP_setphy(0, dev->scale*dev->pngx, 0, dev->scale*dev->pngy);

#ifdef HAVE_FREETYPE
if (pls->dev_text)
   {
    init_freetype_lv2(pls);
   }
#endif

}


#ifdef PLD_gif

/*--------------------------------------------------------------------------*\
 * plD_init_gif_Dev()
 *
 * We need a new initialiser for the GIF version of the GD driver because
 * the GIF one does not support TRUECOLOUR
\*--------------------------------------------------------------------------*/

static void
plD_init_gif_Dev(PLStream *pls)
{
    png_Dev *dev;

/*  Stuff for the driver options, these vars are copied into the driver
 *  structure so that everything is thread safe and reenterant.
 */

    static int black15=0;
    static int red15=0;
#ifdef HAVE_FREETYPE
    static int freetype=1;
    static int smooth_text=1;
    FT_Data *FT;
#endif

    DrvOpt gd_options[] = {{"def_black15", DRV_INT, 0, &black15, "Define idx 15 as black. If the background is \"whiteish\" (from \"-bg\" option), force index 15 (traditionally white) to be \"black\""},
                              {"swp_red15", DRV_INT, 0, &red15, "Swap index 1 (usually red) and 1 (usually white); always done after \"black15\"; quite useful for quick changes to web pages"},
#ifdef HAVE_FREETYPE
                              {"text", DRV_INT, 0, &freetype, "Use driver text (FreeType)"},
                              {"smooth", DRV_INT, 0, &smooth_text, "Turn text smoothing on (1) or off (0)"},
#endif
			      {NULL, DRV_INT, 0, NULL, NULL}};


/* Allocate and initialize device-specific data */

    if (pls->dev != NULL)
	free((void *) pls->dev);

    pls->dev = calloc(1, (size_t) sizeof(png_Dev));
    if (pls->dev == NULL)
	plexit("plD_init_gif_Dev: Out of memory.");

    dev = (png_Dev *) pls->dev;

    dev->colour=1;  /* Set a fall back pen colour in case user doesn't */

/* Check for and set up driver options */

    plParseDrvOpts(gd_options);

    dev->black15=black15;
    dev->red15=red15;

    dev->optimise=0;    /* Optimise does not work for GIFs... should, but it doesn't */

#if GD2_VERS >= 2
    dev->palette=1;     /* Always use palette mode for GIF files */
    dev->truecolour=0;  /* Never have truecolour in GIFS */
#endif

#ifdef HAVE_FREETYPE
if (freetype)
   {
    pls->dev_text = 1; /* want to draw text */
    pls->dev_unicode = 1; /* want unicode */

    init_freetype_lv1(pls);
    FT=(FT_Data *)pls->FT;

    FT->want_smooth_text=smooth_text > 0 ? 1 : 0;
   }

#endif
}

/*----------------------------------------------------------------------*\
 * plD_init_gif()
 *
 * Initialize device.
\*----------------------------------------------------------------------*/

void plD_init_gif(PLStream *pls)
{
    png_Dev *dev=NULL;

    pls->termin = 0;            /* Not an interactive device */
    pls->icol0 = 1;
    pls->bytecnt = 0;
    pls->page = 0;
    pls->dev_fill0 = 1;         /* Can do solid fills */
    pls->family = 1;            /* pmr:  allow multiple pages. */

    if (!pls->colorset)
	pls->color = 1;         /* Is a color device */

/* Initialize family file info */
    plFamInit(pls);

/* Prompt for a file name if not already set */
    plOpenFile(pls);

/* Allocate and initialize device-specific data */
    plD_init_gif_Dev(pls);
    dev=(png_Dev *)pls->dev;

      if (pls->xlength <= 0 || pls->ylength <=0)
      {
/* use default width, height of 800x600 if not specifed by -geometry option
 * or plspage */
	 plspage(0., 0., 800, 600, 0, 0);
      }

     pls->graphx = GRAPHICS_MODE;

     dev->pngx = pls->xlength - 1;	/* should I use -1 or not??? */
     dev->pngy = pls->ylength - 1;

#ifdef use_experimental_hidden_line_hack

     if (dev->pngx>dev->pngy)    /* Work out the scaling factor for the  */
        {                        /* "virtual" (oversized) page           */
        dev->scale=PIXELS_X/dev->pngx;
        }
     else
        {
        dev->scale=PIXELS_Y/dev->pngy;
        }
#else

     dev->scale=1;

#endif


     if (pls->xdpi<=0)
     {
/* This corresponds to a typical monitor resolution of 4 pixels/mm. */
        plspage(4.*25.4, 4.*25.4, 0, 0, 0, 0);
     }
     else
     {
        pls->ydpi=pls->xdpi;        /* Set X and Y dpi's to the same value */
     }
/* Convert DPI to pixels/mm */
     plP_setpxl(dev->scale*pls->xdpi/25.4,dev->scale*pls->ydpi/25.4);

     plP_setphy(0, dev->scale*dev->pngx, 0, dev->scale*dev->pngy);

#ifdef HAVE_FREETYPE
if (pls->dev_text)
   {
    init_freetype_lv2(pls);
   }
#endif

}

#endif


/*----------------------------------------------------------------------*\
 * plD_line_png()
 *
 * Draw a line in the current color from (x1,y1) to (x2,y2).
\*----------------------------------------------------------------------*/

void
plD_line_png(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
{
    png_Dev *dev=(png_Dev *)pls->dev;
    int xx1 = x1a/dev->scale, yy1 = y1a/dev->scale;
    int xx2 = x2a/dev->scale, yy2 = y2a/dev->scale;
    yy1 = dev->pngy - yy1;
    yy2 = dev->pngy - yy2;

    #if GD2_VERS >= 2
      if (dev->smooth==1)
        {
          gdImageSetAntiAliased(dev->im_out,dev->colour);
          gdImageLine(dev->im_out, xx1, yy1, xx2, yy2, gdAntiAliased);
        }
      else
        {
          gdImageLine(dev->im_out, xx1, yy1, xx2, yy2, dev->colour);
        }
    #else
      gdImageLine(dev->im_out, xx1, yy1, xx2, yy2, dev->colour);
    #endif

}

/*----------------------------------------------------------------------*\
 * plD_polyline_png()
 *
 * Draw a polyline in the current color.
\*----------------------------------------------------------------------*/

void
plD_polyline_png(PLStream *pls, short *xa, short *ya, PLINT npts)
{
    PLINT i;

    for (i = 0; i < npts - 1; i++)
	plD_line_png(pls, xa[i], ya[i], xa[i + 1], ya[i + 1]);
}


/*----------------------------------------------------------------------*\
 * fill_polygon()
 *
 * Fill polygon described in points pls->dev_x[] and pls->dev_y[].
\*----------------------------------------------------------------------*/

static void
fill_polygon(PLStream *pls)
{
png_Dev *dev=(png_Dev *)pls->dev;

    int i;
    gdPoint *points=NULL;

    if (pls->dev_npts < 1)
	return;

     points = malloc((size_t)pls->dev_npts * sizeof(gdPoint));

     for (i = 0; i < pls->dev_npts; i++)
         {
	   points[i].x = pls->dev_x[i]/dev->scale;
	   points[i].y = dev->pngy - (pls->dev_y[i]/dev->scale);
         }

    #if GD2_VERS >= 2
      if (dev->smooth==1)
        {
          gdImageSetAntiAliased(dev->im_out,dev->colour);
          gdImageFilledPolygon(dev->im_out, points, pls->dev_npts, gdAntiAliased);
        }
      else
        {
          gdImageFilledPolygon(dev->im_out, points, pls->dev_npts, dev->colour);
        }
    #else
      gdImageFilledPolygon(dev->im_out, points, pls->dev_npts, dev->colour);
    #endif

   free(points);

}

/*----------------------------------------------------------------------*\
 * setcmap()
 *
 * Sets up color palette.
\*----------------------------------------------------------------------*/

static void
setcmap(PLStream *pls)
{
    int i, ncol1=pls->ncol1;
    int ncol0=pls->ncol0, total_colours;
    PLColor cmap1col;
    png_Dev *dev=(png_Dev *)pls->dev;
    PLFLT tmp_colour_pos;

/*
 * Yuckky fix to get rid of the previosuly allocated palette from the
 * GD image
 */

    if (dev->im_out != NULL) {
    for (i=0;i<256;i++)
      {
       gdImageColorDeallocate(dev->im_out,i);
      }
    }

    if (ncol0>NCOLOURS/2)     /* Check for ridiculous number of colours */
       {                      /* in ncol0, and appropriately adjust the */
        plwarn("Too many colours in cmap0.");     /* number, issuing a  */
        ncol0=NCOLOURS/2;                         /* warning if it does */
        pls->ncol0=ncol0;
       }

    dev->totcol=0;       /* Reset the number of colours counter to zero */

    total_colours=ncol0+ncol1;  /* Work out how many colours are wanted */

    if (total_colours>NCOLOURS)     /* Do some rather modest error      */
       {                            /* checking to make sure that       */
        total_colours=NCOLOURS;     /* we are not defining more colours */
        ncol1=total_colours-ncol0;  /* than we have room for.           */

        if (ncol1<=0)
           {
            plexit("Problem setting colourmap in PNG or JPEG driver.");
           }
       }

 dev->ncol1=ncol1;  /* The actual size of ncol1, regardless of what was asked.
                     * This is dependent on colour slots available.
                     * It might well be the same as ncol1.
                     */

/* Initialize cmap 0 colors */

if ((ncol0>0)&&(dev->im_out != NULL))  /* make sure the program actually asked for cmap0 first */
   {
    for (i = 0; i < ncol0; i++)
        {
        gdImageColorAllocate(dev->im_out,
                             pls->cmap0[i].r, pls->cmap0[i].g, pls->cmap0[i].b);
        ++dev->totcol; /* count the number of colours we use as we use them */
        }


  }

/* Initialize any remaining slots for cmap1 */


if ((ncol1>0)&&(dev->im_out != NULL))    /* make sure that we want to define cmap1 first */
   {
    for (i = 0; i < ncol1; i++)
        {

         if (ncol1<pls->ncol1)       /* Check the dynamic range of colours */
            {

             /*
              * Ok, now if we have less colour slots available than are being
              * defined by pls->ncol1, then we still want to use the full
              * dynamic range of cmap1 as best we can, so what we do is work
              * out an approximation to the index in the full dynamic range
              * in cases when pls->ncol1 exceeds the number of free colours.
              */

             tmp_colour_pos= i>0 ? pls->ncol1*((PLFLT)i/ncol1) : 0;
             plcol_interp(pls, &cmap1col, (int) tmp_colour_pos, pls->ncol1);

            }
         else
            {
             plcol_interp(pls, &cmap1col, i, ncol1);
            }


         gdImageColorAllocate(dev->im_out,
                                   cmap1col.r, cmap1col.g, cmap1col.b);

         ++dev->totcol; /* count the number of colours we use as we go */
        }
   }
}


/*----------------------------------------------------------------------*\
 * plD_state_png()
 *
 * Handle change in PLStream state (color, pen width, fill attribute, etc).
\*----------------------------------------------------------------------*/

void
plD_state_png(PLStream *pls, PLINT op)
{
png_Dev *dev=(png_Dev *)pls->dev;
PLFLT tmp_colour_pos;
#if GD2_VERS >= 2
long temp_col;
#endif


    switch (op) {

#if GD2_VERS >= 2
    case PLSTATE_WIDTH:
        gdImageSetThickness(dev->im_out, pls->width);
	break;
#endif

    case PLSTATE_COLOR0:
#if GD2_VERS >= 2

	if ( (pls->icol0 == PL_RGB_COLOR)||     /*  Should never happen since PL_RGB_COLOR is depreciated, but here for backwards compatibility */
             (gdImageTrueColor(dev->im_out)) )  /*  We will do this if we are in "TrueColour" mode */
           {
	    if ( (dev->totcol < NCOLOURS)||         /* See if there are slots left, if so we will allocate a new colour */
                 (gdImageTrueColor(dev->im_out)) )  /* In TrueColour mode we allocate each colour as we come to it */
	       {
	        /* Next allocate a new colour to a temporary slot since what we do with it will varay depending on if its a pallter index or truecolour */
                temp_col=gdImageColorAllocate(dev->im_out,pls->curcolor.r,
                                             pls->curcolor.g, pls->curcolor.b);

                if (gdImageTrueColor(dev->im_out))
                    dev->colour = temp_col;     /* If it's truecolour, then we will directly set dev->colour to our "new" colour */
                else
                    {
                     dev->colour = dev->totcol;  /* or else, we will just set it to the last colour */
                     dev->totcol++;              /* Bump the total colours for next time round */
                    }
	       }

           }
         else  /* just a normal colour allocate, so don't worry about the above stuff, just grab the index */
           {
            dev->colour = pls->icol0;
           }

#else
	dev->colour = pls->icol0;
	if (dev->colour == PL_RGB_COLOR)
           {
	    if (dev->totcol < NCOLOURS)
	       {
                gdImageColorAllocate(dev->im_out,pls->curcolor.r, pls->curcolor.g,  pls->curcolor.b);
		dev->colour = dev->totcol;
	       }

           }
#endif
	break;

    case PLSTATE_COLOR1:

#if GD2_VERS >= 2
       if (!gdImageTrueColor(dev->im_out))
          {
#endif
           /*
            * Start by checking to see if we have to compensate for cases where
            * we don't have the full dynamic range of cmap1 at our disposal
            */
           if (dev->ncol1<pls->ncol1)
              {
               tmp_colour_pos=dev->ncol1*((PLFLT)pls->icol1/(pls->ncol1>0 ? pls->ncol1 : 1));
               dev->colour = pls->ncol0 + (int)tmp_colour_pos;
              }
           else
              dev->colour = pls->ncol0 + pls->icol1;
#if GD2_VERS >= 2
           }
        else    /* it is a truecolour image */
           {
             dev->colour = gdTrueColor(pls->curcolor.r, pls->curcolor.g, pls->curcolor.b);
           }
#endif
	break;


    case PLSTATE_CMAP0:
    case PLSTATE_CMAP1:

#if GD2_VERS >= 2
       if ((dev->im_out != NULL) && !gdImageTrueColor(dev->im_out))
          {
#endif

    /*
     *  Code to redefine the entire palette
     */


	if (pls->color)
	    setcmap(pls);

#if GD2_VERS >= 2
}
#endif

	break;
    }
}


/*----------------------------------------------------------------------*\
 * plD_esc_png()
 *
 * Escape function.
\*----------------------------------------------------------------------*/

void plD_esc_png(PLStream *pls, PLINT op, void *ptr)
{
    switch (op) {

      case PLESC_FILL:  /* fill */
	fill_polygon(pls);
	break;

#ifdef HAVE_FREETYPE
     case PLESC_HAS_TEXT:
        plD_render_freetype_text(pls, (EscText *)ptr);
        break;
#else
	(void) ptr;			/* pmr: make it used */
#endif

    }
}

/*----------------------------------------------------------------------*\
 * plD_bop_png()
 *
 * Set up for the next page.
 * Advance to next family file if necessary (file output).
\*----------------------------------------------------------------------*/

void plD_bop_png(PLStream *pls)
{
    png_Dev *dev;

    plGetFam(pls);
/* force new file if pls->family set for all subsequent calls to plGetFam
 * n.b. putting this after plGetFam call is important since plinit calls
 * bop, and you don't want the familying sequence started until after
 * that first call to bop.*/

/* n.b. pls->dev can change because of an indirect call to plD_init_png
 * from plGetFam if familying is enabled.  Thus, wait to define dev until
 * now. */

    dev = (png_Dev *) pls->dev;

    pls->famadv = 1;

    pls->page++;

if (dev->black15) plD_black15_gd(pls);
if (dev->red15) plD_red15_gd(pls);

#if GD2_VERS >= 2
  if ( ( (((dev->truecolour>0) && (dev->palette>0)) ||     /* In an EXTREMELY convaluted */
         ((dev->truecolour==0) && (dev->palette==0))) &&   /* manner, all this is just   */
          ((pls->ncol1+pls->ncol0) <= 256) ) ||             /* asking the question, do we */
       (((dev->palette>0) && (dev->truecolour==0)))  )   /* want truecolour or not ?   */
        {
#endif

           dev->im_out = gdImageCreate(pls->xlength, pls->ylength);

           setcmap(pls);

#if GD2_VERS >= 2
         }
       else
         {
         dev->im_out = gdImageCreateTrueColor(pls->xlength, pls->ylength);

/*
 * In truecolour mode, the background colour GD makes is ALWAYS black, so to
 * "simulate" (stimulate?) a background colour other than black, we will just
 * draw a dirty big rectange covering the whole image and colour it in
 * whatever colour cmap0[0] happens to be.
 *
 * Question to C gurus: while it is slightly illogical and ugly, would:
 *   if ((pls->cmap0[0].r+pls->cmap0[0].g+pls->cmap0[0].b)!=0)
 * be more computationally efficient than:
 *   if ((pls->cmap0[0].r!=0)||(pls->cmap0[0].g!=0)||(pls->cmap0[0].b!=0))
 *  ???
 */

         if ( (pls->cmap0[0].r!=0)||(pls->cmap0[0].g!=0)||
              (pls->cmap0[0].b!=0) )
            {
             gdImageFilledRectangle(dev->im_out,0,0, pls->xlength-1, pls->ylength-1,
                                    gdTrueColor(pls->cmap0[0].r,pls->cmap0[0].g,
                                                pls->cmap0[0].b));
            }

         }


/* This ensures the line width is set correctly at the beginning of
 *    each page */

   plD_state_png(pls, PLSTATE_WIDTH);

#endif


}

/*----------------------------------------------------------------------*\
 * plD_tidy_png()
 *
 * Close graphics file or otherwise clean up.
\*----------------------------------------------------------------------*/

void plD_tidy_png(PLStream *pls)
{

#ifdef HAVE_FREETYPE
   if (pls->dev_text)
     {
	FT_Data *FT=(FT_Data *)pls->FT;
	plscmap0n(FT->ncol0_org);
	plD_FreeType_Destroy(pls);
     }
#endif

   fclose(pls->OutFile);
   free_mem(pls->dev);
}

/*----------------------------------------------------------------------*\
 * plD_black15_gd()
 *
 *  This small function simply redefines index 15 of cmap0, which is
 *  usually set to white, to black, but only if index 0, which is usually
 *  black, has been redefined to white (for example, through -bg).
 *
\*----------------------------------------------------------------------*/

void plD_black15_gd(PLStream *pls)
{

if (pls->ncol0>15)
   {
    if ((pls->cmap0[0].r>227)&&(pls->cmap0[0].g>227)&&(pls->cmap0[0].b>227))
       {
        pls->cmap0[15].r=0;
        pls->cmap0[15].g=0;
        pls->cmap0[15].b=0;
      }
   }
}


/*----------------------------------------------------------------------*\
 * plD_red15_gd()
 *
 *
 *  This function swaps index 1, often the default plotting colour, with
 *  index 15, the last defined colour.
 *
 *  Colour 15 is usually white, and 1 is usually red, so swapping the two
 *  might be desirable occasionally, but it is principally here for cases
 *  when the background has been set on the command line to white, and the
 *  "def_black15" option has been issued to redefine index 15 as black. By
 *  issuing a command like
 *
 *      ... -bg ffffff -drvopt def_black15,swp_red15
 *
 *  the driver will set the background to white, then redefine index 15 of
 *  cmap0, which is usually white to black, then swap index 2 (red) to 15
 *  (white originally, now black), so at the end of the day, the "default"
 *  plotting colour is now black. Why do all of this ? It is a very quick
 *  way of making a nice web-friendly png without having to redefine the
 *  cmaps within your program.
 *
 *  If you don't like it, don't use it !
 *
\*----------------------------------------------------------------------*/

void plD_red15_gd(PLStream *pls)
{
char r=pls->cmap0[1].r;
char g=pls->cmap0[1].g;
char b=pls->cmap0[1].b;

if (pls->ncol0>15)
   {
    pls->cmap0[1].r=pls->cmap0[15].r;
    pls->cmap0[1].g=pls->cmap0[15].r;
    pls->cmap0[1].b=pls->cmap0[15].r;

    pls->cmap0[15].r=r;
    pls->cmap0[15].g=g;
    pls->cmap0[15].b=b;
   }
}


/*----------------------------------------------------------------------*\
 * plD_gd_optimise()
 *
 *
 *  This function pretty much does exactly what it says - it optimises the
 *  PNG file. It does this by checking to see if all the allocated colours
 *  were actually used. If they were not, then it deallocates them. This
 *  function often results in the PNG file being saved as a 4 bit (16
 *  colour) PNG rather than an 8 bit (256 colour) PNG. The file size
 *  difference is not huge, not as great as for GIFs for example (I think
 *  most of the saving comes from removing redundant entries from the
 *  palette entry in the header); however some modest size savings occur.
 *
 *  The function isn't always successful - the optimiser will always
 *  deallocate unused colours as it finds them, but GD will only deallocate
 *  them "for real" until 16 colours are used up, and then stop since it
 *  doesn't make a difference if you have 17 colours or 255 colours. The
 *  result of this is you may end up with an image using say, 130 colours,
 *  but you will have 240 colour entries, some of which aren't used, and
 *  aren't blanked out.
 *
 *  Another side-effect of this function is the relative position of the
 *  colour indices MAY shift as colours are deallocated. I really don't
 *  think this should worry anyone, but if it does, don't optimise the
 *  image !
 *
\*----------------------------------------------------------------------*/

void plD_gd_optimise(PLStream *pls)
{
png_Dev *dev=(png_Dev *)pls->dev;
int i,j;
char *bbuf;

bbuf=calloc(256,(size_t) 1);    /* Allocate a buffer to "check off" colours as they are used */
if (bbuf==NULL) plexit("plD_gd_optimise: Out of memory.");

    for(i=0;i<(pls->xlength-1);i++)        /* Walk through the image pixel by pixel */
       {                                   /* checking to see what colour it is */
        for(j=0;j<(pls->ylength-1);j++)    /* and adding it to the list of used colours */
           {
            bbuf[gdImagePalettePixel(dev->im_out, i, j)]=1;
           }
       }

for (i=0;i<256;i++)     /* next walk over the colours and deallocate */
    {                   /* unused ones */
    if (bbuf[i]==0) gdImageColorDeallocate(dev->im_out,i);
    }

free(bbuf);
}


#ifdef PLD_png

/*----------------------------------------------------------------------*\
 * plD_eop_png()
 *
 * End of page.
\*----------------------------------------------------------------------*/

void plD_eop_png(PLStream *pls)
{
    png_Dev *dev=(png_Dev *)pls->dev;
    int im_size=0;
    void *im_ptr=NULL;
    int iw;


    if (pls->family || pls->page == 1) {

   if (dev->optimise)
     {
#if GD2_VERS >= 2
      if ( ( (((dev->truecolour>0) && (dev->palette>0)) ||     /* In an EXTREMELY convaluted */
             ((dev->truecolour==0) && (dev->palette==0))) &&   /* manner, all this is just   */
              ((pls->ncol1+pls->ncol0)<=256) ) ||             /* asking the question, do we */
           ( ((dev->palette>0)&&(dev->truecolour==0)) )  )   /* want truecolour or not ?   */
            {
#endif
             plD_gd_optimise(pls);

#if GD2_VERS >= 2
            }
#endif
        }


       /* image is written to output file by the driver
          since if the gd.dll is linked to a different c
          lib a crash occurs - this fix works also in Linux */
       /* gdImagePng(dev->im_out, pls->OutFile); */
       #if GD2_VERS >= 2
         im_ptr = gdImagePngPtrEx (dev->im_out, &im_size, pls->dev_compression >9 ? (pls->dev_compression/10) : pls->dev_compression);
       #else
         im_ptr = gdImagePngPtr(dev->im_out, &im_size);
       #endif
       if( im_ptr ) {
         iw = fwrite(im_ptr, sizeof(char), im_size, pls->OutFile);
         gdFree(im_ptr);
       }

       gdImageDestroy(dev->im_out);
       dev->im_out = NULL;
    }
}

#endif

#ifdef HAVE_FREETYPE

/*----------------------------------------------------------------------*\
 *  void plD_pixel_gd (PLStream *pls, short x, short y)
 *
 *  callback function, of type "plD_pixel_fp", which specifies how a single
 *  pixel is set in the current colour.
\*----------------------------------------------------------------------*/

void plD_pixel_gd (PLStream *pls, short x, short y)
{
png_Dev *dev=(png_Dev *)pls->dev;

   gdImageSetPixel(dev->im_out, x, y,dev->colour);

}

/*----------------------------------------------------------------------*\
 *  void plD_set_pixel_gd (PLStream *pls, short x, short y)
 *
 *  callback function, of type "plD_pixel_fp", which specifies how a single
 *  pixel is set directly to hardware, using the colour provided
\*----------------------------------------------------------------------*/

void plD_set_pixel_gd (PLStream *pls, short x, short y, PLINT colour)
{
png_Dev *dev=(png_Dev *)pls->dev;
int R,G,B;
int Colour;

   G=GetGValue(colour);
   R=GetRValue(colour);
   B=GetBValue(colour);

   Colour=gdImageColorResolve(dev->im_out,R,G,B);
   gdImageSetPixel(dev->im_out, x, y,Colour);
}

/*----------------------------------------------------------------------*\
 *  PLINT plD_read_pixel_gd (PLStream *pls, short x, short y)
 *
 *  callback function, of type "plD_read_pixel_gd", which specifies how a
 *  single pixel's RGB is read (in the destination context), then
 *  returns an RGB encoded int with the info for blending.
\*----------------------------------------------------------------------*/

PLINT plD_read_pixel_gd (PLStream *pls, short x, short y)
{
png_Dev *dev=(png_Dev *)pls->dev;
PLINT colour;
unsigned char R,G,B;

   colour=gdImageGetTrueColorPixel(dev->im_out, x, y);

   R=gdTrueColorGetRed(colour);
   G=gdTrueColorGetGreen(colour);
   B=gdTrueColorGetBlue(colour);

   colour=RGB(R,G,B);
   return(colour);
}


/*----------------------------------------------------------------------*\
 *  void init_freetype_lv1 (PLStream *pls)
 *
 *  "level 1" initialisation of the freetype library.
 *  "Level 1" initialisation calls plD_FreeType_init(pls) which allocates
 *  memory to the pls->FT structure, then sets up the pixel callback
 *  function.
\*----------------------------------------------------------------------*/

static void init_freetype_lv1 (PLStream *pls)
{
FT_Data *FT;

plD_FreeType_init(pls);

FT=(FT_Data *)pls->FT;
FT->pixel= (plD_pixel_fp)plD_pixel_gd;
FT->read_pixel= (plD_read_pixel_fp)plD_read_pixel_gd;
FT->set_pixel= (plD_set_pixel_fp)plD_set_pixel_gd;
}

/*----------------------------------------------------------------------*\
 *  void init_freetype_lv2 (PLStream *pls)
 *
 *  "Level 2" initialisation of the freetype library.
 *  "Level 2" fills in a few setting that aren't public until after the
 *  graphics sub-syetm has been initialised.
 *  The "level 2" initialisation fills in a few things that are defined
 *  later in the initialisation process for the GD driver.
 *
 *  FT->scale is a scaling factor to convert co-ordinates. This is used by
 *  the GD and other drivers to scale back a larger virtual page and this
 *  eliminate the "hidden line removal bug". Set it to 1 if your device
 *  doesn't have scaling.
 *
 *  Some coordinate systems have zero on the bottom, others have zero on
 *  the top. Freetype does it one way, and most everything else does it the
 *  other. To make sure everything is working ok, we have to "flip" the
 *  coordinates, and to do this we need to know how big in the Y dimension
 *  the page is, and whether we have to invert the page or leave it alone.
 *
 *  FT->ymax specifies the size of the page FT->invert_y=1 tells us to
 *  invert the y-coordinates, FT->invert_y=0 will not invert the
 *  coordinates.
\*----------------------------------------------------------------------*/

static void init_freetype_lv2 (PLStream *pls)
{
png_Dev *dev=(png_Dev *)pls->dev;
FT_Data *FT=(FT_Data *)pls->FT;

FT->scale=dev->scale;
FT->ymax=dev->pngy;
FT->invert_y=1;
FT->smooth_text=0;

if ((FT->want_smooth_text==1)&&(FT->BLENDED_ANTIALIASING==0))    /* do we want to at least *try* for smoothing ? */
   {
    FT->ncol0_org=pls->ncol0;                                   /* save a copy of the original size of ncol0 */
    FT->ncol0_xtra=NCOLOURS-(pls->ncol1+pls->ncol0);            /* work out how many free slots we have */
    FT->ncol0_width=FT->ncol0_xtra/(pls->ncol0-1);              /* find out how many different shades of anti-aliasing we can do */
    if (FT->ncol0_width>4)     /* are there enough colour slots free for text smoothing ? */
       {
        if (FT->ncol0_width>max_number_of_grey_levels_used_in_text_smoothing)
           FT->ncol0_width=max_number_of_grey_levels_used_in_text_smoothing;                 /* set a maximum number of shades */
        plscmap0n(FT->ncol0_org+(FT->ncol0_width*pls->ncol0));      /* redefine the size of cmap0 */
/* the level manipulations are to turn off the plP_state(PLSTATE_CMAP0)
 * call in plscmap0 which (a) leads to segfaults since the GD image is
 * not defined at this point and (b) would be inefficient in any case since
 * setcmap is always called later (see plD_bop_png) to update the driver
 * color palette to be consistent with cmap0. */
         {
          PLINT level_save;
          level_save = pls->level;
          pls->level = 0;
          pl_set_extended_cmap0(pls, FT->ncol0_width, FT->ncol0_org); /* call the function to add the extra cmap0 entries and calculate stuff */
          pls->level = level_save;
         }
        FT->smooth_text=1;      /* Yippee ! We had success setting up the extended cmap0 */
      }
    else
      plwarn("Insufficient colour slots available in CMAP0 to do text smoothing.");
   }
else if ((FT->want_smooth_text==1)&&(FT->BLENDED_ANTIALIASING==1))    /* If we have a truecolour device, we wont even bother trying to change the palette */
   {
     FT->smooth_text=1;
   }

}

#endif


#ifdef PLD_jpeg

/*----------------------------------------------------------------------*\
 * plD_eop_jpeg()
 *
 * End of page.
\*----------------------------------------------------------------------*/

void plD_eop_jpeg(PLStream *pls)
{
    png_Dev *dev=(png_Dev *)pls->dev;
    int im_size=0;
    void *im_ptr=NULL;

    if (pls->family || pls->page == 1) {
       /* image is written to output file by the driver
          since if the gd.dll is linked to a different c
          lib a crash occurs - this fix works also in Linux */
       /* gdImageJpeg(dev->im_out, pls->OutFile, pls->dev_compression); */
       im_ptr = gdImageJpegPtr(dev->im_out, &im_size, pls->dev_compression);
       if( im_ptr ) {
         fwrite(im_ptr, sizeof(char), im_size, pls->OutFile);
         gdFree(im_ptr);
       }

       gdImageDestroy(dev->im_out);
       dev->im_out = NULL;
    }
}

#endif

#ifdef PLD_gif

/*----------------------------------------------------------------------*\
 * plD_eop_gif()
 *
 * End of page.
\*----------------------------------------------------------------------*/

void plD_eop_gif(PLStream *pls)
{
    png_Dev *dev=(png_Dev *)pls->dev;
    int im_size=0;
    void *im_ptr=NULL;
    int iw;

    if (pls->family || pls->page == 1) {
       /* image is written to output file by the driver
          since if the gd.dll is linked to a different c
          lib a crash occurs - this fix works also in Linux */
       /* gdImageGif(dev->im_out, pls->OutFile); */
       im_ptr = gdImageGifPtr(dev->im_out, &im_size);
       if( im_ptr ) {
         iw = fwrite(im_ptr, sizeof(char), im_size, pls->OutFile);
         gdFree(im_ptr);
       }

       gdImageDestroy(dev->im_out);
       dev->im_out = NULL;
    }
}

#endif


/*#endif*/


#else
int pldummy_png(void);

int
pldummy_png(void)
{
    return 0;
}

#endif				/* PNG */