IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

2D Java Discussion :

[java2D] filtres et masques


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de lebesnec
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2005
    Messages : 82
    Par défaut [java2D] filtres et masques
    hello,

    je voudrais, sur une image, appliquer un filtre (genre flou ou changer la saturation etc.) sur toutes les parties de l'image qui répondent à un certain critère (par exemple seulement là où alpha>0 ou encore là où saturation<seuil etc.).

    J'arrive bien à appliquer un filtre sur toute l'image mais pas sur une partie donnée. Il faudrait bien sûr si possible que l'opération soit le plus rapide possible et consomme le moins de ressources systèmes possible .

    j'avais penser à calculer d'abord un masque de la zone sur lequel le filtre doit s'appliquer, puis ensuite faire le filtre en suivant le masque, mais je ne sait pas trop comment coder tout ça ...

    toute idée est la bienvenue

  2. #2
    Gfx
    Gfx est déconnecté
    Expert confirmé
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Par défaut
    Il va falloir que tu crées une BufferedImageOp. Voici un exemple de BufferedImageOp que j'ai créé pour SwingX et qui applique un effet de flou sur toute l'image. Inspire-toi de ça pour ajouter la gestion des masques (il y a 3 fichiers en tout, GraphicsUtilities, AbstractFilter et FastBlurFilter).

    Pour appliquer un tel filtre à une image c'est assez simple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    // lit l'image
    BufferedImage image = ImageIO.read(...);
    // applique le filtre
    BufferedImage blurredImage = new FastBlurFilter().filter(image, null);
    Voici les fichiers

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
     
    /*
     * $Id: AbstractFilter.java,v 1.1 2006/10/22 03:26:24 gfx Exp $
     *
     * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
     *
     * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
     * Santa Clara, California 95054, U.S.A. All rights reserved.
     *
     * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. The name of the author may not be used to endorse or promote products
     *    derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
     
    package org.jdesktop.swingx.image;
     
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.BufferedImageOp;
    import java.awt.image.ColorModel;
     
    /**
     * <p>Provides an abstract implementation of the <code>BufferedImageOp</code>
     * interface. This class can be used to created new image filters based
     * on <code>BufferedImageOp</code>.</p>
     *
     * @author Romain Guy <romain.guy@mac.com>
     */
     
    public abstract class AbstractFilter implements BufferedImageOp {
        public abstract BufferedImage filter(BufferedImage src, BufferedImage dest);
     
        /**
         * {@inheritDoc}
         */
        public Rectangle2D getBounds2D(BufferedImage src) {
            return new Rectangle(0, 0, src.getWidth(), src.getHeight());
        }
     
        /**
         * {@inheritDoc}
         */
        public BufferedImage createCompatibleDestImage(BufferedImage src,
                                                       ColorModel destCM) {
            if (destCM == null) {
                destCM = src.getColorModel();
            }
     
            return new BufferedImage(destCM,
                                     destCM.createCompatibleWritableRaster(
                                             src.getWidth(), src.getHeight()),
                                     destCM.isAlphaPremultiplied(), null);
        }
     
        /**
         * {@inheritDoc}
         */
        public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
            return (Point2D) srcPt.clone();
        }
     
        /**
         * {@inheritDoc}
         */
        public RenderingHints getRenderingHints() {
            return null;
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
     
    /*
     * $Id: FastBlurFilter.java,v 1.1 2006/10/22 03:26:24 gfx Exp $
     *
     * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
     *
     * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
     * Santa Clara, California 95054, U.S.A. All rights reserved.
     *
     * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. The name of the author may not be used to endorse or promote products
     *    derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
     
    package org.jdesktop.swingx.image;
     
    import java.awt.image.BufferedImage;
     
    import org.jdesktop.swingx.graphics.GraphicsUtilities;
     
    /**
     * <p>A fast blur filter can be used to blur pictures quickly. This filter is an
     * implementation of the box blur algorithm. The blurs generated by this
     * algorithm might show square artifacts, especially on pictures containing
     * straight lines (rectangles, text, etc.) On most pictures though, the
     * result will look very good.</p>
     * <p>The force of the blur can be controlled with a radius and the
     * default radius is 3. Since the blur clamps values on the edges of the
     * source picture, you might need to provide a picture with empty borders
     * to avoid artifacts at the edges. The performance of this filter are
     * independant from the radius.</p>
     *
     * @author Romain Guy <romain.guy@mac.com>
     */
    public class FastBlurFilter extends AbstractFilter {
        private final int radius;
     
        /**
         * <p>Creates a new blur filter with a default radius of 3.</p>
         */
        public FastBlurFilter() {
            this(3);
        }
     
        /**
         * <p>Creates a new blur filter with the specified radius. If the radius
         * is lower than 1, a radius of 1 will be used automatically.</p>
         *
         * @param radius the radius, in pixels, of the blur
         */
        public FastBlurFilter(int radius) {
            if (radius < 1) {
                radius = 1;
            }
     
            this.radius = radius;
        }
     
        /**
         * <p>Returns the radius used by this filter, in pixels.</p>
         *
         * @return the radius of the blur
         */
        public int getRadius() {
            return radius;
        }
     
        /**
         * {@inheritDoc}
         */
        @Override
        public BufferedImage filter(BufferedImage src, BufferedImage dst) {
            int width = src.getWidth();
            int height = src.getHeight();
     
            if (dst == null) {
                dst = createCompatibleDestImage(src, null);
            }
     
            int[] srcPixels = new int[width * height];
            int[] dstPixels = new int[width * height];
     
            GraphicsUtilities.getPixels(src, 0, 0, width, height, srcPixels);
            // horizontal pass
            blur(srcPixels, dstPixels, width, height, radius);
            // vertical pass
            blur(dstPixels, srcPixels, height, width, radius);
            // the result is now stored in srcPixels due to the 2nd pass
            GraphicsUtilities.setPixels(dst, 0, 0, width, height, srcPixels);
     
            return dst;
        }
     
        /**
         * <p>Blurs the source pixels into the destination pixels. The force of
         * the blur is specified by the radius which must be greater than 0.</p>
         * <p>The source and destination pixels arrays are expected to be in the
         * INT_ARGB format.</p>
         *
         * @param srcPixels the source pixels
         * @param dstPixels the destination pixels
         * @param width the width of the source picture
         * @param height the height of the source picture
         * @param radius the radius of the blur effect
         */
        static void blur(int[] srcPixels, int[] dstPixels,
                         int width, int height, int radius) {
            final int windowSize = radius * 2 + 1;
            final int radiusPlusOne = radius + 1;
     
            int sumAlpha;
            int sumRed;
            int sumGreen;
            int sumBlue;
     
            int srcIndex = 0;
            int dstIndex;
            int pixel;
     
            int[] sumLookupTable = new int[256 * windowSize];
            for (int i = 0; i < sumLookupTable.length; i++) {
                sumLookupTable[i] = i / windowSize;
            }
     
            int[] indexLookupTable = new int[radiusPlusOne];
            if (radius < width) {
                for (int i = 0; i < indexLookupTable.length; i++) {
                    indexLookupTable[i] = i;
                }
            } else {
                for (int i = 0; i < width; i++) {
                    indexLookupTable[i] = i;
                }
                for (int i = width; i < indexLookupTable.length; i++) {
                    indexLookupTable[i] = width - 1;
                }
            }
     
            for (int y = 0; y < height; y++) {
                sumAlpha = sumRed = sumGreen = sumBlue = 0;
                dstIndex = y;
     
                pixel = srcPixels[srcIndex];
                sumAlpha += radiusPlusOne * ((pixel >> 24) & 0xFF);
                sumRed   += radiusPlusOne * ((pixel >> 16) & 0xFF);
                sumGreen += radiusPlusOne * ((pixel >>  8) & 0xFF);
                sumBlue  += radiusPlusOne * ( pixel        & 0xFF);
     
                for (int i = 1; i <= radius; i++) {
                    pixel = srcPixels[srcIndex + indexLookupTable[i]];
                    sumAlpha += (pixel >> 24) & 0xFF;
                    sumRed   += (pixel >> 16) & 0xFF;
                    sumGreen += (pixel >>  8) & 0xFF;
                    sumBlue  +=  pixel        & 0xFF;
                }
     
                for  (int x = 0; x < width; x++) {
                    dstPixels[dstIndex] = sumLookupTable[sumAlpha] << 24 |
                                          sumLookupTable[sumRed]   << 16 |
                                          sumLookupTable[sumGreen] <<  8 |
                                          sumLookupTable[sumBlue];
                    dstIndex += height;
     
                    int nextPixelIndex = x + radiusPlusOne;
                    if (nextPixelIndex >= width) {
                        nextPixelIndex = width - 1;
                    }
     
                    int previousPixelIndex = x - radius;
                    if (previousPixelIndex < 0) {
                        previousPixelIndex = 0;
                    }
     
                    int nextPixel = srcPixels[srcIndex + nextPixelIndex];
                    int previousPixel = srcPixels[srcIndex + previousPixelIndex];
     
                    sumAlpha += (nextPixel     >> 24) & 0xFF;
                    sumAlpha -= (previousPixel >> 24) & 0xFF;
     
                    sumRed += (nextPixel     >> 16) & 0xFF;
                    sumRed -= (previousPixel >> 16) & 0xFF;
     
                    sumGreen += (nextPixel     >> 8) & 0xFF;
                    sumGreen -= (previousPixel >> 8) & 0xFF;
     
                    sumBlue += nextPixel & 0xFF;
                    sumBlue -= previousPixel & 0xFF;
                }
     
                srcIndex += width;
            }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
     
    /*
     * $Id: GraphicsUtilities.java,v 1.1 2006/10/22 03:26:23 gfx Exp $
     *
     * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
     *
     * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
     * Santa Clara, California 95054, U.S.A. All rights reserved.
     *
     * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. The name of the author may not be used to endorse or promote products
     *    derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
     
    package org.jdesktop.swingx.graphics;
     
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorModel;
    import java.awt.image.Raster;
    import java.awt.image.WritableRaster;
    import java.awt.GraphicsConfiguration;
    import java.awt.Transparency;
    import java.awt.Graphics;
    import java.awt.GraphicsEnvironment;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
     
    /**
     * <p><code>GraphicsUtilities</code> contains a set of tools to perform
     * common graphics operations easily. These operations are divided into
     * several themes, listed below.</p>
     * <h2>Compatible Images</h2>
     * <p>Compatible images can, and should, be used to increase drawing
     * performance. This class provides a number of methods to load compatible
     * images directly from files or to convert existing images to compatibles
     * images.</p>
     * <h2>Creating Thumbnails</h2>
     * <p>This class provides a number of methods to easily scale down images.
     * Some of these methods offer a trade-off between speed and result quality and
     * shouuld be used all the time. They also offer the advantage of producing
     * compatible images, thus automatically resulting into better runtime
     * performance.</p>
     * <p>All these methodes are both faster than
     * {@link java.awt.Image#getScaledInstance(int, int, int)} and produce
     * better-looking results than the various <code>drawImage()</code> methods
     * in {@link java.awt.Graphics}, which can be used for image scaling.</p>
     * <h2>Image Manipulation</h2>
     * <p>This class provides two methods to get and set pixels in a buffered image.
     * These methods try to avoid unmanaging the image in order to keep good
     * performance.</p>
     *
     * @author Romain Guy <romain.guy@mac.com>
     */
    public class GraphicsUtilities {
        private static final GraphicsConfiguration CONFIGURATION =
                GraphicsEnvironment.getLocalGraphicsEnvironment().
                        getDefaultScreenDevice().getDefaultConfiguration();
     
        private GraphicsUtilities() {
        }
     
        /**
         * <p>Returns a new <code>BufferedImage</code> using the same color model
         * as the image passed as a parameter. The returned image is only compatible
         * with the image passed as a parameter. This does not mean the returned
         * image is compatible with the hardware.</p>
         *
         * @param image the reference image from which the color model of the new
         *   image is obtained
         * @return a new <code>BufferedImage</code>, compatible with the color model
         *   of <code>image</code>
         */
        public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
            ColorModel cm = image.getColorModel();
            return new BufferedImage(cm,
                cm.createCompatibleWritableRaster(image.getWidth(),
                                                  image.getHeight()),
                cm.isAlphaPremultiplied(), null);
        }
     
        /**
         * <p>Returns a new compatible image with the same width, height and
         * transparency as the image specified as a parameter.</p>
         *
         * @see java.awt.Transparency
         * @see #createCompatibleImage(int, int)
         * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
         * @see #createTranslucentCompatibleImage(int, int)
         * @see #loadCompatibleImage(java.net.URL)
         * @see #toCompatibleImage(java.awt.image.BufferedImage)
         * @param image the reference image from which the dimension and the
         *   transparency of the new image are obtained
         * @return a new compatible <code>BufferedImage</code> with the same
         *   dimension and transparency as <code>image</code>
         */
        public static BufferedImage createCompatibleImage(BufferedImage image) {
            return createCompatibleImage(image, image.getWidth(), image.getHeight());
        }
     
        /**
         * <p>Returns a new compatible image of the specified width and height, and
         * the same transparency setting as the image specified as a parameter.</p>
         *
         * @see java.awt.Transparency
         * @see #createCompatibleImage(java.awt.image.BufferedImage)
         * @see #createCompatibleImage(int, int)
         * @see #createTranslucentCompatibleImage(int, int)
         * @see #loadCompatibleImage(java.net.URL)
         * @see #toCompatibleImage(java.awt.image.BufferedImage)
         * @param width the width of the new image
         * @param height the height of the new image
         * @param image the reference image from which the transparency of the new
         *   image is obtained
         * @return a new compatible <code>BufferedImage</code> with the same
         *   transparency as <code>image</code> and the specified dimension
         */
        public static BufferedImage createCompatibleImage(BufferedImage image,
                                                          int width, int height) {
            return CONFIGURATION.createCompatibleImage(width, height,
                                                       image.getTransparency());
        }
     
        /**
         * <p>Returns a new opaque compatible image of the specified width and
         * height.</p>
         *
         * @see #createCompatibleImage(java.awt.image.BufferedImage)
         * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
         * @see #createTranslucentCompatibleImage(int, int)
         * @see #loadCompatibleImage(java.net.URL)
         * @see #toCompatibleImage(java.awt.image.BufferedImage)
         * @param width the width of the new image
         * @param height the height of the new image
         * @return a new opaque compatible <code>BufferedImage</code> of the
         *   specified width and height
         */
        public static BufferedImage createCompatibleImage(int width, int height) {
            return CONFIGURATION.createCompatibleImage(width, height);
        }
     
        /**
         * <p>Returns a new translucent compatible image of the specified width
         * and height.</p>
         *
         * @see #createCompatibleImage(java.awt.image.BufferedImage)
         * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
         * @see #createCompatibleImage(int, int)
         * @see #loadCompatibleImage(java.net.URL)
         * @see #toCompatibleImage(java.awt.image.BufferedImage)
         * @param width the width of the new image
         * @param height the height of the new image
         * @return a new translucent compatible <code>BufferedImage</code> of the
         *   specified width and height
         */
        public static BufferedImage createTranslucentCompatibleImage(int width,
                                                                     int height) {
            return CONFIGURATION.createCompatibleImage(width, height,
                                                       Transparency.TRANSLUCENT);
        }
     
        /**
         * <p>Returns a new compatible image from a URL. The image is loaded from the
         * specified location and then turned, if necessary into a compatible
         * image.</p>
         *
         * @see #createCompatibleImage(java.awt.image.BufferedImage)
         * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
         * @see #createCompatibleImage(int, int)
         * @see #createTranslucentCompatibleImage(int, int)
         * @see #toCompatibleImage(java.awt.image.BufferedImage)
         * @param resource the URL of the picture to load as a compatible image
         * @return a new translucent compatible <code>BufferedImage</code> of the
         *   specified width and height
         * @throws java.io.IOException if the image cannot be read or loaded
         */
        public static BufferedImage loadCompatibleImage(URL resource)
                throws IOException {
            BufferedImage image = ImageIO.read(resource);
            return toCompatibleImage(image);
        }
     
        /**
         * <p>Return a new compatible image that contains a copy of the specified
         * image. This method ensures an image is compatible with the hardware,
         * and therefore optimized for fast blitting operations.</p>
         *
         * @see #createCompatibleImage(java.awt.image.BufferedImage)
         * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
         * @see #createCompatibleImage(int, int)
         * @see #createTranslucentCompatibleImage(int, int)
         * @see #loadCompatibleImage(java.net.URL)
         * @param image the image to copy into a new compatible image
         * @return a new compatible copy, with the
         *   same width and height and transparency and content, of <code>image</code>
         */
        public static BufferedImage toCompatibleImage(BufferedImage image) {
            if (image.getColorModel().equals(CONFIGURATION.getColorModel())) {
                return image;
            }
     
            BufferedImage compatibleImage = CONFIGURATION.createCompatibleImage(
                    image.getWidth(), image.getHeight(), image.getTransparency());
            Graphics g = compatibleImage.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
     
            return compatibleImage;
        }
     
        /////////////////////////////////////////////////////////////////////
        // J'ai retiré le code des méthodes de création de thumbnails
        /////////////////////////////////////////////////////////////////////
     
        /**
         * <p>Returns an array of pixels, stored as integers, from a
         * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
         * area defined by a location and two dimensions. Calling this method on
         * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
         * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
         *
         * @param img the source image
         * @param x the x location at which to start grabbing pixels
         * @param y the y location at which to start grabbing pixels
         * @param w the width of the rectangle of pixels to grab
         * @param h the height of the rectangle of pixels to grab
         * @param pixels a pre-allocated array of pixels of size w*h; can be null
         * @return <code>pixels</code> if non-null, a new array of integers
         *   otherwise
         * @throws IllegalArgumentException is <code>pixels</code> is non-null and
         *   of length &lt; w*h
         */
        public static int[] getPixels(BufferedImage img,
                                      int x, int y, int w, int h, int[] pixels) {
            if (w == 0 || h == 0) {
                return new int[0];
            }
     
            if (pixels == null) {
                pixels = new int[w * h];
            } else if (pixels.length < w * h) {
                throw new IllegalArgumentException("pixels array must have a length" +
                                                   " >= w*h");
            }
     
            int imageType = img.getType();
            if (imageType == BufferedImage.TYPE_INT_ARGB ||
                imageType == BufferedImage.TYPE_INT_RGB) {
                Raster raster = img.getRaster();
                return (int[]) raster.getDataElements(x, y, w, h, pixels);
            }
     
            // Unmanages the image
            return img.getRGB(x, y, w, h, pixels, 0, w);
        }
     
        /**
         * <p>Writes a rectangular area of pixels in the destination
         * <code>BufferedImage</code>. Calling this method on
         * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
         * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
         *
         * @param img the destination image
         * @param x the x location at which to start storing pixels
         * @param y the y location at which to start storing pixels
         * @param w the width of the rectangle of pixels to store
         * @param h the height of the rectangle of pixels to store
         * @param pixels an array of pixels, stored as integers
         * @throws IllegalArgumentException is <code>pixels</code> is non-null and
         *   of length &lt; w*h
         */
        public static void setPixels(BufferedImage img,
                                     int x, int y, int w, int h, int[] pixels) {
            if (pixels == null || w == 0 || h == 0) {
                return;
            } else if (pixels.length < w * h) {
                throw new IllegalArgumentException("pixels array must have a length" +
                                                   " >= w*h");
            }
     
            int imageType = img.getType();
            if (imageType == BufferedImage.TYPE_INT_ARGB ||
                imageType == BufferedImage.TYPE_INT_RGB) {
                WritableRaster raster = img.getRaster();
                raster.setDataElements(x, y, w, h, pixels);
            } else {
                // Unmanages the image
                img.setRGB(x, y, w, h, pixels, 0, w);
            }
        }
    }

  3. #3
    Membre confirmé Avatar de lebesnec
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2005
    Messages : 82
    Par défaut
    merci !

    je vais essayer avec ça.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Masque des colonnes selon certains critères sans les filtres
    Par CLAUDE19 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 26/10/2011, 17h23
  2. [E-03] Filtre auto et lignes masquées
    Par allergique dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 16/12/2008, 19h25
  3. TADOTable et filtre
    Par GaL dans le forum C++Builder
    Réponses: 16
    Dernier message: 02/07/2002, 16h52
  4. Filtre passe Bande
    Par Mau dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 28/06/2002, 17h03
  5. Probleme de filtre dans bdd
    Par scorpiwolf dans le forum C++Builder
    Réponses: 2
    Dernier message: 04/06/2002, 10h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo