File: csepdjvu.cpp

package info (click to toggle)
djvulibre 3.5.27.1-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,976 kB
  • ctags: 8,037
  • sloc: cpp: 65,455; xml: 5,723; ansic: 4,640; sh: 846; makefile: 315
file content (1886 lines) | stat: -rw-r--r-- 54,348 bytes parent folder | download
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
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
//C-  -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001  AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, either Version 2 of the license,
//C- or (at your option) any later version. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//C- GNU General Public License for more details.
//C- 
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
//C- Lizardtech Software.  Lizardtech Software has authorized us to
//C- replace the original DjVu(r) Reference Library notice by the following
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
//C-
//C-  ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, either Version 2 of the license,
//C- | or (at your option) any later version. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
//C- | General Public License.   This grant only confers the right to 
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
//C- | the extent such infringement is reasonably necessary to enable 
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
//C- | any greater extent that may be necessary to utilize further 
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif

/** @name csepdjvu

    {\bf Synopsis}
    \begin{verbatim}
        csepdjvu <....options-or-separated_files...> <outputdjvufile>
    \end{verbatim}

    {\bf Description}
    
    File #"csepdjvu.cpp"# demonstrates a complete back-end encoder that takes
    {\em separated files} as input and produces a djvu file as output.
    Each {\em separated file} contains a concatenation of pages. 
    Each page contains the following components:
    \begin{itemize}
    \item A run-length encoded file representing the foreground.
          Two kind of run-length encoded files are accepted.
          The Black-And-White RLE format is described in section
          \Ref{PNM and RLE file formats}.  The Color RLE format
          is described below.
    \item An optional PPM image representing the background.
          The size (width and height) of the PPM image must be obtained by
          rounding up the quotient of the foreground image size by
          an integer reduction factor ranging from 1 to 12.
    \item An arbitrary number of comment lines starting with 
          character '#'.
    \end{itemize}
    All the provided pages will be converted to Compound DjVu Images.
    Foreground colors will be encoded using a single solid color per component
    (see \Ref{DjVu Image files}).  Multiple pages will be gathered into a
    single bundled file.  Use \Ref{djvmcvt} or \Ref{djvujoin} to create an
    indirect file.
    
    {\bf Options}

    \begin{description}
    \item[-d n] Resolution written into the output file (default: 300).
    \item[-q <spec>] Quality level for background (default: 72+11+10+10).
                     See option #"-slice"# in program \Ref{c44} for details.
    \item[-v] Displays a brief message per page.
    \item[-vv] Displays lots of additional messages.
    \end{description}
          
    {\bf Color RLE Images}

    The Color RLE file format is a simple run-length encoding scheme for color
    images with a limited number of colors. Color RLE files always begin with
    a text header composed of:
    - the two characters #"R6"#,
    - the number of columns in decimal,\\
    - the number of rows in decimal,\\
    - the number of palette entries in decimal.\\
    These four items are separated by blank characters (space, tab, cr, or nl)
    or by comment lines introduced by character ``\#''.  The last number is
    followed by exactly one character (usually a nl character).  This header
    is followed by a palette containing three bytes per color.  The bytes
    represent the red, green, and blue components of the color.  

    The palette is followed by four bytes integers (MSB first) representing
    runs.  The twelve upper bits of this integer indicate the index of the run
    color in the palette entry.  The twenty lower bits of the integer indicate
    the run length.  Color indices greater than 0xff0 are reserved for pixels
    belonging to the background layer.  Color index 0xfff is used for
    transparent runs. Color index 0xffe is used for don't-care runs
    (i.e. pixels whose values should be taken by smoothly completing the
    background using the wavelet masking algorithm).  Each row is represented
    by a sequence of runs whose lengths add up to the image width.  Rows are
    encoded starting with the top row and progressing towards the bottom row.

    @memo
    Creates DjVu files from Separated files.
    @author
    L\'eon Bottou <leonb@research.att.com>
*/
//@{
//@}


#include "DjVuGlobal.h"
#include "GException.h"
#include "GSmartPointer.h"
#include "GContainer.h"
#include "ByteStream.h"
#include "IFFByteStream.h"
#include "GRect.h"
#include "GBitmap.h"
#include "JB2Image.h"
#include "DjVuPalette.h"
#include "IW44Image.h"
#include "DjVuInfo.h"
#include "DjVmDoc.h"
#include "DjVmNav.h"
#include "GOS.h"
#include "GURL.h"
#include "DjVuMessage.h"
#include "DjVuText.h"
#include "BSByteStream.h"

#include "miniexp.h"
#include "jb2tune.h"
#include "common.h"

#undef MIN
#undef MAX
inline int MIN(int a, int b) { return ( a<b ?a :b); }
inline int MAX(int a, int b) { return ( a>b ?a :b); }



// --------------------------------------------------
// OPTIONS
// --------------------------------------------------


struct csepdjvuopts
{
  int dpi;                  // resolution
  int verbose;              // verbosity level
  DjVuTXT::ZoneType text;   // level of text detail
  unsigned char slice[16];  // background quality spec
  csepdjvuopts();
};

csepdjvuopts::csepdjvuopts()
{
      dpi = 300;
      verbose = 0;
      text = DjVuTXT::WORD;
      slice[0] =  72;
      slice[1] =  83;      
      slice[2] =  93;
      slice[3] = 103;
      slice[4] =   0;
}


// --------------------------------------------------
// BUFFERED BYTESTREAM
// --------------------------------------------------

// -- A bytestream that performs buffering and 
//    offers a stdio-like interface for parsing files.

class BufferByteStream : public ByteStream 
{
public:
  enum {bufsize=512};
private:
  ByteStream &bs;
  unsigned char buffer[bufsize];
  int bufpos;
  int bufend;
public:
  BufferByteStream(ByteStream &lbs);
  size_t read(void *buffer, size_t size);
  size_t write(const void *buffer, size_t size);
  virtual long tell(void) const;
  int eof(void);
  int unget(int c);
  inline int get(void);
  // parsing helpers
  bool skip(const char *s = " \t\n\r");
  bool expect(int &c, const char *s); 
  bool read_integer(int &x);
  bool read_pair(int &x, int &y);
  bool read_geometry(GRect &r);
  bool read_ps_string(GUTF8String &s);
};

BufferByteStream::BufferByteStream(ByteStream &bs)
  : bs(bs), bufpos(1), bufend(1)
{ 
}

int 
BufferByteStream::eof(void) // aka. feof
{
  if (bufpos < bufend) 
    return false;
  bufend = bufpos = 1;
  bufend += bs.read(buffer+bufend, bufsize-bufend);
  return (bufend == bufpos);
}

size_t 
BufferByteStream::read(void *buf, size_t size)
{
  if (size < 1)
    return 0;
  if (bufend == bufpos) 
    {
      if (size >= bufsize)
        return bs.read(buf, size);
      if (eof())
        return 0;
    }
  if (bufpos + (int)size > bufend)
    size = bufend - bufpos;
  memcpy(buf, buffer+bufpos, size);
  bufpos += size;
  return size;
}

size_t 
BufferByteStream::write(const void *, size_t )
{
  G_THROW("Cannot write into a BufferByteStream");
  return 0;
}

