All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ojil.algorithm.RgbCrop Maven / Gradle / Ivy

There is a newer version: 0.0.11
Show newest version
/*
 * RgbCrop.java
 *
 * Created on August 27, 2006, 2:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 * Copyright 2007 by Jon A. Webb
 *     This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 *    You should have received a copy of the Lesser GNU General Public License
 *    along with this program.  If not, see .
 *
 */

package com.github.ojil.algorithm;
import com.github.ojil.core.Error;
import com.github.ojil.core.Image;
import com.github.ojil.core.PipelineStage;
import com.github.ojil.core.Rect;
import com.github.ojil.core.RgbImage;

/**
 * Pipeline stage crops a gray image to a given rectangular cropping window.
 * @author webb
 */
public class RgbCrop extends PipelineStage {
    int cHeight; /* height of cropping window */
    int cWidth; /* width of cropping window */
    int cX; /* left edge of cropping window */
    int cY; /* top of cropping window */
    
    /** Creates a new instance of RgbCrop. The cropping window
     * is specified here.
     *
     * @param x left edge of cropping window
     * @param y top edge of cropping window
     * @param width width of cropping window
     * @param height height of cropping window
     * @throws com.github.ojil.core.Error if the top left corner of the
     * window is negative, or the window area is non-positive.
     */
    public RgbCrop(
            int x,
            int y,
            int width,
            int height) throws com.github.ojil.core.Error {
        setWindow(x, y, width, height);
    }
    
    /** Creates a new instance of RgbCrop. The cropping window
     * is specified here.
     *
     * @param r the rectangle to crop to
     * @throws com.github.ojil.core.Error if the top left corner of the
     * window is negative, or the window area is non-positive.
     */
    public RgbCrop(Rect r) throws com.github.ojil.core.Error {
        setWindow(r.getLeft(), r.getTop(), r.getWidth(), r.getHeight());
    }
    
    /** Crops the input RGB image to the cropping window that was
     * specified in the constructor.
     *
     * @param image the input image.
     * @throws com.github.ojil.core.Error if the cropping window
     *    extends outside the input image, or the input image
     *    is not an RgbImage.
     */
    public void push(Image image) throws com.github.ojil.core.Error {
        if (!(image instanceof RgbImage)) {
            throw new Error(
                			Error.PACKAGE.ALGORITHM,
                			ErrorCodes.IMAGE_NOT_RGBIMAGE,
                			image.toString(),
                			null,
                			null);
        }
        RgbImage imageInput = (RgbImage) image;
        if (this.cX + this.cWidth > image.getWidth() ||
            this.cY + this.cHeight > image.getHeight()) {
            throw new Error(
                            Error.PACKAGE.CORE,
                            com.github.ojil.core.ErrorCodes.BOUNDS_OUTSIDE_IMAGE,
                            image.toString(),
                            this.toString(),
                            null);
        }
        RgbImage imageResult = new RgbImage(this.cWidth,this.cHeight);
        Integer[] src = imageInput.getData();
        Integer[] dst = imageResult.getData();
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy