/
pix2.c
3387 lines (3024 loc) · 101 KB
/
pix2.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 pix2.c
* <pre>
*
* This file has these basic operations:
*
* (1) Get and set: individual pixels, full image, rectangular region,
* pad pixels, border pixels, and color components for RGB
* (2) Add and remove border pixels
* (3) Endian byte swaps
* (4) Simple method for byte-processing images (instead of words)
*
* Pixel poking
* l_int32 pixGetPixel()
* l_int32 pixSetPixel()
* l_int32 pixGetRGBPixel()
* l_int32 pixSetRGBPixel()
* l_int32 pixGetRandomPixel()
* l_int32 pixClearPixel()
* l_int32 pixFlipPixel()
* void setPixelLow()
*
* Find black or white value
* l_int32 pixGetBlackOrWhiteVal()
*
* Full image clear/set/set-to-arbitrary-value
* l_int32 pixClearAll()
* l_int32 pixSetAll()
* l_int32 pixSetAllGray()
* l_int32 pixSetAllArbitrary()
* l_int32 pixSetBlackOrWhite()
* l_int32 pixSetComponentArbitrary()
*
* Rectangular region clear/set/set-to-arbitrary-value/blend
* l_int32 pixClearInRect()
* l_int32 pixSetInRect()
* l_int32 pixSetInRectArbitrary()
* l_int32 pixBlendInRect()
*
* Set pad bits
* l_int32 pixSetPadBits()
* l_int32 pixSetPadBitsBand()
*
* Assign border pixels
* l_int32 pixSetOrClearBorder()
* l_int32 pixSetBorderVal()
* l_int32 pixSetBorderRingVal()
* l_int32 pixSetMirroredBorder()
* PIX *pixCopyBorder()
*
* Add and remove border
* PIX *pixAddBorder()
* PIX *pixAddBlackOrWhiteBorder()
* PIX *pixAddBorderGeneral()
* PIX *pixRemoveBorder()
* PIX *pixRemoveBorderGeneral()
* PIX *pixRemoveBorderToSize()
* PIX *pixAddMirroredBorder()
* PIX *pixAddRepeatedBorder()
* PIX *pixAddMixedBorder()
* PIX *pixAddContinuedBorder()
*
* Helper functions using alpha
* l_int32 pixShiftAndTransferAlpha()
* PIX *pixDisplayLayersRGBA()
*
* Color sample setting and extraction
* PIX *pixCreateRGBImage()
* PIX *pixGetRGBComponent()
* l_int32 pixSetRGBComponent()
* PIX *pixGetRGBComponentCmap()
* l_int32 pixCopyRGBComponent()
* l_int32 composeRGBPixel()
* l_int32 composeRGBAPixel()
* void extractRGBValues()
* void extractRGBAValues()
* l_int32 extractMinMaxComponent()
* l_int32 pixGetRGBLine()
*
* Conversion between big and little endians
* PIX *pixEndianByteSwapNew()
* l_int32 pixEndianByteSwap()
* l_int32 lineEndianByteSwap()
* PIX *pixEndianTwoByteSwapNew()
* l_int32 pixEndianTwoByteSwap()
*
* Extract raster data as binary string
* l_int32 pixGetRasterData()
*
* Test alpha component opaqueness
* l_int32 pixAlphaIsOpaque
*
* Setup helpers for 8 bpp byte processing
* l_uint8 **pixSetupByteProcessing()
* l_int32 pixCleanupByteProcessing()
*
* Setting parameters for antialias masking with alpha transforms
* void l_setAlphaMaskBorder()
* </pre>
*/
#include <string.h>
#include "allheaders.h"
static const l_uint32 rmask32[] = {0x0,
0x00000001, 0x00000003, 0x00000007, 0x0000000f,
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff};
/* This is a global that determines the default 8 bpp alpha mask values
* for rings at distance 1 and 2 from the border. Declare extern
* to use. To change the values, use l_setAlphaMaskBorder(). */
LEPT_DLL l_float32 AlphaMaskBorderVals[2] = {0.0, 0.5};
#ifndef NO_CONSOLE_IO
#define DEBUG_SERIALIZE 0
#endif /* ~NO_CONSOLE_IO */
/*-------------------------------------------------------------*
* Pixel poking *
*-------------------------------------------------------------*/
/*!
* \brief pixGetPixel()
*
* \param[in] pix
* \param[in] x,y pixel coords
* \param[out] pval pixel value
* \return 0 if OK; 1 or 2 on error
*
* <pre>
* Notes:
* (1) This returns the value in the data array. If the pix is
* colormapped, it returns the colormap index, not the rgb value.
* (2) Because of the function overhead and the parameter checking,
* this is much slower than using the GET_DATA_*() macros directly.
* Speed on a 1 Mpixel RGB image, using a 3 GHz machine:
* * pixGet/pixSet: ~25 Mpix/sec
* * GET_DATA/SET_DATA: ~350 MPix/sec
* If speed is important and you're doing random access into
* the pix, use pixGetLinePtrs() and the array access macros.
* (3) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
* </pre>
*/
l_ok
pixGetPixel(PIX *pix,
l_int32 x,
l_int32 y,
l_uint32 *pval)
{
l_int32 w, h, d, wpl, val;
l_uint32 *line, *data;
PROCNAME("pixGetPixel");
if (!pval)
return ERROR_INT("&val not defined", procName, 1);
*pval = 0;
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
wpl = pixGetWpl(pix);
data = pixGetData(pix);
line = data + y * wpl;
switch (d)
{
case 1:
val = GET_DATA_BIT(line, x);
break;
case 2:
val = GET_DATA_DIBIT(line, x);
break;
case 4:
val = GET_DATA_QBIT(line, x);
break;
case 8:
val = GET_DATA_BYTE(line, x);
break;
case 16:
val = GET_DATA_TWO_BYTES(line, x);
break;
case 32:
val = line[x];
break;
default:
return ERROR_INT("depth must be in {1,2,4,8,16,32} bpp", procName, 1);
}
*pval = val;
return 0;
}
/*!
* \brief pixSetPixel()
*
* \param[in] pix
* \param[in] x,y pixel coords
* \param[in] val value to be inserted
* \return 0 if OK; 1 or 2 on error
*
* <pre>
* Notes:
* (1) Warning: the input value is not checked for overflow with respect
* the the depth of %pix, and the sign bit (if any) is ignored.
* * For d == 1, %val > 0 sets the bit on.
* * For d == 2, 4, 8 and 16, %val is masked to the maximum allowable
* pixel value, and any (invalid) higher order bits are discarded.
* (2) See pixGetPixel() for information on performance.
* (3) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
* </pre>
*/
l_ok
pixSetPixel(PIX *pix,
l_int32 x,
l_int32 y,
l_uint32 val)
{
l_int32 w, h, d, wpl;
l_uint32 *line, *data;
PROCNAME("pixSetPixel");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
data = pixGetData(pix);
wpl = pixGetWpl(pix);
line = data + y * wpl;
switch (d)
{
case 1:
if (val)
SET_DATA_BIT(line, x);
else
CLEAR_DATA_BIT(line, x);
break;
case 2:
SET_DATA_DIBIT(line, x, val);
break;
case 4:
SET_DATA_QBIT(line, x, val);
break;
case 8:
SET_DATA_BYTE(line, x, val);
break;
case 16:
SET_DATA_TWO_BYTES(line, x, val);
break;
case 32:
line[x] = val;
break;
default:
return ERROR_INT("depth must be in {1,2,4,8,16,32} bpp", procName, 1);
}
return 0;
}
/*!
* \brief pixGetRGBPixel()
*
* \param[in] pix 32 bpp rgb, not colormapped
* \param[in] x,y pixel coords
* \param[out] prval [optional] red component
* \param[out] pgval [optional] green component
* \param[out] pbval [optional] blue component
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixGetRGBPixel(PIX *pix,
l_int32 x,
l_int32 y,
l_int32 *prval,
l_int32 *pgval,
l_int32 *pbval)
{
l_int32 w, h, d, wpl;
l_uint32 *data, *ppixel;
PROCNAME("pixGetRGBPixel");
if (prval) *prval = 0;
if (pgval) *pgval = 0;
if (pbval) *pbval = 0;
if (!prval && !pgval && !pbval)
return ERROR_INT("no output requested", procName, 1);
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, &d);
if (d != 32)
return ERROR_INT("pix not 32 bpp", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
wpl = pixGetWpl(pix);
data = pixGetData(pix);
ppixel = data + y * wpl + x;
if (prval) *prval = GET_DATA_BYTE(ppixel, COLOR_RED);
if (pgval) *pgval = GET_DATA_BYTE(ppixel, COLOR_GREEN);
if (pbval) *pbval = GET_DATA_BYTE(ppixel, COLOR_BLUE);
return 0;
}
/*!
* \brief pixSetRGBPixel()
*
* \param[in] pix 32 bpp rgb
* \param[in] x,y pixel coords
* \param[in] rval red component
* \param[in] gval green component
* \param[in] bval blue component
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixSetRGBPixel(PIX *pix,
l_int32 x,
l_int32 y,
l_int32 rval,
l_int32 gval,
l_int32 bval)
{
l_int32 w, h, d, wpl;
l_uint32 pixel;
l_uint32 *data, *line;
PROCNAME("pixSetRGBPixel");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, &d);
if (d != 32)
return ERROR_INT("pix not 32 bpp", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
wpl = pixGetWpl(pix);
data = pixGetData(pix);
line = data + y * wpl;
composeRGBPixel(rval, gval, bval, &pixel);
*(line + x) = pixel;
return 0;
}
/*!
* \brief pixGetRandomPixel()
*
* \param[in] pix any depth; can be colormapped
* \param[out] pval [optional] pixel value
* \param[out] px [optional] x coordinate chosen; can be null
* \param[out] py [optional] y coordinate chosen; can be null
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) If the pix is colormapped, it returns the rgb value.
* </pre>
*/
l_ok
pixGetRandomPixel(PIX *pix,
l_uint32 *pval,
l_int32 *px,
l_int32 *py)
{
l_int32 w, h, x, y, rval, gval, bval;
l_uint32 val;
PIXCMAP *cmap;
PROCNAME("pixGetRandomPixel");
if (pval) *pval = 0;
if (px) *px = 0;
if (py) *py = 0;
if (!pval && !px && !py)
return ERROR_INT("no output requested", procName, 1);
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, NULL);
x = rand() % w;
y = rand() % h;
if (px) *px = x;
if (py) *py = y;
if (pval) {
pixGetPixel(pix, x, y, &val);
if ((cmap = pixGetColormap(pix)) != NULL) {
pixcmapGetColor(cmap, val, &rval, &gval, &bval);
composeRGBPixel(rval, gval, bval, pval);
} else {
*pval = val;
}
}
return 0;
}
/*!
* \brief pixClearPixel()
*
* \param[in] pix any depth; warning if colormapped
* \param[in] x,y pixel coords
* \return 0 if OK; 1 or 2 on error.
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixClearPixel(PIX *pix,
l_int32 x,
l_int32 y)
{
l_int32 w, h, d, wpl;
l_uint32 *line, *data;
PROCNAME("pixClearPixel");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
if (pixGetColormap(pix))
L_WARNING("cmapped: setting to 0 may not be intended\n", procName);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
wpl = pixGetWpl(pix);
data = pixGetData(pix);
line = data + y * wpl;
switch (d)
{
case 1:
CLEAR_DATA_BIT(line, x);
break;
case 2:
CLEAR_DATA_DIBIT(line, x);
break;
case 4:
CLEAR_DATA_QBIT(line, x);
break;
case 8:
SET_DATA_BYTE(line, x, 0);
break;
case 16:
SET_DATA_TWO_BYTES(line, x, 0);
break;
case 32:
line[x] = 0;
break;
default:
return ERROR_INT("depth must be in {1,2,4,8,16,32} bpp", procName, 1);
}
return 0;
}
/*!
* \brief pixFlipPixel()
*
* \param[in] pix any depth, warning if colormapped
* \param[in] x,y pixel coords
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixFlipPixel(PIX *pix,
l_int32 x,
l_int32 y)
{
l_int32 w, h, d, wpl;
l_uint32 val;
l_uint32 *line, *data;
PROCNAME("pixFlipPixel");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
if (pixGetColormap(pix))
L_WARNING("cmapped: setting to 0 may not be intended\n", procName);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;
data = pixGetData(pix);
wpl = pixGetWpl(pix);
line = data + y * wpl;
switch (d)
{
case 1:
val = GET_DATA_BIT(line, x);
if (val)
CLEAR_DATA_BIT(line, x);
else
SET_DATA_BIT(line, x);
break;
case 2:
val = GET_DATA_DIBIT(line, x);
val ^= 0x3;
SET_DATA_DIBIT(line, x, val);
break;
case 4:
val = GET_DATA_QBIT(line, x);
val ^= 0xf;
SET_DATA_QBIT(line, x, val);
break;
case 8:
val = GET_DATA_BYTE(line, x);
val ^= 0xff;
SET_DATA_BYTE(line, x, val);
break;
case 16:
val = GET_DATA_TWO_BYTES(line, x);
val ^= 0xffff;
SET_DATA_TWO_BYTES(line, x, val);
break;
case 32:
val = line[x] ^ 0xffffffff;
line[x] = val;
break;
default:
return ERROR_INT("depth must be in {1,2,4,8,16,32} bpp", procName, 1);
}
return 0;
}
/*!
* \brief setPixelLow()
*
* \param[in] line ptr to beginning of line,
* \param[in] x pixel location in line
* \param[in] depth bpp
* \param[in] val to be inserted
* \return void
*
* <pre>
* Notes:
* (1) Caution: input variables are not checked!
* </pre>
*/
void
setPixelLow(l_uint32 *line,
l_int32 x,
l_int32 depth,
l_uint32 val)
{
switch (depth)
{
case 1:
if (val)
SET_DATA_BIT(line, x);
else
CLEAR_DATA_BIT(line, x);
break;
case 2:
SET_DATA_DIBIT(line, x, val);
break;
case 4:
SET_DATA_QBIT(line, x, val);
break;
case 8:
SET_DATA_BYTE(line, x, val);
break;
case 16:
SET_DATA_TWO_BYTES(line, x, val);
break;
case 32:
line[x] = val;
break;
default:
fprintf(stderr, "illegal depth in setPixelLow()\n");
}
return;
}
/*-------------------------------------------------------------*
* Find black or white value *
*-------------------------------------------------------------*/
/*!
* \brief pixGetBlackOrWhiteVal()
*
* \param[in] pixs all depths; cmap ok
* \param[in] op L_GET_BLACK_VAL, L_GET_WHITE_VAL
* \param[out] pval pixel value
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) Side effect. For a colormapped image, if the requested
* color is not present and there is room to add it in the cmap,
* it is added and the new index is returned. If there is no room,
* the index of the closest color in intensity is returned.
* </pre>
*/
l_ok
pixGetBlackOrWhiteVal(PIX *pixs,
l_int32 op,
l_uint32 *pval)
{
l_int32 d, val;
PIXCMAP *cmap;
PROCNAME("pixGetBlackOrWhiteVal");
if (!pval)
return ERROR_INT("&val not defined", procName, 1);
*pval = 0;
if (!pixs)
return ERROR_INT("pixs not defined", procName, 1);
if (op != L_GET_BLACK_VAL && op != L_GET_WHITE_VAL)
return ERROR_INT("invalid op", procName, 1);
cmap = pixGetColormap(pixs);
d = pixGetDepth(pixs);
if (!cmap) {
if ((d == 1 && op == L_GET_WHITE_VAL) ||
(d > 1 && op == L_GET_BLACK_VAL)) { /* min val */
val = 0;
} else { /* max val */
val = (d == 32) ? 0xffffff00 : (1 << d) - 1;
}
} else { /* handle colormap */
if (op == L_GET_BLACK_VAL)
pixcmapAddBlackOrWhite(cmap, 0, &val);
else /* L_GET_WHITE_VAL */
pixcmapAddBlackOrWhite(cmap, 1, &val);
}
*pval = val;
return 0;
}
/*-------------------------------------------------------------*
* Full image clear/set/set-to-arbitrary-value/invert *
*-------------------------------------------------------------*/
/*!
* \brief pixClearAll()
*
* \param[in] pix all depths; use cmapped with caution
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) Clears all data to 0. For 1 bpp, this is white; for grayscale
* or color, this is black.
* (2) Caution: for colormapped pix, this sets the color to the first
* one in the colormap. Be sure that this is the intended color!
* </pre>
*/
l_ok
pixClearAll(PIX *pix)
{
PROCNAME("pixClearAll");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixRasterop(pix, 0, 0, pixGetWidth(pix), pixGetHeight(pix),
PIX_CLR, NULL, 0, 0);
return 0;
}
/*!
* \brief pixSetAll()
*
* \param[in] pix all depths; use cmapped with caution
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) Sets all data to 1. For 1 bpp, this is black; for grayscale
* or color, this is white.
* (2) Caution: for colormapped pix, this sets the pixel value to the
* maximum value supported by the colormap: 2^d - 1. However, this
* color may not be defined, because the colormap may not be full.
* </pre>
*/
l_ok
pixSetAll(PIX *pix)
{
l_int32 n;
PIXCMAP *cmap;
PROCNAME("pixSetAll");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
if ((cmap = pixGetColormap(pix)) != NULL) {
n = pixcmapGetCount(cmap);
if (n < cmap->nalloc) /* cmap is not full */
return ERROR_INT("cmap entry does not exist", procName, 1);
}
pixRasterop(pix, 0, 0, pixGetWidth(pix), pixGetHeight(pix),
PIX_SET, NULL, 0, 0);
return 0;
}
/*!
* \brief pixSetAllGray()
*
* \param[in] pix all depths, cmap ok
* \param[in] grayval in range 0 ... 255
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) N.B. For all images, %grayval == 0 represents black and
* %grayval == 255 represents white.
* (2) For depth < 8, we do our best to approximate the gray level.
* For 1 bpp images, any %grayval < 128 is black; >= 128 is white.
* For 32 bpp images, each r,g,b component is set to %grayval,
* and the alpha component is preserved.
* (3) If pix is colormapped, it adds the gray value, replicated in
* all components, to the colormap if it's not there and there
* is room. If the colormap is full, it finds the closest color in
* L2 distance of components. This index is written to all pixels.
* </pre>
*/
l_ok
pixSetAllGray(PIX *pix,
l_int32 grayval)
{
l_int32 d, spp, index;
l_uint32 val32;
PIX *alpha;
PIXCMAP *cmap;
PROCNAME("pixSetAllGray");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
if (grayval < 0) {
L_WARNING("grayval < 0; setting to 0\n", procName);
grayval = 0;
} else if (grayval > 255) {
L_WARNING("grayval > 255; setting to 255\n", procName);
grayval = 255;
}
/* Handle the colormap case */
cmap = pixGetColormap(pix);
if (cmap) {
pixcmapAddNearestColor(cmap, grayval, grayval, grayval, &index);
pixSetAllArbitrary(pix, index);
return 0;
}
/* Non-cmapped */
d = pixGetDepth(pix);
spp = pixGetSpp(pix);
if (d == 1) {
if (grayval < 128) /* black */
pixSetAll(pix);
else
pixClearAll(pix); /* white */
} else if (d < 8) {
grayval >>= 8 - d;
pixSetAllArbitrary(pix, grayval);
} else if (d == 8) {
pixSetAllArbitrary(pix, grayval);
} else if (d == 16) {
grayval |= (grayval << 8);
pixSetAllArbitrary(pix, grayval);
} else if (d == 32 && spp == 3) {
composeRGBPixel(grayval, grayval, grayval, &val32);
pixSetAllArbitrary(pix, val32);
} else if (d == 32 && spp == 4) {
alpha = pixGetRGBComponent(pix, L_ALPHA_CHANNEL);
composeRGBPixel(grayval, grayval, grayval, &val32);
pixSetAllArbitrary(pix, val32);
pixSetRGBComponent(pix, alpha, L_ALPHA_CHANNEL);
pixDestroy(&alpha);
} else {
L_ERROR("invalid depth: %d\n", procName, d);
return 1;
}
return 0;
}
/*!
* \brief pixSetAllArbitrary()
*
* \param[in] pix all depths; use cmapped with caution
* \param[in] val value to set all pixels
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) Caution 1! For colormapped pix, %val is used as an index
* into a colormap. Be sure that index refers to the intended color.
* If the color is not in the colormap, you should first add it
* and then call this function.
* (2) Caution 2! For 32 bpp pix, the interpretation of the LSB
* of %val depends on whether spp == 3 (RGB) or spp == 4 (RGBA).
* For RGB, the LSB is ignored in image transformations.
* For RGBA, the LSB is interpreted as the alpha (transparency)
* component; full transparency has alpha == 0x0, whereas
* full opacity has alpha = 0xff. An RGBA image with full
* opacity behaves like an RGB image.
* (3) As an example of (2), suppose you want to initialize a 32 bpp
* pix with partial opacity, say 0xee337788. If the pix is 3 spp,
* the 0x88 alpha component will be ignored and may be changed
* in subsequent processing. However, if the pix is 4 spp, the
* alpha component will be retained and used. The function
* pixCreate(w, h, 32) makes an RGB image by default, and
* pixSetSpp(pix, 4) can be used to promote an RGB image to RGBA.
* </pre>
*/
l_ok
pixSetAllArbitrary(PIX *pix,
l_uint32 val)
{
l_int32 n, i, j, w, h, d, wpl, npix;
l_uint32 maxval, wordval;
l_uint32 *data, *line;
PIXCMAP *cmap;
PROCNAME("pixSetAllArbitrary");
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
/* If colormapped, make sure that val is less than the size
* of the cmap array. */
if ((cmap = pixGetColormap(pix)) != NULL) {
n = pixcmapGetCount(cmap);
if (val >= n) {
L_WARNING("index not in colormap; using last color\n", procName);
val = n - 1;
}
}
/* Make sure val isn't too large for the pixel depth.
* If it is too large, set the pixel color to white. */
pixGetDimensions(pix, &w, &h, &d);
if (d < 32) {
maxval = (1 << d) - 1;
if (val > maxval) {
L_WARNING("val = %d too large for depth; using maxval = %d\n",
procName, val, maxval);
val = maxval;
}
}
/* Set up word to tile with */
wordval = 0;
npix = 32 / d; /* number of pixels per 32 bit word */
for (j = 0; j < npix; j++)
wordval |= (val << (j * d));
wpl = pixGetWpl(pix);
data = pixGetData(pix);
for (i = 0; i < h; i++) {
line = data + i * wpl;
for (j = 0; j < wpl; j++) {
*(line + j) = wordval;
}
}
return 0;
}
/*!
* \brief pixSetBlackOrWhite()
*
* \param[in] pixs all depths; cmap ok
* \param[in] op L_SET_BLACK, L_SET_WHITE
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) Function for setting all pixels in an image to either black
* or white.
* (2) If pixs is colormapped, it adds black or white to the
* colormap if it's not there and there is room. If the colormap
* is full, it finds the closest color in intensity.
* This index is written to all pixels.
* </pre>
*/
l_ok
pixSetBlackOrWhite(PIX *pixs,
l_int32 op)
{
l_int32 d, index;
PIXCMAP *cmap;
PROCNAME("pixSetBlackOrWhite");
if (!pixs)
return ERROR_INT("pix not defined", procName, 1);
if (op != L_SET_BLACK && op != L_SET_WHITE)
return ERROR_INT("invalid op", procName, 1);
cmap = pixGetColormap(pixs);
d = pixGetDepth(pixs);
if (!cmap) {
if ((d == 1 && op == L_SET_BLACK) || (d > 1 && op == L_SET_WHITE))
pixSetAll(pixs);
else
pixClearAll(pixs);
} else { /* handle colormap */
if (op == L_SET_BLACK)
pixcmapAddBlackOrWhite(cmap, 0, &index);
else /* L_SET_WHITE */
pixcmapAddBlackOrWhite(cmap, 1, &index);
pixSetAllArbitrary(pixs, index);
}
return 0;
}
/*!
* \brief pixSetComponentArbitrary()
*
* \param[in] pix 32 bpp
* \param[in] comp COLOR_RED, COLOR_GREEN, COLOR_BLUE, L_ALPHA_CHANNEL
* \param[in] val value to set this component
* \return 0 if OK; 1 on error
*
* <pre>
* Notes:
* (1) For example, this can be used to set the alpha component to opaque:
* pixSetComponentArbitrary(pix, L_ALPHA_CHANNEL, 255)
* </pre>
*/
l_ok
pixSetComponentArbitrary(PIX *pix,
l_int32 comp,
l_int32 val)
{
l_int32 i, nwords;
l_uint32 mask1, mask2;
l_uint32 *data;