summaryrefslogtreecommitdiffstats
path: root/backend_shinkos6145.c
blob: 199524bf098bdde2e9b5e2304e1f8b442090a0da (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
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
/*
 *   Shinko/Sinfonia CHC-S6145 CUPS backend -- libusb-1.0 version
 *
 *   (c) 2015-2020 Solomon Peachy <pizza@shaftnet.org>
 *
 *   Low-level documentation was provided by Sinfonia.  Thank you!
 *
 *   The latest version of this program can be found at:
 *
 *     http://git.shaftnet.org/cgit/selphy_print.git
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 3 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 *   An additional permission is granted, under the GPLv3 section 7, to combine
 *   and/or redistribute this program with the proprietary libS6145ImageProcess
 *   library, providing you have *written permission* from Sinfonia Technology
 *   Co. LTD to use and/or redistribute that library.
 *
 *   You must still adhere to all other terms of the license to this program
 *   (ie GPLv3) and the license of the libS6145ImageProcess library.
 *
 *   SPDX-License-Identifier: GPL-3.0+ with special exception
 *
 */

#define BACKEND shinkos6145_backend

#include "backend_common.h"
#include "backend_sinfonia.h"

#include <time.h>

#ifndef WITH_DYNAMIC
#warning "No dynamic loading support!"
#endif

/* Image processing library function prototypes */
typedef int (*ImageProcessingFN)(unsigned char *, unsigned short *, void *);
typedef int (*ImageAvrCalcFN)(unsigned char *, unsigned short, unsigned short, unsigned char *);

#define LIB_NAME    "libS6145ImageProcess" DLL_SUFFIX    // Official library
#define LIB_NAME_RE "libS6145ImageReProcess" DLL_SUFFIX // Reimplemented library

enum {
	S_IDLE = 0,
	S_PRINTER_READY_CMD,
	S_PRINTER_SENT_DATA,
	S_FINISHED,
};

/* "Image Correction Parameter" File */
// 128 bytes total, apparently an array of 32-bit values
struct tankParamTable {
	uint32_t trdTankSize;
	uint32_t sndTankSize;
	uint32_t fstTankSize;
	uint32_t trdTankIniEnergy;
	uint32_t sndTankIniEnergy;
	uint32_t fstTankIniEnergy;
	uint32_t trdTrdConductivity;
	uint32_t sndSndConductivity;
	uint32_t fstFstConductivity;
	uint32_t outTrdConductivity;
	uint32_t trdSndConductivity;
	uint32_t sndFstConductivity;
	uint32_t fstOutConductivity;
	uint32_t plusMaxEnergy;
	uint32_t minusMaxEnergy;
	uint32_t plusMaxEnergyPreRead;
	uint32_t minusMaxEnergyPreRead;
	uint32_t preReadLevelDiff;
	uint32_t rsvd[14]; // null?
} __attribute__((packed));

struct shinkos6145_correctionparam {
	uint16_t pulseTransTable_Y[256];   // @0
	uint16_t pulseTransTable_M[256];   // @512
	uint16_t pulseTransTable_C[256];   // @1024
	uint16_t pulseTransTable_O[256];   // @1536

	uint16_t lineHistCoefTable_Y[256]; // @2048
	uint16_t lineHistCoefTable_M[256]; // @2560
	uint16_t lineHistCoefTable_C[256]; // @3072
	uint16_t lineHistCoefTable_O[256]; // @3584

	uint16_t lineCorrectEnvA_Y;        // @4096
	uint16_t lineCorrectEnvA_M;        // @4098
	uint16_t lineCorrectEnvA_C;        // @4100
	uint16_t lineCorrectEnvA_O;        // @4102

	uint16_t lineCorrectEnvB_Y;        // @4104
	uint16_t lineCorrectEnvB_M;        // @4106
	uint16_t lineCorrectEnvB_C;        // @4108
	uint16_t lineCorrectEnvB_O;        // @4110

	uint16_t lineCorrectEnvC_Y;        // @4112
	uint16_t lineCorrectEnvC_M;        // @4114
	uint16_t lineCorrectEnvC_C;        // @4116
	uint16_t lineCorrectEnvC_O;        // @4118

	uint32_t lineCorrectSlice_Y;       // @4120
	uint32_t lineCorrectSlice_M;       // @4124
	uint32_t lineCorrectSlice_C;       // @4128
	uint32_t lineCorrectSlice_O;       // @4132

	uint32_t lineCorrectSlice1Line_Y;  // @4136
	uint32_t lineCorrectSlice1Line_M;  // @4140
	uint32_t lineCorrectSlice1Line_C;  // @4144
	uint32_t lineCorrectSlice1Line_O;  // @4148

	uint32_t lineCorrectPulseMax_Y;    // @4152 [array]
	uint32_t lineCorrectPulseMax_M;    // @4156 [array]
	uint32_t lineCorrectPulseMax_C;    // @4160 [array]
	uint32_t lineCorrectPulseMax_O;    // @4164 [array]

	struct tankParamTable tableTankParam_Y; // @4168
	struct tankParamTable tableTankParam_M; // @4296
	struct tankParamTable tableTankParam_C; // @4424
	struct tankParamTable tableTankParam_O; // @4552

	uint16_t tankPlusMaxEnergyTable_Y[256]; // @4680
	uint16_t tankPlusMaxEnergyTable_M[256]; // @5192
	uint16_t tankPlusMaxEnergyTable_C[256]; // @5704
	uint16_t tankPlusMaxEnergyTable_O[256]; // @6216

	uint16_t tankMinusMaxEnergy_Y[256];     // @6728
	uint16_t tankMinusMaxEnergy_M[256];     // @7240
	uint16_t tankMinusMaxEnergy_C[256];     // @7752
	uint16_t tankMinusMaxEnergy_O[256];     // @8264

	uint16_t printMaxPulse_Y; // @8776
	uint16_t printMaxPulse_M; // @8778
	uint16_t printMaxPulse_C; // @8780
	uint16_t printMaxPulse_O; // @8782

	uint16_t mtfWeightH_Y;    // @8784
	uint16_t mtfWeightH_M;    // @8786
	uint16_t mtfWeightH_C;    // @8788
	uint16_t mtfWeightH_O;    // @8790

	uint16_t mtfWeightV_Y;    // @8792
	uint16_t mtfWeightV_M;    // @8794
	uint16_t mtfWeightV_C;    // @8796
	uint16_t mtfWeightV_O;    // @8798

	uint16_t mtfSlice_Y;      // @8800
	uint16_t mtfSlice_M;      // @8802
	uint16_t mtfSlice_C;      // @8804
	uint16_t mtfSlice_O;      // @8806

	uint16_t val_1;           // @8808 // 1 enables linepreprintprocess
	uint16_t val_2;		  // @8810 // 1 enables ctankprocess
	uint16_t printOpLevel;    // @8812
	uint16_t matteMode;	  // @8814 // 1 for matte

	uint16_t randomBase[4];   // @8816 [use lower byte of each]

	uint16_t matteSize;       // @8824
	uint16_t matteGloss;      // @8826
	uint16_t matteDeglossBlk; // @8828
	uint16_t matteDeglossWht; // @8830

	uint16_t printSideOffset; // @8832
	uint16_t headDots;        // @8834 [always 0x0780, ie 1920. print width

	uint16_t SideEdgeCoefTable[128];   // @8836
	uint8_t  rsvd_2[256];              // @9092, null?
	uint16_t SideEdgeLvCoefTable[256]; // @9348
	uint8_t  rsvd_3[2572];             // @9860, null?

	/* User-supplied data */
	uint16_t width;           // @12432
	uint16_t height;          // @12434
	uint8_t  pad[3948];       // @12436, null.
} __attribute__((packed)); /* 16384 bytes */

/* Structs for printer */
struct s6145_print_cmd {
	struct sinfonia_cmd_hdr hdr;
	uint8_t  id;
	uint16_t count;
	uint16_t columns;
	uint16_t rows;
	uint8_t  media;      /* reserved in docs, but brava21 uses this */
	uint8_t  combo_wait;
	uint8_t  reserved[6];
	uint8_t  unk_1;      /* Brava 21 sets this to 1 */
	uint8_t  method;
	uint8_t  image_avg;
} __attribute__((packed));

#define PARAM_OC_PRINT     0x20
#define PARAM_PAPER_PRESV  0x3d
#define PARAM_DRIVER_MODE  0x3e
#define PARAM_PAPER_MODE   0x3f
#define PARAM_REGION_CODE  0x53 // Brava 21 only?
#define PARAM_SLEEP_TIME   0x54

static const struct sinfonia_param s6145_params[] =
{
	{ PARAM_OC_PRINT, "Overcoat Mode" },
	{ PARAM_PAPER_PRESV, "Paper Preserve Mode" },
	{ PARAM_DRIVER_MODE, "Driver Mode/Wizard" },
	{ PARAM_PAPER_MODE, "Paper Load Mode" },
	{ PARAM_SLEEP_TIME, "Sleep Time" },
	{ PARAM_REGION_CODE, "Region Code" },
};
#define s6145_params_num (sizeof(s6145_params) / sizeof(struct sinfonia_param))



#define PARAM_OC_PRINT_OFF   0x00000001
#define PARAM_OC_PRINT_GLOSS 0x00000002
#define PARAM_OC_PRINT_MATTE 0x00000003

#define PARAM_PAPER_PRESV_OFF 0x00000000
#define PARAM_PAPER_PRESV_ON  0x00000001

#define PARAM_DRIVER_WIZOFF 0x00000000
#define PARAM_DRIVER_WIZON  0x00000001

#define PARAM_PAPER_NOCUT   0x00000000
#define PARAM_PAPER_CUTLOAD 0x00000001

#define PARAM_SLEEP_5MIN    0x00000000
#define PARAM_SLEEP_15MIN   0x00000001
#define PARAM_SLEEP_30MIN   0x00000002
#define PARAM_SLEEP_60MIN   0x00000003
#define PARAM_SLEEP_120MIN  0x00000004
#define PARAM_SLEEP_240MIN  0x00000005

static const char *error_codes(uint8_t major, uint8_t minor)
{
	switch(major) {
	case 0x01: /* "Controller Error" */
		switch(minor) {
		case 0x01:
			return "Controller: EEPROM Write Timeout";
		case 0x0A:
			return "Controller: Invalid Print Parameter Table";
		case 0x0C:
			return "Controller: Print Parameter Table Mismatch";
		case 0x0F:
			return "Controller: Main FW Checksum";
		case 0x10:
			return "Controller: Flash Write Failed";
		case 0x13:
			return "Controller: Print Parameter Table Checksum";
		case 0x14:
			return "Controller: Print Parameter Table Write Failed";
		case 0x15:
			return "Controller: User Tone Curve Write Failed";
		case 0x16:
			return "Controller: MSP Communication";
		case 0x17:
			return "Controller: THV Autotuning";
		case 0x18:
			return "Controller: THV Value Out of Range";
		case 0x19:
			return "Controller: Thermal Head";
		case 0x1A:
			return "Controller: Wake from Power Save Failed";
		default:
			return "Controller: Unknown";
		}
	case 0x02: /* "Mechanical Error" */
		switch (minor) {
		case 0x01:
			return "Mechanical: Pinch Head Home";
		case 0x02:
			return "Mechanical: Pinch Head (position 1)";
		case 0x03:
			return "Mechanical: Pinch Head (position 2)";
		case 0x04:
			return "Mechanical: Pinch Head (position 3)";
		case 0x0B:
			return "Mechanical: Cutter (Right)";
		case 0x0C:
			return "Mechanical: Cutter (Left)";
		default:
			return "Mechanical: Unknown";
		}
	case 0x03: /* "Sensor Error" */
		switch (minor) {
		case 0x01:
			return "Sensor: Head Up";
		case 0x02:
			return "Sensor: Head Down";
		case 0x0B:
			return "Sensor: Cutter Left";
		case 0x0C:
			return "Sensor: Cutter Right";
		case 0x0D:
			return "Sensor: Cutter Left+Right";
		case 0x15:
			return "Sensor: Head Up Unstable";
		case 0x16:
			return "Sensor: Head Down Unstable";
		case 0x17:
			return "Sensor: Cutter Left Unstable";
		case 0x18:
			return "Sensor: Cutter Right Unstable";
		case 0x19:
			return "Sensor: Cover Open Unstable";
		case 0x1E:
			return "Sensor: Ribbon Mark (Cyan)";
		case 0x1F:
			return "Sensor: Ribbon Mark (OC)";
		default:
			return "Sensor: Unknown";
		}
	case 0x04: /* "Temperature Sensor Error" */
		switch (minor) {
		case 0x01:
			return "Temp Sensor: Thermal Head Low";
		case 0x02:
			return "Temp Sensor: Thermal Head High";
		case 0x05:
			return "Temp Sensor: Environment Low";
		case 0x06:
			return "Temp Sensor: Environment High";
		case 0x07:
			return "Temp Sensor: Preheat";
		case 0x08:
			return "Temp Sensor: Thermal Protect";
		default:
			return "Temp Sensor: Unknown";
		}
	case 0x5: /* "Paper Jam" */
		switch (minor) {
		case 0x01:
			return "Paper Jam: Loading Paper Top On";
		case 0x02:
			return "Paper Jam: Loading Print Position On";
		case 0x03:
			return "Paper Jam: Loading Print Position Off";
		case 0x04:
			return "Paper Jam: Loading Paper Top Off";
		case 0x05:
			return "Paper Jam: Loading Cut Print Position Off";
		case 0x0C:
			return "Paper Jam: Initializing Print Position Off";
		case 0x0D:
			return "Paper Jam: Initializing Print Position On";
		case 0x15:
			return "Paper Jam: Printing Print Position Off";
		case 0x16:
			return "Paper Jam: Printing Paper Top On";
		case 0x17:
			return "Paper Jam: Printing Paper Top Off";
		case 0x1F:
			return "Paper Jam: Precut Print Position Off";
		case 0x20:
			return "Paper Jam: Precut Print Position On";

		case 0x29:
			return "Paper Jam: Printing Paper Top On";
		case 0x2A:
			return "Paper Jam: Printing Pre-Yellow Print Position Off";
		case 0x2B:
			return "Paper Jam: Printing Yellow Print Position Off";
		case 0x2C:
			return "Paper Jam: Printing Yellow Print Position On";
		case 0x2D:
			return "Paper Jam: Printing Pre-Magenta Print Position Off";
		case 0x2E:
			return "Paper Jam: Printing Magenta Print Position On";
		case 0x2F:
			return "Paper Jam: Printing Magenta Print Position Off";
		case 0x30:
			return "Paper Jam: Printing Pre-Cyan Print Position Off";
		case 0x31:
			return "Paper Jam: Printing Cyan Print Position On";
		case 0x32:
			return "Paper Jam: Printing Cyan Print Position Off";
		case 0x33:
			return "Paper Jam: Printing Pre-OC Print Position Off";
		case 0x34:
			return "Paper Jam: Printing OC Print Position On";
		case 0x35:
			return "Paper Jam: Printing OC Print Position Off";
		case 0x36:
			return "Paper Jam: Cut Print Position Off";
		case 0x37:
			return "Paper Jam: Home Position Off";
		case 0x38:
			return "Paper Jam: Paper Top Off";
		case 0x39:
			return "Paper Jam: Print Position On";

		case 0x51:
			return "Paper Jam: Paper Empty On, Top On, Position On";
		case 0x52:
			return "Paper Jam: Paper Empty On, Top On, Position Off";
		case 0x54:
			return "Paper Jam: Paper Empty On, Top Off, Position Off";
		case 0x60:
			return "Paper Jam: Cutter Right";
		case 0x61:
			return "Paper Jam: Cutter Left";

		default:
			return "Paper Jam: Unknown";
		}
	case 0x06: /* User Error */
		switch (minor) {
		case 0x01:
			return "Drawer Unit Open";
		case 0x02:
			return "Incorrect Ribbon";
		case 0x04:
			return "Ribbon Empty";
		case 0x08:
			return "No Paper";
		case 0x0C:
			return "Paper End";
		default:
			return "Unknown";
		}
	default:
		return "Unknown";
	}
}

struct s6145_status_resp {
	struct sinfonia_status_hdr hdr;
	uint32_t count_lifetime;
	uint32_t count_maint;
	uint32_t count_paper;
	uint32_t count_cutter;
	uint32_t count_head;
	uint32_t count_ribbon_left;
	uint32_t reserved;

	uint8_t  bank1_printid;
	uint16_t bank1_remaining;
	uint16_t bank1_finished;
	uint16_t bank1_specified;
	uint8_t  bank1_status;

	uint8_t  bank2_printid;
	uint16_t bank2_remaining;
	uint16_t bank2_finished;
	uint16_t bank2_specified;
	uint8_t  bank2_status;

	uint8_t  reserved2[16];
	uint8_t  tonecurve_status;
	uint8_t  reserved3[6];
} __attribute__((packed));

struct s6145_geteeprom_resp {
	struct sinfonia_status_hdr hdr;
	uint8_t data[256];
} __attribute__((packed));

#define RIBBON_NONE   0x00
#define RIBBON_4x6    0x01
#define RIBBON_3_5x5  0x02
#define RIBBON_5x7    0x03
#define RIBBON_6x8    0x04
#define RIBBON_6x9    0x05
// XXX what about 89xXXXmm ribbons?

static int ribbon_sizes (uint8_t v) {
	switch (v) {
	case RIBBON_4x6:
		return 300;
	case RIBBON_3_5x5:
		return 340;
	case RIBBON_5x7:
		return 170;
	case RIBBON_6x8:
		return 150;
	case RIBBON_6x9:
		return 130; // XXX guessed
	// XXX 89x??? rubbons.
	default:
		return 300; // don't want 0.
	}
}

static const char *print_ribbons (uint8_t v) {
	switch (v) {
	case RIBBON_NONE:
		return "None";
	case RIBBON_4x6:
		return "4x6";
	case RIBBON_3_5x5:
		return "3.5x5";
	case RIBBON_5x7:
		return "5x7";
	case RIBBON_6x8:
		return "6x8";
	case RIBBON_6x9:
		return "6x9";
	// XXX 89x??? ribbons.
	default:
		return "Unknown";
	}
}

struct s6145_imagecorr_resp {
	struct sinfonia_status_hdr hdr;
	uint16_t total_size;
} __attribute__((packed));

struct s6145_imagecorr_data {
	uint8_t  remain_pkt;
	uint8_t  return_size;
	uint8_t  data[16];
} __attribute__((packed));

/* Private data structure */
struct shinkos6145_ctx {
	struct sinfonia_usbdev dev;

	uint8_t jobid;

	uint8_t image_avg[3]; /* YMC */

	char serial[32];
	char fwver[32];

	struct marker marker;

	struct sinfonia_6x45_mediainfo_resp media;

	uint8_t *eeprom;
	size_t eepromlen;

	void *dl_handle;
	ImageProcessingFN ImageProcessing;
	ImageAvrCalcFN ImageAvrCalc;

	struct shinkos6145_correctionparam *corrdata;
	uint16_t corrdatalen;
};

static int shinkos6145_get_imagecorr(struct shinkos6145_ctx *ctx);
static int shinkos6145_get_eeprom(struct shinkos6145_ctx *ctx);

static int get_status(struct shinkos6145_ctx *ctx)
{
	struct sinfonia_cmd_hdr cmd;
	struct s6145_status_resp resp;
	struct sinfonia_getextcounter_resp resp2;
	int ret, num = 0;
	uint32_t val;

	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETSTATUS);
	cmd.len = cpu_to_le16(0);

	if ((ret = sinfonia_docmd(&ctx->dev,
				  (uint8_t*)&cmd, sizeof(cmd),
				  (uint8_t*)&resp, sizeof(resp), &num)) < 0) {
		return ret;
	}

	INFO("Printer Status:  0x%02x (%s)\n", resp.hdr.status,
	     sinfonia_status_str(resp.hdr.status));
	if (resp.hdr.status == ERROR_PRINTER) {
		if(resp.hdr.error == ERROR_NONE)
			resp.hdr.error = resp.hdr.status;
		INFO(" Error 0x%02x (%s) 0x%02x/0x%02x (%s)\n",
		     resp.hdr.error,
		     sinfonia_error_str(resp.hdr.error),
		     resp.hdr.printer_major,
		     resp.hdr.printer_minor, error_codes(resp.hdr.printer_major, resp.hdr.printer_minor));
	}
	if (le16_to_cpu(resp.hdr.payload_len) != (sizeof(struct s6145_status_resp) - sizeof(struct sinfonia_status_hdr)))
		return -1;

	INFO(" Print Counts:\n");
	INFO("\tSince Paper Changed:\t%08u\n", le32_to_cpu(resp.count_paper));
	INFO("\tLifetime:\t\t%08u\n", le32_to_cpu(resp.count_lifetime));
	INFO("\tMaintenance:\t\t%08u\n", le32_to_cpu(resp.count_maint));
	INFO("\tPrint Head:\t\t%08u\n", le32_to_cpu(resp.count_head));
	INFO(" Cutter Actuations:\t%08u\n", le32_to_cpu(resp.count_cutter));
	INFO(" Ribbon Remaining:\t%08u\n", le32_to_cpu(resp.count_ribbon_left));
	INFO("Bank 1: 0x%02x (%s) Job %03u @ %03u/%03u (%03u remaining)\n",
	     resp.bank1_status, sinfonia_bank_statuses(resp.bank1_status),
	     resp.bank1_printid,
	     le16_to_cpu(resp.bank1_finished),
	     le16_to_cpu(resp.bank1_specified),
	     le16_to_cpu(resp.bank1_remaining));

	INFO("Bank 2: 0x%02x (%s) Job %03u @ %03u/%03u (%03u remaining)\n",
	     resp.bank2_status, sinfonia_bank_statuses(resp.bank1_status),
	     resp.bank2_printid,
	     le16_to_cpu(resp.bank2_finished),
	     le16_to_cpu(resp.bank2_specified),
	     le16_to_cpu(resp.bank2_remaining));

	INFO("Tonecurve Status: 0x%02x (%s)\n", resp.tonecurve_status, sinfonia_tonecurve_statuses(resp.tonecurve_status));

	/* Query Extended counters */
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_EXTCOUNTER);
	cmd.len = cpu_to_le16(0);

	if ((ret = sinfonia_docmd(&ctx->dev,
				  (uint8_t*)&cmd, sizeof(cmd),
				  (uint8_t*)&resp2, sizeof(resp2),
				  &num))) {
		return ret;
	}
	if (le16_to_cpu(resp2.hdr.payload_len) != (sizeof(struct sinfonia_getextcounter_resp) - sizeof(struct sinfonia_status_hdr)))
		return -1;

	INFO("Lifetime Distance:     %08u inches\n", le32_to_cpu(resp2.lifetime_distance));
	INFO("Maintenance Distance:  %08u inches\n", le32_to_cpu(resp2.maint_distance));
	INFO("Head Distance:         %08u inches\n", le32_to_cpu(resp2.head_distance));

	/* Query various params */
	if (ctx->dev.type == P_SHINKO_S6145D) {
		if ((ret = sinfonia_getparam(&ctx->dev, PARAM_REGION_CODE, &val))) {
			ERROR("Failed to execute command\n");
			return ret;
		}
		INFO("Region Code: %#x\n", val);

	}
	if ((ret = sinfonia_getparam(&ctx->dev, PARAM_PAPER_PRESV, &val))) {
		ERROR("Failed to execute command\n");
		return ret;
	}
	INFO("Paper Preserve mode: %s\n", (val ? "On" : "Off"));

	if ((ret = sinfonia_getparam(&ctx->dev, PARAM_DRIVER_MODE, &val))) {
		ERROR("Failed to execute command\n");
		return ret;
	}
	INFO("Driver mode:         %s\n", (val ? "On" : "Off"));

	if ((ret = sinfonia_getparam(&ctx->dev, PARAM_PAPER_MODE, &val))) {
		ERROR("Failed to execute command\n");
		return ret;
	}
	INFO("Paper load mode:     %s\n", (val ? "Cut" : "No Cut"));

	if ((ret = sinfonia_getparam(&ctx->dev, PARAM_SLEEP_TIME, &val))) {
		ERROR("Failed to execute command\n");
		return ret;
	}
	if (val == 0)
		val = 5;
	else if (val == 1)
		val = 15;
	else if (val == 2)
		val = 30;
	else if (val == 3)
		val = 60;
	else if (val == 4)
		val = 120;
	else if (val >= 5)
		val = 240;
	else
		val = 240; // default?

	INFO("Sleep delay:         %u minutes\n", val);

	return CUPS_BACKEND_OK;
}