long 
BufferByteStream::tell(void) const
{ 
  return bs.tell() + bufpos - bufend; 
}
    
inline int 
BufferByteStream::get(void) // aka. getc()
{
  if (bufpos < bufend || !eof())
    return buffer[bufpos++];
  return EOF;
}

int  
BufferByteStream::unget(int c) // aka. ungetc()
{
  if (bufpos > 0 && c != EOF) 
    return buffer[--bufpos] = (unsigned char)c;
  return EOF;
}

bool 
BufferByteStream::expect(int &c, const char *s)
{
  c = get();
  if (strchr(s, c))
    return true;
  unget(c);
  return false;
}

bool
BufferByteStream::skip(const char *s)
{
  int c;
  while (expect(c, s)) { }
  return true;
}

bool 
BufferByteStream::read_integer(int &x)
{
  x = 0;
  int c = get();
  if (c<'0' || c>'9')
    return false;
  while (c>='0' && c<='9') 
    {
      x = x*10 + c - '0';
      c = get();
    }
  unget(c);
  return true;
}

bool 
BufferByteStream::read_pair(int &x, int &y)
{
  int c;
  x = y = 0;
  expect(c, "-");
  if (! read_integer(x)) 
    return false;
  if (c == '-') 
    x = -x;
  if (! expect(c, ":"))
    return false;
  expect(c, "-");
  if (! read_integer(y)) 
    return false;
  if (c == '-')
    y = -y;
  return true;
}

bool 
BufferByteStream::read_geometry(GRect &r)
{
  int x,y,w,h,c;
  x = y = w = h = 0;
  if (read_integer(w) && expect(c, "x") && read_integer(h))
    {
      if (expect(c,"+-"))
        {
          if (c == '+')
            expect(c,"-");
          if (! read_integer(x))
            return false;
          if (c == '-')
            x = -x;
        }
      if (expect(c,"+-"))
        {
          if (c == '+')
            expect(c,"-");
          if (! read_integer(y))
            return false;
          if (c == '-')
            y = -y;
        }
      r = GRect(x,y,w,h);
      return true;
    }
  return false;
}

static void
add_to_string(GUTF8String &s, char *buffer, int len, int &bom)
{
  if (!s && !bom && len>=2)
    {
      if (buffer[0]==(char)0xfe && buffer[1]==(char)0xff)
        bom = 0xfeff;
      if (buffer[0]==(char)0xff && buffer[1]==(char)0xfe)
        bom = 0xfffe;
      if (bom)
        {
          buffer += 2; 
          len -= 2;
        }
    }
  if (bom == 0xfeff)
    for (int i=0; i<len; i+=2)
      *(uint16_t*)(buffer+i) = ((buffer[i]<<8) | buffer[i+1]);
  if (bom == 0xfffe)
    for (int i=0; i<len; i+=2)
      *(uint16_t*)(buffer+i) = ((buffer[i+1]<<8) | buffer[i]);
  if (bom)
    s += GUTF8String((const uint16_t*)buffer, len/2);
  else
    s += GUTF8String((const char*)buffer, len);
}

bool 
BufferByteStream::read_ps_string(GUTF8String &s)
{
  int bom = 0;
  unsigned int pos = 0;
  char buffer[512];
  if (get() != '(') 
    return false;
  s = "";
  for(;;)
    {
      int c = get();
      if (c == '\n' || c == '\r')
        return false;
      if (c == ')')
        break;
      if (c == '\\')
        {
          c = get();
          switch (c) 
            {
            case 'n': 
              c='\n' ; break;
            case 'r': 
              c='\r' ; break;
            case 't': 
              c='\t' ; break;
            case 'b': 
              c='\b' ; break;
            case 'f': 
              c='\f' ; break;
            default:
              if (c>='0' && c<='7') 
                {
                  int n = 0;
                  int x = 0;
                  while (c>='0' && c<='7' && n<3)
                    {
                      x = (x * 8) + c - '0';
                      c = get();
                      n++;
                    }
                  unget(c);
                  c = x;
                }
              break;
            }
        }
      if (c == EOF)
        return false;
      if (pos >= (int)sizeof(buffer))
        {
          add_to_string(s, buffer, pos, bom);
          pos = 0;
        }
      buffer[pos++] = c;
    }
  add_to_string(s, buffer, pos, bom);
  return true;
}

// --------------------------------------------------
// COLOR CONNECTED COMPONENT ANALYSIS
// --------------------------------------------------


// -- A run of pixels with the same color
struct Run    
{ 
  short y;       // vertical coordinate
  short x1;      // first horizontal coordinate
  short x2;      // last horizontal coordinate
  short color;   // color id
  int ccid;      // component id
};


// -- Compares runs for y-x sorting
static inline bool
operator <= (const Run &a, const Run &b)
{
  return (a.y<b.y) || (a.y==b.y && a.x1<=b.x1);
}


// -- Color component descriptor
struct CC    
{
  GRect bb;      // bounding box
  int npix;      // number of black pixels
  int nrun;      // number of runs
  int frun;      // first run in cc ordered array of runs
  int color;     // color id
};


// -- An image composed of runs
class CRLEImage 
{
public:
  int height;            // Height of the image in pixels
  int width;             // Width of the image in pixels
  GP<DjVuPalette> pal;   // Color palette
  GTArray<Run>    runs;  // Array of runs
  GTArray<CC>     ccs;   // Array of component descriptors
  int  nregularccs;      // Number of regular ccs (set by merge_and_split_ccs)
  char bg_flags;         // Comment flags about background.
  char fg_flags;         // Comment flags about foreground.
  CRLEImage(BufferByteStream &bs);
  GP<GBitmap> get_bitmap_for_cc(int ccid) const;
  void make_ccids_by_analysis();
  void make_ccs_from_ccids();
  void merge_and_split_ccs(int smallsize, int largesize);
  void sort_in_reading_order(); 
private:
  unsigned int read_integer(BufferByteStream &bs);
  void insert_runs(int y, const short *x1x2color, int nruns);
};


// -- Helper for CRLEImage::CRLEImage(ByteStream &bs)
unsigned int 
CRLEImage::read_integer(BufferByteStream &bs)
{
  int c, x;
  while (bs.skip() && bs.expect(c, "#"))
    {
      char buffer[256];
      char *s = buffer;
      while (c != EOF && c != '\n' && c != '\r')
        {
          if (s - buffer < (int)sizeof(buffer) - 1)
            *s++ = c;
          c = bs.get();
        }
      *s = 0;
      for(s = buffer; *s; s++)
        {
          if (!strncmp(s, "bg-", 3))
            {
              if (!strncmp(s+3,"bw",2)   ||
                  !strncmp(s+3,"gray",4) ||
                  !strncmp(s+3,"color",5)  )
                bg_flags = s[3];
            }
          if (!strncmp(s, "fg-", 3))
            {
              if (!strncmp(s+3,"bw",2)   ||
                  !strncmp(s+3,"gray",4) ||
                  !strncmp(s+3,"color",5)  )
                fg_flags = s[3];
            }
        }
    }
  if (! bs.read_integer(x) )
    G_THROW("csepdjvu: corrupted input file (bad file header)");
  return x;
}


