~kamalmostafa/ubuntu/lucid/pdp/fix-504941-ftbfs

« back to all changes in this revision

Viewing changes to puredata/pdp_ut.c

  • Committer: Bazaar Package Importer
  • Author(s): Guenter Geiger (Debian/GNU)
  • Date: 2005-03-15 22:21:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050315222105-1q287rsihmd9j1tb
Tags: 1:0.12.4-2
* fixed the hardcoded depends
* added 3dp library

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Pure Data Packet - Utility toolkit objects.
3
 
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 *
19
 
 */
20
 
 
21
 
 
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 
24
 
   in the distro. */
25
 
 
26
 
 
27
 
#include "pdp_pd.h"
28
 
#include <math.h>
29
 
 
30
 
/* this object does an add, scale, clip operation */
31
 
 
32
 
t_class *pdp_ut_addscaleclip_class;
33
 
 
34
 
typedef struct pdp_ut_addscaleclip_struct
35
 
{
36
 
    t_object x_obj;
37
 
    t_outlet *x_outlet0;
38
 
    t_float x_min;
39
 
    t_float x_max;
40
 
    t_float x_offset;
41
 
    t_float x_scale;
42
 
} t_pdp_ut_addscaleclip;
43
 
 
44
 
 
45
 
static void pdp_ut_addscaleclip_float(t_pdp_ut_addscaleclip *x, t_floatarg f)
46
 
{
47
 
    f += x->x_offset;
48
 
    f *= x->x_scale;
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);
52
 
}
53
 
 
54
 
static void pdp_ut_addscaleclip_free(t_pdp_ut_addscaleclip *x){}
55
 
 
56
 
void *pdp_ut_addscaleclip_new(t_floatarg offset, t_floatarg scale, t_floatarg min, t_floatarg max)
57
 
{
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); 
60
 
    x->x_offset = offset;
61
 
    x->x_scale = scale;
62
 
    x->x_min = min;
63
 
    x->x_max = max;
64
 
    return (void *)x;
65
 
}
66
 
 
67
 
void pdp_ut_addscaleclip_setup(void)
68
 
{
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);
73
 
}
74
 
 
75
 
 
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 */
79
 
 
80
 
t_class *pdp_ut_linmap_class;
81
 
t_class *pdp_ut_logmap_class;
82
 
t_class *pdp_ut_logmap_comp_class;
83
 
 
84
 
typedef struct pdp_ut_map_struct
85
 
{
86
 
    t_object x_obj;
87
 
    t_outlet *x_outlet0;
88
 
    t_float x_min;
89
 
    t_float x_max;
90
 
} t_pdp_ut_map;
91
 
 
92
 
 
93
 
static void pdp_ut_logmap_float(t_pdp_ut_map *x, t_floatarg f)
94
 
{
95
 
    f = (f < 0.0f) ? 0.0f : f;
96
 
    f = (f > 1.0f) ? 1.0f : f;
97
 
 
98
 
    f = x->x_min * pow((x->x_max / x->x_min), f);
99
 
 
100
 
    outlet_float(x->x_outlet0, f);
101
 
}
102
 
 
103
 
static void pdp_ut_linmap_float(t_pdp_ut_map *x, t_floatarg f)
104
 
{
105
 
    f = (f < 0.0f) ? 0.0f : f;
106
 
    f = (f > 1.0f) ? 1.0f : f;
107
 
 
108
 
    f = x->x_min + ((x->x_max - x->x_min) * f);
109
 
 
110
 
    outlet_float(x->x_outlet0, f);
111
 
}
112
 
 
113
 
static void pdp_ut_logmap_comp_float(t_pdp_ut_map *x, t_floatarg f)
114
 
{
115
 
    f = (f < 0.0f) ? 0.0f : f;
116
 
    f = (f > 1.0f) ? 1.0f : f;
117
 
 
118
 
    f = x->x_min * pow((x->x_max / x->x_min), (1.0f - f));
119
 
 
120
 
    outlet_float(x->x_outlet0, f);
121
 
}
122
 
 
123
 
static void pdp_ut_map_free(t_pdp_ut_map *x){}
124
 
 
125
 
 
126
 
void pdp_ut_map_init(t_pdp_ut_map *x, t_floatarg min, t_floatarg max)
127
 
{
128
 
    x->x_outlet0 = outlet_new(&x->x_obj, &s_float); 
129
 
    x->x_min = min;
130
 
    x->x_max = max;
131
 
}
132
 
 
133
 
void *pdp_ut_logmap_new(t_floatarg min, t_floatarg max)
134
 
{
135
 
    t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_logmap_class);
136
 
    pdp_ut_map_init(x, min, max);
137
 
    return (void *)x;
138
 
}
139
 
 
140
 
void *pdp_ut_linmap_new(t_floatarg min, t_floatarg max)
141
 
{
142
 
    t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_linmap_class);
143
 
    pdp_ut_map_init(x, min, max);
144
 
    return (void *)x;
145
 
}
146
 
 
147
 
void *pdp_ut_logmap_comp_new(t_floatarg min, t_floatarg max)
148
 
{
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);
151
 
    return (void *)x;
152
 
}
153
 
 
154
 
void pdp_ut_logmap_setup(void)
155
 
{
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);
160
 
}
161
 
 
162
 
void pdp_ut_logmap_comp_setup(void)
163
 
{
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);
168
 
}
169
 
 
170
 
void pdp_ut_linmap_setup(void)
171
 
{
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);
176
 
}
177
 
 
178
 
 
179
 
 
180
 
t_class *pdp_ut_rgb2ycrcb_class;
181
 
 
182
 
typedef struct pdp_ut_rgb2ycrcb
183
 
{
184
 
    t_object x_obj;
185
 
    t_outlet *x_outlet_luma;
186
 
    t_outlet *x_outlet_chroma_red;
187
 
    t_outlet *x_outlet_chroma_blue;
188
 
 
189
 
    t_float x_red, x_green, x_blue;
190
 
 
191
 
} t_pdp_ut_rgb2ycrcb;
192
 
 
193
 
 
194
 
static void pdp_ut_rgb2ycrcb_bang  (t_pdp_ut_rgb2ycrcb* x)
195
 
{
196
 
 
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;
200
 
 
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);
204
 
 
205
 
}
206
 
 
207
 
 
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);}
211
 
 
212
 
 
213
 
 
214
 
static void pdp_ut_rgb2ycrcb_free  (t_pdp_ut_rgb2ycrcb* x)  {}
215
 
static void* pdp_ut_rgb2ycrcb_new(void)
216
 
{
217
 
    t_pdp_ut_rgb2ycrcb *x = (t_pdp_ut_rgb2ycrcb *)pd_new(pdp_ut_rgb2ycrcb_class);
218
 
 
219
 
 
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"));
222
 
 
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); 
226
 
 
227
 
    x->x_red = 0.0f;
228
 
    x->x_green = 0.0f;
229
 
    x->x_blue = 0.0f;
230
 
 
231
 
 
232
 
    return (void *)x;
233
 
}
234
 
 
235
 
void pdp_ut_rgb2ycrcb_setup(void)
236
 
{
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);
242
 
}
243
 
 
244
 
 
245
 
#ifdef __cplusplus
246
 
extern "C"
247
 
{
248
 
#endif
249
 
 
250
 
void pdp_ut_setup(void)
251
 
{
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();
257
 
}
258
 
 
259
 
 
260
 
#ifdef __cplusplus
261
 
}
262
 
#endif