static void dump_mediainfo(struct sinfonia_6x45_mediainfo_resp *resp)
{
	int i;

	INFO("Loaded Media Type:  %s\n", print_ribbons(resp->ribbon_code));
	INFO("Supported Print Sizes: %u entries:\n", resp->count);
	for (i = 0 ; i < resp->count ; i++) {
		INFO(" %02d: C 0x%02x (%s), %04ux%04u, P 0x%02x (%s)\n", i,
		     resp->items[i].code,
		     sinfonia_print_codes(resp->items[i].code, 0),
		     resp->items[i].columns,
		     resp->items[i].rows,
		     resp->items[i].method,
		     sinfonia_print_methods(resp->items[i].method));
	}
}

static int shinkos6145_dump_corrdata(struct shinkos6145_ctx *ctx, char *fname)
{
	int ret;

	ret = shinkos6145_get_imagecorr(ctx);
	if (ret) {
		ERROR("Failed to execute command\n");
		return ret;
	}

	/* Open file and write it out */
	{
		int fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
		if (fd < 0) {
			ERROR("Unable to open filename\n");
			return fd;
		}

		ret = write(fd, ctx->corrdata, sizeof(struct shinkos6145_correctionparam));
		close(fd);
	}

	/* Free the buffers */
	free(ctx->corrdata);
	ctx->corrdata = NULL;
	ctx->corrdatalen = 0;

	return ret;
}

