| 1 | /* PixelGrabber.java -- Java class for providing image data
|
|---|
| 2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt.image;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.Image;
|
|---|
| 42 | import java.util.Hashtable;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | PixelGrabber is an ImageConsumer designed to extract a rectangular region of pixels
|
|---|
| 46 | from an Image
|
|---|
| 47 | */
|
|---|
| 48 | public class PixelGrabber implements ImageConsumer
|
|---|
| 49 | {
|
|---|
| 50 | int x, y, width, height, status, scansize, offset;
|
|---|
| 51 | ColorModel model = ColorModel.getRGBdefault();
|
|---|
| 52 | //int hints;
|
|---|
| 53 | //Hashtable props;
|
|---|
| 54 | int pixel_bufferi[];
|
|---|
| 55 | byte pixel_bufferb[];
|
|---|
| 56 | boolean grabbing;
|
|---|
| 57 | ImageProducer ip;
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Create a PixelGrabber used to grab pixels from the specified Image
|
|---|
| 61 | * in the specified rectangle
|
|---|
| 62 | *
|
|---|
| 63 | * @param img the Image to grab pixels from
|
|---|
| 64 | * @param x the x coordinate of the rectangle
|
|---|
| 65 | * @param y the y coordinate of the rectangle
|
|---|
| 66 | * @param w the width of the rectangle
|
|---|
| 67 | * @param h the height of the rectangle
|
|---|
| 68 | * @param pixels the array of pixel values
|
|---|
| 69 | * @param offset the index of the first pixels in the <code>pixels</code> array
|
|---|
| 70 | * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
|
|---|
| 71 | */
|
|---|
| 72 | public PixelGrabber(Image img, int x, int y, int w, int h,
|
|---|
| 73 | int pix[], int off, int scansize)
|
|---|
| 74 | {
|
|---|
| 75 | this( img.getSource(), x, y, w, h, pix, off, scansize );
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Create a PixelGrabber used to grab pixels from the specified ImageProducer
|
|---|
| 80 | * in the specified rectangle
|
|---|
| 81 | *
|
|---|
| 82 | * @param ip the ImageProducer to grab pixels from
|
|---|
| 83 | * @param x the x coordinate of the rectangle
|
|---|
| 84 | * @param y the y coordinate of the rectangle
|
|---|
| 85 | * @param w the width of the rectangle
|
|---|
| 86 | * @param h the height of the rectangle
|
|---|
| 87 | * @param pixels the array of pixel values
|
|---|
| 88 | * @param offset the index of the first pixels in the <code>pixels</code> array
|
|---|
| 89 | * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
|
|---|
| 90 | */
|
|---|
| 91 | public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
|
|---|
| 92 | int pix[], int off, int scansize)
|
|---|
| 93 | {
|
|---|
| 94 | this.ip = ip;
|
|---|
| 95 | this.x = x;
|
|---|
| 96 | this.y = y;
|
|---|
| 97 | this.width = w;
|
|---|
| 98 | this.height = h;
|
|---|
| 99 | this.pixel_bufferi = pix;
|
|---|
| 100 | this.offset = off;
|
|---|
| 101 | this.scansize = scansize;
|
|---|
| 102 | pixel_bufferb = new byte[pix.length * 4];
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Create a PixelGrabber used to grab pixels from the specified Image
|
|---|
| 108 | * in the specified rectangle
|
|---|
| 109 | *
|
|---|
| 110 | * @param img the Image to grab pixels from
|
|---|
| 111 | * @param x the x coordinate of the rectangle
|
|---|
| 112 | * @param y the y coordinate of the rectangle
|
|---|
| 113 | * @param w the width of the rectangle
|
|---|
| 114 | * @param h the height of the rectangle
|
|---|
| 115 | * @param forceRGB true to force conversion to RGB
|
|---|
| 116 | */
|
|---|
| 117 | public PixelGrabber(Image img,
|
|---|
| 118 | int x, int y,
|
|---|
| 119 | int w, int h,
|
|---|
| 120 | boolean forceRGB)
|
|---|
| 121 | {
|
|---|
| 122 | //FIXME
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | Start Grabbing Pixels
|
|---|
| 127 | */
|
|---|
| 128 | public synchronized void startGrabbing()
|
|---|
| 129 | {
|
|---|
| 130 | if ( grabbing == false )
|
|---|
| 131 | {
|
|---|
| 132 | grabbing = true;
|
|---|
| 133 | ip.startProduction( this );
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | Abort the grabbing of pixels
|
|---|
| 139 | */
|
|---|
| 140 | public synchronized void abortGrabbing()
|
|---|
| 141 | {
|
|---|
| 142 | if ( grabbing == true )
|
|---|
| 143 | {
|
|---|
| 144 | grabbing = false;
|
|---|
| 145 | ip.removeConsumer( this );
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | Grab the Pixels.
|
|---|
| 151 |
|
|---|
| 152 | @return true if successful
|
|---|
| 153 |
|
|---|
| 154 | @throws InterruptedExcpetion if interrupted by another thread.
|
|---|
| 155 | */
|
|---|
| 156 | public boolean grabPixels() throws InterruptedException
|
|---|
| 157 | {
|
|---|
| 158 | return grabPixels(0);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | Grab the Pixels and abort if it takes too long
|
|---|
| 163 |
|
|---|
| 164 | @return true if successful
|
|---|
| 165 |
|
|---|
| 166 | @throws InterruptedExcpetion if interrupted by another thread.
|
|---|
| 167 | or time runs out
|
|---|
| 168 | */
|
|---|
| 169 | public synchronized boolean grabPixels(long ms) throws InterruptedException
|
|---|
| 170 | {
|
|---|
| 171 | startGrabbing();
|
|---|
| 172 |
|
|---|
| 173 | if (ms < 0)
|
|---|
| 174 | return (status == ImageObserver.ALLBITS);
|
|---|
| 175 |
|
|---|
| 176 | wait(ms);
|
|---|
| 177 |
|
|---|
| 178 | if (status == ImageObserver.ALLBITS)
|
|---|
| 179 | return true;
|
|---|
| 180 | else
|
|---|
| 181 | return false;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | Get the status of the pixel grabbing representing by ImageObserver flags
|
|---|
| 186 |
|
|---|
| 187 | @return the status
|
|---|
| 188 | */
|
|---|
| 189 | public synchronized int getStatus()
|
|---|
| 190 | {
|
|---|
| 191 | return status;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /**
|
|---|
| 195 | Return width of pixel region
|
|---|
| 196 |
|
|---|
| 197 | @return width of region
|
|---|
| 198 | */
|
|---|
| 199 | public synchronized int getWidth()
|
|---|
| 200 | {
|
|---|
| 201 | return width;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | Return height of pixel region
|
|---|
| 206 |
|
|---|
| 207 | @return height of region
|
|---|
| 208 | */
|
|---|
| 209 | public synchronized int getHeight()
|
|---|
| 210 | {
|
|---|
| 211 | return height;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /**
|
|---|
| 215 | Returns the grabbed pixel buffer
|
|---|
| 216 |
|
|---|
| 217 | @return a byte or int array
|
|---|
| 218 | */
|
|---|
| 219 | public synchronized Object getPixels()
|
|---|
| 220 | {
|
|---|
| 221 | if( pixel_bufferi != null )
|
|---|
| 222 | return pixel_bufferi;
|
|---|
| 223 | return pixel_bufferb;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | Get the ColorModel of the image
|
|---|
| 228 |
|
|---|
| 229 | @return the ColorModel
|
|---|
| 230 | */
|
|---|
| 231 | public synchronized ColorModel getColorModel()
|
|---|
| 232 | {
|
|---|
| 233 | return model;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * An <code>ImageProducer</code> indicates the size of the image
|
|---|
| 238 | * being produced using this method.
|
|---|
| 239 | *
|
|---|
| 240 | * @param width the width of the image
|
|---|
| 241 | * @param height the height of the image
|
|---|
| 242 | */
|
|---|
| 243 | public void setDimensions(int width, int height)
|
|---|
| 244 | {
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * An <code>ImageProducer</code> can set a list of properties
|
|---|
| 249 | * associated with this image by using this method.
|
|---|
| 250 | *
|
|---|
| 251 | * @param props the list of properties associated with this image
|
|---|
| 252 | */
|
|---|
| 253 | public void setProperties(Hashtable props)
|
|---|
| 254 | {
|
|---|
| 255 | //this.props = props; //FIXME - DO WE NEED THIS
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /**
|
|---|
| 259 | * This <code>ColorModel</code> should indicate the model used by
|
|---|
| 260 | * the majority of calls to <code>setPixels</code>. Each call to
|
|---|
| 261 | * <code>setPixels</code> could however indicate a different
|
|---|
| 262 | * <code>ColorModel</code>.
|
|---|
| 263 | *
|
|---|
| 264 | * @param model the color model to be used most often by setPixels
|
|---|
| 265 | * @see ColorModel
|
|---|
| 266 | */
|
|---|
| 267 | public void setColorModel(ColorModel model)
|
|---|
| 268 | {
|
|---|
| 269 | this.model = model;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /**
|
|---|
| 273 | * The <code>ImageProducer</code> should call this method with a
|
|---|
| 274 | * bit mask of hints from any of <code>RANDOMPIXELORDER</code>,
|
|---|
| 275 | * <code>TOPDOWNLEFTRIGHT</code>, <code>COMPLETESCANLINES</code>,
|
|---|
| 276 | * <code>SINGLEPASS</code>, <code>SINGLEFRAME</code>.
|
|---|
| 277 | *
|
|---|
| 278 | * @param flags a bit mask of hints
|
|---|
| 279 | */
|
|---|
| 280 | public void setHints(int flags)
|
|---|
| 281 | {
|
|---|
| 282 | //hints = flags; // FIXME - DO NOT KNOW WHAT TO DO WITH THE HINTS
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * This function delivers a rectangle of pixels where any
|
|---|
| 287 | * pixel(m,n) is stored in the array as a <code>byte</code> at
|
|---|
| 288 | * index (n * scansize + m + offset).
|
|---|
| 289 | */
|
|---|
| 290 | public void setPixels(int x, int y, int w, int h,
|
|---|
| 291 | ColorModel model, byte[] pixels, int offset, int scansize)
|
|---|
| 292 | {
|
|---|
| 293 | //FIXME - I hate bytes
|
|---|
| 294 | int xp, yp;
|
|---|
| 295 | for( xp = x; xp < ( x + w); xp++ )
|
|---|
| 296 | for( yp = y; yp < (y + h); yp++ )
|
|---|
| 297 | if( xp >= this.x &&
|
|---|
| 298 | yp >= this.y &&
|
|---|
| 299 | xp <= ( this.x + this.width ) &&
|
|---|
| 300 | yp <= ( this.y + this.height ) ) {
|
|---|
| 301 | pixel_bufferb[(yp - this.y) * this.scansize + (xp - this.x) + this.offset] =
|
|---|
| 302 | pixels[ offset + yp * scansize + xp ];
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /**
|
|---|
| 308 | * This function delivers a rectangle of pixels where any
|
|---|
| 309 | * pixel(m,n) is stored in the array as an <code>int</code> at
|
|---|
| 310 | * index (n * scansize + m + offset).
|
|---|
| 311 | */
|
|---|
| 312 | public void setPixels(int x, int y, int w, int h,
|
|---|
| 313 | ColorModel model, int[] pixels, int offset, int scansize)
|
|---|
| 314 | {
|
|---|
| 315 | int xp, yp;
|
|---|
| 316 | for( xp = x; xp < ( x + w); xp++ )
|
|---|
| 317 | for( yp = y; yp < (y + h); yp++ )
|
|---|
| 318 | if( xp >= this.x &&
|
|---|
| 319 | yp >= this.y &&
|
|---|
| 320 | xp <= ( this.x + this.width ) &&
|
|---|
| 321 | yp <= ( this.y + this.height ) ) {
|
|---|
| 322 | pixel_bufferi[(yp - this.y) * this.scansize + (xp - this.x) + this.offset] =
|
|---|
| 323 | pixels[ offset + yp * scansize + xp ];
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /**
|
|---|
| 328 | * The <code>ImageProducer</code> calls this method to indicate a
|
|---|
| 329 | * single frame or the entire image is complete. The method is
|
|---|
| 330 | * also used to indicate an error in loading or producing the
|
|---|
| 331 | * image.
|
|---|
| 332 | */
|
|---|
| 333 | public synchronized void imageComplete(int status)
|
|---|
| 334 | {
|
|---|
| 335 | this.status = status;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /**
|
|---|
| 339 | @deprecated by getStatus
|
|---|
| 340 | */
|
|---|
| 341 | public synchronized int status()
|
|---|
| 342 | {
|
|---|
| 343 | return getStatus();
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | }
|
|---|