summaryrefslogtreecommitdiff
path: root/lib/epson-escpage-mono.c
blob: beb924661183702a7f144efb1a52e4f26aab82b5 (plain)
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
/*_______________________________  epson-escpage-mono.c   ______________________________*/

/*       1         2         3         4         5         6         7         8        */
/*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
/*******************************************|********************************************/
/*
 *   Copyright (c) 2010  Seiko Epson Corporation                 All rights reserved.
 *
 *   Copyright protection claimed includes all forms and matters of
 *   copyrightable material and information now allowed by statutory or judicial
 *   law or hereinafter granted, including without limitation, material generated
 *   from the software programs which are displayed on the screen such as icons,
 *   screen display looks, etc.
 *
 */

/*------------------------------------  Includes   -------------------------------------*/
/*******************************************|********************************************/
#include "epson-escpr-err.h"
#include "epson-escpr-media.h"
#include "epson-escpr-mem.h"
#include "epson-escpage.h"
#include "epson-escpage-mono.h"

/*-----------------------------  Local Macro Definitions -------------------------------*/
/*******************************************|********************************************/
#ifdef EPS_LOG_MODULE_PAGE
#define EPS_LOG_MODULE	EPS_LOG_MODULE_PAGE
#else
#define EPS_LOG_MODULE	0
#endif

/*--------------------------------  ESC/PAGE commands ----------------------------------*/
/*******************************************|********************************************/
const static EPS_UINT8 sbCR[]       = {0x0D};											/* Carriage return "CR" */
const static EPS_UINT8 sbFF[]		= {0x0C};											/* Form Feed "FF" */
const static EPS_UINT8 sbIP[]		= {0x1D, 'r', 'h', 'E'};							/* Initialize printer */
const static EPS_UINT8 sbSU150[]    = {0x1D, '0', ';', '.', '4', '8', 'm', 'u', 'E'};	/* Set unit to 150 */
const static EPS_UINT8 sbSU300[]    = {0x1D, '0', ';', '.', '2', '4', 'm', 'u', 'E'};	/* Set unit to 300 */
const static EPS_UINT8 sbSU600[]    = {0x1D, '0', ';', '.', '1', '2', 'm', 'u', 'E'};	/* Set unit to 600 */

const static EPS_UINT8 sbPSSL[]     = {0x1D, '3', '0', 'p', 's', 'E'};					/* Set Letter paper */
const static EPS_UINT8 sbPSSA4[]	= {0x1D, '1', '4', 'p', 's', 'E'};					/* Set A4 paper */
const static EPS_UINT8 sbPSSB5[]    = {0x1D, '2', '5', 'p', 's', 'E'};					/* Set B5 paper */
const static EPS_UINT8 sbPSSLE[]	= {0x1D, '3', '2', 'p', 's', 'E'};					/* Set Legal paper */
const static EPS_UINT8 sbPSSA3[]	= {0x1D, '1', '3', 'p', 's', 'E'};					/* Set A3 paper */
const static EPS_UINT8 sbPSSB4[]    = {0x1D, '2', '4', 'p', 's', 'E'};					/* Set B4 paper */
const static EPS_UINT8 sbPSSPS[]    = {0x1D, '3', '8', 'p', 's', 'E'};					/* Set B4 paper */

const static EPS_UINT8 sbPTE0[]		= {0x1D, '0', 'p', 't', 'E'};						/* select paper type 0:other */
const static EPS_UINT8 sbPTE1[]		= {0x1D, '1', 'p', 't', 'E'};						/* select paper type 1:plane */

const static EPS_UINT8 sbCMS[]      = {0x1D, '1', 'c', 'm', 'E'};						/* Clip Mode Set */
const static EPS_UINT8 sbSDS150[]   = {0x1D, '0', ';', '1', '5', '0', ';', '1', '5', '0', 'd', 'r', 'E'};	/* Select Dot Size(150) */
const static EPS_UINT8 sbSDS300[]   = {0x1D, '0', ';', '3', '0', '0', ';', '3', '0', '0', 'd', 'r', 'E'};	/* Select Dot Size(300) */
const static EPS_UINT8 sbSDS600[]   = {0x1D, '0', ';', '6', '0', '0', ';', '6', '0', '0', 'd', 'r', 'E'};	/* Select Dot Size(600) */

const static EPS_UINT8 sbMMS[]		= {0x1D, '0', 'm', 'm', 'E'};						/* Select Page memory mode */
const static EPS_UINT8 sbPDS[]      = {0x1D, '0', 'p', 'o', 'E'};						/* Set paper direction to normal */
const static EPS_UINT8 sbACRLFS[]	= {0x1D, '0', 'a', 'l', 'f', 'P'};					/* Does not perform auto carriage return */
const static EPS_UINT8 sbAFFS[]		= {0x1D, '0', 'a', 'f', 'f', 'P'};					/* Does not perform auto Form Feed */
const static EPS_UINT8 sbABPS[]		= {0x1D, '0', 'a', 'b', 'P'};						/* Set current position to 0 after bit image draw */
const static EPS_UINT8 sbSARGAS[]	= {0x1D, '0', 's', 'a', 'r', 'G'};					/* Set Absolute Graph Coordinate mode */
const static EPS_UINT8 sbPMPPS[]	= {0x1D, '1', 'p', 'm', 'P'};						/* Set current move mode to print pitch mode */

const static EPS_UINT8 sbSPRCS[]	= {0x1D, '1', '6', '0', 'i', 's', 'E'};				/* Screen Pattern Record Count set to 160 */
const static EPS_UINT8 sbFASCS[]	= {0x1D, '7', 'i', 'a', 'F'};						/* Font Attribute Store Count set to 7 */
const static EPS_UINT8 sbCPSCS[]	= {0x1D, '5', 'i', 'p', 'P'};						/* Current Position Store Count set to 5 */
const static EPS_UINT8 sbMRCS[]		= {0x1D, '1', '6', '0', 'i', 'm', 'M'};				/* Macro Record Store Count set to 160 */
const static EPS_UINT8 sbBIRCS[]	= {0x1D, '2', '3', '2', '8', 'i', 'b', 'I'};		/* Bit Image Count Store Count set to 2328 */
const static EPS_UINT8 sbBIOS[]		= {0x1D, '0', 'b', 'o', 'P'};						/* Set the bit image drawing offset to 0 */
const static EPS_UINT8 sbILGS[]		= {0x1D, '1', '6', '0', 'i', 'l', 'G'};				/* Set line type record count to 160 */
const static EPS_UINT8 sbTSESS[]	= {0x1D, '1', 't', 's', 'E'};						/* Set Screen Mode */
const static EPS_UINT8 sbSPES[]		= {0x1D, '1', ';', '0', ';', '1', '0', '0', 's', 'p', 'E'};	/* Set Screen Pattern */
const static EPS_UINT8 sbOWES[]		= {0x1D, '2', 'o', 'w', 'E'};						/* Set Superimpose Mode */
const static EPS_UINT8 sbBCI1S[]	= {0x1D, '1', 'b', 'c', 'I'};						/* Select Data compression type #1 */
const static EPS_INT8 sbBID[]      = "\x1D""%d;%d;1;0bi{I";								/* Output data */

