{"id":7830,"date":"2015-10-30T08:10:38","date_gmt":"2015-10-30T08:10:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/30\/how-to-implement-gpuimagemaskfilter-using-gpuimage-open-source-projects-bradlarson-gpuimage\/"},"modified":"2015-10-30T08:10:38","modified_gmt":"2015-10-30T08:10:38","slug":"how-to-implement-gpuimagemaskfilter-using-gpuimage-open-source-projects-bradlarson-gpuimage","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/30\/how-to-implement-gpuimagemaskfilter-using-gpuimage-open-source-projects-bradlarson-gpuimage\/","title":{"rendered":"How to Implement GPUImageMaskFilter using GPUImage-open source projects BradLarson\/GPUImage"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1bd01d2d8137d2676bc515f361772a4d?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>lazi74<\/strong><\/p>\n<p>The mask is the <strong>second<\/strong> target, as can been seen in the filter shader code (textureColor2).<\/p>\n<pre><code>\/\/Averages mask's the RGB values, and scales that value by the mask's alpha\n\/\/\n\/\/The dot product should take fewer cycles than doing an average normally\n\/\/\n\/\/Typical\/ideal case, R,G, and B will be the same, and Alpha will be 1.0\n\n lowp float newAlpha = dot(textureColor2.rgb, vec3(.33333334, .33333334, .33333334)) * textureColor2.a;\n\n gl_FragColor = vec4(textureColor.xyz, newAlpha);\n<\/code><\/pre>\n<p>Then you need to &#8220;invert&#8221; your mask : white heart on black background, as the filter uses the &#8220;weight&#8221; of the RGB pixel value to set the alpha value on the target image.<\/p>\n<p>So your code should be<\/p>\n<pre><code>\/\/ Image first, Mask next\n[FullGpuImage addTarget:maskingFilter];\n[FullGpuImage processImage];\n\n[maskingFilter useNextFrameForImageCapture];\n\n[maskGpuImage addTarget:maskingFilter];\n[maskGpuImage processImage];    \n<\/code><\/pre>\n<p>and your mask (ok I did an ugly quick test, use a proper image) like<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/d5h63.png\" \/><\/p>\n<p>for the expected result.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/oCLXa.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>lazi74 The mask is the second target, as can been seen in the filter shader code (textureColor2). \/\/Averages mask&#8217;s the RGB values, and scales that value by the mask&#8217;s alpha \/\/ \/\/The dot product should take fewer cycles than doing an average normally \/\/ \/\/Typical\/ideal case, R,G, and B will be the same, and Alpha [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7830","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7830"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7830\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}