// -- Helper for CRLEImage::CRLEImage(ByteStream &bs)
void
CRLEImage::insert_runs(int y, const short *x1x2color, int count)
{
  if (count > 0)
    {
      int index = runs.lbound() - count;
      runs.resize(index, runs.hbound());
      Run *run = &runs[index];
      while (--count>=0) {
        run->y     = y;
        run->x1    = *x1x2color++;
        run->x2    = *x1x2color++;
        run->color = *x1x2color++;
        run->ccid  = 0;
        run++;
      }
      runs.shift(-index);
    }
}


// -- Constructs CRLEImage from a run lenght encoded file,
//    making sure that runs are properly sorted.
CRLEImage::CRLEImage(BufferByteStream &bs)
  : height(0), width(0), nregularccs(0), bg_flags(0), fg_flags(0)
{
  unsigned int magic = bs.read16();
  width = read_integer(bs);
  height = read_integer(bs);
  if (width<1 || height<1)
    G_THROW("csepdjvu: corrupted input file (bad image size)");
  // An array for the runs and the buffered data
  GTArray<short> ax(3*width+3);
  // File format switch
  if (magic == 0x5234) // Black&White RLE data
    {
      // Skip one character
      bs.get(); 
      // Setup palette with one color
      pal = DjVuPalette::create();
      static char zeroes[4];
      GP<ByteStream> gzbs=ByteStream::create_static(zeroes,4);
      ByteStream &zbs=*gzbs;
      pal->decode_rgb_entries(zbs, 1);
      // RLE format
      int x, c, n;
      unsigned char p = 0;
      short *px = ax;
      n = height - 1;
      c = 0;
      while (n >= 0)
        {
          if (bs.eof())
            G_THROW( ByteStream::EndOfFile );
          x = bs.get();
          if (x >= 0xc0)
            x = (bs.get()) + ((x - 0xc0) << 8);
          if (c+x > width)
            G_THROW("csepdjvu: corrupted input file (lost RLE sync.)");
          if (p)
            {
              px[0] = c;
              px[1] = c+x-1;
              px[2] = 0;
              px += 3;
            }
          c += x;
          p = 1 - p;
          if (c >= width)
            {
              insert_runs(n, ax, (px-ax)/3);
              c = 0;
              p = 0;
              n -= 1; 
              px = ax;
            }
        }
    } else if (magic == 0x5236) { // Color-RLE data 
      // Read ncolors and skip one character.
      int ncolors = read_integer(bs);
      bs.get();
      // Setup palette 
      if (ncolors<1 || ncolors>4095) 
        G_THROW("csepdjvu: corrupted input file (bad number of colors)");

      pal = DjVuPalette::create();
      pal->decode_rgb_entries(bs, ncolors);
      // RLE format
      int x, c, n, p;
      n = height - 1;
      c = 0;
      short *px = ax;
      while (n >= 0)
        {
          if (bs.eof())
            G_THROW( ByteStream::EndOfFile );
          x  = (bs.get() << 24);
          x  |= (bs.get() << 16);
          x  |= (bs.get() << 8);
          x  |= (bs.get());
          p = (x >> 20) & 0xfff;
          x = (x & 0xfffff);
          if (c+x > width)
            G_THROW("csepdjvu: corrupted input file (lost RLE sync.)");
          if (p >= 0 && p < ncolors)
            {
              px[0] = c;
              px[1] = c+x-1;
              px[2] = p;
              px += 3;
            }
          c += x;
          if (c >= width)
            {
              insert_runs(n, ax, (px-ax)/3);
              c = 0;
              p = 0;
              n -= 1; 
              px = ax;
            }
        }
    } else { // Unrecognized file
      G_THROW("csepdjvu: corrupted input file (bad file header)");
    }
}


// -- Performs color connected component analysis
//    assuming that runs are already y-x sorted.
void
CRLEImage::make_ccids_by_analysis()
{
  // runs.sort(); (we know that runs are 
  // Single Pass Connected Component Analysis (with unodes)
  int n;
  int p=0;
  GTArray<int> umap;
  for (n=0; n<=runs.hbound(); n++)
    {
      int y = runs[n].y;
      int x1 = runs[n].x1 - 1;
      int x2 = runs[n].x2 + 1;
      int color = runs[n].color;
      int id = (umap.hbound() + 1);
      // iterate over previous line runs
      if (p>0) p--;
      for(;runs[p].y < y-1;p++);
      for(;(runs[p].y < y) && (runs[p].x1 <= x2);p++ )
        {
          if ( runs[p].x2 >= x1 )
            {
              if (runs[p].color == color)
                {
                  // previous run touches current run and has same color
                  int oid = runs[p].ccid;
                  while (umap[oid] < oid)
                    oid = umap[oid];
                  if ((int)id > umap.hbound()) {
                    id = oid;
                  } else if (id < oid) {
                    umap[oid] = id;
                  } else {
                    umap[id] = oid;
                    id = oid;
                  }
                  // freshen previous run id
                  runs[p].ccid = id;
                }
              // stop if previous run goes past current run
              if (runs[p].x2 >= x2)
                break;
            }
        }
      // create new entry in umap
      runs[n].ccid = id;
      if (id > umap.hbound())
        {
          umap.touch(id);
          umap[id] = id;
        }
    }
  // Update umap and ccid
  for (n=0; n<=runs.hbound(); n++)
    {
      Run &run = runs[n];
      int ccid = run.ccid;
      while (umap[ccid] < ccid)
        ccid = umap[ccid];
      umap[run.ccid] = ccid;
      run.ccid = ccid;
    }
}


// -- Constructs the ``ccs'' array from run's ccids.
void
CRLEImage::make_ccs_from_ccids()
{
  int n;
  Run *pruns = runs;
  // Find maximal ccid
  int maxccid = -1;
  for (n=0; n<=runs.hbound(); n++)
    if (pruns[n].ccid > maxccid)
      maxccid = runs[n].ccid;
  GTArray<int> armap(0,maxccid);
  int *rmap = armap;
  // Renumber ccs 
  for (n=0; n<=maxccid; n++)
    armap[n] = -1;
  for (n=0; n<=runs.hbound(); n++)
    if (pruns[n].ccid >= 0)
      rmap[ pruns[n].ccid ] = 1;
  int nid = 0;
  for (n=0; n<=maxccid; n++)
    if (rmap[n] > 0)
      rmap[n] = nid++;
  // Adjust nregularccs (since ccs are renumbered)
  while (nregularccs>0 && rmap[nregularccs-1]<0)
    nregularccs -= 1;
  if (nregularccs>0)
    nregularccs = 1 + rmap[nregularccs-1];
  // Prepare cc descriptors
  ccs.resize(0,nid-1);
  for (n=0; n<nid; n++)
    ccs[n].nrun = 0;
  // Relabel runs
  for (n=0; n<=runs.hbound(); n++)
    {
      Run &run = pruns[n];
      if (run.ccid < 0) continue;  // runs with negative ccids are destroyed
      int oldccid = run.ccid;
      int newccid = rmap[oldccid];
      CC &cc = ccs[newccid];
      run.ccid = newccid;
      cc.nrun += 1;
    }
  // Compute positions for runs of cc
  int frun = 0;
  for (n=0; n<nid; n++) 
    {
      ccs[n].frun = rmap[n] = frun;
      frun += ccs[n].nrun;
    }
  // Copy runs
  GTArray<Run> rtmp;
  rtmp.steal(runs);
  Run *ptmp = rtmp;
  runs.resize(0,frun-1);
  pruns = runs;
  for (n=0; n<=rtmp.hbound(); n++)
    {
      int id = ptmp[n].ccid;
      if (id < 0) continue;
      int pos = rmap[id]++;
      pruns[pos] = ptmp[n];
    }
  // Finalize ccs
  for (n=0; n<nid; n++)
    {
      CC &cc = ccs[n];
      int npix = 0;
      runs.sort(cc.frun, cc.frun+cc.nrun-1);
      Run *run = &runs[cc.frun];
      int xmin = run->x1;
      int xmax = run->x2;
      int ymin = run->y;
      int ymax = run->y;
      cc.color = run->color;
      for (int i=0; i<cc.nrun; i++, run++)
        {
          if (run->x1 < xmin)  xmin = run->x1;
          if (run->x2 > xmax)  xmax = run->x2;
          if (run->y  < ymin)  ymin = run->y;
          if (run->y  > ymax)  ymax = run->y;
          npix += run->x2 - run->x1 + 1;
        }
      cc.npix = npix;
      cc.bb.xmin = xmin;
      cc.bb.ymin = ymin;
      cc.bb.xmax = xmax + 1;
      cc.bb.ymax = ymax + 1;
    }
}