/*-----------------------------  Local Macro Definitions -------------------------------*/
/*******************************************|********************************************/
#define EPS_EJL_LINEMAX				(256)
#define EPS_PAGE_CMDBUFF_SIZE		EPS_EJL_LINEMAX

#define EMIT_TIFF_REPEAT(n, x)          \
{                                       \
    nRowTIFF += 3;                      \
    if (RowTIFF)                        \
    {                                   \
		*RowTIFF++ = x;                 \
		*RowTIFF++ = x;                 \
		*RowTIFF++ = (EPS_UINT8)n;		\
    }                                   \
}

#define EMIT_TIFF_LITERAL(n, p)         \
{                                       \
    nRowTIFF += n;                      \
    if (RowTIFF)                        \
    {                                   \
        memcpy(RowTIFF, p, n);          \
        RowTIFF += n;                   \
    }                                   \
}

#define TIFF_MAXIMUM_LITERAL 128
#define TIFF_MAXIMUM_REPEAT  129

#define CALCULATE_INTENSITY(r,g,b)  (((((b)<<8) * 11)/100 + (((g)<<8) * 59)/100 + (((r)<<8) * 30)/100)/256)
#define MAX_8           ((1 << 8) - 1)
#define DOT_K           100
#define E_MAX           (1 << 13)
#define E_MID           (E_MAX >> 1)

/* The following table is used to convert a from an intensity we desire	*/
/* to an intensity to tell the device. It is calibrated for the			*/
/* error-diffusion code.												*/
const EPS_UINT8 Intensity2Intensity[256] = 
{
     0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  2,  4,  5,  6,  7,  8,
     9,  9, 10, 10, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16,
    16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22,
    22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27,
    27, 28, 28, 28, 29, 30, 31, 31, 32, 33, 34, 35, 36, 37, 38, 39,
    40, 41, 41, 42, 43, 44, 44, 45, 46, 47, 47, 48, 48, 49, 49, 49,
    50, 50, 50, 51, 51, 51, 52, 52, 52, 53, 53, 54, 54, 54, 55, 55,
    55, 56, 56, 56, 57, 57, 58, 59, 59, 60, 60, 61, 61, 62, 63, 64,
    64, 65, 65, 65, 66, 66, 67, 67, 68, 68, 69, 70, 70, 71, 72, 73,
    74, 75, 76, 76, 77, 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,103,104,
   105,106,108,109,110,112,113,114,115,116,118,120,122,124,126,128,
   130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,159,
   160,162,164,165,166,168,170,172,173,174,176,178,180,182,184,186,
   188,190,192,194,196,198,200,202,204,206,208,210,212,214,216,218,
   220,222,224,226,228,230,232,234,236,238,240,242,244,248,252,255 
};

/* Used in color correction */
const static EPS_INT32 BitMask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };


/*---------------------------  Data Structure Declarations   ---------------------------*/
/*******************************************|********************************************/
/* Band data */
typedef struct _tagEPS_PAGE_BAND_M_ {
    EPS_INT32   WidthInBytes;
    EPS_INT32   WidthInPixels;
	EPS_INT32	currentWidthInPixels;
    EPS_UINT8	*pRasterData;
    EPS_UINT8	*pIntensity;
    EPS_INT32   encDataSize;
    EPS_UINT8	*pEncData;
	EPS_UINT8	*pZeroRow;
}EPS_PAGE_BAND_M;

/* Error Diffusion */
typedef struct tagHT_PARAM{
	EPS_INT32	iDK;
	EPS_INT16	*pEK;
	EPS_INT32	iOrder;
	EPS_INT32	iScanPels;
}   HT_PARAM;


/*----------------------------  ESC/P-R Lib Global Variables  --------------------------*/
/*******************************************|********************************************/
extern EPS_CMN_FUNC		epsCmnFnc;
extern EPS_PRINT_JOB	printJob;

/*----------------------------------   Local Variables  --------------------------------*/
/*******************************************|********************************************/
static EPS_PAGE_BAND_M		band;
static EPS_COMMAND_BUFFER	cmdBuf;
static EPS_UINT8*			Palette2DeviceIntensity = NULL;
static EPS_INT32			iRow = 0;
static EPS_INT32			iNumBytes = 0;
static EPS_INT32			iNumDots = 0;
static EPS_INT32			pageCount = 0;	/* Current Page number */

#if EPS_PAGE_HT_ERR_DIFFUSION
HT_PARAM	htParam;
#else
EPS_UINT8 * pP = 0;
EPS_INT32 iOrder;
EPS_INT32 iScanPels;
#endif

/*--------------------------  Local Functions Declaration   ----------------------------*/
/*******************************************|********************************************/
static EPS_ERR_CODE CmdBuffInit		(EPS_COMMAND_BUFFER *pCmdBuff);
static EPS_ERR_CODE CmdBuffGrow		(EPS_COMMAND_BUFFER *pCmdBuff, EPS_INT32 addSize);
static void			CmdBuffTerm		(EPS_COMMAND_BUFFER *pCmdBuff);

static void			ConvertBYTE2Intensity	(const EPS_BANDBMP* pInBmp, EPS_PAGE_BAND_M *pBand);
static EPS_ERR_CODE ConvertPaletteToIntensity(EPS_UINT16 paletteSize, EPS_UINT8 *paletteData);

static EPS_ERR_CODE BandInit		(EPS_PAGE_BAND_M *pBand, EPS_INT32 widthInPixels);
static void			BandTerm		(EPS_PAGE_BAND_M* pBand);
static void			BandEncode		(EPS_PAGE_BAND_M* pBand);
static EPS_ERR_CODE BandEmit		(EPS_PAGE_BAND_M *pBand, EPS_INT32 iNByte, EPS_INT32 iNDot);	

