File: ljjetready.cpp

package info (click to toggle)
hplip 3.12.6-3.1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 50,996 kB
  • sloc: ansic: 65,346; cpp: 61,325; python: 58,704; sh: 12,084; perl: 4,659; makefile: 768
file content (1862 lines) | stat: -rw-r--r-- 58,567 bytes parent folder | download | duplicates (3)
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
/*****************************************************************************\
  ljjetready.cpp : Implimentation for the LJJetReady class

  Copyright (c) 1996 - 2008, Hewlett-Packard Co.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of Hewlett-Packard nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/


#ifdef APDK_LJJETREADY
/*
 *  Not sure need HAVE_PROTOTYPES here, a user reported a compiler error under Tru64. The error
 *  was for the assignment jerr.error_exit = Gjpeg_error; This seems to be due to JMETHOD
 *  definition in jmorecfg.h
 */

//#define HAVE_PROTOTYPES

#include "header.h"
#include "io_defs.h"
#include "ljjetready.h"
#include "printerproxy.h"
#include "resources.h"

#ifdef HAVE_LIBDL
#include <dlfcn.h>
#endif

extern "C"
{
int (*HPLJJRCompress) (BYTE       *pbOutBuffer, 
                       uint32_t   *outlen, 
                       BYTE       *inmem, 
                       const uint32_t iLogicalImageWidth,
                       const uint32_t iLogicalImageHeight);
}

APDK_BEGIN_NAMESPACE

#ifdef HAVE_LIBDL
extern void *LoadPlugin (const char *szPluginName);
#endif

extern MediaSize PaperToMediaSize(PAPER_SIZE psize);

#define MOJAVE_STRIP_HEIGHT	128 // Mojave strip height should always be 128 for compression sake

#define MOJAVE_RESOLUTION	600 // Mojave supports only one resolution- 600 DPI	

BYTE JrBeginSessionSeq[]				= {0xC0, 0x00, 0xF8, 0x86, 0xC0, 0x03, 0xF8, 0x8F, 0xD1, 0x58, 
										    0x02, 0x58, 0x02, 0xF8, 0x89, 0x41};
BYTE JrFeedOrientationSeq[]				= {0xC0, 0x00  , 0xF8, 0x28 };
//											   |fd ori enum|       |ori cmd|											
BYTE JrPaperSizeSeq[]					= {0xC0,  0x00      ,0xF8, 0x25};
//											   |pap siz enum|     |pap sz cmd|
BYTE JrMedSourceSeq[]					= {0xC0,  0x00      ,0xF8, 0x26        };
//											   |Med src enum|     |Med src cmd|
BYTE JrMedDestinationSeq[]				= {0xC0,  0x00        ,0xF8 , 0x24        };
//											   |Med Dest enum|      |Med src cmd|
BYTE JrBeginPageSeq[]				    = {0x43, 0xD3, 0x64, 0x00, 0x64, 0x00, 0xF8, 0x2A, 0x75, 0xD3, 
										    0x00, 0x00, 0x00, 0x00, 0xF8, 0x4C, 0x6B};
BYTE JrBeginImageSeq[]				    = {0xC2, 0x00, 0x40, 0x70, 0x68, 0xF8, 0x91, 0xC1};

BYTE JrVU_ver_TagSeq[]					= {0xC2, 0x00, 0x00, 0x04, 0x00 , 0xF8, 0x95			  };
//												|endian alignd         |  |Jr_ver_ tag|
BYTE JrDataLengthSeq[]					= {0xC2, 0x86, 0x0A, 0x00, 0x00, 0xF8, 0x92	  };
//																			  | VU data length|
BYTE JrVenUniqSeq[]						= {0x46};
BYTE JrVUExtn_3Seq[]					= {0xC2, 0x02, 0x40, 0x70, 0x68			,0xF8, 0x91	};
//												|endian alignd Jr rd img tag|	  |VU extensn|
BYTE JrEndPageSeq[]						=  {0x44};
BYTE JrEndSessionSeq[]					=  {0x42};
// PJL level commands..

//**JETLIB ENTRIES
const char ccpPJLStartJob[]				= {ESC, '%', '-','1','2','3','4','5','X' };;
const char ccpPJLExitSeq[] 				= {ESC, '%', '-','1','2','3','4','5','X', '@','P','J','L',' ','E','O','J', LF, ESC,'%', '-','1','2','3','4','5','X'};
const char ccpPJLSetRes[]				= {'@','P','J','L',' ','S','E','T',' ','R','E','S','O','L','U','T','I','O','N','=','6','0','0', LF};
const char ccpPJLUTF8[]					= {'@','P','J','L',' ','S','E','T',' ','S','T','R','I','N','G','C','O','D','E','S','E','T','=','U','T','F','8', LF};
const char ccpPJLSetPlanes[]			= {'@','P','J','L',' ','S','E','T',' ','P','L','A','N','E','S','I','N','U','S','E','=','1',LF}; //Sent only for GrayScale Printing
const char ccpPCLEnterXL[]				= {'@','P','J','L',' ','E','N','T','E','R',' ','L','A','N','G','U','A','G','E','=','P','C','L','X','L', LF};
const char ccpPJLSetTO[]				= {'@','P','J','L',' ','S','E','T',' ','T','I','M','E','O','U','T','=','9','0', LF};
const char ccpPJLSetCopyCount[]			= {'@','P','J','L',' ','S','E','T',' ','C','O','P','I','E','S','=','1',LF};
const char ccpUEL[]                     = {ESC, '%', '-','1','2','3','4','5','X' };
const char ccpPJLComment[]				= {')',' ','H','P','-','P','C','L',' ','X','L',';','3',';','0',';','C','o','m','m','e','n','t',',',' ','P','C','L','-','X','L',' ','J','e','t','R','e','a','d','y',' ','g','e','n','e','r','a','t','o','r', LF};
//**END JETLIB ENTRIES


LJJetReady::LJJetReady (SystemServices* pSS, int numfonts, BOOL proto)
    : Printer(pSS, numfonts, proto)
{

    if ((!proto) && (IOMode.bDevID))
    {
        constructor_error = VerifyPenInfo();
        CERRCHECK;
    }
    else ePen = BOTH_PENS;    // matches default mode
	
    pMode[GRAYMODE_INDEX]      = new LJJetReadyGrayMode ();
    pMode[DEFAULTMODE_INDEX]   = new LJJetReadyNormalMode ();
    ModeCount = 2;


    CMYMap = NULL;
    m_bJobStarted = FALSE;
#ifdef  APDK_AUTODUPLEX
    m_bRotateBackPage = FALSE;  // Lasers don't require back side image to be rotated
#endif
    m_pCompressor = NULL;
    m_iYPos = 0;

	m_bStartPageNotSent = TRUE;

    HPLJJRCompress = NULL;

#ifdef HAVE_LIBDL
    m_hHPLibHandle = LoadPlugin ("lj.so");
    if (m_hHPLibHandle)
    {
        dlerror ();
        *(void **) (&HPLJJRCompress) = dlsym (m_hHPLibHandle, "HPJetReadyCompress");
    }
    if (HPLJJRCompress)
    {
        pMode[ModeCount] = new LJJetReadyBestColorMode ();
        ModeCount++;
        pMode[ModeCount] = new LJJetReadyBestGrayMode ();
        ModeCount++;
    }
#endif
    m_eCompressMode = COMPRESS_MODE_JPEG;

    DBG1("LJJetReady created\n");
}

LJJetReady::~LJJetReady ()
{
    DISPLAY_STATUS  eDispStatus;

#ifdef HAVE_LIBDL
    if (m_hHPLibHandle)
    {
        dlclose (m_hHPLibHandle);
    }
#endif

    if (IOMode.bStatus && m_bJobStarted)
    {
        for (int i = 0; i < 5; i++)
        {
            pSS->BusyWait (2000);
            eDispStatus = ParseError (0);
            if (eDispStatus == DISPLAY_PRINTING_COMPLETE)
            {
                pSS->DisplayPrinterStatus (eDispStatus);
                break;
            }
        }
    }
}

LJJetReadyNormalMode::LJJetReadyNormalMode ()
: PrintMode (NULL)
{
    ResolutionX[0] = 600;
    ResolutionY[0] = 600;
    BaseResX = BaseResY = 600;

    bFontCapable = FALSE;

#if defined(APDK_VIP_COLORFILTERING)
    Config.bErnie = FALSE;
#endif
    Config.bColorImage = FALSE;
	theQuality = qualityNormal;

    pmQuality = QUALITY_NORMAL;
	Config.bColorImage = FALSE;
#ifdef APDK_AUTODUPLEX
    bDuplexCapable = TRUE;
#endif
}

LJJetReadyBestColorMode::LJJetReadyBestColorMode () : PrintMode (NULL)
{
    ResolutionX[0] =
    ResolutionY[0] =
    BaseResX =
    BaseResY = 600;

    bFontCapable = FALSE;

#if defined(APDK_VIP_COLORFILTERING)
    Config.bErnie = FALSE; // Raghu
#endif

    Config.bColorImage = FALSE;
    theQuality = qualityPresentation;
    pmQuality = QUALITY_BEST;
#ifdef APDK_AUTODUPLEX
    bDuplexCapable = TRUE;
#endif
}