// -- Helper for merge_and_split_ccs
struct Grid_x_Color 
{
  short gridi;
  short gridj;
  int color;
};


// -- Helper for merge_and_split_ccs
static inline unsigned int
hash(const Grid_x_Color &x) 
{
  return (x.gridi<<16) ^ (x.gridj<<8) ^ x.color;
}


// -- Helper for merge_and_split_ccs
static inline bool
operator==(const Grid_x_Color &x, const Grid_x_Color &y)
{
  return (x.gridi==y.gridi) && (x.gridj==y.gridj) && (x.color==y.color);
}


// -- Helper for merge_and_split_ccs
static int
makeccid(const Grid_x_Color &x, GMap<Grid_x_Color,int> &map, int &ncc)
{
  GPosition p = map.contains(x);
  if (p) return map[p];
  return map[x] = ncc++;
}


// -- Merges small ccs of similar color and splits large ccs
void
CRLEImage::merge_and_split_ccs(int smallsize, int largesize)
{
  int ncc = ccs.size();
  int nruns = runs.size();
  int splitsize = largesize;
  if (ncc <= 0) return;
  // Associative map for storing merged ccids
  GMap<Grid_x_Color,int> map;
  nregularccs = ncc;
  // Set the correct ccids for the runs
  for (int ccid=0; ccid<ccs.size(); ccid++)
    {
      CC* cc = &ccs[ccid];
      if (cc->nrun <= 0) continue;
      Grid_x_Color key;
      key.color = cc->color;
      int ccheight = cc->bb.height();
      int ccwidth = cc->bb.width();
      if (ccheight<=smallsize && ccwidth<=smallsize)
        {
          key.gridi = (cc->bb.ymin+cc->bb.ymax)/splitsize/2;
          key.gridj = (cc->bb.xmin+cc->bb.xmax)/splitsize/2;
          int newccid = makeccid(key, map, ncc);
          for(int runid=cc->frun; runid<cc->frun+cc->nrun; runid++)
            runs[runid].ccid = newccid;
        }
      else if (ccheight>=largesize || ccwidth>=largesize)
        {
          for(int runid=cc->frun; runid<cc->frun+cc->nrun; runid++)
            {
              Run *r = & runs[runid];
              key.gridi = r->y/splitsize;
              key.gridj = r->x1/splitsize;
              int gridj_end = r->x2/splitsize;
              int gridj_span = gridj_end - key.gridj;
              r->ccid = makeccid(key, map, ncc);
              if (gridj_span>0)
                {
                  // truncate current run 
                  runs.touch(nruns+gridj_span-1);
                  r = &runs[runid];
                  int x = key.gridj*splitsize + splitsize;
                  int x_end = r->x2;
                  r->x2 = x-1;
                  // append additional runs to the runs array
                  while (++key.gridj < gridj_end)
                    {
                      Run& newrun = runs[nruns++];
                      newrun.y = r->y;
                      newrun.x1 = x;
                      x += splitsize;
                      newrun.x2 = x-1;
                      newrun.color = key.color;
                      newrun.ccid = makeccid(key, map, ncc);
                    }
                  // append last run to the run array
                  Run& newrun = runs[nruns++];
                  newrun.y = r->y;
                  newrun.x1 = x;
                  newrun.x2 = x_end;
                  newrun.color = key.color;
                  newrun.ccid = makeccid(key, map, ncc);
                }
            }
        }
    }
  // Recompute cc descriptors
  make_ccs_from_ccids();
}


// -- Helps sorting cc
static int 
top_edges_descending (const void *pa, const void *pb)
{
  if (((CC*) pa)->bb.ymax != ((CC*) pb)->bb.ymax)
    return (((CC*) pb)->bb.ymax - ((CC*) pa)->bb.ymax);
  if (((CC*) pa)->bb.xmin != ((CC*) pb)->bb.xmin)
    return (((CC*) pa)->bb.xmin - ((CC*) pb)->bb.xmin);
  return (((CC*) pa)->frun - ((CC*) pb)->frun);
}


// -- Helps sorting cc
static int 
left_edges_ascending (const void *pa, const void *pb)
{
  if (((CC*) pa)->bb.xmin != ((CC*) pb)->bb.xmin)
    return (((CC*) pa)->bb.xmin - ((CC*) pb)->bb.xmin);
  if (((CC*) pb)->bb.ymax != ((CC*) pa)->bb.ymax)
    return (((CC*) pb)->bb.ymax - ((CC*) pa)->bb.ymax);
  return (((CC*) pa)->frun - ((CC*) pb)->frun);
}


// -- Helps sorting cc
static int 
integer_ascending (const void *pa, const void *pb)
{
  return ( *(int*)pb - *(int*)pa );
}


