KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > exampleplugins > ColorToGrayScale


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.media.exampleplugins;
8
9 import javax.media.Buffer;
10
11 import org.jboss.media.engine.MediaPlugin;
12
13 /**
14  * @version <tt>$Revision: 1.3 $</tt>
15  * @author <a HREF="mailto:spyridon_samothrakis@yahoo.com">Spyridon Samothrakis</a>
16  */

17 public class ColorToGrayScale extends MediaPlugin
18 {
19
20    /**
21     * @see org.jboss.media.engine.MediaPlugin#process(javax.media.Buffer)
22     * Convert from color to grayscale
23     */

24    public int process(Object JavaDoc out)
25    {
26       Buffer output = (Buffer)out;
27       byte[] data = (byte[]) output.getData();
28
29       float blue = 0.299f;
30       float green = 0.587f;
31       float red = 0.114f;
32
33       for (int i = 0; i < data.length; i += 3)
34       {
35          //convert from color to black and white
36

37          int grey =
38             java.lang.Math.round(
39                (data[i] * blue + data[i + 1] * green + data[i + 2] * red))
40                & 0xff;
41
42          data[i] = (byte) grey;
43          data[i + 1] = (byte) grey;
44          data[i + 2] = (byte) grey;
45       }
46
47       return 0;
48    }
49
50 }
51
Popular Tags