-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathpixconv.c
4179 lines (3841 loc) · 139 KB
/
pixconv.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
/*====================================================================*
- Copyright (C) 2001 Leptonica. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials
- provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*====================================================================*/
/*!
* \file pixconv.c
* <pre>
*
* These functions convert between images of different types
* without scaling.
*
* Conversion from 8 bpp grayscale to 1, 2, 4 and 8 bpp
* PIX *pixThreshold8()
*
* Conversion from colormap to full color or grayscale
* PIX *pixRemoveColormapGeneral()
* PIX *pixRemoveColormap()
*
* Add colormap losslessly (8 to 8)
* l_int32 pixAddGrayColormap8()
* PIX *pixAddMinimalGrayColormap8()
*
* Conversion from RGB color to 8 bit gray
* PIX *pixConvertRGBToLuminance()
* PIX *pixConvertRGBToGrayGeneral()
* PIX *pixConvertRGBToGray()
* PIX *pixConvertRGBToGrayFast()
* PIX *pixConvertRGBToGrayMinMax()
* PIX *pixConvertRGBToGraySatBoost()
* PIX *pixConvertRGBToGrayArb()
* PIX *pixConvertRGBToBinaryArb()
*
* Conversion from grayscale to colormap
* PIX *pixConvertGrayToColormap() -- 2, 4, 8 bpp
* PIX *pixConvertGrayToColormap8() -- 8 bpp only
*
* Colorizing conversion from grayscale to color
* PIX *pixColorizeGray() -- 8 bpp or cmapped
*
* Conversion from RGB color to colormap
* PIX *pixConvertRGBToColormap()
*
* Conversion from colormap to 1 bpp
* PIX *pixConvertCmapTo1()
*
* Quantization for relatively small number of colors in source
* l_int32 pixQuantizeIfFewColors()
*
* Conversion from 16 bpp to 8 bpp
* PIX *pixConvert16To8()
*
* Conversion from grayscale to false color
* PIX *pixConvertGrayToFalseColor()
*
* Unpacking conversion from 1 bpp to 2, 4, 8, 16 and 32 bpp
* PIX *pixUnpackBinary()
* PIX *pixConvert1To16()
* PIX *pixConvert1To32()
*
* Unpacking conversion from 1 bpp to 2 bpp
* PIX *pixConvert1To2Cmap()
* PIX *pixConvert1To2()
*
* Unpacking conversion from 1 bpp to 4 bpp
* PIX *pixConvert1To4Cmap()
* PIX *pixConvert1To4()
*
* Unpacking conversion from 1, 2 and 4 bpp to 8 bpp
* PIX *pixConvert1To8()
* PIX *pixConvert2To8()
* PIX *pixConvert4To8()
*
* Unpacking conversion from 8 bpp to 16 bpp
* PIX *pixConvert8To16()
*
* Top-level conversion to 1 bpp
* PIX *pixConvertTo1Adaptive()
* PIX *pixConvertTo1()
* PIX *pixConvertTo1BySampling()
*
* Top-level conversion to 2 bpp
* PIX *pixConvertTo2()
* PIX *pixConvert8To2()
*
* Top-level conversion to 4 bpp
* PIX *pixConvertTo4()
* PIX *pixConvert8To4()
*
* Top-level conversion to 8 bpp
* PIX *pixConvertTo8()
* PIX *pixConvertTo8BySampling()
* PIX *pixConvertTo8Colormap()
*
* Top-level conversion to 16 bpp
* PIX *pixConvertTo16()
*
* Top-level conversion to 32 bpp (RGB)
* PIX *pixConvertTo32() ***
* PIX *pixConvertTo32BySampling() ***
* PIX *pixConvert8To32() ***
*
* Top-level conversion to 8 or 32 bpp, without colormap
* PIX *pixConvertTo8Or32
*
* Conversion between 24 bpp and 32 bpp rgb
* PIX *pixConvert24To32()
* PIX *pixConvert32To24()
*
* Conversion between 32 bpp (1 spp) and 16 or 8 bpp
* PIX *pixConvert32To16()
* PIX *pixConvert32To8()
*
* Removal of alpha component by blending with white background
* PIX *pixRemoveAlpha()
*
* Addition of alpha component to 1 bpp
* PIX *pixAddAlphaTo1bpp()
*
* Lossless depth conversion (unpacking)
* PIX *pixConvertLossless()
*
* Conversion for printing in PostScript
* PIX *pixConvertForPSWrap()
*
* Scaling conversion to subpixel RGB
* PIX *pixConvertToSubpixelRGB()
* PIX *pixConvertGrayToSubpixelRGB()
* PIX *pixConvertColorToSubpixelRGB()
*
* Setting neutral point for min/max boost conversion to gray
* void l_setNeutralBoostVal()
* </pre>
*/
#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <math.h>
#include "allheaders.h"
/* ------- Set neutral point for min/max boost conversion to gray ------ */
/* Call l_setNeutralBoostVal() to change this */
static l_int32 var_NEUTRAL_BOOST_VAL = 180;
#ifndef NO_CONSOLE_IO
#define DEBUG_CONVERT_TO_COLORMAP 0
#define DEBUG_UNROLLING 0
#endif /* ~NO_CONSOLE_IO */
/*-------------------------------------------------------------*
* Conversion from 8 bpp grayscale to 1, 2 4 and 8 bpp *
*-------------------------------------------------------------*/
/*!
* \brief pixThreshold8()
*
* \param[in] pixs 8 bpp grayscale
* \param[in] d destination depth: 1, 2, 4 or 8
* \param[in] nlevels number of levels to be used for colormap
* \param[in] cmapflag 1 if makes colormap; 0 otherwise
* \return pixd thresholded with standard dest thresholds,
* or NULL on error
*
* <pre>
* Notes:
* (1) This uses, by default, equally spaced "target" values
* that depend on the number of levels, with thresholds
* halfway between. For N levels, with separation (N-1)/255,
* there are N-1 fixed thresholds.
* (2) For 1 bpp destination, the number of levels can only be 2
* and if a cmap is made, black is (0,0,0) and white
* is (255,255,255), which is opposite to the convention
* without a colormap.
* (3) For 1, 2 and 4 bpp, the nlevels arg is used if a colormap
* is made; otherwise, we take the most significant bits
* from the src that will fit in the dest.
* (4) For 8 bpp, the input pixs is quantized to nlevels. The
* dest quantized with that mapping, either through a colormap
* table or directly with 8 bit values.
* (5) Typically you should not use make a colormap for 1 bpp dest.
* (6) This is not dithering. Each pixel is treated independently.
* </pre>
*/
PIX *
pixThreshold8(PIX *pixs,
l_int32 d,
l_int32 nlevels,
l_int32 cmapflag)
{
PIX *pixd;
PIXCMAP *cmap;
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs not 8 bpp", __func__, NULL);
if (cmapflag && nlevels < 2)
return (PIX *)ERROR_PTR("nlevels must be at least 2", __func__, NULL);
switch (d) {
case 1:
pixd = pixThresholdToBinary(pixs, 128);
if (cmapflag) {
cmap = pixcmapCreateLinear(1, 2);
pixSetColormap(pixd, cmap);
}
break;
case 2:
pixd = pixThresholdTo2bpp(pixs, nlevels, cmapflag);
break;
case 4:
pixd = pixThresholdTo4bpp(pixs, nlevels, cmapflag);
break;
case 8:
pixd = pixThresholdOn8bpp(pixs, nlevels, cmapflag);
break;
default:
return (PIX *)ERROR_PTR("d must be in {1,2,4,8}", __func__, NULL);
}
if (!pixd)
return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
pixCopyInputFormat(pixd, pixs);
return pixd;
}
/*-------------------------------------------------------------*
* Conversion from colormapped pix *
*-------------------------------------------------------------*/
/*!
* \brief pixRemoveColormapGeneral()
*
* \param[in] pixs any depth, with or without colormap
* \param[in] type REMOVE_CMAP_TO_BINARY,
* REMOVE_CMAP_TO_GRAYSCALE,
* REMOVE_CMAP_TO_FULL_COLOR,
* REMOVE_CMAP_WITH_ALPHA,
* REMOVE_CMAP_BASED_ON_SRC
* \param[in] ifnocmap L_CLONE, L_COPY
* \return pixd always a new pix; without colormap, or NULL on error
*
* <pre>
* Notes:
* (1) Convenience function that allows choice between returning
* a clone or a copy if pixs does not have a colormap.
* (2) See pixRemoveColormap().
* </pre>
*/
PIX *
pixRemoveColormapGeneral(PIX *pixs,
l_int32 type,
l_int32 ifnocmap)
{
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (ifnocmap != L_CLONE && ifnocmap != L_COPY)
return (PIX *)ERROR_PTR("invalid value for ifnocmap", __func__, NULL);
if (pixGetColormap(pixs))
return pixRemoveColormap(pixs, type);
if (ifnocmap == L_CLONE)
return pixClone(pixs);
else
return pixCopy(NULL, pixs);
}
/*!
* \brief pixRemoveColormap()
*
* \param[in] pixs see restrictions below
* \param[in] type REMOVE_CMAP_TO_BINARY,
* REMOVE_CMAP_TO_GRAYSCALE,
* REMOVE_CMAP_TO_FULL_COLOR,
* REMOVE_CMAP_WITH_ALPHA,
* REMOVE_CMAP_BASED_ON_SRC
* \return pixd without colormap, or NULL on error
*
* <pre>
* Notes:
* (1) If pixs does not have a colormap, a clone is returned.
* (2) Otherwise, the input pixs is restricted to 1, 2, 4 or 8 bpp.
* (3) Use REMOVE_CMAP_TO_BINARY only on 1 bpp pix.
* (4) For grayscale conversion from RGB, use a weighted average
* of RGB values, and always return an 8 bpp pix, regardless
* of whether the input pixs depth is 2, 4 or 8 bpp.
* (5) REMOVE_CMAP_TO_FULL_COLOR ignores the alpha component and
* returns a 32 bpp pix with spp == 3 and the alpha bytes are 0.
* (6) For REMOVE_CMAP_BASED_ON_SRC, if there is no color, this
* returns either a 1 bpp or 8 bpp grayscale pix.
* If there is color, this returns a 32 bpp pix, with either:
* * 3 spp, if the alpha values are all 255 (opaque), or
* * 4 spp (preserving the alpha), if any alpha values are not 255.
* </pre>
*/
PIX *
pixRemoveColormap(PIX *pixs,
l_int32 type)
{
l_int32 sval, rval, gval, bval, val0, val1;
l_int32 i, j, k, w, h, d, wpls, wpld, ncolors, nalloc, count;
l_int32 opaque, colorfound, blackwhite;
l_int32 *rmap, *gmap, *bmap, *amap;
l_uint32 *datas, *lines, *datad, *lined, *lut, *graymap;
l_uint32 sword, dword;
PIXCMAP *cmap;
PIX *pixd;
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if ((cmap = pixGetColormap(pixs)) == NULL)
return pixClone(pixs);
if (type != REMOVE_CMAP_TO_BINARY &&
type != REMOVE_CMAP_TO_GRAYSCALE &&
type != REMOVE_CMAP_TO_FULL_COLOR &&
type != REMOVE_CMAP_WITH_ALPHA &&
type != REMOVE_CMAP_BASED_ON_SRC) {
L_WARNING("Invalid type; converting based on src\n", __func__);
type = REMOVE_CMAP_BASED_ON_SRC;
}
pixGetDimensions(pixs, &w, &h, &d);
if (d != 1 && d != 2 && d != 4 && d != 8)
return (PIX *)ERROR_PTR("pixs must be {1,2,4,8} bpp", __func__, NULL);
ncolors = pixcmapGetCount(cmap);
nalloc = 1 << d; /* allocate for max size in case of pixel corruption */
if (ncolors > nalloc)
return (PIX *)ERROR_PTR("too many colors for pixel depth",
__func__, NULL);
if (pixcmapToArrays(cmap, &rmap, &gmap, &bmap, &amap))
return (PIX *)ERROR_PTR("colormap arrays not made", __func__, NULL);
if (d != 1 && type == REMOVE_CMAP_TO_BINARY) {
L_WARNING("not 1 bpp; can't remove cmap to binary\n", __func__);
type = REMOVE_CMAP_BASED_ON_SRC;
}
/* Select output type depending on colormap content */
if (type == REMOVE_CMAP_BASED_ON_SRC) {
pixcmapIsOpaque(cmap, &opaque);
pixcmapHasColor(cmap, &colorfound);
pixcmapIsBlackAndWhite(cmap, &blackwhite);
if (!opaque) { /* save the alpha */
type = REMOVE_CMAP_WITH_ALPHA;
} else if (colorfound) {
type = REMOVE_CMAP_TO_FULL_COLOR;
} else { /* opaque and no color */
if (d == 1 && blackwhite) /* can binarize without loss */
type = REMOVE_CMAP_TO_BINARY;
else
type = REMOVE_CMAP_TO_GRAYSCALE;
}
}
datas = pixGetData(pixs);
wpls = pixGetWpl(pixs);
if (type == REMOVE_CMAP_TO_BINARY) {
if ((pixd = pixCopy(NULL, pixs)) == NULL) {
L_ERROR("pixd not made\n", __func__);
goto cleanup_arrays;
}
pixcmapGetColor(cmap, 0, &rval, &gval, &bval);
val0 = rval + gval + bval;
pixcmapGetColor(cmap, 1, &rval, &gval, &bval);
val1 = rval + gval + bval;
if (val0 < val1) /* photometrically inverted from standard */
pixInvert(pixd, pixd);
pixDestroyColormap(pixd);
} else if (type == REMOVE_CMAP_TO_GRAYSCALE) {
if ((pixd = pixCreate(w, h, 8)) == NULL) {
L_ERROR("pixd not made\n", __func__);
goto cleanup_arrays;
}
pixCopyResolution(pixd, pixs);
pixCopyInputFormat(pixd, pixs);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
graymap = (l_uint32 *)LEPT_CALLOC(nalloc, sizeof(l_uint32));
for (i = 0; i < ncolors; i++) {
graymap[i] = (l_uint32)(L_RED_WEIGHT * rmap[i] +
L_GREEN_WEIGHT * gmap[i] +
L_BLUE_WEIGHT * bmap[i] + 0.5);
}
for (i = 0; i < h; i++) {
lines = datas + i * wpls;
lined = datad + i * wpld;
switch (d) /* depth test above; no default permitted */
{
case 8:
/* Unrolled 4x */
for (j = 0, count = 0; j + 3 < w; j += 4, count++) {
sword = lines[count];
dword = (graymap[(sword >> 24) & 0xff] << 24) |
(graymap[(sword >> 16) & 0xff] << 16) |
(graymap[(sword >> 8) & 0xff] << 8) |
graymap[sword & 0xff];
lined[count] = dword;
}
/* Cleanup partial word */
for (; j < w; j++) {
sval = GET_DATA_BYTE(lines, j);
gval = graymap[sval];
SET_DATA_BYTE(lined, j, gval);
}
#if DEBUG_UNROLLING
#define CHECK_VALUE(a, b, c) if (GET_DATA_BYTE(a, b) != c) { \
lept_stderr("Error: mismatch at %d, %d vs %d\n", \
j, GET_DATA_BYTE(a, b), c); }
for (j = 0; j < w; j++) {
sval = GET_DATA_BYTE(lines, j);
gval = graymap[sval];
CHECK_VALUE(lined, j, gval);
}
#endif
break;
case 4:
/* Unrolled 8x */
for (j = 0, count = 0; j + 7 < w; j += 8, count++) {
sword = lines[count];
dword = (graymap[(sword >> 28) & 0xf] << 24) |
(graymap[(sword >> 24) & 0xf] << 16) |
(graymap[(sword >> 20) & 0xf] << 8) |
graymap[(sword >> 16) & 0xf];
lined[2 * count] = dword;
dword = (graymap[(sword >> 12) & 0xf] << 24) |
(graymap[(sword >> 8) & 0xf] << 16) |
(graymap[(sword >> 4) & 0xf] << 8) |
graymap[sword & 0xf];
lined[2 * count + 1] = dword;
}
/* Cleanup partial word */
for (; j < w; j++) {
sval = GET_DATA_QBIT(lines, j);
gval = graymap[sval];
SET_DATA_BYTE(lined, j, gval);
}
#if DEBUG_UNROLLING
for (j = 0; j < w; j++) {
sval = GET_DATA_QBIT(lines, j);
gval = graymap[sval];
CHECK_VALUE(lined, j, gval);
}
#endif
break;
case 2:
/* Unrolled 16x */
for (j = 0, count = 0; j + 15 < w; j += 16, count++) {
sword = lines[count];
dword = (graymap[(sword >> 30) & 0x3] << 24) |
(graymap[(sword >> 28) & 0x3] << 16) |
(graymap[(sword >> 26) & 0x3] << 8) |
graymap[(sword >> 24) & 0x3];
lined[4 * count] = dword;
dword = (graymap[(sword >> 22) & 0x3] << 24) |
(graymap[(sword >> 20) & 0x3] << 16) |
(graymap[(sword >> 18) & 0x3] << 8) |
graymap[(sword >> 16) & 0x3];
lined[4 * count + 1] = dword;
dword = (graymap[(sword >> 14) & 0x3] << 24) |
(graymap[(sword >> 12) & 0x3] << 16) |
(graymap[(sword >> 10) & 0x3] << 8) |
graymap[(sword >> 8) & 0x3];
lined[4 * count + 2] = dword;
dword = (graymap[(sword >> 6) & 0x3] << 24) |
(graymap[(sword >> 4) & 0x3] << 16) |
(graymap[(sword >> 2) & 0x3] << 8) |
graymap[sword & 0x3];
lined[4 * count + 3] = dword;
}
/* Cleanup partial word */
for (; j < w; j++) {
sval = GET_DATA_DIBIT(lines, j);
gval = graymap[sval];
SET_DATA_BYTE(lined, j, gval);
}
#if DEBUG_UNROLLING
for (j = 0; j < w; j++) {
sval = GET_DATA_DIBIT(lines, j);
gval = graymap[sval];
CHECK_VALUE(lined, j, gval);
}
#endif
break;
case 1:
/* Unrolled 8x */
for (j = 0, count = 0; j + 31 < w; j += 32, count++) {
sword = lines[count];
for (k = 0; k < 4; k++) {
/* The top byte is always the relevant one */
dword = (graymap[(sword >> 31) & 0x1] << 24) |
(graymap[(sword >> 30) & 0x1] << 16) |
(graymap[(sword >> 29) & 0x1] << 8) |
graymap[(sword >> 28) & 0x1];
lined[8 * count + 2 * k] = dword;
dword = (graymap[(sword >> 27) & 0x1] << 24) |
(graymap[(sword >> 26) & 0x1] << 16) |
(graymap[(sword >> 25) & 0x1] << 8) |
graymap[(sword >> 24) & 0x1];
lined[8 * count + 2 * k + 1] = dword;
sword <<= 8; /* Move up the next byte */
}
}
/* Cleanup partial word */
for (; j < w; j++) {
sval = GET_DATA_BIT(lines, j);
gval = graymap[sval];
SET_DATA_BYTE(lined, j, gval);
}
#if DEBUG_UNROLLING
for (j = 0; j < w; j++) {
sval = GET_DATA_BIT(lines, j);
gval = graymap[sval];
CHECK_VALUE(lined, j, gval);
}
#undef CHECK_VALUE
#endif
break;
default:
return NULL;
}
}
if (graymap)
LEPT_FREE(graymap);
} else { /* type == REMOVE_CMAP_TO_FULL_COLOR or REMOVE_CMAP_WITH_ALPHA */
if ((pixd = pixCreate(w, h, 32)) == NULL) {
L_ERROR("pixd not made\n", __func__);
goto cleanup_arrays;
}
pixCopyInputFormat(pixd, pixs);
pixCopyResolution(pixd, pixs);
if (type == REMOVE_CMAP_WITH_ALPHA)
pixSetSpp(pixd, 4);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
lut = (l_uint32 *)LEPT_CALLOC(nalloc, sizeof(l_uint32));
for (i = 0; i < ncolors; i++) {
if (type == REMOVE_CMAP_TO_FULL_COLOR)
composeRGBPixel(rmap[i], gmap[i], bmap[i], lut + i);
else /* full color plus alpha */
composeRGBAPixel(rmap[i], gmap[i], bmap[i], amap[i], lut + i);
}
for (i = 0; i < h; i++) {
lines = datas + i * wpls;
lined = datad + i * wpld;
for (j = 0; j < w; j++) {
if (d == 8)
sval = GET_DATA_BYTE(lines, j);
else if (d == 4)
sval = GET_DATA_QBIT(lines, j);
else if (d == 2)
sval = GET_DATA_DIBIT(lines, j);
else /* (d == 1) */
sval = GET_DATA_BIT(lines, j);
if (sval >= ncolors)
L_WARNING("pixel value out of bounds\n", __func__);
else
lined[j] = lut[sval];
}
}
LEPT_FREE(lut);
}
cleanup_arrays:
LEPT_FREE(rmap);
LEPT_FREE(gmap);
LEPT_FREE(bmap);
LEPT_FREE(amap);
return pixd;
}
/*-------------------------------------------------------------*
* Add colormap losslessly (8 to 8) *
*-------------------------------------------------------------*/
/*!
* \brief pixAddGrayColormap8()
*
* \param[in] pixs 8 bpp
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) If pixs has a colormap, this is a no-op.
* </pre>
*/
l_ok
pixAddGrayColormap8(PIX *pixs)
{
PIXCMAP *cmap;
if (!pixs || pixGetDepth(pixs) != 8)
return ERROR_INT("pixs not defined or not 8 bpp", __func__, 1);
if (pixGetColormap(pixs))
return 0;
cmap = pixcmapCreateLinear(8, 256);
pixSetColormap(pixs, cmap);
return 0;
}
/*!
* \brief pixAddMinimalGrayColormap8()
*
* \param[in] pixs 8 bpp
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) This generates a colormapped version of the input image
* that has the same number of colormap entries as the
* input image has unique gray levels.
* </pre>
*/
PIX *
pixAddMinimalGrayColormap8(PIX *pixs)
{
l_int32 ncolors, w, h, i, j, wpl1, wpld, index, val;
l_int32 *inta, *revmap;
l_uint32 *data1, *datad, *line1, *lined;
PIX *pix1, *pixd;
PIXCMAP *cmap;
if (!pixs || pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs undefined or not 8 bpp", __func__, NULL);
/* Eliminate the easy cases */
pixNumColors(pixs, 1, &ncolors);
cmap = pixGetColormap(pixs);
if (cmap) {
if (pixcmapGetCount(cmap) == ncolors) /* irreducible */
return pixCopy(NULL, pixs);
else
pix1 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE);
} else {
if (ncolors == 256) {
pix1 = pixCopy(NULL, pixs);
pixAddGrayColormap8(pix1);
return pix1;
}
pix1 = pixClone(pixs);
}
/* Find the gray levels and make a reverse map */
pixGetDimensions(pix1, &w, &h, NULL);
data1 = pixGetData(pix1);
wpl1 = pixGetWpl(pix1);
inta = (l_int32 *)LEPT_CALLOC(256, sizeof(l_int32));
for (i = 0; i < h; i++) {
line1 = data1 + i * wpl1;
for (j = 0; j < w; j++) {
val = GET_DATA_BYTE(line1, j);
inta[val] = 1;
}
}
cmap = pixcmapCreate(8);
revmap = (l_int32 *)LEPT_CALLOC(256, sizeof(l_int32));
for (i = 0, index = 0; i < 256; i++) {
if (inta[i]) {
pixcmapAddColor(cmap, i, i, i);
revmap[i] = index++;
}
}
/* Set all pixels in pixd to the colormap index */
pixd = pixCreateTemplate(pix1);
pixSetColormap(pixd, cmap);
pixCopyInputFormat(pixd, pixs);
pixCopyResolution(pixd, pixs);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
for (i = 0; i < h; i++) {
line1 = data1 + i * wpl1;
lined = datad + i * wpld;
for (j = 0; j < w; j++) {
val = GET_DATA_BYTE(line1, j);
SET_DATA_BYTE(lined, j, revmap[val]);
}
}
pixDestroy(&pix1);
LEPT_FREE(inta);
LEPT_FREE(revmap);
return pixd;
}
/*-------------------------------------------------------------*
* Conversion from RGB color to grayscale *
*-------------------------------------------------------------*/
/*!
* \brief pixConvertRGBToLuminance()
*
* \param[in] pixs 32 bpp RGB
* \return 8 bpp pix, or NULL on error
*
* <pre>
* Notes:
* (1) Use a standard luminance conversion.
* </pre>
*/
PIX *
pixConvertRGBToLuminance(PIX *pixs)
{
return pixConvertRGBToGray(pixs, 0.0, 0.0, 0.0);
}
/*!
* \brief pixConvertRGBToGrayGeneral()
*
* \param[in] pixs 32 bpp RGB
* \param[in] type color selection flag
* \param[in] rwt, gwt, bwt ignored if type != L_SELECT_WEIGHTED;
* if used, must sum to 1.0.
* \return 8 bpp pix, or NULL on error
*
* <pre>
* Notes:
* (1) The color selection flag is one of: L_SELECT_RED, L_SELECT_GREEN,
* L_SELECT_BLUE, L_SELECT_MIN, L_SELECT_MAX, L_SELECT_AVERAGE,
* L_SELECT_HUE, L_SELECT_SATURATION, L_SELECT_WEIGHTED.
* (2) The weights, if used, must all be non-negative and must sum to 1.0.
* </pre>
*/
PIX *
pixConvertRGBToGrayGeneral(PIX *pixs,
l_int32 type,
l_float32 rwt,
l_float32 gwt,
l_float32 bwt)
{
PIX *pix1;
if (!pixs || pixGetDepth(pixs) != 32)
return (PIX *)ERROR_PTR("pixs undefined or not 32 bpp", __func__, NULL);
if (type != L_SELECT_RED && type != L_SELECT_GREEN &&
type != L_SELECT_BLUE && type != L_SELECT_MIN &&
type != L_SELECT_MAX && type != L_SELECT_AVERAGE &&
type != L_SELECT_HUE && type != L_SELECT_SATURATION &&
type != L_SELECT_WEIGHTED)
return (PIX *)ERROR_PTR("invalid type", __func__, NULL);
if (type == L_SELECT_RED) {
pix1 = pixGetRGBComponent(pixs, COLOR_RED);
} else if (type == L_SELECT_GREEN) {
pix1 = pixGetRGBComponent(pixs, COLOR_GREEN);
} else if (type == L_SELECT_BLUE) {
pix1 = pixGetRGBComponent(pixs, COLOR_BLUE);
} else if (type == L_SELECT_MIN) {
pix1 = pixConvertRGBToGrayMinMax(pixs, L_CHOOSE_MIN);
} else if (type == L_SELECT_MAX) {
pix1 = pixConvertRGBToGrayMinMax(pixs, L_CHOOSE_MAX);
} else if (type == L_SELECT_AVERAGE) {
pix1 = pixConvertRGBToGray(pixs, 0.34, 0.33, 0.33);
} else if (type == L_SELECT_HUE) {
pix1 = pixConvertRGBToHue(pixs);
} else if (type == L_SELECT_SATURATION) {
pix1 = pixConvertRGBToSaturation(pixs);
} else { /* L_SELECT_WEIGHTED */
if (rwt < 0.0 || gwt < 0.0 || bwt < 0.0)
return (PIX *)ERROR_PTR("weights not all >= 0.0", __func__, NULL);
if (rwt + gwt + bwt != 1.0)
return (PIX *)ERROR_PTR("weights don't sum to 1.0", __func__, NULL);
pix1 = pixConvertRGBToGray(pixs, rwt, gwt, bwt);
}
return pix1;
}
/*!
* \brief pixConvertRGBToGray()
*
* \param[in] pixs 32 bpp RGB
* \param[in] rwt, gwt, bwt non-negative; these should add to 1.0,
* or use 0.0 for default
* \return 8 bpp pix, or NULL on error
*
* <pre>
* Notes:
* (1) Use a weighted average of the RGB values.
* </pre>
*/
PIX *
pixConvertRGBToGray(PIX *pixs,
l_float32 rwt,
l_float32 gwt,
l_float32 bwt)
{
l_int32 i, j, w, h, wpls, wpld, val;
l_uint32 word;
l_uint32 *datas, *lines, *datad, *lined;
l_float32 sum;
PIX *pixd;
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (pixGetDepth(pixs) != 32)
return (PIX *)ERROR_PTR("pixs not 32 bpp", __func__, NULL);
if (rwt < 0.0 || gwt < 0.0 || bwt < 0.0)
return (PIX *)ERROR_PTR("weights not all >= 0.0", __func__, NULL);
/* Make sure the sum of weights is 1.0; otherwise, you can get
* overflow in the gray value. */
if (rwt == 0.0 && gwt == 0.0 && bwt == 0.0) {
rwt = L_RED_WEIGHT;
gwt = L_GREEN_WEIGHT;
bwt = L_BLUE_WEIGHT;
}
sum = rwt + gwt + bwt;
if (L_ABS(sum - 1.0) > 0.0001) { /* maintain ratios with sum == 1.0 */
L_WARNING("weights don't sum to 1; maintaining ratios\n", __func__);
rwt = rwt / sum;
gwt = gwt / sum;
bwt = bwt / sum;
}
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
wpls = pixGetWpl(pixs);
if ((pixd = pixCreate(w, h, 8)) == NULL)
return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
pixCopyResolution(pixd, pixs);
pixCopyInputFormat(pixd, pixs);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
for (i = 0; i < h; i++) {
lines = datas + i * wpls;
lined = datad + i * wpld;
for (j = 0; j < w; j++) {
word = *(lines + j);
val = (l_int32)(rwt * ((word >> L_RED_SHIFT) & 0xff) +
gwt * ((word >> L_GREEN_SHIFT) & 0xff) +
bwt * ((word >> L_BLUE_SHIFT) & 0xff) + 0.5);
SET_DATA_BYTE(lined, j, val);
}
}
return pixd;
}
/*!
* \brief pixConvertRGBToGrayFast()
*
* \param[in] pixs 32 bpp RGB
* \return 8 bpp pix, or NULL on error
*
* <pre>
* Notes:
* (1) This function should be used if speed of conversion
* is paramount, and the green channel can be used as
* a fair representative of the RGB intensity. It is
* several times faster than pixConvertRGBToGray().
* (2) To combine RGB to gray conversion with subsampling,
* use pixScaleRGBToGrayFast() instead.
* </pre>
*/
PIX *
pixConvertRGBToGrayFast(PIX *pixs)
{
l_int32 i, j, w, h, wpls, wpld, val;
l_uint32 *datas, *lines, *datad, *lined;
PIX *pixd;
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (pixGetDepth(pixs) != 32)
return (PIX *)ERROR_PTR("pixs not 32 bpp", __func__, NULL);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
wpls = pixGetWpl(pixs);
if ((pixd = pixCreate(w, h, 8)) == NULL)
return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
pixCopyResolution(pixd, pixs);
pixCopyInputFormat(pixd, pixs);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
for (i = 0; i < h; i++) {
lines = datas + i * wpls;
lined = datad + i * wpld;
for (j = 0; j < w; j++, lines++) {
val = ((*lines) >> L_GREEN_SHIFT) & 0xff;
SET_DATA_BYTE(lined, j, val);
}
}
return pixd;
}
/*!
* \brief pixConvertRGBToGrayMinMax()
*
* \param[in] pixs 32 bpp RGB
* \param[in] type L_CHOOSE_MIN, L_CHOOSE_MAX, L_CHOOSE_MAXDIFF,
* L_CHOOSE_MIN_BOOST, L_CHOOSE_MAX_BOOST
* \return 8 bpp pix, or NULL on error
*
* <pre>
* Notes:
* (1) This chooses various components or combinations of them,
* from the three RGB sample values. In addition to choosing
* the min, max, and maxdiff (difference between max and min),
* this also allows boosting the min and max about a reference
* value.
* (2) The default reference value for boosting the min and max
* is 200. This can be changed with l_setNeutralBoostVal()
* (3) The result with L_CHOOSE_MAXDIFF is surprisingly sensitive
* to a jpeg compression/decompression cycle with quality = 75.
* </pre>
*/
PIX *
pixConvertRGBToGrayMinMax(PIX *pixs,
l_int32 type)
{
l_int32 i, j, w, h, wpls, wpld, rval, gval, bval, val, minval, maxval;
l_uint32 *datas, *lines, *datad, *lined;
PIX *pixd;
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (pixGetDepth(pixs) != 32)
return (PIX *)ERROR_PTR("pixs not 32 bpp", __func__, NULL);
if (type != L_CHOOSE_MIN && type != L_CHOOSE_MAX &&
type != L_CHOOSE_MAXDIFF && type != L_CHOOSE_MIN_BOOST &&
type != L_CHOOSE_MAX_BOOST)
return (PIX *)ERROR_PTR("invalid type", __func__, NULL);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
wpls = pixGetWpl(pixs);
if ((pixd = pixCreate(w, h, 8)) == NULL)
return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
pixCopyResolution(pixd, pixs);
pixCopyInputFormat(pixd, pixs);
datad = pixGetData(pixd);
wpld = pixGetWpl(pixd);
for (i = 0; i < h; i++) {
lines = datas + i * wpls;
lined = datad + i * wpld;
for (j = 0; j < w; j++) {
extractRGBValues(lines[j], &rval, &gval, &bval);
if (type == L_CHOOSE_MIN || type == L_CHOOSE_MIN_BOOST) {
val = L_MIN(rval, gval);
val = L_MIN(val, bval);
if (type == L_CHOOSE_MIN_BOOST)
val = L_MIN(255, (val * val) / var_NEUTRAL_BOOST_VAL);
} else if (type == L_CHOOSE_MAX || type == L_CHOOSE_MAX_BOOST) {
val = L_MAX(rval, gval);
val = L_MAX(val, bval);
if (type == L_CHOOSE_MAX_BOOST)
val = L_MIN(255, (val * val) / var_NEUTRAL_BOOST_VAL);
} else { /* L_CHOOSE_MAXDIFF */
minval = L_MIN(rval, gval);
minval = L_MIN(minval, bval);
maxval = L_MAX(rval, gval);
maxval = L_MAX(maxval, bval);
val = maxval - minval;
}
SET_DATA_BYTE(lined, j, val);
}
}
return pixd;
}