4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef OVERLAYFRAME_H
23 #define OVERLAYFRAME_H
25 #include "loadbalance.h"
26 #include "overlayframe.inc"
41 #define STD_ALPHA(mx, Sa, Da) (Sa + Da - (Sa * Da) / mx)
42 #define STD_BLEND(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * (mx - Sa)) / mx)
48 // NORMAL [Sa + Da * (1 - Sa), Sc * Sa + Dc * (1 - Sa)])
49 #define ALPHA_NORMAL(mx, Sa, Da) (Sa + (Da * (mx - Sa)) / mx)
50 #define COLOR_NORMAL(mx, Sc, Sa, Dc, Da) ((Sc * Sa + Dc * (mx - Sa)) / mx)
51 #define CHROMA_NORMAL COLOR_NORMAL
53 // ADDITION [(Sa + Da), (Sc + Dc)]
54 #define ALPHA_ADDITION(mx, Sa, Da) (Sa + Da)
55 #define COLOR_ADDITION(mx, Sc, Sa, Dc, Da) (Sc + Dc)
56 #define CHROMA_ADDITION COLOR_ADDITION
58 // SUBTRACT [(Sa - Da), (Sc - Dc)]
59 #define ALPHA_SUBTRACT(mx, Sa, Da) (Sa - Da)
60 #define COLOR_SUBTRACT(mx, Sc, Sa, Dc, Da) (Sc - Dc)
61 #define CHROMA_SUBTRACT COLOR_SUBTRACT
63 // MULTIPLY [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + Sc * Dc]
64 #define ALPHA_MULTIPLY STD_ALPHA
65 #define COLOR_MULTIPLY(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
67 #define CHROMA_MULTIPLY COLOR_MULTIPLY
69 // DIVIDE [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + Sc / Dc]
70 #define ALPHA_DIVIDE STD_ALPHA
71 #define COLOR_DIVIDE(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
72 (Dc > ZERO ? (Sc * mx) / Dc : ZERO))
73 #define CHROMA_DIVIDE COLOR_DIVIDE
75 // REPLACE [Sa, Sc] (fade = 1)
76 #define ALPHA_REPLACE(mx, Sa, Da) Sa
77 #define COLOR_REPLACE(mx, Sc, Sa, Dc, Da) Sc
78 #define CHROMA_REPLACE COLOR_REPLACE
80 // MAX [max(Sa, Da), MAX(Sc, Dc)]
81 #define ALPHA_MAX(mx, Sa, Da) (Sa > Da ? Sa : Da)
82 #define COLOR_MAX(mx, Sc, Sa, Dc, Da) (Sc > Dc ? Sc : Dc)
83 #define CHROMA_MAX(mx, Sc, Sa, Dc, Da) (mabs(Sc) > mabs(Dc) ? Sc : Dc)
85 // MIN [min(Sa, Da), MIN(Sc, Dc)]
86 #define ALPHA_MIN(mx, Sa, Da) (Sa < Da ? Sa : Da)
87 #define COLOR_MIN(mx, Sc, Sa, Dc, Da) (Sc < Dc ? Sc : Dc)
88 #define CHROMA_MIN(mx, Sc, Sa, Dc, Da) (mabs(Sc) < mabs(Dc) ? Sc : Dc)
90 // DARKEN [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + min(Sc*Da, Dc*Sa)]
91 #define ALPHA_DARKEN STD_ALPHA
92 #define COLOR_DARKEN(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
93 mmin(Sc * Da, Dc * Sa) / mx)
94 #define CHROMA_DARKEN(mx, Sc, Sa, Dc, Da) (CHROMA_XOR(mx,Sc,Sa,Dc,Da) + \
95 (mabs(Sc * Da) < mabs(Dc * Sa) ? Sc * Da : Dc * Sa) / mx)
97 // LIGHTEN [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + max(Sc*Da, Dc*Sa)]
98 #define ALPHA_LIGHTEN STD_ALPHA
99 #define COLOR_LIGHTEN(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
100 mmax(Sc * Da, Dc * Sa) / mx)
101 #define CHROMA_LIGHTEN(mx, Sc, Sa, Dc, Da) (CHROMA_XOR(mx,Sc,Sa,Dc,Da) + \
102 (mabs(Sc * Da) > mabs(Dc * Sa) ? Sc * Da : Dc * Sa) / mx)
105 #define ALPHA_DST(mx, Sa, Da) Da
106 #define COLOR_DST(mx, Sc, Sa, Dc, Da) Dc
107 #define CHROMA_DST COLOR_DST
109 // DST_ATOP [Sa, Sc * (1 - Da) + Dc * Sa]
110 #define ALPHA_DST_ATOP(mx, Sa, Da) Sa
111 #define COLOR_DST_ATOP(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * Sa) / mx)
112 #define CHROMA_DST_ATOP COLOR_DST_ATOP
114 // DST_IN [Da * Sa, Dc * Sa]
115 #define ALPHA_DST_IN(mx, Sa, Da) ((Da * Sa) / mx)
116 #define COLOR_DST_IN(mx, Sc, Sa, Dc, Da) ((Dc * Sa) / mx)
117 #define CHROMA_DST_IN COLOR_DST_IN
119 // DST_OUT [Da * (1 - Sa), Dc * (1 - Sa)]
120 #define ALPHA_DST_OUT(mx, Sa, Da) (Da * (mx - Sa) / mx)
121 #define COLOR_DST_OUT(mx, Sc, Sa, Dc, Da) (Dc * (mx - Sa) / mx)
122 #define CHROMA_DST_OUT COLOR_DST_OUT
124 // DST_OVER [Sa + Da - Sa*Da, Sc * (1 - Da) + Dc]
125 #define ALPHA_DST_OVER STD_ALPHA
126 #define COLOR_DST_OVER(mx, Sc, Sa, Dc, Da) (Sc * (mx - Da)/ mx + Dc)
127 #define CHROMA_DST_OVER COLOR_DST_OVER
130 #define ALPHA_SRC(mx, Sa, Da) Sa
131 #define COLOR_SRC(mx, Sc, Sa, Dc, Da) Sc
132 #define CHROMA_SRC COLOR_SRC
134 // SRC_ATOP [Da, Sc * Da + Dc * (1 - Sa)]
135 #define ALPHA_SRC_ATOP(mx, Sa, Da) Da
136 #define COLOR_SRC_ATOP(mx, Sc, Sa, Dc, Da) ((Sc * Da + Dc * (mx - Sa)) / mx)
137 #define CHROMA_SRC_ATOP COLOR_SRC_ATOP
139 // SRC_IN [Sa * Da, Sc * Da]
140 #define ALPHA_SRC_IN(mx, Sa, Da) ((Sa * Da) / mx)
141 #define COLOR_SRC_IN(mx, Sc, Sa, Dc, Da) (Sc * Da / mx)
142 #define CHROMA_SRC_IN COLOR_SRC_IN
144 // SRC_OUT [Sa * (1 - Da), Sc * (1 - Da)]
145 #define ALPHA_SRC_OUT(mx, Sa, Da) (Sa * (mx - Da) / mx)
146 #define COLOR_SRC_OUT(mx, Sc, Sa, Dc, Da) (Sc * (mx - Da) / mx)
147 #define CHROMA_SRC_OUT COLOR_SRC_OUT
149 // SRC_OVER [Sa + Da - Sa*Da, Sc + (1 - Sa) * Dc]
150 #define ALPHA_SRC_OVER STD_ALPHA
151 #define COLOR_SRC_OVER(mx, Sc, Sa, Dc, Da) (Sc + Dc * (mx - Sa) / mx)
152 #define CHROMA_SRC_OVER COLOR_SRC_OVER
154 // AND [Sa * Da, Sc * Dc]
155 #define ALPHA_AND(mx, Sa, Da) ((Sa * Da) / mx)
156 #define COLOR_AND(mx, Sc, Sa, Dc, Da) ((Sc * Dc) / mx)
157 #define CHROMA_AND COLOR_AND
159 // OR [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
160 #define ALPHA_OR(mx, Sa, Da) (Sa + Da - (Sa * Da) / mx)
161 #define COLOR_OR(mx, Sc, Sa, Dc, Da) (Sc + Dc - (Sc * Dc) / mx)
162 #define CHROMA_OR COLOR_OR
164 // XOR [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + Dc * (1 - Sa)]
165 #define ALPHA_XOR(mx, Sa, Da) (Sa + Da - (TWO * Sa * Da / mx))
166 #define COLOR_XOR(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * (mx - Sa)) / mx)
167 #define CHROMA_XOR COLOR_XOR
170 //https://www.w3.org/TR/2004/WD-SVG12-20041027/rendering.html
171 // OVERLAY [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
172 // 2*Dc < Da ? 2*Sc*Dc : Sa*Da - 2*(Da-Dc)*(Sa-Sc) ]
173 #define ALPHA_OVERLAY STD_ALPHA
174 #define COLOR_OVERLAY(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
176 (TWO * Sc * Dc) : (Sa * Da - TWO * (Da - Dc) * (Sa - Sc))) / mx)
177 #define CHROMA_OVERLAY COLOR_OVERLAY
179 // SCREEN [Sa + Da - Sa * Da, Sc + Dc - (Sc * Dc)] (same as OR)
180 #define ALPHA_SCREEN STD_ALPHA
181 #define COLOR_SCREEN(mx, Sc, Sa, Dc, Da) (Sc + Dc - (Sc * Dc) / mx)
182 #define CHROMA_SCREEN COLOR_SCREEN
184 // BURN [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
185 // Sc <= 0 || Sc*Da + Dc*Sa <= Sa*Da ? 0 : (Sc*Da + Dc*Sa - Sa*Da)*Sa/Sc]
186 #define ALPHA_BURN STD_ALPHA
187 #define COLOR_BURN(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
188 ((Sc <= ZERO || Sc * Da + Dc * Sa <= Sa * Da) ? ZERO : \
189 (Sa * ((Sc * Da + Dc * Sa - Sa * Da) / Sc) / mx)))
190 #define CHROMA_BURN COLOR_BURN
192 // DODGE [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
193 // Sa <= Sc || Sc*Da + Dc*Sa >= Sa*Da) ? Sa*Da : Dc*Sa / (1 - Sc/Sa)]
194 #define ALPHA_DODGE STD_ALPHA
195 #define COLOR_DODGE(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
196 ((Sa <= Sc || Sc * Da + Dc * Sa >= Sa * Da) ? (Sa * Da) : \
197 (Sa * ((Dc * Sa) / (Sa - Sc))) / mx))
198 #define CHROMA_DODGE COLOR_DODGE
200 // HARDLIGHT [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
201 // 2*Sc < Sa ? 2*Sc*Dc : Sa*Da - 2*(Da - Dc)*(Sa - Sc)]
202 #define ALPHA_HARDLIGHT STD_ALPHA
203 #define COLOR_HARDLIGHT(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
205 (TWO * Sc * Dc) : (Sa * Da - TWO * (Da - Dc) * (Sa - Sc))) / mx)
206 #define CHROMA_HARDLIGHT COLOR_HARDLIGHT
208 // SOFTLIGHT [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
209 // Da > 0 ? (Dc*Sa + 2*Sc*(Da - Dc))/Da : 0]
210 #define ALPHA_SOFTLIGHT STD_ALPHA
211 #define COLOR_SOFTLIGHT(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
213 (Dc * ((Dc*Sa + TWO * Sc * (Da - Dc)) / Da) / mx) : ZERO))
214 #define CHROMA_SOFTLIGHT COLOR_SOFTLIGHT
216 // DIFFERENCE [Sa + Da - Sa * Da, Sc*(1 - Da) + Dc*(1 - Sa) +
217 // abs(Sc * Da - Dc * Sa)]
218 #define ALPHA_DIFFERENCE STD_ALPHA
219 #define COLOR_DIFFERENCE(mx, Sc, Sa, Dc, Da) (STD_BLEND(mx,Sc,Sa,Dc,Da) + \
220 (mabs(Sc * Da - Dc * Sa) / mx))
221 #define CHROMA_DIFFERENCE COLOR_DIFFERENCE
223 static inline int mabs(int32_t v) { return abs(v); }
224 static inline int mabs(int64_t v) { return llabs(v); }
225 static inline float mabs(float v) { return fabsf(v); }
226 static inline int mmin(int32_t a, int32_t b) { return a < b ? a : b; }
227 static inline int mmin(int64_t a, int64_t b) { return a < b ? a : b; }
228 static inline float mmin(float a, float b) { return a < b ? a : b; }
229 static inline int mmax(int32_t a, int32_t b) { return a > b ? a : b; }
230 static inline int mmax(int64_t a, int64_t b) { return a > b ? a : b; }
231 static inline float mmax(float a, float b) { return a > b ? a : b; }
233 static inline int32_t aclip(int32_t v, int mx) {
234 return v < 0 ? 0 : v > mx ? mx : v;
236 static inline int64_t aclip(int64_t v, int mx) {
237 return v < 0 ? 0 : v > mx ? mx : v;
239 static inline float aclip(float v, float mx) {
240 return v < 0 ? 0 : v > mx ? mx : v;
242 static inline float aclip(float v, int mx) {
243 return v < 0 ? 0 : v > mx ? mx : v;
245 static inline int aclip(int v, float mx) {
246 return v < 0 ? 0 : v > mx ? mx : v;
248 static inline int32_t cclip(int32_t v, int mx) {
249 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
251 static inline int64_t cclip(int64_t v, int mx) {
252 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
254 static inline float cclip(float v, float mx) {
255 return v > (mx/=2) ? mx : v < (mx=(-mx)) ? mx : v;
257 static inline float cclip(float v, int mx) {
258 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
260 static inline int cclip(int v, float mx) {
261 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
264 /* number of data pts per unit x in lookup table */
265 #define TRANSFORM_SPP (4096)
266 /* bits of fraction past TRANSFORM_SPP on kernel index accumulation */
267 #define INDEX_FRACTION (8)
268 #define TRANSFORM_MIN (.5 / TRANSFORM_SPP)
270 #define ZTYP(ty) typedef ty z_##ty __attribute__ ((__unused__))
271 ZTYP(int8_t); ZTYP(uint8_t);
272 ZTYP(int16_t); ZTYP(uint16_t);
273 ZTYP(int32_t); ZTYP(uint32_t);
274 ZTYP(int64_t); ZTYP(uint64_t);
275 ZTYP(float); ZTYP(double);
277 #define ALPHA3_BLEND(FN, typ, inp, out, mx, iofs, oofs, rnd) \
278 typ inp0 = (typ)inp[0], inp1 = (typ)inp[1] - iofs; \
279 typ inp2 = (typ)inp[2] - iofs, inp3 = mx; \
280 typ out0 = (typ)out[0], out1 = (typ)out[1] - oofs; \
281 typ out2 = (typ)out[2] - oofs, out3 = mx; \
282 r = COLOR_##FN(mx, inp0, inp3, out0, out3); \
284 g = CHROMA_##FN(mx, inp1, inp3, out1, out3); \
285 b = CHROMA_##FN(mx, inp2, inp3, out2, out3); \
288 g = COLOR_##FN(mx, inp1, inp3, out1, out3); \
289 b = COLOR_##FN(mx, inp2, inp3, out2, out3); \
292 #define ALPHA4_BLEND(FN, typ, inp, out, mx, iofs, oofs, rnd) \
293 typ inp0 = (typ)inp[0], inp1 = (typ)inp[1] - iofs; \
294 typ inp2 = (typ)inp[2] - iofs, inp3 = inp[3]; \
295 typ out0 = (typ)out[0], out1 = (typ)out[1] - oofs; \
296 typ out2 = (typ)out[2] - oofs, out3 = out[3]; \
297 r = COLOR_##FN(mx, inp0, inp3, out0, out3); \
299 g = CHROMA_##FN(mx, inp1, inp3, out1, out3); \
300 b = CHROMA_##FN(mx, inp2, inp3, out2, out3); \
303 g = COLOR_##FN(mx, inp1, inp3, out1, out3); \
304 b = COLOR_##FN(mx, inp2, inp3, out2, out3); \
306 a = ALPHA_##FN(mx, inp3, out3)
308 #define ALPHA_STORE(out, ofs, mx) \
313 #define ALPHA3_STORE(out, ofs, mx) \
315 g = ofs ? cclip(g, mx) : aclip(g, mx); \
316 b = ofs ? cclip(b, mx) : aclip(b, mx); \
318 r = (r * opcty + out0 * trnsp) / mx; \
319 g = (g * opcty + out1 * trnsp) / mx; \
320 b = (b * opcty + out2 * trnsp) / mx; \
322 ALPHA_STORE(out, ofs, mx)
324 #define ALPHA4_STORE(out, ofs, mx) \
326 g = ofs ? cclip(g, mx) : aclip(g, mx); \
327 b = ofs ? cclip(b, mx) : aclip(b, mx); \
329 r = (r * opcty + out0 * trnsp) / mx; \
330 g = (g * opcty + out1 * trnsp) / mx; \
331 b = (b * opcty + out2 * trnsp) / mx; \
332 a = (a * opcty + out3 * trnsp) / mx; \
334 ALPHA_STORE(out, ofs, mx); \
335 out[3] = aclip(a, mx)
338 #define BLEND_SWITCH(FN) \
340 case TRANSFER_NORMAL: FN(NORMAL); \
341 case TRANSFER_ADDITION: FN(ADDITION); \
342 case TRANSFER_SUBTRACT: FN(SUBTRACT); \
343 case TRANSFER_MULTIPLY: FN(MULTIPLY); \
344 case TRANSFER_DIVIDE: FN(DIVIDE); \
345 case TRANSFER_REPLACE: FN(REPLACE); \
346 case TRANSFER_MAX: FN(MAX); \
347 case TRANSFER_MIN: FN(MIN); \
348 case TRANSFER_DARKEN: FN(DARKEN); \
349 case TRANSFER_LIGHTEN: FN(LIGHTEN); \
350 case TRANSFER_DST: FN(DST); \
351 case TRANSFER_DST_ATOP: FN(DST_ATOP); \
352 case TRANSFER_DST_IN: FN(DST_IN); \
353 case TRANSFER_DST_OUT: FN(DST_OUT); \
354 case TRANSFER_DST_OVER: FN(DST_OVER); \
355 case TRANSFER_SRC: FN(SRC); \
356 case TRANSFER_SRC_ATOP: FN(SRC_ATOP); \
357 case TRANSFER_SRC_IN: FN(SRC_IN); \
358 case TRANSFER_SRC_OUT: FN(SRC_OUT); \
359 case TRANSFER_SRC_OVER: FN(SRC_OVER); \
360 case TRANSFER_AND: FN(AND); \
361 case TRANSFER_OR: FN(OR); \
362 case TRANSFER_XOR: FN(XOR); \
363 case TRANSFER_OVERLAY: FN(OVERLAY); \
364 case TRANSFER_SCREEN: FN(SCREEN); \
365 case TRANSFER_BURN: FN(BURN); \
366 case TRANSFER_DODGE: FN(DODGE); \
367 case TRANSFER_HARDLIGHT: FN(HARDLIGHT); \
368 case TRANSFER_SOFTLIGHT: FN(SOFTLIGHT); \
369 case TRANSFER_DIFFERENCE: FN(DIFFERENCE); \
375 OverlayKernel(int interpolation_type);
386 class DirectPackage : public LoadPackage
391 int out_row1, out_row2;
396 class NNPackage : public LoadPackage
401 int out_row1, out_row2;
406 class SamplePackage : public LoadPackage
411 int out_col1, out_col2;
415 class DirectUnit : public LoadClient
418 DirectUnit(DirectEngine *server);
421 void process_package(LoadPackage *package);
422 DirectEngine *engine;
425 class NNUnit : public LoadClient
428 NNUnit(NNEngine *server);
431 void process_package(LoadPackage *package);
436 class SampleUnit : public LoadClient
439 SampleUnit(SampleEngine *server);
442 void process_package(LoadPackage *package);
444 SampleEngine *engine;
448 class DirectEngine : public LoadServer
451 DirectEngine(int cpus);
454 void init_packages();
455 LoadClient* new_client();
456 LoadPackage* new_package();
470 class NNEngine : public LoadServer
476 void init_packages();
477 LoadClient* new_client();
478 LoadPackage* new_package();
499 class SampleEngine : public LoadServer
502 SampleEngine(int cpus);
505 void init_packages();
506 LoadClient* new_client();
507 LoadPackage* new_package();
511 OverlayKernel *kernel;
535 OverlayFrame(int cpus = 1);
536 virtual ~OverlayFrame();
538 int overlay(VFrame *output,
550 int interpolation_type);
552 DirectEngine *direct_engine;
554 SampleEngine *sample_engine;
558 OverlayKernel *kernel[4];