static int shinkos6145_dump_eeprom(struct shinkos6145_ctx *ctx, char *fname)
{
	int ret;

	ret = shinkos6145_get_eeprom(ctx);
	if (ret) {
		ERROR("Failed to execute command\n");
		return ret;
	}

	/* Open file and write it out */
	{
		int fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
		if (fd < 0) {
			ERROR("Unable to open filename\n");
			return fd;
		}

		ret = write(fd, ctx->eeprom, ctx->eepromlen);
		close(fd);
	}

	/* Free the buffers */
	free(ctx->eeprom);
	ctx->eeprom = NULL;
	ctx->eepromlen = 0;

	return ret;
}

static int shinkos6145_get_imagecorr(struct shinkos6145_ctx *ctx)
{
	struct sinfonia_cmd_hdr cmd;
	struct s6145_imagecorr_resp resp;

	uint16_t total = 0;
	int ret, num;
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETCORR);
	cmd.len = 0;

	if (ctx->corrdata) {
		free(ctx->corrdata);
		ctx->corrdata = NULL;
	}

	if ((ret = sinfonia_docmd(&ctx->dev,
				  (uint8_t*)&cmd, sizeof(cmd),
				  (uint8_t*)&resp, sizeof(resp),
				  &num))) {
		goto done;
	}

	ctx->corrdatalen = le16_to_cpu(resp.total_size);
	INFO("Fetching %u bytes of image correction data\n", ctx->corrdatalen);

	ctx->corrdata = malloc(sizeof(struct shinkos6145_correctionparam));
	if (!ctx->corrdata) {
		ERROR("Memory allocation failure\n");
		ret = CUPS_BACKEND_FAILED;
		goto done;
	}
	memset(ctx->corrdata, 0, sizeof(struct shinkos6145_correctionparam));
	total = 0;

	while (total < ctx->corrdatalen) {
		struct s6145_imagecorr_data data;

		ret = read_data(ctx->dev.dev, ctx->dev.endp_up, (uint8_t *) &data,
				sizeof(data),
				&num);
		if (ret < 0)
			goto done;

		memcpy(((uint8_t*)ctx->corrdata) + total, data.data, sizeof(data.data));
		total += sizeof(data.data);

		if (data.remain_pkt == 0)
			DEBUG("correction block transferred (%u/%u total)\n", total, ctx->corrdatalen);

	}