LJJetReadyGrayMode::LJJetReadyGrayMode () : PrintMode (NULL)
{
    ResolutionX[0] =
    ResolutionY[0] =
    BaseResX =
    BaseResY = 600;

    bFontCapable = FALSE;

#if defined(APDK_VIP_COLORFILTERING)
    Config.bErnie = FALSE; // Raghu
#endif

    Config.bColorImage = FALSE;
    pmColor = GREY_K;
#ifdef APDK_AUTODUPLEX
    bDuplexCapable = TRUE;
#endif
}

LJJetReadyBestGrayMode::LJJetReadyBestGrayMode () : PrintMode (NULL)
{
    ResolutionX[0] =
    ResolutionY[0] =
    BaseResX =
    BaseResY = 600;

    bFontCapable = FALSE;

#if defined(APDK_VIP_COLORFILTERING)
    Config.bErnie = FALSE; // Raghu
#endif

    Config.bColorImage = FALSE;
    pmColor = GREY_K;
    pmQuality = QUALITY_BEST;
    theQuality = qualityPresentation;
#ifdef APDK_AUTODUPLEX
    bDuplexCapable = TRUE;
#endif
}

HeaderLJJetReady::HeaderLJJetReady (Printer* p, PrintContext* pc)
    : Header(p,pc)
{ 
	SetLastBand(FALSE);
}

DRIVER_ERROR HeaderLJJetReady::Send ()
{
    StartSend ();

    return NO_ERROR;
}

DRIVER_ERROR HeaderLJJetReady::StartSend ()
{
    DRIVER_ERROR err;
    char    szScratchStr[64];

    err = thePrinter->Send ((const BYTE*)ccpPJLStartJob,	sizeof(ccpPJLStartJob));
    ERRCHECK;

    //Send the UTF8 encoding command
    err = thePrinter->Send ((const BYTE*)ccpPJLUTF8, sizeof(ccpPJLUTF8));
    ERRCHECK;

    // If it is Grayscale printjob, send the PJL command indicating the same
    COLORMODE       eC = COLOR;
    MEDIATYPE       eM;
    QUALITY_MODE    eQ;
    BOOL            bD;

    ((LJJetReady *)thePrinter)->bGrey_K = FALSE;
    if ((thePrintContext->GetPrintModeSettings (eQ, eM, eC, bD)) == NO_ERROR &&
        eC == GREY_K)
    {
        ((LJJetReady *)thePrinter)->bGrey_K = TRUE;
        err = thePrinter->Send ((const BYTE*)ccpPJLSetPlanes,sizeof(ccpPJLSetPlanes));
        ERRCHECK;
    }
	
    //Send the Number of copies command
//    err = thePrinter->Send ((const BYTE*)ccpPJLSetCopyCount,sizeof(ccpPJLSetCopyCount));
    sprintf (szScratchStr, "@PJL SET COPIES=%d\015\012", thePrintContext->GetCopyCount ());
    err = thePrinter->Send ((const BYTE *) szScratchStr, strlen (szScratchStr));
    ERRCHECK;

    // Send the Duplex command
    strcpy (szScratchStr, "@PJL SET DUPLEX=OFF\015\012");
#ifdef APDK_AUTODUPLEX
    DUPLEXMODE  dupmode = thePrintContext->QueryDuplexMode ();
    if (dupmode != DUPLEXMODE_NONE)
    {
        strcpy (szScratchStr, "@PJL SET DUPLEX=ON\015\012@PJL SET BINDING=");
        if (dupmode == DUPLEXMODE_BOOK)
            strcat (szScratchStr, "LONGEDGE\015\012");
        else
            strcat (szScratchStr, "SHORTEDGE\015\012");
    }
#endif
    err = thePrinter->Send ((const BYTE *) szScratchStr, strlen (szScratchStr));
    ERRCHECK;

    // Send the Binding command
    strcpy (szScratchStr, "@PJL SET BINDING=LONGEDGE\015\012");
#ifdef APDK_AUTODUPLEX
    if (thePrintContext->QueryDuplexMode () == DUPLEXMODE_TABLET)
    {
        strcpy (szScratchStr, "@PJL SET BINDING=SHORTEDGE\015\012");
    }
#endif
    err = thePrinter->Send ((const BYTE *) szScratchStr, strlen (szScratchStr));
    ERRCHECK;

    //Set the resolution to 600
    err = thePrinter->Send ((const BYTE*)ccpPJLSetRes,sizeof(ccpPJLSetRes));
    ERRCHECK;

    //Send the time out command
    err = thePrinter->Send ((const BYTE*)ccpPJLSetTO,sizeof(ccpPJLSetTO));
    ERRCHECK;

    //send the mojave PCL_XL_ENTER_LANG command
    err = thePrinter->Send ((const BYTE*)ccpPCLEnterXL,sizeof(ccpPCLEnterXL));
    ERRCHECK;

    //send the comment string
    err = thePrinter->Send ((const BYTE*)ccpPJLComment,sizeof(ccpPJLComment));
    ERRCHECK;

    err = thePrinter->Send ((const BYTE*)JrBeginSessionSeq,sizeof(JrBeginSessionSeq));
    ERRCHECK;

    return err;
}

int HeaderLJJetReady::JRPaperToMediaSize(PAPER_SIZE psize)
{
    switch(psize)
    {
        case LETTER:            return 0;
        case LEGAL:             return 1;
        case A4:                return 2;
        case B4:                return 10;
        case B5:                return 11;
        case OUFUKU:            return 14;
        case HAGAKI:            return 14;
        case A6:                return 17;
#ifdef APDK_EXTENDED_MEDIASIZE
        case A3:                return 5;
        case A5:                return 16;
        case LEDGER:            return 4;
        case EXECUTIVE:         return 3;
        case CUSTOM_SIZE:       return 96;
        case ENVELOPE_NO_10:    return 6;
        case ENVELOPE_A2:       return 6;
        case ENVELOPE_C6:       return 8;
        case ENVELOPE_DL:       return 9;
#endif
        default:                return 0;
    }
}

DRIVER_ERROR HeaderLJJetReady::StartPage ()
{
    DRIVER_ERROR err;
    BYTE    szCustomSize[64];

	/* Orienatation: is JrFeedOrientationSeq[1]. Can take the following values:
		Portrait				: 0x00
		Landscape:				: 0x01
		Reversed    Portrait	: 0x02
		Reversed    Landscape	: 0x03
		Image		Orientataion: 0x04
		
		Mojave  supports only one feed orientation: Portrait
	*/
	err = thePrinter->Send ((const BYTE*)JrFeedOrientationSeq,sizeof(JrFeedOrientationSeq));
	ERRCHECK;

	// Paper Size
	PAPER_SIZE ps = thePrintContext->GetPaperSize ();
	int msizeCode;
    msizeCode = JRPaperToMediaSize(ps);	
	//Put the papersize into the JrPaperSizeSeq[]
	JrPaperSizeSeq[1] = (BYTE) msizeCode; 
#ifdef APDK_EXTENDED_MEDIASIZE
	if(msizeCode == 96) //Custom paper size
	{
        BYTE    szScratchStr[] = {"\xF8\x2F\xC0\x00\xF8\x30"};
        union
        {
            float       fValue;
            uint32_t    uiValue;
        } LJJRUnion;
        uint32_t    uiXsize;
        uint32_t    uiYsize;
        int         k = 0;
        LJJRUnion.fValue = (float) thePrintContext->PhysicalPageSizeX ();
        uiXsize = LJJRUnion.uiValue;
        LJJRUnion.fValue = (float) thePrintContext->PhysicalPageSizeY ();
        uiYsize = LJJRUnion.uiValue;
        szCustomSize[k++] = 0xD5;
        szCustomSize[k++] = (BYTE) (uiXsize & 0x000000FF);
        szCustomSize[k++] = (BYTE) ((uiXsize & 0x0000FF00) >> 8);
        szCustomSize[k++] = (BYTE) ((uiXsize & 0x00FF0000) >> 16);
        szCustomSize[k++] = (BYTE) ((uiXsize & 0xFF000000) >> 24);
        szCustomSize[k++] = (BYTE) (uiYsize & 0x000000FF);
        szCustomSize[k++] = (BYTE) ((uiYsize & 0x0000FF00) >> 8);
        szCustomSize[k++] = (BYTE) ((uiYsize & 0x00FF0000) >> 16);
        szCustomSize[k++] = (BYTE) ((uiYsize & 0xFF000000) >> 24);
        err = thePrinter->Send ((const BYTE *) szCustomSize, k);

        err = thePrinter->Send (szScratchStr, sizeof (szScratchStr));
		ERRCHECK;
	}	
	else
#endif
	{
		err = thePrinter->Send ((const BYTE*)JrPaperSizeSeq,sizeof(JrPaperSizeSeq));
		ERRCHECK;
	}

	// If it is Grayscale printjob, send the PJL command indicating the same
    COLORMODE       eC = COLOR;
    MEDIATYPE       eM;
    QUALITY_MODE    eQ;
    BOOL            bD;

    thePrintContext->GetPrintModeSettings (eQ, eM, eC, bD);

/*
 *  Send Printable Area. This is necessary in case source width is less than printable width.
 *
 * 
 *  width is imagewidth - multiple of 96
 *  height is physical page height - margins for 8x5 x 11 - 6600 - 200 = 6400
 */

    BYTE    szPrintableArea[] = {"\xD1\x00\x00\x00\x00\xF8\x74"};
    int     iWidth = (int) thePrintContext->InputPixelsPerRow ();
    int     iPageHeight;

/*
 *  There is a firmware bug in Mojave and Lakota that causes image replicatin across the
 *  page if width is less than the printable width in grayscale. So, set width to printable
 *  width for grayscale.
 */

    if (eC == GREY_K)
    {
        iWidth = (int) (thePrintContext->PrintableWidth () *
                        thePrintContext->EffectiveResolutionX ());
    }

/*
 *  The minimum printable width is 1600 pixels (3 inch * 600 - 200 for margins)
 */

    if (iWidth < 1600)
    {
        iWidth = 1600;
    }

/*
 *  Further, source width must be a multiple of 32.
 */


    iWidth = ((iWidth + 31) / 32) * 32;

    iPageHeight = (int) (thePrintContext->PrintableHeight () *
                         thePrintContext->EffectiveResolutionY ());
    iPageHeight = ((iPageHeight + (MOJAVE_STRIP_HEIGHT - 1)) / MOJAVE_STRIP_HEIGHT) * MOJAVE_STRIP_HEIGHT;

    szPrintableArea[1] = iWidth & 0xFF;
    szPrintableArea[2] = (iWidth >> 8) & 0xFF;
    szPrintableArea[3] = iPageHeight & 0xFF;
    szPrintableArea[4] = (iPageHeight >> 8) & 0xFF;
    err = thePrinter->Send (szPrintableArea, 7);
    ERRCHECK;

	//MapPCLMediaTypeToString (eM);  // Optional.  No need to send

	err = thePrinter->Send ((const BYTE *)JrBeginPageSeq, sizeof(JrBeginPageSeq));
	ERRCHECK;

	// The colorspace command has to be sent to intimate the printer to switch to color/monochrome 
	// depending upon the data that we are about to send.
	// This command must be sent after SetCursor and before beginimage command
	if (eC == GREY_K )
	{
		// indicates switch to monochrome mode 
		err = thePrinter->Send ((const BYTE *) "\xC0\x07\xF8\x03\x6A", 5);
		ERRCHECK;
	}
	else
	{
		// indicates switch Color mode
		err = thePrinter->Send ((const BYTE *) "\xC0\x06\xF8\x03\x6A", 5);
		ERRCHECK;
	}

	bLastBand = FALSE;

    return err;
}

