图片滤镜demo

近一直在研究图片滤镜。呵呵,也是工作需要啊——搜狗输入法的大头贴四期将加入图片滤镜功能。死了很多脑细胞,不过收获还是蛮多。

  1 None.gif package laan.smart.bitmap
  2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  3InBlock.gif    import flash.display.BitmapData;
  4InBlock.gif    import flash.display.BitmapDataChannel;
  5InBlock.gif    import flash.filters.ColorMatrixFilter;
  6InBlock.gif    import flash.filters.ConvolutionFilter;
  7InBlock.gif    import flash.filters.DisplacementMapFilter;
  8InBlock.gif    import flash.filters.DisplacementMapFilterMode;
  9InBlock.gif    import flash.geom.Point;
 10InBlock.gif    import flash.geom.Rectangle;
 11InBlock.gif    
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**
 13InBlock.gif     * 滤镜生成器
 14InBlock.gif     * 
 15InBlock.gif     * <p>
 16InBlock.gif     * 用于生成黑白照、水彩、模糊、膨胀、挤压等滤镜。
 17InBlock.gif     * </p>
 18InBlock.gif     * 
 19InBlock.gif     * <p>
 20InBlock.gif     * 部分代码源自rakuto:http://code.google.com/p/as3filters/
 21InBlock.gif     * </p>
 22InBlock.gif     * 
 23InBlock.gif     * @author laan
 24InBlock.gif     * @createTime 2008.11
 25InBlock.gif     * 
 26InBlock.gif     * @see http://www.laaan.cn
 27InBlock.gif     * @see http://code.google.com/p/as3filters/
 28InBlock.gif     * 
 29ExpandedSubBlockEnd.gif     */
    
 30InBlock.gif    public class FilterFactory
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
 33InBlock.gif         * 水彩滤镜
 34InBlock.gif         *  
 35InBlock.gif         * @param size        目标对象范围
 36InBlock.gif         * @param region    滤镜作用范围
 37InBlock.gif         * @param factor    接收一个0-1的Number数据,指定水彩化系数
 38InBlock.gif         * 
 39InBlock.gif         * @return 返回一个<code>DisplacementMapFilter</code>滤镜
 40InBlock.gif         * 
 41ExpandedSubBlockEnd.gif         */
        
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function aquarelleFilter(size:Rectangle, region:Rectangle = null, factor:Number = 0.5):DisplacementMapFilter dot.gif{
 43InBlock.gif            if (!region) region = new Rectangle(00, size.width, size.height);
 44InBlock.gif            
 45InBlock.gif            if (factor > 1) factor = 1;
 46InBlock.gif            if (factor < 0) factor = 0;
 47InBlock.gif            
 48InBlock.gif            
 49InBlock.gif                
 50InBlock.gif            var bd:BitmapData = new BitmapData(size.width, size.height, false0x000000);
 51InBlock.gif            var no:BitmapData = new BitmapData(size.width, size.height);
 52InBlock.gif            no.noise(factor * 100255, BitmapDataChannel.RED);
 53InBlock.gif            
 54InBlock.gif            bd.copyPixels(no, region, new Point(region.x, region.y));
 55InBlock.gif            
 56InBlock.gif            var chanel:uint = BitmapDataChannel.RED;
 57InBlock.gif            var filter:DisplacementMapFilter = new DisplacementMapFilter(bd, new Point(00), chanel, chanel, factor * 5, factor * 5);
 58InBlock.gif            
 59InBlock.gif            return filter;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif        
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
 63InBlock.gif         * 模糊滤镜
 64InBlock.gif         *  
 65InBlock.gif         * @param factor    接收一个0-1的Number数据,指定水彩化系数
 66InBlock.gif         * 
 67InBlock.gif         * @return 返回一个<code>ConvolutionFilter</code>滤镜
 68InBlock.gif         * 
 69ExpandedSubBlockEnd.gif         */
        
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function FuzzyFilter(factor:Number = 0.5):ConvolutionFilter dot.gif{
 71InBlock.gif            if (factor > 1) factor = 1;
 72InBlock.gif            if (factor < 0) factor = 0;
 73InBlock.gif            
 74InBlock.gif            factor *= 10;
 75InBlock.gif            
 76InBlock.gif            var matrix:Array = [];
 77InBlock.gif            var i:uint = 0;
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            while(i++ < factor * factor) dot.gif{
 79InBlock.gif                matrix.push(1);
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif            
 82InBlock.gif            var filter:ConvolutionFilter = new ConvolutionFilter(factor, factor, matrix, factor * factor);
 83InBlock.gif            
 84InBlock.gif            return filter;
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif        
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
 88InBlock.gif         * 灰度滤镜
 89InBlock.gif         *  
 90InBlock.gif         * 
 91InBlock.gif         * @return 返回一个<code>ColorMatrixFilter</code>滤镜
 92InBlock.gif         * 
 93ExpandedSubBlockEnd.gif         */
        
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function grayFilter():ColorMatrixFilter dot.gif{
 95InBlock.gif            var matrix:Array = [01000,
 96InBlock.gif                                01000,
 97InBlock.gif                                01000,
 98InBlock.gif                                00010];
 99InBlock.gif            
100InBlock.gif            return new ColorMatrixFilter(matrix);
101ExpandedSubBlockEnd.gif        }

102InBlock.gif        
103ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
104InBlock.gif         * 浮雕滤镜
105InBlock.gif         *  
106InBlock.gif         * @return 返回一个<code>ConvolutionFilter</code>滤镜
107InBlock.gif         * 
108ExpandedSubBlockEnd.gif         */
        
109ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function reliefFilter():ConvolutionFilter dot.gif{
110InBlock.gif            var matrix:Array = [-2,-1,0,-1,1,1,0,1,2];
111InBlock.gif            
112InBlock.gif            var filter:ConvolutionFilter = new ConvolutionFilter(33, matrix, 1);
113InBlock.gif            return filter;
114ExpandedSubBlockEnd.gif        }

115InBlock.gif        
116ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
117InBlock.gif         * 木雕滤镜
118InBlock.gif         * 
119InBlock.gif         * @param factor 0-1
120InBlock.gif         * 
121InBlock.gif         * @return 返回一组滤镜
122InBlock.gif         * 
123ExpandedSubBlockEnd.gif         */
        
124ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function woodCarvingFilter(factor:Number = 0.5):Array dot.gif{
125InBlock.gif            if (factor > 1) factor = 1;
126InBlock.gif            if (factor < 0) factor = 0;
127InBlock.gif            
128InBlock.gif            factor *= 10;
129InBlock.gif            
130InBlock.gif            var matrix:Array = [0100-127,
131InBlock.gif                                0100-127,
132InBlock.gif                                0100-127,
133InBlock.gif                                00010];
134InBlock.gif            
135InBlock.gif            var matrix2:Array = [0, factor, 000,
136InBlock.gif                                0, factor, 000,
137InBlock.gif                                0, factor, 000,
138InBlock.gif                                00010];
139InBlock.gif            
140InBlock.gif            return [new ColorMatrixFilter(matrix), new ColorMatrixFilter(matrix2)];
141ExpandedSubBlockEnd.gif        }

142InBlock.gif        
143ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
144InBlock.gif         * 扭曲滤镜
145InBlock.gif         *  
146InBlock.gif         * @param size
147InBlock.gif         * @param region
148InBlock.gif         * @param rotation
149InBlock.gif         * 
150InBlock.gif         * @return 
151InBlock.gif         * 
152ExpandedSubBlockEnd.gif         */
        
153ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function twirlFilter(size:Rectangle, region:Rectangle=null, rotation:Number=0):DisplacementMapFilter dot.gif{
154InBlock.gif            if (!region) region = new Rectangle(00, size.width, size.height);
155InBlock.gif            
156InBlock.gif              rotation ||= Math.PI / 2;
157InBlock.gif              
158InBlock.gif              var width:int = size.width;
159InBlock.gif              var height:int = size.height;
160InBlock.gif              
161InBlock.gif              var dbmd:BitmapData = new BitmapData(size.width, size.height, false0x8080);
162InBlock.gif              var radius:Number = Math.min(region.width, region.height) / 2;
163InBlock.gif              var centerX:int = region.x + region.width / 2;
164InBlock.gif              var centerY:int = region.y + region.height / 2;
165InBlock.gif              
166ExpandedSubBlockStart.gifContractedSubBlock.gif              for(var y:int = 0; y < height; ++y) dot.gif{
167InBlock.gif                var ycoord:int = y - centerY;
168ExpandedSubBlockStart.gifContractedSubBlock.gif                for(var x:int = 0; x < width; ++x) dot.gif{
169InBlock.gif                    var xcoord:int = x - centerX;
170InBlock.gif                    var dr:Number = radius - Math.sqrt(xcoord * xcoord + ycoord * ycoord); 
171ExpandedSubBlockStart.gifContractedSubBlock.gif                      if(dr > 0dot.gif{
172InBlock.gif                        var angle:Number = dr / radius * rotation;
173InBlock.gif                        var dx:Number = xcoord * Math.cos(angle) - ycoord * Math.sin(angle) - xcoord;
174InBlock.gif                        var dy:Number = xcoord * Math.sin(angle) + ycoord * Math.cos(angle) - ycoord;
175InBlock.gif                        var blue:int = 0x80 + Math.round(dx / width * 0xff);
176InBlock.gif                        var green:int = 0x80 + Math.round(dy / height * 0xff);
177InBlock.gif                        dbmd.setPixel(x, y, green << 8 | blue);
178ExpandedSubBlockEnd.gif                    }

179ExpandedSubBlockEnd.gif                }
 
180ExpandedSubBlockEnd.gif            }

181InBlock.gif              return new DisplacementMapFilter(dbmd,
182InBlock.gif                                               new Point(00),
183InBlock.gif                                               BitmapDataChannel.BLUE,
184InBlock.gif                                               BitmapDataChannel.GREEN,
185InBlock.gif                                               width,
186InBlock.gif                                               height,
187InBlock.gif                                               DisplacementMapFilterMode.IGNORE);
188ExpandedSubBlockEnd.gif        }

189InBlock.gif        
190ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
191InBlock.gif         * 挤压滤镜
192InBlock.gif         *  
193InBlock.gif         * @param size
194InBlock.gif         * @param region
195InBlock.gif         * @param amount
196InBlock.gif         * @return 
197InBlock.gif         * 
198ExpandedSubBlockEnd.gif         */
        
199ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function squeezeFilter(size:Rectangle, region:Rectangle = null, factor:Number = 0.5):DisplacementMapFilter dot.gif{
200InBlock.gif              var width:int = size.width;
201InBlock.gif              var height:int = size.height;
202InBlock.gif              
203InBlock.gif              region ||= new Rectangle(00, width, height);
204InBlock.gif              
205InBlock.gif            var radius:Number = Math.min(region.width, region.height) / 2;
206InBlock.gif            var centerX:int = region.x + region.width / 2;
207InBlock.gif            var centerY:int = region.y + region.height / 2;
208InBlock.gif            var dbmd:BitmapData = new BitmapData(width, height, false0x8080);
209InBlock.gif            
210ExpandedSubBlockStart.gifContractedSubBlock.gif            for(var y:int = 0; y < height; ++y) dot.gif{
211InBlock.gif                var ycoord:int = y - centerY;
212ExpandedSubBlockStart.gifContractedSubBlock.gif                for(var x:int = 0; x < width; ++x) dot.gif{
213InBlock.gif                    var xcoord:int = x - centerX;
214InBlock.gif                    var d:Number = Math.sqrt(xcoord * xcoord + ycoord * ycoord);
215ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(d < radius) dot.gif{
216InBlock.gif                        var t:Number = d == 0 ? 0 : Math.pow(Math.sin(Math.PI / 2 * d / radius), -factor);
217InBlock.gif                        var dx:Number = xcoord * (t - 1/ width;
218InBlock.gif                        var dy:Number = ycoord * (t - 1/ height;
219InBlock.gif                        var blue:int = 0x80 + dx * 0xff;
220InBlock.gif                        var green:int = 0x80 + dy * 0xff;
221InBlock.gif                        dbmd.setPixel(x, y, green << 8 | blue);
222ExpandedSubBlockEnd.gif                    }

223ExpandedSubBlockEnd.gif                }

224ExpandedSubBlockEnd.gif            }

225InBlock.gif              
226InBlock.gif            return new DisplacementMapFilter(dbmd,
227InBlock.gif                                               new Point(00),
228InBlock.gif                                               BitmapDataChannel.BLUE,
229InBlock.gif                                               BitmapDataChannel.GREEN,
230InBlock.gif                                               width,
231InBlock.gif                                               height,
232InBlock.gif                                               DisplacementMapFilterMode.CLAMP);
233ExpandedSubBlockEnd.gif        }

234InBlock.gif        
235ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
236InBlock.gif         * 膨胀滤镜
237InBlock.gif         *  
238InBlock.gif         * @param size
239InBlock.gif         * @param region
240InBlock.gif         * @param factor
241InBlock.gif         * 
242InBlock.gif         * @return 
243InBlock.gif         * 
244ExpandedSubBlockEnd.gif         */
        
245ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function bulgeFilter(size:Rectangle, region:Rectangle = null, factor:Number = 0.5):DisplacementMapFilter dot.gif{
246InBlock.gif              return squeezeFilter(size, region, Math.min(-factor, -1));
247ExpandedSubBlockEnd.gif        }

248InBlock.gif        
249ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**
250InBlock.gif         * 鱼眼滤镜
251InBlock.gif         *  
252InBlock.gif         * @param size
253InBlock.gif         * @param region
254InBlock.gif         * @param factor
255InBlock.gif         * 
256InBlock.gif         * @return 
257InBlock.gif         * 
258ExpandedSubBlockEnd.gif         */
        
259ExpandedSubBlockStart.gifContractedSubBlock.gif        public static function fisheyeFilter(size:Rectangle, region:Rectangle = null, factor:Number = 0.8):DisplacementMapFilter dot.gif{
260InBlock.gif            var width:int = size.width;
261InBlock.gif            var height:int = size.height;
262InBlock.gif              
263InBlock.gif            region ||= new Rectangle(00, width, height);
264InBlock.gif              
265InBlock.gif            var dbmd:BitmapData = new BitmapData(width, height, false0x8080);
266InBlock.gif            
267InBlock.gif            var centerX:int = region.x + region.width / 2;
268InBlock.gif            var centerY:int = region.y + region.height / 2;
269InBlock.gif            
270InBlock.gif            var radius:Number = Math.sqrt(region.width * region.width + region.height * region.height);
271InBlock.gif          
272ExpandedSubBlockStart.gifContractedSubBlock.gif            for(var y:int = 0; y < height; ++y) dot.gif{
273InBlock.gif                var ycoord:int = y - centerY;
274ExpandedSubBlockStart.gifContractedSubBlock.gif                for(var x:int = 0; x < width; ++x) dot.gif{
275InBlock.gif                    var xcoord:int = x - centerX;
276InBlock.gif                    var d:Number = Math.sqrt(xcoord * xcoord + ycoord * ycoord);
277ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(d < radius) dot.gif{
278InBlock.gif                        var t:Number = d == 0 ? 0 : Math.pow(Math.sin(Math.PI / 2 * d / radius), factor);
279InBlock.gif                        var dx:Number = xcoord * (t - 1/ width;
280InBlock.gif                        var dy:Number = ycoord * (t - 1/ height;
281InBlock.gif                        var blue:int = 0x80 + dx * 0xff;
282InBlock.gif                        var green:int = 0x80 + dy * 0xff;
283InBlock.gif                        dbmd.setPixel(x, y, green << 8 | blue);
284ExpandedSubBlockEnd.gif                     }

285ExpandedSubBlockEnd.gif                }
  
286ExpandedSubBlockEnd.gif            }

287InBlock.gif            
288InBlock.gif              return new DisplacementMapFilter(dbmd,
289InBlock.gif                                            new Point(00),
290InBlock.gif                                            BitmapDataChannel.BLUE,
291InBlock.gif                                            BitmapDataChannel.GREEN,
292InBlock.gif                                            width,
293InBlock.gif                                            height,
294InBlock.gif                                            DisplacementMapFilterMode.CLAMP);
295ExpandedSubBlockEnd.gif        }

296ExpandedSubBlockEnd.gif    }

297ExpandedBlockEnd.gif}

 

转载于:https://www.cnblogs.com/sophie_wang/archive/2009/12/02/1615460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值