src/java.desktop/share/classes/java/awt/image/RGBImageFilter.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47216 71c04702a3d5
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
/*
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2018, 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.ImageConsumer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class provides an easy way to create an ImageFilter which modifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the pixels of an image in the default RGB ColorModel.  It is meant to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * be used in conjunction with a FilteredImageSource object to produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * filtered versions of existing images.  It is an abstract class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * provides the calls needed to channel all of the pixel data through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * single method which converts pixels one at a time in the default RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * ColorModel regardless of the ColorModel being used by the ImageProducer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The only method which needs to be defined to create a useable image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * filter is the filterRGB method.  Here is an example of a definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of a filter which swaps the red and blue components of an image:
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    42
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      class RedBlueSwapFilter extends RGBImageFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *          public RedBlueSwapFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *              // The filter's operation does not depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *              // pixel's location, so IndexColorModels can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *              // filtered directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *              canFilterIndexColorModel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *          public int filterRGB(int x, int y, int rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *              return ((rgb & 0xff00ff00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *                      | ((rgb & 0xff0000) >> 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                      | ((rgb & 0xff) << 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    59
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see FilteredImageSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see ImageFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public abstract class RGBImageFilter extends ImageFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    70
     * The {@code ColorModel} to be replaced by
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    71
     * {@code newmodel} when the user calls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * {@link #substituteColorModel(ColorModel, ColorModel) substituteColorModel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected ColorModel origmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    77
     * The {@code ColorModel} with which to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    78
     * replace {@code origmodel} when the user calls
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    79
     * {@code substituteColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected ColorModel newmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This boolean indicates whether or not it is acceptable to apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the color filtering of the filterRGB method to the color table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * entries of an IndexColorModel object in lieu of pixel by pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * filtering.  Subclasses should set this variable to true in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * constructor if their filterRGB method does not depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * coordinate of the pixel being filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see #substituteColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see #filterRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see IndexColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected boolean canFilterIndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * If the ColorModel is an IndexColorModel and the subclass has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * set the canFilterIndexColorModel flag to true, we substitute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * a filtered version of the color model here and wherever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * that original ColorModel object appears in the setPixels methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * If the ColorModel is not an IndexColorModel or is null, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * overrides the default ColorModel used by the ImageProducer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * specifies the default RGB ColorModel instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   106
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void setColorModel(ColorModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (canFilterIndexColorModel && (model instanceof IndexColorModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ColorModel newcm = filterIndexColorModel((IndexColorModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            substituteColorModel(model, newcm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            consumer.setColorModel(newcm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            consumer.setColorModel(ColorModel.getRGBdefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Registers two ColorModel objects for substitution.  If the oldcm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * is encountered during any of the setPixels methods, the newcm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * is substituted and the pixels passed through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * untouched (but with the new ColorModel object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param oldcm the ColorModel object to be replaced on the fly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param newcm the ColorModel object to replace oldcm on the fly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void substituteColorModel(ColorModel oldcm, ColorModel newcm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        origmodel = oldcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        newmodel = newcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Filters an IndexColorModel object by running each entry in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * color tables through the filterRGB function that RGBImageFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * subclasses must provide.  Uses coordinates of -1 to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * a color table entry is being filtered rather than an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * pixel value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param icm the IndexColorModel object to be filtered
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   144
     * @exception NullPointerException if {@code icm} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return a new IndexColorModel representing the filtered colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public IndexColorModel filterIndexColorModel(IndexColorModel icm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int mapsize = icm.getMapSize();
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   149
        byte[] r = new byte[mapsize];
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   150
        byte[] g = new byte[mapsize];
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   151
        byte[] b = new byte[mapsize];
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   152
        byte[] a = new byte[mapsize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        icm.getReds(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        icm.getGreens(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        icm.getBlues(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        icm.getAlphas(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int trans = icm.getTransparentPixel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        boolean needalpha = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i = 0; i < mapsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int rgb = filterRGB(-1, -1, icm.getRGB(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            a[i] = (byte) (rgb >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (a[i] != ((byte)0xff) && i != trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                needalpha = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            r[i] = (byte) (rgb >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            g[i] = (byte) (rgb >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            b[i] = (byte) (rgb >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (needalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return new IndexColorModel(icm.getPixelSize(), mapsize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                       r, g, b, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return new IndexColorModel(icm.getPixelSize(), mapsize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                       r, g, b, trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Filters a buffer of pixels in the default RGB ColorModel by passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * them one by one through the filterRGB method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param x the X coordinate of the upper-left corner of the region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *          of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param y the Y coordinate of the upper-left corner of the region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *          of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param w the width of the region of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param h the height of the region of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param pixels the array of pixels
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   188
     * @param off the offset into the {@code pixels} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param scansize the distance from one row of pixels to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *        in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see #filterRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public void filterRGBPixels(int x, int y, int w, int h,
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   195
                                int[] pixels, int off, int scansize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int index = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (int cy = 0; cy < h; cy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (int cx = 0; cx < w; cx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                pixels[index] = filterRGB(x + cx, y + cy, pixels[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            index += scansize - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                           pixels, off, scansize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * If the ColorModel object is the same one that has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * been converted, then simply passes the pixels through with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * converted ColorModel. Otherwise converts the buffer of byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * pixels to the default RGB ColorModel and passes the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * buffer to the filterRGBPixels method to be converted one by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   216
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see #filterRGBPixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void setPixels(int x, int y, int w, int h,
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   225
                          ColorModel model, byte[] pixels, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                          int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (model == origmodel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            consumer.setPixels(x, y, w, h, newmodel, pixels, off, scansize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   230
            int[] filteredpixels = new int[w];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            int index = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            for (int cy = 0; cy < h; cy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                for (int cx = 0; cx < w; cx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    filteredpixels[cx] = model.getRGB((pixels[index] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                index += scansize - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                filterRGBPixels(x, y + cy, w, 1, filteredpixels, 0, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * If the ColorModel object is the same one that has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * been converted, then simply passes the pixels through with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * converted ColorModel, otherwise converts the buffer of integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * pixels to the default RGB ColorModel and passes the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * buffer to the filterRGBPixels method to be converted one by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Converts a buffer of integer pixels to the default RGB ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * and passes the converted buffer to the filterRGBPixels method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   253
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see #filterRGBPixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void setPixels(int x, int y, int w, int h,
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   262
                          ColorModel model, int[] pixels, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                          int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (model == origmodel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            consumer.setPixels(x, y, w, h, newmodel, pixels, off, scansize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   267
            int[] filteredpixels = new int[w];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            int index = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (int cy = 0; cy < h; cy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                for (int cx = 0; cx < w; cx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    filteredpixels[cx] = model.getRGB(pixels[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                index += scansize - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                filterRGBPixels(x, y + cy, w, 1, filteredpixels, 0, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Subclasses must specify a method to convert a single input pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * in the default RGB ColorModel to a single output pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param x the X coordinate of the pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param y the Y coordinate of the pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param rgb the integer pixel representation in the default RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *            color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return a filtered pixel in the default RGB color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @see #filterRGBPixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public abstract int filterRGB(int x, int y, int rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}