DRIVER_ERROR HeaderLJJetReady::EndPage ()
{
    DRIVER_ERROR err;

	err = thePrinter->Send ((const BYTE*)JrVUExtn_3Seq,sizeof(JrVUExtn_3Seq));
	ERRCHECK;
	err = thePrinter->Send ((const BYTE*)JrVU_ver_TagSeq,sizeof(JrVU_ver_TagSeq));
	ERRCHECK;
	err = thePrinter->Send ((const BYTE*)JrVenUniqSeq,sizeof(JrVenUniqSeq));
	ERRCHECK;
	err = thePrinter->Send ((const BYTE*)JrEndPageSeq,sizeof(JrEndPageSeq));
	ERRCHECK;

    return err;
}


//-----------------------------------------------------------------------------
// Function:    CDJPcl::MapPCLMediaTypeToString()
//
// Description: Sends the command for mediatype.
// Input:       None
// Modifies:    None
//
// Precond:     None
//              
//                 
// Postcond:    The mediatype commands are sent
//
// Returns:     None
//
//-----------------------------------------------------------------------------
DRIVER_ERROR HeaderLJJetReady::MapPCLMediaTypeToString (MEDIATYPE eM)
{
    DRIVER_ERROR    err;
    BYTE            szPlain[] = {"\xC8\xC1\x05\x00Plain\xF8\x27"};
    BYTE            szPhoto[] = {"\xC8\xC1\x05\x00Gloss\xF8\x27"};

    switch (eM)
    {
        case (MEDIA_PLAIN):
            err = thePrinter->Send (szPlain, sizeof (szPlain));
            ERRCHECK;
            break;

        case (MEDIA_PHOTO):
            err = thePrinter->Send (szPhoto, sizeof (szPhoto));
            ERRCHECK;
            break;
        default:
            //** unsupported media type; return error code
            assert (0);
			err = SYSTEM_ERROR;
            break;// to be verified..
    }

    return err;	
}

DRIVER_ERROR HeaderLJJetReady::EndJob ()
{
    DRIVER_ERROR    err = NO_ERROR;

	err = thePrinter->Send((const BYTE*)JrEndSessionSeq, sizeof(JrEndSessionSeq));
	ERRCHECK;

	err = thePrinter->Send((const BYTE*)ccpPJLExitSeq, sizeof(ccpPJLExitSeq));
	ERRCHECK;

    return err;
}

DRIVER_ERROR HeaderLJJetReady::FormFeed ()
{
	DRIVER_ERROR    err = NO_ERROR;
    ModeJPEG        *pCompressor;
    LJJetReady      *thisPrinter = (LJJetReady *) thePrinter;
    int             iPageHeight;

    iPageHeight = (int) (thePrintContext->PrintableHeight () *
                         thePrintContext->EffectiveResolutionY ());
    iPageHeight = ((iPageHeight + MOJAVE_STRIP_HEIGHT - 1) / MOJAVE_STRIP_HEIGHT) * MOJAVE_STRIP_HEIGHT;

/*
 *  Send white rasters to fill up the rest of the page.
 *  This is required according to Brian Mayer, but introduces a long delay
 *  in printing. LJ3500 works fine without these white strips.
 */

    pCompressor = (ModeJPEG *) thisPrinter->GetCompressor ();
    err = pCompressor->SendWhiteStrips (iPageHeight, thisPrinter->bGrey_K);


	bLastBand = TRUE;

	err = EndPage ();

    thisPrinter->m_bStartPageNotSent = TRUE;
	return err;
}

DRIVER_ERROR HeaderLJJetReady::SendCAPy (unsigned int iAbsY)
{
    return NO_ERROR;
}

DRIVER_ERROR LJJetReady::Encapsulate (const RASTERDATA* InputRaster, BOOL bLastPlane)
{
    BYTE            res[64];
    BYTE            *pDataPtr = NULL;
    DRIVER_ERROR    err;

    //if (m_bStartPageNotSent)
    //{
    //    err = phLJJetReady->StartPage ();
    //    if (err != NO_ERROR)
    //        return err;
	//	m_bStartPageNotSent = FALSE;
    //}


    unsigned short int   wCoordinate; 

    unsigned long   ulVUDataLength = 0;

	// For color JPEG, you need to skip the header information of 623 bytes

    int iJpegHeaderSize = 623;
#ifdef HAVE_LIBDL
    if (HPLJJRCompress && m_eCompressMode == COMPRESS_MODE_LJ)
    {
        iJpegHeaderSize = 0;
    }
#endif

	if (bGrey_K)
    {
		ulVUDataLength = InputRaster->rastersize[COLORTYPE_COLOR];
        pDataPtr = (BYTE *) InputRaster->rasterdata[COLORTYPE_COLOR];
    }
	else
	{
		ulVUDataLength = InputRaster->rastersize[COLORTYPE_COLOR]    - iJpegHeaderSize;
        pDataPtr = (BYTE *) InputRaster->rasterdata[COLORTYPE_COLOR] + iJpegHeaderSize;
	}


    //VWritePrinter("\xC2\x01\x40\x70\x68\xF8\x91\xC1", 0x08);
	BYTE JrReadImageSeq[] = {0xC2, 0x01, 0x40, 0x70,0x68, 0xF8, 0x91, 0xC1};
	err = Send ((const BYTE *)JrReadImageSeq, sizeof(JrReadImageSeq));
	ERRCHECK;

	ModeJPEG     *pCompressor = NULL;
 	pCompressor = (ModeJPEG*)GetCompressor();

    wCoordinate = (unsigned short int)pCompressor->GetCoordinates() - 128;
//	err = Send ((const BYTE *)&wCoordinate, sizeof(unsigned short int));
    res[0] = wCoordinate & 0xFF;
    res[1] = ((wCoordinate & 0xFF00) >> 8);
    err = Send ((const BYTE *) res, 2);
	ERRCHECK;

	BYTE JrStripHeight[] = {0xF8,0x6D,0xC1,0x80,0x00,0xF8,0x63};
	err = Send ((const BYTE *)JrStripHeight, sizeof(JrStripHeight));
	ERRCHECK;

	BYTE JrTextObjectTypeSeq[] = {0xC0,0x00, 0xF8, 0x96};
	err = Send ((const BYTE *)JrTextObjectTypeSeq, sizeof(JrTextObjectTypeSeq));
	ERRCHECK;

	err = Send ((const BYTE *)JrVU_ver_TagSeq, sizeof(JrVU_ver_TagSeq));
	ERRCHECK;

	err = Send ((const BYTE *) "\xC2", 1);
	ERRCHECK;

    ulVUDataLength += 6;
    res[0] = (BYTE) (ulVUDataLength & 0xFF);
    res[1] = (BYTE) ((ulVUDataLength & 0x0000FF00) >> 8);
    res[2] = (BYTE) ((ulVUDataLength & 0x00FF0000) >> 16);
    res[3] = (BYTE) ((ulVUDataLength & 0xFF000000) >> 24);
    res[4] = 0xF8;
    res[5] = 0x92;
    res[6] = 0x46;
    res[7] = 0x21;
#ifdef HAVE_LIBDL
    if (HPLJJRCompress && m_eCompressMode == COMPRESS_MODE_LJ)
    {
        res[7] = 0x11;
    }
#endif
    res[8] = 0x90;

    ulVUDataLength -= 6;
    res[9] = (BYTE) (ulVUDataLength & 0xFF);
    res[10] = (BYTE) ((ulVUDataLength & 0x0000FF00) >> 8);
    res[11] = (BYTE) ((ulVUDataLength & 0x00FF0000) >> 16);
    res[12] = (BYTE) ((ulVUDataLength & 0xFF000000) >> 24);

	err = Send (res, 13);
	ERRCHECK;

	err = Send (pDataPtr, ulVUDataLength);
	ERRCHECK;

    return err;
}

