Bug 22803 - Make header conform to implementation
[swfdec.git] / swfdec / swfdec_color_transform_as.c
blob3f60aee238c93a13b8ba065dd663f848474dab8c
1 /* Swfdec
2 * Copyright (C) 2007 Pekka Lampila <pekka.lampila@iki.fi>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_color_transform_as.h"
25 #include "swfdec_as_strings.h"
26 #include "swfdec_as_internal.h"
27 #include "swfdec_as_frame_internal.h"
28 #include "swfdec_debug.h"
30 G_DEFINE_TYPE (SwfdecColorTransformAs, swfdec_color_transform_as, SWFDEC_TYPE_AS_RELAY)
32 static void
33 swfdec_color_transform_as_class_init (SwfdecColorTransformAsClass *klass)
37 static void
38 swfdec_color_transform_as_init (SwfdecColorTransformAs *transform)
40 transform->ra = 1;
41 transform->ga = 1;
42 transform->ba = 1;
43 transform->aa = 1;
44 transform->rb = 0;
45 transform->gb = 0;
46 transform->bb = 0;
47 transform->ab = 0;
50 // properties
51 SWFDEC_AS_NATIVE (1105, 101, swfdec_color_transform_as_get_alphaMultiplier)
52 void
53 swfdec_color_transform_as_get_alphaMultiplier (SwfdecAsContext *cx,
54 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
55 SwfdecAsValue *ret)
57 SwfdecColorTransformAs *transform;
59 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
61 *ret = swfdec_as_value_from_number (cx, transform->aa);
64 SWFDEC_AS_NATIVE (1105, 102, swfdec_color_transform_as_set_alphaMultiplier)
65 void
66 swfdec_color_transform_as_set_alphaMultiplier (SwfdecAsContext *cx,
67 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
68 SwfdecAsValue *ret)
70 SwfdecColorTransformAs *transform;
71 double value;
73 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
75 transform->aa = value;
78 SWFDEC_AS_NATIVE (1105, 103, swfdec_color_transform_as_get_redMultiplier)
79 void
80 swfdec_color_transform_as_get_redMultiplier (SwfdecAsContext *cx,
81 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
82 SwfdecAsValue *ret)
84 SwfdecColorTransformAs *transform;
86 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
88 *ret = swfdec_as_value_from_number (cx, transform->ra);
91 SWFDEC_AS_NATIVE (1105, 104, swfdec_color_transform_as_set_redMultiplier)
92 void
93 swfdec_color_transform_as_set_redMultiplier (SwfdecAsContext *cx,
94 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
95 SwfdecAsValue *ret)
97 SwfdecColorTransformAs *transform;
98 double value;
100 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
102 transform->ra = value;
105 SWFDEC_AS_NATIVE (1105, 105, swfdec_color_transform_as_get_greenMultiplier)
106 void
107 swfdec_color_transform_as_get_greenMultiplier (SwfdecAsContext *cx,
108 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
109 SwfdecAsValue *ret)
111 SwfdecColorTransformAs *transform;
113 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
115 *ret = swfdec_as_value_from_number (cx, transform->ga);
118 SWFDEC_AS_NATIVE (1105, 106, swfdec_color_transform_as_set_greenMultiplier)
119 void
120 swfdec_color_transform_as_set_greenMultiplier (SwfdecAsContext *cx,
121 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
122 SwfdecAsValue *ret)
124 SwfdecColorTransformAs *transform;
125 double value;
127 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
129 transform->ga = value;
132 SWFDEC_AS_NATIVE (1105, 107, swfdec_color_transform_as_get_blueMultiplier)
133 void
134 swfdec_color_transform_as_get_blueMultiplier (SwfdecAsContext *cx,
135 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
136 SwfdecAsValue *ret)
138 SwfdecColorTransformAs *transform;
140 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
142 *ret = swfdec_as_value_from_number (cx, transform->ba);
145 SWFDEC_AS_NATIVE (1105, 108, swfdec_color_transform_as_set_blueMultiplier)
146 void
147 swfdec_color_transform_as_set_blueMultiplier (SwfdecAsContext *cx,
148 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
149 SwfdecAsValue *ret)
151 SwfdecColorTransformAs *transform;
152 double value;
154 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
156 transform->ba = value;
159 SWFDEC_AS_NATIVE (1105, 109, swfdec_color_transform_as_get_alphaOffset)
160 void
161 swfdec_color_transform_as_get_alphaOffset (SwfdecAsContext *cx,
162 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
163 SwfdecAsValue *ret)
165 SwfdecColorTransformAs *transform;
167 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
169 *ret = swfdec_as_value_from_number (cx, transform->ab);
172 SWFDEC_AS_NATIVE (1105, 110, swfdec_color_transform_as_set_alphaOffset)
173 void
174 swfdec_color_transform_as_set_alphaOffset (SwfdecAsContext *cx,
175 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
176 SwfdecAsValue *ret)
178 SwfdecColorTransformAs *transform;
179 double value;
181 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
183 transform->ab = value;
186 SWFDEC_AS_NATIVE (1105, 111, swfdec_color_transform_as_get_redOffset)
187 void
188 swfdec_color_transform_as_get_redOffset (SwfdecAsContext *cx,
189 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
190 SwfdecAsValue *ret)
192 SwfdecColorTransformAs *transform;
194 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
196 *ret = swfdec_as_value_from_number (cx, transform->rb);
199 SWFDEC_AS_NATIVE (1105, 112, swfdec_color_transform_as_set_redOffset)
200 void
201 swfdec_color_transform_as_set_redOffset (SwfdecAsContext *cx,
202 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
203 SwfdecAsValue *ret)
205 SwfdecColorTransformAs *transform;
206 double value;
208 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
210 transform->rb = value;
213 SWFDEC_AS_NATIVE (1105, 113, swfdec_color_transform_as_get_greenOffset)
214 void
215 swfdec_color_transform_as_get_greenOffset (SwfdecAsContext *cx,
216 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
217 SwfdecAsValue *ret)
219 SwfdecColorTransformAs *transform;
221 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
223 *ret = swfdec_as_value_from_number (cx, transform->gb);
226 SWFDEC_AS_NATIVE (1105, 114, swfdec_color_transform_as_set_greenOffset)
227 void
228 swfdec_color_transform_as_set_greenOffset (SwfdecAsContext *cx,
229 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
230 SwfdecAsValue *ret)
232 SwfdecColorTransformAs *transform;
233 double value;
235 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
237 transform->gb = value;
240 SWFDEC_AS_NATIVE (1105, 115, swfdec_color_transform_as_get_blueOffset)
241 void
242 swfdec_color_transform_as_get_blueOffset (SwfdecAsContext *cx,
243 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
244 SwfdecAsValue *ret)
246 SwfdecColorTransformAs *transform;
248 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
250 *ret = swfdec_as_value_from_number (cx, transform->bb);
253 SWFDEC_AS_NATIVE (1105, 116, swfdec_color_transform_as_set_blueOffset)
254 void
255 swfdec_color_transform_as_set_blueOffset (SwfdecAsContext *cx,
256 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
257 SwfdecAsValue *ret)
259 SwfdecColorTransformAs *transform;
260 double value;
262 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "n", &value);
264 transform->bb = value;
267 SWFDEC_AS_NATIVE (1105, 117, swfdec_color_transform_as_get_rgb)
268 void
269 swfdec_color_transform_as_get_rgb (SwfdecAsContext *cx,
270 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
271 SwfdecAsValue *ret)
273 SwfdecColorTransformAs *transform;
274 int value;
276 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "");
278 // important to calculate the value this way, to get right results with
279 // negative values
280 value = swfdec_as_double_to_integer (transform->rb) << 16;
281 value |= swfdec_as_double_to_integer (transform->gb) << 8;
282 value |= swfdec_as_double_to_integer (transform->bb);
284 *ret = swfdec_as_value_from_integer (cx, value);
287 SWFDEC_AS_NATIVE (1105, 118, swfdec_color_transform_as_set_rgb)
288 void
289 swfdec_color_transform_as_set_rgb (SwfdecAsContext *cx,
290 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
291 SwfdecAsValue *ret)
293 SwfdecColorTransformAs *transform;
294 int value;
296 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "i", &value);
298 transform->ra = 0;
299 transform->ga = 0;
300 transform->ba = 0;
301 transform->rb = (value & 0xFF0000) >> 16;
302 transform->gb = (value & 0x00FF00) >> 8;
303 transform->bb = (value & 0x0000FF);
306 // normal
307 SWFDEC_AS_NATIVE (1105, 1, swfdec_color_transform_as_concat)
308 void
309 swfdec_color_transform_as_concat (SwfdecAsContext *cx,
310 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
311 SwfdecAsValue *ret)
313 SwfdecColorTransformAs *transform, *other;
314 SwfdecAsObject *other_object;
316 SWFDEC_AS_CHECK (SWFDEC_TYPE_COLOR_TRANSFORM_AS, &transform, "o",
317 &other_object);
319 if (!SWFDEC_IS_COLOR_TRANSFORM_AS (other_object->relay))
320 return;
321 other = SWFDEC_COLOR_TRANSFORM_AS (other_object->relay);
323 transform->rb += (transform->ra * other->rb);
324 transform->gb += (transform->ga * other->gb);
325 transform->bb += (transform->ba * other->bb);
326 transform->ab += (transform->aa * other->ab);
327 transform->ra *= other->ra;
328 transform->ga *= other->ga;
329 transform->ba *= other->ba;
330 transform->aa *= other->aa;
333 // constructor
334 SWFDEC_AS_NATIVE (1105, 0, swfdec_color_transform_as_construct)
335 void
336 swfdec_color_transform_as_construct (SwfdecAsContext *cx,
337 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
338 SwfdecAsValue *ret)
340 SwfdecColorTransformAs *transform;
342 if (!swfdec_as_context_is_constructing (cx))
343 return;
345 transform = g_object_new (SWFDEC_TYPE_COLOR_TRANSFORM_AS, "context", cx, NULL);
346 swfdec_as_object_set_relay (object, SWFDEC_AS_RELAY (transform));
347 SWFDEC_AS_VALUE_SET_OBJECT (ret, object);
349 if (argc < 8)
350 return;
352 SWFDEC_AS_CHECK (0, NULL, "nnnnnnnn",
353 &transform->ra, &transform->ga, &transform->ba, &transform->aa,
354 &transform->rb, &transform->gb, &transform->bb, &transform->ab);
357 SwfdecColorTransformAs *
358 swfdec_color_transform_as_new_from_transform (SwfdecAsContext *context,
359 const SwfdecColorTransform *transform)
361 SwfdecColorTransformAs *transform_as;
362 SwfdecAsObject *object;
364 g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context), NULL);
365 g_return_val_if_fail (transform != NULL, NULL);
367 transform_as = g_object_new (SWFDEC_TYPE_COLOR_TRANSFORM_AS, "context", context, NULL);
368 /* do it this way so the constructor isn't called */
369 object = swfdec_as_object_new (context, NULL);
370 swfdec_as_object_set_relay (object, SWFDEC_AS_RELAY (transform_as));
371 swfdec_as_object_set_constructor_by_name (object,
372 SWFDEC_AS_STR_flash, SWFDEC_AS_STR_geom, SWFDEC_AS_STR_ColorTransform, NULL);
374 transform_as->ra = transform->ra / 256.0;
375 transform_as->ga = transform->ga / 256.0;
376 transform_as->ba = transform->ba / 256.0;
377 transform_as->aa = transform->aa / 256.0;
378 transform_as->rb = transform->rb;
379 transform_as->gb = transform->gb;
380 transform_as->bb = transform->bb;
381 transform_as->ab = transform->ab;
383 return transform_as;
386 void
387 swfdec_color_transform_get_transform (SwfdecColorTransformAs *trans,
388 SwfdecColorTransform *ctrans)
390 g_return_if_fail (SWFDEC_IS_COLOR_TRANSFORM_AS (trans));
391 g_return_if_fail (ctrans != NULL);
393 SWFDEC_FIXME ("This conversion needs serious testing with NaN and overflows");
394 ctrans->mask = FALSE;
395 ctrans->ra = trans->ra * 256.0;
396 ctrans->ga = trans->ga * 256.0;
397 ctrans->ba = trans->ba * 256.0;
398 ctrans->aa = trans->aa * 256.0;
399 ctrans->rb = trans->rb;
400 ctrans->gb = trans->gb;
401 ctrans->bb = trans->bb;
402 ctrans->ab = trans->ab;