001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015 */
016
017package com.github.sarxos.webcam.util.jh;
018
019import java.awt.Rectangle;
020import java.awt.RenderingHints;
021import java.awt.geom.Point2D;
022import java.awt.geom.Rectangle2D;
023import java.awt.image.BufferedImage;
024import java.awt.image.BufferedImageOp;
025import java.awt.image.ColorModel;
026
027
028/**
029 * A convenience class which implements those methods of BufferedImageOp which
030 * are rarely changed.
031 */
032public abstract class JHFilter implements BufferedImageOp, Cloneable {
033
034        @Override
035        public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
036                if (dstCM == null)
037                        dstCM = src.getColorModel();
038                return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
039        }
040
041        @Override
042        public Rectangle2D getBounds2D(BufferedImage src) {
043                return new Rectangle(0, 0, src.getWidth(), src.getHeight());
044        }
045
046        @Override
047        public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
048                if (dstPt == null)
049                        dstPt = new Point2D.Double();
050                dstPt.setLocation(srcPt.getX(), srcPt.getY());
051                return dstPt;
052        }
053
054        @Override
055        public RenderingHints getRenderingHints() {
056                return null;
057        }
058
059        /**
060         * A convenience method for getting ARGB pixels from an image. This tries to
061         * avoid the performance penalty of BufferedImage.getRGB unmanaging the
062         * image.
063         * 
064         * @param image a BufferedImage object
065         * @param x the left edge of the pixel block
066         * @param y the right edge of the pixel block
067         * @param width the width of the pixel arry
068         * @param height the height of the pixel arry
069         * @param pixels the array to hold the returned pixels. May be null.
070         * @return the pixels
071         * @see #setRGB
072         */
073        public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
074                int type = image.getType();
075                if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
076                        return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels);
077                return image.getRGB(x, y, width, height, pixels, 0, width);
078        }
079
080        /**
081         * A convenience method for setting ARGB pixels in an image. This tries to
082         * avoid the performance penalty of BufferedImage.setRGB unmanaging the
083         * image.
084         * 
085         * @param image a BufferedImage object
086         * @param x the left edge of the pixel block
087         * @param y the right edge of the pixel block
088         * @param width the width of the pixel arry
089         * @param height the height of the pixel arry
090         * @param pixels the array of pixels to set
091         * @see #getRGB
092         */
093        public void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
094                int type = image.getType();
095                if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
096                        image.getRaster().setDataElements(x, y, width, height, pixels);
097                else
098                        image.setRGB(x, y, width, height, pixels, 0, width);
099        }
100
101        @Override
102        public Object clone() {
103                try {
104                        return super.clone();
105                } catch (CloneNotSupportedException e) {
106                        return null;
107                }
108        }
109}