// -- Sort ccs in approximate reading order
void 
CRLEImage::sort_in_reading_order()
{
  if (nregularccs<2) return;
  CC *ccarray = new CC[nregularccs];
  // Copy existing ccarray (but segregate special ccs)
  int ccid;
  for(ccid=0; ccid<nregularccs; ccid++)
    ccarray[ccid] = ccs[ccid];
  // Sort the ccarray list into top-to-bottom order.
  qsort (ccarray, nregularccs, sizeof(CC), top_edges_descending);
  // Subdivide the ccarray list roughly into text lines
  int maxtopchange = width / 40;
  if (maxtopchange < 32) 
    maxtopchange = 32;
  // - Loop until processing all ccs
  int ccno = 0;
  int *bottoms = new int[nregularccs];
  while (ccno < nregularccs)
    {
      // - Gather first line approximation
      int nccno;
      int sublist_top = ccarray[ccno].bb.ymax-1;
      int sublist_bottom = ccarray[ccno].bb.ymin;
      for (nccno=ccno; nccno < nregularccs; nccno++)
        {
          if (ccarray[nccno].bb.ymax-1 < sublist_bottom) break;
          if (ccarray[nccno].bb.ymax-1 < sublist_top - maxtopchange) break;
          int bottom = ccarray[nccno].bb.ymin;
          bottoms[nccno-ccno] = bottom;
          if (bottom < sublist_bottom)
            sublist_bottom = bottom;
        }
      // - If more than one candidate cc for the line
      if (nccno > ccno + 1)
        {
          // - Compute median bottom
          qsort(bottoms, nccno-ccno, sizeof(int), integer_ascending);
          int bottom = bottoms[ (nccno-ccno-1)/2 ];
          // - Compose final line
          for (nccno=ccno; nccno < nregularccs; nccno++)
            if (ccarray[nccno].bb.ymax-1 < bottom)
              break;
          // - Sort final line
          qsort (ccarray+ccno, nccno-ccno, sizeof(CC), left_edges_ascending);
        }
      // - Next line
      ccno = nccno;
    }
  // Copy ccarray back and renumber the runs
  for(ccid=0; ccid<nregularccs; ccid++)
    {
      CC& cc = ccarray[ccid];
      ccs[ccid] = cc;
      for(int r=cc.frun; r<cc.frun+cc.nrun; r++)
        runs[r].ccid = ccid;
    }
  // Free memory
  delete [] bottoms;
  delete[] ccarray;
}


// -- Creates a bitmap for a particular component
GP<GBitmap>   
CRLEImage::get_bitmap_for_cc(const int ccid) const
{
  const CC &cc = ccs[ccid];
  const GRect &bb = cc.bb;
  GP<GBitmap> bits = GBitmap::create(bb.height(), bb.width());
  const Run *prun = & runs[(int)cc.frun];
  for (int i=0; i<cc.nrun; i++,prun++)
    {
      if (prun->y<bb.ymin || prun->y>=bb.ymax)
        G_THROW("Internal error (y bounds)");
      if (prun->x1<bb.xmin || prun->x2>=bb.xmax)
        G_THROW("Internal error (x bounds)");
      unsigned char *row = (*bits)[prun->y - bb.ymin];
      for (int x=prun->x1; x<=prun->x2; x++)
        row[x - bb.xmin] = 1;
    }
  return bits;
}



// --------------------------------------------------
// PROCESS BACKGROUND PIXMAP
// --------------------------------------------------


// -- Tries to read a background pixmap
GP<GPixmap>
read_background(BufferByteStream &bs, int w, int h, int &bgred)
{
  // Skip null bytes (why?)
  int lookahead;
  while (! (lookahead = bs.get())) { }
  bs.unget(lookahead);
  // Check pixmap
  if (lookahead != 'P') 
    return 0;
  GP<GPixmap> pix = GPixmap::create(bs);
  // Check background reduction
  for (bgred=1; bgred<=12; bgred++) 
    {
      int subw = (w + bgred - 1) / bgred;
      int subh = (h + bgred - 1) / bgred;
      if (subh == (int)pix->rows() && subw == (int)pix->columns())
        // Found reduction factor
        return pix;
    }
  // Failure
  G_THROW("Background pixmap size does not match foreground");
  return 0;
}


// --------------------------------------------------
// HANDLE COMMENTS IN SEP FILES
// --------------------------------------------------

class Comments
{
public:
  Comments(int w, int h, const csepdjvuopts &opts);
  void process_comments(BufferByteStream &bs, int verbose=0);
  bool parse_comment_line(BufferByteStream &bs);
  void make_chunks(IFFByteStream &iff);
  GP<DjVmNav> get_djvm_nav();
  GUTF8String get_pagetitle();
protected:
  int w;
  int h;
  GRectMapper mapper;
  DjVuTXT::ZoneType detail;
  int lastx, lasty;
  int lastdirx, lastdiry;
  int lastsize[3];
  struct TxtMark : public GPEnabled {
    int x,y,dx,dy;
    int inter;
    GRect r;
    GUTF8String s;
  };
  GPList<TxtMark> lastline;
  GP<DjVuTXT> txt;
  struct LnkMark : public GPEnabled {
    GRect r;
    GUTF8String s;
  };
  GPList<LnkMark> links;
  GP<DjVmNav> nav;
  GUTF8String pagetitle;
protected:
  bool allspace(const TxtMark *mark);
  void textmark(GP<TxtMark> mark);
  void textflush(void);
};

Comments::Comments(int w, int h, const csepdjvuopts &opts)
  : w(w), h(h), detail(opts.text)
{
  GRect pagerect(0,0,w,h);
  mapper.set_input(pagerect);
  mapper.set_output(pagerect);
  mapper.mirrory();
}

void 
Comments::process_comments(BufferByteStream &bs, int verbose)
{
  int c;
  // Skip null bytes
  while (! (c = bs.get())) { }
  // Process comment lines
  while (c == '#')
    {
      const char *message = 0;
      bs.skip(" \t");
      G_TRY
        {
          if (! parse_comment_line(bs) && verbose > 1)
            message = "csepdjvu: unrecognized comment '# ";
          else if (bs.skip(" \t") && bs.expect(c, "\n\r"))
            bs.unget(c);
          else if (verbose > 1)
            message = "csepdjvu: garbage in comments: '";
        }
      G_CATCH(ex)
      {
        message = 0;
        GUTF8String str = DjVuMessageLite::LookUpUTF8(ex.get_cause());
        if (verbose > 1)
          DjVuPrintErrorUTF8("%s\n",(const char *)str);
      } 
      G_ENDCATCH;
      if (message)
        DjVuPrintErrorUTF8(message);
      c = bs.get();
      while (c != EOF && c != '\r' && c != '\n')
        {
          if (message)
            DjVuPrintErrorUTF8("%c", c);
          c = bs.get();
        }
      if (message)
        DjVuPrintErrorUTF8("'\n");
      bs.skip();
      c = bs.get();
    }
  bs.unget(c);
}

bool
Comments::parse_comment_line(BufferByteStream &bs)
{
  int c = bs.get();
  // Text comments
  if (c == 'T')
    {
      GP<TxtMark> mark = new TxtMark;
      if (! (bs.skip(" \t") && bs.read_pair(mark->x,mark->y) &&
             bs.skip(" \t") && bs.read_pair(mark->dx,mark->dy) &&
             bs.skip(" \t") && bs.read_geometry(mark->r) &&
             bs.skip(" \t") && bs.read_ps_string(mark->s) ) )
        G_THROW("csepdjvu: corrupted file (syntax error in text comment)");
      if (mark->r.isempty())
        G_THROW("csepdjvu: corrupted file (empty rectangle in text comment)");
      textmark(mark);
      return true;
    }
  // Link comments
  if (c == 'L')
    {
      GP<LnkMark> mark = new LnkMark;
      if (! (bs.skip(" \t") && bs.read_geometry(mark->r) &&
             bs.skip(" \t") && bs.read_ps_string(mark->s) ) )
        G_THROW("csepdjvu: corrupted file (syntax error in link comment)");
      if (mark->r.isempty())
        G_THROW("csepdjvu: corrupted file (empty rectangle in link comment)");
      int ymax = h - mark->r.ymin - 1; // reversed in gsdjvu ?
      int ymin = h - mark->r.ymax - 1; // reversed in gsdjvu ?
      mark->r.ymax = ymax;
      mark->r.ymin = ymin;
      links.append(mark);
      return true;
    }
  // Bookmark comments
  if (c == 'B')
    {
      int count; 
      GUTF8String url;
      GUTF8String title;
      if (! (bs.skip(" \t") && bs.read_integer(count) &&
             bs.skip(" \t") && bs.read_ps_string(title) &&
             bs.skip(" \t") && bs.read_ps_string(url) ) )
        G_THROW("csepdjvu: corrupted file (syntax error in outline comment)");
      GP<DjVmNav::DjVuBookMark> b = 
        DjVmNav::DjVuBookMark::create(count, title, url);
      if (b && ! nav)
        nav = DjVmNav::create();
      if (b)
        nav->append(b);
      return true;
    }
  // Page title comment
  if (c == 'P')
    {
      if (pagetitle.length())
        G_THROW("csepdjvu: corrupted file (multiple page title comments)");
      if (! (bs.skip(" \t") && bs.read_ps_string(pagetitle) ))
	G_THROW("csepdjvu: corrupted file (syntax error in title comment)");
      return true;
    }
  // Unrecognized
  bs.unget(c);
  return false;
}