Header* LJJetReady::SelectHeader (PrintContext *pc)
{

/*
 *  To take care of what seems to be a firmware bug in the printer's grayscale path,
 *  need printable width value later. So, save the PrintContext here.
 *  Raghu
 */

    COLORMODE       eC = COLOR;
    MEDIATYPE       eM;
    QUALITY_MODE    eQ;
    BOOL            bD;

    thePrintContext = pc;
    phLJJetReady = new HeaderLJJetReady (this, pc);
    pc->GetPrintModeSettings (eQ, eM, eC, bD);
    if (eQ == QUALITY_BEST)
    {
        m_eCompressMode = COMPRESS_MODE_LJ;
    }

#ifdef HAVE_LIBDL
    if (HPLJJRCompress && m_eCompressMode != COMPRESS_MODE_LJ)
    {
        dlclose (m_hHPLibHandle);
        m_hHPLibHandle = NULL;
        HPLJJRCompress = NULL;
    }
#endif
	return phLJJetReady;
}

DRIVER_ERROR LJJetReady::VerifyPenInfo()
{
    ePen = BOTH_PENS;
    return NO_ERROR;
}

DRIVER_ERROR LJJetReady::ParsePenInfo(PEN_TYPE& ePen, BOOL QueryPrinter)
{
    ePen = BOTH_PENS;

    return NO_ERROR;
}

Compressor* LJJetReady::CreateCompressor (unsigned int RasterSize)
{

/*
 *  There is some problem printing in grayscale. When the inputwidth is less than the full
 *  page width, printer replicates the image across the width. To fix this, make the width of
 *  each strip the width of the printable page size. Note that this does not create any scaling
 *  of the image.
 *  In addition, there is a minimum source width requirement, which is
 *      3 in. * resolution - 2 * margins = 3 * 600 - 2 * 100 = 1600
 *  So, minimum RasterSize must be 1600 * 3 here (width * 3 for RGB).
 *  Raghu
 */

    if (bGrey_K)
    {
        RasterSize = (int) (thePrintContext->PrintableWidth () * thePrintContext->EffectiveResolutionX ()) * 3;
    }

    if (RasterSize < 4800)
    {
        RasterSize = 4800;
    }

/*
 *  Further, source width must be a multiple of 32 (96 for RGB here).
 */

    RasterSize = ((RasterSize + 95) / 96) * 96;

    m_pCompressor = new ModeJPEG (pSS, this, RasterSize, m_eCompressMode);
    return m_pCompressor;
}

/*
 *  Function name: ParseError
 *
 *  Owner: Darrell Walker
 *
 *  Purpose:  To determine what error state the printer is in.
 *
 *  Called by: Send()
 *
 *  Parameters on entry: status_reg is the contents of the centronics
 *                      status register (at the time the error was
 *                      detected)
 *
 *  Parameters on exit: unchanged
 *
 *  Return Values: The proper DISPLAY_STATUS to reflect the printer
 *              error state.
 *
 */
DISPLAY_STATUS LJJetReady::ParseError(BYTE status_reg)
{
    DBG1("LJJetReady: parsing error info\n");

    DRIVER_ERROR err = NO_ERROR;
    BYTE    szReadBuff[256];
    DWORD   iReadCount = 256;
    DISPLAY_STATUS  eStatus = (DISPLAY_STATUS) status_reg;
    char    *tmpStr;
    int     iErrorCode;

    if (!IOMode.bDevID)
        return eStatus;

    memset (szReadBuff, 0, 256);
    err = pSS->FromDevice (szReadBuff, &iReadCount);
    if (err == NO_ERROR && iReadCount == 0)
        return eStatus;

    if (strstr ((char *) szReadBuff, "JOB"))
    {
        if (!(tmpStr = strstr ((char *) szReadBuff, "NAME")))
            return DISPLAY_PRINTING;
        tmpStr += 6;
        while (*tmpStr < '0' || *tmpStr > '9')
            tmpStr++;
        sscanf (tmpStr, "%d", &iErrorCode);
        if (iErrorCode != (long) (this))
            return DISPLAY_PRINTING;
    }

    if (strstr ((char *) szReadBuff, "END"))
    {
        return DISPLAY_PRINTING_COMPLETE;
    }


    if (strstr ((char *) szReadBuff, "CANCEL"))
        return DISPLAY_PRINTING_CANCELED;

    if (!(tmpStr = strstr ((char *) szReadBuff, "CODE")))
        return eStatus;

    tmpStr += 4;
    while (*tmpStr < '0' || *tmpStr > '9')
        tmpStr++;
    sscanf (tmpStr, "%d", &iErrorCode);

    if (iErrorCode < 32000)
        return DISPLAY_PRINTING;

    if (iErrorCode == 40010 || iErrorCode == 40020)
        return DISPLAY_NO_PENS;     // Actually, out of toner

    if (iErrorCode == 40021)
        return DISPLAY_TOP_COVER_OPEN;

    if ((iErrorCode / 100) == 419)
        return DISPLAY_OUT_OF_PAPER;

    if ((iErrorCode / 1000) == 42 || iErrorCode == 40022)
    {
        DBG1("Paper Jammed\n");
        return DISPLAY_PAPER_JAMMED;
    }

    if (iErrorCode > 40049 && iErrorCode < 41000)
    {
        DBG1("IO trap\n");
        return DISPLAY_ERROR_TRAP;
    }

    if (iErrorCode == 40079)
        return DISPLAY_OFFLINE;

    return DISPLAY_ERROR_TRAP;
}

BYTE *    ModeJPEG::fpJPEGBuffer = NULL;       // image buffer
DWORD     ModeJPEG::fJPEGBufferPos;            // position of 1'st empty byte in image buffer

void ModeJPEG::jpeg_flush_output_buffer_callback(JOCTET *outbuf, BYTE* buffer, DWORD size)
{
    fJPEGBufferPos += size;
    memcpy (fpJPEGBuffer, buffer, size);
    fpJPEGBuffer += size;
}

//----------------------------------------------------------------
// These are "overrides" to the JPEG library error routines
//----------------------------------------------------------------
static void Gjpeg_error (j_common_ptr cinfo)
{
    // The standard behavior is to send a message to stderr.
}
/*
void ModeJPEG::SetBuffer(BYTE * pJPEGBuffer)
{
    fpJPEGBuffer = pJPEGBuffer;
}
*/
DWORD ModeJPEG::GetJPEGBufferSize() const
{
    return fJPEGBufferPos;
}

/*
BYTE* ModeJPEG::GetBuffer() 
{
    return fpJPEGBuffer;
}
*/

extern "C"
{
void jpeg_buffer_dest (j_compress_ptr cinfo, JOCTET* outbuff, void* flush_output_buffer_callback);
void hp_rgb_ycc_setup (int iFlag);
}

#define ConvertToGrayMacro(red, green, blue) ((unsigned char)( ( (red * 30) + (green * 59) + (blue * 11) ) / 100 ))

