📜 ⬆️ ⬇️

Use PixelBender filter for two images in Flex

Still dre

This is the simple effect when using pixel bender.

In fact, I first saw this effect in Andre Michelle , but he did not post the sors. In those days I wrote it on the eighth player (AS2), but with the release of new features I decided to repeat the experiment. It was not without incident, but the code turned out without a rake, which had to be built in the eight.
')
Watch here . (5 mb)


We write the filter


Variables:
src - first input image
src2 - second input image
white - white color
black - black color
min - the value by which we determine to put white or black color in the output image

The logic of the filter is:
- (a1) we take the color of the first pixel of the first picture
- (a2) we take the color of the first pixel of the second picture
- (ss) subtract from a1 a2
- if the difference of each color ss is greater than min, then in the outgoing picture, at the place of the first pixel there will be white color otherwise black
<languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  1. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  2. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  3. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  4. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  5. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  6. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  7. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  8. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  9. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  10. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  11. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  12. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  13. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  14. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  15. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  16. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  17. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  18. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  19. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  20. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  21. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  22. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
  23. <languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .
<languageVersion : 1.0;> kernel NewFilter < namespace : "Your Namespace" ; vendor : "Your Vendor" ; version : 1; description : "your description" ; > { input image4 src; input image4 src2; output pixel4 dst; const pixel4 white = float4(1.0, 1.0, 1.0, 1.0); const pixel4 black = float4(0.0, 0.0, 0.0, 0.0); const float min = 0.05; void evaluatePixel() { pixel4 a1 = sampleNearest(src, outCoord()); pixel4 a2 = sampleNearest(src2, outCoord()); pixel4 ss = a1 - a2; dst = (ss.r>min && ss.g>min && ss.b>min) ? white : black; } } * This source code was highlighted with Source Code Highlighter .


We write the program


Reading the code should not cause difficulties, just explain some things.

bmp1 - thumbnail image of the video of the newest frame
bmp2 - imprint bmp1, that is, the previous frame from the newest
bmp3 - resulting image after applying the PixelBender filter
cmf - a filter that subtracts alpha from 10 pixels from all pixels
out - the picture that we see, bmp3 is superimposed on it every frame, as well as filter cmf and blur
  1. package {
  2. import flash.display.Bitmap;
  3. import flash.display.BitmapData;
  4. import flash.display.Shader;
  5. import flash.display.ShaderJob;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.filters.BlurFilter;
  9. import flash.filters.ColorMatrixFilter;
  10. import flash.geom.Point;
  11. import flash.media.Video;
  12. import flash.net.NetConnection;
  13. import flash.net.NetStream;
  14. import flash.utils.ByteArray;
  15. [SWF (width = 376, height = 160, backgroundColor = 0x0)]
  16. public class SubstractTv extends Sprite {
  17. [Embed (source = "../assets/kutu.pbj" , mimeType = "application / octet-stream" )]
  18. private var pbj: Class;
  19. private var wid: uint = stage.stageWidth;
  20. private var hei: uint = stage.stageHeight;
  21. private var video: Video;
  22. private var nc: NetConnection;
  23. private var ns: NetStream;
  24. private var bmp1: BitmapData;
  25. private var bmp2: BitmapData;
  26. private var bmp3: BitmapData;
  27. private var out : BitmapData;
  28. private var bmp: Bitmap;
  29. private var shader: Shader;
  30. private var shaderJob: ShaderJob;
  31. private var blur: BlurFilter;
  32. private var cmf: ColorMatrixFilter;
  33. public function SubstractTv () {
  34. nc = new NetConnection ();
  35. nc.connect ( null );
  36. ns = new NetStream (nc);
  37. ns.client = new Object ();
  38. ns.play ( "video.f4v" );
  39. video = new Video (wid, hei);
  40. video.attachNetStream (ns);
  41. bmp1 = new BitmapData (wid, hei, true );
  42. bmp2 = bmp1.clone ();
  43. bmp3 = bmp1.clone ();
  44. out = bmp1.clone ();
  45. bmp = new Bitmap ( out , "auto" , true );
  46. addChild (bmp);
  47. shader = new Shader ( new pbj () as ByteArray);
  48. shader.data.src.input = bmp1;
  49. shader.data.src2.input = bmp2;
  50. createShaderJob ();
  51. addEventListener (Event.ENTER_FRAME, onEnterFrame);
  52. blur = new BlurFilter (3, 3, 3);
  53. cmf = new ColorMatrixFilter ([1, 0, 0, 0, 0,
  54. 0, 1, 0, 0, 0,
  55. 0, 0, 1, 0, 0,
  56. 0, 0, 0, 1, -10]);
  57. }
  58. private function onEnterFrame (e: Event): void {
  59. if (shaderJob.progress! = 1) return ;
  60. bmp2.draw (bmp1);
  61. try {
  62. bmp1.draw (video);
  63. } catch (e: Error) {}
  64. createShaderJob ();
  65. out .applyFilter ( out , out .rect, new Point (), blur);
  66. out .applyFilter ( out , out .rect, new Point (), cmf);
  67. out .draw (bmp3);
  68. }
  69. private function createShaderJob (): void {
  70. shaderJob = new ShaderJob (shader, bmp3, wid, hei);
  71. shaderJob.start ();
  72. }
  73. }
  74. }
* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/46647/


All Articles