GRASS GIS 8 Programmer's Manual
8.2.2dev(2023)-3d2c704037
color_insrt.c
Go to the documentation of this file.
1
/* This routine is public only because source is in different files.
2
* It should NEVER be called directly.
3
* It is used by Rast_add_c_color_rule() and G__read_old_colors().
4
* These routines know when it is appropriate to call this routine.
5
*/
6
#include <
grass/gis.h
>
7
#include <
grass/raster.h
>
8
9
#define umalloc(n) (unsigned char *) G_malloc((size_t)n)
10
#define urealloc(s,n) (unsigned char *) G_realloc(s,(size_t)n)
11
12
#define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
13
14
int
Rast__insert_color_into_lookup
(
CELL
cat,
15
int
red,
int
grn,
int
blu,
16
struct
_Color_Info_
*cp)
17
{
18
long
nalloc;
19
long
i;
20
long
newlen, curlen, gap;
21
22
LIMIT
(red);
23
LIMIT
(grn);
24
LIMIT
(blu);
25
26
/* first color? */
27
if
(!cp->
lookup
.
active
) {
28
cp->
lookup
.
active
= 1;
29
cp->
lookup
.
nalloc
= 256;
30
cp->
lookup
.
red
=
umalloc
(cp->
lookup
.
nalloc
);
31
cp->
lookup
.
grn
=
umalloc
(cp->
lookup
.
nalloc
);
32
cp->
lookup
.
blu
=
umalloc
(cp->
lookup
.
nalloc
);
33
cp->
lookup
.
set
=
umalloc
(cp->
lookup
.
nalloc
);
34
cp->
max
= cp->
min
= cat;
35
}
36
37
/* extend the color table? */
38
else
if
(cat > cp->
max
) {
39
curlen = cp->
max
- cp->
min
+ 1;
40
newlen = cat - cp->
min
+ 1;
41
nalloc = newlen;
42
if
(nalloc != (
int
)nalloc)
/* check for int overflow */
43
return
-1;
44
45
if
(nalloc > cp->
lookup
.
nalloc
) {
46
while
(cp->
lookup
.
nalloc
< nalloc)
47
cp->
lookup
.
nalloc
+= 256;
48
nalloc = cp->
lookup
.
nalloc
;
49
50
cp->
lookup
.
red
=
urealloc
((
char
*)cp->
lookup
.
red
, nalloc);
51
cp->
lookup
.
grn
=
urealloc
((
char
*)cp->
lookup
.
grn
, nalloc);
52
cp->
lookup
.
blu
=
urealloc
((
char
*)cp->
lookup
.
blu
, nalloc);
53
cp->
lookup
.
set
=
urealloc
((
char
*)cp->
lookup
.
set
, nalloc);
54
}
55
56
/* fill in gap with white */
57
for
(i = curlen; i < newlen; i++) {
58
cp->
lookup
.
red
[i] = 255;
59
cp->
lookup
.
grn
[i] = 255;
60
cp->
lookup
.
blu
[i] = 255;
61
cp->
lookup
.
set
[i] = 0;
62
}
63
cp->
max
= cat;
64
}
65
else
if
(cat < cp->
min
) {
66
curlen = cp->
max
- cp->
min
+ 1;
67
newlen = cp->
max
- cat + 1;
68
gap = newlen - curlen;
69
nalloc = newlen;
70
if
(nalloc != (
int
)nalloc)
/* check for int overflow */
71
return
-1;
72
73
if
(nalloc > cp->
lookup
.
nalloc
) {
74
while
(cp->
lookup
.
nalloc
< nalloc)
75
cp->
lookup
.
nalloc
+= 256;
76
nalloc = cp->
lookup
.
nalloc
;
77
78
cp->
lookup
.
red
=
urealloc
((
char
*)cp->
lookup
.
red
, nalloc);
79
cp->
lookup
.
grn
=
urealloc
((
char
*)cp->
lookup
.
grn
, nalloc);
80
cp->
lookup
.
blu
=
urealloc
((
char
*)cp->
lookup
.
blu
, nalloc);
81
cp->
lookup
.
set
=
urealloc
((
char
*)cp->
lookup
.
set
, nalloc);
82
}
83
84
/* shift the table to make room in front */
85
for
(i = 1; i <= curlen; i++) {
86
cp->
lookup
.
red
[newlen - i] = cp->
lookup
.
red
[curlen - i];
87
cp->
lookup
.
grn
[newlen - i] = cp->
lookup
.
grn
[curlen - i];
88
cp->
lookup
.
blu
[newlen - i] = cp->
lookup
.
blu
[curlen - i];
89
cp->
lookup
.
set
[newlen - i] = cp->
lookup
.
set
[curlen - i];
90
}
91
92
/* fill in gap with white */
93
for
(i = 1; i < gap; i++) {
94
cp->
lookup
.
red
[i] = 255;
95
cp->
lookup
.
grn
[i] = 255;
96
cp->
lookup
.
blu
[i] = 255;
97
cp->
lookup
.
set
[i] = 0;
98
}
99
cp->
min
= cat;
100
}
101
102
/* set the color! */
103
i = cat - cp->
min
;
104
cp->
lookup
.
red
[i] = red;
105
cp->
lookup
.
grn
[i] = grn;
106
cp->
lookup
.
blu
[i] = blu;
107
cp->
lookup
.
set
[i] = 1;
108
109
return
1;
110
}
Rast__insert_color_into_lookup
int Rast__insert_color_into_lookup(CELL cat, int red, int grn, int blu, struct _Color_Info_ *cp)
Definition:
color_insrt.c:14
_Color_Info_::blu
unsigned char * blu
Definition:
gis.h:658
_Color_Info_::red
unsigned char * red
Definition:
gis.h:656
min
#define min(x, y)
Definition:
draw2.c:31
umalloc
#define umalloc(n)
Definition:
color_insrt.c:9
_Color_Info_::grn
unsigned char * grn
Definition:
gis.h:657
_Color_Info_::nalloc
int nalloc
Definition:
gis.h:660
_Color_Info_::set
unsigned char * set
Definition:
gis.h:659
urealloc
#define urealloc(s, n)
Definition:
color_insrt.c:10
_Color_Info_
Definition:
gis.h:649
raster.h
gis.h
_Color_Info_::min
DCELL min
Definition:
gis.h:673
_Color_Info_::active
int active
Definition:
gis.h:661
CELL
int CELL
Definition:
gis.h:613
_Color_Info_::lookup
struct _Color_Info_::@3 lookup
_Color_Info_::max
DCELL max
Definition:
gis.h:673
LIMIT
#define LIMIT(x)
Definition:
color_insrt.c:12
lib
raster
color_insrt.c
Generated on Tue Feb 14 2023 05:55:58 for GRASS GIS 8 Programmer's Manual by
1.8.13