示例#1
0
        /// <summary>
        /// Creates a zeroed destination image with the correct size and number of
        /// bands. </summary>
        /// <param name="src">       Source image for the filter operation. </param>
        /// <param name="destCM">    ColorModel of the destination.  If null, the
        ///                  ColorModel of the source will be used. </param>
        /// <returns> the zeroed-destination image. </returns>
        public virtual BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM)
        {
            BufferedImage image;

            if (destCM == null)
            {
                ColorModel cm = src.ColorModel;
                image = new BufferedImage(cm, src.Raster.CreateCompatibleWritableRaster(), cm.AlphaPremultiplied, null);
            }
            else
            {
                int w = src.Width;
                int h = src.Height;
                image = new BufferedImage(destCM, destCM.CreateCompatibleWritableRaster(w, h), destCM.AlphaPremultiplied, null);
            }

            return(image);
        }
示例#2
0
        /// <summary>
        /// Creates a zeroed destination image with the correct size and number
        /// of bands.  If destCM is null, an appropriate ColorModel will be used. </summary>
        /// <param name="src">       Source image for the filter operation. </param>
        /// <param name="destCM">    ColorModel of the destination.  Can be null. </param>
        /// <returns> a destination <code>BufferedImage</code> with the correct
        ///         size and number of bands. </returns>
        public virtual BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM)
        {
            BufferedImage image;

            int w = src.Width;
            int h = src.Height;

            WritableRaster wr = null;

            if (destCM == null)
            {
                destCM = src.ColorModel;
                // Not much support for ICM
                if (destCM is IndexColorModel)
                {
                    destCM = ColorModel.RGBdefault;
                }
                else
                {
                    /* Create destination image as similar to the source
                     *  as it possible...
                     */
                    wr = src.Data.CreateCompatibleWritableRaster(w, h);
                }
            }

            if (wr == null)
            {
                /* This is the case when destination color model
                 * was explicitly specified (and it may be not compatible
                 * with source raster structure) or source is indexed image.
                 * We should use destination color model to create compatible
                 * destination raster here.
                 */
                wr = destCM.CreateCompatibleWritableRaster(w, h);
            }

            image = new BufferedImage(destCM, wr, destCM.AlphaPremultiplied, null);

            return(image);
        }
示例#3
0
        /// <summary>
        /// Creates a zeroed destination image with the correct size and number of
        /// bands.  A <CODE>RasterFormatException</CODE> may be thrown if the
        /// transformed width or height is equal to 0.
        /// <para>
        /// If <CODE>destCM</CODE> is null,
        /// an appropriate <CODE>ColorModel</CODE> is used; this
        /// <CODE>ColorModel</CODE> may have
        /// an alpha channel even if the source <CODE>ColorModel</CODE> is opaque.
        ///
        /// </para>
        /// </summary>
        /// <param name="src">  The <CODE>BufferedImage</CODE> to be transformed. </param>
        /// <param name="destCM">  <CODE>ColorModel</CODE> of the destination.  If null,
        /// an appropriate <CODE>ColorModel</CODE> is used.
        /// </param>
        /// <returns> The zeroed destination image. </returns>
        public virtual BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM)
        {
            BufferedImage image;
            Rectangle     r = GetBounds2D(src).Bounds;

            // If r.x (or r.y) is < 0, then we want to only create an image
            // that is in the positive range.
            // If r.x (or r.y) is > 0, then we need to create an image that
            // includes the translation.
            int w = r.x + r.Width_Renamed;
            int h = r.y + r.Height_Renamed;

            if (w <= 0)
            {
                throw new RasterFormatException("Transformed width (" + w + ") is less than or equal to 0.");
            }
            if (h <= 0)
            {
                throw new RasterFormatException("Transformed height (" + h + ") is less than or equal to 0.");
            }

            if (destCM == null)
            {
                ColorModel cm = src.ColorModel;
                if (InterpolationType_Renamed != TYPE_NEAREST_NEIGHBOR && (cm is IndexColorModel || cm.Transparency == java.awt.Transparency_Fields.OPAQUE))
                {
                    image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                }
                else
                {
                    image = new BufferedImage(cm, src.Raster.CreateCompatibleWritableRaster(w, h), cm.AlphaPremultiplied, null);
                }
            }
            else
            {
                image = new BufferedImage(destCM, destCM.CreateCompatibleWritableRaster(w, h), destCM.AlphaPremultiplied, null);
            }

            return(image);
        }
示例#4
0
        /// <summary>
        /// Creates a zeroed destination image with the correct size and number of
        /// bands.  If destCM is <code>null</code>, an appropriate
        /// <code>ColorModel</code> will be used. </summary>
        /// <param name="src">       Source image for the filter operation. </param>
        /// <param name="destCM">    the destination's <code>ColorModel</code>, which
        ///                  can be <code>null</code>. </param>
        /// <returns> a filtered destination <code>BufferedImage</code>. </returns>
        public virtual BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM)
        {
            BufferedImage image;
            int           w            = src.Width;
            int           h            = src.Height;
            int           transferType = DataBuffer.TYPE_BYTE;

            if (destCM == null)
            {
                ColorModel cm     = src.ColorModel;
                Raster     raster = src.Raster;
                if (cm is ComponentColorModel)
                {
                    DataBuffer db       = raster.DataBuffer;
                    bool       hasAlpha = cm.HasAlpha();
                    bool       isPre    = cm.AlphaPremultiplied;
                    int        trans    = cm.Transparency;
                    int[]      nbits    = null;
                    if (Ltable is ByteLookupTable)
                    {
                        if (db.DataType == db.TYPE_USHORT)
                        {
                            // Dst raster should be of type byte
                            if (hasAlpha)
                            {
                                nbits = new int[2];
                                if (trans == cm.BITMASK)
                                {
                                    nbits[1] = 1;
                                }
                                else
                                {
                                    nbits[1] = 8;
                                }
                            }
                            else
                            {
                                nbits = new int[1];
                            }
                            nbits[0] = 8;
                        }
                        // For byte, no need to change the cm
                    }
                    else if (Ltable is ShortLookupTable)
                    {
                        transferType = DataBuffer.TYPE_USHORT;
                        if (db.DataType == db.TYPE_BYTE)
                        {
                            if (hasAlpha)
                            {
                                nbits = new int[2];
                                if (trans == cm.BITMASK)
                                {
                                    nbits[1] = 1;
                                }
                                else
                                {
                                    nbits[1] = 16;
                                }
                            }
                            else
                            {
                                nbits = new int[1];
                            }
                            nbits[0] = 16;
                        }
                    }
                    if (nbits != null)
                    {
                        cm = new ComponentColorModel(cm.ColorSpace, nbits, hasAlpha, isPre, trans, transferType);
                    }
                }
                image = new BufferedImage(cm, cm.CreateCompatibleWritableRaster(w, h), cm.AlphaPremultiplied, null);
            }
            else
            {
                image = new BufferedImage(destCM, destCM.CreateCompatibleWritableRaster(w, h), destCM.AlphaPremultiplied, null);
            }

            return(image);
        }