//--------------------------------------------------------------------
// Function:    ModeJPEG::ModeJPEG
//
// Release:     [PROTO4_1]
//
// Description: Preferred ctor
//
// Input:        padMultiple - the fBufferDataLength returned from
//                             GetRow must be divisible by this value.
//
// Modifies:     fpBuffer
//
// Precond:      None
//
// Postcond:     None
//
// Returns:      None
//
// Created:            11/07/96 cal
// Last Modified:      5/020/01 DG
//--------------------------------------------------------------------
ModeJPEG::ModeJPEG 
(    
	SystemServices* pSys,
    Printer* pPrinter,
    unsigned int PlaneSize,
    COMPRESS_MODE eCompressMode
) :
    Compressor(pSys, PlaneSize, TRUE),
    thePrinter(pPrinter)    // needed by Flush                        
{
    if (constructor_error != NO_ERROR)  // if error in base constructor
    {
        return;
    }
    fpJPEGBuffer = NULL;    // image buffer
    fJPEGBufferPos = 0;     // position of 1'st empty byte in image buffer
    fpJPEGStart = NULL;

	m_SourceBitmap.pvBits = (BYTE*) new BYTE[(inputsize * MOJAVE_STRIP_HEIGHT)];

	if (m_SourceBitmap.pvBits == NULL)
    {
		constructor_error = ALLOCMEM_ERROR;
    }
    else
    {
    	memset (m_SourceBitmap.pvBits, 0xFF, inputsize * MOJAVE_STRIP_HEIGHT);
    }
    m_SourceBitmap.cjBits = 0;
    m_SourceBitmap.bitmapInfo.bmiHeader.biWidth = 0;
    m_SourceBitmap.bitmapInfo.bmiHeader.biHeight = 0;


	m_DestBitmap.pvBits = NULL;
    m_DestBitmap.cjBits = 0;
    m_DestBitmap.bitmapInfo.bmiHeader.biWidth = 0;
    m_DestBitmap.bitmapInfo.bmiHeader.biHeight = 0;

	m_lCurrCDRasterRow	= 0;
	m_lPrinterRasterRow = 0;

    m_iRasterWidth = inputsize / 3;

	iRastersReady = 0;

	compressBuf = NULL;
    m_eCompressMode = eCompressMode;

} // ModeJPEG::ModeJPEG


//--------------------------------------------------------------------
// Function:    ModeJPEG::~ModeJPEG
//
// Release:     [PROTO4_1]
//
// Description: Destructor.
//
// Input:       None
//
// Modifies:    fpBuffer - deletes the buffer
//              ?? - deletes other buffer(s)??
//
// Precond:     None
//
// Postcond:    None
//
// Returns:     None
//
// Created:            11/07/96 cal
// Last Modified:      5/020/01 DG
//--------------------------------------------------------------------
ModeJPEG::~ModeJPEG ()
{   
	if (m_SourceBitmap.pvBits)
	{
		delete m_SourceBitmap.pvBits;
		m_SourceBitmap.pvBits = NULL;
	}

	if (m_DestBitmap.pvBits)
	{
		delete m_DestBitmap.pvBits;
		m_DestBitmap.pvBits = NULL;
	}

} // ModeJPEG::~ModeJPEG

/*
 *  JetReady specs require that data be sent for the whole page. When input data
 *  does not cover the whole page, send white rasters to cover the rest of the
 *  page.
 */

DRIVER_ERROR ModeJPEG::SendWhiteStrips (int iPageHeight, BOOL bGray)
{
    DRIVER_ERROR    err = NO_ERROR;
	HPLJBITMAP      hWhiteBitmap;
    QTABLEINFO      qTableInfo;
    RASTERDATA      whiteRaster;

    qTableInfo.qFactor = 6;

	hWhiteBitmap.pvBits = NULL;

	if (bGray)
	{
        m_SourceBitmap.cjBits = m_iRasterWidth * MOJAVE_STRIP_HEIGHT;
        memset (m_SourceBitmap.pvBits, 0x00, m_SourceBitmap.cjBits);
    }
    else
    {
        m_SourceBitmap.cjBits = m_iRasterWidth * MOJAVE_STRIP_HEIGHT * 3;
        memset (m_SourceBitmap.pvBits, 0xFF, m_SourceBitmap.cjBits);
    }
	memset(&m_SourceBitmap.bitmapInfo.bmiHeader, 0, sizeof(HPBITMAPINFOHEADER));
	m_SourceBitmap.bitmapInfo.bmiHeader.biWidth = m_iRasterWidth;
	m_SourceBitmap.bitmapInfo.bmiHeader.biHeight = MOJAVE_STRIP_HEIGHT;
	m_SourceBitmap.bitmapInfo.bmiHeader.biSizeImage = m_SourceBitmap.cjBits;
	m_SourceBitmap.bitmapInfo.bmiHeader.biBitCount = 8;
    Compress (&m_SourceBitmap, &m_DestBitmap, &qTableInfo, bGray);
    whiteRaster.rastersize[COLORTYPE_COLOR] = m_DestBitmap.cjBits;
    whiteRaster.rasterdata[COLORTYPE_COLOR] = m_DestBitmap.pvBits;
    while (m_lPrinterRasterRow < iPageHeight)
    {
        thePrinter->Encapsulate (&whiteRaster, TRUE);
        m_lPrinterRasterRow += MOJAVE_STRIP_HEIGHT;
    }

    return err;
}

BOOL  ModeJPEG::Compress( HPLJBITMAP *pSrcBitmap,
                          HPLJBITMAP *pTrgBitmap,
                          QTABLEINFO *pQTable,
						  BOOL		  bGrayscaleSet
                        ) 

