src/java.desktop/share/classes/java/awt/image/PixelGrabber.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.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ImageProducer;
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.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The PixelGrabber class implements an ImageConsumer which can be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to an Image or ImageProducer object to retrieve a subset of the pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * in that image.  Here is an example:
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    38
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * public void handlesinglepixel(int x, int y, int pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      int alpha = (pixel >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      int red   = (pixel >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      int green = (pixel >>  8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      int blue  = (pixel      ) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      // Deal with the pixel as necessary...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * public void handlepixels(Image img, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      int[] pixels = new int[w * h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *          pg.grabPixels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *          System.err.println("interrupted waiting for pixels!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *          return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *          System.err.println("image fetch aborted or errored");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *          return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *          for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *              handlesinglepixel(x+i, y+j, pixels[j * w + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    68
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class PixelGrabber implements ImageConsumer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    ImageProducer producer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int dstX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    int dstY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int dstH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    ColorModel imageModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    byte[] bytePixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int[] intPixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int dstOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int dstScan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean grabbing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                            | ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final int DONEBITS = (GRABBEDBITS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                         | ImageObserver.ERROR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * section of pixels from the specified image into the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The pixels are stored into the array in the default RGB ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * (x, y, w, h) is stored in the array at
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   102
     * {@code pix[(j - y) * scansize + (i - x) + off]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param img the image to retrieve pixels from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param x the x coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * of pixels to retrieve from the image, relative to the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * (unscaled) size of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param y the y coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * of pixels to retrieve from the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param w the width of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param h the height of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param pix the array of integers which are to be used to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * RGB pixels retrieved from the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param off the offset into the array of where to store the first pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param scansize the distance from one row of pixels to the next in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public PixelGrabber(Image img, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        int[] pix, int off, int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this(img.getSource(), x, y, w, h, pix, off, scansize);
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
     * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * section of pixels from the image produced by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * ImageProducer into the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The pixels are stored into the array in the default RGB ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * (x, y, w, h) is stored in the array at
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   130
     * {@code pix[(j - y) * scansize + (i - x) + off]}.
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   131
     * @param ip the {@code ImageProducer} that produces the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * image from which to retrieve pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param x the x coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * of pixels to retrieve from the image, relative to the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * (unscaled) size of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param y the y coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * of pixels to retrieve from the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param w the width of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param h the height of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param pix the array of integers which are to be used to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * RGB pixels retrieved from the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param off the offset into the array of where to store the first pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param scansize the distance from one row of pixels to the next in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        int[] pix, int off, int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        producer = ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        dstX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        dstY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        dstW = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        dstH = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        dstOff = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        dstScan = scansize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        intPixels = pix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        imageModel = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * section of pixels from the specified image.  The pixels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * accumulated in the original ColorModel if the same ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * is used for every call to setPixels, otherwise the pixels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * accumulated in the default RGB ColorModel.  If the forceRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * parameter is true, then the pixels will be accumulated in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * default RGB ColorModel anyway.  A buffer is allocated by the
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
   168
     * PixelGrabber to hold the pixels in either case.  If {@code (w < 0)} or
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
   169
     * {@code (h < 0)}, then they will default to the remaining width and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * height of the source data when that information is delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param img the image to retrieve the image data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param x the x coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * of pixels to retrieve from the image, relative to the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * (unscaled) size of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param y the y coordinate of the upper left corner of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * of pixels to retrieve from the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param w the width of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param h the height of the rectangle of pixels to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param forceRGB true if the pixels should always be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the default RGB ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public PixelGrabber(Image img, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        boolean forceRGB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        producer = img.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        dstX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        dstY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        dstW = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        dstH = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (forceRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            imageModel = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Request the PixelGrabber to start fetching the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public synchronized void startGrabbing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if ((flags & DONEBITS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (!grabbing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            grabbing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            flags &= ~(ImageObserver.ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            producer.startProduction(this);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Request the PixelGrabber to abort the image fetch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public synchronized void abortGrabbing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        imageComplete(IMAGEABORTED);
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
     * Request the Image or ImageProducer to start delivering pixels and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * wait for all of the pixels in the rectangle of interest to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return true if the pixels were successfully grabbed, false on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * abort, error or timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *            Another thread has interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public boolean grabPixels() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return grabPixels(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Request the Image or ImageProducer to start delivering pixels and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * wait for all of the pixels in the rectangle of interest to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * delivered or until the specified timeout has elapsed.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * behaves in the following ways, depending on the value of
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   234
     * {@code ms}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <ul>
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
   236
     * <li> If {@code ms == 0}, waits until all pixels are delivered
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
   237
     * <li> If {@code ms > 0}, waits until all pixels are delivered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * as timeout expires.
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   239
     * <li> If {@code ms < 0}, returns {@code true} if all pixels
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   240
     * are grabbed, {@code false} otherwise and does not wait.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param ms the number of milliseconds to wait for the image pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * to arrive before timing out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return true if the pixels were successfully grabbed, false on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * abort, error or timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @exception InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *            Another thread has interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public synchronized boolean grabPixels(long ms)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if ((flags & DONEBITS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return (flags & GRABBEDBITS) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        long end = ms + System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (!grabbing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            grabbing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            flags &= ~(ImageObserver.ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            producer.startProduction(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        while (grabbing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            long timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (ms == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                timeout = end - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (flags & GRABBEDBITS) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Return the status of the pixels.  The ImageObserver flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * representing the available pixel information are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the bitwise OR of all relevant ImageObserver flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see ImageObserver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public synchronized int getStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Get the width of the pixel buffer (after adjusting for image width).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * If no width was specified for the rectangle of pixels to grab then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * then this information will only be available after the image has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * delivered the dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return the final width used for the pixel buffer or -1 if the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * is not yet known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see #getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public synchronized int getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return (dstW < 0) ? -1 : dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Get the height of the pixel buffer (after adjusting for image height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * If no width was specified for the rectangle of pixels to grab then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * then this information will only be available after the image has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * delivered the dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the final height used for the pixel buffer or -1 if the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * is not yet known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see #getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public synchronized int getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return (dstH < 0) ? -1 : dstH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Get the pixel buffer.  If the PixelGrabber was not constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * with an explicit pixel buffer to hold the pixels then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * will return null until the size and format of the image data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Since the PixelGrabber may fall back on accumulating the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * in the default RGB ColorModel at any time if the source image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * uses more than one ColorModel to deliver the data, the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * object returned by this method may change over time until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * image grab is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return either a byte array or an int array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see #getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #setPixels(int, int, int, int, ColorModel, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #setPixels(int, int, int, int, ColorModel, int[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public synchronized Object getPixels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return (bytePixels == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            ? ((Object) intPixels)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            : ((Object) bytePixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Get the ColorModel for the pixels stored in the array.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * PixelGrabber was constructed with an explicit pixel buffer then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * this method will always return the default RGB ColorModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * otherwise it may return null until the ColorModel used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * ImageProducer is known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Since the PixelGrabber may fall back on accumulating the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * in the default RGB ColorModel at any time if the source image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * uses more than one ColorModel to deliver the data, the ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * object returned by this method may change over time until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * image grab is complete and may not reflect any of the ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * objects that was used by the ImageProducer to deliver the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @return the ColorModel object used for storing the pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see #getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see #setColorModel(ColorModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public synchronized ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return imageModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The setDimensions method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param width the width of the dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param height the height of the dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public void setDimensions(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (dstW < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            dstW = width - dstX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (dstH < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            dstH = height - dstY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (dstW <= 0 || dstH <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            imageComplete(STATICIMAGEDONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else if (intPixels == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                   imageModel == ColorModel.getRGBdefault()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            intPixels = new int[dstW * dstH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            dstScan = dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            dstOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * The setHints method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param hints a set of hints used to process the pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void setHints(int hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * The setProperties method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param props the list of properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public void setProperties(Hashtable<?,?> props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * The setColorModel method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * with retrieving the requested pixels.
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   423
     * @param model the specified {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see #getColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public void setColorModel(ColorModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private void convertToRGB() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int size = dstW * dstH;
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   432
        int[] newpixels = new int[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (bytePixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } else if (intPixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                newpixels[i] = imageModel.getRGB(intPixels[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        bytePixels = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        intPixels = newpixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        dstScan = dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        dstOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        imageModel = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * The setPixels method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param srcX the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *        of the area of pixels to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param srcY the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *        of the area of pixels to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param srcW the width of the area of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param srcH the height of the area of pixels
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   464
     * @param model the specified {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param pixels the array of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param srcOff the offset into the pixels array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @param srcScan the distance from one row of pixels to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *        in the pixels array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see #getPixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public void setPixels(int srcX, int srcY, int srcW, int srcH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                          ColorModel model,
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   473
                          byte[] pixels, int srcOff, int srcScan) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (srcY < dstY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            int diff = dstY - srcY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (diff >= srcH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            srcOff += srcScan * diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            srcY += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            srcH -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (srcY + srcH > dstY + dstH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            srcH = (dstY + dstH) - srcY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (srcH <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (srcX < dstX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int diff = dstX - srcX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (diff >= srcW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            srcOff += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            srcX += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            srcW -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (srcX + srcW > dstX + dstW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            srcW = (dstX + dstW) - srcX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (srcW <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (intPixels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (bytePixels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                bytePixels = new byte[dstW * dstH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                dstScan = dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                dstOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                imageModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } else if (imageModel != model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                convertToRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (bytePixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                for (int h = srcH; h > 0; h--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    System.arraycopy(pixels, srcOff, bytePixels, dstPtr, srcW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    srcOff += srcScan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    dstPtr += dstScan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (intPixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            int dstRem = dstScan - srcW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            int srcRem = srcScan - srcW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            for (int h = srcH; h > 0; h--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                for (int w = srcW; w > 0; w--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                srcOff += srcRem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                dstPtr += dstRem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        flags |= ImageObserver.SOMEBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * The setPixels method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param srcX the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *        of the area of pixels to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param srcY the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *        of the area of pixels to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param srcW the width of the area of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param srcH the height of the area of pixels
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   551
     * @param model the specified {@code ColorModel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param pixels the array of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param srcOff the offset into the pixels array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param srcScan the distance from one row of pixels to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *        in the pixels array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @see #getPixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public void setPixels(int srcX, int srcY, int srcW, int srcH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                          ColorModel model,
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
   560
                          int[] pixels, int srcOff, int srcScan) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (srcY < dstY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int diff = dstY - srcY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (diff >= srcH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            srcOff += srcScan * diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            srcY += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            srcH -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (srcY + srcH > dstY + dstH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            srcH = (dstY + dstH) - srcY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (srcH <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (srcX < dstX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            int diff = dstX - srcX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            if (diff >= srcW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            srcOff += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            srcX += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            srcW -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (srcX + srcW > dstX + dstW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            srcW = (dstX + dstW) - srcX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (srcW <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (intPixels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            if (bytePixels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                intPixels = new int[dstW * dstH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                dstScan = dstW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                dstOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                imageModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                convertToRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (imageModel == model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            for (int h = srcH; h > 0; h--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                System.arraycopy(pixels, srcOff, intPixels, dstPtr, srcW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                srcOff += srcScan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                dstPtr += dstScan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (imageModel != ColorModel.getRGBdefault()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                convertToRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            int dstRem = dstScan - srcW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            int srcRem = srcScan - srcW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            for (int h = srcH; h > 0; h--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                for (int w = srcW; w > 0; w--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                srcOff += srcRem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                dstPtr += dstRem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        flags |= ImageObserver.SOMEBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * The imageComplete method is part of the ImageConsumer API which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * this class must implement to retrieve the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Note: This method is intended to be called by the ImageProducer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * of the Image whose pixels are being grabbed.  Developers using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * this class to retrieve pixels from an image should avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * this method directly since that operation could result in problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * with retrieving the requested pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param status the status of image loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public synchronized void imageComplete(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        grabbing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        switch (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        case IMAGEERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            flags |= ImageObserver.ERROR | ImageObserver.ABORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        case IMAGEABORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            flags |= ImageObserver.ABORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        case STATICIMAGEDONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            flags |= ImageObserver.ALLBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        case SINGLEFRAMEDONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            flags |= ImageObserver.FRAMEBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        producer.removeConsumer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Returns the status of the pixels.  The ImageObserver flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * representing the available pixel information are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * This method and {@link #getStatus() getStatus} have the
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   661
     * same implementation, but {@code getStatus} is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * preferred method because it conforms to the convention of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * naming information-retrieval methods with the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * "getXXX".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return the bitwise OR of all relevant ImageObserver flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @see ImageObserver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @see #getStatus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public synchronized int status() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
}