done:
	return ret;
}

static int shinkos6145_get_eeprom(struct shinkos6145_ctx *ctx)
{
	struct sinfonia_cmd_hdr cmd;
	struct s6145_geteeprom_resp resp;

	int ret, num;
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETEEPROM);
	cmd.len = 0;

	if (ctx->eeprom) {
		free(ctx->eeprom);
		ctx->eeprom = NULL;
	}

	if ((ret = sinfonia_docmd(&ctx->dev,
				  (uint8_t*)&cmd, sizeof(cmd),
				  (uint8_t*)&resp, sizeof(resp),
				  &num))) {
		goto done;
	}

	ctx->eepromlen = le16_to_cpu(resp.hdr.payload_len);
	ctx->eeprom = malloc(ctx->eepromlen);
	if (!ctx->eeprom) {
		ERROR("Memory allocation failure\n");
		ret = CUPS_BACKEND_FAILED;
		goto done;
	}
	memcpy(ctx->eeprom, resp.data, ctx->eepromlen);

done:
	return ret;
}

static void shinkos6145_cmdline(void)
{
	DEBUG("\t\t[ -c filename ]  # Get user/NV tone curve\n");
	DEBUG("\t\t[ -C filename ]  # Set user/NV tone curve\n");
	DEBUG("\t\t[ -e ]           # Query error log\n");
	DEBUG("\t\t[ -F ]           # Flash Printer LED\n");
	DEBUG("\t\t[ -i ]           # Query printer info\n");
	DEBUG("\t\t[ -k num ]       # Set sleep time (5-240 minutes)\n");
	DEBUG("\t\t[ -l filename ]  # Get current tone curve\n");
	DEBUG("\t\t[ -L filename ]  # Set current tone curve\n");
	DEBUG("\t\t[ -m ]           # Query media\n");
	DEBUG("\t\t[ -q filename ]  # Extract eeprom data\n");
	DEBUG("\t\t[ -Q filename ]  # Extract image correction params\n");
	DEBUG("\t\t[ -r ]           # Reset user/NV tone curve\n");
	DEBUG("\t\t[ -R ]           # Reset printer to factory defaults\n");
	DEBUG("\t\t[ -s ]           # Query status\n");
	DEBUG("\t\t[ -X jobid ]     # Abort a printjob\n");
//	DEBUG("\t\t[ -Z ]           # Dump all parameters\n");
}

static int shinkos6145_cmdline_arg(void *vctx, int argc, char **argv)
{
	struct shinkos6145_ctx *ctx = vctx;
	int i, j = 0;

	if (!ctx)
		return -1;

	while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "c:C:eFik:l:L:mr:Q:q:rR:sX:Z:")) >= 0) {
		switch(i) {
		GETOPT_PROCESS_GLOBAL
		case 'c':
			j = sinfonia_gettonecurve(&ctx->dev, TONECURVE_USER, optarg);
			break;
		case 'C':
			j = sinfonia_settonecurve(&ctx->dev, UPDATE_TARGET_TONE_USER, optarg);
			break;
		case 'e':
			j = sinfonia_geterrorlog(&ctx->dev);
			break;
		case 'F':
			j = sinfonia_flashled(&ctx->dev);
			break;
		case 'i':
			j = sinfonia_getfwinfo(&ctx->dev);
			break;
		case 'k': {
			i = atoi(optarg);
			if (i <= 5)
				i = 0;
			else if (i <= 15)
				i = 1;
			else if (i <= 30)
				i = 2;
			else if (i <= 60)
				i = 3;
			else if (i <= 120)
				i = 4;
			else if (i <= 240)
				i = 5;
			else
				i = 5;

			j = sinfonia_setparam(&ctx->dev, PARAM_SLEEP_TIME, i);
			break;
		}
		case 'l':
			j = sinfonia_gettonecurve(&ctx->dev, TONECURVE_CURRENT, optarg);
			break;
		case 'L':
			j = sinfonia_settonecurve(&ctx->dev, UPDATE_TARGET_TONE_CURRENT, optarg);
			break;
		case 'm':
			dump_mediainfo(&ctx->media);
			break;
		case 'q':
			j = shinkos6145_dump_eeprom(ctx, optarg);
			break;
		case 'Q':
			j = shinkos6145_dump_corrdata(ctx, optarg);
			break;
		case 'r':
			j = sinfonia_resetcurve(&ctx->dev, RESET_TONE_CURVE, TONE_CURVE_ID);
			break;
		case 'R':
			j = sinfonia_resetcurve(&ctx->dev, RESET_PRINTER, 0);
			break;
		case 's':
			j = get_status(ctx);
			break;
		case 'X':
			j = sinfonia_canceljob(&ctx->dev, atoi(optarg));
			break;
		case 'Z':
			j = sinfonia_dumpallparams(&ctx->dev, atoi(optarg));
			break;
		default:
			break;  /* Ignore completely */
		}

		if (j) return j;
	}

	return CUPS_BACKEND_OK;
}

static void *shinkos6145_init(void)
{
	struct shinkos6145_ctx *ctx = malloc(sizeof(struct shinkos6145_ctx));
	if (!ctx) {
		ERROR("Memory Allocation Failure!\n");
		return NULL;
	}
	memset(ctx, 0, sizeof(struct shinkos6145_ctx));

	DL_INIT();

	return ctx;
}

static int shinkos6145_attach(void *vctx, struct libusb_device_handle *dev, int type,
			      uint8_t endp_up, uint8_t endp_down, int iface, uint8_t jobid)
{
	struct shinkos6145_ctx *ctx = vctx;

	ctx->dev.dev = dev;
	ctx->dev.endp_up = endp_up;
	ctx->dev.endp_down = endp_down;
	ctx->dev.type = type;
	ctx->dev.iface = iface;
	ctx->dev.error_codes = &error_codes;

	if (type == P_SHINKO_S6145 ||
	    type == P_SHINKO_S6145D) {
		ctx->dev.params = s6145_params;
		ctx->dev.params_count = s6145_params_num;
	}

	/* Attempt to open the library */
#if defined(WITH_DYNAMIC)
	INFO("Attempting to load image processing library\n");
	ctx->dl_handle = DL_OPEN(LIB_NAME); /* Try the Sinfonia one first */
	if (!ctx->dl_handle)
		ctx->dl_handle = DL_OPEN(LIB_NAME_RE); /* Then the RE one */
	if (!ctx->dl_handle)
		WARNING("Image processing library not found, using internal fallback code\n");
	if (ctx->dl_handle) {
		ctx->ImageProcessing = DL_SYM(ctx->dl_handle, "ImageProcessing");
		ctx->ImageAvrCalc = DL_SYM(ctx->dl_handle, "ImageAvrCalc");
		if (!ctx->ImageProcessing || !ctx->ImageAvrCalc) {
			WARNING("Problem resolving symbols in imaging processing library\n");
			DL_CLOSE(ctx->dl_handle);
			ctx->dl_handle = NULL;
		} else {
			INFO("Image processing library successfully loaded\n");
		}
	}
#else
	WARNING("Dynamic library support not enabled, using internal fallback code\n");
#endif

	/* Ensure jobid is sane */
	ctx->jobid = (jobid & 0x7f);
	if (!ctx->jobid)
		ctx->jobid++;

	/* Query Media */
	if (test_mode < TEST_MODE_NOATTACH) {
		int ret = sinfonia_query_media(&ctx->dev,
					       &ctx->media);
		if (ret)
			return ret;
	} else {
		int media_code = RIBBON_6x8;
		if (getenv("MEDIA_CODE"))
			media_code = atoi(getenv("MEDIA_CODE"));

		ctx->media.ribbon_code = media_code;
	}

	ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
	ctx->marker.name = print_ribbons(ctx->media.ribbon_code);
	ctx->marker.numtype = ctx->media.ribbon_code;
	ctx->marker.levelmax = ribbon_sizes(ctx->media.ribbon_code);
	ctx->marker.levelnow = CUPS_MARKER_UNKNOWN;

	return CUPS_BACKEND_OK;
}

static void shinkos6145_teardown(void *vctx) {
	struct shinkos6145_ctx *ctx = vctx;

	if (!ctx)
		return;

	if (ctx->eeprom)
		free(ctx->eeprom);
	if (ctx->corrdata)
		free(ctx->corrdata);
	if (ctx->dl_handle)
		DL_CLOSE(ctx->dl_handle);

	DL_EXIT();

	free(ctx);
}

static void lib6145_calc_avg(struct shinkos6145_ctx *ctx,
			     const struct sinfonia_printjob *job,
			     uint16_t rows, uint16_t cols)
{
	uint32_t plane, i, planelen;
	planelen = rows * cols;

	for (plane = 0 ; plane < 3 ; plane++) {
		uint64_t sum = 0;

		for (i = 0 ; i < planelen ; i++) {
			sum += job->databuf[(planelen * plane) + i];
		}
		ctx->image_avg[plane] = (sum / planelen);
	}
}

static void lib6145_process_image(uint8_t *src, uint16_t *dest,
				  struct shinkos6145_correctionparam *corrdata,
				  uint8_t oc_mode)
{
	uint32_t in, out;

	uint16_t pad_l, pad_r, row_lim;
	uint16_t row, col;

	row_lim = le16_to_cpu(corrdata->headDots);
	pad_l = (row_lim - le16_to_cpu(corrdata->width)) / 2;
	pad_r = pad_l + le16_to_cpu(corrdata->width);
	out = 0;
	in = 0;

	/* Convert YMC 8-bit to 16-bit, and pad appropriately to full stripe */
	for (row = 0 ; row < le16_to_cpu(corrdata->height) ; row++) {
		for (col = 0 ; col < row_lim; col++) {
			uint16_t val;
			if (col < pad_l) {
				val = 0;
			} else if (col < pad_r) {
				val = corrdata->pulseTransTable_Y[src[in++]];
			} else {
				val = 0;
			}
			dest[out++] = val;
		}
	}
	for (row = 0 ; row < le16_to_cpu(corrdata->height) ; row++) {
		for (col = 0 ; col < row_lim; col++) {
			uint16_t val;
			if (col < pad_l) {
				val = 0;
			} else if (col < pad_r) {
				val = corrdata->pulseTransTable_M[src[in++]];
			} else {
				val = 0;
			}
			dest[out++] = val;
		}
	}
	for (row = 0 ; row < le16_to_cpu(corrdata->height) ; row++) {
		for (col = 0 ; col < row_lim; col++) {
			uint16_t val;
			if (col < pad_l) {
				val = 0;
			} else if (col < pad_r) {
				val = corrdata->pulseTransTable_C[src[in++]];
			} else {
				val = 0;
			}
			dest[out++] = val;
		}
	}

	/* Generate lamination plane, if desired */
	if (oc_mode > PRINT_MODE_NO_OC) {
		// XXX matters if we're using glossy/matte...
		for (row = 0 ; row < le16_to_cpu(corrdata->height) ; row++) {
			for (col = 0 ; col < row_lim; col++) {
				uint16_t val;
				if (col < pad_l) {
					val = 0;
				} else if (col < pad_r) {
					val = corrdata->pulseTransTable_O[corrdata->printOpLevel];
				} else {
					val = 0;
				}
				dest[out++] = val;
			}
		}
	}
}