{
#ifdef HAVE_LIBDL
    if (HPLJJRCompress && m_eCompressMode == COMPRESS_MODE_LJ)
    {
        int     iRet;
        memcpy (pTrgBitmap, pSrcBitmap, sizeof (HPLJBITMAP));
        pTrgBitmap->pvBits = new BYTE[pSrcBitmap->bitmapInfo.bmiHeader.biWidth * pSrcBitmap->bitmapInfo.bmiHeader.biHeight * 3];
        if (pTrgBitmap->pvBits == NULL)
        {
            return FALSE;
        }
        if (bGrayscaleSet)
        {
            BYTE    *p = pSrcBitmap->pvBits;
            for (int j = 0; j < pSrcBitmap->bitmapInfo.bmiHeader.biHeight; j++)
            {
                for (int i = 0; i < pSrcBitmap->bitmapInfo.bmiHeader.biWidth; i++)
                {
                    p[0] = ConvertToGrayMacro (p[0], p[1], p[2]);
                    p[1] = p[2] = 0;
                    p += 3;
                }
            }
        }
        iRet = HPLJJRCompress (pTrgBitmap->pvBits, (uint32_t *) &pTrgBitmap->cjBits, pSrcBitmap->pvBits,
                               pSrcBitmap->bitmapInfo.bmiHeader.biWidth, pSrcBitmap->bitmapInfo.bmiHeader.biHeight);
        if (iRet < 0)
            return FALSE;
        return TRUE;
    }
#endif // HAVE_LIBDL

#ifdef JPEG_FILE_OUTPUT
	static int ifilecount = 0;
    char szFileName [256];
    sprintf(szFileName,"C:\\Temp\\compressed%03d",ifilecount++);
    strcat(szFileName,".jpg");
    FILE *pFile = fopen(szFileName, "w+b");
#endif

	fJPEGBufferPos = 0;

    hp_rgb_ycc_setup (1);   // Use modified Mojave CSC table

    //----------------------------------------------------------------
    // Setup for compression 
    //----------------------------------------------------------------
    
    //----------------------------------------------------------------
    // JPEG Lib Step 1: Allocate and initialize JPEG compression object
    //----------------------------------------------------------------
    cinfo.err = jpeg_std_error( &jerr );

    //Fix the error handler to return when an error occurs,
    //the default exit()s which is nasty for a driver to do.
    jerr.error_exit = Gjpeg_error;
    //Set the return jump address. This must be done now since
    // jpeg_create_compress could cause an error.
    if (setjmp(setjmp_buffer)) {

        // If we get here, the JPEG code has signaled an error.
        //* We need to clean up the JPEG object, and return.
         
        jpeg_destroy_compress(&cinfo);
        return FALSE;
    }

    jpeg_create_compress( &cinfo );
   
    //----------------------------------------------------------------
    // JPEG Lib Step 2: Specify the destination for the compressed data
    // For our purposes we need to replace the "data destination" module
    // with one that uses the JPEG library's I/O suspension mode.
    // The buffer pointer and free space are updated each time GetNextRow
    // is called; so only the function pointers are set here.
    //----------------------------------------------------------------

    //----------------------------------------------------------------
    // JPEG Lib Step 3: Set parameters for compression, including image size & colorspace
    //----------------------------------------------------------------

    int iColorsUsed;

    if(bGrayscaleSet)
    {
        cinfo.in_color_space = JCS_GRAYSCALE;
        iColorsUsed = 1;
    }
    else
    {
        cinfo.in_color_space = JCS_RGB; // arbitrary guess
        iColorsUsed = 3;
    }
    jpeg_set_defaults( &cinfo );

    cinfo.image_width = pSrcBitmap->bitmapInfo.bmiHeader.biWidth; 
    cinfo.image_height = pSrcBitmap->bitmapInfo.bmiHeader.biHeight;
    cinfo.input_components = iColorsUsed;  //change if bit depths others than 24bpp are ever needed
    cinfo.data_precision = 8;

    //
    // Create a static quant table here. 
    //
    static unsigned int mojave_quant_table1[64] =  { 2,3,4,5,5,5,5,5,
                                                    3,6,5,8,5,8,5,8,
                                                    4,5,5,5,5,5,5,5,
                                                    5,8,5,8,5,8,5,8,
                                                    5,5,5,5,5,5,5,5,
                                                    5,8,5,8,5,8,5,8,
                                                    5,5,5,5,5,5,5,5,
                                                    5,8,5,8,5,8,5,8 };
    //
    // Use this variable for representing the scale_factor for now.
    //
    unsigned int iScaleFactor = pQTable->qFactor;
    
    //
    // JetReady specific Q-Tables will be added here. We do the following:
    //  1. Add three Q-Tables.
    //  2. Scale the Q-Table elemets with the given scale factor.
    //  3. Check to see if any of the element is in the table is greater than 255
    //     reset that elemet to 255.
    //  5. There is a specific scaling need to be done to the first 6 
    //     elements in the matrix. This required to achieve the better
    //     compression ratio.
    //  4. Check to see if any the of recently modified element is
    //     greater than 255, reset that with 255.
    //  Following for loop implements the above logic.
    //
    //  Please refer to sRGBLaserHostBasedSoftwareERS.doc v9.0 section 5.2.5.3.1.1
    //  for more details.
    //
    //  [NOTE] These loop needs to be further optimized.
    //
    for (int i = 0; i < 3; i++)
    {
        //
        // Adding Q-Table.
        //
        jpeg_add_quant_table(&cinfo, i, mojave_quant_table1,  0, FALSE );
        //
        // Scaling the Q-Table elements. 
        // Reset the element to 255, if it is greater than 255.
        //
        for(int j = 1; j < 64; j++)
        {
            cinfo.quant_tbl_ptrs[i]->quantval[j] = (UINT16)((mojave_quant_table1[j] * iScaleFactor) & 0xFF);  

        }   // for (int j = 1; j < 64; j++)
        //
        // Special scaling for first 6 elements in the table.
        // Reset the specially scaled elements 255, if it is greater than 255.
        //

        //
        // 1st component in the table. Unchanged, I need not change anything here.
        //
        cinfo.quant_tbl_ptrs[i]->quantval[0] = (UINT16)mojave_quant_table1[0];
        
        //
        // 2nd and 3rd components in the zig zag order
        //
		// The following dTemp is being used  to ceil the vales: e.g 28.5 to 29
        //
        double dTemp = mojave_quant_table1[1] * (1 + 0.25 * (iScaleFactor - 1)) + 0.5;
        cinfo.quant_tbl_ptrs[i]->quantval[1] = (UINT16)dTemp & 0xFF;
        
        dTemp = mojave_quant_table1[8] * (1 + 0.25 * (iScaleFactor - 1)) + 0.5;
        cinfo.quant_tbl_ptrs[i]->quantval[8] = (UINT16)dTemp & 0xFF;
       
        //
        // 4th, 5th and 6th components in the zig zag order
        //
        dTemp = mojave_quant_table1[16] * (1 + 0.50 * (iScaleFactor - 1)) + 0.5;
        cinfo.quant_tbl_ptrs[i]->quantval[16] = (UINT16)dTemp & 0xFF;
        
        dTemp = mojave_quant_table1[9] * (1 + 0.50 * (iScaleFactor - 1)) + 0.5;
        cinfo.quant_tbl_ptrs[i]->quantval[9]  = (UINT16)dTemp & 0xFF;
        
        dTemp = mojave_quant_table1[2] * (1 + 0.50 * (iScaleFactor - 1)) + 0.5;
        cinfo.quant_tbl_ptrs[i]->quantval[2]  = (UINT16)dTemp & 0xFF;
    }   // for (i = 0; i < 3; i++)

    //
    // Hard code to use sampling mode 4:4:4
    //
    cinfo.comp_info[0].h_samp_factor = 1;
    cinfo.comp_info[0].v_samp_factor = 1;

    // Specify data destination for compression 
    fpJPEGBuffer = new BYTE[cinfo.image_width * cinfo.image_height * iColorsUsed];  //3 bytes for 24 bpp
    if (NULL == fpJPEGBuffer)
    {
        //ERR(("JPEGCompress: Not enough memory\n"));
        return FALSE;
    }

    fpJPEGStart = fpJPEGBuffer;

    jpeg_buffer_dest(&cinfo, (JOCTET *) fpJPEGBuffer, (void*) (ModeJPEG::jpeg_flush_output_buffer_callback));
    if(bGrayscaleSet)
    {
        cinfo.write_JFIF_header  =
        cinfo.write_Adobe_marker = FALSE;
        jpeg_suppress_tables(&cinfo, TRUE);
    }

    //----------------------------------------------------------------
    // JPEG Lib Step 4: Start the compression cycle
    //  set destination to table file
    //    jpeg_write_tables(cinfo);
    //    set destination to image file
    //  jpeg_start_compress(cinfo, FALSE);
    //----------------------------------------------------------------
    
    jpeg_start_compress( &cinfo, TRUE );    
   

    //----------------------------------------------------------------
    // This completes the JPEG setup.
    //----------------------------------------------------------------
    

    // do the jpeg compression
    JSAMPROW    pRowArray[1];
    BYTE* pCurLine = (BYTE*) pSrcBitmap->pvBits;
    DWORD dwScanLine;
    
    dwScanLine = cinfo.image_width * iColorsUsed;

	for(unsigned int nrow = 0; nrow < cinfo.image_height; nrow++)
    {
        pRowArray[0] = (JSAMPROW) pCurLine;
        jpeg_write_scanlines(&cinfo, pRowArray, 1);
        pCurLine += dwScanLine;
    }

    //----------------------------------------------------------------
    // JPEG Lib Step 6: Finish compression
    //----------------------------------------------------------------
    // Tell the compressor about the extra buffer space for the trailer
    jpeg_finish_compress( &cinfo );

    //
    // Read the quantization tables used for the compression.
    //

    if (cinfo.quant_tbl_ptrs[0] != NULL)
    {
        for (int iI = 0; iI < QTABLE_SIZE; iI++)
        {
            pQTable->qtable0[iI] = (DWORD)cinfo.quant_tbl_ptrs[0]->quantval[iI];
        }

    }

    if (cinfo.quant_tbl_ptrs[1] != NULL)
    {
        for (int iI = 0; iI < QTABLE_SIZE; iI++)
        {
            pQTable->qtable1[iI] = (DWORD)cinfo.quant_tbl_ptrs[1]->quantval[iI];
        }
    }

    if (cinfo.quant_tbl_ptrs[2] != NULL)
    {
        for (int iI = 0; iI < QTABLE_SIZE; iI++)
        {
            pQTable->qtable2[iI] = (DWORD)cinfo.quant_tbl_ptrs[2]->quantval[iI];
        }
    }


    //----------------------------------------------------------------
    // JPEG Lib Step 7: Destroy the compression object
    //----------------------------------------------------------------
    jpeg_destroy_compress( &cinfo );
                  
    memcpy(pTrgBitmap, pSrcBitmap, sizeof(HPLJBITMAP));

#ifdef JPEG_FILE_OUTPUT
        fwrite(fpJPEGStart, sizeof(BYTE), GetJPEGBufferSize(), pFile);
#endif
    //
    if(fpJPEGStart && bGrayscaleSet)
    {
        long lBufferSize = GetJPEGBufferSize();
        long l = 0;
        while (l < lBufferSize)
        {
            if(fpJPEGStart[l] == 0xFF && fpJPEGStart[l+1] == 0xDA)
                break;
            l++;
        }
        if(l != lBufferSize)
        {
            memcpy (fpJPEGStart, fpJPEGStart + l + 10, lBufferSize - l - 10);
            memset (fpJPEGStart + lBufferSize - l - 10, 0xFF, l + 10);
            pTrgBitmap->cjBits = lBufferSize - l - 10;
            pTrgBitmap->pvBits = (BYTE*)fpJPEGStart;
        }
        else
        {
            pTrgBitmap->pvBits = (BYTE*)fpJPEGStart;
        }
    }
    else
    {
        pTrgBitmap->pvBits = (BYTE*) fpJPEGStart;
        pTrgBitmap->cjBits = GetJPEGBufferSize();
    }

#ifdef JPEG_FILE_OUTPUT
	fclose(pFile);
#endif

    return TRUE;

}

