src/java.desktop/share/classes/java/awt/image/ReplicateScaleFilter.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) 1996, 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
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An ImageFilter class for scaling images using the simplest algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class extends the basic ImageFilter Class to scale an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * image and provide a source for a new image containing the resampled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * image.  The pixels in the source image are sampled to produce pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * for an image of the specified size by replicating rows and columns of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * pixels to scale up or omitting rows and columns of pixels to scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>It is meant to be used in conjunction with a FilteredImageSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * object to produce scaled versions of existing images.  Due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * implementation dependencies, there may be differences in pixel values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * of an image filtered on different platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see FilteredImageSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see ImageFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class ReplicateScaleFilter extends ImageFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The width of the source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected int srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The height of the source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected int srcHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The target width to scale the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected int destWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The target height to scale the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected int destHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    74
     * An {@code int} array containing information about a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * row of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
    77
    protected int[] srcrows;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    80
     * An {@code int} array containing information about a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * column of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
    83
    protected int[] srccols;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    86
     * A {@code byte} array initialized with a size of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * {@link #destWidth} and used to deliver a row of pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * data to the {@link ImageConsumer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected Object outpixbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a ReplicateScaleFilter that scales the pixels from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * its source Image as specified by the width and height parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param width the target width to scale the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param height the target height to scale the image
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    97
     * @throws IllegalArgumentException if {@code width} equals
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    98
     *         zero or {@code height} equals zero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public ReplicateScaleFilter(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (width == 0 || height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("Width ("+width+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                ") and height ("+height+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                                ") must be non-zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        destWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        destHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Passes along the properties from the source object after adding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * property indicating the scale applied.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   113
     * This method invokes {@code super.setProperties},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * which might result in additional properties being added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   117
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void setProperties(Hashtable<?,?> props) {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 5506
diff changeset
   124
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String key = "rescale";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String val = destWidth + "x" + destHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object o = p.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (o != null && o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            val = ((String) o) + ", " + val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        p.put(key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        super.setProperties(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Override the dimensions of the source image and pass the dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * of the new scaled size to the ImageConsumer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   141
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see ImageConsumer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void setDimensions(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        srcWidth = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        srcHeight = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (destWidth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (destHeight < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                destWidth = srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                destHeight = srcHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                destWidth = srcWidth * destHeight / srcHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else if (destHeight < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            destHeight = srcHeight * destWidth / srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        consumer.setDimensions(destWidth, destHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private void calculateMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        srcrows = new int[destHeight + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        for (int y = 0; y <= destHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        srccols = new int[destWidth + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int x = 0; x <= destWidth; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Choose which rows and columns of the delivered byte pixels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * needed for the destination scaled image and pass through just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * those rows and columns that are needed, replicated as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   181
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    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
   188
                          ColorModel model, byte[] pixels, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                          int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (srcrows == null || srccols == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            calculateMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int sx, sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   196
        byte[] outpix;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (outpixbuf != null && outpixbuf instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            outpix = (byte[]) outpixbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            outpix = new byte[destWidth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            outpixbuf = outpix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            int srcoff = off + scansize * (sy - y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                outpix[dx] = pixels[srcoff + sx - x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (dx > dx1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                consumer.setPixels(dx1, dy, dx - dx1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                   model, outpix, dx1, destWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Choose which rows and columns of the delivered int pixels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * needed for the destination scaled image and pass through just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * those rows and columns that are needed, replicated as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Note: This method is intended to be called by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   222
     * {@code ImageProducer} of the {@code Image} whose pixels
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * are being filtered. Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * this class to filter pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * this method directly since that operation could interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * with the filtering operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    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
   229
                          ColorModel model, int[] pixels, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                          int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (srcrows == null || srccols == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            calculateMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int sx, sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   237
        int[] outpix;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (outpixbuf != null && outpixbuf instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            outpix = (int[]) outpixbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            outpix = new int[destWidth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            outpixbuf = outpix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            int srcoff = off + scansize * (sy - y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                outpix[dx] = pixels[srcoff + sx - x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (dx > dx1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                consumer.setPixels(dx1, dy, dx - dx1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                   model, outpix, dx1, destWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}