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
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
|
/* Copyright (C) 2001-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
#include "stdint_.h"
#include "gdevprn.h"
#include "gxdownscale.h"
#include "gdevkrnlsclass.h" /* 'standard' built in subclasses, currently First/Last Page and obejct filter */
#include "stream.h"
#include "spprint.h"
#include "time_.h"
#include "smd5.h"
#include "sstring.h"
#include "strimpl.h"
#include "slzwx.h"
#include "szlibx.h"
#include "sdct.h"
#include "srlx.h"
#include "gsicc_cache.h"
#include "sjpeg.h"
#include "gdevpdfimg.h"
enum {
COMPRESSION_NONE = 1, /* dump mode */
COMPRESSION_LZW = 2, /* Lempel-Ziv & Welch */
COMPRESSION_FLATE = 3,
COMPRESSION_JPEG = 4,
COMPRESSION_RLE = 5
};
static struct compression_string {
unsigned char id;
const char *str;
} compression_strings [] = {
{ COMPRESSION_NONE, "None" },
{ COMPRESSION_LZW, "LZW" }, /* Not supported in PCLm */
{ COMPRESSION_FLATE, "Flate" },
{ COMPRESSION_JPEG, "JPEG" },
{ COMPRESSION_RLE, "RLE" },
{ 0, NULL }
};
int PCLm_open(gx_device *pdev);
int PCLm_close(gx_device * pdev);
/* ------ The pdfimage8 device ------ */
static void
pdfimage8_initialize_device_procs(gx_device *dev)
{
gdev_prn_initialize_device_procs_gray(dev);
set_dev_proc(dev, open_device, pdf_image_open);
set_dev_proc(dev, output_page, gdev_prn_output_page);
set_dev_proc(dev, close_device, pdf_image_close);
set_dev_proc(dev, get_params, pdf_image_get_params_downscale);
set_dev_proc(dev, put_params, pdf_image_put_params_downscale);
set_dev_proc(dev, encode_color, gx_default_8bit_map_gray_color);
set_dev_proc(dev, decode_color, gx_default_8bit_map_color_gray);
}
const gx_device_pdf_image gs_pdfimage8_device = {
prn_device_body(gx_device_pdf_image,
pdfimage8_initialize_device_procs,
"pdfimage8",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
600, 600, /* 600 dpi by default */
0, 0, 0, 0, /* Margins */
1, /* num components */
8, /* bits per sample */
255, 0, 256, 0,
pdf_image_print_page),
3,
GX_DOWNSCALER_PARAMS_DEFAULTS,
0, /* StripHeight */
0.0, /* QFactor */
0 /* JPEGQ */
};
/* ------ The pdfimage24 device ------ */
static void
pdfimage24_initialize_device_procs(gx_device *dev)
{
gdev_prn_initialize_device_procs_rgb(dev);
set_dev_proc(dev, open_device, pdf_image_open);
set_dev_proc(dev, output_page, gdev_prn_output_page);
set_dev_proc(dev, close_device, pdf_image_close);
set_dev_proc(dev, get_params, pdf_image_get_params_downscale);
set_dev_proc(dev, put_params, pdf_image_put_params_downscale);
}
const gx_device_pdf_image gs_pdfimage24_device = {
prn_device_body(gx_device_pdf_image,
pdfimage24_initialize_device_procs,
"pdfimage24",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
600, 600, /* 600 dpi by default */
0, 0, 0, 0, /* Margins */
3, /* num components */
24, /* bits per sample */
255, 255, 256, 256,
pdf_image_print_page),
3,
GX_DOWNSCALER_PARAMS_DEFAULTS,
0, /* StripHeight */
0.0, /* QFactor */
0 /* JPEGQ */
};
/* ------ The pdfimage32 device ------ */
static void
pdfimage32_initialize_device_procs(gx_device *dev)
{
gdev_prn_initialize_device_procs_cmyk8(dev);
set_dev_proc(dev, open_device, pdf_image_open);
set_dev_proc(dev, output_page, gdev_prn_output_page);
set_dev_proc(dev, close_device, pdf_image_close);
set_dev_proc(dev, get_params, pdf_image_get_params_downscale_cmyk);
set_dev_proc(dev, put_params, pdf_image_put_params_downscale_cmyk);
}
const gx_device_pdf_image gs_pdfimage32_device = {
prn_device_body(gx_device_pdf_image,
pdfimage32_initialize_device_procs,
"pdfimage32",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
600, 600, /* 600 dpi by default */
0, 0, 0, 0, /* Margins */
4, /* num components */
32, /* bits per sample */
255, 255, 256, 256,
pdf_image_print_page),
3,
GX_DOWNSCALER_PARAMS_DEFAULTS,
0, /* StripHeight */
0.0, /* QFactor */
0 /* JPEGQ */
};
static int
pdfimage_write_args_comment(gx_device_pdf_image *pdev, stream *s)
{
const char * const *argv = NULL;
const char *arg;
int towrite, length, i, j, argc;
argc = gs_lib_ctx_get_args(pdev->memory->gs_lib_ctx, &argv);
stream_write(s, (byte *)"%%Invocation:", 13);
length = 12;
for (i=0;i < argc; i++) {
arg = argv[i];
if ((strlen(arg) + length) > 255) {
stream_write(s, (byte *)"\n%%+ ", 5);
length = 5;
} else {
stream_write(s, (byte *)" ", 1);
length++;
}
if (strlen(arg) > 250)
towrite = 250;
else
towrite = strlen(arg);
length += towrite;
for (j=0;j < towrite;j++) {
if (arg[j] == 0x0A) {
stream_write(s, (byte *)"<0A>", 4);
} else {
if (arg[j] == 0x0D) {
stream_write(s, (byte *)"<0D>", 4);
} else {
stream_write(s, (byte *)&arg[j], 1);
}
}
}
}
stream_write(s, (byte *)"\n", 1);
return 0;
}
static int gdev_pdf_image_begin_page(gx_device_pdf_image *pdf_dev,
gp_file *file)
{
gx_device_printer *const pdev = (gx_device_printer *)pdf_dev;
cmm_dev_profile_t *profile_struct;
gsicc_rendering_param_t rendering_params;
int code;
pdfimage_page *page;
page = (pdfimage_page *)gs_alloc_bytes(pdf_dev->memory->non_gc_memory, sizeof(pdfimage_page), "pdfimage create new page");
if (page == NULL)
return_error(gs_error_VMerror);
memset(page, 0x00, sizeof(pdfimage_page));
if (gdev_prn_file_is_new(pdev)) {
/* Set up the icc link settings at this time */
code = dev_proc(pdev, get_profile)((gx_device *)pdev, &profile_struct);
if (code < 0)
return_error(gs_error_undefined);
if (profile_struct->postren_profile != NULL) {
rendering_params.black_point_comp = gsBLACKPTCOMP_ON;
rendering_params.graphics_type_tag = GS_UNKNOWN_TAG;
rendering_params.override_icc = false;
rendering_params.preserve_black = gsBLACKPRESERVE_OFF;
rendering_params.rendering_intent = gsRELATIVECOLORIMETRIC;
rendering_params.cmm = gsCMM_DEFAULT;
if (profile_struct->oi_profile != NULL) {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->oi_profile, profile_struct->postren_profile,
&rendering_params);
} else if (profile_struct->link_profile != NULL) {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->link_profile, profile_struct->postren_profile,
&rendering_params);
} else {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE], profile_struct->postren_profile,
&rendering_params);
}
if (pdf_dev->icclink == NULL) {
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
/* If it is identity, release it now and set link to NULL */
if (pdf_dev->icclink->is_identity) {
pdf_dev->icclink->procs.free_link(pdf_dev->icclink);
gsicc_free_link_dev(pdev->memory, pdf_dev->icclink);
pdf_dev->icclink = NULL;
}
}
/* Set up the stream and insert the file header */
pdf_dev->strm = s_alloc(pdf_dev->memory->non_gc_memory, "pdfimage_open_temp_stream(strm)");
if (pdf_dev->strm == 0) {
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
pdf_dev->strm_buf = gs_alloc_bytes(pdf_dev->memory->non_gc_memory, pdf_dev->width * (pdf_dev->color_info.depth / 8),
"pdfimage_open_temp_stream(strm_buf)");
if (pdf_dev->strm_buf == NULL) {
pdf_dev->strm->file = NULL; /* Don't close underlying file when we free the stream */
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm,
"pdfimage_open_temp_stream(strm)");
pdf_dev->strm = NULL;
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
swrite_file(pdf_dev->strm, pdf_dev->file, pdf_dev->strm_buf, pdf_dev->width * (pdf_dev->color_info.depth / 8));
stream_puts(pdf_dev->strm, "%PDF-1.3\n");
stream_puts(pdf_dev->strm, "%\307\354\217\242\n");
pdfimage_write_args_comment(pdf_dev,pdf_dev->strm);
if (pdf_dev->ocr.file_init) {
code = pdf_dev->ocr.file_init(pdf_dev);
if (code < 0) {
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm_buf, "pdfimage_open_temp_stream(strm_buf)");
pdf_dev->strm->file = NULL; /* Don't close underlying file when we free the stream */
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm,
"pdfimage_open_temp_stream(strm)");
pdf_dev->strm = NULL;
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return code;
}
}
pdf_dev->Pages = page;
} else {
pdfimage_page *current = pdf_dev->Pages;
while(current->next)
current = current->next;
current->next = page;
}
page->ImageObjectNumber = (pdf_dev->NumPages * PDFIMG_OBJS_PER_PAGE) + PDFIMG_STATIC_OBJS + pdf_dev->ocr.file_objects;
page->LengthObjectNumber = page->ImageObjectNumber + 1;
page->PageStreamObjectNumber = page->LengthObjectNumber + 1;
page->PageDictObjectNumber = page->PageStreamObjectNumber + 1;
page->PageLengthObjectNumber = page->PageDictObjectNumber + 1;
page->ImageOffset = stell(pdf_dev->strm);
pdf_dev->StripHeight = pdf_dev->height;
return 0;
}
static int pdf_image_chunky_post_cm(void *arg, byte **dst, byte **src, int w, int h,
int raster)
{
gsicc_bufferdesc_t input_buffer_desc, output_buffer_desc;
gsicc_link_t *icclink = (gsicc_link_t*)arg;
gsicc_init_buffer(&input_buffer_desc, icclink->num_input, 1, false,
false, false, 0, raster, h, w);
gsicc_init_buffer(&output_buffer_desc, icclink->num_output, 1, false,
false, false, 0, raster, h, w);
icclink->procs.map_buffer(NULL, icclink, &input_buffer_desc, &output_buffer_desc,
src[0], dst[0]);
return 0;
}
static int
encode(gx_device *pdev, stream **s, const stream_template *t, gs_memory_t *mem)
{
gx_device_pdf_image *pdf_dev = (gx_device_pdf_image *)pdev;
stream_state *st;
if (t == &s_DCTE_template) {
int code;
stream_DCT_state *sDCT;
jpeg_compress_data *jcdp;
st = s_alloc_state(mem, t->stype, "pdfimage.encode");
if (st == 0)
return_error(gs_error_VMerror);
sDCT = (stream_DCT_state *)st;
st->templat = t;
if (t->set_defaults)
t->set_defaults(st);
jcdp = gs_alloc_struct_immovable(mem, jpeg_compress_data,
&st_jpeg_compress_data, "zDCTE");
if (jcdp == 0) {
gs_free_object(mem, st, "pdfimage.encode");
return_error(gs_error_VMerror);
}
sDCT->data.compress = jcdp;
sDCT->icc_profile = NULL;
jcdp->memory = sDCT->jpeg_memory = mem; /* set now for allocation */
if ((code = gs_jpeg_create_compress(sDCT)) < 0) {
gs_jpeg_destroy(sDCT);
gs_free_object(mem, jcdp, "setup_image_compression");
sDCT->data.compress = NULL; /* Avoid problems with double frees later */
return code;
}
jcdp->Picky = 0;
jcdp->Relax = 0;
jcdp->cinfo.image_width = gx_downscaler_scale(pdf_dev->width, pdf_dev->downscale.downscale_factor);
jcdp->cinfo.image_height = pdf_dev->StripHeight;
switch (pdf_dev->color_info.depth) {
case 32:
jcdp->cinfo.input_components = 4;
jcdp->cinfo.in_color_space = JCS_CMYK;
break;
case 24:
jcdp->cinfo.input_components = 3;
jcdp->cinfo.in_color_space = JCS_RGB;
break;
case 8:
jcdp->cinfo.input_components = 1;
jcdp->cinfo.in_color_space = JCS_GRAYSCALE;
break;
}
if ((code = gs_jpeg_set_defaults(sDCT)) < 0) {
gs_jpeg_destroy(sDCT);
gs_free_object(mem, jcdp, "setup_image_compression");
sDCT->data.compress = NULL; /* Avoid problems with double frees later */
return code;
}
if (pdf_dev->JPEGQ > 0) {
code = gs_jpeg_set_quality(sDCT, pdf_dev->JPEGQ, TRUE);
if (code < 0) {
gs_jpeg_destroy(sDCT);
gs_free_object(mem, jcdp, "setup_image_compression");
sDCT->data.compress = NULL; /* Avoid problems with double frees later */
return code;
}
} else if (pdf_dev->QFactor > 0.0) {
code = gs_jpeg_set_linear_quality(sDCT,
(int)(min(pdf_dev->QFactor, 100.0)
* 100.0 + 0.5),
TRUE);
if (code < 0) {
gs_jpeg_destroy(sDCT);
gs_free_object(mem, jcdp, "setup_image_compression");
sDCT->data.compress = NULL; /* Avoid problems with double frees later */
return code;
}
}
jcdp->cinfo.write_JFIF_header = FALSE;
jcdp->cinfo.write_Adobe_marker = FALSE; /* must do it myself */
jcdp->templat = s_DCTE_template;
/* Make sure we get at least a full scan line of input. */
sDCT->scan_line_size = jcdp->cinfo.input_components *
jcdp->cinfo.image_width;
jcdp->templat.min_in_size =
max(s_DCTE_template.min_in_size, sDCT->scan_line_size);
/* Make sure we can write the user markers in a single go. */
jcdp->templat.min_out_size =
max(s_DCTE_template.min_out_size, sDCT->Markers.size);
if (s_add_filter(s, &jcdp->templat, st, mem) == 0) {
gs_jpeg_destroy(sDCT);
gs_free_object(mem, jcdp, "setup_image_compression");
sDCT->data.compress = NULL; /* Avoid problems with double frees later */
return_error(gs_error_VMerror);
}
} else {
st = s_alloc_state(mem, t->stype, "pdfimage.encode");
if (st == 0)
return_error(gs_error_VMerror);
if (t->set_defaults)
t->set_defaults(st);
if (s_add_filter(s, t, st, mem) == 0) {
gs_free_object(mem, st, "pdfimage.encode");
return_error(gs_error_VMerror);
}
}
return 0;
}
static int
pdf_image_downscale_and_print_page(gx_device_printer *dev,
gx_downscaler_params *params,
int bpc, int num_comps)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)dev;
int code = 0;
byte *data = NULL;
int size = gdev_mem_bytes_per_scan_line((gx_device *)dev);
int max_size = size;
int row;
int factor = params->downscale_factor;
int height = gx_downscaler_scale(dev->height, factor);
int width = gx_downscaler_scale(dev->width, factor);
gx_downscaler_t ds;
gs_offset_t stream_pos = 0;
pdfimage_page *page = pdf_dev->Pages;
char Buffer[1024];
stream *s = pdf_dev->strm;
gs_offset_t len;
if (page == NULL)
return_error(gs_error_undefined);
while(page->next)
page = page->next;
if (num_comps != 4)
params->trap_w = params->trap_h = 0;
if (pdf_dev->icclink == NULL) {
code = gx_downscaler_init(&ds, (gx_device *)dev,
8, bpc, num_comps,
params,
NULL, /*&fax_adjusted_width*/
0 /*aw*/);
} else {
code = gx_downscaler_init_cm(&ds, (gx_device *)dev,
8, bpc, num_comps,
params,
NULL, /*&fax_adjusted_width*/
0, /*aw*/
pdf_image_chunky_post_cm,
pdf_dev->icclink,
pdf_dev->icclink->num_output);
}
if (code < 0)
return code;
data = gs_alloc_bytes(dev->memory, max_size, "pdf_image_print_page(data)");
if (data == NULL) {
gx_downscaler_fin(&ds);
return_error(gs_error_VMerror);
}
if (pdf_dev->ocr.begin_page) {
code = pdf_dev->ocr.begin_page(pdf_dev, width, height, num_comps*8);
if (code < 0)
{
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return code;
}
}
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->ImageObjectNumber);
pprintd1(pdf_dev->strm, "<<\n/Length %d 0 R\n", page->LengthObjectNumber);
stream_puts(pdf_dev->strm, "/Subtype /Image\n");
pprintd1(pdf_dev->strm, "/Width %d\n", width);
pprintd1(pdf_dev->strm, "/Height %d\n", height);
switch(num_comps) {
case 1:
stream_puts(pdf_dev->strm, "/ColorSpace /DeviceGray\n");
stream_puts(pdf_dev->strm, "/BitsPerComponent 8\n");
break;
case 3:
stream_puts(pdf_dev->strm, "/ColorSpace /DeviceRGB\n");
stream_puts(pdf_dev->strm, "/BitsPerComponent 8\n");
break;
case 4:
stream_puts(pdf_dev->strm, "/ColorSpace /DeviceCMYK\n");
stream_puts(pdf_dev->strm, "/BitsPerComponent 8\n");
break;
}
switch (pdf_dev->Compression) {
case COMPRESSION_LZW:
stream_puts(pdf_dev->strm, "/Filter /LZWDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
stream_pos = stell(pdf_dev->strm);
encode((gx_device *)pdf_dev, &pdf_dev->strm, &s_LZWE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_FLATE:
stream_puts(pdf_dev->strm, "/Filter /FlateDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
stream_pos = stell(pdf_dev->strm);
encode((gx_device *)pdf_dev, &pdf_dev->strm, &s_zlibE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_JPEG:
stream_puts(pdf_dev->strm, "/Filter /DCTDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
stream_pos = stell(pdf_dev->strm);
encode((gx_device *)pdf_dev, &pdf_dev->strm, &s_DCTE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_RLE:
stream_puts(pdf_dev->strm, "/Filter /RunLengthDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
stream_pos = stell(pdf_dev->strm);
encode((gx_device *)pdf_dev, &pdf_dev->strm, &s_RLE_template, pdf_dev->memory->non_gc_memory);
break;
default:
case COMPRESSION_NONE:
stream_puts(pdf_dev->strm, ">>\nstream\n");
stream_pos = stell(pdf_dev->strm);
break;
}
for (row = 0; row < height && code >= 0; row++) {
code = gx_downscaler_getbits(&ds, data, row);
if (code < 0)
break;
if (pdf_dev->ocr.line)
pdf_dev->ocr.line(pdf_dev, data);
stream_write(pdf_dev->strm, data, width * num_comps);
}
if (code < 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return code;
}
switch(pdf_dev->Compression) {
case COMPRESSION_LZW:
case COMPRESSION_FLATE:
case COMPRESSION_JPEG:
case COMPRESSION_RLE:
s_close_filters(&pdf_dev->strm, s);
break;
default:
case COMPRESSION_NONE:
break;
}
stream_puts(pdf_dev->strm, "\nendstream\nendobj\n");
page->LengthOffset = stell(pdf_dev->strm);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->LengthObjectNumber);
pprintd1(pdf_dev->strm, "%d\n", page->LengthOffset - stream_pos - 18); /* 18 is the length of \nendstream\nendobj\n we need to take that off for the stream length */
stream_puts(pdf_dev->strm, "endobj\n");
page->PageStreamOffset = stell(pdf_dev->strm);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->PageStreamObjectNumber);
pprintd1(pdf_dev->strm, "<<\n/Filter/FlateDecode/Length %d 0 R\n>>\nstream\n", page->PageLengthObjectNumber);
stream_pos = stell(pdf_dev->strm);
encode((gx_device *)pdf_dev, &pdf_dev->strm, &s_zlibE_template, pdf_dev->memory->non_gc_memory);
if (pdf_dev->ocr.end_page)
stream_puts(pdf_dev->strm, "q\n");
pprintd2(pdf_dev->strm, "%d 0 0 %d 0 0 cm\n/Im1 Do",
(int)((width / (pdf_dev->HWResolution[0] / 72)) * factor),
(int)((height / (pdf_dev->HWResolution[1] / 72)) * factor));
if (pdf_dev->ocr.end_page) {
stream_puts(pdf_dev->strm, "\nQ");
pdf_dev->ocr.end_page(pdf_dev);
}
s_close_filters(&pdf_dev->strm, s);
len = stell(pdf_dev->strm) - stream_pos;
stream_puts(pdf_dev->strm, "\nendstream\nendobj\n");
page->PageLengthOffset = stell(pdf_dev->strm);
pprintd2(pdf_dev->strm, "%d 0 obj\n%d\nendobj\n", page->PageLengthObjectNumber, len);
page->PageDictOffset = stell(pdf_dev->strm);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->PageDictObjectNumber);
pprintd1(pdf_dev->strm, "<<\n/Contents %d 0 R\n", page->PageStreamObjectNumber);
stream_puts(pdf_dev->strm, "/Type /Page\n/Parent 2 0 R\n");
gs_sprintf(Buffer, "/MediaBox [0 0 %f %f]\n", ((double)pdf_dev->width / pdf_dev->HWResolution[0]) * 72, ((double)pdf_dev->height / pdf_dev->HWResolution[1]) * 72);
stream_puts(pdf_dev->strm, Buffer);
pprintd1(pdf_dev->strm, "/Resources <<\n/XObject <<\n/Im1 %d 0 R\n>>\n", page->ImageObjectNumber);
if (pdf_dev->ocr.file_init)
pprintd1(pdf_dev->strm, "/Font <<\n/Ft0 %d 0 R\n>>\n", PDFIMG_STATIC_OBJS);
stream_puts(pdf_dev->strm, ">>\n>>\nendobj\n");
gx_downscaler_fin(&ds);
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
pdf_dev->NumPages++;
return code;
}
static void write_fileID(stream *s, const byte *str, int size)
{
const stream_template *templat;
stream_AXE_state state;
stream_state *st = NULL;
templat = &s_AXE_template;
st = (stream_state *) & state;
s_AXE_init_inline(&state);
stream_putc(s, '<');
{
byte buf[100]; /* size is arbitrary */
stream_cursor_read r;
stream_cursor_write w;
int status;
stream_cursor_read_init(&r, str, size);
do {
stream_cursor_write_init(&w, buf, sizeof(buf));
status = (*templat->process) (st, &r, &w, true);
stream_write(s, buf, (uint) (w.ptr + 1 - buf));
}
while (status == 1);
}
}
static int
pdf_compute_fileID(gx_device_pdf_image * pdev, byte fileID[16], char *CreationDate, char *Title, char *Producer)
{
/* We compute a file identifier when beginning a document
to allow its usage with PDF encryption. Due to that,
in contradiction to the Adobe recommendation, our
ID doesn't depend on the document size.
*/
gs_memory_t *mem = pdev->memory->non_gc_memory;
uint ignore;
stream *s = s_MD5E_make_stream(mem, fileID, 16);
long secs_ns[2];
if (s == NULL)
return_error(gs_error_VMerror);
#ifdef CLUSTER
secs_ns[0] = 0;
secs_ns[1] = 0;
#else
gp_get_realtime(secs_ns);
#endif
sputs(s, (byte *)secs_ns, sizeof(secs_ns), &ignore);
#ifdef CLUSTER
/* Don't have the ID's vary by filename output in the cluster testing.
* This prevents us comparing gs to gpdl results, and makes it harder
* to manually reproduce results. */
sputs(s, (const byte *)"ClusterTest.pdf", strlen("ClusterTest.pdf"), &ignore);
#else
sputs(s, (const byte *)pdev->fname, strlen(pdev->fname), &ignore);
#endif
stream_puts(s, "/ModDate ");
stream_puts(s, CreationDate);
stream_puts(s, "\n/CreationDate ");
stream_puts(s, CreationDate);
stream_puts(s, "\n/Title (");
stream_puts(s, Title);
stream_puts(s, ")\n/Producer (");
stream_puts(s, Producer);
stream_puts(s, ")\n");
sclose(s);
gs_free_object(mem, s, "pdf_compute_fileID");
return 0;
}
static void write_xref_entry (stream *s, gs_offset_t Offset)
{
char O[11];
int i;
if (Offset > 9999999999){
Offset = 0;
}
gs_sprintf(O, "%d", Offset);
for (i=0; i< (10 - strlen(O)); i++)
stream_puts(s, "0");
stream_puts(s, O);
stream_puts(s, " 00000 n \n");
}
static void
pdf_store_default_Producer(char *buf)
{
int major = (int)(gs_revision / 1000);
int minor = (int)(gs_revision - (major * 1000)) / 10;
int patch = gs_revision % 10;
gs_sprintf(buf, "(%s %d.%02d.%d)", gs_product, major, minor, patch);
}
static int pdf_image_finish_file(gx_device_pdf_image *pdf_dev, int PCLm)
{
pdfimage_page *page = pdf_dev->Pages;
struct tm tms;
time_t t;
int timeoffset;
char timesign;
char CreationDate[26], Title[] = "Untitled", Producer[256];
if (pdf_dev->strm != NULL) {
byte fileID[16];
pdf_store_default_Producer(Producer);
pdf_dev->RootOffset = stell(pdf_dev->strm);
stream_puts(pdf_dev->strm, "1 0 obj\n<<\n/Pages 2 0 R\n/Type /Catalog\n/Info 3 0 R\n>>\nendobj\n");
pdf_dev->PagesOffset = stell(pdf_dev->strm);
pprintd1(pdf_dev->strm, "2 0 obj\n<<\n/Count %d\n", pdf_dev->NumPages);
stream_puts(pdf_dev->strm, "/Kids [");
while(page){
pprintd1(pdf_dev->strm, "%d 0 R ", page->PageDictObjectNumber);
page = page->next;
}
stream_puts(pdf_dev->strm, "]\n/Type /Pages\n>>\nendobj\n");
#ifdef CLUSTER
memset(&t, 0, sizeof(t));
memset(&tms, 0, sizeof(tms));
timesign = 'Z';
timeoffset = 0;
#else
time(&t);
tms = *gmtime(&t);
tms.tm_isdst = -1;
timeoffset = (int)difftime(t, mktime(&tms)); /* tz+dst in seconds */
timesign = (timeoffset == 0 ? 'Z' : timeoffset < 0 ? '-' : '+');
timeoffset = any_abs(timeoffset) / 60;
tms = *localtime(&t);
#endif
gs_sprintf(CreationDate, "(D:%04d%02d%02d%02d%02d%02d%c%02d\'%02d\')",
tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
tms.tm_hour, tms.tm_min, tms.tm_sec,
timesign, timeoffset / 60, timeoffset % 60);
pdf_dev->InfoOffset = stell(pdf_dev->strm);
stream_puts(pdf_dev->strm, "3 0 obj\n<<\n/Producer");
stream_puts(pdf_dev->strm, Producer);
stream_puts(pdf_dev->strm, "\n/CreationDate");
stream_puts(pdf_dev->strm, CreationDate);
stream_puts(pdf_dev->strm, "\n>>\nendobj\n");
pdf_dev->xrefOffset = stell(pdf_dev->strm);
if (PCLm)
pprintd1(pdf_dev->strm, "xref\n0 %d\n0000000000 65536 f \n", pdf_dev->NextObject);
else
pprintd1(pdf_dev->strm, "xref\n0 %d\n0000000000 65536 f \n", (pdf_dev->NumPages * PDFIMG_OBJS_PER_PAGE) + PDFIMG_STATIC_OBJS + pdf_dev->ocr.file_objects);
write_xref_entry(pdf_dev->strm, pdf_dev->RootOffset);
write_xref_entry(pdf_dev->strm, pdf_dev->PagesOffset);
write_xref_entry(pdf_dev->strm, pdf_dev->InfoOffset);
if (pdf_dev->ocr.file_objects) {
int i;
for (i = 0; i < OCR_MAX_FILE_OBJECTS; i++)
if (pdf_dev->ocr.file_object_offset[i])
write_xref_entry(pdf_dev->strm, pdf_dev->ocr.file_object_offset[i]);
}
if (!PCLm) {
page = pdf_dev->Pages;
while(page){
write_xref_entry(pdf_dev->strm, page->ImageOffset);
write_xref_entry(pdf_dev->strm, page->LengthOffset);
write_xref_entry(pdf_dev->strm, page->PageStreamOffset);
write_xref_entry(pdf_dev->strm, page->PageDictOffset);
write_xref_entry(pdf_dev->strm, page->PageLengthOffset);
page = page->next;
}
pprintd1(pdf_dev->strm, "trailer\n<<\n/Size %d\n/Root 1 0 R\n/ID [", (pdf_dev->NumPages * PDFIMG_OBJS_PER_PAGE) + PDFIMG_STATIC_OBJS + pdf_dev->ocr.file_objects);
pdf_compute_fileID(pdf_dev, fileID, CreationDate, Title, Producer);
write_fileID(pdf_dev->strm, (const byte *)&fileID, 16);
write_fileID(pdf_dev->strm, (const byte *)&fileID, 16);
pprintd1(pdf_dev->strm, "]\n>>\nstartxref\n%d\n%%%%EOF\n", pdf_dev->xrefOffset);
} else {
gs_offset_t streamsize, R = 0;
char Buffer[1024];
sflush(pdf_dev->xref_stream.strm);
streamsize = gp_ftell(pdf_dev->xref_stream.file);
if (gp_fseek(pdf_dev->xref_stream.file, 0, SEEK_SET) != 0)
return_error(gs_error_ioerror);
while(streamsize > 0) {
if (streamsize > 1024) {
streamsize -= gp_fpread(Buffer, 1024, R, pdf_dev->xref_stream.file);
R += 1024;
stream_write(pdf_dev->strm, Buffer, 1024);
}
else {
gp_fpread(Buffer, streamsize, R, pdf_dev->xref_stream.file);
stream_write(pdf_dev->strm, Buffer, streamsize);
streamsize = 0;
}
}
if (gp_fseek(pdf_dev->xref_stream.file, 0, SEEK_SET) != 0)
return_error(gs_error_ioerror);
pprintd1(pdf_dev->strm, "trailer\n<<\n/Size %d\n/Root 1 0 R\n/ID [", pdf_dev->NextObject);
pdf_compute_fileID(pdf_dev, fileID, CreationDate, Title, Producer);
write_fileID(pdf_dev->strm, (const byte *)&fileID, 16);
write_fileID(pdf_dev->strm, (const byte *)&fileID, 16);
pprintd1(pdf_dev->strm, "]\n>>\nstartxref\n%d\n%%%%EOF\n", pdf_dev->xrefOffset);
}
sflush(pdf_dev->strm);
pdf_dev->strm->file = NULL; /* Don't close underlying file when we free the stream */
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm, "pdfimage_close(strm)");
pdf_dev->strm = 0;
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm_buf, "pdfimage_close(strmbuf)");
pdf_dev->strm_buf = 0;
}
if (pdf_dev->Pages) {
pdfimage_page *p = pdf_dev->Pages, *n;
do {
n = p->next;
gs_free_object(pdf_dev->memory->non_gc_memory, p,
"pdfimage free a page");
p = n;
}while (p);
pdf_dev->Pages = NULL;
pdf_dev->NumPages = 0;
}
if (pdf_dev->icclink != NULL)
{
pdf_dev->icclink->procs.free_link(pdf_dev->icclink);
gsicc_free_link_dev(pdf_dev->memory, pdf_dev->icclink);
pdf_dev->icclink = NULL;
}
pdf_dev->RootOffset = 0;
pdf_dev->PagesOffset = 0;
pdf_dev->xrefOffset = 0;
if (!PCLm)
pdf_dev->StripHeight = 0;
else
pdf_dev->NextObject = 0;
return 0;
}
int
pdf_image_print_page(gx_device_printer * pdev, gp_file * file)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)pdev;
const char *fmt;
gs_parsed_file_name_t parsed;
int code;
code = gdev_pdf_image_begin_page(pdf_dev, file);
if (code < 0)
return code;
code = pdf_image_downscale_and_print_page(pdev,
&pdf_dev->downscale,
8,
pdf_dev->color_info.num_components);
if (code < 0)
return code;
code = gx_parse_output_file_name(&parsed, &fmt, pdev->fname,
strlen(pdev->fname), pdev->memory);
if (code >= 0 && fmt) {
code = pdf_image_finish_file(pdf_dev, 0);
}
return code;
}
int
pdf_image_open(gx_device *pdev)
{
gx_device_pdf_image *ppdev;
int code;
bool update_procs = false;
code = install_internal_subclass_devices((gx_device **)&pdev, &update_procs);
if (code < 0)
return code;
/* If we've been subclassed, find the terminal device */
while(pdev->child)
pdev = pdev->child;
ppdev = (gx_device_pdf_image *)pdev;
ppdev->file = NULL;
ppdev->Pages = NULL;
ppdev->NumPages = 0;
ppdev->RootOffset = 0;
ppdev->PagesOffset = 0;
ppdev->xrefOffset = 0;
ppdev->StripHeight = 0;
code = gdev_prn_allocate_memory(pdev, NULL, 0, 0);
if (code < 0)
return code;
if (update_procs) {
if (pdev->ObjectHandlerPushed) {
gx_copy_device_procs(pdev->parent, pdev, &gs_obj_filter_device);
pdev = pdev->parent;
}
if (pdev->PageHandlerPushed)
gx_copy_device_procs(pdev->parent, pdev, &gs_flp_device);
}
if (ppdev->OpenOutputFile)
code = gdev_prn_open_printer(pdev, 1);
return code;
}
int
pdf_image_close(gx_device * pdev)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)pdev;
int code;
code = pdf_image_finish_file(pdf_dev, 0);
if (code < 0)
return code;
return gdev_prn_close(pdev);
}
static int
pdf_image_compression_param_string(gs_param_string *param, unsigned char id)
{
struct compression_string *c;
for (c = compression_strings; c->str; c++)
if (id == c->id) {
param_string_from_string(*param, c->str);
return 0;
}
return_error(gs_error_undefined);
}
static int
pdf_image_compression_id(unsigned char *id, gs_param_string *param)
{
struct compression_string *c;
for (c = compression_strings; c->str; c++)
if (!bytes_compare(param->data, param->size,
(const byte *)c->str, strlen(c->str)))
{
*id = c->id;
return 0;
}
return_error(gs_error_undefined);
}
static int
pdf_image_get_some_params(gx_device * dev, gs_param_list * plist, int which)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)dev;
int code = gdev_prn_get_params(dev, plist);
int ecode = code;
gs_param_string comprstr;
if (ecode < 0)
return ecode;
ecode = param_write_bool(plist, "Tumble", &pdf_dev->Tumble);
if (ecode < 0)
return ecode;
ecode = param_write_bool(plist, "Tumble2", &pdf_dev->Tumble2);
if (ecode < 0)
return ecode;
ecode = param_write_int(plist, "StripHeight", &pdf_dev->StripHeight);
if (ecode < 0)
return ecode;
ecode = param_write_int(plist, "JPEGQ", &pdf_dev->JPEGQ);
if (ecode < 0)
return ecode;
ecode = param_write_float(plist, "QFactor", &pdf_dev->QFactor);
if (ecode < 0)
return ecode;
if ((code = pdf_image_compression_param_string(&comprstr, pdf_dev->Compression)) < 0 ||
(code = param_write_string(plist, "Compression", &comprstr)) < 0)
ecode = code;
if (which & 1) {
if ((code = gx_downscaler_write_params(plist, &pdf_dev->downscale,
GX_DOWNSCALER_PARAMS_MFS |
(which & 2 ? GX_DOWNSCALER_PARAMS_TRAP : 0) |
(which & 4 ? GX_DOWNSCALER_PARAMS_ETS : 0))) < 0)
ecode = code;
}
return ecode;
}
int
pdf_image_get_params_downscale(gx_device * dev, gs_param_list * plist)
{
return pdf_image_get_some_params(dev, plist, 1);
}
int
pdf_image_get_params_downscale_cmyk(gx_device * dev, gs_param_list * plist)
{
return pdf_image_get_some_params(dev, plist, 3);
}
int
pdf_image_get_params_downscale_cmyk_ets(gx_device * dev, gs_param_list * plist)
{
return pdf_image_get_some_params(dev, plist, 7);
}
static int
pdf_image_put_some_params(gx_device * dev, gs_param_list * plist, int which)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)dev;
int ecode = 0;
int code;
unsigned char compr = pdf_dev->Compression;
gs_param_string comprstr;
const char *param_name;
code = param_read_bool(plist, param_name = "Tumble", &pdf_dev->Tumble);
if (code < 0) {
errprintf(pdf_dev->memory, "Invalid Tumble setting\n");
param_signal_error(plist, param_name, ecode);
return code;
}
code = param_read_bool(plist, param_name = "Tumble2", &pdf_dev->Tumble2);
if (code < 0) {
errprintf(pdf_dev->memory, "Invalid Tumble2 setting\n");
param_signal_error(plist, param_name, ecode);
return code;
}
code = param_read_int(plist, param_name = "StripHeight", &pdf_dev->StripHeight);
if (code < 0) {
errprintf(pdf_dev->memory, "Invalid StripHeight setting\n");
param_signal_error(plist, param_name, ecode);
return code;
}
code = param_read_int(plist, param_name = "JPEGQ", &pdf_dev->JPEGQ);
if (code < 0) {
errprintf(pdf_dev->memory, "Invalid JPEQG setting\n");
param_signal_error(plist, param_name, ecode);
return code;
}
code = param_read_float(plist, param_name = "QFactor", &pdf_dev->QFactor);
if (code < 0) {
errprintf(pdf_dev->memory, "Invalid QFactor setting\n");
param_signal_error(plist, param_name, ecode);
return code;
}
/* Read Compression */
switch (code = param_read_string(plist, (param_name = "Compression"), &comprstr)) {
case 0:
if ((ecode = pdf_image_compression_id(&compr, &comprstr)) < 0) {
errprintf(pdf_dev->memory, "Unknown compression setting\n");
param_signal_error(plist, param_name, ecode);
return ecode;
}
pdf_dev->Compression = compr;
break;
case 1:
break;
default:
ecode = code;
param_signal_error(plist, param_name, ecode);
}
if (which & 1)
{
code = gx_downscaler_read_params(plist, &pdf_dev->downscale,
(GX_DOWNSCALER_PARAMS_MFS |
(which & 2 ? GX_DOWNSCALER_PARAMS_TRAP : 0) |
(which & 4 ? GX_DOWNSCALER_PARAMS_ETS : 0)));
if (code < 0)
{
ecode = code;
param_signal_error(plist, param_name, ecode);
}
}
if (ecode < 0)
return ecode;
code = gdev_prn_put_params(dev, plist);
if (code < 0)
return code;
return code;
}
int
pdf_image_put_params_downscale(gx_device * dev, gs_param_list * plist)
{
return pdf_image_put_some_params(dev, plist, 1);
}
int
pdf_image_put_params_downscale_cmyk(gx_device * dev, gs_param_list * plist)
{
return pdf_image_put_some_params(dev, plist, 3);
}
int
pdf_image_put_params_downscale_cmyk_ets(gx_device * dev, gs_param_list * plist)
{
return pdf_image_put_some_params(dev, plist, 7);
}
void
PCLm_get_initial_matrix(gx_device * dev, register gs_matrix * pmat)
{
gx_device_pdf_image *pdev = (gx_device_pdf_image *)dev;
gx_default_get_initial_matrix(dev, pmat);
if (pdev->Duplex && (pdev->NumPages & 1)) {
/* Duplexing and we are on the back of the page. */
if (pdev->Tumble) {
if (pdev->Tumble2) {
/* Rotate by 180 degrees and flip on X. */
pmat->xx = pmat->xx;
pmat->xy = -pmat->xy;
pmat->yx = -pmat->yx;
pmat->yy = -pmat->yy;
pmat->ty = pdev->height - pmat->ty;
} else {
/* Rotate by 180 degrees */
pmat->xx = -pmat->xx;
pmat->xy = -pmat->xy;
pmat->yx = -pmat->yx;
pmat->yy = -pmat->yy;
pmat->tx = pdev->width - pmat->tx;
pmat->ty = pdev->height - pmat->ty;
}
} else if (pdev->Tumble2) {
/* Flip on X */
pmat->xx = -pmat->xx;
pmat->yx = -pmat->yx;
pmat->tx = pdev->width - pmat->tx;
}
}
}
/* ------ The PCLm device ------ */
static void
PCLm_initialize_device_procs(gx_device *dev)
{
gdev_prn_initialize_device_procs_rgb(dev);
set_dev_proc(dev, open_device, PCLm_open);
set_dev_proc(dev, output_page, gdev_prn_output_page);
set_dev_proc(dev, get_initial_matrix, PCLm_get_initial_matrix);
set_dev_proc(dev, close_device, PCLm_close);
set_dev_proc(dev, get_params, pdf_image_get_params_downscale);
set_dev_proc(dev, put_params, pdf_image_put_params_downscale);
}
static dev_proc_print_page(PCLm_print_page);
const gx_device_pdf_image gs_PCLm_device = {
prn_device_body_duplex(gx_device_pdf_image,
PCLm_initialize_device_procs,
"pclm",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
600, 600, /* 600 dpi by default */
0, 0, 0, 0, /* Margins */
3, /* num components */
24, /* bits per sample */
255, 255, 256, 256,
PCLm_print_page),
3,
GX_DOWNSCALER_PARAMS_DEFAULTS,
16, /* StripHeight */
0.0, /* QFactor */
0 /* JPEGQ */
};
/* Open a temporary file, with or without a stream. */
static int
PCLm_open_temp_file(gx_device_pdf_image *pdev, PCLm_temp_file_t *ptf)
{
char fmode[4];
if (strlen(gp_fmode_binary_suffix) > 2)
return_error(gs_error_invalidfileaccess);
strcpy(fmode, "w+");
strcat(fmode, gp_fmode_binary_suffix);
ptf->file = gp_open_scratch_file(pdev->memory,
gp_scratch_file_name_prefix,
ptf->file_name,
fmode);
if (ptf->file == 0)
return_error(gs_error_invalidfileaccess);
return 0;
}
/* Close and remove temporary files. */
static int
PCLm_close_temp_file(gx_device_pdf_image *pdev, PCLm_temp_file_t *ptf, int code)
{
int err = 0;
gp_file *file = ptf->file;
/*
* ptf->strm == 0 or ptf->file == 0 is only possible if this procedure
* is called to clean up during initialization failure, but ptf->strm
* might not be open if it was finalized before the device was closed.
*/
if (ptf->strm) {
if (s_is_valid(ptf->strm)) {
sflush(ptf->strm);
/* Prevent freeing the stream from closing the file. */
ptf->strm->file = 0;
} else
ptf->file = file = 0; /* file was closed by finalization */
gs_free_object(pdev->memory->non_gc_memory, ptf->strm_buf,
"pdf_close_temp_file(strm_buf)");
ptf->strm_buf = 0;
gs_free_object(pdev->memory->non_gc_memory, ptf->strm,
"pdf_close_temp_file(strm)");
ptf->strm = 0;
}
if (file) {
err = gp_ferror(file) | gp_fclose(file);
unlink(ptf->file_name);
ptf->file = 0;
}
return
(code < 0 ? code : err != 0 ? gs_note_error(gs_error_ioerror) : code);
}
static int
PCLm_open_temp_stream(gx_device_pdf_image *pdev, PCLm_temp_file_t *ptf)
{
int code = PCLm_open_temp_file(pdev, ptf);
if (code < 0)
return code;
ptf->strm = s_alloc(pdev->memory->non_gc_memory, "pdf_open_temp_stream(strm)");
if (ptf->strm == 0) {
PCLm_close_temp_file(pdev, ptf, 0);
return_error(gs_error_VMerror);
}
ptf->strm_buf = gs_alloc_bytes(pdev->memory->non_gc_memory, 512,
"pdf_open_temp_stream(strm_buf)");
if (ptf->strm_buf == 0) {
gs_free_object(pdev->memory->non_gc_memory, ptf->strm,
"pdf_open_temp_stream(strm)");
ptf->strm = 0;
PCLm_close_temp_file(pdev, ptf, 0);
return_error(gs_error_VMerror);
}
swrite_file(ptf->strm, ptf->file, ptf->strm_buf, 512);
return 0;
}
int
PCLm_open(gx_device *pdev)
{
gx_device_pdf_image *ppdev;
int code;
bool update_procs = false;
code = install_internal_subclass_devices((gx_device **)&pdev, &update_procs);
if (code < 0)
return code;
/* If we've been subclassed, find the terminal device */
while(pdev->child)
pdev = pdev->child;
ppdev = (gx_device_pdf_image *)pdev;
memset(&ppdev->ocr, 0, sizeof(ppdev->ocr));
ppdev->file = NULL;
ppdev->Pages = NULL;
ppdev->NumPages = 0;
ppdev->RootOffset = 0;
ppdev->PagesOffset = 0;
ppdev->xrefOffset = 0;
ppdev->NextObject = 0;
code = gdev_prn_allocate_memory(pdev, NULL, 0, 0);
if (code < 0)
return code;
if (update_procs) {
if (pdev->ObjectHandlerPushed) {
gx_copy_device_procs(pdev->parent, pdev, &gs_obj_filter_device);
pdev = pdev->parent;
}
if (pdev->PageHandlerPushed)
gx_copy_device_procs(pdev->parent, pdev, &gs_flp_device);
}
if (ppdev->OpenOutputFile)
code = gdev_prn_open_printer(pdev, 1);
if(code < 0)
return code;
code = PCLm_open_temp_stream(ppdev, &ppdev->xref_stream);
if (code < 0)
return code;
code = PCLm_open_temp_stream(ppdev, &ppdev->temp_stream);
if (code < 0)
PCLm_close_temp_file(ppdev, &ppdev->xref_stream, 0);
return code;
}
static int gdev_PCLm_begin_page(gx_device_pdf_image *pdf_dev,
gp_file *file)
{
gx_device_printer *const pdev = (gx_device_printer *)pdf_dev;
cmm_dev_profile_t *profile_struct;
gsicc_rendering_param_t rendering_params;
int code;
pdfimage_page *page;
page = (pdfimage_page *)gs_alloc_bytes(pdf_dev->memory->non_gc_memory, sizeof(pdfimage_page), "pdfimage create new page");
if (page == NULL)
return_error(gs_error_VMerror);
memset(page, 0x00, sizeof(pdfimage_page));
if (gdev_prn_file_is_new(pdev)) {
/* Set up the icc link settings at this time */
code = dev_proc(pdev, get_profile)((gx_device *)pdev, &profile_struct);
if (code < 0)
return_error(gs_error_undefined);
if (profile_struct->postren_profile != NULL) {
rendering_params.black_point_comp = gsBLACKPTCOMP_ON;
rendering_params.graphics_type_tag = GS_UNKNOWN_TAG;
rendering_params.override_icc = false;
rendering_params.preserve_black = gsBLACKPRESERVE_OFF;
rendering_params.rendering_intent = gsRELATIVECOLORIMETRIC;
rendering_params.cmm = gsCMM_DEFAULT;
if (profile_struct->oi_profile != NULL) {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->oi_profile, profile_struct->postren_profile,
&rendering_params);
} else if (profile_struct->link_profile != NULL) {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->link_profile, profile_struct->postren_profile,
&rendering_params);
} else {
pdf_dev->icclink = gsicc_alloc_link_dev(pdev->memory,
profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE], profile_struct->postren_profile,
&rendering_params);
}
if (pdf_dev->icclink == NULL) {
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
/* If it is identity, release it now and set link to NULL */
if (pdf_dev->icclink->is_identity) {
pdf_dev->icclink->procs.free_link(pdf_dev->icclink);
gsicc_free_link_dev(pdev->memory, pdf_dev->icclink);
pdf_dev->icclink = NULL;
}
}
/* Set up the stream and insert the file header */
pdf_dev->strm = s_alloc(pdf_dev->memory->non_gc_memory, "pdfimage_open_temp_stream(strm)");
if (pdf_dev->strm == 0) {
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
pdf_dev->strm_buf = gs_alloc_bytes(pdf_dev->memory->non_gc_memory, 512,
"pdfimage_open_temp_stream(strm_buf)");
if (pdf_dev->strm_buf == 0) {
pdf_dev->strm->file = NULL; /* Don't close underlying file when we free the stream */
gs_free_object(pdf_dev->memory->non_gc_memory, pdf_dev->strm,
"pdfimage_open_temp_stream(strm)");
pdf_dev->strm = 0;
gs_free_object(pdf_dev->memory->non_gc_memory, page, "pdfimage create new page");
return_error(gs_error_VMerror);
}
swrite_file(pdf_dev->strm, pdf_dev->file, pdf_dev->strm_buf, 512);
stream_puts(pdf_dev->strm, "%PDF-1.3\n");
stream_puts(pdf_dev->strm, "%PCLm 1.0\n");
pdf_dev->Pages = page;
pdf_dev->NextObject = 4;
} else {
pdfimage_page *current = pdf_dev->Pages;
while(current->next)
current = current->next;
current->next = page;
}
page->PageDictObjectNumber = pdf_dev->NextObject++;
page->PageStreamObjectNumber = pdf_dev->NextObject++;
page->ImageObjectNumber = pdf_dev->NextObject++;
return 0;
}
static int
PCLm_downscale_and_print_page(gx_device_printer *dev,
int bpc, int num_comps)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)dev;
int code = 0;
uint Read;
byte *data = NULL;
int size = gdev_mem_bytes_per_scan_line((gx_device *)dev);
int max_size = size;
int row, NumStrips;
double StripDecrement;
const gx_downscaler_params *params = &pdf_dev->downscale;
int factor = params->downscale_factor;
int height = dev->height/factor;
int width = dev->width/factor;
gx_downscaler_t ds;
gs_offset_t stream_pos = 0;
pdfimage_page *page = pdf_dev->Pages;
char Buffer[1024];
if (page == NULL)
return_error(gs_error_undefined);
while(page->next)
page = page->next;
if (pdf_dev->icclink == NULL) {
code = gx_downscaler_init(&ds, (gx_device *)dev,
8, bpc, num_comps,
params,
NULL, /*&fax_adjusted_width*/
0 /*aw*/);
} else {
code = gx_downscaler_init_cm(&ds, (gx_device *)dev,
8, bpc, num_comps,
params,
NULL, /*&fax_adjusted_width*/
0, /*aw*/
pdf_image_chunky_post_cm,
pdf_dev->icclink,
pdf_dev->icclink->num_output);
}
if (code < 0)
return code;
data = gs_alloc_bytes(dev->memory, max_size, "pdf_image_print_page(data)");
if (data == NULL) {
gx_downscaler_fin(&ds);
return_error(gs_error_VMerror);
}
if (pdf_dev->StripHeight == 0) {
NumStrips = 1;
pdf_dev->StripHeight = pdf_dev->height;
}
else
NumStrips = (int)ceil((float)height / pdf_dev->StripHeight);
page->PageDictOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->PageDictOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->PageDictObjectNumber);
pprintd1(pdf_dev->strm, "<<\n/Contents %d 0 R\n", page->PageStreamObjectNumber);
stream_puts(pdf_dev->strm, "/Type /Page\n/Parent 2 0 R\n");
gs_sprintf(Buffer, "/MediaBox [0 0 %.3f %.3f]\n", ((double)pdf_dev->width / pdf_dev->HWResolution[0]) * 72, ((double)pdf_dev->height / pdf_dev->HWResolution[1]) * 72);
stream_puts(pdf_dev->strm, Buffer);
stream_puts(pdf_dev->strm, "/Resources <<\n/XObject <<\n");
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
StripDecrement = pdf_dev->StripHeight / (pdf_dev->HWResolution[1] / (factor * 72));
for (row = 0; row < NumStrips;row++) {
stream_puts(pdf_dev->temp_stream.strm, "/P <</MCID 0>> BDC q\n");
/* Special handling for the last strip, potentially shorter than all the previous ones */
if (row == NumStrips - 1) {
double adjusted;
adjusted = height - (row * pdf_dev->StripHeight);
adjusted = adjusted / (pdf_dev->HWResolution[1] / (factor * 72));
gs_sprintf(Buffer, "%.3f 0 0 %.3f 0 0 cm\n/Im%d Do Q\n", (width / (pdf_dev->HWResolution[0] / 72)) * factor, adjusted, row);
} else
gs_sprintf(Buffer, "%.3f 0 0 %.3f 0 %f cm\n/Im%d Do Q\n", (width / (pdf_dev->HWResolution[0] / 72)) * factor, StripDecrement, ((height / (pdf_dev->HWResolution[1] / 72)) * factor) - (StripDecrement * (row + 1)), row);
stream_puts(pdf_dev->temp_stream.strm, Buffer);
pprintd2(pdf_dev->strm, "/Im%d %d 0 R\n", row, page->ImageObjectNumber + (row * 2));
}
sflush(pdf_dev->temp_stream.strm);
stream_pos = gp_ftell(pdf_dev->temp_stream.file);
stream_puts(pdf_dev->strm, ">>\n>>\n>>\nendobj\n");
page->PageStreamOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->PageStreamOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->PageStreamObjectNumber);
pprintd1(pdf_dev->strm, "<<\n/Length %d\n>>\nstream\n", stream_pos);
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
Read = 0;
while(stream_pos > 0) {
if (stream_pos > 1024) {
stream_pos -= gp_fpread(Buffer, 1024, Read, pdf_dev->temp_stream.file);
Read += 1024;
stream_write(pdf_dev->strm, Buffer, 1024);
}
else {
gp_fpread(Buffer, stream_pos, Read, pdf_dev->temp_stream.file);
stream_write(pdf_dev->strm, Buffer, stream_pos);
stream_pos = 0;
}
}
stream_puts(pdf_dev->strm, "endstream\nendobj\n");
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
pdf_dev->temp_stream.save = pdf_dev->temp_stream.strm;
switch (pdf_dev->Compression) {
case COMPRESSION_FLATE:
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_zlibE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_JPEG:
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_DCTE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_RLE:
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_RLE_template, pdf_dev->memory->non_gc_memory);
break;
default:
break;
}
Read = 0;
for (row = 0; row < height;row++) {
code = gx_downscaler_getbits(&ds, data, row);
if (code < 0)
break;
stream_write(pdf_dev->temp_stream.strm, data, width * num_comps);
Read++;
if (Read == pdf_dev->StripHeight) {
uint R = 0;
if (pdf_dev->temp_stream.save != pdf_dev->temp_stream.strm)
s_close_filters(&pdf_dev->temp_stream.strm, pdf_dev->temp_stream.save);
sflush(pdf_dev->temp_stream.strm);
stream_pos = gp_ftell(pdf_dev->temp_stream.file);
page->ImageOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->ImageOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->ImageObjectNumber++);
pprintd1(pdf_dev->strm, "<<\n/Length %d\n", stream_pos);
stream_puts(pdf_dev->strm, "/Subtype /Image\n");
pprintd1(pdf_dev->strm, "/Width %d\n", width);
pprintd1(pdf_dev->strm, "/Height %d\n", Read);
stream_puts(pdf_dev->strm, "/ColorSpace /DeviceRGB\n");
stream_puts(pdf_dev->strm, "/BitsPerComponent 8\n");
switch (pdf_dev->Compression) {
case COMPRESSION_FLATE:
stream_puts(pdf_dev->strm, "/Filter /FlateDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
case COMPRESSION_JPEG:
stream_puts(pdf_dev->strm, "/Filter /DCTDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
case COMPRESSION_RLE:
stream_puts(pdf_dev->strm, "/Filter /RunLengthDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
default:
case COMPRESSION_LZW:
case COMPRESSION_NONE:
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
}
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
while(stream_pos > 0) {
if (stream_pos > 1024) {
stream_pos -= gp_fpread(Buffer, 1024, R, pdf_dev->temp_stream.file);
R += 1024;
stream_write(pdf_dev->strm, Buffer, 1024);
}
else {
memset(Buffer, 0xf5, 1024);
gp_fpread(Buffer, stream_pos, R, pdf_dev->temp_stream.file);
stream_write(pdf_dev->strm, Buffer, stream_pos);
stream_pos = 0;
}
}
stream_puts(pdf_dev->strm, "\nendstream\nendobj\n");
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
switch (pdf_dev->Compression) {
case COMPRESSION_FLATE:
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_zlibE_template, pdf_dev->memory->non_gc_memory);
break;
case COMPRESSION_JPEG:
{
int t = pdf_dev->StripHeight;
if (height - (row + 1) < pdf_dev->StripHeight)
pdf_dev->StripHeight = height - (row + 1);
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_DCTE_template, pdf_dev->memory->non_gc_memory);
pdf_dev->StripHeight = t;
}
break;
case COMPRESSION_RLE:
encode((gx_device *)pdf_dev, &pdf_dev->temp_stream.strm, &s_RLE_template, pdf_dev->memory->non_gc_memory);
break;
default:
break;
}
Read = 0;
page->ImageOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->ImageOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->ImageObjectNumber++);
stream_puts(pdf_dev->strm, "<</Length 14>>\nstream\nq /image Do Q\nendstream\nendobj\n");
}
}
if (code < 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return code;
}
if (Read) {
uint R = 0, saved = pdf_dev->StripHeight;
pdf_dev->StripHeight = Read;
if (pdf_dev->temp_stream.save != pdf_dev->temp_stream.strm)
s_close_filters(&pdf_dev->temp_stream.strm, pdf_dev->temp_stream.save);
sflush(pdf_dev->temp_stream.strm);
stream_pos = gp_ftell(pdf_dev->temp_stream.file);
page->ImageOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->ImageOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->ImageObjectNumber++);
pprintd1(pdf_dev->strm, "<<\n/Length %d\n", stream_pos);
stream_puts(pdf_dev->strm, "/Subtype /Image\n");
pprintd1(pdf_dev->strm, "/Width %d\n", width);
pprintd1(pdf_dev->strm, "/Height %d\n", Read);
stream_puts(pdf_dev->strm, "/ColorSpace /DeviceRGB\n");
stream_puts(pdf_dev->strm, "/BitsPerComponent 8\n");
switch (pdf_dev->Compression) {
case COMPRESSION_FLATE:
stream_puts(pdf_dev->strm, "/Filter /FlateDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
default:
case COMPRESSION_JPEG:
stream_puts(pdf_dev->strm, "/Filter /DCTDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
case COMPRESSION_RLE:
stream_puts(pdf_dev->strm, "/Filter /RunLengthDecode\n");
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
case COMPRESSION_LZW:
case COMPRESSION_NONE:
stream_puts(pdf_dev->strm, ">>\nstream\n");
break;
}
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
while(stream_pos > 0) {
if (stream_pos > 1024) {
stream_pos -= gp_fpread(Buffer, 1024, R, pdf_dev->temp_stream.file);
R += 1024;
stream_write(pdf_dev->strm, Buffer, 1024);
}
else {
gp_fpread(Buffer, stream_pos, R, pdf_dev->temp_stream.file);
stream_write(pdf_dev->strm, Buffer, stream_pos);
stream_pos = 0;
}
}
stream_puts(pdf_dev->strm, "\nendstream\nendobj\n");
if (gp_fseek(pdf_dev->temp_stream.file, 0, SEEK_SET) != 0) {
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
gx_downscaler_fin(&ds);
return_error(gs_error_ioerror);
}
page->ImageOffset = stell(pdf_dev->strm);
write_xref_entry(pdf_dev->xref_stream.strm, page->ImageOffset);
pprintd1(pdf_dev->strm, "%d 0 obj\n", page->ImageObjectNumber++);
stream_puts(pdf_dev->strm, "<</Length 14>>\nstream\nq /image Do Q\nendstream\nendobj\n");
pdf_dev->StripHeight = saved;
}
pdf_dev->NextObject = page->ImageObjectNumber;
gx_downscaler_fin(&ds);
gs_free_object(dev->memory, data, "pdf_image_print_page(data)");
pdf_dev->NumPages++;
return code;
}
static int
PCLm_print_page(gx_device_printer * pdev, gp_file * file)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)pdev;
const char *fmt;
gs_parsed_file_name_t parsed;
int code;
code = gdev_PCLm_begin_page(pdf_dev, file);
if (code < 0)
return code;
code = PCLm_downscale_and_print_page(pdev,
8, pdf_dev->color_info.num_components);
if (code < 0)
return code;
code = gx_parse_output_file_name(&parsed, &fmt, pdev->fname,
strlen(pdev->fname), pdev->memory);
if (code >= 0 && fmt) {
code = pdf_image_finish_file(pdf_dev, 1);
}
return code;
}
int
PCLm_close(gx_device * pdev)
{
gx_device_pdf_image *const pdf_dev = (gx_device_pdf_image *)pdev;
int code, code1;
code = pdf_image_finish_file(pdf_dev, 1);
if (code < 0)
return code;
code = PCLm_close_temp_file(pdf_dev, &pdf_dev->xref_stream, 0);
code1 = PCLm_close_temp_file(pdf_dev, &pdf_dev->temp_stream, 0);
if (code == 0)
code = code1;
code1 = gdev_prn_close(pdev);
if (code == 0)
code = code1;
return code;
}
|