BOOL ModeJPEG::Process
(
    RASTERDATA* input
)
{
	DRIVER_ERROR		err = NO_ERROR;

    if (input==NULL || 
		(input->rasterdata[COLORTYPE_COLOR]==NULL && input->rasterdata[COLORTYPE_BLACK]==NULL))    // flushing pipeline
    {
        return FALSE;
    }
	if (input->rasterdata[COLORTYPE_COLOR])
	{
		if (m_lCurrCDRasterRow < MOJAVE_STRIP_HEIGHT )
		{
			//Copy the data to m_SourceBitmap
			memcpy(m_SourceBitmap.pvBits + m_lCurrCDRasterRow * inputsize, input->rasterdata[COLORTYPE_COLOR], input->rastersize[COLORTYPE_COLOR]);
			m_lCurrCDRasterRow ++;
		}

		if (m_lCurrCDRasterRow == MOJAVE_STRIP_HEIGHT || ((LJJetReady*)thePrinter)->phLJJetReady->IsLastBand())
		{
			m_SourceBitmap.cjBits = inputsize * MOJAVE_STRIP_HEIGHT;
			memset(&m_SourceBitmap.bitmapInfo.bmiHeader, 0, sizeof(HPBITMAPINFOHEADER));
			m_SourceBitmap.bitmapInfo.bmiHeader.biWidth = inputsize / 3;
			m_SourceBitmap.bitmapInfo.bmiHeader.biHeight = MOJAVE_STRIP_HEIGHT;

			QTABLEINFO  qTableInfo;
			BOOL	bGrayScaleSet = ((LJJetReady *)thePrinter)->bGrey_K;
			BOOL	bRet		  = TRUE;

			qTableInfo.qFactor = 6;
			if (((LJJetReady*)thePrinter)->m_bStartPageNotSent)
			{
				err = (((LJJetReady*)thePrinter)->phLJJetReady)->StartPage();
				((LJJetReady*)thePrinter)->m_bStartPageNotSent = FALSE;

				char res[64];
				//
				//  0xC2       - unsigned 32 bit int   
				//  0x68704000 - begin image for the JetReady path - endin correct to 00 40 70 68
				//  0xF8       - unsigned 8 bit attribute
				//  0x91       - VUextension  (a vendor unique attribute for JetReady) 
				//  0xC1       - Unsigned 16 bit int
				err = thePrinter->Send ((const BYTE *)JrBeginImageSeq, sizeof(JrBeginImageSeq));
				ERRCHECK;

				unsigned short int sourcewidth = (unsigned short int) (inputsize / 3);
				unsigned short int sourceheight = (unsigned short int)((((LJJetReady*)thePrinter)->phLJJetReady)->thePrintContext->PhysicalPageSizeY() * MOJAVE_RESOLUTION - 200);
				if ( 0 != (sourceheight % MOJAVE_STRIP_HEIGHT) )
				{
					sourceheight = ( (sourceheight / MOJAVE_STRIP_HEIGHT) + 1) * MOJAVE_STRIP_HEIGHT;
				}	

				//Write the source width to the printer;
//				err = thePrinter->Send ((const BYTE*)&sourcewidth, sizeof(unsigned short int));
                res[0] = sourcewidth & 0x00FF;
                res[1] = ((sourcewidth & 0xFF00) >> 8);
                err = thePrinter->Send ((const BYTE *) res, 2);
				ERRCHECK;
				//  0xF8       - unsigned 8 bit attribute
				//  0x6C       - source width attribute
				//  0xC1       - Unsigned 16 bit int
				strcpy (res, "\xF8\x6C\xC1");
				err = thePrinter->Send ((const BYTE *) res, strlen (res));
				ERRCHECK;
				
				//Write the source height to the printer
//				err = thePrinter->Send ((const BYTE*)&sourceheight, sizeof(unsigned short int));
                res[0] = sourceheight & 0x00FF;
                res[1] = ((sourceheight & 0xFF00) >> 8);
                err = thePrinter->Send ((const BYTE *) res, 2);
				ERRCHECK;
				// 0xF8       - unsigned 8 bit attribute
				// 0x6B       - source height attribute
				// 0xC1       - Unsigned 16 bit int
				strcpy (res, "\xF8\x6B\xC1");
				err = thePrinter->Send ((const BYTE *) res, strlen (res));
				ERRCHECK;
				
				unsigned short int stripcount = sourceheight / MOJAVE_STRIP_HEIGHT;
				//stripcount = 4;
//				err = thePrinter->Send ((const BYTE *)&stripcount, sizeof(stripcount));
                res[0] = stripcount & 0x00FF;
                res[1] = ((stripcount & 0xFF00) >> 8);
                err = thePrinter->Send ((const BYTE *) res, 2);
				ERRCHECK;
				//0xF8       - unsigned 8 bit attribute
				//0x93       - special attribute 1 for strip count  
				//0xC1       - Unsigned 16 bit int
				strcpy (res, "\xF8\x93\xC1");
				err = thePrinter->Send ((const BYTE *) res, strlen (res));
				//BYTE JrStripCountSeq[] = {0x32,0x00,0xF8,0x93,0xC1};
				//err = thePrinter->Send ((const BYTE *)JrStripCountSeq, sizeof(JrStripCountSeq));
				ERRCHECK;

				// Write the MOJAVE_STRIP_HEIGHT value
				//0xF8       - unsigned 8 bit attribute
				//0x94       - special attribute 2 used for strip height
				BYTE JrStripHeightSeq[] = {0x80,0x00,0xF8,0x94};
				err = thePrinter->Send ((const BYTE *)JrStripHeightSeq, sizeof(JrStripHeightSeq));
				ERRCHECK;

				if  (bGrayScaleSet)
				{
					// 0x00 indicates grayscale printing
					BYTE JrGrayScaleSeq[] = {0xC0,0x00,0xF8,0x97};
					err = thePrinter->Send ((const BYTE *)JrGrayScaleSeq, sizeof(JrGrayScaleSeq));
					ERRCHECK;
				}
				else
				{
					// 0x04 indicates Color printing
					BYTE JrColorSeq[] = {0xC0,0x04,0xF8,0x97};
					err = thePrinter->Send ((const BYTE *)JrColorSeq, sizeof(JrColorSeq));
					ERRCHECK;
				}

				// Interleaved Color Enumeration for Mojave
				BYTE JrSeq[] = {0xC0,0x00,0xF8,0x98};
				err = thePrinter->Send ((const BYTE *)JrSeq, sizeof(JrSeq));
				ERRCHECK;

				//0xC2       - unsigned 32 bit int   
				//0x00030000 - JetReady version number 
				//0xF8       - unsigned 8 bit attribute
				//0x95       - JetReadyVersion attribute
				err = thePrinter->Send ((const BYTE *)JrVU_ver_TagSeq, sizeof(JrVU_ver_TagSeq));
				ERRCHECK;

				//  Send the JPEG statement for the sake of Dual Compression
				//  0xC2       - unsigned 32 bit int   
				//  VU DataLength - 824 bytes for jpeg header
				//  0xF8       - unsigned 8 bit attribute
				//  0x92       - VU data length
				//  0x46       - vendor unique  
				BYTE JrDataLengthSeq[] = {0xC2,0x38,0x03,0x00,0x00,0xF8,0x92,0x46};
#ifdef HAVE_LIBDL
                if (HPLJJRCompress && m_eCompressMode == COMPRESS_MODE_LJ)
                {
                    JrDataLengthSeq[1] = 0;
                    JrDataLengthSeq[2] = 0;
                }
#endif
				err = thePrinter->Send ((const BYTE *)JrDataLengthSeq, sizeof(JrDataLengthSeq));
				ERRCHECK;

				bRet = Compress (&m_SourceBitmap, 
								 &m_DestBitmap,
								 &qTableInfo,
								 FALSE	// We are only worried about the qTables not about the colorspace.
							    );
                if (!HPLJJRCompress || (HPLJJRCompress && m_eCompressMode != COMPRESS_MODE_LJ))
                {

				    //VWritePrinter("\x00\x80\x00\x03\x00\x00", 0x6);
				    BYTE JrQTSeq[] = {0x00,0x80,0x00,0x03,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrQTSeq, sizeof(JrQTSeq));
				    ERRCHECK;

#ifdef APDK_LITTLE_ENDIAN
				    //VWritePrinter((VOID*) pQTableInfo->qtable0, sizeof(DWORD) * QTABLE_SIZE);
				    err = thePrinter->Send ((const BYTE *)qTableInfo.qtable0, sizeof(DWORD) * QTABLE_SIZE);
				    ERRCHECK;


				    //VWritePrinter((VOID*) pQTableInfo->qtable1, sizeof(DWORD) * QTABLE_SIZE);
				    err = thePrinter->Send ((const BYTE *)qTableInfo.qtable1, sizeof(DWORD) * QTABLE_SIZE);
				    ERRCHECK;

				    //VWritePrinter((VOID*) pQTableInfo->qtable2, sizeof(DWORD) * QTABLE_SIZE);
				    err = thePrinter->Send ((const BYTE *)qTableInfo.qtable2, sizeof(DWORD) * QTABLE_SIZE);
				    ERRCHECK;
#else
                    BYTE    szStr[sizeof (DWORD) * QTABLE_SIZE * 3];
                    BYTE    *p;
                    p = szStr;
                    for (int i = 0; i < QTABLE_SIZE; i++)
                    {
                        *p++ = qTableInfo.qtable0[i] & 0xFF;
                        *p++ = (qTableInfo.qtable0[i] >> 8) & 0xFF;
                        *p++ = (qTableInfo.qtable0[i] >> 16) & 0xFF;
                        *p++ = (qTableInfo.qtable0[i] >> 24) & 0xFF;
                    }
                    for (int i = 0; i < QTABLE_SIZE; i++)
                    {
                        *p++ = qTableInfo.qtable1[i] & 0xFF;
                        *p++ = (qTableInfo.qtable1[i] >> 8) & 0xFF;
                        *p++ = (qTableInfo.qtable1[i] >> 16) & 0xFF;
                        *p++ = (qTableInfo.qtable1[i] >> 24) & 0xFF;
                    }
                    for (int i = 0; i < QTABLE_SIZE; i++)
                    {
                        *p++ = qTableInfo.qtable2[i] & 0xFF;
                        *p++ = (qTableInfo.qtable2[i] >> 8) & 0xFF;
                        *p++ = (qTableInfo.qtable2[i] >> 16) & 0xFF;
                        *p++ = (qTableInfo.qtable2[i] >> 24) & 0xFF;
                    }
                    err = thePrinter->Send ((const BYTE *) szStr, 3 * sizeof (DWORD) * QTABLE_SIZE);
                    ERRCHECK;

#endif
				    // Start of JPEG Control
				    // 0x8001 JPEG Control register
				    // size - unsigned 32 bit number 0x0000_002C
				    // 11 32 bit words total = 44 bytes
				    // Control  one
				    // Color 0x6614_E001
				    // Mono  0x0000_E005
				    //
				    //VWritePrinter("\x01\x80\x2C\x00\x00\x00", 0x6);
				    BYTE JrCRSeq[] = {0x01,0x80,0x2C,0x00,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCRSeq, sizeof(JrCRSeq));
				    ERRCHECK;

				    if (bGrayScaleSet)
				    {
					    //VWritePrinter("\x05\xE0\x00\x00", 0x4);
					    BYTE JrCR1GSeq[] = {0x05,0xE0,0x00,0x00};
					    err = thePrinter->Send ((const BYTE *)JrCR1GSeq, sizeof(JrCR1GSeq));
					    ERRCHECK;
				    }
				    else
				    {
					    //VWritePrinter("\x01\xE0\x14\x66", 0x4);
					    BYTE JrCR1CSeq[] = {0x01,0xE0,0x14,0x66};
					    err = thePrinter->Send ((const BYTE *)JrCR1CSeq, sizeof(JrCR1CSeq));
					    ERRCHECK;
				    }
        
				    //
				    // Control three
				    // Color 0x0000_0001
				    // Mono  0x0000_0000
				    // bit 0 = convert rgb data
				    // bit 1: 0 = 13 bit precision 
				    //        1 = 14 bit precision
				    //
				    if (bGrayScaleSet)
				    {
					    //VWritePrinter("\x00\x00\x00\x00", 0x4);
					    BYTE JrCR3GSeq[] = {0x00,0x00,0x00,0x00};

					    err = thePrinter->Send ((const BYTE *)JrCR3GSeq, sizeof(JrCR3GSeq));
					    ERRCHECK;
				    }
				    else
				    {
					    //VWritePrinter("\x01\x00\x00\x00", 0x4);
					    BYTE JrCR3CSeq[] = {0x01,0x00,0x00,0x00};
					    err = thePrinter->Send ((const BYTE *)JrCR3CSeq, sizeof(JrCR3CSeq));
					    ERRCHECK;
				    }
        
				    //
				    // CSC matrix
				    // 11  12  13  2.0   0.0    0.0
				    // 21  22  23  2.0  -2.0    0.0
				    // 31  32  33  2.0   0.0   -2.0

				    // Decompression matrix
				    //VWritePrinter("\x00\x20\x00\x00", 0x4);
				    BYTE JrCSC1Seq[] = {0x00,0x20,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC1Seq, sizeof(JrCSC1Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x00\x00\x00", 0x4);
				    BYTE JrCSC2Seq[] = {0x00,0x00,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC2Seq, sizeof(JrCSC2Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x00\x00\x00", 0x4);
				    BYTE JrCSC3Seq[] = {0x00,0x00,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC3Seq, sizeof(JrCSC3Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x20\x00\x00", 0x4);
				    BYTE JrCSC4Seq[] = {0x00,0x20,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC4Seq, sizeof(JrCSC4Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\xE0\x00\x00", 0x4);
				    BYTE JrCSC5Seq[] = {0x00,0xE0,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC5Seq, sizeof(JrCSC5Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x00\x00\x00", 0x4);
				    BYTE JrCSC6Seq[] = {0x00,0x00,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC6Seq, sizeof(JrCSC6Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x20\x00\x00", 0x4);
				    BYTE JrCSC7Seq[] = {0x00,0x20,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC7Seq, sizeof(JrCSC7Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\x00\x00\x00", 0x4);
				    BYTE JrCSC8Seq[] = {0x00,0x00,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC8Seq, sizeof(JrCSC8Seq));
				    ERRCHECK;

				    //VWritePrinter("\x00\xE0\x00\x00", 0x4);
				    BYTE JrCSC9Seq[] = {0x00,0xE0,0x00,0x00};
				    err = thePrinter->Send ((const BYTE *)JrCSC9Seq, sizeof(JrCSC9Seq));
				    ERRCHECK;
                } // if (!HPLJJRCompress)
            }     // if (....bStartPageNotSent)


			BYTE         *pbTemp;
			HPLJBITMAP   jpegGrayBitmap;

			jpegGrayBitmap.pvBits = NULL;
			//
			// Convert 24bpp Gray to 8bpp Gray.
			// JPEG takes K 8bpp gray data. We are using two different buffers for these.
			//
			if(bGrayScaleSet && m_eCompressMode == COMPRESS_MODE_JPEG)
			{
				pbTemp = (BYTE*)m_SourceBitmap.pvBits;
        
				jpegGrayBitmap.pvBits = new BYTE[m_SourceBitmap.cjBits / 3];

				for(long i = 0, j=0; i < (long)m_SourceBitmap.cjBits; i += 3, j++)
				{
					pbTemp[i] = ConvertToGrayMacro(pbTemp[i], pbTemp[i + 1], pbTemp[i + 2] );
					pbTemp[i]	  = 255 - pbTemp[i];
					pbTemp[i + 1] = pbTemp[i];
					pbTemp[i + 2] = pbTemp[i];

					jpegGrayBitmap.pvBits[j] = pbTemp[i];
				}
			}

			if (m_DestBitmap.pvBits)
            {
				delete m_DestBitmap.pvBits;
				m_DestBitmap.pvBits = NULL;
			}
			//
			// JPEG grayscale specific operations are done here.
			//
			if(bGrayScaleSet && m_eCompressMode == COMPRESS_MODE_JPEG)
			{
				jpegGrayBitmap.cjBits = m_SourceBitmap.cjBits / 3;
				jpegGrayBitmap.bitmapInfo.bmiHeader.biSizeImage = m_SourceBitmap.bitmapInfo.bmiHeader.biSizeImage / 3;
				jpegGrayBitmap.bitmapInfo.bmiHeader.biBitCount = 8;
				jpegGrayBitmap.bitmapInfo.bmiHeader.biWidth = m_SourceBitmap.bitmapInfo.bmiHeader.biWidth;
				jpegGrayBitmap.bitmapInfo.bmiHeader.biHeight = m_SourceBitmap.bitmapInfo.bmiHeader.biHeight;
			}
			if (bGrayScaleSet && m_eCompressMode == COMPRESS_MODE_JPEG)
			{
				bRet = Compress (&jpegGrayBitmap, &m_DestBitmap, &qTableInfo,bGrayScaleSet);
			}
			else
			{
				bRet = Compress (&m_SourceBitmap, &m_DestBitmap, &qTableInfo,bGrayScaleSet);
			}

			m_SourceBitmap.cjBits = 0;
			m_SourceBitmap.bitmapInfo.bmiHeader.biWidth = 0;
			m_SourceBitmap.bitmapInfo.bmiHeader.biHeight = 0;

			memset(m_SourceBitmap.pvBits, 0xFF, inputsize * MOJAVE_STRIP_HEIGHT);

			if (jpegGrayBitmap.pvBits)
			{
				delete jpegGrayBitmap.pvBits;
				jpegGrayBitmap.pvBits = NULL;
			}
			m_lPrinterRasterRow += MOJAVE_STRIP_HEIGHT;
	
			m_lCurrCDRasterRow = 0;
			iRastersReady = 1;

			((LJJetReady*)thePrinter)->phLJJetReady->SetLastBand(FALSE);
		}
		else
		{
			return FALSE;
		}
	}

	return TRUE;
} //Process

BYTE* ModeJPEG::NextOutputRaster(COLORTYPE color)
// since we return 1-for-1, just return result first call
{
	if (iRastersReady==0)
		return (BYTE*)NULL;

	if (color == COLORTYPE_BLACK)
	{
		return (BYTE*)NULL;
	}
	else
	{
		iRastersReady=0;
		return m_DestBitmap.pvBits;
	}
}

unsigned int ModeJPEG::GetOutputWidth(COLORTYPE  color)
{
	if (color == COLORTYPE_COLOR)
	{
		return m_DestBitmap.cjBits;
	}
	else
	{
		return 0;
	}
} //GetOutputWidth

APDK_END_NAMESPACE

#endif  // defined APDK_LJJetReady