static int 
median3(int *p)
{
  if (p[0] > p[1])
    return MAX(p[1],MIN(p[0],p[2]));
  else
    return MIN(p[1],MAX(p[0],p[2]));
}

static bool 
allspaces(const GUTF8String &s)
{
  bool ok = true;
  for (int i=0; ok && i<(int)s.length(); i++)
    if (s[i] != ' ')
      ok = false;
  return ok;
}

void 
Comments::textmark(GP<TxtMark> mark)
{
  // determine direction
  int dirx = 0;
  int diry = 0;
  int size = 0;
  if (abs(mark->dx) > 8*abs(mark->dy))
    {
      dirx = (mark->dx > 0) ? +1 : -1;
      size = mark->r.height();
    }
  else if (abs(mark->dy) > 8*abs(mark->dy))
    {
      diry = (mark->dy > 0) ? +1 : -1;
      size = mark->r.width();
    }
  // make mark
  mark->inter = 0;
  // flush previous line
  if (lastline.size())
    {
      if (size != lastsize[0])
        {
          lastsize[2] = lastsize[1];
          lastsize[1] = lastsize[0];
          lastsize[0] = size;
        }
      int fontsize = median3(lastsize) + 1;
      int shx = (mark->x - lastx) * 100 / fontsize;
      int shy = (mark->y - lasty) * 100 / fontsize;
      int inter = dirx * shx + diry * shy;
      if ( (dirx || diry) && (dirx == lastdirx) && (diry == lastdiry) &&
           (inter > -150) && (inter < 300) && 
           abs(diry * shx + dirx * shy) < 80 )
        mark->inter = inter;
      else
        textflush();
    }
  if (! lastline.size())
    lastsize[0] = lastsize[1] = lastsize[2] = size;
  lastline.append(mark);
  lastdirx = dirx;
  lastdiry = diry;
  lastx = mark->x + mark->dx;
  lasty = mark->y + mark->dy;
}

void 
Comments::textflush(void)
{
  int size = lastline.size();
  if (size > 0)
    {
      // compute word spacing
      int i = 0;
      GTArray<int> inter(0,size-1);
      for (GPosition p=lastline; p; ++p)
        inter[i++] = lastline[p]->inter;
      inter.sort();
      int wordsep = MAX(10, 2 * inter[(2*size)/3]);
      // compute word list
      GP<TxtMark> word;
      GPList<TxtMark> words;
	  { // extra nesting for windows
        for (GPosition p=lastline; p; ++p)
        {
          TxtMark *mark = lastline[p];
          if (word && mark->inter > wordsep) 
            {
              if (! allspaces(word->s))
                words.append(word);
              word = 0;
            }
          if (! word) 
            {
              word = mark;
            } 
          else 
            {
              word->dx += mark->dx;
              word->dy += mark->dy;
              word->s += mark->s;
              word->r.recthull(word->r, mark->r);
            }
        }
	  }
      if (word)
        {
          if (! allspaces(word->s))
            words.append(word);
          word = 0;
        }
      // create text data
      int size = words.size();
      if (size)
        {
          DjVuTXT::Zone *lzone = 0;
          for (GPosition p = words; p; ++p)
            {
              word = words[p];
              mapper.map(word->r);
              if (! lzone)
                {
                  if (! txt)
                    {
                      txt = DjVuTXT::create();
                      txt->page_zone.ztype = DjVuTXT::PAGE;
                      txt->page_zone.rect = GRect(0,0,w,h);
                      txt->page_zone.text_start = 0;
                      txt->page_zone.text_length = 0;
                    }
                  lzone = txt->page_zone.append_child();
                  lzone->ztype = DjVuTXT::LINE;
                  lzone->text_start = txt->textUTF8.length();
                  lzone->text_length = 0;
                }
              if (detail >= DjVuTXT::WORD)
                {
                  DjVuTXT::Zone *wzone = lzone->append_child();
                  wzone->ztype = DjVuTXT::WORD;
                  wzone->text_start = txt->textUTF8.length();
                  txt->textUTF8 += word->s;
                  wzone->text_length = 
                    txt->textUTF8.length() - wzone->text_start;
                  wzone->rect = word->r;
                  lzone->rect.recthull(lzone->rect, word->r);
                }
              else
                {
                  if (lzone->text_length > 0) txt->textUTF8 += " ";
                  txt->textUTF8 += word->s;
                  lzone->text_length = 
                    txt->textUTF8.length() - lzone->text_start;
                  lzone->rect.recthull(lzone->rect, word->r);
                }
            }
        }
    }
  lastline.empty();
}

static int 
bytestream_fputs(miniexp_io_t *io, const char *s)
{
  ByteStream *outbs = (ByteStream*)io->data[0];
  return (outbs) ? outbs->write((const void*)s, strlen(s)) : -1;
}

void 
Comments::make_chunks(IFFByteStream &iff)
{
  // Write text chunk
  textflush();
  if (txt)
    {
      txt->normalize_text();
      iff.put_chunk("TXTz");
      {
        GP<ByteStream> bsb = BSByteStream::create(iff.get_bytestream(), 50);
        txt->encode(bsb);
      }
      iff.close_chunk();
    }
  // Create annotation chunk
  if (links.size() > 0)
    {
      iff.put_chunk("ANTz");
      {
        GP<ByteStream> bsb = BSByteStream::create(iff.get_bytestream(), 50);
        miniexp_io_t io;
        miniexp_io_init(&io);
        io.fputs = bytestream_fputs;
        io.data[0] = (void*)(ByteStream*)bsb;
        minivar_t exor = miniexp_cons(miniexp_symbol("xor"),miniexp_nil);
        minivar_t zstr = miniexp_string("");
        for (GPosition p = links; p; ++p)
          {
            GP<LnkMark> mark = links[p];
            minivar_t url = miniexp_string((const char*)(mark->s));
            minivar_t expr = miniexp_cons(exor, miniexp_nil);
            minivar_t area;
            area = miniexp_cons(miniexp_number(mark->r.height()), area);
            area = miniexp_cons(miniexp_number(mark->r.width()), area);
            area = miniexp_cons(miniexp_number(mark->r.ymin), area);
            area = miniexp_cons(miniexp_number(mark->r.xmin), area);
            area = miniexp_cons(miniexp_symbol("rect"),area);
            expr = miniexp_cons(area, expr);
            expr = miniexp_cons(zstr, expr);
            expr = miniexp_cons(url, expr);
            expr = miniexp_cons(miniexp_symbol("maparea"), expr);
            miniexp_pprint_r(&io, expr, 72);
          }
      }
      iff.close_chunk();
    }
}

