diff options
Diffstat (limited to 'webcam/com/neave/webcam/effects/pixel/RGBEffect.as')
| -rwxr-xr-x | webcam/com/neave/webcam/effects/pixel/RGBEffect.as | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/webcam/com/neave/webcam/effects/pixel/RGBEffect.as b/webcam/com/neave/webcam/effects/pixel/RGBEffect.as new file mode 100755 index 0000000..4d2b2ff --- /dev/null +++ b/webcam/com/neave/webcam/effects/pixel/RGBEffect.as @@ -0,0 +1,126 @@ +/**
+ * Neave Webcam // Red Green Blue Effect
+ *
+ * Copyright (C) 2008 Paul Neave
+ * http://www.neave.com/
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation at http://www.gnu.org/licenses/gpl.html
+ */
+
+package com.neave.webcam.effects.pixel
+{
+ import flash.display.*;
+ import flash.geom.*;
+ import com.neave.webcam.effects.*;
+
+ public class RGBEffect extends AbstractEffect
+ {
+ private const COLORS:Array = [0xFF0000, 0x00FF00, 0x0000FF];
+
+ private var pixelsWide:int;
+ private var smallBitmap:BitmapData;
+ private var smallMatrix:Matrix;
+ private var pixels:Sprite;
+ private var pixelsX:int;
+ private var pixelsY:int;
+ private var size:Number;
+
+ /**
+ * Creates a low-resolution effect where each pixel is split into its red, green and blue components
+ *
+ * @param source The source object to use for the effect
+ * @param targetBitmap The target bitmap data to draw the resulting effect into
+ * @param pixelsWide The number of pixels to use in width
+ */
+ public function RGBEffect(source:IBitmapDrawable, targetBitmap:BitmapData, pixelsWide:int = 40)
+ {
+ super(source, targetBitmap, "RGB");
+ this.pixelsWide = pixelsWide < 3 ? 3 : pixelsWide;
+
+ createRGB();
+ }
+
+ /**
+ * Sets up the RGB effect
+ */
+ private function createRGB():void
+ {
+ // Create a smaller, lower resolution bitmap to sample pixels from
+ smallBitmap = new BitmapData(pixelsWide, Math.round(pixelsWide * rect.height / rect.width), false, 0xFF000000);
+ pixelsX = smallBitmap.width;
+ pixelsY = smallBitmap.height;
+ smallMatrix = new Matrix();
+ smallMatrix.scale(pixelsX / rect.width, pixelsY / rect.height);
+ size = rect.width / pixelsX;
+
+ // Create a sprite containing all the RGB pixels needed
+ pixels = new Sprite();
+ for (var y:int = pixelsY; y--; )
+ {
+ for (var x:int = pixelsX; x--; )
+ {
+ // Position each RGB pixel and set its colour to either red, green or blue
+ pixels.addChild(new RGBPixel((x + 0.5) * size, (y + 0.5) * size, COLORS[x % 3], size));
+ }
+ }
+ }
+
+ /**
+ * Draws the RGB effect
+ */
+ override public function draw():void
+ {
+ // Generate a lower resolution bitmap data to sample pixels from
+ smallBitmap.draw(source, smallMatrix, color);
+
+ // Create the RGB pixels from each pixel
+ var n:int = 0;
+ for (var y:int = pixelsY; y--; )
+ {
+ for (var x:int = pixelsX; x--; )
+ {
+ // Set the brightness of this RGB pixel
+ var c:uint = smallBitmap.getPixel(x, y);
+ var p:RGBPixel = pixels.getChildAt(n++) as RGBPixel;
+ switch (x % 3)
+ {
+ case 0: // Red
+ p.alpha = ((c >> 16) & 0xFF) / 0xFF;
+ break;
+
+ case 1: // Green
+ p.alpha = ((c >> 8) & 0xFF) / 0xFF;
+ break;
+
+ case 2: // Blue
+ p.alpha = (c & 0xFF) / 0xFF;
+ break;
+ }
+ }
+ }
+
+ // Draw the new RGB pixels
+ targetBitmap.lock();
+ targetBitmap.fillRect(rect, 0xFF000000);
+ targetBitmap.draw(pixels);
+ targetBitmap.unlock();
+ }
+
+ /**
+ * Removes the RGB effect and all other referenced objects
+ */
+ override public function destroy():void
+ {
+ super.destroy();
+
+ // Remove all the pixels
+ for (var i:int = pixelsX * pixelsY; i--; ) pixels.removeChildAt(i);
+ pixels = null;
+
+ smallBitmap.dispose();
+ smallBitmap = null;
+ }
+ }
+}
\ No newline at end of file |
