-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathkprEffect.c
390 lines (360 loc) · 11.4 KB
/
kprEffect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 2010-2015 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define __FSKBITMAP_PRIV__
#define __FSKPORT_PRIV__
#include "FskBlur.h"
#include "FskButtonShade.h"
#include "FskDilateErode.h"
#include "FskECMAScript.h"
#include "FskPixelOps.h"
#include "FskMemory.h"
#if FSKBITMAP_OPENGL
#include "FskGLBlit.h" /* needed for FskGLPortSourceTexture, when RARELY_COMPUTE_EFFECTS_IN_GL is defined */
#endif /* FSKBITMAP_OPENGL */
#include "kprSkin.h"
#include "kprBehavior.h"
#include "kprContent.h"
#include "kprEffect.h"
#include "kprHandler.h"
#include "kprLayer.h"
#include "kprShell.h"
#include "kprUtilities.h"
/* EFFECT */
static void KprEffectDispose(void* it);
#if SUPPORT_INSTRUMENTATION
static FskInstrumentedTypeRecord KprEffectInstrumentation = { NULL, sizeof(FskInstrumentedTypeRecord), "KprEffect", FskInstrumentationOffset(KprEffectRecord), NULL, 0, NULL, KprInsrumentationFormatMessage, NULL, 0 };
#endif
FskErr KprEffectNew(KprEffect *it, KprContext context)
{
FskErr err = kFskErrNone;
KprEffect self;
bailIfError(KprAssetNew((KprAsset *)&self, sizeof(KprEffectRecord), context, &context->firstEffect, KprEffectDispose));
*it = self;
FskInstrumentedItemNew(self, NULL, &KprEffectInstrumentation);
#if SUPPORT_INSTRUMENTATION
FskInstrumentedItemSetOwner(self, context);
#endif
bail:
return err;
}
void KprEffectDispose(void* it)
{
KprEffect self = it;
FskMemPtrDispose(self->compound);
FskInstrumentedItemDispose(self);
FskMemPtrDispose(it);
}
void KprEffectAdd(KprEffect self, FskEffect step)
{
FskErr err = kFskErrNone;
FskEffect compound;
if (self->compound) {
if (self->compound->effectID == kFskEffectCompound) {
UInt32 count = 1 + self->compound->params.compound.numStages;
bailIfError(FskMemPtrNewClear(((count + 1) * sizeof(FskEffectRecord)), &compound));
FskMemCopy(compound, self->compound, count * sizeof(FskEffectRecord));
FskMemCopy(compound + count, step, sizeof(FskEffectRecord));
compound->params.compound.numStages = count;
}
else {
bailIfError(FskMemPtrNewClear((3 * sizeof(FskEffectRecord)), &compound));
compound->effectID = kFskEffectCompound;
compound->params.compound.topology = kFskEffectCompoundTopologyPipeline;
compound->params.compound.numStages = 2;
FskMemCopy(compound + 1, self->compound, sizeof(FskEffectRecord));
FskMemCopy(compound + 2, step, sizeof(FskEffectRecord));
}
FskMemPtrDispose(self->compound);
}
else {
bailIfError(FskMemPtrNewClear(sizeof(FskEffectRecord), &compound));
FskMemCopy(compound, step, sizeof(FskEffectRecord));
}
self->compound = compound;
bail:
return; // @@
}
FskErr KprEffectApply(KprEffect self, FskBitmap srcBits, FskPort port, FskBitmap *result)
{
FskErr err = kFskErrNone;
FskRectangleRecord bounds;
FskBitmap dstBits = NULL;
FskBitmap tmp = NULL;
Boolean flag;
FskBitmapGetBounds(srcBits, &bounds);
if (port) {
bailIfError(FskPortGetTempEffectBitmap(port, bounds.width, bounds.height, kFskBitmapFormatDefaultAlpha, &dstBits));
}
else {
bailIfError(FskBitmapNew(bounds.width, bounds.height, kFskBitmapFormatDefaultAlpha, &dstBits));
}
FskBitmapGetHasAlpha(srcBits, &flag);
FskBitmapSetHasAlpha(dstBits, flag);
FskBitmapGetAlphaIsPremultiplied(srcBits, &flag);
FskBitmapSetAlphaIsPremultiplied(dstBits, flag);
#if FSKBITMAP_OPENGL
if (FskBitmapIsOpenGLDestinationAccelerated(dstBits)) { // If we are using GL to do the effects, ...
FskBitmapCheckGLSourceAccelerated(srcBits); // ... make sure that the source is uploaded as a texture, ...
err = FskGLEffectApply(self->compound, srcBits, &bounds, dstBits, NULL);
}
else {
#endif
if (srcBits->pixelFormat != dstBits->pixelFormat) {
bailIfError(FskBitmapNew(bounds.width, bounds.height, kFskBitmapFormatDefaultAlpha, &tmp));
if (kFskErrNone == FskBitmapDraw(srcBits, NULL, dstBits, NULL, NULL, NULL, kFskGraphicsModeCopy, NULL))
srcBits = tmp;
}
err = FskEffectApply(self->compound, srcBits, &bounds, dstBits, NULL);
FskBitmapDispose(tmp);
#if FSKBITMAP_OPENGL
FskBitmapSetOpenGLSourceAccelerated(dstBits, 1); // ... else make sure the result is accelerated.
}
#endif
*result = dstBits;
bail:
return err;
}
void KprEffectExtend(KprEffect self, FskRectangle area)
{
// ??
}
/* ECMASCRIPT */
#ifndef KPR_NO_GRAMMAR
void KPR_effect(void *it)
{
if (it) {
KprEffect self = it;
kprVolatileDestructor(KPR_effect);
KprAssetDispose(self);
}
}
void KPR_Effect(xsMachine *the)
{
KprEffect self;
xsThrowIfFskErr(KprEffectNew(&self, xsGetContext(the)));
kprVolatileConstructor(KPR_Effect);
}
#define APPLY_OPACITY_TO_ALPHA(opacity, alpha) do { if ((opacity) < 1.f) (alpha) = (UInt8)((alpha) * (opacity) + .5f); } while(0)
void KPR_effect_colorize(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
FskColorRGBARecord color = { 128, 128, 128, 255 };
float opacity = 1;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
FskEffectColorizeSet(&step, &color);
KprEffectAdd(self, &step);
}
void KPR_effect_gaussianBlur(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float x = 1;
float y = 1;
FskEffectRecord step;
if (c > 0)
x = y = (float)xsToNumber(xsArg(0));
if (c > 1)
y = (float)xsToNumber(xsArg(1));
FskEffectGaussianBlurSet(&step, x, y);
KprEffectAdd(self, &step);
}
void KPR_effect_gray(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
FskColorRGBARecord dark = { 0, 0, 0, 255 };
FskColorRGBARecord lite = { 255, 255, 255, 255 };
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &dark);
if ((c > 1 && xsTest(xsArg(1))))
KprParseColor(the, xsToString(xsArg(1)), &lite);
FskEffectMonochromeSet(&step, &dark, &lite);
KprEffectAdd(self, &step);
}
void KPR_effect_innerGlow(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 1;
FskColorRGBARecord color = { 255, 255, 255, 255 };
float opacity = 1;
SInt32 radius = 1;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
radius = xsToInteger(xsArg(3));
FskEffectInnerGlowSet(&step, radius, blur, &color);
KprEffectAdd(self, &step);
}
void KPR_effect_innerHilite(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 2;
FskColorRGBARecord color = { 255, 255, 255, 255 };
float opacity = 1;
SInt32 x = 2, y = 2;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
x = xsToInteger(xsArg(3));
if (c > 4)
y = xsToInteger(xsArg(4));
FskEffectInnerShadowScalarSet(&step, x, y, blur, color.r, color.g, color.b, color.a);
KprEffectAdd(self, &step);
}
void KPR_effect_innerShadow(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 2;
FskColorRGBARecord color = { 0, 0, 0, 255 };
float opacity = 1;
SInt32 x = 2, y = 2;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
x = xsToInteger(xsArg(3));
if (c > 4)
y = xsToInteger(xsArg(4));
FskEffectInnerShadowScalarSet(&step, x, y, blur, color.r, color.g, color.b, color.a);
KprEffectAdd(self, &step);
}
void KPR_effect_mask(xsMachine *the)
{
KprEffect self = kprGetHostData(xsThis, this, effect);
KprTexture texture = kprGetHostData(xsArg(0), texture, texture);
Boolean owned = false;
FskBitmap bitmap = KprTextureGetBitmap(texture, NULL, &owned);
FskEffectRecord step;
FskEffectMaskSet(&step, bitmap, NULL);
KprEffectAdd(self, &step);
}
void KPR_effect_outerGlow(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 1;
FskColorRGBARecord color = { 255, 255, 255, 255 };
float opacity = 1;
SInt32 radius = 1;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
radius = xsToInteger(xsArg(3));
FskEffectOuterGlowSet(&step, radius, blur, &color);
KprEffectAdd(self, &step);
}
void KPR_effect_outerHilite(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 2;
FskColorRGBARecord color = { 255, 255, 255, 255 };
float opacity = 1;
SInt32 x = 2, y = 2;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
x = xsToInteger(xsArg(3));
if (c > 4)
y = xsToInteger(xsArg(4));
FskEffectOuterShadowScalarSet(&step, x, y, blur, color.r, color.g, color.b, color.a);
KprEffectAdd(self, &step);
}
void KPR_effect_outerShadow(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
float blur = 2;
FskColorRGBARecord color = { 0, 0, 0, 255 };
float opacity = 1;
SInt32 x = 2, y = 2;
FskEffectRecord step;
if ((c > 0) && xsTest(xsArg(0)))
KprParseColor(the, xsToString(xsArg(0)), &color);
if (c > 1) {
opacity = (float)xsToNumber(xsArg(1));
APPLY_OPACITY_TO_ALPHA(opacity, color.a);
}
if (c > 2)
blur = (float)xsToNumber(xsArg(2));
if (c > 3)
x = xsToInteger(xsArg(3));
if (c > 4)
y = xsToInteger(xsArg(4));
FskEffectOuterShadowScalarSet(&step, x, y, blur, color.r, color.g, color.b, color.a);
KprEffectAdd(self, &step);
}
void KPR_effect_shade(xsMachine *the)
{
xsIntegerValue c = xsToInteger(xsArgc);
KprEffect self = kprGetHostData(xsThis, this, effect);
KprTexture texture = kprGetHostData(xsArg(0), texture, texture);
Boolean owned = false;
FskBitmap bitmap = KprTextureGetBitmap(texture, NULL, &owned);
float opacity = 0.5;
FskEffectRecord step;
if (c > 1)
opacity = (float)xsToNumber(xsArg(1));
FskEffectShadeSet(&step, bitmap, NULL, (UInt8)(opacity * 255));
KprEffectAdd(self, &step);
}
#endif /* KPR_NO_GRAMMAR */