2
* Pure Data Packet - Utility toolkit objects.
3
* Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
/* This file contains some small utility pd objects that make working with
23
pdp objects a lot easier. Mainly as glue to be used in the abstractions
30
/* this object does an add, scale, clip operation */
32
t_class *pdp_ut_addscaleclip_class;
34
typedef struct pdp_ut_addscaleclip_struct
42
} t_pdp_ut_addscaleclip;
45
static void pdp_ut_addscaleclip_float(t_pdp_ut_addscaleclip *x, t_floatarg f)
49
f = (f < x->x_min) ? x->x_min : f;
50
f = (f > x->x_max) ? x->x_max : f;
51
outlet_float(x->x_outlet0, f);
54
static void pdp_ut_addscaleclip_free(t_pdp_ut_addscaleclip *x){}
56
void *pdp_ut_addscaleclip_new(t_floatarg offset, t_floatarg scale, t_floatarg min, t_floatarg max)
58
t_pdp_ut_addscaleclip *x = (t_pdp_ut_addscaleclip *)pd_new(pdp_ut_addscaleclip_class);
59
x->x_outlet0 = outlet_new(&x->x_obj, &s_float);
67
void pdp_ut_addscaleclip_setup(void)
69
pdp_ut_addscaleclip_class = class_new(gensym("pdp_ut_addscaleclip"), (t_newmethod)pdp_ut_addscaleclip_new,
70
(t_method)pdp_ut_addscaleclip_free, sizeof(t_pdp_ut_addscaleclip), 0,
71
A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
72
class_addfloat(pdp_ut_addscaleclip_class, pdp_ut_addscaleclip_float);
76
/* pdp_ut_logmap does a logarithmic parameter mapping [0->1] x -> min(max/min)^x max an add, scale, clip operation */
77
/* pdp_ut_logmap_comp does x -> min(max/min)^(1-x) */
78
/* pdp_ut_linmap dos x -> min + (max - min * x */
80
t_class *pdp_ut_linmap_class;
81
t_class *pdp_ut_logmap_class;
82
t_class *pdp_ut_logmap_comp_class;
84
typedef struct pdp_ut_map_struct
93
static void pdp_ut_logmap_float(t_pdp_ut_map *x, t_floatarg f)
95
f = (f < 0.0f) ? 0.0f : f;
96
f = (f > 1.0f) ? 1.0f : f;
98
f = x->x_min * pow((x->x_max / x->x_min), f);
100
outlet_float(x->x_outlet0, f);
103
static void pdp_ut_linmap_float(t_pdp_ut_map *x, t_floatarg f)
105
f = (f < 0.0f) ? 0.0f : f;
106
f = (f > 1.0f) ? 1.0f : f;
108
f = x->x_min + ((x->x_max - x->x_min) * f);
110
outlet_float(x->x_outlet0, f);
113
static void pdp_ut_logmap_comp_float(t_pdp_ut_map *x, t_floatarg f)
115
f = (f < 0.0f) ? 0.0f : f;
116
f = (f > 1.0f) ? 1.0f : f;
118
f = x->x_min * pow((x->x_max / x->x_min), (1.0f - f));
120
outlet_float(x->x_outlet0, f);
123
static void pdp_ut_map_free(t_pdp_ut_map *x){}
126
void pdp_ut_map_init(t_pdp_ut_map *x, t_floatarg min, t_floatarg max)
128
x->x_outlet0 = outlet_new(&x->x_obj, &s_float);
133
void *pdp_ut_logmap_new(t_floatarg min, t_floatarg max)
135
t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_logmap_class);
136
pdp_ut_map_init(x, min, max);
140
void *pdp_ut_linmap_new(t_floatarg min, t_floatarg max)
142
t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_linmap_class);
143
pdp_ut_map_init(x, min, max);
147
void *pdp_ut_logmap_comp_new(t_floatarg min, t_floatarg max)
149
t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_logmap_comp_class);
150
pdp_ut_map_init(x, min, max);
154
void pdp_ut_logmap_setup(void)
156
pdp_ut_logmap_class = class_new(gensym("pdp_ut_logmap"), (t_newmethod)pdp_ut_logmap_new,
157
(t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0,
158
A_FLOAT, A_FLOAT, A_NULL);
159
class_addfloat(pdp_ut_logmap_class, pdp_ut_logmap_float);
162
void pdp_ut_logmap_comp_setup(void)
164
pdp_ut_logmap_comp_class = class_new(gensym("pdp_ut_logmap_comp"), (t_newmethod)pdp_ut_logmap_comp_new,
165
(t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0,
166
A_FLOAT, A_FLOAT, A_NULL);
167
class_addfloat(pdp_ut_logmap_comp_class, pdp_ut_logmap_comp_float);
170
void pdp_ut_linmap_setup(void)
172
pdp_ut_linmap_class = class_new(gensym("pdp_ut_linmap"), (t_newmethod)pdp_ut_linmap_new,
173
(t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0,
174
A_FLOAT, A_FLOAT, A_NULL);
175
class_addfloat(pdp_ut_linmap_class, pdp_ut_linmap_float);
180
t_class *pdp_ut_rgb2ycrcb_class;
182
typedef struct pdp_ut_rgb2ycrcb
185
t_outlet *x_outlet_luma;
186
t_outlet *x_outlet_chroma_red;
187
t_outlet *x_outlet_chroma_blue;
189
t_float x_red, x_green, x_blue;
191
} t_pdp_ut_rgb2ycrcb;
194
static void pdp_ut_rgb2ycrcb_bang (t_pdp_ut_rgb2ycrcb* x)
197
float luma = 0.299f * x->x_red + 0.587f * x->x_green + 0.114f * x->x_blue;
198
float chroma_red = (x->x_red - luma) * 0.713f;
199
float chroma_blue = (x->x_blue - luma) * 0.565f;
201
outlet_float(x->x_outlet_chroma_blue, chroma_blue);
202
outlet_float(x->x_outlet_chroma_red, chroma_red);
203
outlet_float(x->x_outlet_luma, luma);
208
static void pdp_ut_rgb2ycrcb_red (t_pdp_ut_rgb2ycrcb* x, t_floatarg f) {x->x_red = f; pdp_ut_rgb2ycrcb_bang(x);}
209
static void pdp_ut_rgb2ycrcb_green (t_pdp_ut_rgb2ycrcb* x, t_floatarg f) {x->x_green = f; pdp_ut_rgb2ycrcb_bang(x);}
210
static void pdp_ut_rgb2ycrcb_blue (t_pdp_ut_rgb2ycrcb* x, t_floatarg f) {x->x_blue = f; pdp_ut_rgb2ycrcb_bang(x);}
214
static void pdp_ut_rgb2ycrcb_free (t_pdp_ut_rgb2ycrcb* x) {}
215
static void* pdp_ut_rgb2ycrcb_new(void)
217
t_pdp_ut_rgb2ycrcb *x = (t_pdp_ut_rgb2ycrcb *)pd_new(pdp_ut_rgb2ycrcb_class);
220
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("green"));
221
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("blue"));
223
x->x_outlet_luma = outlet_new(&x->x_obj, &s_float);
224
x->x_outlet_chroma_red = outlet_new(&x->x_obj, &s_float);
225
x->x_outlet_chroma_blue = outlet_new(&x->x_obj, &s_float);
235
void pdp_ut_rgb2ycrcb_setup(void)
237
pdp_ut_rgb2ycrcb_class = class_new(gensym("pdp_ut_rgb2ycrcb"), (t_newmethod)pdp_ut_rgb2ycrcb_new,
238
(t_method)pdp_ut_rgb2ycrcb_free, sizeof(t_pdp_ut_rgb2ycrcb), 0, A_NULL);
239
class_addfloat(pdp_ut_rgb2ycrcb_class, pdp_ut_rgb2ycrcb_red);
240
class_addmethod(pdp_ut_rgb2ycrcb_class, (t_method)pdp_ut_rgb2ycrcb_green, gensym("green"), A_FLOAT, A_NULL);
241
class_addmethod(pdp_ut_rgb2ycrcb_class, (t_method)pdp_ut_rgb2ycrcb_blue, gensym("blue"), A_FLOAT, A_NULL);
250
void pdp_ut_setup(void)
252
pdp_ut_addscaleclip_setup();
253
pdp_ut_logmap_setup();
254
pdp_ut_logmap_comp_setup();
255
pdp_ut_linmap_setup();
256
pdp_ut_rgb2ycrcb_setup();