BrightenLookupEffect.java :  » Graphic-Library » apollo » org » apollo » effect » Java Open Source

Java Open Source » Graphic Library » apollo 
apollo » org » apollo » effect » BrightenLookupEffect.java
/*
 * 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.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.LookupOp;
import java.awt.image.ShortLookupTable;
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 brighten effect using lookup table
 * 
 * @author Giovane.Kuhn on 06/04/2005
 */
public final class BrightenLookupEffect extends ApolloMediaCore implements Effect, IVideoEffect {

    /** Effect name */
    private static final String NAME = "Brighten lookup effect";

    private static final List<String> PROPERTY_NAMES;

    /** Operation to brighten image */
    private static final LookupOp BRIGHTEN_OP;

    static {
        // properties
        List<String> list = new ArrayList<String>();
        list.add("times");
        PROPERTY_NAMES = Collections.unmodifiableList(list);

        // register
        PlugInManager.addPlugIn( //
                BrightenLookupEffect.class.getName(), //
                SUPPORTED_FORMATS, //
                SUPPORTED_FORMATS, //
                PlugInManager.EFFECT //
                );
        try {
            PlugInManager.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // bright opertion
        short[] data = new short[256];
        for (int i = 0; i < data.length; i++) {
            data[i] = (short) (Math.sqrt(i / 255.0) * 255.0);
        }
        BRIGHTEN_OP = new LookupOp(new ShortLookupTable(0, data), null);
    }

    /** How many times to apply brigthen operation */
    private Integer times = 1;

    public BrightenLookupEffect() {
        // 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);

        // brightness operation
        for (int i = 0; i < times; i++) {
            bi = BRIGHTEN_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;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.