kernel DifferenceKeyFilter < namespace : "aboutme.be"; vendor : "Wouter Verweirder"; version : 1; description : "Difference Key Blendmode > create mask for the different pixels"; > { input image4 background; input image4 foreground; output pixel4 dst; parameter float tolerance1 < defaultValue: 0.1; >; parameter float tolerance2 < defaultValue: 0.1; >; parameter float tolerance3 < defaultValue: 0.1; >; parameter int mode < minValue: 0; maxValue: 1; >; void evaluatePixel() { pixel4 backgroundPixel = sampleNearest(background, outCoord()); float3 backgroundRGB = backgroundPixel.rgb; pixel4 foregroundPixel = sampleNearest(foreground,outCoord()); float3 foregroundRGB = foregroundPixel.rgb; float3 diff; dst = foregroundPixel; //dp we check HSB or RGB? if(mode == 1) { float maxBackground = max(max(backgroundPixel.r, backgroundPixel.g), backgroundPixel.b); float minBackground = min(min(backgroundPixel.r, backgroundPixel.g), backgroundPixel.b); float deltaBackground = maxBackground - minBackground; float3 backgroundHSB; backgroundHSB[2] = maxBackground; if(deltaBackground == 0.0) backgroundHSB[0] = backgroundHSB[1] = 1.0; else { backgroundHSB[1] = deltaBackground / maxBackground; float3 deltaBackgroundRGB = (((maxBackground - backgroundRGB) / 6.0) + (deltaBackground / 2.0)) / deltaBackground; if(maxBackground == backgroundRGB[0]) backgroundHSB[0] = deltaBackgroundRGB[2] - deltaBackgroundRGB[1]; else if(maxBackground == backgroundRGB[1]) backgroundHSB[0] = (1.0 / 3.0) + deltaBackgroundRGB[0] - deltaBackgroundRGB[2]; else backgroundHSB[0] = (2.0 / 3.0) + deltaBackgroundRGB[1] - deltaBackgroundRGB[0]; } float maxForeground = max(max(foregroundPixel.r, foregroundPixel.g), foregroundPixel.b); float minForeground = min(min(foregroundPixel.r, foregroundPixel.g), foregroundPixel.b); float deltaForeground = maxForeground - minForeground; float3 foregroundHSB; foregroundHSB[2] = maxForeground; if(deltaForeground == 0.0) foregroundHSB[0] = foregroundHSB[1] = 1.0; else { foregroundHSB[1] = deltaForeground / maxForeground; float3 deltaForegroundRGB = (((maxForeground - foregroundRGB) / 6.0) + (deltaForeground / 2.0)) / deltaForeground; if(maxForeground == foregroundRGB[0]) foregroundHSB[0] = deltaForegroundRGB[2] - deltaForegroundRGB[1]; else if(maxForeground == foregroundRGB[1]) foregroundHSB[0] = (1.0 / 3.0) + deltaForegroundRGB[0] - deltaForegroundRGB[2]; else foregroundHSB[0] = (2.0 / 3.0) + deltaForegroundRGB[1] - deltaForegroundRGB[0]; } diff = abs(foregroundHSB - backgroundHSB); } else { diff = abs(foregroundRGB - backgroundRGB); } if(diff[0] < tolerance1 && diff[1] < tolerance2 && diff[2] < tolerance3) { dst.a = 0.0; dst.r = dst.g = dst.b = 1.0; } else { dst.r = dst.g = dst.b = 0.0; } } }