src/java.desktop/share/classes/java/awt/image/FilteredImageSource.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 48289 054fecf0c1d2
permissions -rw-r--r--
8211300: Convert C-style array declarations in JDK client code Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48289
054fecf0c1d2 8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ImageFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ImageConsumer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ImageProducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class is an implementation of the ImageProducer interface which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * takes an existing image and a filter object and uses them to produce
48289
054fecf0c1d2 8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents: 47216
diff changeset
    38
 * image data for a new filtered version of the original image. Furthermore,
054fecf0c1d2 8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents: 47216
diff changeset
    39
 * {@code FilteredImageSource} is safe for use by multiple threads.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Here is an example which filters an image by swapping the red and
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
    41
 * blue components:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      Image src = getImage("doc:///demo/images/duke/T1.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      ImageFilter colorfilter = new RedBlueSwapFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      Image img = createImage(new FilteredImageSource(src.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *                                                      colorfilter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class FilteredImageSource implements ImageProducer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    ImageProducer src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    ImageFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructs an ImageProducer object from an existing ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * and a filter object.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    62
     * @param orig the specified {@code ImageProducer}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    63
     * @param imgf the specified {@code ImageFilter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @see ImageFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @see java.awt.Component#createImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public FilteredImageSource(ImageProducer orig, ImageFilter imgf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        src = orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        filter = imgf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
    72
    private Hashtable<ImageConsumer, ImageFilter> proxies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    75
     * Adds the specified {@code ImageConsumer}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * to the list of consumers interested in data for the filtered image.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    77
     * An instance of the original {@code ImageFilter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * is created
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    79
     * (using the filter's {@code getFilterInstance} method)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * to manipulate the image data
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    81
     * for the specified {@code ImageConsumer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The newly created filter instance
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    83
     * is then passed to the {@code addConsumer} method
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    84
     * of the original {@code ImageProducer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * This method is public as a side effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * of this class implementing
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
    89
     * the {@code ImageProducer} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * It should not be called from user code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * and its behavior if called from user code is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param ic  the consumer for the filtered image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public synchronized void addConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (proxies == null) {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
    98
            proxies = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!proxies.containsKey(ic)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ImageFilter imgf = filter.getFilterInstance(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            proxies.put(ic, imgf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            src.addConsumer(imgf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Determines whether an ImageConsumer is on the list of consumers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * currently interested in data for this image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * This method is public as a side effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * of this class implementing
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   114
     * the {@code ImageProducer} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * It should not be called from user code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * and its behavior if called from user code is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   118
     * @param ic the specified {@code ImageConsumer}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return true if the ImageConsumer is on the list; false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public synchronized boolean isConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return (proxies != null && proxies.containsKey(ic));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Removes an ImageConsumer from the list of consumers interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * data for this image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * This method is public as a side effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of this class implementing
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   133
     * the {@code ImageProducer} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * It should not be called from user code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * and its behavior if called from user code is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public synchronized void removeConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (proxies != null) {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
   141
            ImageFilter imgf =  proxies.get(ic);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (imgf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                src.removeConsumer(imgf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                proxies.remove(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (proxies.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    proxies = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Starts production of the filtered image.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   154
     * If the specified {@code ImageConsumer}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * isn't already a consumer of the filtered image,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   156
     * an instance of the original {@code ImageFilter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * is created
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   158
     * (using the filter's {@code getFilterInstance} method)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * to manipulate the image data
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   160
     * for the {@code ImageConsumer}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   161
     * The filter instance for the {@code ImageConsumer}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   162
     * is then passed to the {@code startProduction} method
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   163
     * of the original {@code ImageProducer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * This method is public as a side effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * of this class implementing
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   168
     * the {@code ImageProducer} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * It should not be called from user code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * and its behavior if called from user code is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param ic  the consumer for the filtered image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
48289
054fecf0c1d2 8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents: 47216
diff changeset
   175
    public synchronized void startProduction(ImageConsumer ic) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (proxies == null) {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
   177
            proxies = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
   179
        ImageFilter imgf = proxies.get(ic);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (imgf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            imgf = filter.getFilterInstance(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            proxies.put(ic, imgf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        src.startProduction(imgf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Requests that a given ImageConsumer have the image data delivered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * one more time in top-down, left-right order.  The request is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * handed to the ImageFilter for further processing, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * ability to preserve the pixel ordering depends on the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * This method is public as a side effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * of this class implementing
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26749
diff changeset
   196
     * the {@code ImageProducer} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * It should not be called from user code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * and its behavior if called from user code is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
48289
054fecf0c1d2 8188083: NullPointerExcpn-java.awt.image.FilteredImageSource.startProduction JDK-8079607
pnarayanan
parents: 47216
diff changeset
   202
    public synchronized void requestTopDownLeftRightResend(ImageConsumer ic) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (proxies != null) {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
   204
            ImageFilter imgf = proxies.get(ic);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (imgf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                imgf.resendTopDownLeftRight(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}