-
Notifications
You must be signed in to change notification settings - Fork 0
/
appliers.cpp
2242 lines (2106 loc) · 91.9 KB
/
appliers.cpp
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
/*
* appliers.cpp -- specific OutputRule implementations
* by [email protected] at Sat Mar 16 14:45:02 CET 2002
* Blanca JAI additions at Tue May 21 13:18:26 CEST 2002
* TIFF output at Tue Jun 4 19:50:45 CEST 2002
*/
#ifdef __GNUC__
#ifndef __clang__
#pragma implementation
#endif
#endif
#include "rule.hpp"
#include "error.hpp"
#include "encoder.hpp"
#include "in_jai.hpp"
#include "crc32.h" /* crc32() used by out_png_work() */
#include <string.h>
/** Appends 4 bytes in MSB first (network) byte order */
static inline void mf32(char *&p, slen_t u32) {
p[0]=(u32>>24)&255;
p[1]=(u32>>16)&255;
p[2]=(u32>> 8)&255;
p[3]=(u32 )&255;
p+=4;
}
/** Appends 4 bytes in LSB first (PC) byte order */
static inline void lf32(char *&p, slen_t u32) {
p[0]=(u32 )&255;
p[1]=(u32>> 8)&255;
p[2]=(u32>>16)&255;
p[3]=(u32>>24)&255;
p+=4;
}
/** Appends 2 bytes in LSB first (PC) byte order */
static inline void lf16(char *&p, unsigned u16) {
p[0]=(u16 )&255;
p[1]=(u16>> 8)&255;
p+=2;
}
/* --- at Sat Mar 23 15:42:17 CET 2002, removed obsolete
* PostScript Level2 FlateEncode or LZWEncode filter, predictors supported
* written at Mar 16 14:48:27 CET 2002
*/
/* --- Sun Mar 17 15:43:20 CET 2002 */
#if 0
/* l1fa85g.tte was a special .tte that is not built in to bts.ttt, but I've
* integrated it into bts2.ttt at Sun Sep 22 00:48:21 CEST 2002. I've also
* integrated _l1fa85g_ to _l1c_ that day.
*/
static char *l1fa85g_tte=
#include "l1fa85g.tth"
/** PostScript Level1 FlateEncode, A85, without Predictor */
Rule::Applier::cons_t out_l1fa85g_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (!cache->isPS()
|| cache->Compression!=Rule::Cache::CO_ZIP
|| cache->hasPredictor()
|| !cache->isGray()
|| cache->TransferEncoding!=cache->TE_A85
|| (!cache->WarningOK && !cache->isPSL3())
) return Rule::Applier::DONT_KNOW;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_l1fa85g_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
/* by [email protected] at Sun Mar 17 15:52:48 CET 2002 */
// Imp: two other TransferEncodings [no more, since #if 0]
if (out_l1fa85g_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
if (!or_->cache.isPSL3())
Error::sev(Error::WARNING_DEFER) << "l1fa85g: /ZIP without /PSL3 will be slow" << (Error*)0;
or_->doSampleFormat(sf);
PSEncoder *bp=PSEncoder::newASCII85Encode(out, or_->cacheHints.TransferCPL);
PSEncoder *cp=PSEncoder::newFlateEncode(*bp, or_->cacheHints.Effort);
Rule::writeTTE(out, out, *cp, l1fa85g_tte, or_, sf, Rule::writeData);
delete bp;
delete cp;
return Rule::Applier::OK;
}
Rule::Applier out_l1fa85g_applier = { "l1fa85g", out_l1fa85g_check_rule, out_l1fa85g_work, 0 };
#endif
/* l2jbin --- Sun Mar 17 21:45:22 CET 2002
* p0jbin (integrated to p0jbin) --- Mon Apr 15 23:29:13 CEST 2002
*/
//static char *l2jbin_tte=
//#include "l2jbin.tth"
/** PostScript Level2 DCTEncode (Baseline JPEG), Binary */
Rule::Applier::cons_t out_l2jbin_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if ((!cache->isPS() && !cache->isPDF())
|| cache->Compression!=Rule::Cache::CO_JAI
) return Rule::Applier::DONT_KNOW;
/* Dat: not unrequired anymore: cache->TransferEncoding!=cache->TE_Binary */
bool badp=false;
if (cache->isPS() && !cache->isPSL2()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /FileFormat/PSL* /Compression/JAI requires /PSL2+" << (Error*)0;
badp=true;
}
if (cache->SampleFormat!=Image::SF_Asis) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /FileFormat/PSL*|PDF* /Compression/JAI requires /SampleFormat/Asis" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /FileFormat/PSL*|PDF* /Compression/JAI requires /Predictor 1" << (Error*)0;
badp=true;
}
return (Rule::Applier::cons_t)(badp ? 0+Rule::Applier::BAD : 0+Rule::Applier::OK);
/* ^^^ 0+: pacify g++-3.1 */
}
Rule::Applier::cons_t out_l2jbin_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
char const*strings[]={ (char const*)NULLP/*LanguageLevel, PDF-1.`0'*/, ""/*colorSpace*/ };
//, " F closefile T closefile"/*closes*/ };
// Error::sev(Error::WARNING_DEFER) << "l2jbin: /ZIP without /PSL3 will be slow" << (Error*)0;
if (out_l2jbin_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
assert(sf->getImg()->getBpc()==8);
or_->doSampleFormat(sf);
// PSEncoder *bp=PSEncoder::newASCII85Encode(out, or_->cacheHints.TransferCPL);
Rule::Cache *cache=&or_->cache;
Filter::VerbatimE outve(out); /* required since template /p0jbin is TTM */
GenBuffer::Writable *op=cache->isPDF() ? &outve : &out, *tp=op;
strings[0]=const_cast<char*>(cache->isPDF() ? (char const*)"0" : (char const*)"2");
if (cache->TransferEncoding==cache->TE_A85) tp=PSEncoder::newASCII85Encode (*op, or_->cacheHints.TransferCPL);
else if (cache->TransferEncoding==cache->TE_Hex) tp=PSEncoder::newASCIIHexEncode(*op, or_->cacheHints.TransferCPL);
// else strings[2]=" F closefile";
Rule::writeTTT(*op, *tp, *tp, !cache->isPDF()?"l2jbin":cache->isPDFB()?"p0jbb":"p0jbin", or_, sf, Rule::writePalData, strings);
if (tp!=op) delete tp;
/* Dat: outve is deleted by C++ scope */
// if (tp!=op) delete tp; /* BUGFIX at Thu Jan 20 15:04:47 CET 2005 */
return Rule::Applier::OK;
}
Rule::Applier out_l2jbin_applier = { "PSL2+PDF-JAI", out_l2jbin_check_rule, out_l2jbin_work, 0 };
/* --- */
#if 0 /* integrated to _l2jbin_ at Sun Sep 22 14:29:13 CEST 2002 */
//static char *p0jbin_ttm=
//#include "p0jbin.tth"
/** PostScript Level2 DCTEncode (Baseline JPEG), Binary */
Rule::Applier::cons_t out_p0jbin_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (!cache->isPDF()
|| cache->Compression!=Rule::Cache::CO_JAI
) return Rule::Applier::DONT_KNOW;
/* Dat: not unrequired anymore: cache->TransferEncoding!=cache->TE_Binary */
bool badp=false;
if (cache->SampleFormat!=Image::SF_Asis) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /FileFormat/PDF* /Compression/JAI requires /SampleFormat/Asis" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /FileFormat/PDF* /Compression/JAI requires /Predictor 1" << (Error*)0;
badp=true;
}
if (badp) return Rule::Applier::BAD;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_p0jbin_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
char *strings[]={ "0"/*PDF-1.`0'*/, ""/*colorSpace*/ };
// " F closefile"/*closes*/ };
if (out_p0jbin_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
assert(sf->getImg()->getBpc()==8);
or_->doSampleFormat(sf);
Rule::Cache *cache=&or_->cache;
Filter::VerbatimE outve(out); /* required since template /p0jbin is TTM */
GenBuffer::Writable *tp=&outve;
if (cache->TransferEncoding==cache->TE_A85) tp=PSEncoder::newASCII85Encode (outve, or_->cacheHints.TransferCPL);
else if (cache->TransferEncoding==cache->TE_Hex) tp=PSEncoder::newASCIIHexEncode(outve, or_->cacheHints.TransferCPL);
else strings[2]="";
#if 0 /* old */
MiniPS::VALUE ttm;
{ Filter::FlatR flatD(p0jbin_ttm);
MiniPS::Parser p(&flatD);
ttm=p.parse1();
if (p.parse1(p.EOF_ALLOWED)!=MiniPS::Qundef || MiniPS::getType(ttm)!=MiniPS::T_ARRAY)
Error::sev(Error::EERROR) << "TTM: the TTM file should contain a single array" << (Error*)0;
/* ^^^ Dat: the result of the second p.parse1() doesn't get delete0(...)d */
}
Filter::VerbatimE outve(out);
Rule::writeTTM(outve, outve, outve, MiniPS::RARRAY(ttm), or_, sf, Rule::writePalData, strings);
MiniPS::delete0(ttm);
#else /* new */
Rule::writeTTT(outve, *tp, *tp, cache->isPDFB()?"p0jbb":"p0jbin", or_, sf, Rule::writePalData, strings);
#endif
if (tp!=&outve)delete tp;
return Rule::Applier::OK;
}
Rule::Applier out_p0jbin_applier = { "PDF-JAI", out_p0jbin_check_rule, out_p0jbin_work, 0 };
#endif
/* --- Fri Mar 22 11:52:53 CET 2002 */
/* PDF added at Sat Apr 20 20:07:34 CEST 2002 */
/* Integrated l23ind1 at Sat Apr 20 20:24:21 CEST 2002 */
//static char *l23_tte=
//#include "l23.tth"
/** PostScript Level2 and PDF generic non-transparent */
Rule::Applier::cons_t out_l23_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
unsigned char sf=cache->SampleFormat;
if (cache->hasPredictor() && cache->Compression!=Rule::Cache::CO_ZIP && cache->Compression!=Rule::Cache::CO_LZW) {
Error::sev(Error::WARNING_DEFER) << "check_rule: real /Predictor requires /ZIP or /LZW" << (Error*)0;
return Rule::Applier::BAD;
}
// assert(cache->isPSL2() && (sf==Image::SF_Transparent2 || sf==Image::SF_Transparent4 || sf==Image::SF_Transparent8));
if ((!cache->isPSL2() && !cache->isPDF())
|| (sf!=Image::SF_Gray1 && sf!=Image::SF_Gray2 && sf!=Image::SF_Gray4 && sf!=Image::SF_Gray8
&& sf!=Image::SF_Indexed1 && sf!=Image::SF_Indexed2 && sf!=Image::SF_Indexed4 && sf!=Image::SF_Indexed8
&& sf!=Image::SF_Rgb1 && sf!=Image::SF_Rgb2 && sf!=Image::SF_Rgb4 && sf!=Image::SF_Rgb8
&& sf!=Image::SF_Transparent2 && sf!=Image::SF_Transparent4 && sf!=Image::SF_Transparent8
&& sf!=Image::SF_Mask)
|| cache->TransferEncoding==cache->TE_ASCII
|| !cache->isZIPOK()
) return Rule::Applier::DONT_KNOW;
#if !HAVE_LZW
if (cache->Compression==Rule::Cache::CO_LZW) return Rule::Applier::DONT_KNOW;
#endif
// if (cache->isDCTE() && !cache->isRGB() && !cache->isGray()) {
bool badp=false;
if (cache->isDCTE() && cache->SampleFormat!=Image::SF_Rgb8 && cache->SampleFormat!=Image::SF_Gray8 && cache->SampleFormat!=Image::SF_Indexed8) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /DCTEncode requires /Rgb8 or /Gray8 (or /Indexed8)" << (Error*)0;
badp=true;
}
if (sf==Image::SF_Transparent2 || sf==Image::SF_Transparent4 || sf==Image::SF_Transparent8) {
if (cache->isPDF()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: unsupported /Transparent+ for /PDF*" << (Error*)0;
badp=true;
} else assert(cache->isPSL2());
}
return (Rule::Applier::cons_t)(badp ? 0+Rule::Applier::BAD : 0+Rule::Applier::OK);
}
Rule::Applier::cons_t out_l23_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
Rule::stream_writer_t writeXData=Rule::writePalData;
char LanguageLevel[2]="2", closes[30];
SimBuffer::B colorSpace;
char const*strings[]={ LanguageLevel, (char*)NULLP /*colorSpace*/, closes };
Rule::Cache *cache=&or_->cache;
// assert(0);
if (out_l23_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->doSampleFormat(sf, true);
if (cache->Compression==Rule::Cache::CO_ZIP) {
if (!cache->isPDF()) LanguageLevel[0]='3';
} else if (cache->isPDF()) LanguageLevel[0]='0';
if (cache->isIndexed() || cache->isTransparentM()) {
unsigned ncols=PTS_dynamic_cast(Image::Indexed*,sf->getImg())->getNcols();
colorSpace << "[/Indexed/DeviceRGB " << ncols-1;
if (ncols==0) { /* Avoid writing zero bytes to the filters */
/* Imp: verify this in Acrobat Reader */
colorSpace << "()]";
} else if (!cache->isPDF()) { /* Insert an in-line hexdump. No better way :-( */
/*sprintf(colorSpace, "[/Indexed/DeviceRGB %u T %u string readstring pop]", ncols-1, ncols*3); */
colorSpace << " T " << ncols*3 << " string readstring pop]";
} else { /* Insert an in-line hexdump. No shorter way for PDF */
#if 0
colorSpace="/DeviceGray ";
#else
/* SUXX: gs5.50 & PDF & /Indexed doesn't work */
colorSpace << "\n<";
GenBuffer::Writable *hp=PSEncoder::newASCIIHexEncode(colorSpace, or_->cacheHints.TransferCPL);
hp->vi_write(sf->getImg()->getHeadp(), ncols*3);
hp->vi_write(0,0);
delete hp;
colorSpace << ']'; /* Dat: '>' is appended by ASCIIHexEncode */
#endif
writeXData=Rule::writeData; /* The palette has already been written. */
}
} else if (cache->isGray()) colorSpace="/DeviceGray ";
else { assert(cache->isRGB()); colorSpace="/DeviceRGB "; }
if (cache->SampleFormat==Image::SF_Mask || cache->SampleFormat==Image::SF_Indexed1) writeXData=Rule::writeData;
GenBuffer::Writable *vp=&out;
if (cache->isPDF()) vp=new Filter::VerbatimE(out); /* required since template /p02* is TTM */
GenBuffer::Writable *tp=vp;
if (cache->TransferEncoding==cache->TE_A85) tp=PSEncoder::newASCII85Encode (*vp, or_->cacheHints.TransferCPL);
else if (cache->TransferEncoding==cache->TE_Hex) tp=PSEncoder::newASCIIHexEncode(*vp, or_->cacheHints.TransferCPL);
GenBuffer::Writable *cp=tp;
switch (cache->Compression) {
case Rule::Cache::CO_None: break;
case Rule::Cache::CO_ZIP: cp=PSEncoder::newFlateEncode(*tp, or_->cacheHints.Effort); break;
case Rule::Cache::CO_LZW: cp=PSEncoder::newLZWEncode(*tp); break;
case Rule::Cache::CO_RLE: cp=PSEncoder::newRunLengthEncode(*tp, or_->cacheHints.RecordSize); break;
case Rule::Cache::CO_Fax: cp=PSEncoder::newCCITTFaxEncode(*tp, or_->cacheHints.K, or_->cacheHints.EncoderBPL, /*EndOfLine:*/ or_->cacheHints.K>0); break;
/* ^^^ getBpp() BUGFIX at Wed Jul 3 20:00:30 CEST 2002 */
/* ^^^ EndOfLine BUGFIX at Wed Jul 3 21:12:54 CEST 2002
* With EndOfLine==false, `sam2p -c:fax:1', acroread triggers the bug.
* With EndOfLine==false, `sam2p -c:fax:2', acroread and gs trigger the bug.
*/
case Rule::Cache::CO_IJG: cp=PSEncoder::newDCTIJGEncode(*tp, or_->cacheHints.EncoderColumns, or_->cacheHints.EncoderRows, or_->cacheHints.EncoderColors, or_->cacheHints.Quality); break;
case Rule::Cache::CO_DCT: { SimBuffer::B other_parameters;
or_->cacheHints.DCT->dump(other_parameters, 0, false);
cp=PSEncoder::newDCTEncode(*tp, or_->cacheHints.EncoderColumns, or_->cacheHints.EncoderRows, or_->cacheHints.EncoderColors, or_->cacheHints.ColorTransform, other_parameters);
break; }
default: assert(0);
}
GenBuffer::Writable *pp=cp;
if (cache->hasPredictor()) pp=PSEncoder::newPredictor(*cp, cache->Predictor, or_->cacheHints.PredictorBPC, or_->cacheHints.PredictorColumns, or_->cacheHints.PredictorColors);
#if 0 /* Sun Sep 22 20:40:51 CEST 2002 */
if (cp!=tp) strcpy(closes," F closefile"); else closes[0]='\0';
if (tp!=vp) strcpy(closes+strlen(closes)," T closefile");
#endif
strings[1]=colorSpace.term0()();
Rule::writeTTT(*vp, *tp, *pp,
!cache->isPDF() ? (
cache->SampleFormat==Image::SF_Indexed1 ? "l23ind1" :
cache->SampleFormat==Image::SF_Mask ? "l23mask" :
cache->isTransparentM() ? "l23tran2" :
"l23"
) : cache->isPDFB() ? (
cache->SampleFormat==Image::SF_Indexed1 ? "p02ind1bb" :
cache->SampleFormat==Image::SF_Mask ? "p02maskbb" :
"p02bb"
) : (
cache->SampleFormat==Image::SF_Indexed1 ? "p02ind1" :
cache->SampleFormat==Image::SF_Mask ? "p02mask" :
"p02"
), or_, sf, writeXData, strings);
if (pp!=cp) delete pp;
if (cp!=tp) delete cp;
if (tp!=vp) delete tp;
if (vp!=&out)delete vp;
return Rule::Applier::OK;
}
Rule::Applier out_l23_applier = { "PSL23+PDF", out_l23_check_rule, out_l23_work, 0 };
/* --- Fri Mar 22 17:22:40 CET 2002 -- Sat Jun 15 16:03:06 CEST 2002 */
/* integrated l1tr at Sat Jun 15 16:03:03 CEST 2002 */
/* added /Bbox at Sat Jun 15 16:03:31 CEST 2002 */
//static char *l1tr_tte=
//#include "l1tr.tth"
/** PostScript Level1 or PDF Fully transparent image */
Rule::Applier::cons_t out_l1tr_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (!(cache->isPS() || cache->isPDF())
|| (cache->SampleFormat!=Image::SF_Transparent && cache->SampleFormat!=Image::SF_Bbox && cache->SampleFormat!=Image::SF_Opaque)
) return Rule::Applier::DONT_KNOW;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_l1tr_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
char t[]="....";
if (out_l1tr_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->doSampleFormat(sf);
Rule::Cache *cache=&or_->cache;
Filter::VerbatimE outve(out); /* required since template /p0* is TTM */
if (cache->isPS()) { t[0]='l'; t[1]='1'; }
else { t[0]='p'; t[1]='0'; }
switch (cache->SampleFormat) {
case Image::SF_Transparent: t[2]='t'; t[3]='r'; break;
case Image::SF_Opaque: t[2]='o'; t[3]='p'; break;
case Image::SF_Bbox: t[2]='b'; t[3]='b'; break;
default: assert(0);
}
Rule::writeTTT(outve, outve, outve, t, or_, sf, 0/*NULLP*/);
return Rule::Applier::OK;
}
Rule::Applier out_l1tr_applier = { "P-TrOpBb", out_l1tr_check_rule, out_l1tr_work, 0 };
/* --- */
/* --- l23mask Fri Mar 22 18:11:12 CET 2002
* --- l23ind1 Fri Mar 22 18:11:12 CET 2002
* removed (integrated to l23) at Sat Apr 20 20:25:57 CEST 2002
*/
/* --- l1mask Fri Mar 22 18:33:01 CET 2002
* --- l1mashex Sun Apr 14 15:25:25 CEST 2002
* --- l1in1 Fri Mar 22 18:33:37 CET 2002
* --- l1in1hex Fri Mar 22 18:33:37 CET 2002
* removed (integrated to l1c) at Sat Apr 20 20:25:57 CEST 2002
* --- lcr Sat Jun 1 17:09:57 CEST 2002
* removed (integrated to l1c) at Sun Jun 2 16:48:31 CEST 2002
* --- l1gbh
* removed (integrated to l1c) at Sun Jun 2 16:48:31 CEST 2002
* --- l1fa85g.tte: PostScript Level1 FlateEncode, A85, without Predictor
* removed (integrated to l1c) at Sun Sep 22 00:48:21 CEST 2002
*/
/* --- Sun Jun 2 16:48:44 CEST 2002 */
//static char *l1mask_tte=
//#include "l1mask.tth"
static void gen_tkey(char *tkey, GenBuffer::Writable& out, GenBuffer::Writable*&tp, GenBuffer::Writable*& cp, Rule::OutputRule*or_) {
Rule::Cache *cache=&or_->cache;
tp=&out;
if (cache->TransferEncoding==cache->TE_A85) { tkey[3]='8'; tp=PSEncoder::newASCII85Encode (out, or_->cacheHints.TransferCPL); }
else if (cache->TransferEncoding==cache->TE_Hex) { tkey[3]='h'; tp=PSEncoder::newASCIIHexEncode(out, or_->cacheHints.TransferCPL); }
else tkey[3]='b';
cp=tp;
if (cache->Compression==Rule::Cache::CO_RLE) { tkey[4]='r'; cp=PSEncoder::newRunLengthEncode(*tp, or_->cacheHints.RecordSize); }
else if (cache->Compression==Rule::Cache::CO_ZIP) { tkey[4]='z'; cp=PSEncoder::newFlateEncode(*tp, or_->cacheHints.Effort); }
else if (cache->Compression==Rule::Cache::CO_LZW) { tkey[4]='l'; cp=PSEncoder::newLZWEncode(*tp); }
else tkey[4]='n';
/* vvv removed 'm' and '1' at Sun Sep 22 17:53:08 CEST 2002 */
tkey[2]=// cache->SampleFormat==Image::SF_Mask ? 'm' :
// cache->SampleFormat==Image::SF_Indexed1 ? '1' :
cache->SampleFormat==Image::SF_Indexed2 ? '2' :
cache->SampleFormat==Image::SF_Indexed4 ? '4' :
cache->SampleFormat==Image::SF_Indexed8 ? '8' :
cache->SampleFormat==Image::SF_Transparent2 ? 't' :
cache->SampleFormat==Image::SF_Transparent4 ? 't' :
cache->SampleFormat==Image::SF_Transparent8 ? 't' :
'g'; /* /Gray*, /Rgb*, /Mask, /Indexed1 */
}
/** PostScript Level1 uncompressed binary or hex */
Rule::Applier::cons_t out_l1c_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if ((cache->FileFormat!=cache->FF_PSL1 && cache->FileFormat!=cache->FF_PSLC && cache->FileFormat!=cache->FF_PSL2)
|| (cache->FileFormat==cache->FF_PSL2 && cache->Compression!=Rule::Cache::CO_ZIP)
|| (cache->TransferEncoding!=cache->TE_Binary && cache->TransferEncoding!=cache->TE_Hex && cache->TransferEncoding!=cache->TE_A85)
|| (cache->Compression!=Rule::Cache::CO_None && cache->Compression!=Rule::Cache::CO_RLE && cache->Compression!=Rule::Cache::CO_ZIP && cache->Compression!=Rule::Cache::CO_LZW)
|| cache->hasPredictor()
|| !(cache->isTransparentM() || cache->isIndexed() || cache->isGray() || cache->isRGB())
) return Rule::Applier::DONT_KNOW;
bool badp=false;
if (cache->FileFormat==cache->FF_PSL1 && cache->SampleFormat!=Image::SF_Indexed1 && cache->isIndexed()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /SampleFormat/Indexed+ doesn't work with /FileFormat/PSL1 (use /PSLC or /PSL2)" << (Error*)0;
badp=true;
}
if (cache->FileFormat==cache->FF_PSL1 && cache->isRGB()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /SampleFormat/RGB* doesn't work with /FileFormat/PSL1 (use /PSLC or /PSL2)" << (Error*)0;
badp=true;
}
char tkey[]="l1..."; /* /l1{2,4,8,t}{8,h}{r,z,l} */
GenBuffer::Writable *tp0,*cp0,*out=(GenBuffer::Writable*)NULLP;
gen_tkey(tkey, *out, tp0, cp0, or_);
/* fprintf(stderr,"tkey=%s\n", tkey); */
if (!badp && Rule::Templates->get(tkey, strlen(tkey))==MiniPS::Qundef) return Rule::Applier::DONT_KNOW;
return (Rule::Applier::cons_t)(badp ? 0+Rule::Applier::BAD : 0+Rule::Applier::OK);
/* ^^^ Dat: 0+: pacify gcc-3.1 */
}
Rule::Applier::cons_t out_l1c_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
if (out_l1c_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
/* Dat: only these have been defined so far: grep '^/l1[248tg][8h][rzl]' bts2.ttt
* /l1thr /l1g8r /l1ghr /l128r /l12hr /l148r /l14hr /l188r /l18hr /l1g8z /l1ghz /l1g8l /l1ghl
*/
or_->doSampleFormat(sf, true); /* Dat: `true': because of /Transparent+ */
char tkey[]="l1..."; /* /l1{2,4,8,t}{8,h}{r,z,l} */
GenBuffer::Writable *tp, *cp;
gen_tkey(tkey, out, tp, cp, or_);
// fprintf(stderr, "tkey=(%s)\n", tkey);
Rule::writeTTT(out, *tp, *cp, tkey, or_, sf,
tkey[2]=='2' || tkey[2]=='4' || tkey[2]=='8' || tkey[2]=='t' ? Rule::writePalData : Rule::writeData
);
// Rule::writeTTT(out, *tp, *cp, tkey, or_, sf, Rule::writePalData);
if (cp!=tp) delete cp;
if (tp!=&out)delete tp;
return Rule::Applier::OK;
}
Rule::Applier out_l1c_applier = { "PSL1C", out_l1c_check_rule, out_l1c_work, 0 };
/* lcrbin (lcrb) --- Sun Apr 14 16:50:14 CEST 2002
* lcrhex (lcr8, lcrh) --- Sun Apr 14 16:50:22 CEST 2002
* removed (integrated to _l1c_) at Sat Jun 1 17:09:52 CEST 2002
*/
#if 0
/* --- Sat Jun 1 17:09:57 CEST 2002 */
//static char *lcrbin_tte=
//#include "lcrbin.tth"
/** PostScript Level1+C uncompressed RGB image */
Rule::Applier::cons_t out_lcr_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (!cache->isPS()
|| cache->FileFormat==cache->FF_PSL1
|| (cache->TransferEncoding!=cache->TE_Binary && cache->TransferEncoding!=cache->TE_Hex && cache->TransferEncoding!=cache->TE_A85)
|| cache->Compression!=Rule::Cache::CO_None
|| cache->hasPredictor()
|| !cache->isRGB()
) return Rule::Applier::DONT_KNOW;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_lcr_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
Rule::Cache *cache=&or_->cache;
if (out_lcr_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->doSampleFormat(sf);
char tkey[]="lcr.";
GenBuffer::Writable *tp=&out;
if (cache->TransferEncoding==cache->TE_A85) { tkey[3]='8'; tp=PSEncoder::newASCII85Encode (out, or_->cacheHints.TransferCPL); }
else if (cache->TransferEncoding==cache->TE_Hex) { tkey[3]='h'; tp=PSEncoder::newASCIIHexEncode(out, or_->cacheHints.TransferCPL); }
else tkey[3]='b';
Rule::writeTTT(out, *tp, *tp, tkey, or_, sf, Rule::writeData);
if (tp!=&out)delete tp;
return Rule::Applier::OK;
}
Rule::Applier out_lcr_applier = { "PSLC-RGB", out_lcr_check_rule, out_lcr_work, 0 };
#endif
/* --- Sat Mar 23 12:37:04 CET 2002; Tue Jul 2 10:23:44 CEST 2002 */
Rule::Applier::cons_t out_gif89a_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
bool badp=false;
if (cache->FileFormat!=cache->FF_GIF89a
) return Rule::Applier::DONT_KNOW;
if (!cache->isIndexed() && !cache->isTransparentM()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /GIF89a must be /Indexed*, /Mask or /Transparent+" << (Error*)0;
badp=true;
}
if (cache->TransferEncoding!=cache->TE_Binary) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /GIF89a requires /Binary" << (Error*)0;
badp=true;
}
if (cache->Compression!=Rule::Cache::CO_None && cache->Compression!=Rule::Cache::CO_LZW) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /GIF89a requires /LZW" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /GIF89a requires /Predictor 1" << (Error*)0;
badp=true;
}
#if !USE_OUT_GIF
Error::sev(Error::WARNING_DEFER) << "check_rule: please `configure --enable-gif' for /GIF89a" << (Error*)0;
badp=true;
#endif
if (badp) return Rule::Applier::BAD;
or_->cache.WarningOK=true;
return Rule::Applier::OK;
}
#if USE_OUT_GIF
#if OBJDEP
# warning REQUIRES: out_gif.o
#endif
extern void out_gif_write(GenBuffer::Writable& out, Image::Indexed *img); /* out_gif.cpp */
Rule::Applier::cons_t out_gif89a_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
if (out_gif89a_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->cache.SampleFormat=(Image::sf_t)(or_->cache.isIndexed()? 0+Image::SF_Indexed8: 0+Image::SF_Transparent8);
/* ^^^ Dat: 0+: pacify gcc-3.1; (Image::sf_t): pacify VC6.0 */
or_->doSampleFormat(sf);
out_gif_write(out, PTS_dynamic_cast(Image::Indexed*,sf->getImg()));
return Rule::Applier::OK;
}
#else
/*# define out_gif89a_check_rule (Rule::Applier::check_rule_t)NULLP*/
# define out_gif89a_work (Rule::Applier::work_t)0
#endif
Rule::Applier out_gif89a_applier = {
#if HAVE_LZW
"GIF89a+LZW"
#else
"GIF89a"
#endif
, out_gif89a_check_rule, out_gif89a_work, 0 };
/* --- Tue Jun 4 19:51:03 CEST 2002 */
Rule::Applier::cons_t out_xpm_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
bool badp=false;
if (cache->FileFormat!=cache->FF_XPM
) return Rule::Applier::DONT_KNOW;
if (!cache->isIndexed() && !cache->isTransparentM()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XPM must be /Indexed*, /Mask or /Transparent+" << (Error*)0;
badp=true;
}
if (cache->TransferEncoding!=cache->TE_ASCII && cache->TransferEncoding!=cache->TE_Binary) {
/* ^^^ && BUGFIX at Thu Jul 11 21:57:09 CEST 2002 */
Error::sev(Error::WARNING_DEFER) << "check_rule: /XPM requires /TransferEncoding/ASCII" << (Error*)0;
badp=true;
}
if (cache->Compression!=Rule::Cache::CO_None) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XPM requires /Compression/None" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XPM requires /Predictor 1" << (Error*)0;
badp=true;
}
if (badp) return Rule::Applier::BAD;
or_->cache.WarningOK=true;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_xpm_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
if (out_xpm_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->cache.SampleFormat=(Image::sf_t)(or_->cache.isIndexed()? 0+Image::SF_Indexed8: 0+Image::SF_Transparent8);
/* ^^^ force 8-bit; may trigger warnings... */
/* ^^^ Dat: 0+: pacify gcc-3.1 */
or_->doSampleFormat(sf);
Image::Indexed *iimg=PTS_dynamic_cast(Image::Indexed*,sf->getImg());
/* vvv 93 useful ASCII chars (+ '\0'), missing: " and \ ; plus a hextable */
static char const xpmc=93;
static char const xpms[xpmc+1]="0123456789abcdef !#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`ghijklmnopqrstuvwxyz{|}~";
Image::Sampled::dimen_t wd=iimg->getWd(), htc=iimg->getHt();
bool pal2ch=iimg->getNcols()>xpmc;
// pal2ch=true;
out << "/* XPM */\nstatic char *sam2p_xpm[] = {\n"
"/* columns rows colors chars-per-pixel */\n\""
<< wd << ' ' << htc << ' ' << iimg->getNcols()
<< ' ' << (pal2ch?2:1) << "\",\n";
char const *p=iimg->getHeadp(), *pend=iimg->getRowbeg(), *phend;
char coline3[]="\".. c #ABCDEF\",\n";
char cotran3[]="\".. c None s None \",\n";
assert((pend-p)%3==0);
unsigned i=0;
bool transp=false;
if (iimg->getTransp()>=0) { pend-=3; assert((iimg->getTransp())*3+p==pend); transp=true; }
if (pal2ch) {
while (p!=pend) {
coline3[ 1]=xpms[i>>2];
coline3[ 2]=xpms[i++&3];
coline3[ 7]=xpms[*(unsigned char const*)p>>4];
coline3[ 8]=xpms[*(unsigned char const*)p++&15];
coline3[ 9]=xpms[*(unsigned char const*)p>>4];
coline3[10]=xpms[*(unsigned char const*)p++&15];
coline3[11]=xpms[*(unsigned char const*)p>>4];
coline3[12]=xpms[*(unsigned char const*)p++&15];
out << coline3;
}
if (transp) {
cotran3[ 1]=xpms[i>>2];
cotran3[ 2]=xpms[i++&3];
out << cotran3;
p+=3;
}
pend=iimg->getRowbeg()+wd*htc;
out << "/* Pixels */\n";
char *obuf=new char[4+2*wd], *op;
obuf[0]='"';
obuf[2*wd+1]='"';
obuf[2*wd+2]=','; /* Dat: it's OK to have a comma in the last line */
obuf[2*wd+3]='\n';
while (htc--!=0) {
phend=p+wd;
op=obuf;
while (p!=phend) { *++op=xpms[*p>>2]; *++op=xpms[*p++&3]; }
out.vi_write(obuf, 2*wd+4);
}
delete [] obuf;
} else {
coline3[1]='"';
while (p!=pend) {
coline3[ 2]=xpms[i++];
coline3[ 7]=xpms[*(unsigned char const*)p>>4];
coline3[ 8]=xpms[*(unsigned char const*)p++&15];
coline3[ 9]=xpms[*(unsigned char const*)p>>4];
coline3[10]=xpms[*(unsigned char const*)p++&15];
coline3[11]=xpms[*(unsigned char const*)p>>4];
coline3[12]=xpms[*(unsigned char const*)p++&15];
out << (coline3+1);
}
if (transp) {
cotran3[1]='"';
cotran3[ 2]=xpms[i];
out << (cotran3+1);
p+=3;
}
pend=iimg->getRowbeg()+wd*htc;
out << "/* Pixels */\n";
char *obuf=new char[4+wd], *op;
obuf[0]='"';
obuf[wd+1]='"';
obuf[wd+2]=','; /* Dat: it's OK to have a comma in the last line */
obuf[wd+3]='\n';
while (htc--!=0) {
phend=p+wd;
op=obuf;
while (p!=phend) *++op=xpms[0U+*p++];
out.vi_write(obuf, wd+4);
}
delete [] obuf;
}
assert(p==pend);
out << "};\n";
return Rule::Applier::OK;
}
Rule::Applier out_xpm_applier = { "XPM", out_xpm_check_rule, out_xpm_work, 0 };
/* --- Sat Mar 23 13:18:07 CET 2002 */
Rule::Applier::cons_t out_pnm_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (cache->FileFormat!=cache->FF_PNM
) return Rule::Applier::DONT_KNOW;
bool badp=false;
// if (cache->SampleFormat!=Image::SF_Rgb8 && cache->SampleFormat!=Image::SF_Gray8 && cache->SampleFormat!=Image::SF_Gray1) {
if (!cache->isGray() && !cache->isRGB() && !cache->isIndexed() && !cache->isTransparentM()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /PNM must be /Rgb8, /Gray8 or /Gray1" << (Error*)0;
badp=true;
}
if (cache->TransferEncoding!=cache->TE_Binary && cache->TransferEncoding!=cache->TE_ASCII) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /PNM requires /Binary or /ASCII" << (Error*)0;
badp=true;
}
if (cache->Compression!=Rule::Cache::CO_None) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /PNM requires /Compression/None" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /PNM requires /Predictor 1" << (Error*)0;
badp=true;
}
if (badp) return Rule::Applier::BAD;
or_->cache.WarningOK=true;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_pnm_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
/*static*/ char head[]={ 'P', 0, '\n', '#', ' ', 'b', 'y', ' ' };
/*static*/ char tmp[72];
Image::sf_t sfo=or_->cache.SampleFormat;
Image::Indexed *alphaChannel=(Image::Indexed*)NULLP;
if (out_pnm_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
if (or_->cache.SampleFormat==Image::SF_Gray1 || or_->cache.SampleFormat==Image::SF_Gray8) {
if (or_->cache.TransferEncoding==or_->cache.TE_ASCII) or_->cache.SampleFormat=Image::SF_Gray8;
} else if (or_->cache.SampleFormat==Image::SF_Gray2 || or_->cache.SampleFormat==Image::SF_Gray4) {
or_->cache.SampleFormat=Image::SF_Gray8;
} else if (or_->cache.isRGB()) {
or_->cache.SampleFormat=Image::SF_Rgb8;
} else {
assert(or_->cache.isIndexed() || or_->cache.isTransparentM());
if (or_->cache.isTransparentM()) {
alphaChannel=PTS_dynamic_cast(Image::Indexed*,sf->getImg())->calcAlpha();
sf->clearTransp(); /* BUGFIX at Tue Sep 17 10:05:47 CEST 2002 */
}
or_->cache.SampleFormat=(Image::sf_t)(
!sf->canGrayy() ? 0+Image::SF_Rgb8
: sf->minRGBBpcc()==1 ? 0+Image::SF_Gray1 : 0+Image::SF_Gray8);
/* ^^^ Dat: 0+: pacify gcc-3.1 */
}
if (or_->cache.SampleFormat==Image::SF_Gray1 && or_->cache.TransferEncoding==or_->cache.TE_ASCII)
or_->cache.SampleFormat=Image::SF_Gray8;
or_->doSampleFormat(sf);
sfo=or_->cache.SampleFormat;
head[1]=(sfo==Image::SF_Rgb8 ? '3' : sfo==Image::SF_Gray8 ? '2' : '1')
+(or_->cache.TransferEncoding==or_->cache.TE_Binary)*3;
out.vi_write(head, sizeof(head));
out << Error::banner0;
Image::Sampled *img=sf->getImg();
out << '\n' << img->getWd() << ' ' << img->getHt();
out << &(" 255\n"[sfo==Image::SF_Gray1?4:0]);
/* ^^^ SF_Gray1 BUGFIX at Tue Jun 4 21:44:17 CEST 2002 */
register char *p=img->getRowbeg(), *t=(char*)NULLP;
slen_t len=img->getRlen()*img->getHt();
// fprintf(stderr, "len=%u\n", len);
register unsigned smallen, i;
switch (head[1]) {
case '1': /* PBM ASCII */
while (len>=70) {
smallen=70; t=tmp; while (smallen--!=0) *t++=(*p++==0)?'1':'0';
/* ^^^ note the swapping of '0' and '1' above */
*t++='\n'; out.vi_write(tmp, 71);
len-=70;
}
while (len--!=0) *t++=(*p++==0)?'0':'1';
/* Dat: xv requires a whitespace just before EOF */
*t++='\n'; out.vi_write(tmp, t-tmp);
break;
case '2': case '3': /* PGM ASCII, PPM ASCII */
while (len!=0) {
if (len>17) { smallen=17; len-=17; } else { smallen=len; len=0; }
t=tmp; while (smallen--!=0) {
if ((i=*(unsigned char*)p++)<10) *t++=i+'0';
else if (i<100) { *t++=i/10+'0'; *t++=i%10+'0'; }
else if (i<200) { *t++='1'; *t++=(i-100)/10+'0'; *t++=i%10+'0'; }
else { *t++='2'; *t++=(i-200)/10+'0'; *t++=i%10+'0'; }
*t++=' ';
}
/* Dat: xv requires a whitespace just before EOF */
t[-1]='\n'; out.vi_write(tmp, t-tmp/*-(len==0)*/);
}
break;
case '4': /* PBM RAWBITS */
/* Invert the image */
while (len--!=0) *p++^=-1;
p=img->getRowbeg();
len=img->getRlen()*img->getHt();
/* fall through */
default: /* PBM RAWBITS, PGM RAWBITS, PPM RAWBITS */
/* fwrite(p, 1, len, stdout); */
out.vi_write(p, len);
}
if (alphaChannel!=NULLP) {
/* OK: don't always output rawbits PBM file */
assert(alphaChannel->getBpc()==1);
assert(alphaChannel->getWd()==img->getWd());
assert(alphaChannel->getHt()==img->getHt());
/* write PBM RAWBITS subfile (alpha channel) */
if (or_->cache.TransferEncoding==or_->cache.TE_Binary) {
out << "P4 " << img->getWd() << ' ' << img->getHt() << '\n';
out.vi_write(alphaChannel->getRowbeg(), alphaChannel->getRlen()*alphaChannel->getHt());
/* ^^^ BUGFIX at Tue Sep 17 10:18:28 CEST 2002 */
} else {
out << "P1 " << img->getWd() << ' ' << img->getHt() << '\n';
alphaChannel->to8();
p=alphaChannel->getRowbeg(); len=alphaChannel->getRlen()*alphaChannel->getHt();
while (len>=70) {
smallen=70; t=tmp; while (smallen--!=0) *t++=(*p++!=0)?'1':'0';
/* ^^^ note the non-swapping of '0' and '1' above */
*t++='\n'; out.vi_write(tmp, 71);
len-=70;
}
while (len--!=0) *t++=(*p++!=0)?'0':'1';
/* Dat: xv requires a whitespace just before EOF */
*t++='\n'; out.vi_write(tmp, t-tmp);
}
delete alphaChannel;
}
return Rule::Applier::OK;
}
Rule::Applier out_pnm_applier = { "PNM", out_pnm_check_rule, out_pnm_work, 0 };
/* --- Sat Aug 10 22:18:33 CEST 2002 */
Rule::Applier::cons_t out_xwd_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (cache->FileFormat!=cache->FF_XWD
) return Rule::Applier::DONT_KNOW;
bool badp=false;
if (cache->SampleFormat!=Image::SF_Rgb8 && cache->SampleFormat!=Image::SF_Gray8 && cache->SampleFormat!=Image::SF_Indexed8) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XWD must be /Rgb8, /Gray8 or /Indexed8" << (Error*)0;
return Rule::Applier::BAD;
}
if (cache->TransferEncoding!=cache->TE_Binary) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XWD requires /Binary" << (Error*)0;
badp=true;
}
if (cache->Compression!=Rule::Cache::CO_None) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XWD requires /Compression/None" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /XWD requires /Predictor 1" << (Error*)0;
badp=true;
}
if (badp) return Rule::Applier::BAD;
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_xwd_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
static const unsigned
XWD_FILE_VERSION=7,
ZPixmap=2,
MSBFirst=1,
DirectColor=5,
PseudoColor=3,
//GrayScale=1,
StaticGray=0;
Image::sf_t sfo=or_->cache.SampleFormat;
if (out_xwd_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->doSampleFormat(sf);
sfo=or_->cache.SampleFormat;
char head[101], *p=head;
Image::Sampled *img=sf->getImg();
slen_t bits_per_pixel, bitmap_pad, bytes_per_line;
unsigned ncolors;
memset(head, '\0', 101);
/*header_size*/ mf32(p,101);
/*file_version*/ (p+=4)[-1]=XWD_FILE_VERSION;
/*pixmap_format*/ (p+=4)[-1]=ZPixmap;
/*pixmap_depth*/ (p+=4)[-1]=sfo==Image::SF_Rgb8 ? 24 : 8;
/*pixmap_width */ mf32(p, img->getWd());
/*pixmap_height*/ mf32(p, img->getHt());
/*xoffset*/ p+=4;
/*byte_order*/ (p+=4)[-1]=MSBFirst;
/*bitmap_unit*/ (p+=4)[-1]=8; // sfo==Image::SF_Rgb8 ? 32 : 8;
/*bitmap_bit_order*/ (p+=4)[-1]=MSBFirst;
/*bitmap_pad*/ bitmap_pad=(p+=4)[-1]=8; // sfo==Image::SF_Rgb8 ? 32 : 8;
/* ^^^ force no padding at all */
/*bits_per_pixel*/ bits_per_pixel=(p+=4)[-1]=sfo==Image::SF_Rgb8 ? 24 : 8;
/*bytes_per_line*/ mf32(p, bytes_per_line=((bits_per_pixel*img->getWd()+bitmap_pad-1)&~(bitmap_pad-1))>>3);
/*visual_class*/ (p+=4)[-1]=sfo==Image::SF_Rgb8 ? DirectColor : sfo==Image::SF_Indexed8 ? PseudoColor : StaticGray;
/*red_mask */ (p+=4)[-3]=(char)(sfo==Image::SF_Rgb8 ? 255 : 0);
/*green_mask*/ (p+=4)[-2]=(char)(sfo==Image::SF_Rgb8 ? 255 : 0);
/*blue_mask */ (p+=4)[-1]=(char)(sfo==Image::SF_Rgb8 ? 255 : 0);
/*bits_per_rgb*/ (p+=4)[-1]=sfo==Image::SF_Rgb8 ? 24 : 8;
/*colormap_entries*/ (p+=4)[-2]=1; /*256*/
/*ncolors*/ mf32(p, ncolors=sfo==Image::SF_Rgb8 ? 0 : sfo==Image::SF_Indexed8 ? ((Image::Indexed*)img)->getNcols() : 256);
/*window_width */ mf32(p, img->getWd());
/*window_height*/ mf32(p, img->getHt());
assert(p+13==head+101);
/*window_x*/ /*0*/
/*window_y*/ /*0*/
/*window_bdrwidth*/ /*0*/
/*filename*/ /*""*/
out.vi_write(head, 101);
if (sfo!=Image::SF_Rgb8) {
char *pal=new char[ncolors*12], *pp=pal;
unsigned pixel;
if (sfo==Image::SF_Indexed8) {
char const* q=img->getHeadp();
for (pixel=0;pixel<ncolors;pixel++) {
*pp++=0; *pp++=0; *pp++=0; *pp++=pixel;
*pp++=*q; *pp++=*q++; /* red */
*pp++=*q; *pp++=*q++; /* green */
*pp++=*q; *pp++=*q++; /* blue */
*pp++=7;
*pp++=0;
}
} else { assert(sfo==Image::SF_Gray8);
for (pixel=0;pixel<ncolors;pixel++) {
*pp++=0; *pp++=0; *pp++=0; *pp++=pixel;
*pp++=pixel; *pp++=pixel; /* red */
*pp++=pixel; *pp++=pixel; /* green */
*pp++=pixel; *pp++=pixel; /* blue */
*pp++=7;
*pp++=0;
}
}
out.vi_write(pal, ncolors*12);
delete [] pal;
}
slen_t rlen=img->getRlen();
Image::Sampled::dimen_t htc=img->getHt();
char const* rp=img->getRowbeg();
unsigned scanline_pad=bytes_per_line-rlen;
// assert(*rp=='\xff');
if (scanline_pad!=0) {
assert(1<=scanline_pad && scanline_pad<=3);
while (htc--!=0) {
out.vi_write(rp, rlen);
rp+=rlen;
if (scanline_pad!=0) out.vi_write("\0\0", scanline_pad);
}
} else out.vi_write(rp, rlen*htc);
return Rule::Applier::OK;
}
Rule::Applier out_xwd_applier = { "XWD", out_xwd_check_rule, out_xwd_work, 0 };
Rule::Applier out_x11_applier = { "X11", 0/*out_x11_check_rule*/, 0/*out_x11_work*/, 0 };
/* --- Sat Apr 20 11:49:56 CEST 2002 */
/** Baseline (lossy) JPEG */
Rule::Applier::cons_t out_jpeg_check_rule(Rule::OutputRule* or_) {
Rule::Cache *cache=&or_->cache;
if (cache->FileFormat!=cache->FF_JPEG
|| cache->Compression==Rule::Cache::CO_JAI
) return Rule::Applier::DONT_KNOW;
bool badp=false;
if (cache->SampleFormat!=Image::SF_Rgb8 && cache->SampleFormat!=Image::SF_Gray8) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /DCTEncode requires /Rgb8 or /Gray8" << (Error*)0;
badp=true;
}
if (cache->TransferEncoding!=cache->TE_Binary) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /JPEG requires /Binary" << (Error*)0;
badp=true;
}
if (cache->Compression!=Rule::Cache::CO_None && cache->Compression!=Rule::Cache::CO_DCT && cache->Compression!=Rule::Cache::CO_IJG) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /JPEG requires /DCT or /IJG" << (Error*)0;
badp=true;
}
if (cache->hasPredictor()) {
Error::sev(Error::WARNING_DEFER) << "check_rule: /JPEG requires /Predictor 1" << (Error*)0;
badp=true;
}
if (badp) return Rule::Applier::BAD;
or_->cache.WarningOK=true; /* ?? */
return Rule::Applier::OK;
}
Rule::Applier::cons_t out_jpeg_work(GenBuffer::Writable& out, Rule::OutputRule*or_, Image::SampledInfo *sf) {
Rule::Cache *cache=&or_->cache;
// assert(0);
if (out_jpeg_check_rule(or_)!=Rule::Applier::OK) return Rule::Applier::DONT_KNOW;
or_->doSampleFormat(sf);
// GenBuffer::Writable *tp=&out; /* always binary */
GenBuffer::Writable *cp=&out;
/* SUXX: cjpeg(1) won't create a color JPEG for a grayscale image */
if (cache->Compression==Rule::Cache::CO_DCT) {
SimBuffer::B other_parameters;
or_->cacheHints.DCT->dump(other_parameters, 0, false);