GP<DjVmNav>
Comments::get_djvm_nav()
{
  if (nav && nav->getBookMarkCount() && nav->isValidBookmark())
    return nav;
  if (nav)
    DjVuPrintErrorUTF8("%s", "csepdjvu: corrupted outline comments.\n");
  return 0;
}

GUTF8String
Comments::get_pagetitle()
{
  return pagetitle;
}


// --------------------------------------------------
// MAIN COMPRESSION ROUTINE
// --------------------------------------------------


// -- Compresses one page:
//    - bytestream bs contains the input separated file.
//    - bytestream obs will receive the output djvu file.
void 
csepdjvu_page(BufferByteStream &bs, 
              GP<ByteStream> obs, 
              GP<DjVmNav> &nav,
	      GUTF8String &pagetitle,
              const csepdjvuopts &opts)
{
  // Read rle data from separation file
  CRLEImage rimg(bs);
  int w = rimg.width;
  int h = rimg.height;
  if (opts.verbose > 1)
    DjVuFormatErrorUTF8( "%s\t%d\t%d\t%d\t%d",
                     ERR_MSG("csepdjvu.summary"), 
                     w, h, rimg.pal->size(), rimg.runs.size());
  
  // Perform Color Connected Component Analysis
  rimg.make_ccids_by_analysis();                  // Obtain ccids
  rimg.make_ccs_from_ccids();                     // Compute cc descriptors
  if (opts.verbose > 1)
    DjVuFormatErrorUTF8("%s\t%d", ERR_MSG("csepdjvu.analyzed"), 
                        rimg.ccs.size());
  
  // Post-process Color Connected Components
  int largesize = MIN(500, MAX(64, opts.dpi));
  int smallsize = MAX(2, opts.dpi/150);
  rimg.merge_and_split_ccs(smallsize,largesize);  // Eliminates gross ccs
  if (opts.verbose > 1)
    DjVuFormatErrorUTF8( "%s\t%d",
                     ERR_MSG("csepdjvu.merge_split"), 
                     rimg.ccs.size());
  rimg.sort_in_reading_order();                   // Sort cc descriptors
  
  // Create JB2Image and fill colordata
  GP<JB2Image> gjimg=JB2Image::create(); 
  JB2Image &jimg=*gjimg;
  jimg.set_dimension(w, h);
  int nccs = rimg.ccs.size();
  for (int ccid=0; ccid<nccs; ccid++)
    {
      JB2Shape shape;
      JB2Blit  blit;
      shape.parent = -1;
      shape.userdata = 0;
      if (ccid >= rimg.nregularccs)
        shape.userdata |= JB2SHAPE_SPECIAL;
      shape.bits = rimg.get_bitmap_for_cc(ccid);
      shape.bits->compress();
      CC& cc = rimg.ccs[ccid];
      blit.shapeno = jimg.add_shape(shape);
      blit.left = cc.bb.xmin;
      blit.bottom = cc.bb.ymin;
      int blitno = jimg.add_blit(blit);
      rimg.pal->colordata.touch(blitno);
      rimg.pal->colordata[blitno] = cc.color;
    }
  
  // Organize JB2Image
  tune_jb2image_lossless(&jimg);
  if (opts.verbose> 1)
    {
      int nshape=0, nrefine=0;
      for (int i=0; i<jimg.get_shape_count(); i++) 
        {
          if (!jimg.get_shape(i).bits) continue;
          if (jimg.get_shape(i).parent >= 0) nrefine++; 
          nshape++; 
        }
      DjVuFormatErrorUTF8( "%s\t%d\t%d",
                       ERR_MSG("csepdjvu.cross_code"), 
                       nshape, nrefine);
    }
  
  // Obtain background image
  int bgred;
  GP<GPixmap> bgpix = read_background(bs, w, h, bgred);
  if (opts.verbose > 1 && bgpix)
    DjVuFormatErrorUTF8( "%s\t%d", ERR_MSG("csepdjvu.reduction"), bgred);

  // Process comments
  Comments coms(w, h, opts);
  coms.process_comments(bs, opts.verbose);
  
  // Compute flags for simplifying output format
  bool white_background = (bgpix ? false : true);
  bool gray_background = white_background;
  if (rimg.bg_flags == 'g' || rimg.bg_flags=='b')
    gray_background = true;
  bool bitonal = false;
  if (white_background && rimg.pal->size() == 1) 
    {
      GPixel fgcolor;
      rimg.pal->index_to_color(0, fgcolor);
      if (fgcolor == GPixel::BLACK)
        bitonal = true;
    }
  if (opts.verbose > 1)
    {
      if (bitonal)
        DjVuWriteError( ERR_MSG("csepdjvu.bilevel") );
      else if (white_background) 
        DjVuWriteError( ERR_MSG("csepdjvu.white_bg") );
      else if (gray_background) 
        DjVuWriteError( ERR_MSG("csepdjvu.gray_bg") );
      else 
        DjVuWriteError( ERR_MSG("csepdjvu.color") );
    }
  
  // Create background image
  GP<IW44Image> iw;
  if (! white_background)
    {
      /* Perform masked compression */
      GP<GBitmap> mask = jimg.get_bitmap(bgred);
      mask->binarize_grays(bgred*bgred-1);
      IW44Image::CRCBMode mode = IW44Image::CRCBnormal;
      if (gray_background) mode = IW44Image::CRCBnone;
      iw = IW44Image::create_encode(*bgpix, mask, mode);
      bgpix = 0;
    } 
  else if (! bitonal) 
    {
      /* Compute white background */
      GPixel bgcolor = GPixel::WHITE;
      GP<GPixmap> inputsub=GPixmap::create((h+11)/12, (w+11)/12, &bgcolor);
      iw = IW44Image::create_encode(*inputsub, 0, IW44Image::CRCBnone);
    }
  
  // Assemble DJVU file
  GP<IFFByteStream> giff=IFFByteStream::create(obs);
  IFFByteStream &iff=*giff;
  // -- main composite chunk
  iff.put_chunk("FORM:DJVU", 1);
  // -- ``INFO'' chunk
  iff.put_chunk("INFO");
  GP<DjVuInfo> ginfo=DjVuInfo::create();
  DjVuInfo &info=*ginfo;
  info.height = h;
  info.width = w;
  info.dpi = opts.dpi;
  info.encode(*iff.get_bytestream());
  iff.close_chunk();
  // -- ``Sjbz'' chunk
  iff.put_chunk("Sjbz");
  jimg.encode(iff.get_bytestream());
  iff.close_chunk();
  // -- Color stuff
  if (! bitonal) 
    {
      // -- ``FGbz'' chunk
      iff.put_chunk("FGbz");
      rimg.pal->encode(iff.get_bytestream());
      iff.close_chunk();
      // -- ``BG44'' chunk
      IWEncoderParms iwparms;
      if (white_background) 
        {
          iff.put_chunk("BG44");
          iwparms.slices = 97;
          iw->encode_chunk(iff.get_bytestream(), iwparms);
          iff.close_chunk();
        }
      else
        {
          const unsigned char *slice = opts.slice;
          while ((iwparms.slices = *slice++))
            {
              iff.put_chunk("BG44");
              iw->encode_chunk(iff.get_bytestream(), iwparms);
              iff.close_chunk();
            }
        }
    }
  // -- terminate main composite chunk
  coms.make_chunks(iff);
  iff.close_chunk();
  // -- returns page title and outline
  pagetitle = coms.get_pagetitle();
  if (! nav) 
    nav = coms.get_djvm_nav();
}  