static EPS_ERR_CODE HT_Init			(EPS_INT32 WidthPixels);
static void			HT_End			(void);
static void			HT_StartPage	(void);
static void			HT_Scan			(EPS_UINT8 *Con, EPS_UINT8 *Bin, EPS_INT32 widthInPixels);

static EPS_INT32 DoDeltaRow			(EPS_UINT8 *Row, EPS_INT32 nRow, EPS_UINT8 *RowDeltaRow, EPS_UINT8 *Seed);


/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
/*--------------------              Public Functions               ---------------------*/
/*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageAllocBuffer_M()			                                        */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*                                                                                      */
/* Description:                                                                         */
/*      Allocate buffer for ESC/Page Job.												*/
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageAllocBuffer_M(void)
{
	EPS_ERR_CODE	ret = EPS_ERR_NONE;

	EPS_LOG_FUNCIN

	ret = BandInit(&band, printJob.printableAreaWidth);
	if ( EPS_ERR_NONE != ret ) {
		EPS_RETURN( ret )
	}
	
	if (printJob.attr.colorPlane == EPS_CP_256COLOR){
		ret = ConvertPaletteToIntensity(printJob.attr.paletteSize, printJob.attr.paletteData);
		if ( EPS_ERR_NONE != ret ) {
			EPS_RETURN( ret )
		}
	}

	/* Halftoning Initialization */
	ret = HT_Init( printJob.printableAreaWidth );
	if ( EPS_ERR_NONE != ret ) {
		EPS_RETURN( ret )
	}

	ret = CmdBuffInit(&cmdBuf);

	EPS_RETURN( ret )
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageRelaseBuffer_M()		                                        */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      N/A                                                                             */
/*                                                                                      */
/* Description:                                                                         */
/*      Free buffer for ESC/Page Job.			                                        */
/*                                                                                      */
/*******************************************|********************************************/
void pageRelaseBuffer_M(void)
{
 	EPS_LOG_FUNCIN

	BandTerm(&band);
	
	EPS_SAFE_RELEASE( Palette2DeviceIntensity );

	CmdBuffTerm(&cmdBuf);

	/* Halftoning Ending process */
	HT_End();

	EPS_RETURN_VOID
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageStartJob_M()													*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      Send ESC/Page start job commands.				                                */
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageStartJob_M(void)
{
	EPS_ERR_CODE	ret = EPS_ERR_NONE;
	EPS_UINT32      retBufSize = 0;

	EPS_LOG_FUNCIN

#define pgStartJob_ADDCMD(CMD) {							\
		ret = CmdBuffGrow(&cmdBuf, sizeof(CMD) );			\
		if( EPS_ERR_NONE != ret){EPS_RETURN( ret )}			\
		memcpy(cmdBuf.p + cmdBuf.len, CMD, sizeof(CMD));	\
		cmdBuf.len += sizeof(CMD);							\
	}

	cmdBuf.len = 0;	/* reset */

	ret = ejlStart(&cmdBuf, CmdBuffGrow);
	if( EPS_ERR_NONE != ret){
		EPS_RETURN( ret )
	}

    /* Step 1 - Start Job */
    /* Initialize the printer. */
	pgStartJob_ADDCMD(sbIP)

    /* Set the minimum Unit Setting (600 or 300 resolution)	 */
	/* Dot Pattern Resolution Selection						 */
	if(       EPS_IR_600X600 == printJob.attr.inputResolution) {
		pgStartJob_ADDCMD(sbSU600);
		pgStartJob_ADDCMD(sbSDS600);
	} else if(EPS_IR_300X300 == printJob.attr.inputResolution) {	
		pgStartJob_ADDCMD(sbSU300);
		pgStartJob_ADDCMD(sbSDS300);
	}else{
		pgStartJob_ADDCMD(sbSU150);
		pgStartJob_ADDCMD(sbSDS150);
	}

	/* Set Memory Mode Selection to Page memory mode */
	pgStartJob_ADDCMD(sbMMS);

	/* Set Screen Record Count */
	pgStartJob_ADDCMD(sbSPRCS);
	
	/* Set Font Attribute Store Count */
	pgStartJob_ADDCMD(sbFASCS);

	/* Set Current Position Store Count */
	pgStartJob_ADDCMD(sbCPSCS);

	/* Set Macro Record Store Count */
	pgStartJob_ADDCMD(sbMRCS);

	/* Set Bit Image Record Store Count */
	pgStartJob_ADDCMD(sbBIRCS);

	/* Set Paper Size */
	switch ( printJob.attr.mediaSizeIdx ) {
	case EPS_MSID_A4:		pgStartJob_ADDCMD(sbPSSA4);	break;
	case EPS_MSID_B5:		pgStartJob_ADDCMD(sbPSSB5);	break;
	case EPS_MSID_B4:		pgStartJob_ADDCMD(sbPSSB4);	break;	
	case EPS_MSID_A3:		pgStartJob_ADDCMD(sbPSSA3);	break;
	case EPS_MSID_LETTER:	pgStartJob_ADDCMD(sbPSSL);	break;
	case EPS_MSID_LEGAL:	pgStartJob_ADDCMD(sbPSSLE);	break;
	case EPS_MSID_POSTCARD:	pgStartJob_ADDCMD(sbPSSPS);	break;
	default:				pgStartJob_ADDCMD(sbPSSA4);	break;
	}

	/* select paper type */
	if(printJob.attr.mediaTypeIdx == EPS_MTID_PLAIN){
		pgStartJob_ADDCMD(sbPTE1);
	}else{
		pgStartJob_ADDCMD(sbPTE0);
	}

	/* Set paper direction */
	pgStartJob_ADDCMD(sbPDS);

	ret = ejlPageEsc(&cmdBuf, CmdBuffGrow);
	if( EPS_ERR_NONE != ret){
		EPS_RETURN( ret )
	}

	/* Set Clip Mode */
	pgStartJob_ADDCMD(sbCMS);
	
	/* Does not perform automatic carriage return line feed */
	pgStartJob_ADDCMD(sbACRLFS);

	/* Does not perform automatic Form Feed */
	pgStartJob_ADDCMD(sbAFFS);

	/* Set the bit image drawing offset to 0 */
	pgStartJob_ADDCMD(sbBIOS);

	/* Set current position to 0 after Bit image draw */
	pgStartJob_ADDCMD(sbABPS);

	/* Set line type record count to 160 */
	pgStartJob_ADDCMD(sbILGS);

    /* Step 4 - Adjust Vertical Print Position (if necessary) */
    /* CR */
    pgStartJob_ADDCMD(sbCR);
    
    /* Select compression type */
	pgStartJob_ADDCMD(sbBCI1S);

	/* Set Absolute graph coordinate mode */
	pgStartJob_ADDCMD(sbSARGAS);

	/* Set current position move mode to print pitch */
	pgStartJob_ADDCMD(sbPMPPS);

	/* Set Screen Mode */
	pgStartJob_ADDCMD(sbTSESS);

	/* Set Screen Pattern */
	pgStartJob_ADDCMD(sbSPES);

	/* Set Screen Pattern */
	pgStartJob_ADDCMD(sbOWES);

	ret = SendCommand((EPS_UINT8*)cmdBuf.p, cmdBuf.len, &retBufSize, TRUE);

    pageCount = 0;

	EPS_RETURN( ret )
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageEndJob_M()														*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      Send ESC/Page end job commands.					                                */
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageEndJob_M()
{
	EPS_ERR_CODE	ret = EPS_ERR_NONE;           /* Return status of internal calls  */
	EPS_UINT32      retBufSize = 0;

	EPS_LOG_FUNCIN

	cmdBuf.len = 0;	/* reset */

	/* Check empty document */
	if( iRow != 0 ) {
		/* Formfeed (FF) */
		memcpy((EPS_INT8*)cmdBuf.p, sbFF, sizeof(sbFF));
		cmdBuf.len += sizeof(sbFF);

		/* Send command to initialize the printer. */
		memcpy((EPS_INT8*)cmdBuf.p+cmdBuf.len, sbIP, sizeof(sbIP));
		cmdBuf.len += sizeof(sbIP);
	}

	/*** Make EJL Command ***/
	ret = ejlEnd(&cmdBuf, CmdBuffGrow, pageCount);
	if( EPS_ERR_NONE != ret){
		EPS_RETURN( ret )
	}

	ret = SendCommand((EPS_UINT8*)cmdBuf.p, cmdBuf.len, &retBufSize, TRUE);

	EPS_RETURN( ret )
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageStartPage_M()													*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      Send ESC/Page start page commands.					                            */
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageStartPage_M()
{
	EPS_ERR_CODE ret = EPS_ERR_NONE;
	EPS_UINT32  retBufSize = 0;

	EPS_LOG_FUNCIN

	HT_StartPage();
    iRow = 0;

	if (pageCount++) {

		cmdBuf.len = 0;	/* reset */

		memcpy(cmdBuf.p, sbFF, sizeof(sbFF));
		cmdBuf.len += sizeof(sbFF);

		ret = ejlPageEsc(&cmdBuf, CmdBuffGrow);
		if( EPS_ERR_NONE != ret){
			EPS_RETURN( ret )
		}

		ret = SendCommand((EPS_UINT8*)cmdBuf.p, cmdBuf.len, &retBufSize, TRUE);
    }

	EPS_RETURN( ret );
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageColorRow_M()													*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      Send ESC/Page raster commands.						                            */
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageColorRow_M(        
						  
		const EPS_BANDBMP*  pInBmp,
        EPS_RECT*           pBandRec

){
 	EPS_ERR_CODE	ret = EPS_ERR_NONE;

	EPS_LOG_FUNCIN

    if( (EPS_UINT32)(pBandRec->right - pBandRec->left) <= printJob.printableAreaWidth){
        band.currentWidthInPixels = (EPS_UINT16)(pBandRec->right - pBandRec->left);
    } else{
        band.currentWidthInPixels = (EPS_UINT16) printJob.printableAreaWidth;
    }

	ConvertBYTE2Intensity( pInBmp, &band );

    memset( band.pRasterData, 0, band.WidthInBytes);
	HT_Scan( band.pIntensity, (EPS_UINT8*)band.pRasterData, band.currentWidthInPixels);

	iNumBytes = 0;
	iNumDots = 0;

	BandEncode(&band);

    if( band.encDataSize ) {
		ret = BandEmit(&band, iNumBytes, iNumDots);
    }

	iRow++;

	EPS_RETURN( ret )
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   pageSendLeftovers_M()												*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* N/A																					*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_INVALID_CALL            - This call was unnecessary                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      send leftovers data.                                                            */
/*                                                                                      */
/*******************************************|********************************************/
EPS_ERR_CODE pageSendLeftovers_M(
							   
		   void
							   
){
	EPS_ERR_CODE	ret = EPS_ERR_NONE;
	EPS_UINT32      retBufSize = 0;

	EPS_LOG_FUNCIN

	if( NULL != printJob.contData.sendData && 0 < printJob.contData.sendDataSize){
		/* send command */
		ret = SendCommand(printJob.contData.sendData, 
								printJob.contData.sendDataSize, &retBufSize, TRUE);
	} else{
		ret = EPS_ERR_INVALID_CALL;
	}
	
 	EPS_RETURN( ret )
}


/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
/*--------------------               Local Functions               ---------------------*/
/*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   CmdBuffInit()														*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pCmdBuff     EPS_COMMAND_BUFFER*     I: command buffer strcture                      */
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*                                                                                      */
/* Description:                                                                         */
/*      Allocate command buffer.			                                            */
/*                                                                                      */
/*******************************************|********************************************/
static EPS_ERR_CODE CmdBuffInit(
								
		EPS_COMMAND_BUFFER *pCmdBuff
		
){
	pCmdBuff->size = EPS_EJL_LINEMAX*2;
	pCmdBuff->p = (EPS_INT8*)EPS_ALLOC(pCmdBuff->size);
	if( NULL == pCmdBuff->p){
		return EPS_ERR_MEMORY_ALLOCATION;
	}

	pCmdBuff->len = 0;

	return EPS_ERR_NONE;
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   CmdBuffGrow()														*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pCmdBuff     EPS_COMMAND_BUFFER*     I: command buffer strcture                      */
/* addSize      EPS_INT32			    I: growing size                                 */
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*                                                                                      */
/* Description:                                                                         */
/*      ReAllocate command buffer.			                                            */
/*                                                                                      */
/*******************************************|********************************************/
static EPS_ERR_CODE CmdBuffGrow(
								
		EPS_COMMAND_BUFFER *pCmdBuff, 
		EPS_INT32			addSize
		
){
	EPS_INT32	block, sizeNew;

	if(pCmdBuff->size < pCmdBuff->len + addSize){
		block = (((pCmdBuff->len+addSize) / EPS_PAGE_CMDBUFF_SIZE) + 1);
		sizeNew = block * EPS_PAGE_CMDBUFF_SIZE;
		pCmdBuff->p =  memRealloc(pCmdBuff->p, pCmdBuff->size, sizeNew);
		if( NULL == pCmdBuff->p){
			return EPS_ERR_MEMORY_ALLOCATION;
		}
		pCmdBuff->size = sizeNew;
	}

	return EPS_ERR_NONE;
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   CmdBuffTerm()														*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pCmdBuff     EPS_COMMAND_BUFFER*     I: command buffer strcture                      */
/*                                                                                      */
/* Return value:                                                                        */
/* N/A																					*/
/*                                                                                      */
/* Description:                                                                         */
/*      Free command buffer.				                                            */
/*                                                                                      */
/*******************************************|********************************************/
static void CmdBuffTerm		(
									 
		EPS_COMMAND_BUFFER	*pCmdBuff
									 
){
	EPS_SAFE_RELEASE(pCmdBuff->p);
	pCmdBuff->len = 0;
	pCmdBuff->size = 0;
	pCmdBuff->pExtent = NULL;
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   ConvertBYTE2Intensity()												*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* paletteSize  EPS_UINT16			    I: Palette Size									*/
/* paletteData  EPS_UINT8*			    I: Palette Size									*/
/*                                                                                      */
/* Return value:                                                                        */
/* N/A																					*/
/*                                                                                      */
/* Description:                                                                         */
/*      Convert palette to Intensity.													*/
/*                                                                                      */
/*******************************************|********************************************/
static void ConvertBYTE2Intensity(
								  
		const EPS_BANDBMP* pInBmp, 
		EPS_PAGE_BAND_M *pBand
		
){
	EPS_UINT32 idx;
	EPS_INT32 i, j, widthBytes;

	EPS_LOG_FUNCIN;

	widthBytes = pBand->currentWidthInPixels * printJob.bpp;
	j = 0;
	if(printJob.bpp == 3){
		for (i = 0; i < widthBytes; i += printJob.bpp) {
			/* idx = CALCULATE_INTENSITY(pInBmp->bits[i], pInBmp->bits[i+1], pInBmp->bits[i+2]);*/
			idx = pInBmp->bits[i];
			idx = 255 - idx;
			if (idx > 255) idx = 255;

			pBand->pIntensity[j++] = Intensity2Intensity[idx];

			if(j >= pBand->WidthInPixels){
				break;
			}
	   }
	} else{
		for (i = 0; i < widthBytes; i += printJob.bpp) {
			idx = pInBmp->bits[i];
			pBand->pIntensity[j++] = Palette2DeviceIntensity[idx];
			if(j >= pBand->WidthInPixels){
				break;
			}
		}
	}
 
   EPS_RETURN_VOID
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   ConvertPaletteToIntensity()											*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* paletteSize  EPS_UINT16			    I: Palette Size									*/
/* paletteData  EPS_UINT8*			    I: Palette Size									*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*                                                                                      */
/* Description:                                                                         */
/*      Convert palette to Intensity.													*/
/*                                                                                      */
/*******************************************|********************************************/
static EPS_ERR_CODE ConvertPaletteToIntensity(

		EPS_UINT16  paletteSize,
		EPS_UINT8	*paletteData
					   
){
	EPS_UINT32 idx;
	EPS_UINT32 i, j;
	EPS_LOG_FUNCIN;

	Palette2DeviceIntensity = (EPS_UINT8*)EPS_ALLOC( (paletteSize/3)*sizeof(EPS_UINT8) );
	if (!Palette2DeviceIntensity) {
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
	}
	j = 0;
    for (i = 0; i < paletteSize; i += 3) {
		/* idx = CALCULATE_INTENSITY(paletteData[i], paletteData[i+1], paletteData[i+2]); */
		idx = paletteData[i];
		idx = 255 - idx;
        if (idx > 255) idx = 255;
		Palette2DeviceIntensity[j++] = Intensity2Intensity[idx];
	}

	EPS_RETURN( EPS_ERR_NONE )
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   BandInit()															*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pBand		EPS_PAGE_BAND_M*		I: band data strcture		                    */
/* widthInPixels EPS_INT32			    I: width in Pixels								*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*                                                                                      */
/* Description:                                                                         */
/*      Allocate band data buffers.														*/
/*                                                                                      */
/*******************************************|********************************************/
static EPS_ERR_CODE BandInit(
							 
		EPS_PAGE_BAND_M*	pBand, 
		EPS_INT32		widthInPixels
		
){
	EPS_ERR_CODE	ret = EPS_ERR_NONE;
	EPS_INT32 maxEpsonEncodedData;

	EPS_LOG_FUNCIN;

	memset(pBand, 0, sizeof(pBand));

    pBand->WidthInPixels = widthInPixels;
    pBand->WidthInBytes = (pBand->WidthInPixels+7)/8;

    /* We leave room for an entire TIFF literal -plus- room	*/
    /* for the longest possible XFER header (3 bytes).		*/
    maxEpsonEncodedData = 256 + 3 + pBand->WidthInBytes
        + (pBand->WidthInBytes + TIFF_MAXIMUM_LITERAL - 1)/TIFF_MAXIMUM_LITERAL;

    pBand->pRasterData       = (EPS_UINT8*)EPS_ALLOC(pBand->WidthInBytes);
    if (NULL == pBand->pRasterData) {
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
    }

	pBand->pEncData = (EPS_UINT8*)EPS_ALLOC(maxEpsonEncodedData);
    if (NULL == pBand->pEncData) {
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
    }

	pBand->pZeroRow			 = (EPS_UINT8*)EPS_ALLOC(pBand->WidthInBytes);
    if (NULL == pBand->pZeroRow) {
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
    }
    memset(pBand->pZeroRow, 0, pBand->WidthInBytes);

    pBand->pIntensity        = (EPS_UINT8*)EPS_ALLOC(pBand->WidthInPixels);
	if (!pBand->pIntensity){
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
	}

	EPS_RETURN(ret)
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   BandTerm()															*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pBand		EPS_PAGE_BAND_M*		I: band data strcture		                    */
/*                                                                                      */
/* Return value:                                                                        */
/* N/A																					*/
/*                                                                                      */
/* Description:                                                                         */
/*      free band data buffers.															*/
/*                                                                                      */
/*******************************************|********************************************/
static void BandTerm(
					 
		EPS_PAGE_BAND_M* pBand
		
){
	EPS_LOG_FUNCIN

	EPS_SAFE_RELEASE( pBand->pRasterData );
	EPS_SAFE_RELEASE( pBand->pEncData );
	EPS_SAFE_RELEASE( pBand->pZeroRow );
	EPS_SAFE_RELEASE( pBand->pIntensity );

	EPS_RETURN_VOID
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   BandEmit()															*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pBand		EPS_PAGE_BAND_M*		I: band data strcture		                    */
/* pCmdBuff     EPS_COMMAND_BUFFER*     I: command buffer strcture                      */
/* iNByte		EPS_INT32				I: number of data bytes							*/
/* iNDot		EPS_INT32				I: number of data dots							*/
/*                                                                                      */
/* Return value:                                                                        */
/*      EPS_ERR_NONE                    - Success										*/
/*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */
/*      EPS_ERR_OPR_FAIL                - Internal Error                                */
/*      EPS_ERR_COMM_ERROR              - Communication Error                           */
/*      EPS_ERR_PRINTER_ERR_OCCUR       - Printer Error happened                        */
/*                                                                                      */
/* Description:                                                                         */
/*      Emit data to printer.															*/
/*                                                                                      */
/*******************************************|********************************************/
static EPS_ERR_CODE BandEmit(
							 
		EPS_PAGE_BAND_M*		pBand, 
		EPS_INT32			iNByte, 
		EPS_INT32			iNDot
							 
){
	EPS_ERR_CODE	ret = EPS_ERR_NONE;
	EPS_INT8		*p;
	EPS_UINT32      retBufSize = 0;

	EPS_LOG_FUNCIN

	cmdBuf.len = 0;	/* reset */
	p = (EPS_INT8*)cmdBuf.p;

	EPS_DBGPRINT(("Y = %d \r\n", iRow));
	sprintf(p, "\x1D%dY", iRow);
	p += strlen(p);

	/* Number of data bytes	 */
	/* Bit image width		 */
	/* Bit image height		 */
	/* Angle of rotation	 */
	sprintf(p, sbBID, iNByte, iNDot);
	p += strlen(p);

	cmdBuf.len += (EPS_UINT32)(p - cmdBuf.p);	
	ret = CmdBuffGrow(&cmdBuf, pBand->encDataSize);
	if( EPS_ERR_NONE != ret){
		EPS_RETURN( ret )
	}
	p = cmdBuf.p + cmdBuf.len;

	memcpy(p, pBand->pEncData, pBand->encDataSize);
	cmdBuf.len += pBand->encDataSize;

	pBand->encDataSize = 0;

	ret = SendCommand((EPS_UINT8*)cmdBuf.p, cmdBuf.len, &retBufSize, TRUE);

	EPS_RETURN(ret)
}


/*******************************************|********************************************/
/*                                                                                      */
/* Function name:   BandEncode()														*/
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:		Type:					Description:                                    */
/* pBand		EPS_PAGE_BAND_M*		I: band data strcture		                    */
/*                                                                                      */
/* Return value:                                                                        */
/* N/A																					*/
/*                                                                                      */
/* Description:                                                                         */
/*      Do Compression.																	*/
/*                                                                                      */
/*******************************************|********************************************/
static void BandEncode(
					   
		EPS_PAGE_BAND_M* pBand
		
){
	EPS_BOOL isZero = FALSE;
    EPS_INT32 widthInBytes = (pBand->currentWidthInPixels+7)/8;

	EPS_LOG_FUNCIN

	/* Check blank line */
	isZero = (memcmp(pBand->pRasterData, pBand->pZeroRow, widthInBytes) == 0)?TRUE:FALSE;

	/* No compression for blank line */
	if( isZero ) {
		pBand->encDataSize = 0;
	} else{
       	pBand->encDataSize = DoDeltaRow( pBand->pRasterData,
                                    widthInBytes,
                                    pBand->pEncData,
                                    pBand->pZeroRow
                                  );
	}

	EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      HT_Init											   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Halftoning initialization						   */
/*                                                         */
/*=========================================================*/
static EPS_ERR_CODE HT_Init(EPS_INT32 WidthPixels)
{
	EPS_LOG_FUNCIN
	 
	srand(119);

    htParam.pEK			= NULL;
    htParam.iScanPels	= WidthPixels;
    htParam.iOrder		= 5;
    htParam.iDK			= DOT_K * E_MAX / 100;

	htParam.pEK = (EPS_INT16*)EPS_ALLOC((htParam.iScanPels + 2)*sizeof(EPS_INT16));
	if( NULL == htParam.pEK){
		EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )
	}

	EPS_RETURN( EPS_ERR_NONE )
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      HT_End											   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Halftoning Ending process						   */
/*                                                         */
/*=========================================================*/
static void HT_End()
{
EPS_LOG_FUNCIN
	EPS_SAFE_RELEASE( htParam.pEK );
EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      HT_StartPage									   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Halftoning start page process					   */
/*                                                         */
/*=========================================================*/
static void HT_StartPage()
{
	EPS_LOG_FUNCIN

	if (htParam.pEK){
        memset(htParam.pEK, 0, (htParam.iScanPels + 2) * sizeof(EPS_INT16));
	}

	EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      HT_Scan											   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Check halftoning method							   */
/*                                                         */
/*=========================================================*/
static void HT_Scan(EPS_UINT8 *Con, EPS_UINT8 *Bin, EPS_INT32 widthInPixels)
{
/*      Error Diffusion									   */
    EPS_INT32 i, j, k;
    EPS_INT32 iE, iE1, iE3, iE5, iE7, iR;
    EPS_INT32 iCon;
    EPS_INT32 iDot;
	EPS_INT32 iF;
    EPS_INT16 *p;

	EPS_LOG_FUNCIN

	p    = htParam.pEK + 1;
	iDot = htParam.iDK;

    iE1 = 0;
    iE7 = 0;
    iR  = 0;

	i = 0;
	j = 1;
    
    for (k = 0; k < widthInPixels; k++) {
        if (Con[i] == 0) {
        } else if (Con[i] == MAX_8) {
            Bin[i >> 3] |= BitMask[i & 7];
        } else {
            iCon = Con[i] * (E_MAX / (MAX_8 + 1));

            iE = rand() * (iCon - E_MID) / RAND_MAX;
            if (((iR > 0) && (iE > 0)) || ((iR < 0) && (iE < 0))) {
                iE = -iE;
			}
            iR = iE;

            iCon += p[i] + iE7;
            
            if (iCon + iR > E_MID) {
                Bin[i >> 3] |= BitMask[i & 7];
                iE = iCon - iDot;
            } else {
                iE = iCon;
            }

			iF = iE / 16;
            iE3 = iF * 3;
            iE5 = iF * 5;

            p[i]      = iE5 + iE1;
            p[i - j] += iE3;

            iE1 = iF;
            iE7 = iE - iE1 - iE3 - iE5;
        }

        i += j;
    }

    p[i - j] += iE7 + iE1;

    p[0] += p[-1];
    p[htParam.iScanPels - 1] += p[htParam.iScanPels];

    p[-1] = 0;
    p[htParam.iScanPels] = 0;
	
	EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      DoTIFF                                             */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Do TIFF compression                                */
/*                                                         */
/*=========================================================*/
EPS_INT32 DoTIFF(EPS_UINT8 *Row, EPS_INT32 nRow, EPS_UINT8 *RowTIFF)
{
    EPS_INT32 nRowTIFF = 0;      /* Total size of TIFF encoded row. */
    EPS_INT32 nLiteral = 0;      /* Count of literal bytes. */
    EPS_INT32 nRepeat  = 0;      /* Count of repeated byte. */
	EPS_UINT8 ch; 
	EPS_INT32 n;

    /* Load first byte from row into parser. */
    nRepeat++;
	nLiteral++;
    ch = *Row++;

    for( ; nRow--; ch = *Row++ ) {
        if( nLiteral > TIFF_MAXIMUM_LITERAL ) {
            /* Literal part has grown too large, emit the largest literal pattern we can. */
            EMIT_TIFF_LITERAL( TIFF_MAXIMUM_LITERAL, Row-nLiteral );
            nLiteral -= TIFF_MAXIMUM_LITERAL;
        } else if( nRepeat > TIFF_MAXIMUM_REPEAT ) {
            /* Repeated part has grown too large, emit the literal (if present) and then  */
			/* emit the largest repeated pattern we can. */
			EMIT_TIFF_REPEAT( TIFF_MAXIMUM_REPEAT, ch );
            nRepeat -= TIFF_MAXIMUM_REPEAT;
        }
        if( ch == *Row ) {
			if( nLiteral ) {
				if( nLiteral >= 2 ) {
					EMIT_TIFF_LITERAL( nLiteral-1, Row-nLiteral );
				}
				nLiteral = 1;
			}
            nRepeat++;
        } else {
			if( nRepeat >= 2 ) {
				EMIT_TIFF_REPEAT( nRepeat, ch );
				nRepeat = 1;
				nLiteral = 0;
			}
			nLiteral++;
        }
    }
    if( nRepeat == 1 ) {
        nRepeat  = 0;
    }
	if( nLiteral == 1 && nRepeat >= 2 ) {
        nLiteral = 0;
    }

    while (nLiteral) {
        n = nLiteral;
        if (n > TIFF_MAXIMUM_LITERAL) {
            n = TIFF_MAXIMUM_LITERAL;
        }
		if( nRepeat ) {
			EMIT_TIFF_LITERAL(n-1, Row-nLiteral);
		} else {
			EMIT_TIFF_LITERAL(n-1, Row-nLiteral);
		}
        nLiteral -= n;
    }

    while (nRepeat) {
        n = nRepeat;
        if (n > TIFF_MAXIMUM_REPEAT)  {
            n = TIFF_MAXIMUM_REPEAT;
        }
        EMIT_TIFF_REPEAT(n, ch);
        nRepeat -= n;
    }

	return nRowTIFF;
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      DeltaRowEmitXFER                                   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Construct DeltaRow control code and data           */
/*                                                         */
/*=========================================================*/
void DeltaRowEmitXFER(EPS_UINT8 *Row, EPS_INT32 nXFER, EPS_INT32 nXFERTIFF, EPS_INT32 *nRowDeltaRow, EPS_UINT8 **RowDeltaRow)
{
	EPS_LOG_FUNCIN;

    /* Get data length and send before all data of the current row */
    iNumBytes += nXFERTIFF;
	iNumDots += nXFER * 8;

    /* TIFF data for XFER */
    *nRowDeltaRow += nXFERTIFF;
    if (*RowDeltaRow)
    {
        DoTIFF(Row, nXFER, *RowDeltaRow);
        *RowDeltaRow += nXFERTIFF;
    }

	EPS_RETURN_VOID
}

/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      DeltaRowEmitMOVX                                   */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Construct DeltaRow horizontal position movement    */
/*                                                         */
/*=========================================================*/
void DeltaRowEmitMOVX(EPS_INT32 nSkip, EPS_INT32 *nRowDeltaRow, EPS_UINT8 **RowDeltaRow)
{
    EPS_UINT8 N;
    EPS_INT32 num=0;
    EPS_INT32 num1=0;

	EPS_LOG_FUNCIN;

    /* Send repeat "00" instead. Send 255 "00" the most each time */
    if( nSkip == 0 ) {
		EPS_RETURN_VOID
    }
    
    if( nSkip > 255 ) {
        num1 = nSkip - 255;
        num = 255;
    } else {
        num = nSkip;
    }
    *nRowDeltaRow += 3;
    iNumBytes += 3;
	iNumDots += num * 8;

    if (*RowDeltaRow) {
        N = num;
        *((*RowDeltaRow)++) = 0x00;
		*((*RowDeltaRow)++) = 0x00;
		*((*RowDeltaRow)++) = N;
    }
    if( num1 > 255 ) {
        /* Still have more than 255 "00" */
        DeltaRowEmitMOVX(num1, nRowDeltaRow, RowDeltaRow);
    } else {
        if( num1 > 0 ) {
            *nRowDeltaRow += 3;
            iNumBytes += 3;
			iNumDots += num1 * 8;
            if (*RowDeltaRow)
            {
                N = num1;
                *((*RowDeltaRow)++) = 0x00;
				*((*RowDeltaRow)++) = 0x00;
				*((*RowDeltaRow)++) = N;
            }
        }
    }

	EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      DeltaRowEmitReduce                                 */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*                                                         */
/*                                                         */
/*=========================================================*/
void DeltaRowEmitReduce(EPS_UINT8 *Row, EPS_INT32 *nDelta0, EPS_INT32 *nSkip, EPS_INT32 *nDelta1, EPS_INT32 *nRowDeltaRow, EPS_UINT8 **RowDeltaRow)
{
    EPS_INT32 nDelta0TIFF, nDelta1TIFF, nDeltaTIFF, SizeofDSD, SizeofD;

	EPS_LOG_FUNCIN;
	
	if (*nDelta0) {
        /* (DSD) state. */
        nDelta0TIFF = DoTIFF(Row - *nDelta1 - *nSkip - *nDelta0, *nDelta0, 0);
        nDelta1TIFF = DoTIFF(Row - *nDelta1,                     *nDelta1, 0);
        nDeltaTIFF  = DoTIFF(Row - *nDelta1 - *nSkip - *nDelta0, *nDelta0 + *nSkip + *nDelta1, 0);

        SizeofDSD = nDelta0TIFF + nDelta1TIFF + (nDelta0TIFF>15?1:0)
            + (nDelta0TIFF>255?1:0) + (nDelta1TIFF>15?1:0)
            + (nDelta1TIFF>255?1:0) + (*nSkip>7?1:0) + (*nSkip>127?1:0) + 3;
        SizeofD = nDeltaTIFF + (nDeltaTIFF>15?1:0) + (nDeltaTIFF>255?1:0) + 1;

    } else {
        /* (-SD) state. */
        nDelta0TIFF = 0;
        nDelta1TIFF = DoTIFF(Row - *nDelta1, *nDelta1, 0);
        nDeltaTIFF  = DoTIFF(Row - *nDelta1 - *nSkip, *nSkip + *nDelta1, 0);

        SizeofDSD = nDelta1TIFF + (nDelta1TIFF>15?1:0)
            + (nDelta1TIFF>255?1:0) + (*nSkip>7?1:0) + (*nSkip>127?1:0) + 2;
        SizeofD = nDeltaTIFF + (nDeltaTIFF>15?1:0) + (nDeltaTIFF>255?1:0) + 1;
    }

    if (SizeofD <= SizeofDSD) {
        /* Turn the delta/skip/delta into a single delta. */
        *nDelta0 += *nSkip + *nDelta1;
        *nSkip = 0;
        *nDelta1 = 0;

    } else {
        /* Emit a delta, then a skip, and the shift the leftover delta. */
        if (*nDelta0) {
            DeltaRowEmitXFER(Row- *nDelta0 - *nSkip - *nDelta1, *nDelta0, nDelta0TIFF, nRowDeltaRow, RowDeltaRow);
        }
        DeltaRowEmitMOVX(*nSkip, nRowDeltaRow, RowDeltaRow);

        /* Shift remaining delta */
        *nDelta0 = *nDelta1;
        *nSkip = 0;
        *nDelta1 = 0;
    }

	EPS_RETURN_VOID
}


/*=========================================================*/
/*                                                         */
/* Function:                                               */
/*                                                         */
/*      DoDeltaRow                                         */
/*                                                         */
/* Description:                                            */
/*                                                         */
/*      Do DeltaRow compression                            */
/*                                                         */
/*=========================================================*/
static EPS_INT32 DoDeltaRow(EPS_UINT8 *Row, EPS_INT32 nRow, EPS_UINT8 *RowDeltaRow, EPS_UINT8 *Seed)
{
	EPS_INT32 nRowDeltaRow = 0;
    EPS_INT32 nSkip        = 0;
    EPS_INT32 nDelta1      = 0;
    EPS_INT32 nDelta0      = 0;
	EPS_INT32 nDelta0TIFF;

    for ( ; nRow--; Row++, Seed++) {
		if (*Row != *Seed) {
			nDelta1++;
		}
        else
        {
            if (nDelta1)
            {
                if (nDelta0 == 0 && nSkip ==0)
                {
                    nDelta0 = nDelta1;
                    nDelta1 = 0;
                }
                else
                {
                    if (nSkip <= 3)
                    {
                        nDelta0 += nSkip + nDelta1;
                        nSkip = 0;
                        nDelta1 = 0;
                    }
                    else
                    {
                       DeltaRowEmitReduce(Row, &nDelta0, &nSkip, &nDelta1,
                            &nRowDeltaRow, &RowDeltaRow);
                    }
                }
            }
            nSkip++;
        }
    }

    /* Have reached the end of the line. */
    if (nDelta0)
    {
        if (nDelta1)
        {
            DeltaRowEmitReduce(Row, &nDelta0, &nSkip, &nDelta1, &nRowDeltaRow, &RowDeltaRow);
            nDelta0TIFF = DoTIFF(Row-nDelta0, nDelta0, 0);
            DeltaRowEmitXFER(Row-nDelta0, nDelta0, nDelta0TIFF, &nRowDeltaRow, &RowDeltaRow);
        }
        else
        {
            nDelta0TIFF = DoTIFF(Row-nSkip-nDelta0, nDelta0, 0);
            DeltaRowEmitXFER(Row-nDelta0-nSkip, nDelta0, nDelta0TIFF, &nRowDeltaRow, &RowDeltaRow);
        }
    }
    else if (nDelta1)
    {
        if (nSkip)
        {
            DeltaRowEmitReduce(Row, &nDelta0, &nSkip, &nDelta1, &nRowDeltaRow, &RowDeltaRow);
            nDelta0TIFF = DoTIFF(Row-nDelta0, nDelta0, 0);
            DeltaRowEmitXFER(Row-nDelta0, nDelta0, nDelta0TIFF, &nRowDeltaRow, &RowDeltaRow);
        }
        else
        {
            nDelta0TIFF = DoTIFF(Row-nDelta1, nDelta1, 0);
            DeltaRowEmitXFER(Row-nDelta1, nDelta1, nDelta0TIFF, &nRowDeltaRow, &RowDeltaRow);
        }
    }

    return nRowDeltaRow;
}

/*_______________________________  epson-escpage-mono.c   ______________________________*/

/*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
/*       1         2         3         4         5         6         7         8        */
/*******************************************|********************************************/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/***** End of File *** End of File *** End of File *** End of File *** End of File ******/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/