static int shinkos6145_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
	struct shinkos6145_ctx *ctx = vctx;
	struct sinfonia_printjob *job = NULL;
	int ret;
	int model;
	uint8_t input_ymc;

	if (!ctx)
		return CUPS_BACKEND_FAILED;

	if (ctx->dev.type == P_SHINKO_S6145 ||
	    ctx->dev.type == P_SHINKO_S6145D)
		model = 6145;
	else
		model = 2245;

	job = malloc(sizeof(*job));
	if (!job) {
		ERROR("Memory allocation failure!\n");
		return CUPS_BACKEND_RETRY_CURRENT;
	}
	memset(job, 0, sizeof(*job));
	job->jobsize = sizeof(*job);

	/* Common read/parse code */
	if (ctx->dev.type == P_KODAK_6900) {
		ret = sinfonia_raw28_read_parse(data_fd, job);
	} else {
		ret = sinfonia_read_parse(data_fd, model, job);
	}
	if (ret) {
		free(job);
		return ret;
	}

	/* Use whicever copy count is larger */
	if ((int)job->jp.copies > copies)
		job->copies = job->jp.copies;
	else
		job->copies = copies;

	/* S6145 can only combine 2* 4x6 -> 8x6.
	   2x6 strips and 3.5x5 -> 5x7 can't. */
	if (job->jp.columns == 1844 &&
	    job->jp.rows == 1240 &&
	    job->jp.method == PRINT_METHOD_STD &&
	    (ctx->media.ribbon_code == RIBBON_6x8 ||
	     ctx->media.ribbon_code == RIBBON_6x9)) {
		job->can_combine = 1;
	}

	/* Extended spool format to re-purpose an unused header field.
	   When bit 0 is set, this tells the backend that the data is
	   already in planar YMC format (vs packed RGB) so we don't need
	   to do the conversion ourselves.  Saves some processing overhead */
	input_ymc = job->jp.ext_flags & EXT_FLAG_PLANARYMC;

	/* Convert packed RGB to planar YMC if necessary */
	if (!input_ymc) {
		INFO("Converting Packed RGB to Planar YMC\n");
		int planelen = job->jp.columns * job->jp.rows;
		uint8_t *databuf3 = malloc(job->datalen);
		int i;
		if (!databuf3) {
			ERROR("Memory allocation failure!\n");
			sinfonia_cleanup_job(job);
			return CUPS_BACKEND_RETRY_CURRENT;
		}
		for (i = 0 ; i < planelen ; i++) {
			uint8_t r, g, b;
			r = job->databuf[3*i];
			g = job->databuf[3*i+1];
			b = job->databuf[3*i+2];
			databuf3[i] = 255 - b;
			databuf3[planelen + i] = 255 - g;
			databuf3[planelen + planelen + i] = 255 - r;
		}
		free(job->databuf);
		job->databuf = databuf3;
	}

	*vjob = job;

	return CUPS_BACKEND_OK;
}

#define JOB_EQUIV(__x)  if (job1->__x != job2->__x) goto done

// XXX this code could be "generic sinfonia?"
// but only the S6145 and S2245 can't automatically combine
// or rewind.  So it's really only useful here.
static void *shinkos6145_combine_jobs(const void *vjob1,
				      const void *vjob2)
{
	const struct sinfonia_printjob *job1 = vjob1;
	const struct sinfonia_printjob *job2 = vjob2;
	struct sinfonia_printjob *newjob = NULL;

	uint16_t newrows;
	uint16_t newpad;

	if (!job1 || !job2)
		goto done;

	/* Make sure we're okay to proceed */
	JOB_EQUIV(jp.columns);
	JOB_EQUIV(jp.rows);
	JOB_EQUIV(jp.oc_mode);
	JOB_EQUIV(jp.method);
	JOB_EQUIV(jp.media);
	JOB_EQUIV(jp.quality);

	switch (job1->jp.rows) {
	case 1240:  /* 4x6 */
		if (job1->jp.method != PRINT_METHOD_STD)
			goto done;
		newrows = 2492;
		newpad = 12;
		break;
	default:
		/* All other sizes, we can't combine */
		goto done;
	}

        newjob = malloc(sizeof(*newjob));
        if (!newjob) {
                ERROR("Memory allocation failure!\n");
                goto done;
        }
        memcpy(newjob, job1, sizeof(*newjob));

	newjob->jp.rows = newrows;
	newjob->jp.media = CODE_6x8;
	newjob->jp.method = PRINT_METHOD_SPLIT;

	/* Allocate new buffer */
	newjob->databuf = malloc(newjob->jp.rows * newjob->jp.columns * 3);
	newjob->datalen = 0;
	if (!newjob->databuf) {
		sinfonia_cleanup_job(newjob);
		newjob = NULL;
		goto done;
	}

	/* Copy Planar YMC payload into new buffer! */
	memcpy(newjob->databuf + newjob->datalen,
	       job1->databuf,
	       job1->jp.rows * job1->jp.columns);
	newjob->datalen += job1->jp.rows * job1->jp.columns;
	memset(newjob->databuf + newjob->datalen, 0xff, newpad * newjob->jp.columns);
	memcpy(newjob->databuf + newjob->datalen,
	       job2->databuf,
	       job2->jp.rows * job2->jp.columns);
	newjob->datalen += job2->jp.rows * job2->jp.columns;

	memcpy(newjob->databuf + newjob->datalen,
	       job1->databuf + (job1->jp.rows * job1->jp.columns),
	       job1->jp.rows * job1->jp.columns);
	newjob->datalen += job1->jp.rows * job1->jp.columns;
	memset(newjob->databuf + newjob->datalen, 0xff, newpad * newjob->jp.columns);
	memcpy(newjob->databuf + newjob->datalen,
	       job2->databuf + (job2->jp.rows * job2->jp.columns),
	       job2->jp.rows * job2->jp.columns);
	newjob->datalen += job2->jp.rows * job2->jp.columns;

	memcpy(newjob->databuf + newjob->datalen,
	       job1->databuf + 2*(job1->jp.rows * job1->jp.columns),
	       job1->jp.rows * job1->jp.columns);
	newjob->datalen += job1->jp.rows * job1->jp.columns;
	memset(newjob->databuf + newjob->datalen, 0xff, newpad * newjob->jp.columns);
	memcpy(newjob->databuf + newjob->datalen,
	       job2->databuf + 2*(job2->jp.rows * job2->jp.columns),
	       job2->jp.rows * job2->jp.columns);
	newjob->datalen += job2->jp.rows * job2->jp.columns;

done:
	return newjob;
}

