Exemplo n.º 1
0
        /// <summary>
        /// Performs a convolution on BufferedImages.  Each component of the
        /// source image will be convolved (including the alpha component, if
        /// present).
        /// If the color model in the source image is not the same as that
        /// in the destination image, the pixels will be converted
        /// in the destination.  If the destination image is null,
        /// a BufferedImage will be created with the source ColorModel.
        /// The IllegalArgumentException may be thrown if the source is the
        /// same as the destination. </summary>
        /// <param name="src"> the source <code>BufferedImage</code> to filter </param>
        /// <param name="dst"> the destination <code>BufferedImage</code> for the
        ///        filtered <code>src</code> </param>
        /// <returns> the filtered <code>BufferedImage</code> </returns>
        /// <exception cref="NullPointerException"> if <code>src</code> is <code>null</code> </exception>
        /// <exception cref="IllegalArgumentException"> if <code>src</code> equals
        ///         <code>dst</code> </exception>
        /// <exception cref="ImagingOpException"> if <code>src</code> cannot be filtered </exception>
        public BufferedImage Filter(BufferedImage src, BufferedImage dst)
        {
            if (src == null)
            {
                throw new NullPointerException("src image is null");
            }
            if (src == dst)
            {
                throw new IllegalArgumentException("src image cannot be the " + "same as the dst image");
            }

            bool          needToConvert = false;
            ColorModel    srcCM         = src.ColorModel;
            ColorModel    dstCM;
            BufferedImage origDst = dst;

            // Can't convolve an IndexColorModel.  Need to expand it
            if (srcCM is IndexColorModel)
            {
                IndexColorModel icm = (IndexColorModel)srcCM;
                src   = icm.ConvertToIntDiscrete(src.Raster, false);
                srcCM = src.ColorModel;
            }

            if (dst == null)
            {
                dst     = CreateCompatibleDestImage(src, null);
                dstCM   = srcCM;
                origDst = dst;
            }
            else
            {
                dstCM = dst.ColorModel;
                if (srcCM.ColorSpace.Type != dstCM.ColorSpace.Type)
                {
                    needToConvert = true;
                    dst           = CreateCompatibleDestImage(src, null);
                    dstCM         = dst.ColorModel;
                }
                else if (dstCM is IndexColorModel)
                {
                    dst   = CreateCompatibleDestImage(src, null);
                    dstCM = dst.ColorModel;
                }
            }

            if (ImagingLib.filter(this, src, dst) == null)
            {
                throw new ImagingOpException("Unable to convolve src image");
            }

            if (needToConvert)
            {
                ColorConvertOp ccop = new ColorConvertOp(Hints);
                ccop.Filter(dst, origDst);
            }
            else if (origDst != dst)
            {
                java.awt.Graphics2D g = origDst.CreateGraphics();
                try
                {
                    g.DrawImage(dst, 0, 0, null);
                }
                finally
                {
                    g.Dispose();
                }
            }

            return(origDst);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Transforms the source <CODE>BufferedImage</CODE> and stores the results
        /// in the destination <CODE>BufferedImage</CODE>.
        /// If the color models for the two images do not match, a color
        /// conversion into the destination color model is performed.
        /// If the destination image is null,
        /// a <CODE>BufferedImage</CODE> is created with the source
        /// <CODE>ColorModel</CODE>.
        /// <para>
        /// The coordinates of the rectangle returned by
        /// <code>getBounds2D(BufferedImage)</code>
        /// are not necessarily the same as the coordinates of the
        /// <code>BufferedImage</code> returned by this method.  If the
        /// upper-left corner coordinates of the rectangle are
        /// negative then this part of the rectangle is not drawn.  If the
        /// upper-left corner coordinates of the  rectangle are positive
        /// then the filtered image is drawn at that position in the
        /// destination <code>BufferedImage</code>.
        /// </para>
        /// <para>
        /// An <CODE>IllegalArgumentException</CODE> is thrown if the source is
        /// the same as the destination.
        ///
        /// </para>
        /// </summary>
        /// <param name="src"> The <CODE>BufferedImage</CODE> to transform. </param>
        /// <param name="dst"> The <CODE>BufferedImage</CODE> in which to store the results
        /// of the transformation.
        /// </param>
        /// <returns> The filtered <CODE>BufferedImage</CODE>. </returns>
        /// <exception cref="IllegalArgumentException"> if <code>src</code> and
        ///         <code>dst</code> are the same </exception>
        /// <exception cref="ImagingOpException"> if the image cannot be transformed
        ///         because of a data-processing error that might be
        ///         caused by an invalid image format, tile format, or
        ///         image-processing operation, or any other unsupported
        ///         operation. </exception>
        public BufferedImage Filter(BufferedImage src, BufferedImage dst)
        {
            if (src == null)
            {
                throw new NullPointerException("src image is null");
            }
            if (src == dst)
            {
                throw new IllegalArgumentException("src image cannot be the " + "same as the dst image");
            }

            bool          needToConvert = false;
            ColorModel    srcCM         = src.ColorModel;
            ColorModel    dstCM;
            BufferedImage origDst = dst;

            if (dst == null)
            {
                dst     = CreateCompatibleDestImage(src, null);
                dstCM   = srcCM;
                origDst = dst;
            }
            else
            {
                dstCM = dst.ColorModel;
                if (srcCM.ColorSpace.Type != dstCM.ColorSpace.Type)
                {
                    int  type      = Xform.Type;
                    bool needTrans = ((type & (Xform.TYPE_MASK_ROTATION | Xform.TYPE_GENERAL_TRANSFORM)) != 0);
                    if (!needTrans && type != Xform.TYPE_TRANSLATION && type != Xform.TYPE_IDENTITY)
                    {
                        double[] mtx = new double[4];
                        Xform.GetMatrix(mtx);
                        // Check out the matrix.  A non-integral scale will force ARGB
                        // since the edge conditions can't be guaranteed.
                        needTrans = (mtx[0] != (int)mtx[0] || mtx[3] != (int)mtx[3]);
                    }

                    if (needTrans && srcCM.Transparency == java.awt.Transparency_Fields.OPAQUE)
                    {
                        // Need to convert first
                        ColorConvertOp ccop   = new ColorConvertOp(Hints);
                        BufferedImage  tmpSrc = null;
                        int            sw     = src.Width;
                        int            sh     = src.Height;
                        if (dstCM.Transparency == java.awt.Transparency_Fields.OPAQUE)
                        {
                            tmpSrc = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB);
                        }
                        else
                        {
                            WritableRaster r = dstCM.CreateCompatibleWritableRaster(sw, sh);
                            tmpSrc = new BufferedImage(dstCM, r, dstCM.AlphaPremultiplied, null);
                        }
                        src = ccop.Filter(src, tmpSrc);
                    }
                    else
                    {
                        needToConvert = true;
                        dst           = CreateCompatibleDestImage(src, null);
                    }
                }
            }

            if (InterpolationType_Renamed != TYPE_NEAREST_NEIGHBOR && dst.ColorModel is IndexColorModel)
            {
                dst = new BufferedImage(dst.Width, dst.Height, BufferedImage.TYPE_INT_ARGB);
            }
            if (ImagingLib.filter(this, src, dst) == null)
            {
                throw new ImagingOpException("Unable to transform src image");
            }

            if (needToConvert)
            {
                ColorConvertOp ccop = new ColorConvertOp(Hints);
                ccop.Filter(dst, origDst);
            }
            else if (origDst != dst)
            {
                java.awt.Graphics2D g = origDst.CreateGraphics();
                try
                {
                    g.Composite = AlphaComposite.Src;
                    g.DrawImage(dst, 0, 0, null);
                }
                finally
                {
                    g.Dispose();
                }
            }

            return(origDst);
        }