forked from koenkooi/foo2zjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo2lava.c
1703 lines (1511 loc) · 41.9 KB
/
foo2lava.c
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
/*
GENERAL
This program converts pbm (B/W) images and 1-bit-per-pixel cmyk images
(both produced by ghostscript) to Zenographics LAVAFLOW format.
With this utility, you can print to some HP and Minolta/QMS printers,
such as these:
Model 0:
- Konica Minolta magicolor 2490 MF B/W and color
- Konica Minolta magicolor 2530 DL B/W and color
- Xerox Phaser 6115MFP B/W and color
Model 1:
- Konica Minolta magicolor 2480 MF B/W and color
NOTE: Copies is unimplented!
Model 2:
- Konica Minolta magicolor 1600W B/W and color
- Konica Minolta magicolor 1680MF B/W and color
- Konica Minolta magicolor 1690MF B/W and color
- Konica Minolta magicolor 4690MF B/W and color
- Oki C110 B/W and color
- Xerox Phaser 6121MFP B/W and color
AUTHORS
It also uses Markus Kuhn's jbig-kit compression library (included, but
also available at http:https://www.cl.cam.ac.uk/~mgk25/jbigkit/).
You can contact the current author at mailto:[email protected]
LICENSE
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
If you want to use this program under different license conditions,
then contact the author for an arrangement.
It is possible that certain products which can be built using the jbig
software module might form inventions protected by patent rights in
some countries (e.g., by patents about arithmetic coding algorithms
owned by IBM and AT&T in the USA). Provision of this software by the
author does NOT include any licenses for any patents. In those
countries where a patent license is required for certain applications
of this software module, you will have to obtain such a license
yourself.
*/
static char Version[] = "$Id: foo2lava.c,v 1.40 2014/06/27 14:34:46 rick Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#ifdef linux
#include <sys/utsname.h>
#endif
#include "jbig.h"
typedef enum
{
DMDUPLEX_OFF = 1,
DMDUPLEX_LONGEDGE = 2,
DMDUPLEX_SHORTEDGE = 3,
DMDUPLEX_MANUALLONG = 4,
DMDUPLEX_MANUALSHORT= 5
} DMDUPLEX;
/*
* Command line options
*/
int Debug = 0;
int ResX = 1200;
int ResY = 600;
int Bpp = 1;
int PaperCode = 1;
char *PaperStr = "na_letter_8.5x11in";
int PageWidth = 1200 * 8.5;
int PageHeight = 600 * 11;
int UpperLeftX = 0;
int UpperLeftY = 0;
int LowerRightX = 0;
int LowerRightY = 0;
int Copies = 1;
int Duplex = 1;
int SourceCode = 255;
int MediaCode = 0;
char *MediaStr = "plain";
char *Username = NULL;
char *Filename = NULL;
int Mode = 0;
#define MODE_MONO 1
#define MODE_COLOR 2
int Model = 0;
#define MODEL_2530DL 0
#define MODEL_2480MF 1
#define MODEL_1600W 2
#define MODEL_LAST 2
int Color2Mono = 0;
int BlackClears = 0;
int AllIsBlack = 0;
int OutputStartPlane = 1;
int ExtraPad = 16;
int LogicalOffsetX = 0;
int LogicalOffsetY = 0;
#define LOGICAL_CLIP_X 2
#define LOGICAL_CLIP_Y 1
int LogicalClip = LOGICAL_CLIP_X | LOGICAL_CLIP_Y;
int SaveToner = 0;
int PageNum = 0;
int RealWidth;
int EconoMode = 0;
int BihW;
int BihH;
int Dots[4];
int TotalDots;
int IsCUPS = 0;
FILE *EvenPages = NULL;
typedef struct
{
off_t b, e;
} SEEKREC;
SEEKREC SeekRec[2000];
int SeekIndex = 0;
off_t SeekMedia;
long JbgOptions[5] =
{
/* Order */
JBG_ILEAVE | JBG_SMID,
/* Options */
JBG_DELAY_AT | JBG_LRLTWO | JBG_TPDON | JBG_TPBON | JBG_DPON,
/* L0 */
128,
/* MX */
0,
/* MY */
0
};
void
usage(void)
{
fprintf(stderr,
"Usage:\n"
" foo2lava [options] <pbmraw-file >lava-file\n"
"\n"
" Convert Ghostscript pbmraw format to a monochrome LAVAFLOW stream,\n"
" for driving the Konica Minolta magicolor 2530 DL network color laser\n"
" printer and other LAVAFLOW-based black and white printers.\n"
"\n"
" gs -q -dBATCH -dSAFER -dQUIET -dNOPAUSE \\ \n"
" -sPAPERSIZE=letter -r1200x600 -sDEVICE=pbmraw \\ \n"
" -sOutputFile=- - < testpage.ps \\ \n"
" | foo2lava -r1200x600 -g10200x6600 -p1 >testpage.zm\n"
"\n"
" foo2lava [options] <bitcmyk-file >lava-file\n"
" foo2lava [options] <pksmraw-file >lava-file\n"
"\n"
" Convert Ghostscript bitcmyk or pksmraw format to a color LAVAFLOW\n"
" stream, for driving the Konica Minolta magicolor 2530 DL network\n"
" color laser printer.\n"
" N.B. Color correction is expected to be performed by ghostscript.\n"
"\n"
" gs -q -dBATCH -dSAFER -dQUIET -dNOPAUSE \\ \n"
" -sPAPERSIZE=letter -g10200x6600 -r1200x600 -sDEVICE=bitcmyk \\ \n"
" -sOutputFile=- - < testpage.ps \\ \n"
" | foo2lava -r1200x600 -g10200x6600 -p1 >testpage.zc\n"
"\n"
"Normal Options:\n"
"-c Force color mode if autodetect doesn't work\n"
"-d duplex Duplex code to send to printer [%d]\n"
" 1=off, 2=longedge, 3=shortedge\n"
" 4=manual longedge, 5=manual shortedge\n"
"-g <xpix>x<ypix> Set page dimensions in pixels [%dx%d]\n"
"-m media Media code to send to printer [%d]\n"
" 1=standard 4=transparency 20=thick stock 22=envelope\n"
" 23=letterhead 25=postcard 26=labels 27=recycled\n"
" 28=glossy\n"
"-p paper Paper code to send to printer [%d]\n"
" 1=executive 2=letter 3=legal 25=A5 26=A4 45=B5jis\n"
" 65=B5iso 80=envMonarch 81=env#10 90=envDL 91=envC5\n"
" 92=envC6 835=4x6\" 837=10x15cm\n"
"-n copies Number of copies [%d]\n"
"-r <xres>x<yres> Set device resolution in pixels/inch [%dx%d]\n"
"-s source Source code to send to printer [%d]\n"
" 1=tray 1 (multipurpose) 4=tray 2 255=auto\n"
" Code numbers may vary with printer model\n"
"-t Draft mode. Every other pixel is white.\n"
"-J filename Filename string to send to printer [%s]\n"
"-U username Username string to send to printer [%s]\n"
"\n"
"Printer Tweaking Options:\n"
"-u <xoff>x<yoff> Set offset of upper left printable in pixels [%dx%d]\n"
"-l <xoff>x<yoff> Set offset of lower right printable in pixels [%dx%d]\n"
"-L mask Send logical clipping values from -u/-l in ZjStream [%d]\n"
" 0=no, 1=Y, 2=X, 3=XY\n"
"-A AllIsBlack: convert C=1,M=1,Y=1 to just K=1\n"
"-B BlackClears: K=1 forces C,M,Y to 0\n"
" -A, -B work with bitcmyk input only\n"
"-P Do not output START_PLANE codes. May be needed by some\n"
" some black and white only printers.\n"
"-X padlen Add extra zero padding to the end of BID segments [%d]\n"
"-z model Model: 0=2530DL, 2=1600W (LAVAFLOW), 1=2480MF (OPL) [%d]\n"
"\n"
"Debugging Options:\n"
"-S plane Output just a single color plane from a color print [all]\n"
" 1=Cyan, 2=Magenta, 3=Yellow, 4=Black\n"
"-D lvl Set Debug level [%d]\n"
"-V Version %s\n"
, Duplex
, PageWidth , PageHeight
, MediaCode
, PaperCode
, Copies
, ResX , ResY
, SourceCode
, Filename ? Filename : ""
, Username ? Username : ""
, UpperLeftX , UpperLeftY
, LowerRightX , LowerRightY
, LogicalClip
, ExtraPad
, Model
, Debug
, Version
);
exit(1);
}
/*
* Mirror1: bits 01234567 become 76543210
*/
unsigned char Mirror1[256] =
{
0,128, 64,192, 32,160, 96,224, 16,144, 80,208, 48,176,112,240,
8,136, 72,200, 40,168,104,232, 24,152, 88,216, 56,184,120,248,
4,132, 68,196, 36,164,100,228, 20,148, 84,212, 52,180,116,244,
12,140, 76,204, 44,172,108,236, 28,156, 92,220, 60,188,124,252,
2,130, 66,194, 34,162, 98,226, 18,146, 82,210, 50,178,114,242,
10,138, 74,202, 42,170,106,234, 26,154, 90,218, 58,186,122,250,
6,134, 70,198, 38,166,102,230, 22,150, 86,214, 54,182,118,246,
14,142, 78,206, 46,174,110,238, 30,158, 94,222, 62,190,126,254,
1,129, 65,193, 33,161, 97,225, 17,145, 81,209, 49,177,113,241,
9,137, 73,201, 41,169,105,233, 25,153, 89,217, 57,185,121,249,
5,133, 69,197, 37,165,101,229, 21,149, 85,213, 53,181,117,245,
13,141, 77,205, 45,173,109,237, 29,157, 93,221, 61,189,125,253,
3,131, 67,195, 35,163, 99,227, 19,147, 83,211, 51,179,115,243,
11,139, 75,203, 43,171,107,235, 27,155, 91,219, 59,187,123,251,
7,135, 71,199, 39,167,103,231, 23,151, 87,215, 55,183,119,247,
15,143, 79,207, 47,175,111,239, 31,159, 95,223, 63,191,127,255,
};
/*
* Mirror2: bits 01234567 become 67452301
*/
unsigned char Mirror2[256] =
{
0, 64,128,192, 16, 80,144,208, 32, 96,160,224, 48,112,176,240,
4, 68,132,196, 20, 84,148,212, 36,100,164,228, 52,116,180,244,
8, 72,136,200, 24, 88,152,216, 40,104,168,232, 56,120,184,248,
12, 76,140,204, 28, 92,156,220, 44,108,172,236, 60,124,188,252,
1, 65,129,193, 17, 81,145,209, 33, 97,161,225, 49,113,177,241,
5, 69,133,197, 21, 85,149,213, 37,101,165,229, 53,117,181,245,
9, 73,137,201, 25, 89,153,217, 41,105,169,233, 57,121,185,249,
13, 77,141,205, 29, 93,157,221, 45,109,173,237, 61,125,189,253,
2, 66,130,194, 18, 82,146,210, 34, 98,162,226, 50,114,178,242,
6, 70,134,198, 22, 86,150,214, 38,102,166,230, 54,118,182,246,
10, 74,138,202, 26, 90,154,218, 42,106,170,234, 58,122,186,250,
14, 78,142,206, 30, 94,158,222, 46,110,174,238, 62,126,190,254,
3, 67,131,195, 19, 83,147,211, 35, 99,163,227, 51,115,179,243,
7, 71,135,199, 23, 87,151,215, 39,103,167,231, 55,119,183,247,
11, 75,139,203, 27, 91,155,219, 43,107,171,235, 59,123,187,251,
15, 79,143,207, 31, 95,159,223, 47,111,175,239, 63,127,191,255,
};
/*
* Mirror4: bits 01234567 become 45670123
*/
unsigned char Mirror4[256] =
{
0, 16, 32, 48, 64, 80, 96,112,128,144,160,176,192,208,224,240,
1, 17, 33, 49, 65, 81, 97,113,129,145,161,177,193,209,225,241,
2, 18, 34, 50, 66, 82, 98,114,130,146,162,178,194,210,226,242,
3, 19, 35, 51, 67, 83, 99,115,131,147,163,179,195,211,227,243,
4, 20, 36, 52, 68, 84,100,116,132,148,164,180,196,212,228,244,
5, 21, 37, 53, 69, 85,101,117,133,149,165,181,197,213,229,245,
6, 22, 38, 54, 70, 86,102,118,134,150,166,182,198,214,230,246,
7, 23, 39, 55, 71, 87,103,119,135,151,167,183,199,215,231,247,
8, 24, 40, 56, 72, 88,104,120,136,152,168,184,200,216,232,248,
9, 25, 41, 57, 73, 89,105,121,137,153,169,185,201,217,233,249,
10, 26, 42, 58, 74, 90,106,122,138,154,170,186,202,218,234,250,
11, 27, 43, 59, 75, 91,107,123,139,155,171,187,203,219,235,251,
12, 28, 44, 60, 76, 92,108,124,140,156,172,188,204,220,236,252,
13, 29, 45, 61, 77, 93,109,125,141,157,173,189,205,221,237,253,
14, 30, 46, 62, 78, 94,110,126,142,158,174,190,206,222,238,254,
15, 31, 47, 63, 79, 95,111,127,143,159,175,191,207,223,239,255,
};
int BlackOnes[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
void
rotate_bytes_180(unsigned char *sp, unsigned char *ep, unsigned char *mirror)
{
unsigned char tmp;
while (sp < ep)
{
tmp = mirror[*sp];
*sp = mirror[*ep];
*ep = tmp;
++sp;
--ep;
}
if (sp == ep)
*sp = mirror[*sp];
}
void
debug(int level, char *fmt, ...)
{
va_list ap;
if (Debug < level)
return;
setvbuf(stderr, (char *) NULL, _IOLBF, BUFSIZ);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void
error(int fatal, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fatal)
exit(fatal);
}
/*
* A linked list of compressed data
*/
typedef struct _BIE_CHAIN{
unsigned char *data;
size_t len;
struct _BIE_CHAIN *next;
} BIE_CHAIN;
void
free_chain(BIE_CHAIN *chain)
{
BIE_CHAIN *next;
next = chain;
while ((chain = next))
{
next = chain->next;
if (chain->data)
free(chain->data);
free(chain);
}
}
int
write_plane(int planeNum, BIE_CHAIN **root, FILE *fp)
{
BIE_CHAIN *current = *root;
BIE_CHAIN *next;
int len;
debug(3, "Write Plane %d\n", planeNum);
/* error handling */
if (!current)
error(1,"There is no JBIG!\n");
if (!current->next)
error(1,"There is no or wrong JBIG header!\n");
if (current->len != 20)
error(1,"wrong BIH length\n");
if (Model == MODEL_2480MF)
{
int w, h, x = 0;
switch (planeNum)
{
case 1: x = 0x00FFFF; break;
case 2: x = 0xFF00FF; break;
case 3: x = 0xFFFF00; break;
}
fprintf(fp, "RasterObject.Planes=%06X,0,0,0,0,0,0;", x);
/* startpage, jbig_bih, jbig_bid, jbig_end, endpage */
w = (((long) current->data[ 4] << 24)
| ((long) current->data[ 5] << 16)
| ((long) current->data[ 6] << 8)
| (long) current->data[ 7]);
h = (((long) current->data[ 8] << 24)
| ((long) current->data[ 9] << 16)
| ((long) current->data[10] << 8)
| (long) current->data[11]);
fprintf(fp, "RasterObject.Width=%d;", w);
fprintf(fp, "RasterObject.Height=%d;", h);
}
for (current = *root; current && current->len; current = current->next)
{
if (current == *root)
{
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
fprintf(fp, "\033*b20V");
fwrite(current->data, 1, current->len, fp);
break;
case MODEL_2480MF:
fprintf(fp, "RasterObject.Data#%d=", (int) current->len);
fwrite(current->data, 1, current->len, fp);
fprintf(fp, ";");
break;
}
}
else
{
int i;
int pad;
#define PAD 32
len = current->len;
next = current->next;
if (!next || !next->len)
pad = PAD * ((len+PAD-1)/PAD) - len;
else
pad = 0;
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
fprintf(fp, "\033*b%d%s", len + pad, next ? "V" : "W");
fwrite(current->data, 1, len, fp);
for (i = 0; i < pad; i++)
putc(0, fp);
break;
case MODEL_2480MF:
fprintf(fp, "RasterObject.Data#%d=", len + pad);
fwrite(current->data, 1, len, fp);
for (i = 0; i < pad; i++)
putc(0, fp);
fprintf(fp, ";");
break;
}
}
}
free_chain(*root);
if (Model == MODEL_2530DL || Model == MODEL_1600W)
{
switch (planeNum)
{
case 0: case 4:
fprintf(fp, "\033*x%dK", Dots[3]);
fprintf(fp, "\033*x%dW", TotalDots - Dots[3]);
break;
case 1:
fprintf(fp, "\033*x%dC", Dots[0]);
fprintf(fp, "\033*x%dZ", TotalDots - Dots[0]);
break;
case 2:
fprintf(fp, "\033*x%dM", Dots[1]);
fprintf(fp, "\033*x%dV", TotalDots - Dots[1]);
break;
case 3:
fprintf(fp, "\033*x%dY", Dots[2]);
fprintf(fp, "\033*x%dU", TotalDots - Dots[2]);
break;
}
}
return 0;
}
void
start_page(BIE_CHAIN **root, int nbie, FILE *ofp)
{
BIE_CHAIN *current = *root;
unsigned long w, h;
int i, np;
static int pageno = 0;
/* error handling */
if (!current)
error(1, "There is no JBIG!\n");
if (!current->next)
error(1, "There is no or wrong JBIG header!\n");
if (current->len != 20 )
error(1,"wrong BIH length\n");
/* startpage, jbig_bih, jbig_bid, jbig_end, endpage */
w = (((long) current->data[ 4] << 24)
| ((long) current->data[ 5] << 16)
| ((long) current->data[ 6] << 8)
| (long) current->data[ 7]);
h = (((long) current->data[ 8] << 24)
| ((long) current->data[ 9] << 16)
| ((long) current->data[10] << 8)
| (long) current->data[11]);
TotalDots = w*h;
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
fprintf(ofp, "\033&l%dO", 0);
fprintf(ofp, "\033*r%dU",
(Mode == MODE_COLOR && nbie == 4) ? -1004 : 1);
fprintf(ofp, "\033*g%dW",
(Mode == MODE_COLOR && nbie == 4) ? 26 : 8);
putc(2, ofp);
putc(nbie, ofp);
np = (Mode == MODE_COLOR && nbie == 4) ? 4 : 1;
for (i = 0; i < np; ++i)
{
putc(ResX>>8, ofp);
putc(ResX, ofp);
putc(ResY>>8, ofp);
putc(ResY, ofp);
putc(0, ofp);
//putc(Mode == MODE_COLOR ? 2 : 0, ofp); // Number of bpc
putc(2, ofp);
}
fprintf(ofp, "\033*b%dM", 1234);
fprintf(ofp, "\033&l%dA", PaperCode);
if (PaperCode == 101)
{
fprintf(ofp, "\033&f%dG", PageHeight * 720 / ResY); // Custom Y
fprintf(ofp, "\033&f%dF", PageWidth * 720 / ResX); // Custom X
}
fprintf(ofp, "\033&l%dH", SourceCode);
fprintf(ofp, "\033&l%dM", MediaCode);
fprintf(ofp, "\033&l%dE", 0);
fprintf(ofp, "\033*r%dS", (int) w);
fprintf(ofp, "\033*r%dT", (int) h);
fprintf(ofp, "\033&l%dU", 0);
fprintf(ofp, "\033&l%dZ", 0);
fprintf(ofp, "\033*p%dX", ResX / 6);
fprintf(ofp, "\033*p%dY", ResX / 6);
fprintf(ofp, "\033*r1A");
break;
case MODEL_2480MF:
fprintf(ofp, "MediaSize=%s;", PaperStr);
fprintf(ofp, "MediaType=%s;", MediaStr);
fprintf(ofp, "MediaInputTrayCheck=top;");
fprintf(ofp, "RasterObject.BitsPerPixel=1;");
break;
}
if ((PageNum & 1) == 0 && EvenPages)
SeekMedia = ftell(EvenPages) - 4;
++pageno;
if (IsCUPS)
fprintf(stderr, "PAGE: %d %d\n", pageno, Copies);
}
void
end_page(FILE *ofp)
{
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
fprintf(ofp, "\033*rC");
if (0)
fprintf(ofp, "\f"); // Eject page
else
fprintf(ofp, "\033&l0H"); // Eject page
break;
case MODEL_2480MF:
fprintf(ofp, "Event=EndOfPage;");
break;
}
}
int
write_page(BIE_CHAIN **root, BIE_CHAIN **root2,
BIE_CHAIN **root3, BIE_CHAIN **root4, FILE *ofp)
{
int nbie = root2 ? 4 : 1;
start_page(root, nbie, ofp);
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
if (root3) write_plane(3, root3, ofp);
if (root2) write_plane(2, root2, ofp);
if (root)
{
if (OutputStartPlane)
write_plane(nbie == 1 ? 4 : 1, root, ofp);
else
write_plane(nbie == 1 ? 0 : 1, root, ofp);
}
if (root4) write_plane(4, root4, ofp);
break;
case MODEL_2480MF:
if (root)
{
if (OutputStartPlane)
write_plane(nbie == 1 ? 4 : 1, root, ofp);
else
write_plane(nbie == 1 ? 0 : 1, root, ofp);
}
if (root2) write_plane(2, root2, ofp);
if (root3) write_plane(3, root3, ofp);
if (root4) write_plane(4, root4, ofp);
break;
}
end_page(ofp);
return 0;
}
/*
* This creates a linked list of compressed data. The first item
* in the list is the BIH and is always 20 bytes in size. Each following
* item is 65536 bytes in length. The last item length is whatever remains.
*/
void
output_jbig(unsigned char *start, size_t len, void *cbarg)
{
BIE_CHAIN *current, **root = (BIE_CHAIN **) cbarg;
int size = 65536; // Printer does strange things otherwise.
if (Model == MODEL_2480MF)
size = 32768;
if ( (*root) == NULL)
{
(*root) = malloc(sizeof(BIE_CHAIN));
if (!(*root))
error(1, "Can't allocate space for chain\n");
(*root)->data = NULL;
(*root)->next = NULL;
(*root)->len = 0;
size = 20;
if (len != 20)
error(1, "First chunk must be BIH and 20 bytes long\n");
}
current = *root;
while (current->next)
current = current->next;
while (len > 0)
{
int amt, left;
if (!current->data)
{
current->data = malloc(size);
if (!current->data)
error(1, "Can't allocate space for compressed data\n");
}
left = size - current->len;
amt = (len > left) ? left : len;
memcpy(current->data + current->len, start, amt);
current->len += amt;
len -= amt;
start += amt;
if (current->len == size)
{
current->next = malloc(sizeof(BIE_CHAIN));
if (!current->next)
error(1, "Can't allocate space for chain\n");
current = current->next;
current->data = NULL;
current->next = NULL;
current->len = 0;
}
}
}
void
start_doc(FILE *fp)
{
char buf[64];
time_t now;
struct tm *tmp;
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
now = time(NULL);
tmp = localtime(&now);
strftime(buf, sizeof(buf), "%m/%d/%Y", tmp);
fprintf(fp, "\033%%-12345X@PJL JOB NAME=\"%s\"\n",
Filename ? Filename : "stdin");
fprintf(fp, "\033%%-12345X@PJL JOB USERNAME=\"%s\"\n",
Username ? Username : "");
fprintf(fp, "\033%%-12345X@PJL JOB TIMESTAMP=\"%s\"\n", buf);
#ifdef linux
{
struct utsname u;
u.release[0] = 0;
uname(&u);
fprintf(fp, "\033%%-12345X@PJL JOB OSINFO=\"Linux/%s\"\n",
u.release);
}
#else
fprintf(fp, "\033%%-12345X@PJL JOB OSINFO=\"%s\"\n", "Unknown");
#endif
fprintf(fp, "\033%%-12345X@PJL ENTER LANGUAGE=LAVAFLOW\n");
fprintf(fp, "\033E");
fprintf(fp, "\033&l%dS", Duplex-1);
fprintf(fp, "\033&l%dG", 0);
fprintf(fp, "\033&u%dD", ResX);
fprintf(fp, "\033&l%dX", Copies);
fprintf(fp, "\033&x%dX", 1);
break;
case MODEL_2480MF:
fprintf(fp, "Event=StartOfJob;");
fprintf(fp, "OSVersion=Linux;");
fprintf(fp, "DrvVersion=2.0.1410.0;");
fprintf(fp, "Resolution=%dx%d;", ResX, ResY);
fprintf(fp, "RasterObject.Compression=JBIG;");
fprintf(fp, "Sides=%sSided;", (Duplex-1) ? "Two" : "One");
break;
}
}
void
end_doc(FILE *fp)
{
switch (Model)
{
case MODEL_1600W:
case MODEL_2530DL:
fprintf(fp, "\033E");
fprintf(fp, "\033%%-12345X");
break;
case MODEL_2480MF:
fprintf(fp, "Event=EndOfJob;");
break;
}
}
void
load_tray2(FILE *fp)
{
//int nitems;
//nitems = 0;
//chunk_write(ZJT_2600N_PAUSE, nitems, nitems * sizeof(ZJ_ITEM_UINT32), fp);
}
int
compute_image_dots(int w, int h, unsigned char *bitmap)
{
int dots = 0;
int x, y, bpl;
bpl = (w + 7) / 8;
for (y = 0; y < h; ++y)
for (x = 0; x < bpl; ++x)
dots += BlackOnes[ bitmap[y*bpl + x] ];
return dots;
}
static int AnyColor;
void
cmyk_planes(unsigned char *plane[4], unsigned char *raw, int w, int h)
{
int rawbpl = (w+1) / 2;
int bpl = (w + 7) / 8;
int i;
int x, y;
unsigned char byte;
unsigned char mask[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
int aib = AllIsBlack;
int bc = BlackClears;
AnyColor = 0;
for (i = 0; i < 4; ++i)
memset(plane[i], 0, bpl * h);
//
// Unpack the combined plane into individual color planes
//
// TODO: this can be speeded up using a 256 or 65536 entry lookup table
//
for (y = 0; y < h; ++y)
{
for (x = 0; x < w; ++x)
{
byte = raw[y*rawbpl + x/2];
if (aib && (byte & 0xE0) == 0xE0)
{
plane[3][y*bpl + x/8] |= mask[x&7];
}
else if (byte & 0x10)
{
plane[3][y*bpl + x/8] |= mask[x&7];
if (!bc)
{
if (byte & 0x80) plane[0][y*bpl + x/8] |= mask[x&7];
if (byte & 0x40) plane[1][y*bpl + x/8] |= mask[x&7];
if (byte & 0x20) plane[2][y*bpl + x/8] |= mask[x&7];
if (byte & 0xE0) AnyColor |= byte;
}
}
else
{
if (byte & 0x80) plane[0][y*bpl + x/8] |= mask[x&7];
if (byte & 0x40) plane[1][y*bpl + x/8] |= mask[x&7];
if (byte & 0x20) plane[2][y*bpl + x/8] |= mask[x&7];
if (byte & 0xE0) AnyColor |= byte;
}
++x;
if (aib && (byte & 0x0E) == 0x0E)
{
plane[3][y*bpl + x/8] |= mask[x&7];
}
else if (byte & 0x1)
{
plane[3][y*bpl + x/8] |= mask[x&7];
if (!bc)
{
if (byte & 0x8) plane[0][y*bpl + x/8] |= mask[x&7];
if (byte & 0x4) plane[1][y*bpl + x/8] |= mask[x&7];
if (byte & 0x2) plane[2][y*bpl + x/8] |= mask[x&7];
if (byte & 0xE) AnyColor |= byte;
}
}
else
{
if (byte & 0x8) plane[0][y*bpl + x/8] |= mask[x&7];
if (byte & 0x4) plane[1][y*bpl + x/8] |= mask[x&7];
if (byte & 0x2) plane[2][y*bpl + x/8] |= mask[x&7];
if (byte & 0xE) AnyColor |= byte;
}
}
}
debug(2, "BlackClears = %d; AnyColor = %s %s %s\n",
BlackClears,
(AnyColor & 0x88) ? "Cyan" : "",
(AnyColor & 0x44) ? "Magenta" : "",
(AnyColor & 0x22) ? "Yellow" : ""
);
}
int
cmyk_page(unsigned char *raw, int w, int h, FILE *ofp)
{
BIE_CHAIN *chain[4];
int i;
int bpl = (w + 7) / 8;
unsigned char *plane[4], *bitmaps[4][1];
struct jbg_enc_state se[4];
RealWidth = w;
for (i = 0; i < 4; ++i)
{
plane[i] = malloc(bpl * h);
if (!plane[i]) error(3, "Cannot allocate space for bit plane\n");
chain[i] = NULL;
}
cmyk_planes(plane, raw, w, h);
for (i = 0; i < 4; ++i)
{
if (Debug >= 9)
{
FILE *dfp;
char fname[256];
sprintf(fname, "xxxplane%d", i);
dfp = fopen(fname, "w");
if (dfp)
{
fwrite(plane[i], bpl*h, 1, dfp);
fclose(dfp);
}
}
Dots[i] = compute_image_dots(w, h, plane[i]);
*bitmaps[i] = plane[i];
jbg_enc_init(&se[i], w, h, 1, bitmaps[i], output_jbig, &chain[i]);
jbg_enc_options(&se[i], JbgOptions[0], JbgOptions[1],
JbgOptions[2], JbgOptions[3], JbgOptions[4]);
jbg_enc_out(&se[i]);
jbg_enc_free(&se[i]);
}
if (Color2Mono)
write_page(&chain[Color2Mono-1], NULL, NULL, NULL, ofp);
else if (AnyColor)
write_page(&chain[0], &chain[1], &chain[2], &chain[3], ofp);
else
write_page(&chain[3], NULL, NULL, NULL, ofp);
for (i = 0; i < 4; ++i)
free(plane[i]);
return 0;
}
int
pksm_page(unsigned char *plane[4], int w, int h, FILE *ofp)
{
BIE_CHAIN *chain[4];
int i;
unsigned char *bitmaps[4][1];
struct jbg_enc_state se[4];
RealWidth = w;
for (i = 0; i < 4; ++i)
chain[i] = NULL;
for (i = 0; i < 4; ++i)
{
Dots[i] = compute_image_dots(w, h, plane[i]);
*bitmaps[i] = plane[i];
jbg_enc_init(&se[i], w, h, 1, bitmaps[i], output_jbig, &chain[i]);
jbg_enc_options(&se[i], JbgOptions[0], JbgOptions[1],
JbgOptions[2], JbgOptions[3], JbgOptions[4]);
jbg_enc_out(&se[i]);
jbg_enc_free(&se[i]);
}
if (Color2Mono)
write_page(&chain[Color2Mono-1], NULL, NULL, NULL, ofp);
else if (AnyColor)
write_page(&chain[0], &chain[1], &chain[2], &chain[3], ofp);
else
write_page(&chain[3], NULL, NULL, NULL, ofp);
return 0;
}