/*
* Apollo - Motion capture and animation system
* Copyright (c) 2005 Apollo
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*
* @author Giovane.Kuhn - brain@netuno.com.br
*
*/
package org.apollo.effect;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.Kernel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.media.Buffer;
import javax.media.Effect;
import javax.media.PlugInManager;
import org.apollo.ApolloMediaCore;
/**
* Applyes blur effect
*
* @author Giovane.Kuhn on 06/04/2005
*/
public final class BlurEffect extends ApolloMediaCore implements Effect, IVideoEffect {
/** Effect name */
private static final String NAME = "Blur effect";
private static final List<String> PROPERTY_NAMES;
private static final float ninth = 1.0f / 9.0f;
/** Operation to blur image */
private static final ConvolveOp BLUR_OP = new ConvolveOp(new Kernel(3, 3, new float[] { ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth }), ConvolveOp.EDGE_NO_OP, null);
static {
// properties
List<String> list = new ArrayList<String>();
list.add("times");
PROPERTY_NAMES = Collections.unmodifiableList(list);
// register
PlugInManager.addPlugIn( //
BlurEffect.class.getName(), //
SUPPORTED_FORMATS, //
SUPPORTED_FORMATS, //
PlugInManager.EFFECT //
);
try {
PlugInManager.commit();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/** How many times to apply blur operation */
private Integer times = 1;
public BlurEffect() {
// nop
}
/* instance methods */
public List<String> getProperties() {
return PROPERTY_NAMES;
}
public Integer getTimes() {
return times;
}
public void setTimes(Integer times) {
this.times = times;
}
public String getName() {
return NAME;
}
public int process(Buffer inputBuffer, Buffer outputBuffer) {
long start = System.currentTimeMillis();
assert inputFormat.matches(inputBuffer.getFormat());
assert outputFormat != null;
// input data
int[] in = (int[]) inputBuffer.getData();
int inLength = inputBuffer.getLength();
int inOffset = inputBuffer.getOffset();
// update output buffer properties
outputBuffer.setFormat(outputFormat);
outputBuffer.setLength(inLength);
outputBuffer.setOffset(inOffset);
if (inLength == 0) {
outputBuffer.setData(in);
return BUFFER_PROCESSED_OK;
}
// create buffer image
DataBufferInt db = new DataBufferInt(in, inLength, 0);
BufferedImage bi = createBufferedImage(db);
// blur operation
for (int i = 0; i < times; i++) {
bi = BLUR_OP.filter(bi, null);
}
// set output buffer data
assert bi.getData().getDataBuffer().getDataType() == DataBuffer.TYPE_INT;
db = (DataBufferInt) bi.getData().getDataBuffer();
outputBuffer.setData(db.getData());
System.out.println("[" + this.getClass().getSimpleName() + "] #" + inputBuffer.getSequenceNumber() + " in " + (System.currentTimeMillis() - start) + "ms.");
return BUFFER_PROCESSED_OK;
}
}
|