static int shinkos6145_main_loop(void *vctx, const void *vjob) {
	struct shinkos6145_ctx *ctx = vctx;

	int ret, num;

	int i, last_state = -1, state = S_IDLE;

	struct sinfonia_cmd_hdr cmd;
	struct s6145_status_resp sts, sts2;

	uint32_t cur_mode;

	struct sinfonia_printjob *job = (struct sinfonia_printjob*) vjob; /* XXX stupid, we can't do this. */

	if (!job)
		return CUPS_BACKEND_FAILED;

	/* Validate print sizes */
	for (i = 0; i < ctx->media.count ; i++) {
		/* Look for matching media */
		if (ctx->media.items[i].columns == job->jp.columns &&
		    ctx->media.items[i].rows == job->jp.rows &&
		    ctx->media.items[i].method == job->jp.method &&
		    ctx->media.items[i].code == job->jp.media)
			break;
	}
	if (i == ctx->media.count) {
		ERROR("Incorrect media loaded for print!\n");
		return CUPS_BACKEND_HOLD;
	}

	// XXX check copies against remaining media?

	/* Query printer mode */
	ret = sinfonia_getparam(&ctx->dev, PARAM_OC_PRINT, &cur_mode);
	if (ret) {
		ERROR("Failed to execute command\n");
		return ret;
	}

top:
	if (state != last_state) {
		if (dyesub_debug)
			DEBUG("last_state %d new %d\n", last_state, state);
	}

	/* Send Status Query */
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETSTATUS);
	cmd.len = cpu_to_le16(0);

	if ((ret = sinfonia_docmd(&ctx->dev,
				  (uint8_t*)&cmd, sizeof(cmd),
				  (uint8_t*)&sts, sizeof(sts),
				  &num))) {
		return CUPS_BACKEND_FAILED;
	}

	if (memcmp(&sts, &sts2, sizeof(sts))) {
		memcpy(&sts2, &sts, sizeof(sts));

		INFO("Printer Status: 0x%02x (%s)\n",
		     sts.hdr.status, sinfonia_status_str(sts.hdr.status));

		if (ctx->marker.levelnow != (int)sts.count_ribbon_left) {
			ctx->marker.levelnow = sts.count_ribbon_left;
			dump_markers(&ctx->marker, 1, 0);
		}

		if (sts.hdr.result != RESULT_SUCCESS)
			goto printer_error;
		if (sts.hdr.status == ERROR_PRINTER)
			goto printer_error;
	} else if (state == last_state) {
		sleep(1);
		goto top;
	}
	last_state = state;

	fflush(stderr);

	switch (state) {
	case S_IDLE:
		INFO("Waiting for printer idle\n");
		/* If either bank is free, continue */
		if (sts.bank1_status == BANK_STATUS_FREE ||
		    sts.bank2_status == BANK_STATUS_FREE)
			state = S_PRINTER_READY_CMD;

		break;
	case S_PRINTER_READY_CMD: {
		/* Set matte/etc */

		uint32_t oc_mode = job->jp.oc_mode;
		uint32_t updated = 0;

		if (!oc_mode) /* if nothing set, default to glossy */
			oc_mode = PARAM_OC_PRINT_GLOSS;

		if (cur_mode != oc_mode) {
			/* If cur_mode is not the same as desired oc_mode,
			   change it -- but we have to wait until the printer
			   is COMPLETELY idle */
			if (sts.bank1_status != BANK_STATUS_FREE ||
			    sts.bank2_status != BANK_STATUS_FREE) {
				INFO("Need to switch overcoat mode, waiting for printer idle\n");
				sleep(1);
				goto top;
			}
			ret = sinfonia_setparam(&ctx->dev, PARAM_OC_PRINT, oc_mode);
			if (ret) {
				ERROR("Failed to execute command\n");
				return ret;
			}
			updated = 1;
		}

		ret = shinkos6145_get_eeprom(ctx);
		if (ret) {
			ERROR("Failed to execute command\n");
			return ret;
		}

		/* Get image correction parameters if necessary */
		if (updated || !ctx->corrdata || !ctx->corrdatalen) {
			ret = shinkos6145_get_imagecorr(ctx);
			if (ret) {
				ERROR("Failed to execute command\n");
				return ret;
			}
		}

		/* Set up library transform... */
		uint32_t newlen = le16_to_cpu(ctx->corrdata->headDots) *
			job->jp.rows * sizeof(uint16_t) * 4;
		uint16_t *databuf2 = malloc(newlen);

		/* Set the size in the correctiondata */
		ctx->corrdata->width = cpu_to_le16(job->jp.columns);
		ctx->corrdata->height = cpu_to_le16(job->jp.rows);


		/* Perform the actual library transform */
		if (ctx->dl_handle) {
			INFO("Calling image processing library...\n");

			if (ctx->ImageAvrCalc(job->databuf, job->jp.columns, job->jp.rows, ctx->image_avg)) {
				free(databuf2);
				ERROR("Library returned error!\n");
				return CUPS_BACKEND_FAILED;
			}
			ctx->ImageProcessing(job->databuf, databuf2, ctx->corrdata);
		} else {
			WARNING("Utilizing fallback internal image processing code\n");
			WARNING(" *** Output quality will be poor! *** \n");

			lib6145_calc_avg(ctx, job, job->jp.columns, job->jp.rows);
			lib6145_process_image(job->databuf, databuf2, ctx->corrdata, oc_mode);
		}

		free(job->databuf);
		job->databuf = (uint8_t*) databuf2;
		job->datalen = newlen;


		INFO("Sending print job (internal id %u)\n", ctx->jobid);

		if (ctx->dev.type != P_SHINKO_S2245) {
			struct s6145_print_cmd print;
			memset(&print, 0, sizeof(print));
			print.hdr.cmd = cpu_to_le16(SINFONIA_CMD_PRINTJOB);
			print.hdr.len = cpu_to_le16(sizeof (print) - sizeof(cmd));

			print.id = ctx->jobid;
			print.count = cpu_to_le16(job->copies);
			print.columns = cpu_to_le16(job->jp.columns);
			print.rows = cpu_to_le16(job->jp.rows);
			print.image_avg = ctx->image_avg[2]; /* Cyan level */
			print.method = cpu_to_le32(job->jp.method);
			print.combo_wait = 0;

			/* Brava21 header has a few quirks */
			if(ctx->dev.type == P_SHINKO_S6145D) {
				print.media = job->jp.media;
				print.unk_1 = 0x01;
			}
			if ((ret = sinfonia_docmd(&ctx->dev,
						  (uint8_t*)&print, sizeof(print),
						  (uint8_t*)&sts, sizeof(sts),
						  &num))) {
				return ret;
			}
		} else {
			/* XXX this is totally a guess */
			struct sinfonia_printcmd28_hdr print;
			memset(&print, 0, sizeof(print));
			print.hdr.cmd = cpu_to_le16(SINFONIA_CMD_PRINTJOB);
			print.hdr.len = cpu_to_le16(sizeof (print) - sizeof(cmd));
			print.jobid = ctx->jobid;
			print.copies = cpu_to_le16(job->copies);
			print.columns = cpu_to_le16(job->jp.columns);
			print.rows = cpu_to_le16(job->jp.rows);
			print.options = job->jp.oc_mode & 0x3;
			if (job->jp.quality)
				print.options |= 0x08;
			print.media = job->jp.media;

			//XXX print.image_avg = ctx->image_avg[2]; /* Cyan level */
			print.method = cpu_to_le32(job->jp.method);

			if ((ret = sinfonia_docmd(&ctx->dev,
						  (uint8_t*)&print, sizeof(print),
						  (uint8_t*)&sts, sizeof(sts),
						  &num))) {
				return ret;
			}
		}

		if (sts.hdr.result != RESULT_SUCCESS) {
			if (sts.hdr.error == ERROR_BUFFER_FULL) {
				INFO("Printer Buffers full, retrying\n");
				break;
			} else if ((sts.hdr.status & 0xf0) == 0x30 || sts.hdr.status == 0x21) {
				INFO("Printer busy (%s), retrying\n", sinfonia_status_str(sts.hdr.status));
				break;
			} else if (sts.hdr.status != ERROR_NONE)
				goto printer_error;
		}

		INFO("Sending image data to printer\n");
		// XXX we shouldn't send the lamination layer over if
		// it's not needed.  hdr->oc_mode == PRINT_MODE_NO_OC
		if ((ret = send_data(ctx->dev.dev, ctx->dev.endp_down,
				     job->databuf, job->datalen)))
			return CUPS_BACKEND_FAILED;

		INFO("Waiting for printer to acknowledge completion\n");
		sleep(1);
		state = S_PRINTER_SENT_DATA;
		break;
	}
	case S_PRINTER_SENT_DATA:
		if (fast_return) {
			INFO("Fast return mode enabled.\n");
			state = S_FINISHED;
		} else if (sts.hdr.status == STATUS_READY) {
			state = S_FINISHED;
		}
		break;
	default:
		break;
	};

	if (state != S_FINISHED)
		goto top;

	INFO("Print complete\n");

	return CUPS_BACKEND_OK;

printer_error:
	ERROR("Printer reported error: %#x (%s) status: %#x (%s) -> %#x.%#x (%s)\n",
	      sts.hdr.error,
	      sinfonia_error_str(sts.hdr.error),
	      sts.hdr.status,
	      sinfonia_status_str(sts.hdr.status),
	      sts.hdr.printer_major, sts.hdr.printer_minor,
	      error_codes(sts.hdr.printer_major, sts.hdr.printer_minor));
	return CUPS_BACKEND_FAILED;
}

static int shinkos6145_query_markers(void *vctx, struct marker **markers, int *count)
{
	struct shinkos6145_ctx *ctx = vctx;
	struct sinfonia_cmd_hdr cmd;
	struct s6145_status_resp sts;
	int num;

	/* Query Status */
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETSTATUS);
	cmd.len = cpu_to_le16(0);

	if (sinfonia_docmd(&ctx->dev,
			   (uint8_t*)&cmd, sizeof(cmd),
			   (uint8_t*)&sts, sizeof(sts),
			   &num)) {
		return CUPS_BACKEND_FAILED;
	}

	ctx->marker.levelnow = le32_to_cpu(sts.count_ribbon_left);

	if (markers) *markers = &ctx->marker;
	if (count) *count = 1;

	return CUPS_BACKEND_OK;
}