// -- Checks whether there is another page in the same file
bool
check_for_another_page(BufferByteStream &bs, const csepdjvuopts &opts)
{
  // Skip null bytes (why?)
  int lookahead;
  while (! (lookahead = bs.get())) { }
  bs.unget(lookahead);
  // Check next header
  if (lookahead == 'R') 
    return true;
  if (lookahead != EOF) 
    DjVuPrintErrorUTF8("%s","csepdjvu: found corrupted data\n");
  return false;
}


// -- Prints usage message
void
usage()
{
  const char *msg = 
#ifdef DJVULIBRE_VERSION
    "CSEPDJVU --- DjVuLibre-" DJVULIBRE_VERSION "\n"
#endif
    "DjVu encoder working with \"separated\" files\n\n"
    "Usage: csepdjvu <...options_or_separatedfiles...> <outputdjvufile>\n"
    "Options are:\n"
    "   -v, -vv    Select verbosity level.\n"
    "   -d <n>     Set resolution to <n> dpi (default: 300).\n"
    "   -t         Restricts text information to lines only.\n"
    "   -q <spec>  Select quality for background (default: 72+11+10+10);\n"
    "              see option -slice in program c44 for more information.\n"
    "Each separated files contain one or more pages\n"
    "Each page is composed of:\n"
    " (1) a B&W-RLE or Color-RLE image representing the foreground,\n"
    " (2) an optional PPM image representing the background layer.\n";
  DjVuPrintErrorUTF8(msg);
  exit(10);
}


// -- Parsing quality spec (borrowed from c44)
void 
parse_slice(const char *q, csepdjvuopts &opts)
{
  int count = 0;
  int lastx = 0;
  while (*q)
    {
      char *ptr; 
      int x = strtol(q, &ptr, 10);
      if (ptr == q)
        G_THROW("csepdjvu: "
                "illegal quality specification (number expected)");
      if (lastx>0 && q[-1]=='+')
        x += lastx;
      if (x<1 || x>1000 || x<lastx)
        G_THROW("csepdjvu: "
                "illegal quality specification (number out of range)");
      lastx = x;
      if (*ptr && *ptr!='+' && *ptr!=',')
        G_THROW("csepdjvu: "
                "illegal quality specification (comma expected)");        
      q = (*ptr ? ptr+1 : ptr);
      if (count+1 >= (int)(sizeof(opts.slice)/sizeof(opts.slice[0])))
        G_THROW("csepdjvu: "
                "illegal quality specification (too many chunks)");
      opts.slice[count++] = x;
      opts.slice[count] = 0;
    }
  if (count < 1)
    G_THROW("csepdjvu: "
            "illegal quality specification (no chunks)");
}


// -- Main routine
int 
main(int argc, const char **argv)
{
  DJVU_LOCALE;
  GArray<GUTF8String> dargv(0,argc-1);
  for(int i=0;i<argc;++i)
    dargv[i]=GNativeString(argv[i]);
  G_TRY
    {
      GP<DjVmDoc> gdoc=DjVmDoc::create();
      GP<DjVmNav> gnav;
      GUTF8String pagetitle;
      DjVmDoc &doc=*gdoc;
      GURL outputurl;
      GP<ByteStream> goutputpage=ByteStream::create();
      csepdjvuopts opts;
      int pageno = 0;
      // Read outputurl name
      if (argc < 3) usage();
      outputurl = GURL::Filename::UTF8(dargv[--argc]);
      // Process arguments
      for (int i=1; i<argc; i++)
        {
          GUTF8String arg = dargv[i];
          if (arg == "-v")
            opts.verbose = 1;
          else if (arg == "-vv")
            opts.verbose = 2;
          else if (arg == "-t")
            opts.text = DjVuTXT::LINE;
          else if (arg == "-d" && i+1<argc)
            {
              // Specify resolution
              char *end;
              opts.dpi = strtol(dargv[++i], &end, 10);
              if (*end || opts.dpi<25 || opts.dpi>6000)
                usage();
            }
          else if (arg == "-q" && i+1 < argc)
            {
              // Specify background quality
              parse_slice(dargv[++i], opts);
            }
          else if (arg == "-l" || arg == "-t" || arg == "-h")
            {
              DjVuPrintErrorUTF8("csepdjvu: option %s not yet supported\n",
                                 (const char *)arg );
            }
          else if ((arg == "-j" || arg == "-p") && i+1 < argc)
            {
              i += 1;
              DjVuPrintErrorUTF8("csepdjvu: option %s not yet supported\n",
                                 (const char *)arg );
            }
          else 
            {
              // Process separation file
              GP<ByteStream> fbs = 
                ByteStream::create(GURL::Filename::UTF8(arg),"rb");
              BufferByteStream ibs(*fbs);
              do {
                char pagename[16];
                sprintf(pagename, "p%04d.djvu", ++pageno);
                if (opts.verbose > 1)
                  DjVuPrintErrorUTF8("%s","--------------------\n");
                // Compress page 
                goutputpage=ByteStream::create();
                ByteStream &outputpage=*goutputpage;
                csepdjvu_page(ibs, goutputpage, gnav, pagetitle, opts);
                if (opts.verbose) {
                  DjVuPrintErrorUTF8("csepdjvu: %d bytes for page %d",
                                     outputpage.size(), pageno);
                  if (arg == "-")
                    DjVuPrintErrorUTF8("%s"," (from stdin)\n");
                  else
                    DjVuPrintErrorUTF8(" (from file '%s')\n", 
                                       (const char*)arg);
                }
                // Insert page into document
                outputpage.seek(0);
                doc.insert_file(outputpage, DjVmDir::File::PAGE, 
                                pagename, pagename, pagetitle);
              } while (check_for_another_page(ibs, opts));
            }
        } 
      // Save file
      if (pageno == 1 && ! gnav && ! pagetitle) 
        {
          ByteStream &outputpage=*goutputpage;
          // Save as a single page 
          outputpage.seek(0);
          ByteStream::create(outputurl,"wb")->copy(outputpage);
        }
      else if (pageno >= 1)
        {
          // Save as a bundled file
          doc.set_djvm_nav(gnav);
          doc.write(ByteStream::create(outputurl,"wb"));
        }
      else 
        usage();
    }
  G_CATCH(ex)
    {
      ex.perror();
      exit(1);
    }
  G_ENDCATCH;
  return 0;
}