-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cpp
1923 lines (1831 loc) · 67.9 KB
/
image.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
/*
* image.cpp (formerly sampled.cpp)
* by [email protected] at Wed Feb 27 09:26:05 CET 2002
*/
#ifdef __GNUC__
#ifndef __clang__
#pragma implementation
#endif
#endif
#include "image.hpp"
#include "error.hpp"
#include <string.h> /* strlen() */
#include "gensio.hpp"
/* --- 4-byte hashing */
#if SIZEOF_INT>=4
typedef unsigned int u32_t;
typedef signed int s32_t;
#else
typedef unsigned long u32_t;
typedef signed long s32_t;
#endif
/* vvv Dat: moved these out of Hash46 to pacify VC6.0 */
const unsigned M=1409;
/** Number of _value_ data bytes (they are not hashed) */
const unsigned D=2;
/** Size of each tuple in the array `t' */
const unsigned HD=4+D;
/** A tuple is considered free iff its first byte equals FREE */
const unsigned char FREE=255;
/**
* M=1409
* h(K)=K%1409
* h(i,K)=-i*(1+(K%1408)) (i in 0..1408)
* K=[a,r,g,b]=(a<<24)+(r<<16)+(g<<8)+b (4 bytes)
* h(K)=(253*a+722*r+(g<<8)+b)%1409
* h(i,K)=-i*(1+(896*a+768*r+(g<<8)+b)%1408)
*
* -- No dynamic growing or re-hashing. Useful for hashing colormap palettes
* with a maximum size of 256.
* -- Deleting not supported.
*
* Implementation: Use 32 bit integers for calculation.
*
* Imp: remove unused attr `size'
*/
class Hash46 {
public:
/** Creates an empty hash. */
Hash46();
inline unsigned getSize() const { return size; }
inline unsigned getLength() const { return size; }
inline unsigned getMaxSize() const { return M; }
inline bool isFull() const { return size==M; }
/** @return NULLP or the pointer to a tuple */
inline unsigned char* lookup(unsigned char k[4]) {
unsigned char *ret=walk(k);
return ret==NULLP || *ret==FREE ? (unsigned char*)NULLP : ret;
}
/** Can be called only if !isFull()
* @return NULL if isFull() and not found; otherwise: pointer to the tuple
* found or the place to which the insert can take place.
*/
unsigned char* walk(unsigned char k[4]);
protected:
/** Number of non-free tuples in the hash. */
unsigned size;
unsigned char t[M*HD];
};
Hash46::Hash46(): size(0) {
memset(t, FREE, sizeof(t));
}
unsigned char *Hash46::walk(unsigned char k[4]) {
u32_t hk, hik;
hk=HD*(((((u32_t)1<<24)%M)*k[0]+(((u32_t)1<<16)%M)*k[1]+
(((u32_t)1<< 8)%M)*k[2]+k[3])%M);
hik=HD*(1+((((u32_t)1<<24)%(M-1))*k[0]+(((u32_t)1<<16)%(M-1))*k[1]+
(((u32_t)1<< 8)%(M-1))*k[2]+k[3])%(M-1));
/* fprintf(stderr, "hk=%u hik=%u\n", hk, hik); */
register unsigned char *p=t+hk;
unsigned i=M;
/* fprintf(stderr, "?? %02x %02x %02x %02x\n", k[0], k[1], k[2], k[3]); */
do {
/* fprintf(stderr, "examining %02x %02x %02x %02x %d\n", p[0], p[1], p[2], p[3], (k[0]=p[0] && k[1]==p[1] && k[2]==p[2])); */
if (*p==FREE || (k[0]==p[0] && k[1]==p[1] && k[2]==p[2] && k[3]==p[3])) return p;
/* ^^^ huge == BUGFIX at Sun Apr 14 00:16:59 CEST 2002 */
if (hk>=hik) { hk-=hik; p-=hik; }
else { hk+=M*HD-hik; p+=M*HD-hik; }
} while (--i!=0);
/* fprintf(stderr, "full\n"); */
return (unsigned char*)NULLP;
}
/* --- */
const unsigned char Image::Sampled::cs2cpp[6]= { 0, 1, 3, 3, 4, 4 };
char const *Image::Sampled::cs2devcs(unsigned char cs) {
static const char *names[]={ (char*)NULLP, "Gray", "RGB", "RGB", "CMYK", "CMYK" };
return cs>=1 && cs<=5 ? names[cs] : (char*)NULLP;
}
static void fatal_image_too_large() {
Error::sev(Error::EERROR) << "Image: Image too large." << (Error*)0;
}
static slen_t multiply_check(slen_t a, slen_t b) {
slen_t result;
if (a == 0) return 0;
/* Check for overflow. Works only if everything is unsigned. */
if ((result = a * b) / a != b) fatal_image_too_large();
return result;
}
static slen_t multiply_check(slen_t a, slen_t b, slen_t c) {
return multiply_check(multiply_check(a, b), c);
}
static slen_t add_check(slen_t a, slen_t b) {
/* Check for overflow. Works only if everything is unsigned. */
if (b > (slen_t)-1 - a) fatal_image_too_large();
return a + b;
}
#if 0
static slen_t add_check(slen_t a, slen_t b, slen_t c) {
return add_check(add_check(a, b), c);
}
#endif
static slen_t add_check(slen_t a, slen_t b, slen_t c, slen_t d) {
return add_check(add_check(a, b), add_check(c, d));
}
void Image::Sampled::init(slen_t l_comment, slen_t l_header, dimen_t wd_, dimen_t ht_,
/* ^^^ 24 is required for /Transparent in out_tiff_work */
unsigned char bpc_, unsigned char ty_, unsigned char cpp_) {
/* Even if we continue from here, most probably we'll reach
* ``sam2p.yes: Error: applyProfile: invalid combination, no applicable OutputRule''.
* So more work is needed to support output images of size 0.
*/
if (wd_ <= 0 || ht_ <= 0) Error::sev(Error::EERROR) << "Image: Image of size 0." << (Error*)0;
bpc=bpc_;
ty=ty_;
wd=wd_;
ht=ht_;
cpp=cpp_;
// pred=1;
transpc=0x1000000UL; /* Dat: this means: no transparent color */
const slen_t rlens = add_check(multiply_check(bpc_, cpp_, wd_), 7) >> 3;
rlen = rlens;
if (rlen != rlens) fatal_image_too_large();
beg=new char[len=add_check(l_comment, l_header, multiply_check(rlen, ht_), bpc)];
rowbeg=(headp=const_cast<char*>(beg)+l_comment)+l_header;
trail=const_cast<char*>(beg)+len-bpc;
memset(trail, 0, bpc);
}
Image::Gray* Image::Sampled::toGray0(unsigned char bpc_) {
unsigned char *crow=new unsigned char[wd*3+7*3], *p, *pend;
Image::Gray *img=new Image::Gray(wd, ht, bpc_);
unsigned char *outp=(unsigned char*)img->getRowbeg();
dimen_t htc;
memset(crow+wd*3, '\0', 7*3); /* *3 BUGFIX at Tue Jan 18 17:04:15 CET 2005 */
unsigned i;
/* Dat: not optimising for minimal rounding error since caller should ensure
* that there is no such error at all.
*/
if (bpc_==1) {
assert(img->getBpc()==1);
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow-3,pend=p+wd*3; p<pend; ) {
i =(*(p+=3)!=0)<<7; i|=(*(p+=3)!=0)<<6; i|=(*(p+=3)!=0)<<5; i|=(*(p+=3)!=0)<<4;
i|=(*(p+=3)!=0)<<3; i|=(*(p+=3)!=0)<<2; i|=(*(p+=3)!=0)<<1; i|=(*(p+=3)!=0);
*outp++=i;
}
}
} else if (bpc_==2) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow-3,pend=p+wd*3; p<pend; ) {
i =(*(p+=3)/85)<<6; i|=(*(p+=3)/85)<<4; i|=(*(p+=3)/85)<<2; i|=(*(p+=3)/85);
*outp++=i;
}
}
} else if (bpc_==4) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow-3,pend=p+wd*3; p<pend; ) {
i =(*(p+=3)/17)<<4; i|=(*(p+=3)/17);
*outp++=i;
}
}
} else if (bpc_==8) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow-3,pend=p+wd*3; p!=pend; ) {
*outp++=*(p+=3);
}
}
} else assert(0);
delete [] crow;
return img;
}
Image::RGB* Image::Sampled::toRGB0(unsigned char bpc_) {
unsigned char *crow=new unsigned char[wd*3+7], *p, *pend;
Image::RGB *img=new Image::RGB(wd, ht, bpc_);
unsigned char *outp=(unsigned char*)img->getRowbeg();
dimen_t htc;
memset(crow+wd*3, '\0', 7);
unsigned i;
/* Dat: not optimising for minimal rounding error since caller should ensure
* that there is no such error at all.
*/
if (bpc_==1) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow,pend=crow+wd*3; p<pend; ) {
i =(*p++!=0)<<7; i|=(*p++!=0)<<6; i|=(*p++!=0)<<5; i|=(*p++!=0)<<4;
i|=(*p++!=0)<<3; i|=(*p++!=0)<<2; i|=(*p++!=0)<<1; i|=(*p++!=0);
*outp++=i;
}
}
} else if (bpc_==2) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow,pend=crow+wd*3; p<pend; ) {
i =(*p++/85)<<6; i|=(*p++/85)<<4; i|=(*p++/85)<<2; i|=(*p++/85);
*outp++=i;
}
}
} else if (bpc_==4) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow,pend=crow+wd*3; p<pend; ) {
i =(*p++/17)<<4; i|=(*p++/17);
*outp++=i;
}
}
} else if (bpc_==8) {
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)outp, htc);
outp+=wd*3;
}
} else assert(0);
delete [] crow;
return img;
}
Image::Indexed* Image::Sampled::toIndexed0()/* const*/ {
unsigned char *crow=new unsigned char[wd*3], k[6], *p, *pend, *w;
Image::Indexed *img=new Image::Indexed(wd, ht, 256, 8);
dimen_t htc;
unsigned char *pal=(unsigned char*)img->getHeadp(), *outp=(unsigned char*)img->getRowbeg();
unsigned ncols=0;
Hash46 h;
k[0]=0;
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow,pend=crow+wd*3; p!=pend; p+=3) {
memcpy(k+1, p, 3);
w=h.walk(k);
assert(w!=NULL); /* Hash cannot be full since h.M>=256. */
/* fprintf(stderr, "w=%p\n", w); */
if (*w==/*h.*/FREE) {
if (ncols==256) { delete img; delete [] crow; return (Image::Indexed*)NULLP; }
/* ^^^ too many colors; cannot convert image to indexed */
memcpy(w,k,4);
memcpy(pal,k+1,3);
/* fprintf(stderr,"newcol=%02x #%02x%02x%02x\n", k[0], k[1], k[2], k[3]); */
/* fprintf(stderr,"newcol=pal #%02x%02x%02x\n", pal[0], pal[1], pal[2]); */
pal+=3;
*outp++=w[4]=ncols++;
} else { /* a color that we have already seen */
*outp++=w[4];
}
}
}
img->setNcolsMove(ncols);
delete [] crow;
/* Now img is ready. The user should call packPal() to make it even tighter. */
return img;
}
void Image::Sampled::to8mul() {
if (bpc==8) return;
if (wd==0 || ht==0) { bpc=8; return; }
unsigned oldBpc=bpc;
slen_t wdcpp=wd*cpp;
const char *oldBeg=beg;
unsigned char *p=(unsigned char*)rowbeg;
bpc=8;
rlen=wd;
beg=new char[len=rowbeg-oldBeg+rlen*ht+bpc];
headp= const_cast<char*>(beg)+(headp-oldBeg);
rowbeg=const_cast<char*>(beg)+(rowbeg-oldBeg);
trail= const_cast<char*>(beg)+len-bpc;
memcpy(const_cast<char*>(beg), oldBeg, rowbeg-beg);
unsigned char *to=(unsigned char*)rowbeg, *toend;
unsigned int i, j;
Image::Sampled::dimen_t htc;
if (oldBpc==1) {
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~7);
while (to!=toend) {
i=*p++;
*to++=(i>>7)*255;
*to++=((i>>6)&1)*255;
*to++=((i>>5)&1)*255;
*to++=((i>>4)&1)*255;
*to++=((i>>3)&1)*255;
*to++=((i>>2)&1)*255;
*to++=((i>>1)&1)*255;
*to++=( i&1)*255;
}
if (0!=(j=(wdcpp)&7)) {
i=*p; /* No mem overrun, even if (wd&7)==0 */
while (j--!=0) { *to++=(i>>7)*255; i<<=1; }
}
}
} else if (oldBpc==2) {
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~3);
while (to!=toend) {
i=*p++;
*to++=(i>>6)*85;
*to++=((i>>4)&3)*85;
*to++=((i>>2)&3)*85;
*to++=( i&3)*85;
}
if (0!=(j=(wdcpp)&3)) {
i=*p; /* No mem overrun, even if (wd&7)==0 */
while (j--!=0) { *to++=(i>>6)*85; i<<=2; }
}
}
} else if (oldBpc==4) {
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~1);
while (to!=toend) {
i=*p++;
*to++=(i>>4)*17;
*to++=( i&15)*17;
}
if (0!=((wdcpp)&1)) *to++=(*p++>>4)*17;
}
} else assert(0 && "invalid bpc");
delete [] const_cast<char*>(oldBeg);
}
void Image::Sampled::to8nomul() {
if (bpc==8) return;
if (wd==0 || ht==0) { bpc=8; return; }
unsigned oldBpc=bpc;
slen_t wdcpp=wd*cpp;
const char *oldBeg=beg;
unsigned char *p=(unsigned char*)rowbeg;
bpc=8;
rlen=wd;
beg=new char[len=rowbeg-oldBeg+rlen*ht+bpc];
headp= const_cast<char*>(beg)+(headp-oldBeg);
rowbeg=const_cast<char*>(beg)+(rowbeg-oldBeg);
trail= const_cast<char*>(beg)+len-bpc;
memcpy(const_cast<char*>(beg), oldBeg, rowbeg-beg);
unsigned char *to=(unsigned char*)rowbeg, *toend;
unsigned int i, j;
Image::Sampled::dimen_t htc;
if (oldBpc==1) {
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~7);
while (to!=toend) {
i=*p++;
*to++=(i>>7);
*to++=((i>>6)&1);
*to++=((i>>5)&1);
*to++=((i>>4)&1);
*to++=((i>>3)&1);
*to++=((i>>2)&1);
*to++=((i>>1)&1);
*to++=( i&1);
}
if (0!=(j=(wdcpp)&7)) {
i=*p++; /* No mem overrun, even if (wd&7)==0 */
while (j--!=0) { *to++=(i>>7); i<<=1; }
}
}
} else if (oldBpc==2) {
// assert(0);
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~3);
while (to!=toend) {
i=*p++;
*to++=(i>>6);
*to++=((i>>4)&3);
*to++=((i>>2)&3);
*to++=( i&3);
}
if (0!=(j=(wdcpp)&3)) {
i=*p++;
// fprintf(stderr,"j=%d\n",j);
while (j--!=0) { *to++=(i>>6); i<<=2; }
}
}
assert((slen_t)((char*)to-rowbeg)==(slen_t)wd*cpp*ht);
} else if (oldBpc==4) {
htc=ht; while (htc--!=0) {
toend=to+((wdcpp)&~1);
while (to!=toend) {
i=*p++;
*to++=(i>>4);
*to++=( i&15);
}
if (0!=((wdcpp)&1)) *to++=(*p++>>4);
}
} else assert(0 && "invalid bpc");
delete [] const_cast<char*>(oldBeg);
}
unsigned char Image::Sampled::minRGBBpc() const {
unsigned char *crow=new unsigned char[wd*3], *p, *pend=crow+wd*3;
register unsigned minbpb=0;
dimen_t htc;
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow; p!=pend; p++) {
if ((*p&15)*17!=*p) { delete [] crow; return 8; } /* 4 bits are not enough */
else if ((*p&3)*85!=*p) minbpb=3; /* 2 bits are not enough */
else if ((*p&1)*255!=*p) minbpb|=1; /* 1 bit is not enough */
}
}
delete [] crow;
return 1+minbpb;
}
bool Image::Sampled::hasPixelRGB(Image::Sampled::rgb_t rgb) const {
/* by [email protected] at Sat Jan 8 13:24:19 CET 2005 */
/* Dat: this dumb implementation will be overridden */
if (rgb>0xffffffUL) return false;
unsigned char *crow=new unsigned char[wd*3], *p, *pend=crow+wd*3, t[3];
dimen_t htc;
t[0]=(rgb>>16)&255; t[1]=(rgb>>8)&255; t[2]=rgb&255;
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc);
for (p=crow; p!=pend; p+=3) {
if (t[0]==p[0] && t[1]==p[1] && t[2]==p[2]) { delete [] crow; return true; }
}
}
delete [] crow;
return false;
}
bool Image::Gray::hasPixelRGB(Image::Sampled::rgb_t rgb) const {
/* by [email protected] at Sat Jan 8 13:24:19 CET 2005 */
/* Dat: faster than Image::Sampled::hasPixelRGB */
if (rgb>0xffffffUL) return false;
unsigned char t[3];
t[0]=(rgb>>16)&255; t[1]=(rgb>>8)&255; t[2]=rgb&255;
if (t[0]!=t[1] || t[0]!=t[2]) return false;
if (bpc==8) {
unsigned char *p=(unsigned char*)rowbeg, *pend=p+wd*ht;
/* Imp: use memchr() if available */
while (p!=pend && t[0]!=p[0]) p++;
return p!=pend;
}
unsigned char *crow=new unsigned char[wd*3], *p, *pend=crow+wd*3;
dimen_t htc;
for (htc=0;htc<ht;htc++) {
copyRGBRow((char*)crow, htc); /* Imp: avoid this if bpp==8 */
p=crow; while (p!=pend && t[0]!=p[0]) p+=3;
if (p!=pend) { delete [] crow; return true; }
}
delete [] crow;
return false;
}
Image::Indexed* Image::Sampled::addAlpha0(Image::Indexed *iimg, Image::Gray *al) {
if (iimg==NULLP) Error::sev(Error::EERROR) << "addAlpha: too many colors, transparency impossible" << (Error*)0;
iimg->to8();
unsigned ncols;
if ((ncols=iimg->getNcols()) == 256) {
iimg->packPal(); // TODO: Do a more lightweight palette packing if there are only 2 colors (such as PNG import through PNM).
if ((ncols=iimg->getNcols())==256) Error::sev(Error::EERROR) << "addAlpha: too many colors, transparency impossible" << (Error*)0;
}
iimg->setNcolsMove(ncols+1);
/* fprintf(stderr,"old ncols=%u\n", ncols); */
iimg->setPal(ncols,0); /* black */
iimg->setTransp(ncols);
assert(iimg->getRlen()==iimg->getWd());
assert(iimg->getWd()==al->getWd());
char *p=iimg->getRowbeg(), *pend=p+iimg->getRlen()*iimg->getHt(), *alq=al->getRowbeg();
while (p!=pend) {
if ((unsigned char)*alq++!=255) *p=ncols; /* make it transparent */
p++;
}
return iimg;
}
/* --- */
Image::Indexed::Indexed(Image::Sampled::dimen_t wd_, Image::Sampled::dimen_t ht_, unsigned short ncols_, unsigned char bpc_) {
param_assert(ncols_<=256);
/* vvv Dat: `3' is here for an extra palette entry */
init(3,3*ncols_,wd_,ht_,bpc_,TY_INDEXED,1);
transp=-1;
cs=CS_Indexed_RGB;
}
void Image::Indexed::setNcols(unsigned short ncols_) {
headp=rowbeg-ncols_*3;
}
void Image::Indexed::setNcolsMove(unsigned short ncols_) {
param_assert(ncols_<=256);
unsigned ncols=getNcols();
if (ncols_==ncols) return;
if (ncols_<ncols || (slen_t)(headp-beg)>=(ncols_-ncols)*3) {
memmove(rowbeg-ncols_*3, headp, (ncols_<ncols ? ncols_ : ncols)*3);
/* ^^^ *3 BUGFIX at Sun Apr 14 00:50:34 CEST 2002 */
} else { /* Imp: test this routine */
/* Tue Jun 11 16:22:52 CEST 2002 */
assert(ncols_>ncols);
const char *oldBeg=beg, *oldHeadp=headp, *oldRowbeg=rowbeg, *oldEnd=beg+len;
slen_t delta=(ncols_-ncols)*3;
// substr_grow(headp-oldBeg, ncols*3, ncols_*3); /* no such method */
beg=new char[len+delta];
headp= const_cast<char*>(beg)+(headp-oldBeg);
rowbeg=const_cast<char*>(beg)+(rowbeg-oldBeg)+delta;
trail= const_cast<char*>(beg)+(trail-oldBeg)+delta;
assert(beg+(headp-oldBeg)==rowbeg-ncols_*3);
/* Dat: this->xoffs is left unchanged */
memcpy(headp, oldHeadp, oldRowbeg-oldHeadp);
memcpy(rowbeg, oldRowbeg, oldEnd-oldRowbeg);
delete [] const_cast<char*>(oldBeg);
}
headp=rowbeg-ncols_*3;
}
void Image::Indexed::setPal(unsigned char color, Image::Sampled::rgb_t rgb) {
assert(color<(rowbeg-headp)/3);
unsigned char *p=(unsigned char*)headp+3*color;
*p++=rgb>>16;
*p++=rgb>>8;
*p=rgb;
}
void Image::Indexed::setTransp(unsigned char color) {
// param_assert(color>=0); /* always */
assert(transp==-1);
transp=color;
unsigned char *p=(unsigned char*)headp+3*color;
transpc=((Image::Sampled::rgb_t)p[0]<<16)+(p[1]<<8)+p[2];
}
bool Image::Indexed::setTranspc(rgb_t color) {
if (color!=0x1000000UL && color!=transpc) {
char t[3];
t[0]=color>>16; t[1]=color>>8; t[2]=color;
char *p=headp, *pend=rowbeg;
while (p!=pend) { /* Examine the palette. */
if (p[0]==t[0] && p[1]==t[1] && p[2]==t[2]) {
transpc=color;
transp=(p-headp)/3; /* destroy old transparency */
}
p+=3;
}
}
return transp!=-1;
}
bool Image::Indexed::wouldSetTranspc(rgb_t color) const {
if (transp!=-1) return true;
if (color!=0x1000000UL && color!=transpc) {
char t[3];
t[0]=color>>16; t[1]=color>>8; t[2]=color;
char *p=headp, *pend=rowbeg;
while (p!=pend) { /* Examine the palette. */
if (p[0]==t[0] && p[1]==t[1] && p[2]==t[2]) return true;
p+=3;
}
}
return false;
}
void Image::Indexed::setTranspcAndRepack(rgb_t color) {
if (!(color!=0x1000000UL && color!=transpc)) return;
char t[3];
t[0]=color>>16; t[1]=color>>8; t[2]=color;
char *p=headp, *pend=rowbeg;
bool need_repack = false;
while (p!=pend) { /* Examine the palette. */
if (p[0]==t[0] && p[1]==t[1] && p[2]==t[2]) {
transpc=color;
transp=(p-headp)/3; /* destroy old transparency */
need_repack = true;
}
p+=3;
}
if (need_repack) {
const unsigned char old_bpc = bpc;
packPal(); /* May change bpc. */
setBpc(old_bpc);
}
}
void Image::Indexed::to8() { to8nomul(); }
Image::Indexed* Image::Indexed::toIndexed()/* const*/ { return this; }
Image::RGB* Image::Indexed::toRGB(unsigned char bpc_)/* const*/ { return toRGB0(bpc_); }
Image::Gray* Image::Indexed::toGray(unsigned char bpc_)/* const*/ { return toGray0(bpc_); }
bool Image::Indexed::canGray() const {
char *p=headp, *pend=rowbeg, *tp=p+transp*3;
/* ignore transparent color at Sat Jun 15 15:18:24 CEST 2002 */
if (transp!=-1 && tp!=pend-3) {
while (p!=pend) { /* Examine the palette. */
if (p!=tp && (p[0]!=p[1] || p[1]!=p[2])) return false; /* Found a non-gray color. */
p+=3;
}
} else {
if (transp!=-1 && tp==pend-3) pend-=3; /* both conditions are important */
while (p!=pend) { /* Examine the palette. */
if (p[0]!=p[1] || p[1]!=p[2]) return false; /* Found a non-gray color. */
p+=3;
}
}
return true;
}
unsigned char Image::Indexed::minRGBBpc() const {
unsigned char *p=(unsigned char*)headp, *pend=(unsigned char*)rowbeg;
unsigned char *tp=p+transp*3;
/* ignore transparent color at Sat Jun 15 15:18:24 CEST 2002 */
register unsigned minbpb=0;
while (p!=pend) { /* Examine the palette. */
if (p==tp) { p+=3; continue; } /* ignore transparent color */
if ((*p&15)*17!=*p) return 8; /* 4 bits are not enough */
else if ((*p&3)*85!=*p) minbpb=3; /* 2 bits are not enough */
else if ((*p&1)*255!=*p) minbpb|=1; /* 1 bit is not enough */
p++;
}
return 1+minbpb;
}
void Image::Indexed::copyRGBRow(char *to, Image::Sampled::dimen_t whichrow) const {
param_assert(whichrow<ht);
if (wd==0) return;
unsigned char *p=(unsigned char*)rowbeg+rlen*whichrow;
char *r, *toend=to+3*wd;
unsigned int i, j;
if (bpc==1) {
toend-=3*(wd&7);
while (to!=toend) {
i=*p++;
r=headp+3*(i>>7); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>6)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>5)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>4)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>3)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>2)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>1)&1); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*( i&1); *to++=*r++; *to++=*r++; *to++=*r++;
}
i=*p; /* No mem overrun, even if (wd&7)==0 */
j=wd&7;
while (j--!=0) { r=headp+3*(i>>7); *to++=*r++; *to++=*r++; *to++=*r++; i<<=1; }
} else if (bpc==2) {
toend-=3*(wd&3);
while (to!=toend) {
i=*p++;
r=headp+3*(i>>6); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>4)&3); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*((i>>2)&3); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*( i&3); *to++=*r++; *to++=*r++; *to++=*r++;
}
i=*p; /* No mem overrun, even if (wd&7)==0 */
j=wd&3;
while (j--!=0) { r=headp+3*(i>>6); *to++=*r++; *to++=*r++; *to++=*r++; i<<=2; }
} else if (bpc==4) {
toend-=3*(wd&1);
while (to!=toend) {
i=*p++;
r=headp+3*(i>>4); *to++=*r++; *to++=*r++; *to++=*r++;
r=headp+3*( i&15); *to++=*r++; *to++=*r++; *to++=*r++;
}
if (0!=(wd&1)) { r=headp+3*(*p>>4); *to++=*r++; *to++=*r++; *to++=*r++; }
} else if (bpc==8) {
// fprintf(stderr, "p=%u pp=%u ppp=%u\n", p[0], p[1], p[2]);
while (to!=toend) {
r=headp+3**p++; *to++=*r++; *to++=*r++; *to++=*r++;
}
} else assert(0 && "invalid bpc");
}
void Image::Indexed::packPal() {
/* Convert samples, make bpc=8. */
to8();
unsigned oldNcols=getNcols();
unsigned char *p, *pend;
assert((rowbeg-headp)%3==0);
assert(transp>=-1);
assert(transp<(int)oldNcols);
if (oldNcols<=1) return; /* Cannot optimize further. */
/* Find unused colors. old2new[c]=(is c used at least once)?1:0 */
unsigned char old2new[256], newpal[768];
memset(old2new, 0, sizeof(old2new));
for (p=(unsigned char*)rowbeg, pend=p+wd*ht; p!=pend; p++) old2new[*p]=1;
/* Find and eliminate duplicate colors. Build the new palette in the
* beginning of newpal. Use a Hash46 for a quick lookup of colors already
* seen. Use the previously computed old2new, but also overwrite it.
*/
Hash46 h;
int newTransp=-1;
unsigned char *op=old2new, *opend=op+oldNcols, *w, k[6],
*ptransp=(unsigned char*)headp+3*transp; /* ==p-3 if no transparent color */
/* ^^^ headp BUGFIX at Fri Mar 22 18:02:18 CET 2002 */
p=(unsigned char*)headp;
// fprintf(stderr, "oldNcols=%d\n", (int)oldNcols);
unsigned newNcols=0;
while (op!=opend) {
// fprintf(stderr, "color=%d %d\n", (int)(op-old2new), p-ptransp);
if (0!=*op) { /* Map the color only if it is used in the image. */
// fprintf(stderr, "used=%d\n", (int)(op-old2new));
if (p==ptransp) { k[0]=1; k[1]=k[2]=k[3]=0; newTransp=newNcols; }
else { k[0]=0; memcpy(k+1,p,3); }
w=h.walk(k);
assert(w!=NULL); /* Hash cannot be full since h.M>=256. */
if (*w==/*h.*/FREE) {
memcpy(newpal+3*newNcols, p /* k+1 */, 3);
/* ^^^ side effect: make the transparent color black */
memcpy(w,k,4); w[4]=newNcols; *op=newNcols++;
} else *op=w[4];
}
p+=3; op++;
}
// fprintf(stderr,"newTransp=%d transp=%d\n", newTransp, transp);
// assert((newTransp==-1) == (transp==-1));
assert(newTransp==-1 || transp!=-1);
/* ^^^ BUGFIX: == not true, because image may have transparency, but no
* transparent pixels.
*/
assert((char*)p==headp+oldNcols*3);
if (newNcols==oldNcols && transp==newTransp) {
/* Could not change # colors. */
if (transp==-1) goto done;
if ((unsigned)transp==oldNcols-1) { setPal(transp, 0); goto done; }
}
/* Make the transparent color last. */
if (newTransp!=-1 && newTransp!=(int)newNcols-1) {
assert(transp!=-1);
unsigned newLast=newNcols-1;
memcpy(newpal+3*newTransp, newpal+3*newLast, 3);
memset(newpal+3*newLast, 0, 3); transpc=0; /* make it black */
for (op=old2new; op!=opend; op++) if (*op==newLast) *op=newTransp;
old2new[transp]=newLast;
transp=newTransp=newLast;
p=newpal+newTransp*3;
}
/* Update the image. */
for (p=(unsigned char*)rowbeg, pend=p+wd*ht; p!=pend; p++) {
assert(*p<oldNcols);
*p=old2new[*p];
}
/* Update the palette. */
headp=rowbeg-3*newNcols; /* public method getNcols() uses rowbeg-headp */
memcpy(headp, newpal, 3*newNcols);
transp=newTransp;
/* vvv BUGFIX at Tue May 21 13:10:30 CEST 2002 */
if (newTransp==-1) transpc=0x1000000UL; /* Dat: this means: no transparent color */
else { transp=-1; setTransp(newTransp); }
done:
sortPal();
}
void Image::Indexed::sortPal() {
/* Run packPal() first (but it's recursive!) if transp is in the middle. */
unsigned ncols = getNcols(), i;
assert(transp == -1 || transp + 0U == ncols - 1);
assert(ncols <= 256);
if (ncols == 0) return; /* Safe if ncols == 0 and transp == -1. */
if (transp + 0U == ncols - 1) --ncols;
if (ncols <= 1) return;
#if SIZEOF_SHORT>=4
typedef unsigned short d_t;
#elif SIZEOF_INT>=4
typedef unsigned d_t;
#else
typedef unsigned long d_t;
#endif
d_t d[256];
unsigned char *p, *pend;
for (i = 0, p = (unsigned char*)headp; i < ncols; ++i, p += 3) {
d[i] = (d_t)p[0] << 24 | (d_t)p[1] << 16 | (d_t)p[2] << 8 | i;
/*printf("c[%d]=0x%08x\n", i, d[i]);*/
}
for (i = 1; i < ncols; ++i) {
if (d[i] < d[i - 1]) break;
}
if (i >= ncols) return; /* Palette already sorted. */
/* Heap sort (unstable). Based on Knuth's TAOCP 5.2.3.H .
* Although heap sort is unstable, sortPal implements a stable sort, because
* the color index (i) is included in the sorted number (d[i]).
*/
{ unsigned k;
d_t tmp, *i, *j, *l=d+(ncols>>1), *r=d+ncols-1;
while (1) { /* h2: */
k=l-d;
if (k!=0) {
tmp=*--l;
} else {
tmp=*r; *r=d[0];
if (--r==d) { d[0]=tmp; break; }
k++;
}
i=j=l;
while ((j+=k)<=r) { /* h4: */
k<<=1;
if (j<r && j[0]<j[1]) {
if (!(tmp<*++j)) break;
k++;
} else if (!(tmp<*j)) break;
*i=*j; i=j;
}
/* h8: */
*i=tmp;
}
}
unsigned char old2new[256];
for (i = 0, p = (unsigned char*)headp; i < ncols; ++i) {
d_t di = d[i];
/*printf("d[%d]=0x%08x\n", i, di);*/
assert((i == 0 || di >= d[i - 1]) && "bug in sorting palette");
old2new[di & 255] = i;
*p++ = di >> 24; *p++ = di >> 16; *p++ = di >> 8;
}
/* Update the image. */
for (p=(unsigned char*)rowbeg, pend=p+wd*ht; p!=pend; p++) {
*p=old2new[*p];
}
}
void Image::Indexed::delete_separated(register Indexed **p) {
while (*p!=NULLP) delete *p++;
}
Image::Indexed **Image::Indexed::separate() {
assert(getNcols()>=1);
unsigned char ncols1=getNcols()-1;
signed nncols=getNcols()-(transp==-1 ? 0 : 1);
register unsigned char curcol;
Indexed **ret=new Indexed*[nncols+1], **curimg=ret;
Image::Sampled::dimen_t htc;
assert(cpp==1);
slen_t wdcpp=wd/* *cpp*/;
register unsigned char *p;
char *to, *toend;
register unsigned int i;
ret[nncols]=(Indexed*)NULLP;
to8();
for (curcol=0; curcol<=ncols1; curcol++) {
if (transp==(signed int)curcol) continue;
curimg[0]=new Indexed(wd, ht, /*ncols:*/2, /*bpc:*/1);
memcpy(curimg[0]->headp, headp+3*curcol, 3); /* copy the color value */
curimg[0]->setTransp(1);
to=curimg[0]->rowbeg; p=(unsigned char*)rowbeg;
htc=ht; while (htc--!=0) {
toend=to+((wdcpp+7)>>3);
while (to!=toend) {
i =(*p++!=curcol)<<7; i|=(*p++!=curcol)<<6;
i|=(*p++!=curcol)<<5; i|=(*p++!=curcol)<<4;
i|=(*p++!=curcol)<<3; i|=(*p++!=curcol)<<2;
i|=(*p++!=curcol)<<1; i|=(*p++!=curcol);
*to++=i;
}
if (0!=(wdcpp&7)) p+=(wdcpp&7)-8; /* negative */
}
curimg++;
}
assert(curimg==ret+nncols);
return ret;
}
Image::Indexed *Image::Indexed::calcAlpha() {
/* by [email protected] at Tue Jun 4 21:27:29 CEST 2002 */
assert(getNcols()>=1);
packPal(); /* removes transparency if no transparent pixel */
if (transp==-1) return (Image::Indexed*)NULLP;
to8();
Indexed *ret=new Indexed(wd, ht, /*ncols:*/2, /*bpc:*/1);
Image::Sampled::dimen_t htc;
assert(cpp==1);
slen_t wdcpp=wd/* *cpp*/;
register unsigned char *p;
char *to, *toend;
register unsigned int i, i8, i7;
unsigned char transpx=transp;
ret->headp[0]=ret->headp[1]=ret->headp[2]='\xFF'; /* white */
ret->headp[3]=ret->headp[4]=ret->headp[5]='\x00'; /* black, transparent */
ret->setTransp(1);
to=ret->rowbeg; p=(unsigned char*)rowbeg;
assert(transpx!=0);
#if 0
printf("tx=%u\n", transpx);
printf("%u %u %u\n", headp[0], headp[1], headp[2]);
#endif
htc=ht; while (htc--!=0) {
// putchar('.'); printf("mod=%d\n",(to-ret->rowbeg)%ret->rlen);
// assert((to-ret->rowbeg)%ret->rlen==0);
toend=to+(wdcpp>>3); /* ((wdcpp+7)>>3)-1; */
/* ^^^ BUGFIX at Tue Sep 17 11:08:46 CEST 2002 */
assert(toend>=to);
while (to!=toend) {
#if 1 /* add ->pal[0] funcitonality at Sat Jun 15 14:24:25 CEST 2002 */
i=0; i8=256;
/* vvv p[-1]=0 BUGFIX at Sun Dec 8 23:21:47 CET 2002 */
while ((i8>>=1)!=0) if (*p++==transpx) { p[-1]=0; i|=i8; }
#else
i =(*p++==transpx)<<7; i|=(*p++==transpx)<<6;
i|=(*p++==transpx)<<5; i|=(*p++==transpx)<<4;
i|=(*p++==transpx)<<3; i|=(*p++==transpx)<<2;
i|=(*p++==transpx)<<1; i|=(*p++==transpx);
#endif
*to++=i;
}
#if 1 /* This works even when p gets modified; this puts fixed 0 pads at EOLs */
if ((wdcpp&7)!=0) {
i7=1<<(7-(wdcpp&7)); i8=256; i=0;
/* vvv p[-1]=0 BUGFIX at Sun Dec 8 23:21:47 CET 2002 */
while ((i8>>=1)!=i7) if (*p++==transpx) { p[-1]=0; i|=i8; }
*to++=i;
}
#else
if (0!=(wdcpp&7)) p+=(wdcpp&7)-8; /* negative */
#endif
}
assert(ret->rlen==((wd+7)>>3));
/* printf("rlen=%d %d\n", ret->rlen, to-ret->rowbeg); */
assert(to==ret->rowbeg+ht*ret->rlen);
return ret;
}
void Image::Indexed::setBpc(unsigned char bpc_) {
unsigned ncols=getNcols();
if (bpc_==0) {
if (ncols<=2) bpc_=1;
else if (ncols<=4) bpc_=2;
else if (ncols<=16) bpc_=4;
else bpc_=8;
} else {
if (bpc_==1) assert(ncols<=2);
else if (bpc_==2) assert(ncols<=4);
else if (bpc_==4) assert(ncols<=16);
else if (bpc_!=8) param_assert(0 && "invalid bpc_");
}
// fprintf(stderr, "bpc: %u -> %u\n", bpc, bpc_);
if (bpc==bpc_) return;
to8(); /* Imp: make the transition without the intermediate 8-bits... */
if (bpc_==8) return;
if (ht==0 || wd==0) { bpc=bpc_; return; }
const char *oldBeg=beg;
unsigned char *p=(unsigned char*)rowbeg;
assert(cpp==1);
slen_t wdcpp=wd/* *cpp*/;
bpc=bpc_;
rlen=(((rlen_t)bpc_)*wd+7)>>3;
beg=new char[len=rowbeg-oldBeg+rlen*ht+bpc];
headp= const_cast<char*>(beg)+(headp-oldBeg);
rowbeg=const_cast<char*>(beg)+(rowbeg-oldBeg);
trail= const_cast<char*>(beg)+len-bpc;
memcpy(const_cast<char*>(beg), oldBeg, rowbeg-beg);
unsigned char *to=(unsigned char*)rowbeg, *toend;
unsigned int i;
Image::Sampled::dimen_t htc;
if (bpc_==1) {
// This reads bytes from trail.
htc=ht; while (htc--!=0) {
toend=to+((wdcpp+7)>>3);
while (to!=toend) {
i =*p++<<7; i|=*p++<<6; i|=*p++<<5; i|=*p++<<4;
i|=*p++<<3; i|=*p++<<2; i|=*p++<<1; i|=*p++;