/* Exported */
#define USB_VID_SHINKO        0x10CE
#define USB_PID_SHINKO_S6145  0x0019
#define USB_PID_SHINKO_S6145D 0x001E /* Aka CIAAT Brava 21 */
#define USB_PID_SHINKO_S2245  0x0039
#define USB_VID_KODAKALARIS   0x29CC
#define USB_PID_KA_6900       0x0003 /* Aka S2245-6A */
#define USB_VID_HITI          0x0D16
#define USB_PID_HITI_M610     0x0010

static int shinkos6145_query_stats(void *vctx,  struct printerstats *stats)
{
	struct shinkos6145_ctx *ctx = vctx;
	struct sinfonia_cmd_hdr cmd;
	struct s6145_status_resp status;
	int num;
	uint16_t usbID = 0xffff;

	if (shinkos6145_query_markers(ctx, NULL, NULL))
		return CUPS_BACKEND_FAILED;

	/* Query Status */
	cmd.cmd = cpu_to_le16(SINFONIA_CMD_GETSTATUS);
	cmd.len = cpu_to_le16(0);

	if (sinfonia_docmd(&ctx->dev,
			   (uint8_t*)&cmd, sizeof(cmd),
			   (uint8_t*)&status, sizeof(status),
			   &num)) {
		return CUPS_BACKEND_FAILED;
	}

	/* Query USB ID */
	{
		struct libusb_device_descriptor desc;
		struct libusb_device *dev;

		dev = libusb_get_device(ctx->dev.dev);
		libusb_get_device_descriptor(dev, &desc);

		usbID = desc.idProduct;
	}

	switch (ctx->dev.type) {
	case P_SHINKO_S6145:
		stats->mfg = "Sinfonia";
		stats->model = "CS2 / S6145";
		break;
	case P_SHINKO_S6145D:
		stats->mfg = "Ciaat";
		stats->model = "Brava 21";
		break;
	case P_SHINKO_S2245:
		if (usbID == USB_PID_KA_6900) {
			stats->mfg = "Kodak";
			stats->model = "6900";
		} else if (usbID == USB_PID_HITI_M610) {
			stats->mfg = "HiTi";
			stats->model = "M610";
		} else if (usbID == USB_PID_SHINKO_S2245) {
			stats->mfg = "Sinfonia";
			stats->model = "S3 / S2245";
		} else {
			stats->mfg = "Unknown";
			stats->model = "Unknown";
		}
		break;
	default:
		stats->mfg = "Unknown";
		stats->model = "Unknown";
		break;
	}

	if (sinfonia_query_serno(ctx->dev.dev, ctx->dev.endp_up,
				 ctx->dev.endp_down, ctx->dev.iface,
				 ctx->serial, sizeof(ctx->serial)))
		return CUPS_BACKEND_FAILED;

	stats->serial = ctx->serial;

	{
		struct sinfonia_fwinfo_cmd  fcmd;
		struct sinfonia_fwinfo_resp resp;

		num = 0;
		fcmd.hdr.cmd = cpu_to_le16(SINFONIA_CMD_FWINFO);
		fcmd.hdr.len = cpu_to_le16(1);
		fcmd.target = FWINFO_TARGET_MAIN_APP;

		if (sinfonia_docmd(&ctx->dev,
				   (uint8_t*)&fcmd, sizeof(fcmd),
				   (uint8_t*)&resp, sizeof(resp),
				   &num))
			return CUPS_BACKEND_FAILED;
		snprintf(ctx->fwver, sizeof(ctx->fwver)-1,
			 "%d.%d", resp.major, resp.minor);
		stats->fwver = ctx->fwver;
	}

	stats->decks = 1;
	stats->mediatype[0] = ctx->marker.name;
	stats->levelmax[0] = ctx->marker.levelmax;
	stats->levelnow[0] = ctx->marker.levelnow;
	stats->name[0] = "Roll";
	if (status.hdr.status == ERROR_PRINTER) {
		if(status.hdr.error == ERROR_NONE)
			status.hdr.error = status.hdr.status;
		stats->status[0] = strdup(sinfonia_error_str(status.hdr.error));
	} else {
		stats->status[0] = strdup(sinfonia_status_str(status.hdr.status));
	}
	stats->cnt_life[0] = le32_to_cpu(status.count_lifetime);

	return CUPS_BACKEND_OK;
}

static const char *shinkos6145_prefixes[] = {
	"shinkos6145", /* Family Name */
	// backwards-compatiblity
	"brava21",
	NULL
};

struct dyesub_backend shinkos6145_backend = {
	.name = "Shinko/Sinfonia CHC-S6145/CS2/S2245/S3",
	.version = "0.48" " (lib " LIBSINFONIA_VER ")",
	.uri_prefixes = shinkos6145_prefixes,
	.cmdline_usage = shinkos6145_cmdline,
	.cmdline_arg = shinkos6145_cmdline_arg,
	.init = shinkos6145_init,
	.attach = shinkos6145_attach,
	.teardown = shinkos6145_teardown,
	.cleanup_job = sinfonia_cleanup_job,
	.read_parse = shinkos6145_read_parse,
	.main_loop = shinkos6145_main_loop,
	.query_serno = sinfonia_query_serno,
	.query_markers = shinkos6145_query_markers,
	.query_stats = shinkos6145_query_stats,
	.combine_jobs = shinkos6145_combine_jobs,
	.devices = {
		{ USB_VID_SHINKO, USB_PID_SHINKO_S6145, P_SHINKO_S6145, NULL, "sinfonia-chcs6145"},
		{ USB_VID_SHINKO, USB_PID_SHINKO_S6145, P_SHINKO_S6145, NULL, "shinko-chcs6145"}, /* Duplicate */
		{ USB_VID_SHINKO, USB_PID_SHINKO_S6145D, P_SHINKO_S6145D, NULL, "ciaat-brava-21"},
		{ USB_VID_SHINKO, USB_PID_SHINKO_S2245, P_SHINKO_S2245, NULL, "sinfonia-chcs2245"},
		{ USB_VID_KODAKALARIS, USB_PID_KA_6900, P_SHINKO_S2245, NULL, "kodak-6900"},
		{ USB_VID_HITI, USB_PID_HITI_M610, P_SHINKO_S2245, NULL, "hiti-m610"},
		{ 0, 0, 0, NULL, NULL}
	}
};

/* CHC-S6145 spool file format

  Spool file consists of an 116-byte header, followed by RGB-packed data,
  followed by a 4-byte footer.  Header appears to consist of a series of
  4-byte Little Endian words.

   10 00 00 00 MM MM 00 00  HH 00 00 00 01 00 00 00  MM == Model (ie 6145d), HH == 0x02 (5" media), 0x03 (6" media)
   64 00 00 00 00 00 00 00  TT 00 00 00 00 00 00 00  TT == 0x08 5x5, 0x03 5x7, 0x07 2x6, 0x00 4x6, 0x06 6x6/6x6+6x2/6x8
   UU 00 00 00 ZZ 00 00 00  XX 00 00 00 00 00 00 00  XX == 0x00 default, 0x02 glossy, 0x03 matte, ZZ == 0x00 default, 0x01 == std qual; UU == 0x00 normal, 0x04 2x6*2, 0x05 6x6+2x6
   00 00 00 00 WW WW 00 00  HH HH 00 00 NN 00 00 00  WW/HH Width, Height (LE), NN == Copies
   00 00 00 00 00 00 00 00  00 00 00 00 ce ff ff ff
   00 00 00 00 ce ff ff ff  QQ QQ 00 00 ce ff ff ff  QQ == DPI (300)
   00 00 00 00 ce ff ff ff  00 00 00 00 00 00 00 00
   00 00 00 00

   [[Packed RGB payload of WW*HH*3 bytes]]

   04 03 02 01  [[ footer ]]

*/