pike.git
/
src
/
modules
/
Image
/
colors.c
version
»
Context lines:
10
20
40
80
file
none
3
pike.git/src/modules/Image/colors.c:1:
/* **! module Image **! note
-
**! $Id: colors.c,v 1.
9
1999/
02
/10
21
:
48
:
24
hubbe
Exp $
+
**! $Id: colors.c,v 1.
10
1999/
04
/10
02
:
02
:
01
mirar
Exp $
**! submodule color **! **! This module keeps names and easy handling **! for easy color support. It gives you an easy **! way to get colors from names. **! **! A color is here an object, containing color **! information and methods for conversion, see below. **! **! <ref>Image.color</ref> can be called to make a color object.
pike.git/src/modules/Image/colors.c:90:
**! This is the color object. It has six readable variables, **! <tt>r</tt>, <tt>g</tt>, <tt>b</tt>, for the <i>red</i>, **! <i>green</i> and <i>blue</i> values, **! and <tt>h</tt>, <tt>s</tt>, <tt>v</tt>, for **! the <i>hue</i>, <i>saturation</i> anv <i>value</i> values. */ #include "global.h" #include <config.h>
-
RCSID("$Id: colors.c,v 1.
9
1999/
02
/10
21
:
48
:
24
hubbe
Exp $");
+
RCSID("$Id: colors.c,v 1.
10
1999/
04
/10
02
:
02
:
01
mirar
Exp $");
#include "config.h" #include <math.h> #include "stralloc.h" #include "pike_macros.h" #include "object.h" #include "constants.h" #include "interpret.h"
pike.git/src/modules/Image/colors.c:130:
static struct pike_string *str_array; static struct pike_string *str_string; static struct pike_string *str_r; static struct pike_string *str_g; static struct pike_string *str_b; static struct pike_string *str_h; static struct pike_string *str_s; static struct pike_string *str_v;
-
struct
color_
struct
-
{
-
rgb_group rgb;
-
rgbl_group rgbl;
-
struct
pike_string *name;
-
};
+
static
struct pike_string *
no_
name;
/* forward */
-
static void image_make_rgb_color(INT32 args);
-
static void _image_make_rgb_color(INT32 r,INT32 g,INT32 b);
+
static void _image_make_rgbl_color(INT32 r,INT32 g,INT32 b); static void _image_make_rgbf_color(float r,float g,float b); static void image_make_hsv_color(INT32 args); static void image_make_cmyk_color(INT32 args); static void image_make_color(INT32 args);
-
+
static void image_make_rgb_color(INT32 args);
struct html_color { int r,g,b; char *name; struct pike_string *pname; } html_color[]= {{0,0,0,"black",NULL}, {255,255,255,"white",NULL}, {0,128,0,"green",NULL}, {192,192,192,"silver",NULL}, {0,255,0,"lime",NULL}, {128,128,128,"gray",NULL},
pike.git/src/modules/Image/colors.c:208:
for (i=0;i<n;i++) { push_int(c[i].r); push_int(c[i].g); push_int(c[i].b); f_aggregate(3); } f_aggregate(n); colortable=clone_object(image_colortable_program,1);
+
if (!colortable)
+
fatal("couldn't create colortable\n");
-
push_int(
5
);
-
push_int(
5
);
-
push_int(
5
);
+
push_int(
12
);
+
push_int(
12
);
+
push_int(
12
);
push_int(1); safe_apply(colortable,"cubicles",4); pop_stack(); for (i=0;i<n;i++) push_string(c[i].pname); f_aggregate(n); colornames=sp[-1].u.array; sp--;
pike.git/src/modules/Image/colors.c:245:
static void exit_color_struct(struct object *dummy) { if (THIS->name) { free_string(THIS->name); THIS->name=NULL; } }
+
void _img_nct_map_to_flat_cubicles(rgb_group *s,
+
rgb_group *d,
+
int n,
+
struct neo_colortable *nct,
+
struct nct_dither *dith,
+
int rowlen);
+
static void try_find_name(struct color_struct *this) { rgb_group d;
-
+
static struct nct_dither dith = { NCTD_NONE,NULL,NULL,NULL,NULL };
if (!colors)
-
+
{
+
fprintf(stderr,"make colors\n");
make_colors();
-
+
}
if (this->name)
-
{
-
free_string
(
this->
name
);
-
this->name=NULL
;
-
}
+
fatal("try
_
find_
name
called
twice\n")
;
if (this->rgbl.r!=COLOR_TO_COLORL(this->rgb.r) || this->rgbl.g!=COLOR_TO_COLORL(this->rgb.g) || this->rgbl.b!=COLOR_TO_COLORL(this->rgb.b))
-
+
{
+
copy_shared_string(this->name,no_name);
return;
-
+
}
-
image
_
colortable
_map_
image
((struct neo_colortable*)colortable->storage,
-
&
(this->rgb)
,
&d,
1
,1
);
+
_
img
_
nct_
map_
to_flat_cubicles
(
&
(
this->rgb),&d,1,
+
(
struct neo_colortable*)colortable->storage,
+
&
dith
,1);
if (d.r==this->rgb.r && d.g==this->rgb.g && d.b==this->rgb.b) { unsigned short d2; image_colortable_index_16bit_image( (struct neo_colortable*)colortable->storage, &(this->rgb),&d2,1,1); if (d2<colornames->size) { copy_shared_string(this->name, colornames->item[d2].u.string);
-
+
return;
} }
-
+
copy_shared_string(this->name,no_name);
} /* **! method void create(int r,int g,int b) **! This is the main <ref>Image.color.color</ref> creation **! method, mostly for internal use. **----- internal note: it takes a fourth argument, name of color --- **! */ /*
pike.git/src/modules/Image/colors.c:322:
static void image_color_rgb(INT32 args) { pop_n_elems(args); push_int(THIS->rgb.r); push_int(THIS->rgb.g); push_int(THIS->rgb.b); f_aggregate(3); }
+
static void image_color_rgbf(INT32 args)
+
{
+
pop_n_elems(args);
+
push_float(COLORL_TO_FLOAT(THIS->rgbl.r));
+
push_float(COLORL_TO_FLOAT(THIS->rgbl.g));
+
push_float(COLORL_TO_FLOAT(THIS->rgbl.b));
+
f_aggregate(3);
+
}
+
static void image_color_greylevel(INT32 args) { INT32 r,g,b; if (args==0) { r=87; g=127; b=41; } else
pike.git/src/modules/Image/colors.c:547:
return; } push_int(2); image_color_hex(1); } static void image_color_name(INT32 args) { pop_n_elems(args);
-
if (THIS->name)
-
ref
_
push
_
string
(THIS
->name
);
-
else
+
if (
!
THIS->name)
try_find_name(THIS);
+
+
if
(THIS->name
==no_name
)
image_color_hex(0);
-
+
else
+
ref_push_string(THIS->name);
} /* **! method array|string cast() **! cast the object to an array, representing red, green **! and blue (equal to <tt>-><ref>rgb</ref>()</tt>), or **! to a string, giving the name (equal to <tt>-><ref>name</ref>()</tt>). **! returns the name as string or rgb as array **! see also: rgb, name */
pike.git/src/modules/Image/colors.c:711:
THIS->rgb.r==THIS->rgb.g && THIS->rgb.r==THIS->rgb.b) { pop_stack(); push_int(1); return; } } else if (sp[-1].type==T_STRING) {
-
if (sp[-1].u.string==THIS->name)
+
if (
!THIS->name)
+
try_find_name(THIS);
+
if (
sp[-1].u.string==THIS->name
&& THIS->name!=no_name
)
{ pop_stack(); push_int(1); return; } } pop_stack(); push_int(0); }
pike.git/src/modules/Image/colors.c:866:
static void image_color_mult(INT32 args) { float x=0.0; get_all_args("Image.color.color->`*",args,"%f",&x); pop_n_elems(args); _image_make_rgb_color((int)(THIS->rgb.r*x), (int)(THIS->rgb.g*x), (int)(THIS->rgb.b*x)); }
-
static
int image_color_
arg
(
INT32
args
,rgb_group *rgb)
+
int image_color_
svalue
(
struct
svalue *v
,rgb_group *rgb)
{
-
if (
!args) return 0;
-
if (sp[
-
args].
type==T_OBJECT)
+
if (
v
-
>
type==T_OBJECT)
{ struct color_struct *cs=(struct color_struct*)
-
get_storage(
sp[
-
args].
u.object,image_color_program);
+
get_storage(
v
-
>
u.object,image_color_program);
if (cs) { *rgb=cs->rgb; return 1; } }
-
else if (
sp[
-
args].
type==T_ARRAY)
+
else if (
v
-
>
type==T_ARRAY)
{
-
int
n=sp[
-
args].
u.array->size
;
-
add_ref(sp[
-
args].
u.array
);
-
push_array_items(sp[
-
args].
u.array
);
-
image_make_color(n);
-
if (sp[
-
1
].type==T_
OBJECT
)
+
if
(v
-
>
u.array->size
==3 &&
+
v
-
>
u.array
->item[0].type==T_INT &&
+
v->u.
array
->item
[
1
].
type==T_INT &&
+
v->u.array->item
[
2
].type==T_
INT
)
{
-
struct color_struct *cs
=(
struct color_struct*
)
-
get_storage
(
sp[
-
args
].u.
object,image_color_program
);
-
*
rgb=
cs
->
rgb
;
-
pop_stack
();
+
rgb->r
=(
COLORTYPE
)(
v->u.array->item
[
0
].u.
integer
);
+
rgb
->g
=
(COLORTYPE)(v
->
u.array->item[1].u.integer)
;
+
rgb->b=
(
COLORTYPE
)
(v->u.array->item[2].u.integer)
;
return 1; }
-
pop_stack();
+
}
-
else if (
sp[
-
args].
type==T_STRING)
+
else if (
v
-
>
type==T_STRING)
{
-
push_svalue(
sp-args
);
+
push_svalue(
v
);
image_make_color(1); if (sp[-1].type==T_OBJECT) { struct color_struct *cs=(struct color_struct*)
-
get_storage(
sp[
-
args].
u.object,image_color_program);
+
get_storage(
v
-
>
u.object,image_color_program);
*rgb=cs->rgb; pop_stack(); return 1; } pop_stack(); } return 0; }
-
+
int image_color_arg(INT32 args,rgb_group *rgb)
+
{
+
if (args<=0) return 0;
+
return image_color_svalue(sp-args,rgb);
+
}
+
+
static void image_color_add(INT32 args) { rgb_group rgb; if (!image_color_arg(args,&rgb)) error("Image.color.color->`+: Illegal argument(s)"); pop_n_elems(args); _image_make_rgb_color((int)(THIS->rgb.r+rgb.r), (int)(THIS->rgb.g+rgb.g),
pike.git/src/modules/Image/colors.c:1186:
push_object(clone_object(image_color_program,0)); cs=(struct color_struct*) get_storage(sp[-1].u.object,image_color_program); cs->rgbl.r=(INT32)r; cs->rgbl.g=(INT32)g; cs->rgbl.b=(INT32)b; RGBL_TO_RGB(cs->rgb,cs->rgbl);
-
-
try_find_name(cs);
+
} static void _image_make_rgbf_color(float r,float g,float b) { #define FOO(X) FLOAT_TO_COLORL((X)<0.0?0.0:(X)>1.0?1.0:(X)) _image_make_rgbl_color(FOO(r),FOO(g),FOO(b)); #undef FOO }
-
static
void _image_make_rgb_color(INT32 r,INT32 g,INT32 b)
+
void _image_make_rgb_color(INT32 r,INT32 g,INT32 b)
{ struct color_struct *cs; if (r<0) r=0; else if (r>COLORMAX) r=COLORMAX; if (g<0) g=0; else if (g>COLORMAX) g=COLORMAX; if (b<0) b=0; else if (b>COLORMAX) b=COLORMAX; push_object(clone_object(image_color_program,0)); cs=(struct color_struct*) get_storage(sp[-1].u.object,image_color_program); cs->rgb.r=(COLORTYPE)r; cs->rgb.g=(COLORTYPE)g; cs->rgb.b=(COLORTYPE)b; RGB_TO_RGBL(cs->rgbl,cs->rgb);
-
-
try_find_name(cs);
+
} static void image_make_rgb_color(INT32 args) { INT32 r=0,g=0,b=0; get_all_args("Image.color.rgb()",args,"%i%i%i",&r,&g,&b); _image_make_rgb_color(r,g,b); }
pike.git/src/modules/Image/colors.c:1383:
str_array=make_shared_string("array"); str_string=make_shared_string("string"); str_r=make_shared_string("r"); str_g=make_shared_string("g"); str_b=make_shared_string("b"); str_h=make_shared_string("h"); str_s=make_shared_string("s"); str_v=make_shared_string("v");
+
no_name=make_shared_string("");
+
start_new_program(); ADD_STORAGE(struct color_struct); set_init_callback(init_color_struct); set_exit_callback(exit_color_struct); /* color info methods */ /* function(string:array|string) */ ADD_FUNCTION("cast",image_color_cast,tFunc(tStr,tOr(tArray,tStr)),/* opt */0);
pike.git/src/modules/Image/colors.c:1407:
/* function(object|int:int) */ ADD_FUNCTION("`==",image_color_equal,tFunc(tOr(tObj,tInt),tInt),/* opt */0); /* function(:string) */ ADD_FUNCTION("name",image_color_name,tFunc(,tStr),/* opt */0); /* function(:string) */ ADD_FUNCTION("hex",image_color_hex,tFunc(,tStr),/* opt */0); /* function(:string) */ ADD_FUNCTION("html",image_color_html,tFunc(,tStr),/* opt */0);
-
/* function(:array(int)) */
+
ADD_FUNCTION("rgb",image_color_rgb,tFunc(,tArr(tInt)),/* opt */0);
-
/* function
(
:array
(
int
)) */
+
ADD_FUNCTION
(
"rgbf",image_color_rgbf,tFunc
(
,tArr(tFlt
))
,/*
opt
*/
0);
ADD_FUNCTION("hsv",image_color_hsv,tFunc(,tArr(tInt)),/* opt */0);
-
/* function(:array(float)) */
+
ADD_FUNCTION("hsvf",image_color_hsvf,tFunc(,tArr(tFlt)),/* opt */0);
-
/* function(:array(float)) */
+
ADD_FUNCTION("cmyk",image_color_cmyk,tFunc(,tArr(tFlt)),/* opt */0);
-
/* function(:int)|function(int,int,int:int) */
+
ADD_FUNCTION("greylevel",image_color_greylevel,tOr(tFunc(,tInt),tFunc(tInt tInt tInt,tInt)),/* opt */0); /* color conversion methods */ /* function(:object)|function(int,int,int:object) */ ADD_FUNCTION("grey",image_color_grey,tOr(tFunc(,tObj),tFunc(tInt tInt tInt,tObj)), /* opt */0); /* function(:object) */ ADD_FUNCTION("light",image_color_light,tFunc(,tObj),/* opt */0);
pike.git/src/modules/Image/colors.c:1508:
} free_string(str_array); free_string(str_string); free_string(str_r); free_string(str_g); free_string(str_b); free_string(str_h); free_string(str_s); free_string(str_v);
+
free_string(no_name);
}